authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-05-26 21:22:45+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 20:43:07-04:00
logf9e7bd2682a83bad2b74686dd9b860fc2ce7ef8f
treeeeb3fd9390da4a43471edc770b63108e5b5d9526
parent01a4897da54d6c789b0a5232188bbafbbd660946

std.meta/trait: def/definition => decl/declaration

TypeInfo: defs/Definition => decls/Declarations

7 files changed, 136 insertions(+), 190 deletions(-)

doc/langref.html.in+6-6
......@@ -7545,7 +7545,7 @@ pub const TypeInfo = union(TypeId) {
75457545 pub const Struct = struct {
75467546 layout: ContainerLayout,
75477547 fields: []StructField,
7548 defs: []Definition,
7548 decls: []Declaration,
75497549 };
75507550
75517551 pub const Optional = struct {
......@@ -7573,7 +7573,7 @@ pub const TypeInfo = union(TypeId) {
75737573 layout: ContainerLayout,
75747574 tag_type: type,
75757575 fields: []EnumField,
7576 defs: []Definition,
7576 decls: []Declaration,
75777577 };
75787578
75797579 pub const UnionField = struct {
......@@ -7586,7 +7586,7 @@ pub const TypeInfo = union(TypeId) {
75867586 layout: ContainerLayout,
75877587 tag_type: ?type,
75887588 fields: []UnionField,
7589 defs: []Definition,
7589 decls: []Declaration,
75907590 };
75917591
75927592 pub const CallingConvention = enum {
......@@ -7622,7 +7622,7 @@ pub const TypeInfo = union(TypeId) {
76227622 child: type,
76237623 };
76247624
7625 pub const Definition = struct {
7625 pub const Declaration = struct {
76267626 name: []const u8,
76277627 is_pub: bool,
76287628 data: Data,
......@@ -7630,9 +7630,9 @@ pub const TypeInfo = union(TypeId) {
76307630 pub const Data = union(enum) {
76317631 Type: type,
76327632 Var: type,
7633 Fn: FnDef,
7633 Fn: FnDecl,
76347634
7635 pub const FnDef = struct {
7635 pub const FnDecl = struct {
76367636 fn_type: type,
76377637 inline_type: Inline,
76387638 calling_convention: CallingConvention,
src/codegen.cpp+6-6
......@@ -7762,7 +7762,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
77627762 " pub const Struct = struct {\n"
77637763 " layout: ContainerLayout,\n"
77647764 " fields: []StructField,\n"
7765 " defs: []Definition,\n"
7765 " decls: []Declaration,\n"
77667766 " };\n"
77677767 "\n"
77687768 " pub const Optional = struct {\n"
......@@ -7790,7 +7790,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
77907790 " layout: ContainerLayout,\n"
77917791 " tag_type: type,\n"
77927792 " fields: []EnumField,\n"
7793 " defs: []Definition,\n"
7793 " decls: []Declaration,\n"
77947794 " };\n"
77957795 "\n"
77967796 " pub const UnionField = struct {\n"
......@@ -7803,7 +7803,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
78037803 " layout: ContainerLayout,\n"
78047804 " tag_type: ?type,\n"
78057805 " fields: []UnionField,\n"
7806 " defs: []Definition,\n"
7806 " decls: []Declaration,\n"
78077807 " };\n"
78087808 "\n"
78097809 " pub const CallingConvention = enum {\n"
......@@ -7839,7 +7839,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
78397839 " child: type,\n"
78407840 " };\n"
78417841 "\n"
7842 " pub const Definition = struct {\n"
7842 " pub const Declaration = struct {\n"
78437843 " name: []const u8,\n"
78447844 " is_pub: bool,\n"
78457845 " data: Data,\n"
......@@ -7847,9 +7847,9 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
78477847 " pub const Data = union(enum) {\n"
78487848 " Type: type,\n"
78497849 " Var: type,\n"
7850 " Fn: FnDef,\n"
7850 " Fn: FnDecl,\n"
78517851 "\n"
7852 " pub const FnDef = struct {\n"
7852 " pub const FnDecl = struct {\n"
78537853 " fn_type: type,\n"
78547854 " inline_type: Inline,\n"
78557855 " calling_convention: CallingConvention,\n"
src/ir.cpp+88-88
......@@ -18380,37 +18380,37 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1838018380 return var->const_value->data.x_type;
1838118381}
1838218382
18383static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr, ConstExprValue *out_val,
18383static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr, ConstExprValue *out_val,
1838418384 ScopeDecls *decls_scope)
1838518385{
1838618386 Error err;
18387 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
18388 if ((err = type_resolve(ira->codegen, type_info_definition_type, ResolveStatusSizeKnown)))
18387 ZigType *type_info_declaration_type = ir_type_info_get_type(ira, "Declaration", nullptr);
18388 if ((err = type_resolve(ira->codegen, type_info_declaration_type, ResolveStatusSizeKnown)))
1838918389 return err;
1839018390
18391 ensure_field_index(type_info_definition_type, "name", 0);
18392 ensure_field_index(type_info_definition_type, "is_pub", 1);
18393 ensure_field_index(type_info_definition_type, "data", 2);
18391 ensure_field_index(type_info_declaration_type, "name", 0);
18392 ensure_field_index(type_info_declaration_type, "is_pub", 1);
18393 ensure_field_index(type_info_declaration_type, "data", 2);
1839418394
18395 ZigType *type_info_definition_data_type = ir_type_info_get_type(ira, "Data", type_info_definition_type);
18396 if ((err = ensure_complete_type(ira->codegen, type_info_definition_data_type)))
18395 ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type);
18396 if ((err = ensure_complete_type(ira->codegen, type_info_declaration_data_type)))
1839718397 return err;
1839818398
18399 ZigType *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_type);
18400 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_type)))
18399 ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type);
18400 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_type)))
1840118401 return err;
1840218402
18403 ZigType *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_type);
18404 if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_inline_type)))
18403 ZigType *type_info_fn_decl_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_decl_type);
18404 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_inline_type)))
1840518405 return err;
1840618406
18407 // Loop through our definitions once to figure out how many definitions we will generate info for.
18407 // Loop through our declarations once to figure out how many declarations we will generate info for.
1840818408 auto decl_it = decls_scope->decl_table.entry_iterator();
1840918409 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
18410 int definition_count = 0;
18410 int declaration_count = 0;
1841118411
1841218412 while ((curr_entry = decl_it.next()) != nullptr) {
18413 // If the definition is unresolved, force it to be resolved again.
18413 // If the declaration is unresolved, force it to be resolved again.
1841418414 if (curr_entry->value->resolution == TldResolutionUnresolved) {
1841518415 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
1841618416 if (curr_entry->value->resolution != TldResolutionOk) {
......@@ -18426,21 +18426,21 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1842618426 continue;
1842718427 }
1842818428
18429 definition_count += 1;
18429 declaration_count += 1;
1843018430 }
1843118431 }
1843218432
18433 ConstExprValue *definition_array = create_const_vals(1);
18434 definition_array->special = ConstValSpecialStatic;
18435 definition_array->type = get_array_type(ira->codegen, type_info_definition_type, definition_count);
18436 definition_array->data.x_array.special = ConstArraySpecialNone;
18437 definition_array->data.x_array.data.s_none.elements = create_const_vals(definition_count);
18438 init_const_slice(ira->codegen, out_val, definition_array, 0, definition_count, false);
18433 ConstExprValue *declaration_array = create_const_vals(1);
18434 declaration_array->special = ConstValSpecialStatic;
18435 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count);
18436 declaration_array->data.x_array.special = ConstArraySpecialNone;
18437 declaration_array->data.x_array.data.s_none.elements = create_const_vals(declaration_count);
18438 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);
1843918439
18440 // Loop through the definitions and generate info.
18440 // Loop through the declarations and generate info.
1844118441 decl_it = decls_scope->decl_table.entry_iterator();
1844218442 curr_entry = nullptr;
18443 int definition_index = 0;
18443 int declaration_index = 0;
1844418444 while ((curr_entry = decl_it.next()) != nullptr) {
1844518445 // Skip comptime blocks and test functions.
1844618446 if (curr_entry->value->id == TldIdCompTime) {
......@@ -18451,10 +18451,10 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1845118451 continue;
1845218452 }
1845318453
18454 ConstExprValue *definition_val = &definition_array->data.x_array.data.s_none.elements[definition_index];
18454 ConstExprValue *declaration_val = &declaration_array->data.x_array.data.s_none.elements[declaration_index];
1845518455
18456 definition_val->special = ConstValSpecialStatic;
18457 definition_val->type = type_info_definition_type;
18456 declaration_val->special = ConstValSpecialStatic;
18457 declaration_val->type = type_info_declaration_type;
1845818458
1845918459 ConstExprValue *inner_fields = create_const_vals(3);
1846018460 ConstExprValue *name = create_const_str_lit(ira->codegen, curr_entry->key);
......@@ -18463,9 +18463,9 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1846318463 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
1846418464 inner_fields[1].data.x_bool = curr_entry->value->visib_mod == VisibModPub;
1846518465 inner_fields[2].special = ConstValSpecialStatic;
18466 inner_fields[2].type = type_info_definition_data_type;
18466 inner_fields[2].type = type_info_declaration_data_type;
1846718467 inner_fields[2].parent.id = ConstParentIdStruct;
18468 inner_fields[2].parent.data.p_struct.struct_val = definition_val;
18468 inner_fields[2].parent.data.p_struct.struct_val = declaration_val;
1846918469 inner_fields[2].parent.data.p_struct.field_index = 1;
1847018470
1847118471 switch (curr_entry->value->id) {
......@@ -18476,7 +18476,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1847618476 return ErrorSemanticAnalyzeFail;
1847718477
1847818478 if (var->const_value->type->id == ZigTypeIdMetaType) {
18479 // We have a variable of type 'type', so it's actually a type definition.
18479 // We have a variable of type 'type', so it's actually a type declaration.
1848018480 // 0: Data.Type: type
1848118481 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);
1848218482 inner_fields[2].data.x_union.payload = var->const_value;
......@@ -18496,7 +18496,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1849618496 }
1849718497 case TldIdFn:
1849818498 {
18499 // 2: Data.Fn: Data.FnDef
18499 // 2: Data.Fn: Data.FnDecl
1850018500 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 2);
1850118501
1850218502 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
......@@ -18509,68 +18509,68 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1850918509
1851018510 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;
1851118511
18512 ConstExprValue *fn_def_val = create_const_vals(1);
18513 fn_def_val->special = ConstValSpecialStatic;
18514 fn_def_val->type = type_info_fn_def_type;
18515 fn_def_val->parent.id = ConstParentIdUnion;
18516 fn_def_val->parent.data.p_union.union_val = &inner_fields[2];
18512 ConstExprValue *fn_decl_val = create_const_vals(1);
18513 fn_decl_val->special = ConstValSpecialStatic;
18514 fn_decl_val->type = type_info_fn_decl_type;
18515 fn_decl_val->parent.id = ConstParentIdUnion;
18516 fn_decl_val->parent.data.p_union.union_val = &inner_fields[2];
1851718517
18518 ConstExprValue *fn_def_fields = create_const_vals(9);
18519 fn_def_val->data.x_struct.fields = fn_def_fields;
18518 ConstExprValue *fn_decl_fields = create_const_vals(9);
18519 fn_decl_val->data.x_struct.fields = fn_decl_fields;
1852018520
1852118521 // fn_type: type
18522 ensure_field_index(fn_def_val->type, "fn_type", 0);
18523 fn_def_fields[0].special = ConstValSpecialStatic;
18524 fn_def_fields[0].type = ira->codegen->builtin_types.entry_type;
18525 fn_def_fields[0].data.x_type = fn_entry->type_entry;
18526 // inline_type: Data.FnDef.Inline
18527 ensure_field_index(fn_def_val->type, "inline_type", 1);
18528 fn_def_fields[1].special = ConstValSpecialStatic;
18529 fn_def_fields[1].type = type_info_fn_def_inline_type;
18530 bigint_init_unsigned(&fn_def_fields[1].data.x_enum_tag, fn_entry->fn_inline);
18522 ensure_field_index(fn_decl_val->type, "fn_type", 0);
18523 fn_decl_fields[0].special = ConstValSpecialStatic;
18524 fn_decl_fields[0].type = ira->codegen->builtin_types.entry_type;
18525 fn_decl_fields[0].data.x_type = fn_entry->type_entry;
18526 // inline_type: Data.FnDecl.Inline
18527 ensure_field_index(fn_decl_val->type, "inline_type", 1);
18528 fn_decl_fields[1].special = ConstValSpecialStatic;
18529 fn_decl_fields[1].type = type_info_fn_decl_inline_type;
18530 bigint_init_unsigned(&fn_decl_fields[1].data.x_enum_tag, fn_entry->fn_inline);
1853118531 // calling_convention: TypeInfo.CallingConvention
18532 ensure_field_index(fn_def_val->type, "calling_convention", 2);
18533 fn_def_fields[2].special = ConstValSpecialStatic;
18534 fn_def_fields[2].type = ir_type_info_get_type(ira, "CallingConvention", nullptr);
18535 bigint_init_unsigned(&fn_def_fields[2].data.x_enum_tag, fn_node->cc);
18532 ensure_field_index(fn_decl_val->type, "calling_convention", 2);
18533 fn_decl_fields[2].special = ConstValSpecialStatic;
18534 fn_decl_fields[2].type = ir_type_info_get_type(ira, "CallingConvention", nullptr);
18535 bigint_init_unsigned(&fn_decl_fields[2].data.x_enum_tag, fn_node->cc);
1853618536 // is_var_args: bool
18537 ensure_field_index(fn_def_val->type, "is_var_args", 3);
18537 ensure_field_index(fn_decl_val->type, "is_var_args", 3);
1853818538 bool is_varargs = fn_node->is_var_args;
18539 fn_def_fields[3].special = ConstValSpecialStatic;
18540 fn_def_fields[3].type = ira->codegen->builtin_types.entry_bool;
18541 fn_def_fields[3].data.x_bool = is_varargs;
18539 fn_decl_fields[3].special = ConstValSpecialStatic;
18540 fn_decl_fields[3].type = ira->codegen->builtin_types.entry_bool;
18541 fn_decl_fields[3].data.x_bool = is_varargs;
1854218542 // is_extern: bool
18543 ensure_field_index(fn_def_val->type, "is_extern", 4);
18544 fn_def_fields[4].special = ConstValSpecialStatic;
18545 fn_def_fields[4].type = ira->codegen->builtin_types.entry_bool;
18546 fn_def_fields[4].data.x_bool = fn_node->is_extern;
18543 ensure_field_index(fn_decl_val->type, "is_extern", 4);
18544 fn_decl_fields[4].special = ConstValSpecialStatic;
18545 fn_decl_fields[4].type = ira->codegen->builtin_types.entry_bool;
18546 fn_decl_fields[4].data.x_bool = fn_node->is_extern;
1854718547 // is_export: bool
18548 ensure_field_index(fn_def_val->type, "is_export", 5);
18549 fn_def_fields[5].special = ConstValSpecialStatic;
18550 fn_def_fields[5].type = ira->codegen->builtin_types.entry_bool;
18551 fn_def_fields[5].data.x_bool = fn_node->is_export;
18548 ensure_field_index(fn_decl_val->type, "is_export", 5);
18549 fn_decl_fields[5].special = ConstValSpecialStatic;
18550 fn_decl_fields[5].type = ira->codegen->builtin_types.entry_bool;
18551 fn_decl_fields[5].data.x_bool = fn_node->is_export;
1855218552 // lib_name: ?[]const u8
18553 ensure_field_index(fn_def_val->type, "lib_name", 6);
18554 fn_def_fields[6].special = ConstValSpecialStatic;
18553 ensure_field_index(fn_decl_val->type, "lib_name", 6);
18554 fn_decl_fields[6].special = ConstValSpecialStatic;
1855518555 ZigType *u8_ptr = get_pointer_to_type_extra(
1855618556 ira->codegen, ira->codegen->builtin_types.entry_u8,
1855718557 true, false, PtrLenUnknown,
1855818558 0, 0, 0, false);
18559 fn_def_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
18559 fn_decl_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
1856018560 if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {
18561 fn_def_fields[6].data.x_optional = create_const_vals(1);
18561 fn_decl_fields[6].data.x_optional = create_const_vals(1);
1856218562 ConstExprValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name);
18563 init_const_slice(ira->codegen, fn_def_fields[6].data.x_optional, lib_name, 0, buf_len(fn_node->lib_name), true);
18563 init_const_slice(ira->codegen, fn_decl_fields[6].data.x_optional, lib_name, 0, buf_len(fn_node->lib_name), true);
1856418564 } else {
18565 fn_def_fields[6].data.x_optional = nullptr;
18565 fn_decl_fields[6].data.x_optional = nullptr;
1856618566 }
1856718567 // return_type: type
18568 ensure_field_index(fn_def_val->type, "return_type", 7);
18569 fn_def_fields[7].special = ConstValSpecialStatic;
18570 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
18571 fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
18568 ensure_field_index(fn_decl_val->type, "return_type", 7);
18569 fn_decl_fields[7].special = ConstValSpecialStatic;
18570 fn_decl_fields[7].type = ira->codegen->builtin_types.entry_type;
18571 fn_decl_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
1857218572 // arg_names: [][] const u8
18573 ensure_field_index(fn_def_val->type, "arg_names", 8);
18573 ensure_field_index(fn_decl_val->type, "arg_names", 8);
1857418574 size_t fn_arg_count = fn_entry->variable_list.length;
1857518575 ConstExprValue *fn_arg_name_array = create_const_vals(1);
1857618576 fn_arg_name_array->special = ConstValSpecialStatic;
......@@ -18579,7 +18579,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1857918579 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
1858018580 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
1858118581
18582 init_const_slice(ira->codegen, &fn_def_fields[8], fn_arg_name_array, 0, fn_arg_count, false);
18582 init_const_slice(ira->codegen, &fn_decl_fields[8], fn_arg_name_array, 0, fn_arg_count, false);
1858318583
1858418584 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
1858518585 ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index);
......@@ -18591,7 +18591,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1859118591 fn_arg_name_val->parent.data.p_array.elem_index = fn_arg_index;
1859218592 }
1859318593
18594 inner_fields[2].data.x_union.payload = fn_def_val;
18594 inner_fields[2].data.x_union.payload = fn_decl_val;
1859518595 break;
1859618596 }
1859718597 case TldIdContainer:
......@@ -18615,11 +18615,11 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1861518615 zig_unreachable();
1861618616 }
1861718617
18618 definition_val->data.x_struct.fields = inner_fields;
18619 definition_index++;
18618 declaration_val->data.x_struct.fields = inner_fields;
18619 declaration_index++;
1862018620 }
1862118621
18622 assert(definition_index == definition_count);
18622 assert(declaration_index == declaration_count);
1862318623 return ErrorNone;
1862418624}
1862518625
......@@ -18927,9 +18927,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1892718927 enum_field_val->parent.data.p_array.array_val = enum_field_array;
1892818928 enum_field_val->parent.data.p_array.elem_index = enum_field_index;
1892918929 }
18930 // defs: []TypeInfo.Definition
18931 ensure_field_index(result->type, "defs", 3);
18932 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],
18930 // decls: []TypeInfo.Declaration
18931 ensure_field_index(result->type, "decls", 3);
18932 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[3],
1893318933 type_entry->data.enumeration.decls_scope)))
1893418934 {
1893518935 return err;
......@@ -19094,9 +19094,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1909419094 union_field_val->parent.data.p_array.array_val = union_field_array;
1909519095 union_field_val->parent.data.p_array.elem_index = union_field_index;
1909619096 }
19097 // defs: []TypeInfo.Definition
19098 ensure_field_index(result->type, "defs", 3);
19099 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],
19097 // decls: []TypeInfo.Declaration
19098 ensure_field_index(result->type, "decls", 3);
19099 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[3],
1910019100 type_entry->data.unionation.decls_scope)))
1910119101 {
1910219102 return err;
......@@ -19175,9 +19175,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1917519175 struct_field_val->parent.data.p_array.array_val = struct_field_array;
1917619176 struct_field_val->parent.data.p_array.elem_index = struct_field_index;
1917719177 }
19178 // defs: []TypeInfo.Definition
19179 ensure_field_index(result->type, "defs", 2);
19180 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[2],
19178 // decls: []TypeInfo.Declaration
19179 ensure_field_index(result->type, "decls", 2);
19180 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[2],
1918119181 type_entry->data.structure.decls_scope)))
1918219182 {
1918319183 return err;
std/fmt.zig+1-17
......@@ -145,23 +145,7 @@ pub fn formatType(
145145 return format(context, Errors, output, "promise@{x}", @ptrToInt(value));
146146 },
147147 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
148 const has_cust_fmt = comptime cf: {
149 const info = @typeInfo(T);
150 const defs = switch (info) {
151 builtin.TypeId.Struct => |s| s.defs,
152 builtin.TypeId.Union => |u| u.defs,
153 builtin.TypeId.Enum => |e| e.defs,
154 else => unreachable,
155 };
156
157 for (defs) |def| {
158 if (mem.eql(u8, def.name, "format")) {
159 break :cf true;
160 }
161 }
162 break :cf false;
163 };
164 if (has_cust_fmt) return value.format(fmt, context, Errors, output);
148 if (comptime std.meta.trait.hasFn("format")(T)) return value.format(fmt, context, Errors, output);
165149
166150 try output(context, @typeName(T));
167151 switch (comptime @typeId(T)) {
std/meta.zig+22-22
......@@ -160,16 +160,16 @@ test "std.meta.containerLayout" {
160160 testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern);
161161}
162162
163pub fn definitions(comptime T: type) []TypeInfo.Definition {
163pub fn declarations(comptime T: type) []TypeInfo.Declaration {
164164 return switch (@typeInfo(T)) {
165 TypeId.Struct => |info| info.defs,
166 TypeId.Enum => |info| info.defs,
167 TypeId.Union => |info| info.defs,
165 TypeId.Struct => |info| info.decls,
166 TypeId.Enum => |info| info.decls,
167 TypeId.Union => |info| info.decls,
168168 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
169169 };
170170}
171171
172test "std.meta.definitions" {
172test "std.meta.declarations" {
173173 const E1 = enum {
174174 A,
175175
......@@ -184,28 +184,28 @@ test "std.meta.definitions" {
184184 fn a() void {}
185185 };
186186
187 const defs = comptime [][]TypeInfo.Definition{
188 definitions(E1),
189 definitions(S1),
190 definitions(U1),
187 const decls = comptime [][]TypeInfo.Declaration{
188 declarations(E1),
189 declarations(S1),
190 declarations(U1),
191191 };
192192
193 inline for (defs) |def| {
194 testing.expect(def.len == 1);
195 testing.expect(comptime mem.eql(u8, def[0].name, "a"));
193 inline for (decls) |decl| {
194 testing.expect(decl.len == 1);
195 testing.expect(comptime mem.eql(u8, decl[0].name, "a"));
196196 }
197197}
198198
199pub fn definitionInfo(comptime T: type, comptime def_name: []const u8) TypeInfo.Definition {
200 inline for (comptime definitions(T)) |def| {
201 if (comptime mem.eql(u8, def.name, def_name))
202 return def;
199pub fn declarationInfo(comptime T: type, comptime decl_name: []const u8) TypeInfo.Declaration {
200 inline for (comptime declarations(T)) |decl| {
201 if (comptime mem.eql(u8, decl.name, decl_name))
202 return decl;
203203 }
204204
205 @compileError("'" ++ @typeName(T) ++ "' has no definition '" ++ def_name ++ "'");
205 @compileError("'" ++ @typeName(T) ++ "' has no declaration '" ++ decl_name ++ "'");
206206}
207207
208test "std.meta.definitionInfo" {
208test "std.meta.declarationInfo" {
209209 const E1 = enum {
210210 A,
211211
......@@ -220,10 +220,10 @@ test "std.meta.definitionInfo" {
220220 fn a() void {}
221221 };
222222
223 const infos = comptime []TypeInfo.Definition{
224 definitionInfo(E1, "a"),
225 definitionInfo(S1, "a"),
226 definitionInfo(U1, "a"),
223 const infos = comptime []TypeInfo.Declaration{
224 declarationInfo(E1, "a"),
225 declarationInfo(S1, "a"),
226 declarationInfo(U1, "a"),
227227 };
228228
229229 inline for (infos) |info| {
std/meta/trait.zig+5-43
......@@ -55,53 +55,15 @@ test "std.meta.trait.multiTrait" {
5555 testing.expect(!isVector(u8));
5656}
5757
58///
59pub fn hasDef(comptime name: []const u8) TraitFn {
60 const Closure = struct {
61 pub fn trait(comptime T: type) bool {
62 const info = @typeInfo(T);
63 const defs = switch (info) {
64 builtin.TypeId.Struct => |s| s.defs,
65 builtin.TypeId.Union => |u| u.defs,
66 builtin.TypeId.Enum => |e| e.defs,
67 else => return false,
68 };
69
70 inline for (defs) |def| {
71 if (mem.eql(u8, def.name, name)) return def.is_pub;
72 }
73
74 return false;
75 }
76 };
77 return Closure.trait;
78}
79
80test "std.meta.trait.hasDef" {
81 const TestStruct = struct {
82 pub const value = u8(16);
83 };
84
85 const TestStructFail = struct {
86 const value = u8(16);
87 };
88
89 testing.expect(hasDef("value")(TestStruct));
90 testing.expect(!hasDef("value")(TestStructFail));
91 testing.expect(!hasDef("value")(*TestStruct));
92 testing.expect(!hasDef("value")(**TestStructFail));
93 testing.expect(!hasDef("x")(TestStruct));
94 testing.expect(!hasDef("value")(u8));
95}
96
9758///
9859pub fn hasFn(comptime name: []const u8) TraitFn {
9960 const Closure = struct {
10061 pub fn trait(comptime T: type) bool {
101 if (!comptime hasDef(name)(T)) return false;
102 const DefType = @typeOf(@field(T, name));
103 const def_type_id = @typeId(DefType);
104 return def_type_id == builtin.TypeId.Fn;
62 if (!comptime isContainer(T)) return false;
63 if (!comptime @hasDecl(T, name)) return false;
64 const DeclType = @typeOf(@field(T, name));
65 const decl_type_id = @typeId(DeclType);
66 return decl_type_id == builtin.TypeId.Fn;
10567 }
10668 };
10769 return Closure.trait;
test/stage1/behavior/type_info.zig+8-8
......@@ -177,7 +177,7 @@ fn testEnum() void {
177177 expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
178178 expect(os_info.Enum.fields[3].value == 3);
179179 expect(os_info.Enum.tag_type == u2);
180 expect(os_info.Enum.defs.len == 0);
180 expect(os_info.Enum.decls.len == 0);
181181}
182182
183183test "type info: union info" {
......@@ -194,7 +194,7 @@ fn testUnion() void {
194194 expect(typeinfo_info.Union.fields[4].enum_field != null);
195195 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
196196 expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));
197 expect(typeinfo_info.Union.defs.len == 21);
197 expect(typeinfo_info.Union.decls.len == 21);
198198
199199 const TestNoTagUnion = union {
200200 Foo: void,
......@@ -232,12 +232,12 @@ fn testStruct() void {
232232 expect(struct_info.Struct.fields.len == 3);
233233 expect(struct_info.Struct.fields[1].offset == null);
234234 expect(struct_info.Struct.fields[2].field_type == *TestStruct);
235 expect(struct_info.Struct.defs.len == 2);
236 expect(struct_info.Struct.defs[0].is_pub);
237 expect(!struct_info.Struct.defs[0].data.Fn.is_extern);
238 expect(struct_info.Struct.defs[0].data.Fn.lib_name == null);
239 expect(struct_info.Struct.defs[0].data.Fn.return_type == void);
240 expect(struct_info.Struct.defs[0].data.Fn.fn_type == fn (*const TestStruct) void);
235 expect(struct_info.Struct.decls.len == 2);
236 expect(struct_info.Struct.decls[0].is_pub);
237 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
238 expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
239 expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
240 expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
241241}
242242
243243const TestStruct = packed struct {