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) {...@@ -7545,7 +7545,7 @@ pub const TypeInfo = union(TypeId) {
7545 pub const Struct = struct {7545 pub const Struct = struct {
7546 layout: ContainerLayout,7546 layout: ContainerLayout,
7547 fields: []StructField,7547 fields: []StructField,
7548 defs: []Definition,7548 decls: []Declaration,
7549 };7549 };
75507550
7551 pub const Optional = struct {7551 pub const Optional = struct {
...@@ -7573,7 +7573,7 @@ pub const TypeInfo = union(TypeId) {...@@ -7573,7 +7573,7 @@ pub const TypeInfo = union(TypeId) {
7573 layout: ContainerLayout,7573 layout: ContainerLayout,
7574 tag_type: type,7574 tag_type: type,
7575 fields: []EnumField,7575 fields: []EnumField,
7576 defs: []Definition,7576 decls: []Declaration,
7577 };7577 };
75787578
7579 pub const UnionField = struct {7579 pub const UnionField = struct {
...@@ -7586,7 +7586,7 @@ pub const TypeInfo = union(TypeId) {...@@ -7586,7 +7586,7 @@ pub const TypeInfo = union(TypeId) {
7586 layout: ContainerLayout,7586 layout: ContainerLayout,
7587 tag_type: ?type,7587 tag_type: ?type,
7588 fields: []UnionField,7588 fields: []UnionField,
7589 defs: []Definition,7589 decls: []Declaration,
7590 };7590 };
75917591
7592 pub const CallingConvention = enum {7592 pub const CallingConvention = enum {
...@@ -7622,7 +7622,7 @@ pub const TypeInfo = union(TypeId) {...@@ -7622,7 +7622,7 @@ pub const TypeInfo = union(TypeId) {
7622 child: type,7622 child: type,
7623 };7623 };
76247624
7625 pub const Definition = struct {7625 pub const Declaration = struct {
7626 name: []const u8,7626 name: []const u8,
7627 is_pub: bool,7627 is_pub: bool,
7628 data: Data,7628 data: Data,
...@@ -7630,9 +7630,9 @@ pub const TypeInfo = union(TypeId) {...@@ -7630,9 +7630,9 @@ pub const TypeInfo = union(TypeId) {
7630 pub const Data = union(enum) {7630 pub const Data = union(enum) {
7631 Type: type,7631 Type: type,
7632 Var: type,7632 Var: type,
7633 Fn: FnDef,7633 Fn: FnDecl,
76347634
7635 pub const FnDef = struct {7635 pub const FnDecl = struct {
7636 fn_type: type,7636 fn_type: type,
7637 inline_type: Inline,7637 inline_type: Inline,
7638 calling_convention: CallingConvention,7638 calling_convention: CallingConvention,
src/codegen.cpp+6-6
...@@ -7762,7 +7762,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7762,7 +7762,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7762 " pub const Struct = struct {\n"7762 " pub const Struct = struct {\n"
7763 " layout: ContainerLayout,\n"7763 " layout: ContainerLayout,\n"
7764 " fields: []StructField,\n"7764 " fields: []StructField,\n"
7765 " defs: []Definition,\n"7765 " decls: []Declaration,\n"
7766 " };\n"7766 " };\n"
7767 "\n"7767 "\n"
7768 " pub const Optional = struct {\n"7768 " pub const Optional = struct {\n"
...@@ -7790,7 +7790,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7790,7 +7790,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7790 " layout: ContainerLayout,\n"7790 " layout: ContainerLayout,\n"
7791 " tag_type: type,\n"7791 " tag_type: type,\n"
7792 " fields: []EnumField,\n"7792 " fields: []EnumField,\n"
7793 " defs: []Definition,\n"7793 " decls: []Declaration,\n"
7794 " };\n"7794 " };\n"
7795 "\n"7795 "\n"
7796 " pub const UnionField = struct {\n"7796 " pub const UnionField = struct {\n"
...@@ -7803,7 +7803,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7803,7 +7803,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7803 " layout: ContainerLayout,\n"7803 " layout: ContainerLayout,\n"
7804 " tag_type: ?type,\n"7804 " tag_type: ?type,\n"
7805 " fields: []UnionField,\n"7805 " fields: []UnionField,\n"
7806 " defs: []Definition,\n"7806 " decls: []Declaration,\n"
7807 " };\n"7807 " };\n"
7808 "\n"7808 "\n"
7809 " pub const CallingConvention = enum {\n"7809 " pub const CallingConvention = enum {\n"
...@@ -7839,7 +7839,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7839,7 +7839,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7839 " child: type,\n"7839 " child: type,\n"
7840 " };\n"7840 " };\n"
7841 "\n"7841 "\n"
7842 " pub const Definition = struct {\n"7842 " pub const Declaration = struct {\n"
7843 " name: []const u8,\n"7843 " name: []const u8,\n"
7844 " is_pub: bool,\n"7844 " is_pub: bool,\n"
7845 " data: Data,\n"7845 " data: Data,\n"
...@@ -7847,9 +7847,9 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7847,9 +7847,9 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7847 " pub const Data = union(enum) {\n"7847 " pub const Data = union(enum) {\n"
7848 " Type: type,\n"7848 " Type: type,\n"
7849 " Var: type,\n"7849 " Var: type,\n"
7850 " Fn: FnDef,\n"7850 " Fn: FnDecl,\n"
7851 "\n"7851 "\n"
7852 " pub const FnDef = struct {\n"7852 " pub const FnDecl = struct {\n"
7853 " fn_type: type,\n"7853 " fn_type: type,\n"
7854 " inline_type: Inline,\n"7854 " inline_type: Inline,\n"
7855 " calling_convention: CallingConvention,\n"7855 " 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...@@ -18380,37 +18380,37 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
18380 return var->const_value->data.x_type;18380 return var->const_value->data.x_type;
18381}18381}
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,
18384 ScopeDecls *decls_scope)18384 ScopeDecls *decls_scope)
18385{18385{
18386 Error err;18386 Error err;
18387 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);18387 ZigType *type_info_declaration_type = ir_type_info_get_type(ira, "Declaration", nullptr);
18388 if ((err = type_resolve(ira->codegen, type_info_definition_type, ResolveStatusSizeKnown)))18388 if ((err = type_resolve(ira->codegen, type_info_declaration_type, ResolveStatusSizeKnown)))
18389 return err;18389 return err;
1839018390
18391 ensure_field_index(type_info_definition_type, "name", 0);18391 ensure_field_index(type_info_declaration_type, "name", 0);
18392 ensure_field_index(type_info_definition_type, "is_pub", 1);18392 ensure_field_index(type_info_declaration_type, "is_pub", 1);
18393 ensure_field_index(type_info_definition_type, "data", 2);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);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_definition_data_type)))18396 if ((err = ensure_complete_type(ira->codegen, type_info_declaration_data_type)))
18397 return err;18397 return err;
1839818398
18399 ZigType *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_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_def_type)))18400 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_type)))
18401 return err;18401 return err;
1840218402
18403 ZigType *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_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_def_inline_type)))18404 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_inline_type)))
18405 return err;18405 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.
18408 auto decl_it = decls_scope->decl_table.entry_iterator();18408 auto decl_it = decls_scope->decl_table.entry_iterator();
18409 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;18409 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
18410 int definition_count = 0;18410 int declaration_count = 0;
1841118411
18412 while ((curr_entry = decl_it.next()) != nullptr) {18412 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.
18414 if (curr_entry->value->resolution == TldResolutionUnresolved) {18414 if (curr_entry->value->resolution == TldResolutionUnresolved) {
18415 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);18415 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
18416 if (curr_entry->value->resolution != TldResolutionOk) {18416 if (curr_entry->value->resolution != TldResolutionOk) {
...@@ -18426,21 +18426,21 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18426,21 +18426,21 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18426 continue;18426 continue;
18427 }18427 }
1842818428
18429 definition_count += 1;18429 declaration_count += 1;
18430 }18430 }
18431 }18431 }
1843218432
18433 ConstExprValue *definition_array = create_const_vals(1);18433 ConstExprValue *declaration_array = create_const_vals(1);
18434 definition_array->special = ConstValSpecialStatic;18434 declaration_array->special = ConstValSpecialStatic;
18435 definition_array->type = get_array_type(ira->codegen, type_info_definition_type, definition_count);18435 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count);
18436 definition_array->data.x_array.special = ConstArraySpecialNone;18436 declaration_array->data.x_array.special = ConstArraySpecialNone;
18437 definition_array->data.x_array.data.s_none.elements = create_const_vals(definition_count);18437 declaration_array->data.x_array.data.s_none.elements = create_const_vals(declaration_count);
18438 init_const_slice(ira->codegen, out_val, definition_array, 0, definition_count, false);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.
18441 decl_it = decls_scope->decl_table.entry_iterator();18441 decl_it = decls_scope->decl_table.entry_iterator();
18442 curr_entry = nullptr;18442 curr_entry = nullptr;
18443 int definition_index = 0;18443 int declaration_index = 0;
18444 while ((curr_entry = decl_it.next()) != nullptr) {18444 while ((curr_entry = decl_it.next()) != nullptr) {
18445 // Skip comptime blocks and test functions.18445 // Skip comptime blocks and test functions.
18446 if (curr_entry->value->id == TldIdCompTime) {18446 if (curr_entry->value->id == TldIdCompTime) {
...@@ -18451,10 +18451,10 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18451,10 +18451,10 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18451 continue;18451 continue;
18452 }18452 }
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;18456 declaration_val->special = ConstValSpecialStatic;
18457 definition_val->type = type_info_definition_type;18457 declaration_val->type = type_info_declaration_type;
1845818458
18459 ConstExprValue *inner_fields = create_const_vals(3);18459 ConstExprValue *inner_fields = create_const_vals(3);
18460 ConstExprValue *name = create_const_str_lit(ira->codegen, curr_entry->key);18460 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,...@@ -18463,9 +18463,9 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18463 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;18463 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
18464 inner_fields[1].data.x_bool = curr_entry->value->visib_mod == VisibModPub;18464 inner_fields[1].data.x_bool = curr_entry->value->visib_mod == VisibModPub;
18465 inner_fields[2].special = ConstValSpecialStatic;18465 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;
18467 inner_fields[2].parent.id = ConstParentIdStruct;18467 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;
18469 inner_fields[2].parent.data.p_struct.field_index = 1;18469 inner_fields[2].parent.data.p_struct.field_index = 1;
1847018470
18471 switch (curr_entry->value->id) {18471 switch (curr_entry->value->id) {
...@@ -18476,7 +18476,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18476,7 +18476,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18476 return ErrorSemanticAnalyzeFail;18476 return ErrorSemanticAnalyzeFail;
1847718477
18478 if (var->const_value->type->id == ZigTypeIdMetaType) {18478 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.
18480 // 0: Data.Type: type18480 // 0: Data.Type: type
18481 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);18481 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);
18482 inner_fields[2].data.x_union.payload = var->const_value;18482 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,...@@ -18496,7 +18496,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18496 }18496 }
18497 case TldIdFn:18497 case TldIdFn:
18498 {18498 {
18499 // 2: Data.Fn: Data.FnDef18499 // 2: Data.Fn: Data.FnDecl
18500 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 2);18500 bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 2);
1850118501
18502 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;18502 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,...@@ -18509,68 +18509,68 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1850918509
18510 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;18510 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;
1851118511
18512 ConstExprValue *fn_def_val = create_const_vals(1);18512 ConstExprValue *fn_decl_val = create_const_vals(1);
18513 fn_def_val->special = ConstValSpecialStatic;18513 fn_decl_val->special = ConstValSpecialStatic;
18514 fn_def_val->type = type_info_fn_def_type;18514 fn_decl_val->type = type_info_fn_decl_type;
18515 fn_def_val->parent.id = ConstParentIdUnion;18515 fn_decl_val->parent.id = ConstParentIdUnion;
18516 fn_def_val->parent.data.p_union.union_val = &inner_fields[2];18516 fn_decl_val->parent.data.p_union.union_val = &inner_fields[2];
1851718517
18518 ConstExprValue *fn_def_fields = create_const_vals(9);18518 ConstExprValue *fn_decl_fields = create_const_vals(9);
18519 fn_def_val->data.x_struct.fields = fn_def_fields;18519 fn_decl_val->data.x_struct.fields = fn_decl_fields;
1852018520
18521 // fn_type: type18521 // fn_type: type
18522 ensure_field_index(fn_def_val->type, "fn_type", 0);18522 ensure_field_index(fn_decl_val->type, "fn_type", 0);
18523 fn_def_fields[0].special = ConstValSpecialStatic;18523 fn_decl_fields[0].special = ConstValSpecialStatic;
18524 fn_def_fields[0].type = ira->codegen->builtin_types.entry_type;18524 fn_decl_fields[0].type = ira->codegen->builtin_types.entry_type;
18525 fn_def_fields[0].data.x_type = fn_entry->type_entry;18525 fn_decl_fields[0].data.x_type = fn_entry->type_entry;
18526 // inline_type: Data.FnDef.Inline18526 // inline_type: Data.FnDecl.Inline
18527 ensure_field_index(fn_def_val->type, "inline_type", 1);18527 ensure_field_index(fn_decl_val->type, "inline_type", 1);
18528 fn_def_fields[1].special = ConstValSpecialStatic;18528 fn_decl_fields[1].special = ConstValSpecialStatic;
18529 fn_def_fields[1].type = type_info_fn_def_inline_type;18529 fn_decl_fields[1].type = type_info_fn_decl_inline_type;
18530 bigint_init_unsigned(&fn_def_fields[1].data.x_enum_tag, fn_entry->fn_inline);18530 bigint_init_unsigned(&fn_decl_fields[1].data.x_enum_tag, fn_entry->fn_inline);
18531 // calling_convention: TypeInfo.CallingConvention18531 // calling_convention: TypeInfo.CallingConvention
18532 ensure_field_index(fn_def_val->type, "calling_convention", 2);18532 ensure_field_index(fn_decl_val->type, "calling_convention", 2);
18533 fn_def_fields[2].special = ConstValSpecialStatic;18533 fn_decl_fields[2].special = ConstValSpecialStatic;
18534 fn_def_fields[2].type = ir_type_info_get_type(ira, "CallingConvention", nullptr);18534 fn_decl_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);18535 bigint_init_unsigned(&fn_decl_fields[2].data.x_enum_tag, fn_node->cc);
18536 // is_var_args: bool18536 // 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);
18538 bool is_varargs = fn_node->is_var_args;18538 bool is_varargs = fn_node->is_var_args;
18539 fn_def_fields[3].special = ConstValSpecialStatic;18539 fn_decl_fields[3].special = ConstValSpecialStatic;
18540 fn_def_fields[3].type = ira->codegen->builtin_types.entry_bool;18540 fn_decl_fields[3].type = ira->codegen->builtin_types.entry_bool;
18541 fn_def_fields[3].data.x_bool = is_varargs;18541 fn_decl_fields[3].data.x_bool = is_varargs;
18542 // is_extern: bool18542 // is_extern: bool
18543 ensure_field_index(fn_def_val->type, "is_extern", 4);18543 ensure_field_index(fn_decl_val->type, "is_extern", 4);
18544 fn_def_fields[4].special = ConstValSpecialStatic;18544 fn_decl_fields[4].special = ConstValSpecialStatic;
18545 fn_def_fields[4].type = ira->codegen->builtin_types.entry_bool;18545 fn_decl_fields[4].type = ira->codegen->builtin_types.entry_bool;
18546 fn_def_fields[4].data.x_bool = fn_node->is_extern;18546 fn_decl_fields[4].data.x_bool = fn_node->is_extern;
18547 // is_export: bool18547 // is_export: bool
18548 ensure_field_index(fn_def_val->type, "is_export", 5);18548 ensure_field_index(fn_decl_val->type, "is_export", 5);
18549 fn_def_fields[5].special = ConstValSpecialStatic;18549 fn_decl_fields[5].special = ConstValSpecialStatic;
18550 fn_def_fields[5].type = ira->codegen->builtin_types.entry_bool;18550 fn_decl_fields[5].type = ira->codegen->builtin_types.entry_bool;
18551 fn_def_fields[5].data.x_bool = fn_node->is_export;18551 fn_decl_fields[5].data.x_bool = fn_node->is_export;
18552 // lib_name: ?[]const u818552 // lib_name: ?[]const u8
18553 ensure_field_index(fn_def_val->type, "lib_name", 6);18553 ensure_field_index(fn_decl_val->type, "lib_name", 6);
18554 fn_def_fields[6].special = ConstValSpecialStatic;18554 fn_decl_fields[6].special = ConstValSpecialStatic;
18555 ZigType *u8_ptr = get_pointer_to_type_extra(18555 ZigType *u8_ptr = get_pointer_to_type_extra(
18556 ira->codegen, ira->codegen->builtin_types.entry_u8,18556 ira->codegen, ira->codegen->builtin_types.entry_u8,
18557 true, false, PtrLenUnknown,18557 true, false, PtrLenUnknown,
18558 0, 0, 0, false);18558 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));
18560 if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {18560 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);
18562 ConstExprValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name);18562 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);
18564 } else {18564 } else {
18565 fn_def_fields[6].data.x_optional = nullptr;18565 fn_decl_fields[6].data.x_optional = nullptr;
18566 }18566 }
18567 // return_type: type18567 // return_type: type
18568 ensure_field_index(fn_def_val->type, "return_type", 7);18568 ensure_field_index(fn_decl_val->type, "return_type", 7);
18569 fn_def_fields[7].special = ConstValSpecialStatic;18569 fn_decl_fields[7].special = ConstValSpecialStatic;
18570 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;18570 fn_decl_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;18571 fn_decl_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
18572 // arg_names: [][] const u818572 // 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);
18574 size_t fn_arg_count = fn_entry->variable_list.length;18574 size_t fn_arg_count = fn_entry->variable_list.length;
18575 ConstExprValue *fn_arg_name_array = create_const_vals(1);18575 ConstExprValue *fn_arg_name_array = create_const_vals(1);
18576 fn_arg_name_array->special = ConstValSpecialStatic;18576 fn_arg_name_array->special = ConstValSpecialStatic;
...@@ -18579,7 +18579,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18579,7 +18579,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18579 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;18579 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
18580 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);18580 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
18584 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {18584 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
18585 ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index);18585 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,...@@ -18591,7 +18591,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18591 fn_arg_name_val->parent.data.p_array.elem_index = fn_arg_index;18591 fn_arg_name_val->parent.data.p_array.elem_index = fn_arg_index;
18592 }18592 }
1859318593
18594 inner_fields[2].data.x_union.payload = fn_def_val;18594 inner_fields[2].data.x_union.payload = fn_decl_val;
18595 break;18595 break;
18596 }18596 }
18597 case TldIdContainer:18597 case TldIdContainer:
...@@ -18615,11 +18615,11 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18615,11 +18615,11 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18615 zig_unreachable();18615 zig_unreachable();
18616 }18616 }
1861718617
18618 definition_val->data.x_struct.fields = inner_fields;18618 declaration_val->data.x_struct.fields = inner_fields;
18619 definition_index++;18619 declaration_index++;
18620 }18620 }
1862118621
18622 assert(definition_index == definition_count);18622 assert(declaration_index == declaration_count);
18623 return ErrorNone;18623 return ErrorNone;
18624}18624}
1862518625
...@@ -18927,9 +18927,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18927,9 +18927,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18927 enum_field_val->parent.data.p_array.array_val = enum_field_array;18927 enum_field_val->parent.data.p_array.array_val = enum_field_array;
18928 enum_field_val->parent.data.p_array.elem_index = enum_field_index;18928 enum_field_val->parent.data.p_array.elem_index = enum_field_index;
18929 }18929 }
18930 // defs: []TypeInfo.Definition18930 // decls: []TypeInfo.Declaration
18931 ensure_field_index(result->type, "defs", 3);18931 ensure_field_index(result->type, "decls", 3);
18932 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],18932 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[3],
18933 type_entry->data.enumeration.decls_scope)))18933 type_entry->data.enumeration.decls_scope)))
18934 {18934 {
18935 return err;18935 return err;
...@@ -19094,9 +19094,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -19094,9 +19094,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
19094 union_field_val->parent.data.p_array.array_val = union_field_array;19094 union_field_val->parent.data.p_array.array_val = union_field_array;
19095 union_field_val->parent.data.p_array.elem_index = union_field_index;19095 union_field_val->parent.data.p_array.elem_index = union_field_index;
19096 }19096 }
19097 // defs: []TypeInfo.Definition19097 // decls: []TypeInfo.Declaration
19098 ensure_field_index(result->type, "defs", 3);19098 ensure_field_index(result->type, "decls", 3);
19099 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[3],19099 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[3],
19100 type_entry->data.unionation.decls_scope)))19100 type_entry->data.unionation.decls_scope)))
19101 {19101 {
19102 return err;19102 return err;
...@@ -19175,9 +19175,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -19175,9 +19175,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
19175 struct_field_val->parent.data.p_array.array_val = struct_field_array;19175 struct_field_val->parent.data.p_array.array_val = struct_field_array;
19176 struct_field_val->parent.data.p_array.elem_index = struct_field_index;19176 struct_field_val->parent.data.p_array.elem_index = struct_field_index;
19177 }19177 }
19178 // defs: []TypeInfo.Definition19178 // decls: []TypeInfo.Declaration
19179 ensure_field_index(result->type, "defs", 2);19179 ensure_field_index(result->type, "decls", 2);
19180 if ((err = ir_make_type_info_defs(ira, source_instr, &fields[2],19180 if ((err = ir_make_type_info_decls(ira, source_instr, &fields[2],
19181 type_entry->data.structure.decls_scope)))19181 type_entry->data.structure.decls_scope)))
19182 {19182 {
19183 return err;19183 return err;
std/fmt.zig+1-17
...@@ -145,23 +145,7 @@ pub fn formatType(...@@ -145,23 +145,7 @@ pub fn formatType(
145 return format(context, Errors, output, "promise@{x}", @ptrToInt(value));145 return format(context, Errors, output, "promise@{x}", @ptrToInt(value));
146 },146 },
147 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {147 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
148 const has_cust_fmt = comptime cf: {148 if (comptime std.meta.trait.hasFn("format")(T)) return value.format(fmt, context, Errors, output);
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);
165149
166 try output(context, @typeName(T));150 try output(context, @typeName(T));
167 switch (comptime @typeId(T)) {151 switch (comptime @typeId(T)) {
std/meta.zig+22-22
...@@ -160,16 +160,16 @@ test "std.meta.containerLayout" {...@@ -160,16 +160,16 @@ test "std.meta.containerLayout" {
160 testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern);160 testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern);
161}161}
162162
163pub fn definitions(comptime T: type) []TypeInfo.Definition {163pub fn declarations(comptime T: type) []TypeInfo.Declaration {
164 return switch (@typeInfo(T)) {164 return switch (@typeInfo(T)) {
165 TypeId.Struct => |info| info.defs,165 TypeId.Struct => |info| info.decls,
166 TypeId.Enum => |info| info.defs,166 TypeId.Enum => |info| info.decls,
167 TypeId.Union => |info| info.defs,167 TypeId.Union => |info| info.decls,
168 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),168 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
169 };169 };
170}170}
171171
172test "std.meta.definitions" {172test "std.meta.declarations" {
173 const E1 = enum {173 const E1 = enum {
174 A,174 A,
175175
...@@ -184,28 +184,28 @@ test "std.meta.definitions" {...@@ -184,28 +184,28 @@ test "std.meta.definitions" {
184 fn a() void {}184 fn a() void {}
185 };185 };
186186
187 const defs = comptime [][]TypeInfo.Definition{187 const decls = comptime [][]TypeInfo.Declaration{
188 definitions(E1),188 declarations(E1),
189 definitions(S1),189 declarations(S1),
190 definitions(U1),190 declarations(U1),
191 };191 };
192192
193 inline for (defs) |def| {193 inline for (decls) |decl| {
194 testing.expect(def.len == 1);194 testing.expect(decl.len == 1);
195 testing.expect(comptime mem.eql(u8, def[0].name, "a"));195 testing.expect(comptime mem.eql(u8, decl[0].name, "a"));
196 }196 }
197}197}
198198
199pub fn definitionInfo(comptime T: type, comptime def_name: []const u8) TypeInfo.Definition {199pub fn declarationInfo(comptime T: type, comptime decl_name: []const u8) TypeInfo.Declaration {
200 inline for (comptime definitions(T)) |def| {200 inline for (comptime declarations(T)) |decl| {
201 if (comptime mem.eql(u8, def.name, def_name))201 if (comptime mem.eql(u8, decl.name, decl_name))
202 return def;202 return decl;
203 }203 }
204204
205 @compileError("'" ++ @typeName(T) ++ "' has no definition '" ++ def_name ++ "'");205 @compileError("'" ++ @typeName(T) ++ "' has no declaration '" ++ decl_name ++ "'");
206}206}
207207
208test "std.meta.definitionInfo" {208test "std.meta.declarationInfo" {
209 const E1 = enum {209 const E1 = enum {
210 A,210 A,
211211
...@@ -220,10 +220,10 @@ test "std.meta.definitionInfo" {...@@ -220,10 +220,10 @@ test "std.meta.definitionInfo" {
220 fn a() void {}220 fn a() void {}
221 };221 };
222222
223 const infos = comptime []TypeInfo.Definition{223 const infos = comptime []TypeInfo.Declaration{
224 definitionInfo(E1, "a"),224 declarationInfo(E1, "a"),
225 definitionInfo(S1, "a"),225 declarationInfo(S1, "a"),
226 definitionInfo(U1, "a"),226 declarationInfo(U1, "a"),
227 };227 };
228228
229 inline for (infos) |info| {229 inline for (infos) |info| {
std/meta/trait.zig+5-43
...@@ -55,53 +55,15 @@ test "std.meta.trait.multiTrait" {...@@ -55,53 +55,15 @@ test "std.meta.trait.multiTrait" {
55 testing.expect(!isVector(u8));55 testing.expect(!isVector(u8));
56}56}
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
97///58///
98pub fn hasFn(comptime name: []const u8) TraitFn {59pub fn hasFn(comptime name: []const u8) TraitFn {
99 const Closure = struct {60 const Closure = struct {
100 pub fn trait(comptime T: type) bool {61 pub fn trait(comptime T: type) bool {
101 if (!comptime hasDef(name)(T)) return false;62 if (!comptime isContainer(T)) return false;
102 const DefType = @typeOf(@field(T, name));63 if (!comptime @hasDecl(T, name)) return false;
103 const def_type_id = @typeId(DefType);64 const DeclType = @typeOf(@field(T, name));
104 return def_type_id == builtin.TypeId.Fn;65 const decl_type_id = @typeId(DeclType);
66 return decl_type_id == builtin.TypeId.Fn;
105 }67 }
106 };68 };
107 return Closure.trait;69 return Closure.trait;
test/stage1/behavior/type_info.zig+8-8
...@@ -177,7 +177,7 @@ fn testEnum() void {...@@ -177,7 +177,7 @@ fn testEnum() void {
177 expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));177 expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
178 expect(os_info.Enum.fields[3].value == 3);178 expect(os_info.Enum.fields[3].value == 3);
179 expect(os_info.Enum.tag_type == u2);179 expect(os_info.Enum.tag_type == u2);
180 expect(os_info.Enum.defs.len == 0);180 expect(os_info.Enum.decls.len == 0);
181}181}
182182
183test "type info: union info" {183test "type info: union info" {
...@@ -194,7 +194,7 @@ fn testUnion() void {...@@ -194,7 +194,7 @@ fn testUnion() void {
194 expect(typeinfo_info.Union.fields[4].enum_field != null);194 expect(typeinfo_info.Union.fields[4].enum_field != null);
195 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);195 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
196 expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));196 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
199 const TestNoTagUnion = union {199 const TestNoTagUnion = union {
200 Foo: void,200 Foo: void,
...@@ -232,12 +232,12 @@ fn testStruct() void {...@@ -232,12 +232,12 @@ fn testStruct() void {
232 expect(struct_info.Struct.fields.len == 3);232 expect(struct_info.Struct.fields.len == 3);
233 expect(struct_info.Struct.fields[1].offset == null);233 expect(struct_info.Struct.fields[1].offset == null);
234 expect(struct_info.Struct.fields[2].field_type == *TestStruct);234 expect(struct_info.Struct.fields[2].field_type == *TestStruct);
235 expect(struct_info.Struct.defs.len == 2);235 expect(struct_info.Struct.decls.len == 2);
236 expect(struct_info.Struct.defs[0].is_pub);236 expect(struct_info.Struct.decls[0].is_pub);
237 expect(!struct_info.Struct.defs[0].data.Fn.is_extern);237 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
238 expect(struct_info.Struct.defs[0].data.Fn.lib_name == null);238 expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
239 expect(struct_info.Struct.defs[0].data.Fn.return_type == void);239 expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
240 expect(struct_info.Struct.defs[0].data.Fn.fn_type == fn (*const TestStruct) void);240 expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
241}241}
242242
243const TestStruct = packed struct {243const TestStruct = packed struct {