| author | |
| committer | |
| log | 0298442100b2d5707099f09d29af554e8c6ac87e |
| tree | c0eefd69483492a922bfcb0ae2bb85db76eed111 |
| parent | 44b105a38d966a43dcd407b971ea39fe26353eab |
| parent | 4d22fa5a2a4afce59b91efc7a6142ec633f0bd4d |
| signature |
stage1: remove the `data` field from `TypeInfo.Declaration`13 files changed, 113 insertions(+), 268 deletions(-)
lib/std/builtin.zig+2-22| ... | @@ -171,6 +171,7 @@ pub const TypeId = std.meta.Tag(TypeInfo); | ... | @@ -171,6 +171,7 @@ pub const TypeId = std.meta.Tag(TypeInfo); |
| 171 | 171 | ||
| 172 | /// This data structure is used by the Zig language code generation and | 172 | /// This data structure is used by the Zig language code generation and |
| 173 | /// therefore must be kept in sync with the compiler implementation. | 173 | /// therefore must be kept in sync with the compiler implementation. |
| 174 | /// TODO: rename to `Type` because "info" is redundant. | ||
| 174 | pub const TypeInfo = union(enum) { | 175 | pub const TypeInfo = union(enum) { |
| 175 | Type: void, | 176 | Type: void, |
| 176 | Void: void, | 177 | Void: void, |
| ... | @@ -338,6 +339,7 @@ pub const TypeInfo = union(enum) { | ... | @@ -338,6 +339,7 @@ pub const TypeInfo = union(enum) { |
| 338 | 339 | ||
| 339 | /// This data structure is used by the Zig language code generation and | 340 | /// This data structure is used by the Zig language code generation and |
| 340 | /// therefore must be kept in sync with the compiler implementation. | 341 | /// therefore must be kept in sync with the compiler implementation. |
| 342 | /// TODO rename to Param and put inside `Fn`. | ||
| 341 | pub const FnArg = struct { | 343 | pub const FnArg = struct { |
| 342 | is_generic: bool, | 344 | is_generic: bool, |
| 343 | is_noalias: bool, | 345 | is_noalias: bool, |
| ... | @@ -385,28 +387,6 @@ pub const TypeInfo = union(enum) { | ... | @@ -385,28 +387,6 @@ pub const TypeInfo = union(enum) { |
| 385 | pub const Declaration = struct { | 387 | pub const Declaration = struct { |
| 386 | name: []const u8, | 388 | name: []const u8, |
| 387 | is_pub: bool, | 389 | is_pub: bool, |
| 388 | data: Data, | ||
| 389 | |||
| 390 | /// This data structure is used by the Zig language code generation and | ||
| 391 | /// therefore must be kept in sync with the compiler implementation. | ||
| 392 | pub const Data = union(enum) { | ||
| 393 | Type: type, | ||
| 394 | Var: type, | ||
| 395 | Fn: FnDecl, | ||
| 396 | |||
| 397 | /// This data structure is used by the Zig language code generation and | ||
| 398 | /// therefore must be kept in sync with the compiler implementation. | ||
| 399 | pub const FnDecl = struct { | ||
| 400 | fn_type: type, | ||
| 401 | is_noinline: bool, | ||
| 402 | is_var_args: bool, | ||
| 403 | is_extern: bool, | ||
| 404 | is_export: bool, | ||
| 405 | lib_name: ?[]const u8, | ||
| 406 | return_type: type, | ||
| 407 | arg_names: []const []const u8, | ||
| 408 | }; | ||
| 409 | }; | ||
| 410 | }; | 390 | }; |
| 411 | }; | 391 | }; |
| 412 | 392 |
lib/std/crypto.zig+83-21| ... | @@ -164,31 +164,93 @@ const std = @import("std.zig"); | ... | @@ -164,31 +164,93 @@ const std = @import("std.zig"); |
| 164 | 164 | ||
| 165 | pub const errors = @import("crypto/errors.zig"); | 165 | pub const errors = @import("crypto/errors.zig"); |
| 166 | 166 | ||
| 167 | test "crypto" { | 167 | test { |
| 168 | const please_windows_dont_oom = @import("builtin").os.tag == .windows; | 168 | const please_windows_dont_oom = @import("builtin").os.tag == .windows; |
| 169 | if (please_windows_dont_oom) return error.SkipZigTest; | 169 | if (please_windows_dont_oom) return error.SkipZigTest; |
| 170 | 170 | ||
| 171 | inline for (std.meta.declarations(@This())) |decl| { | 171 | _ = aead.aegis.Aegis128L; |
| 172 | switch (decl.data) { | 172 | _ = aead.aegis.Aegis256; |
| 173 | .Type => |t| { | 173 | |
| 174 | if (@typeInfo(t) != .ErrorSet) { | 174 | _ = aead.aes_gcm.Aes128Gcm; |
| 175 | std.testing.refAllDecls(t); | 175 | _ = aead.aes_gcm.Aes256Gcm; |
| 176 | } | 176 | |
| 177 | }, | 177 | _ = aead.aes_ocb.Aes128Ocb; |
| 178 | .Var => |v| { | 178 | _ = aead.aes_ocb.Aes256Ocb; |
| 179 | _ = v; | 179 | |
| 180 | }, | 180 | _ = aead.Gimli; |
| 181 | .Fn => |f| { | 181 | |
| 182 | _ = f; | 182 | _ = aead.chacha_poly.ChaCha20Poly1305; |
| 183 | }, | 183 | _ = aead.chacha_poly.ChaCha12Poly1305; |
| 184 | } | 184 | _ = aead.chacha_poly.ChaCha8Poly1305; |
| 185 | } | 185 | _ = aead.chacha_poly.XChaCha20Poly1305; |
| 186 | _ = aead.chacha_poly.XChaCha12Poly1305; | ||
| 187 | _ = aead.chacha_poly.XChaCha8Poly1305; | ||
| 188 | |||
| 189 | _ = aead.isap; | ||
| 190 | _ = aead.salsa_poly.XSalsa20Poly1305; | ||
| 191 | |||
| 192 | _ = auth.hmac; | ||
| 193 | _ = auth.siphash; | ||
| 194 | |||
| 195 | _ = core.aes; | ||
| 196 | _ = core.Gimli; | ||
| 197 | _ = core.modes; | ||
| 198 | |||
| 199 | _ = dh.X25519; | ||
| 200 | |||
| 201 | _ = ecc.Curve25519; | ||
| 202 | _ = ecc.Edwards25519; | ||
| 203 | _ = ecc.P256; | ||
| 204 | _ = ecc.Ristretto255; | ||
| 205 | |||
| 206 | _ = hash.blake2; | ||
| 207 | _ = hash.Blake3; | ||
| 208 | _ = hash.Gimli; | ||
| 209 | _ = hash.Md5; | ||
| 210 | _ = hash.Sha1; | ||
| 211 | _ = hash.sha2; | ||
| 212 | _ = hash.sha3; | ||
| 213 | |||
| 214 | _ = kdf.hkdf; | ||
| 215 | |||
| 216 | _ = onetimeauth.Ghash; | ||
| 217 | _ = onetimeauth.Poly1305; | ||
| 218 | |||
| 219 | _ = pwhash.Encoding; | ||
| 220 | |||
| 221 | _ = pwhash.Error; | ||
| 222 | _ = pwhash.HasherError; | ||
| 223 | _ = pwhash.KdfError; | ||
| 224 | |||
| 225 | _ = pwhash.argon2; | ||
| 226 | _ = pwhash.bcrypt; | ||
| 227 | _ = pwhash.scrypt; | ||
| 228 | _ = pwhash.pbkdf2; | ||
| 229 | |||
| 230 | _ = pwhash.phc_format; | ||
| 231 | |||
| 232 | _ = sign.Ed25519; | ||
| 233 | |||
| 234 | _ = stream.chacha.ChaCha20IETF; | ||
| 235 | _ = stream.chacha.ChaCha12IETF; | ||
| 236 | _ = stream.chacha.ChaCha8IETF; | ||
| 237 | _ = stream.chacha.ChaCha20With64BitNonce; | ||
| 238 | _ = stream.chacha.ChaCha12With64BitNonce; | ||
| 239 | _ = stream.chacha.ChaCha8With64BitNonce; | ||
| 240 | _ = stream.chacha.XChaCha20IETF; | ||
| 241 | _ = stream.chacha.XChaCha12IETF; | ||
| 242 | _ = stream.chacha.XChaCha8IETF; | ||
| 243 | |||
| 244 | _ = stream.salsa.Salsa20; | ||
| 245 | _ = stream.salsa.XSalsa20; | ||
| 246 | |||
| 247 | _ = nacl.Box; | ||
| 248 | _ = nacl.SecretBox; | ||
| 249 | _ = nacl.SealedBox; | ||
| 186 | 250 | ||
| 187 | _ = @import("crypto/aegis.zig"); | 251 | _ = utils; |
| 188 | _ = @import("crypto/aes_gcm.zig"); | 252 | _ = random; |
| 189 | _ = @import("crypto/aes_ocb.zig"); | 253 | _ = errors; |
| 190 | _ = @import("crypto/blake2.zig"); | ||
| 191 | _ = @import("crypto/chacha20.zig"); | ||
| 192 | } | 254 | } |
| 193 | 255 | ||
| 194 | test "CSPRNG" { | 256 | test "CSPRNG" { |
lib/std/meta.zig+2| ... | @@ -361,6 +361,8 @@ test "std.meta.containerLayout" { | ... | @@ -361,6 +361,8 @@ test "std.meta.containerLayout" { |
| 361 | try testing.expect(containerLayout(U3) == .Extern); | 361 | try testing.expect(containerLayout(U3) == .Extern); |
| 362 | } | 362 | } |
| 363 | 363 | ||
| 364 | /// Instead of this function, prefer to use e.g. `@TypeInfo(foo).Struct.decls` | ||
| 365 | /// directly when you know what kind of type it is. | ||
| 364 | pub fn declarations(comptime T: type) []const TypeInfo.Declaration { | 366 | pub fn declarations(comptime T: type) []const TypeInfo.Declaration { |
| 365 | return switch (@typeInfo(T)) { | 367 | return switch (@typeInfo(T)) { |
| 366 | .Struct => |info| info.decls, | 368 | .Struct => |info| info.decls, |
lib/std/target.zig+1-1| ... | @@ -1236,7 +1236,7 @@ pub const Target = struct { | ... | @@ -1236,7 +1236,7 @@ pub const Target = struct { |
| 1236 | } | 1236 | } |
| 1237 | 1237 | ||
| 1238 | fn allCpusFromDecls(comptime cpus: type) []const *const Cpu.Model { | 1238 | fn allCpusFromDecls(comptime cpus: type) []const *const Cpu.Model { |
| 1239 | const decls = std.meta.declarations(cpus); | 1239 | const decls = @typeInfo(cpus).Struct.decls; |
| 1240 | var array: [decls.len]*const Cpu.Model = undefined; | 1240 | var array: [decls.len]*const Cpu.Model = undefined; |
| 1241 | for (decls) |decl, i| { | 1241 | for (decls) |decl, i| { |
| 1242 | array[i] = &@field(cpus, decl.name); | 1242 | array[i] = &@field(cpus, decl.name); |
lib/std/testing.zig+1-1| ... | @@ -465,7 +465,7 @@ test { | ... | @@ -465,7 +465,7 @@ test { |
| 465 | /// Given a type, reference all the declarations inside, so that the semantic analyzer sees them. | 465 | /// Given a type, reference all the declarations inside, so that the semantic analyzer sees them. |
| 466 | pub fn refAllDecls(comptime T: type) void { | 466 | pub fn refAllDecls(comptime T: type) void { |
| 467 | if (!builtin.is_test) return; | 467 | if (!builtin.is_test) return; |
| 468 | inline for (std.meta.declarations(T)) |decl| { | 468 | inline for (comptime std.meta.declarations(T)) |decl| { |
| 469 | _ = decl; | 469 | _ = decl; |
| 470 | } | 470 | } |
| 471 | } | 471 | } |
src/stage1/all_types.hpp+4-1| ... | @@ -1713,10 +1713,13 @@ struct ZigFn { | ... | @@ -1713,10 +1713,13 @@ struct ZigFn { |
| 1713 | 1713 | ||
| 1714 | bool calls_or_awaits_errorable_fn; | 1714 | bool calls_or_awaits_errorable_fn; |
| 1715 | bool is_cold; | 1715 | bool is_cold; |
| 1716 | bool is_test; | ||
| 1717 | bool is_noinline; | 1716 | bool is_noinline; |
| 1718 | }; | 1717 | }; |
| 1719 | 1718 | ||
| 1719 | static inline bool fn_is_test(const ZigFn *fn) { | ||
| 1720 | return fn->proto_node->type == NodeTypeTestDecl; | ||
| 1721 | } | ||
| 1722 | |||
| 1720 | uint32_t fn_table_entry_hash(ZigFn*); | 1723 | uint32_t fn_table_entry_hash(ZigFn*); |
| 1721 | bool fn_table_entry_eql(ZigFn *a, ZigFn *b); | 1724 | bool fn_table_entry_eql(ZigFn *a, ZigFn *b); |
| 1722 | 1725 |
src/stage1/analyze.cpp-1| ... | @@ -3861,7 +3861,6 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { | ... | @@ -3861,7 +3861,6 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { |
| 3861 | fn_table_entry->fndef_scope = create_fndef_scope(g, source_node, tld_fn->base.parent_scope, fn_table_entry); | 3861 | fn_table_entry->fndef_scope = create_fndef_scope(g, source_node, tld_fn->base.parent_scope, fn_table_entry); |
| 3862 | fn_table_entry->type_entry = get_test_fn_type(g); | 3862 | fn_table_entry->type_entry = get_test_fn_type(g); |
| 3863 | fn_table_entry->body_node = source_node->data.test_decl.body; | 3863 | fn_table_entry->body_node = source_node->data.test_decl.body; |
| 3864 | fn_table_entry->is_test = true; | ||
| 3865 | 3864 | ||
| 3866 | g->fn_defs.append(fn_table_entry); | 3865 | g->fn_defs.append(fn_table_entry); |
| 3867 | g->test_fns.append(fn_table_entry); | 3866 | g->test_fns.append(fn_table_entry); |
src/stage1/ir.cpp+16-181| ... | @@ -17934,7 +17934,6 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa | ... | @@ -17934,7 +17934,6 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa |
| 17934 | 17934 | ||
| 17935 | ensure_field_index(type_info_declaration_type, "name", 0); | 17935 | ensure_field_index(type_info_declaration_type, "name", 0); |
| 17936 | ensure_field_index(type_info_declaration_type, "is_pub", 1); | 17936 | ensure_field_index(type_info_declaration_type, "is_pub", 1); |
| 17937 | ensure_field_index(type_info_declaration_type, "data", 2); | ||
| 17938 | 17937 | ||
| 17939 | if (!resolve_types) { | 17938 | if (!resolve_types) { |
| 17940 | ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, type_info_declaration_type, | 17939 | ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, type_info_declaration_type, |
| ... | @@ -17954,61 +17953,27 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa | ... | @@ -17954,61 +17953,27 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa |
| 17954 | return ErrorNone; | 17953 | return ErrorNone; |
| 17955 | } | 17954 | } |
| 17956 | 17955 | ||
| 17957 | ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type); | ||
| 17958 | if ((err = type_resolve(ira->codegen, type_info_declaration_data_type, ResolveStatusSizeKnown))) | ||
| 17959 | return err; | ||
| 17960 | |||
| 17961 | ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type); | ||
| 17962 | if ((err = type_resolve(ira->codegen, type_info_fn_decl_type, ResolveStatusSizeKnown))) | ||
| 17963 | return err; | ||
| 17964 | |||
| 17965 | resolve_container_usingnamespace_decls(ira->codegen, decls_scope); | 17956 | resolve_container_usingnamespace_decls(ira->codegen, decls_scope); |
| 17966 | 17957 | ||
| 17967 | // The unresolved declarations are collected in a separate queue to avoid | 17958 | // Loop through our declarations once to figure out how many declarations |
| 17968 | // modifying decl_table while iterating over it | 17959 | // we will generate info for. |
| 17969 | ZigList<Tld*> resolve_decl_queue{}; | 17960 | int declaration_count = 0; |
| 17970 | |||
| 17971 | auto decl_it = decls_scope->decl_table.entry_iterator(); | 17961 | auto decl_it = decls_scope->decl_table.entry_iterator(); |
| 17972 | decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr; | 17962 | decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr; |
| 17973 | while ((curr_entry = decl_it.next()) != nullptr) { | ||
| 17974 | if (curr_entry->value->resolution == TldResolutionInvalid) { | ||
| 17975 | return ErrorSemanticAnalyzeFail; | ||
| 17976 | } | ||
| 17977 | |||
| 17978 | if (curr_entry->value->resolution == TldResolutionResolving) { | ||
| 17979 | ir_error_dependency_loop(ira, source_node); | ||
| 17980 | return ErrorSemanticAnalyzeFail; | ||
| 17981 | } | ||
| 17982 | |||
| 17983 | // If the declaration is unresolved, force it to be resolved again. | ||
| 17984 | if (curr_entry->value->resolution == TldResolutionUnresolved) | ||
| 17985 | resolve_decl_queue.append(curr_entry->value); | ||
| 17986 | } | ||
| 17987 | |||
| 17988 | for (size_t i = 0; i < resolve_decl_queue.length; i++) { | ||
| 17989 | Tld *decl = resolve_decl_queue.at(i); | ||
| 17990 | resolve_top_level_decl(ira->codegen, decl, decl->source_node, false); | ||
| 17991 | if (decl->resolution == TldResolutionInvalid) { | ||
| 17992 | return ErrorSemanticAnalyzeFail; | ||
| 17993 | } | ||
| 17994 | } | ||
| 17995 | |||
| 17996 | resolve_decl_queue.deinit(); | ||
| 17997 | |||
| 17998 | // Loop through our declarations once to figure out how many declarations we will generate info for. | ||
| 17999 | int declaration_count = 0; | ||
| 18000 | decl_it = decls_scope->decl_table.entry_iterator(); | ||
| 18001 | while ((curr_entry = decl_it.next()) != nullptr) { | 17963 | while ((curr_entry = decl_it.next()) != nullptr) { |
| 18002 | // Skip comptime blocks and test functions. | 17964 | // Skip comptime blocks and test functions. |
| 18003 | if (curr_entry->value->id == TldIdCompTime) | 17965 | if (curr_entry->value->id == TldIdCompTime) |
| 18004 | continue; | 17966 | continue; |
| 18005 | 17967 | ||
| 18006 | if (curr_entry->value->id == TldIdFn) { | 17968 | if (curr_entry->value->id == TldIdFn && |
| 18007 | ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry; | 17969 | curr_entry->value->source_node->type == NodeTypeTestDecl) |
| 18008 | if (fn_entry->is_test) | 17970 | { |
| 18009 | continue; | 17971 | continue; |
| 18010 | } | 17972 | } |
| 18011 | 17973 | ||
| 17974 | if (curr_entry->value->resolution == TldResolutionInvalid) | ||
| 17975 | return ErrorSemanticAnalyzeFail; | ||
| 17976 | |||
| 18012 | declaration_count += 1; | 17977 | declaration_count += 1; |
| 18013 | } | 17978 | } |
| 18014 | 17979 | ||
| ... | @@ -18027,10 +17992,11 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa | ... | @@ -18027,10 +17992,11 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa |
| 18027 | // Skip comptime blocks and test functions. | 17992 | // Skip comptime blocks and test functions. |
| 18028 | if (curr_entry->value->id == TldIdCompTime) { | 17993 | if (curr_entry->value->id == TldIdCompTime) { |
| 18029 | continue; | 17994 | continue; |
| 18030 | } else if (curr_entry->value->id == TldIdFn) { | 17995 | } |
| 18031 | ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry; | 17996 | if (curr_entry->value->id == TldIdFn && |
| 18032 | if (fn_entry->is_test) | 17997 | curr_entry->value->source_node->type == NodeTypeTestDecl) |
| 18033 | continue; | 17998 | { |
| 17999 | continue; | ||
| 18034 | } | 18000 | } |
| 18035 | 18001 | ||
| 18036 | ZigValue *declaration_val = &declaration_array->data.x_array.data.s_none.elements[declaration_index]; | 18002 | ZigValue *declaration_val = &declaration_array->data.x_array.data.s_none.elements[declaration_index]; |
| ... | @@ -18038,143 +18004,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa | ... | @@ -18038,143 +18004,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa |
| 18038 | declaration_val->special = ConstValSpecialStatic; | 18004 | declaration_val->special = ConstValSpecialStatic; |
| 18039 | declaration_val->type = type_info_declaration_type; | 18005 | declaration_val->type = type_info_declaration_type; |
| 18040 | 18006 | ||
| 18041 | ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3); | 18007 | ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 2); |
| 18042 | ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee; | 18008 | ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee; |
| 18043 | init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true, nullptr); | 18009 | init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true, nullptr); |
| 18044 | inner_fields[1]->special = ConstValSpecialStatic; | 18010 | inner_fields[1]->special = ConstValSpecialStatic; |
| 18045 | inner_fields[1]->type = ira->codegen->builtin_types.entry_bool; | 18011 | inner_fields[1]->type = ira->codegen->builtin_types.entry_bool; |
| 18046 | inner_fields[1]->data.x_bool = curr_entry->value->visib_mod == VisibModPub; | 18012 | inner_fields[1]->data.x_bool = curr_entry->value->visib_mod == VisibModPub; |
| 18047 | inner_fields[2]->special = ConstValSpecialStatic; | ||
| 18048 | inner_fields[2]->type = type_info_declaration_data_type; | ||
| 18049 | inner_fields[2]->parent.id = ConstParentIdStruct; | ||
| 18050 | inner_fields[2]->parent.data.p_struct.struct_val = declaration_val; | ||
| 18051 | inner_fields[2]->parent.data.p_struct.field_index = 1; | ||
| 18052 | |||
| 18053 | switch (curr_entry->value->id) { | ||
| 18054 | case TldIdVar: | ||
| 18055 | { | ||
| 18056 | ZigVar *var = ((TldVar *)curr_entry->value)->var; | ||
| 18057 | assert(var != nullptr); | ||
| 18058 | |||
| 18059 | if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown))) | ||
| 18060 | return ErrorSemanticAnalyzeFail; | ||
| 18061 | |||
| 18062 | if (var->const_value->type->id == ZigTypeIdMetaType) { | ||
| 18063 | // We have a variable of type 'type', so it's actually a type declaration. | ||
| 18064 | // 0: Data.Type: type | ||
| 18065 | bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 0); | ||
| 18066 | inner_fields[2]->data.x_union.payload = var->const_value; | ||
| 18067 | } else { | ||
| 18068 | // We have a variable of another type, so we store the type of the variable. | ||
| 18069 | // 1: Data.Var: type | ||
| 18070 | bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 1); | ||
| 18071 | |||
| 18072 | ZigValue *payload = ira->codegen->pass1_arena->create<ZigValue>(); | ||
| 18073 | payload->special = ConstValSpecialStatic; | ||
| 18074 | payload->type = ira->codegen->builtin_types.entry_type; | ||
| 18075 | payload->data.x_type = var->const_value->type; | ||
| 18076 | |||
| 18077 | inner_fields[2]->data.x_union.payload = payload; | ||
| 18078 | } | ||
| 18079 | |||
| 18080 | break; | ||
| 18081 | } | ||
| 18082 | case TldIdFn: | ||
| 18083 | { | ||
| 18084 | // 2: Data.Fn: Data.FnDecl | ||
| 18085 | bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 2); | ||
| 18086 | |||
| 18087 | ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry; | ||
| 18088 | assert(!fn_entry->is_test); | ||
| 18089 | assert(fn_entry->type_entry != nullptr); | ||
| 18090 | |||
| 18091 | AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto; | ||
| 18092 | |||
| 18093 | ZigValue *fn_decl_val = ira->codegen->pass1_arena->create<ZigValue>(); | ||
| 18094 | fn_decl_val->special = ConstValSpecialStatic; | ||
| 18095 | fn_decl_val->type = type_info_fn_decl_type; | ||
| 18096 | fn_decl_val->parent.id = ConstParentIdUnion; | ||
| 18097 | fn_decl_val->parent.data.p_union.union_val = inner_fields[2]; | ||
| 18098 | |||
| 18099 | ZigValue **fn_decl_fields = alloc_const_vals_ptrs(ira->codegen, 9); | ||
| 18100 | fn_decl_val->data.x_struct.fields = fn_decl_fields; | ||
| 18101 | |||
| 18102 | // fn_type: type | ||
| 18103 | ensure_field_index(fn_decl_val->type, "fn_type", 0); | ||
| 18104 | fn_decl_fields[0]->special = ConstValSpecialStatic; | ||
| 18105 | fn_decl_fields[0]->type = ira->codegen->builtin_types.entry_type; | ||
| 18106 | fn_decl_fields[0]->data.x_type = fn_entry->type_entry; | ||
| 18107 | // is_noinline: bool | ||
| 18108 | ensure_field_index(fn_decl_val->type, "is_noinline", 1); | ||
| 18109 | fn_decl_fields[1]->special = ConstValSpecialStatic; | ||
| 18110 | fn_decl_fields[1]->type = ira->codegen->builtin_types.entry_bool; | ||
| 18111 | fn_decl_fields[1]->data.x_bool = fn_entry->is_noinline; | ||
| 18112 | // is_var_args: bool | ||
| 18113 | ensure_field_index(fn_decl_val->type, "is_var_args", 2); | ||
| 18114 | bool is_varargs = fn_node->is_var_args; | ||
| 18115 | fn_decl_fields[2]->special = ConstValSpecialStatic; | ||
| 18116 | fn_decl_fields[2]->type = ira->codegen->builtin_types.entry_bool; | ||
| 18117 | fn_decl_fields[2]->data.x_bool = is_varargs; | ||
| 18118 | // is_extern: bool | ||
| 18119 | ensure_field_index(fn_decl_val->type, "is_extern", 3); | ||
| 18120 | fn_decl_fields[3]->special = ConstValSpecialStatic; | ||
| 18121 | fn_decl_fields[3]->type = ira->codegen->builtin_types.entry_bool; | ||
| 18122 | fn_decl_fields[3]->data.x_bool = fn_node->is_extern; | ||
| 18123 | // is_export: bool | ||
| 18124 | ensure_field_index(fn_decl_val->type, "is_export", 4); | ||
| 18125 | fn_decl_fields[4]->special = ConstValSpecialStatic; | ||
| 18126 | fn_decl_fields[4]->type = ira->codegen->builtin_types.entry_bool; | ||
| 18127 | fn_decl_fields[4]->data.x_bool = fn_node->is_export; | ||
| 18128 | // lib_name: ?[]const u8 | ||
| 18129 | ensure_field_index(fn_decl_val->type, "lib_name", 5); | ||
| 18130 | fn_decl_fields[5]->special = ConstValSpecialStatic; | ||
| 18131 | ZigType *u8_ptr = get_pointer_to_type_extra( | ||
| 18132 | ira->codegen, ira->codegen->builtin_types.entry_u8, | ||
| 18133 | true, false, PtrLenUnknown, | ||
| 18134 | 0, 0, 0, false); | ||
| 18135 | fn_decl_fields[5]->type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr)); | ||
| 18136 | if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) { | ||
| 18137 | ZigValue *slice_val = ira->codegen->pass1_arena->create<ZigValue>(); | ||
| 18138 | ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee; | ||
| 18139 | init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true, nullptr); | ||
| 18140 | set_optional_payload(fn_decl_fields[5], slice_val); | ||
| 18141 | } else { | ||
| 18142 | set_optional_payload(fn_decl_fields[5], nullptr); | ||
| 18143 | } | ||
| 18144 | // return_type: type | ||
| 18145 | ensure_field_index(fn_decl_val->type, "return_type", 6); | ||
| 18146 | fn_decl_fields[6]->special = ConstValSpecialStatic; | ||
| 18147 | fn_decl_fields[6]->type = ira->codegen->builtin_types.entry_type; | ||
| 18148 | fn_decl_fields[6]->data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type; | ||
| 18149 | // arg_names: [][] const u8 | ||
| 18150 | ensure_field_index(fn_decl_val->type, "arg_names", 7); | ||
| 18151 | size_t fn_arg_count = fn_entry->variable_list.length; | ||
| 18152 | ZigValue *fn_arg_name_array = ira->codegen->pass1_arena->create<ZigValue>(); | ||
| 18153 | fn_arg_name_array->special = ConstValSpecialStatic; | ||
| 18154 | fn_arg_name_array->type = get_array_type(ira->codegen, | ||
| 18155 | get_slice_type(ira->codegen, u8_ptr), fn_arg_count, nullptr); | ||
| 18156 | fn_arg_name_array->data.x_array.special = ConstArraySpecialNone; | ||
| 18157 | fn_arg_name_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count); | ||
| 18158 | |||
| 18159 | init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false, nullptr); | ||
| 18160 | |||
| 18161 | for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) { | ||
| 18162 | ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index); | ||
| 18163 | ZigValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.data.s_none.elements[fn_arg_index]; | ||
| 18164 | ZigValue *arg_name = create_const_str_lit(ira->codegen, | ||
| 18165 | buf_create_from_str(arg_var->name))->data.x_ptr.data.ref.pointee; | ||
| 18166 | init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, strlen(arg_var->name), true, nullptr); | ||
| 18167 | fn_arg_name_val->parent.id = ConstParentIdArray; | ||
| 18168 | fn_arg_name_val->parent.data.p_array.array_val = fn_arg_name_array; | ||
| 18169 | fn_arg_name_val->parent.data.p_array.elem_index = fn_arg_index; | ||
| 18170 | } | ||
| 18171 | |||
| 18172 | inner_fields[2]->data.x_union.payload = fn_decl_val; | ||
| 18173 | break; | ||
| 18174 | } | ||
| 18175 | default: | ||
| 18176 | zig_unreachable(); | ||
| 18177 | } | ||
| 18178 | 18013 | ||
| 18179 | declaration_val->data.x_struct.fields = inner_fields; | 18014 | declaration_val->data.x_struct.fields = inner_fields; |
| 18180 | declaration_index += 1; | 18015 | declaration_index += 1; |
src/translate_c.zig+2-2| ... | @@ -408,7 +408,7 @@ pub fn translate( | ... | @@ -408,7 +408,7 @@ pub fn translate( |
| 408 | context.pattern_list.deinit(gpa); | 408 | context.pattern_list.deinit(gpa); |
| 409 | } | 409 | } |
| 410 | 410 | ||
| 411 | inline for (meta.declarations(std.zig.c_builtins)) |decl| { | 411 | inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| { |
| 412 | if (decl.is_pub) { | 412 | if (decl.is_pub) { |
| 413 | const builtin = try Tag.pub_var_simple.create(context.arena, .{ | 413 | const builtin = try Tag.pub_var_simple.create(context.arena, .{ |
| 414 | .name = decl.name, | 414 | .name = decl.name, |
| ... | @@ -2009,7 +2009,7 @@ fn transImplicitCastExpr( | ... | @@ -2009,7 +2009,7 @@ fn transImplicitCastExpr( |
| 2009 | } | 2009 | } |
| 2010 | 2010 | ||
| 2011 | fn isBuiltinDefined(name: []const u8) bool { | 2011 | fn isBuiltinDefined(name: []const u8) bool { |
| 2012 | inline for (meta.declarations(std.zig.c_builtins)) |decl| { | 2012 | inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| { |
| 2013 | if (!decl.is_pub) continue; | 2013 | if (!decl.is_pub) continue; |
| 2014 | if (std.mem.eql(u8, name, decl.name)) return true; | 2014 | if (std.mem.eql(u8, name, decl.name)) return true; |
| 2015 | } | 2015 | } |
test/behavior.zig+1| ... | @@ -149,6 +149,7 @@ test { | ... | @@ -149,6 +149,7 @@ test { |
| 149 | _ = @import("behavior/bugs/10147.zig"); | 149 | _ = @import("behavior/bugs/10147.zig"); |
| 150 | _ = @import("behavior/byteswap.zig"); | 150 | _ = @import("behavior/byteswap.zig"); |
| 151 | _ = @import("behavior/const_slice_child.zig"); | 151 | _ = @import("behavior/const_slice_child.zig"); |
| 152 | _ = @import("behavior/export_self_referential_type_info.zig"); | ||
| 152 | _ = @import("behavior/field_parent_ptr.zig"); | 153 | _ = @import("behavior/field_parent_ptr.zig"); |
| 153 | _ = @import("behavior/floatop_stage1.zig"); | 154 | _ = @import("behavior/floatop_stage1.zig"); |
| 154 | _ = @import("behavior/fn_delegation.zig"); | 155 | _ = @import("behavior/fn_delegation.zig"); |
test/behavior/export_self_referential_type_info.zig created+1| ... | @@ -0,0 +1 @@ | ||
| 1 | export const foo = @typeInfo(@This()).Struct.decls; | ||
test/behavior/type_info.zig-32| ... | @@ -286,10 +286,6 @@ fn testStruct() !void { | ... | @@ -286,10 +286,6 @@ fn testStruct() !void { |
| 286 | try expect(struct_info.Struct.fields[3].alignment == 1); | 286 | try expect(struct_info.Struct.fields[3].alignment == 1); |
| 287 | try expect(struct_info.Struct.decls.len == 2); | 287 | try expect(struct_info.Struct.decls.len == 2); |
| 288 | try expect(struct_info.Struct.decls[0].is_pub); | 288 | try expect(struct_info.Struct.decls[0].is_pub); |
| 289 | try expect(!struct_info.Struct.decls[0].data.Fn.is_extern); | ||
| 290 | try expect(struct_info.Struct.decls[0].data.Fn.lib_name == null); | ||
| 291 | try expect(struct_info.Struct.decls[0].data.Fn.return_type == void); | ||
| 292 | try expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void); | ||
| 293 | } | 289 | } |
| 294 | 290 | ||
| 295 | const TestUnpackedStruct = struct { | 291 | const TestUnpackedStruct = struct { |
| ... | @@ -420,34 +416,6 @@ test "type info: TypeId -> TypeInfo impl cast" { | ... | @@ -420,34 +416,6 @@ test "type info: TypeId -> TypeInfo impl cast" { |
| 420 | _ = comptime passTypeInfo(TypeId.Void); | 416 | _ = comptime passTypeInfo(TypeId.Void); |
| 421 | } | 417 | } |
| 422 | 418 | ||
| 423 | test "type info: extern fns with and without lib names" { | ||
| 424 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | ||
| 425 | |||
| 426 | const S = struct { | ||
| 427 | extern fn bar1() void; | ||
| 428 | extern "cool" fn bar2() void; | ||
| 429 | }; | ||
| 430 | const info = @typeInfo(S); | ||
| 431 | comptime { | ||
| 432 | for (info.Struct.decls) |decl| { | ||
| 433 | if (std.mem.eql(u8, decl.name, "bar1")) { | ||
| 434 | try expect(decl.data.Fn.lib_name == null); | ||
| 435 | } else { | ||
| 436 | try expectEqualStrings("cool", decl.data.Fn.lib_name.?); | ||
| 437 | } | ||
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | test "data field is a compile-time value" { | ||
| 443 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | ||
| 444 | |||
| 445 | const S = struct { | ||
| 446 | const Bar = @as(isize, -1); | ||
| 447 | }; | ||
| 448 | comptime try expect(@typeInfo(S).Struct.decls[0].data.Var == isize); | ||
| 449 | } | ||
| 450 | |||
| 451 | test "sentinel of opaque pointer type" { | 419 | test "sentinel of opaque pointer type" { |
| 452 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | 420 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO |
| 453 | 421 |
test/compile_errors.zig-6| ... | @@ -1199,12 +1199,6 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -1199,12 +1199,6 @@ pub fn addCases(ctx: *TestContext) !void { |
| 1199 | "tmp.zig:5:22: error: expected type 'fn([*c]u8, ...) callconv(.C) void', found 'fn([*:0]u8, ...) callconv(.C) void'", | 1199 | "tmp.zig:5:22: error: expected type 'fn([*c]u8, ...) callconv(.C) void', found 'fn([*:0]u8, ...) callconv(.C) void'", |
| 1200 | }); | 1200 | }); |
| 1201 | 1201 | ||
| 1202 | ctx.testErrStage1("dependency loop in top-level decl with @TypeInfo when accessing the decls", | ||
| 1203 | \\export const foo = @typeInfo(@This()).Struct.decls; | ||
| 1204 | , &[_][]const u8{ | ||
| 1205 | "tmp.zig:1:20: error: dependency loop detected", | ||
| 1206 | }); | ||
| 1207 | |||
| 1208 | ctx.objErrStage1("function call assigned to incorrect type", | 1202 | ctx.objErrStage1("function call assigned to incorrect type", |
| 1209 | \\export fn entry() void { | 1203 | \\export fn entry() void { |
| 1210 | \\ var arr: [4]f32 = undefined; | 1204 | \\ var arr: [4]f32 = undefined; |