Skip to content

Commit aa32632

Browse files
committed
stage1: remove the data field from TypeInfo.Declaration
Partially implements #10706
1 parent 44b105a commit aa32632

File tree

5 files changed

+24
-207
lines changed

5 files changed

+24
-207
lines changed

lib/std/builtin.zig

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ pub const TypeId = std.meta.Tag(TypeInfo);
171171

172172
/// This data structure is used by the Zig language code generation and
173173
/// therefore must be kept in sync with the compiler implementation.
174+
/// TODO: rename to `Type` because "info" is redundant.
174175
pub const TypeInfo = union(enum) {
175176
Type: void,
176177
Void: void,
@@ -338,6 +339,7 @@ pub const TypeInfo = union(enum) {
338339

339340
/// This data structure is used by the Zig language code generation and
340341
/// therefore must be kept in sync with the compiler implementation.
342+
/// TODO rename to Param and put inside `Fn`.
341343
pub const FnArg = struct {
342344
is_generic: bool,
343345
is_noalias: bool,
@@ -385,28 +387,6 @@ pub const TypeInfo = union(enum) {
385387
pub const Declaration = struct {
386388
name: []const u8,
387389
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-
};
410390
};
411391
};
412392

src/stage1/all_types.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,10 +1713,13 @@ struct ZigFn {
17131713

17141714
bool calls_or_awaits_errorable_fn;
17151715
bool is_cold;
1716-
bool is_test;
17171716
bool is_noinline;
17181717
};
17191718

1719+
static inline bool fn_is_test(const ZigFn *fn) {
1720+
return fn->proto_node->type == NodeTypeTestDecl;
1721+
}
1722+
17201723
uint32_t fn_table_entry_hash(ZigFn*);
17211724
bool fn_table_entry_eql(ZigFn *a, ZigFn *b);
17221725

src/stage1/analyze.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3861,7 +3861,6 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
38613861
fn_table_entry->fndef_scope = create_fndef_scope(g, source_node, tld_fn->base.parent_scope, fn_table_entry);
38623862
fn_table_entry->type_entry = get_test_fn_type(g);
38633863
fn_table_entry->body_node = source_node->data.test_decl.body;
3864-
fn_table_entry->is_test = true;
38653864

38663865
g->fn_defs.append(fn_table_entry);
38673866
g->test_fns.append(fn_table_entry);

src/stage1/ir.cpp

Lines changed: 16 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -17934,7 +17934,6 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa
1793417934

1793517935
ensure_field_index(type_info_declaration_type, "name", 0);
1793617936
ensure_field_index(type_info_declaration_type, "is_pub", 1);
17937-
ensure_field_index(type_info_declaration_type, "data", 2);
1793817937

1793917938
if (!resolve_types) {
1794017939
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
1795417953
return ErrorNone;
1795517954
}
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-
1796517956
resolve_container_usingnamespace_decls(ira->codegen, decls_scope);
1796617957

17967-
// The unresolved declarations are collected in a separate queue to avoid
17968-
// modifying decl_table while iterating over it
17969-
ZigList<Tld*> resolve_decl_queue{};
17970-
17958+
// Loop through our declarations once to figure out how many declarations
17959+
// we will generate info for.
17960+
int declaration_count = 0;
1797117961
auto decl_it = decls_scope->decl_table.entry_iterator();
1797217962
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();
1800117963
while ((curr_entry = decl_it.next()) != nullptr) {
1800217964
// Skip comptime blocks and test functions.
1800317965
if (curr_entry->value->id == TldIdCompTime)
1800417966
continue;
1800517967

18006-
if (curr_entry->value->id == TldIdFn) {
18007-
ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
18008-
if (fn_entry->is_test)
18009-
continue;
17968+
if (curr_entry->value->id == TldIdFn &&
17969+
curr_entry->value->source_node->type == NodeTypeTestDecl)
17970+
{
17971+
continue;
1801017972
}
1801117973

17974+
if (curr_entry->value->resolution == TldResolutionInvalid)
17975+
return ErrorSemanticAnalyzeFail;
17976+
1801217977
declaration_count += 1;
1801317978
}
1801417979

@@ -18027,154 +17992,24 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, AstNode *source_node, ZigVa
1802717992
// Skip comptime blocks and test functions.
1802817993
if (curr_entry->value->id == TldIdCompTime) {
1802917994
continue;
18030-
} else if (curr_entry->value->id == TldIdFn) {
18031-
ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
18032-
if (fn_entry->is_test)
18033-
continue;
17995+
}
17996+
if (curr_entry->value->id == TldIdFn &&
17997+
curr_entry->value->source_node->type == NodeTypeTestDecl)
17998+
{
17999+
continue;
1803418000
}
1803518001

1803618002
ZigValue *declaration_val = &declaration_array->data.x_array.data.s_none.elements[declaration_index];
1803718003

1803818004
declaration_val->special = ConstValSpecialStatic;
1803918005
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);
1804218008
ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee;
1804318009
init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true, nullptr);
1804418010
inner_fields[1]->special = ConstValSpecialStatic;
1804518011
inner_fields[1]->type = ira->codegen->builtin_types.entry_bool;
1804618012
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

1817918014
declaration_val->data.x_struct.fields = inner_fields;
1818018015
declaration_index += 1;

src/translate_c.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub fn translate(
408408
context.pattern_list.deinit(gpa);
409409
}
410410

411-
inline for (meta.declarations(std.zig.c_builtins)) |decl| {
411+
inline for (@typeInfo(std.zig.c_builtins).Struct.decls) |decl| {
412412
if (decl.is_pub) {
413413
const builtin = try Tag.pub_var_simple.create(context.arena, .{
414414
.name = decl.name,
@@ -2009,7 +2009,7 @@ fn transImplicitCastExpr(
20092009
}
20102010

20112011
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| {
20132013
if (!decl.is_pub) continue;
20142014
if (std.mem.eql(u8, name, decl.name)) return true;
20152015
}

0 commit comments

Comments
 (0)