authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-31 22:09:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-31 22:09:41-07:00
logaa326328d0b4a0d4ee8167be9b2ab25fd560b0c9
treedfe604416f201b5bff587c1ae8031669c26c3b83
parent44b105a38d966a43dcd407b971ea39fe26353eab

stage1: remove the `data` field from TypeInfo.Declaration

Partially implements #10706

5 files changed, 24 insertions(+), 207 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);
171171
172/// This data structure is used by the Zig language code generation and172/// 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.
174pub const TypeInfo = union(enum) {175pub 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) {
338339
339 /// This data structure is used by the Zig language code generation and340 /// 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};
412392
src/stage1/all_types.hpp+4-1
...@@ -1713,10 +1713,13 @@ struct ZigFn {...@@ -1713,10 +1713,13 @@ struct ZigFn {
17131713
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};
17191718
1719static inline bool fn_is_test(const ZigFn *fn) {
1720 return fn->proto_node->type == NodeTypeTestDecl;
1721}
1722
1720uint32_t fn_table_entry_hash(ZigFn*);1723uint32_t fn_table_entry_hash(ZigFn*);
1721bool fn_table_entry_eql(ZigFn *a, ZigFn *b);1724bool fn_table_entry_eql(ZigFn *a, ZigFn *b);
17221725
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;
38653864
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
1793417934
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);
1793817937
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 }
1795617955
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);
1796617957
17967 // The unresolved declarations are collected in a separate queue to avoid17958 // Loop through our declarations once to figure out how many declarations
17968 // modifying decl_table while iterating over it17959 // 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;
1800517967
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 }
1801117973
17974 if (curr_entry->value->resolution == TldResolutionInvalid)
17975 return ErrorSemanticAnalyzeFail;
17976
18012 declaration_count += 1;17977 declaration_count += 1;
18013 }17978 }
1801417979
...@@ -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 }
1803518001
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;
1804018006
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 }
1817818013
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 }
410410
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}
20102010
2011fn isBuiltinDefined(name: []const u8) bool {2011fn 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 }