authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-01 17:15:58-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-01 17:15:58-05:00
log5f7d9c58458b8c6a675f747f811c54ab4e4f60c2
tree94f2705efb79ed9d5670acb48b66bc97d7886bc6
parenta7ddcabb5034f4fef90d266263611f6cfbe04f41
signaturelock-open Commit is signed but in an unrecognized format.

@typeInfo for structs and opaque types is the bare name


7 files changed, 77 insertions(+), 42 deletions(-)

src/all_types.hpp+6
......@@ -1262,6 +1262,10 @@ enum OnePossibleValue {
12621262 OnePossibleValueYes,
12631263};
12641264
1265struct ZigTypeOpaque {
1266 Buf *bare_name;
1267};
1268
12651269struct ZigType {
12661270 ZigTypeId id;
12671271 Buf name;
......@@ -1284,6 +1288,7 @@ struct ZigType {
12841288 ZigTypeBoundFn bound_fn;
12851289 ZigTypePromise promise;
12861290 ZigTypeVector vector;
1291 ZigTypeOpaque opaque;
12871292 } data;
12881293
12891294 // use these fields to make sure we don't duplicate type table entries for the same type
......@@ -1941,6 +1946,7 @@ struct ScopeDecls {
19411946 ZigType *import;
19421947 // If this is a scope from a container, this is the type entry, otherwise null
19431948 ZigType *container_type;
1949 Buf *bare_name;
19441950
19451951 bool safety_off;
19461952 bool fast_math_on;
src/analyze.cpp+28-17
......@@ -120,13 +120,16 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
120120 dest->parent = parent;
121121}
122122
123ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import) {
123static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,
124 ZigType *import, Buf *bare_name)
125{
124126 assert(node == nullptr || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
125127 ScopeDecls *scope = allocate<ScopeDecls>(1);
126128 init_scope(g, &scope->base, ScopeIdDecls, node, parent);
127129 scope->decl_table.init(4);
128130 scope->container_type = container_type;
129131 scope->import = import;
132 scope->bare_name = bare_name;
130133 return scope;
131134}
132135
......@@ -225,9 +228,12 @@ ZigType *get_scope_import(Scope *scope) {
225228 zig_unreachable();
226229}
227230
228static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *source_node, Scope *parent_scope) {
231static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *source_node, Scope *parent_scope,
232 Buf *bare_name)
233{
229234 ZigType *entry = new_type_table_entry(id);
230 *get_container_scope_ptr(entry) = create_decls_scope(g, source_node, parent_scope, entry, get_scope_import(parent_scope));
235 *get_container_scope_ptr(entry) = create_decls_scope(g, source_node, parent_scope, entry,
236 get_scope_import(parent_scope), bare_name);
231237 return entry;
232238}
233239
......@@ -1009,21 +1015,22 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
10091015 return entry;
10101016}
10111017
1012ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name) {
1018ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name) {
10131019 ZigType *entry = new_type_table_entry(ZigTypeIdOpaque);
10141020
1015 buf_init_from_str(&entry->name, name);
1021 buf_init_from_str(&entry->name, full_name);
10161022
10171023 ZigType *import = scope ? get_scope_import(scope) : nullptr;
10181024 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
10191025
10201026 entry->type_ref = LLVMInt8Type();
10211027 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
1022 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
1028 ZigLLVMTag_DW_structure_type(), full_name,
10231029 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
10241030 import ? import->data.structure.root_struct->di_file : nullptr,
10251031 line);
10261032 entry->zero_bits = false;
1033 entry->data.opaque.bare_name = bare_name;
10271034
10281035 return entry;
10291036}
......@@ -1293,29 +1300,31 @@ static ZigTypeId container_to_type(ContainerKind kind) {
12931300}
12941301
12951302// This is like get_partial_container_type except it's for the implicit root struct of files.
1296ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct) {
1303ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *bare_name,
1304 RootStruct *root_struct)
1305{
12971306 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
1298 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry);
1307 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
12991308 entry->data.structure.root_struct = root_struct;
13001309 entry->data.structure.layout = ContainerLayoutAuto;
1301 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
1310 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
13021311
13031312 size_t line = 0; // root therefore first line
13041313 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
13051314
13061315 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1307 dwarf_kind, name,
1316 dwarf_kind, full_name,
13081317 ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1));
13091318
1310 buf_init_from_str(&entry->name, name);
1319 buf_init_from_str(&entry->name, full_name);
13111320 return entry;
13121321}
13131322
13141323ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
1315 AstNode *decl_node, const char *name, ContainerLayout layout)
1324 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout)
13161325{
13171326 ZigTypeId type_id = container_to_type(kind);
1318 ZigType *entry = new_container_type_entry(g, type_id, decl_node, scope);
1327 ZigType *entry = new_container_type_entry(g, type_id, decl_node, scope, bare_name);
13191328
13201329 switch (kind) {
13211330 case ContainerKindStruct:
......@@ -1336,13 +1345,13 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
13361345 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
13371346
13381347 ZigType *import = get_scope_import(scope);
1339 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
1348 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
13401349 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1341 dwarf_kind, name,
1350 dwarf_kind, full_name,
13421351 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
13431352 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
13441353
1345 buf_init_from_str(&entry->name, name);
1354 buf_init_from_str(&entry->name, full_name);
13461355
13471356 return entry;
13481357}
......@@ -4501,6 +4510,8 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
45014510 buf_len(&noextname) - (buf_len(&resolved_root_src_dir) + 1));
45024511 buf_replace(&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR);
45034512 }
4513 Buf *bare_name = buf_alloc();
4514 os_path_extname(src_basename, bare_name, nullptr);
45044515
45054516 RootStruct *root_struct = allocate<RootStruct>(1);
45064517 root_struct->package = package;
......@@ -4508,7 +4519,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
45084519 root_struct->line_offsets = tokenization.line_offsets;
45094520 root_struct->path = resolved_path;
45104521 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
4511 ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), root_struct);
4522 ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), bare_name, root_struct);
45124523 if (source_kind == SourceKindRoot) {
45134524 assert(g->root_import == nullptr);
45144525 g->root_import = import_entry;
src/analyze.hpp+4-4
......@@ -30,12 +30,13 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type);
3030ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size);
3131ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
3232ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
33 AstNode *decl_node, const char *name, ContainerLayout layout);
34ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct);
33 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
34ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *bare_name,
35 RootStruct *root_struct);
3536ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
3637ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
3738ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
38ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name);
39ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name);
3940ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
4041 ZigType *field_types[], size_t field_count);
4142ZigType *get_promise_type(CodeGen *g, ZigType *result_type);
......@@ -117,7 +118,6 @@ ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent);
117118ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
118119ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
119120ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
120ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import);
121121Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
122122Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent);
123123Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
src/codegen.cpp+7-7
......@@ -7123,7 +7123,8 @@ static void define_builtin_types(CodeGen *g) {
71237123 g->builtin_types.entry_i64 = get_int_type(g, true, 64);
71247124
71257125 {
7126 g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void");
7126 g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void",
7127 buf_create_from_str("c_void"));
71277128 g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
71287129 }
71297130
......@@ -7943,19 +7944,18 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
79437944 Buf noextname = BUF_INIT;
79447945 os_path_extname(src_basename, &noextname, nullptr);
79457946
7947 detect_libc(g);
7948
7949 init(g);
7950
79467951 RootStruct *root_struct = allocate<RootStruct>(1);
79477952 root_struct->source_code = nullptr;
79487953 root_struct->path = full_path;
79497954 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
79507955
7951 ZigType *import = get_root_container_type(g, buf_ptr(&noextname), root_struct);
7956 ZigType *import = get_root_container_type(g, buf_ptr(&noextname), &noextname, root_struct);
79527957 g->root_import = import;
79537958
7954 detect_libc(g);
7955
7956 init(g);
7957
7958
79597959 ZigList<ErrorMsg *> errors = {0};
79607960 Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
79617961
src/ir.cpp+25-10
......@@ -6609,13 +6609,14 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
66096609}
66106610
66116611static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
6612 Scope *scope, AstNode *source_node)
6612 Scope *scope, AstNode *source_node, Buf *out_bare_name)
66136613{
66146614 if (exec->name) {
66156615 ZigType *import = get_scope_import(scope);
66166616 Buf *namespace_name = buf_create_from_buf(&import->name);
66176617 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
66186618 buf_append_buf(namespace_name, exec->name);
6619 buf_init_from_buf(out_bare_name, exec->name);
66196620 return namespace_name;
66206621 } else if (exec->name_fn != nullptr) {
66216622 Buf *name = buf_alloc();
......@@ -6623,6 +6624,7 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
66236624 buf_appendf(name, "(");
66246625 render_instance_name_recursive(codegen, name, &exec->name_fn->fndef_scope->base, exec->begin_scope);
66256626 buf_appendf(name, ")");
6627 buf_init_from_buf(out_bare_name, name);
66266628 return name;
66276629 } else {
66286630 ZigType *import = get_scope_import(scope);
......@@ -6630,6 +6632,7 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
66306632 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
66316633 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
66326634 source_node->line + 1, source_node->column + 1);
6635 buf_init_from_buf(out_bare_name, namespace_name);
66336636 return namespace_name;
66346637 }
66356638}
......@@ -6655,11 +6658,12 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
66556658 assert(node->type == NodeTypeContainerDecl);
66566659
66576660 ContainerKind kind = node->data.container_decl.kind;
6658 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node);
6661 Buf *bare_name = buf_alloc();
6662 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node, bare_name);
66596663
66606664 ContainerLayout layout = node->data.container_decl.layout;
66616665 ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,
6662 kind, node, buf_ptr(name), layout);
6666 kind, node, buf_ptr(name), bare_name, layout);
66636667 ScopeDecls *child_scope = get_container_scope(container_type);
66646668
66656669 for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {
......@@ -6668,7 +6672,7 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
66686672 }
66696673
66706674 TldContainer *tld_container = allocate<TldContainer>(1);
6671 init_tld(&tld_container->base, TldIdContainer, name, VisibModPub, node, parent_scope);
6675 init_tld(&tld_container->base, TldIdContainer, bare_name, VisibModPub, node, parent_scope);
66726676 tld_container->type_entry = container_type;
66736677 tld_container->decls_scope = child_scope;
66746678 irb->codegen->resolve_queue.append(&tld_container->base);
......@@ -6755,7 +6759,8 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
67556759
67566760 uint32_t err_count = node->data.err_set_decl.decls.length;
67576761
6758 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node);
6762 Buf bare_name = BUF_INIT;
6763 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node, &bare_name);
67596764 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
67606765 buf_init_from_buf(&err_set_type->name, type_name);
67616766 err_set_type->data.error_set.err_count = err_count;
......@@ -18680,7 +18685,15 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc
1868018685 return ira->codegen->invalid_instruction;
1868118686
1868218687 if (!type_entry->cached_const_name_val) {
18683 type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, &type_entry->name);
18688 Buf *name;
18689 if (is_container(type_entry)) {
18690 name = get_container_scope(type_entry)->bare_name;
18691 } else if (type_entry->id == ZigTypeIdOpaque) {
18692 name = type_entry->data.opaque.bare_name;
18693 } else {
18694 name = &type_entry->name;
18695 }
18696 type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, name);
1868418697 }
1868518698 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
1868618699 copy_const_val(&result->value, type_entry->cached_const_name_val, true);
......@@ -18715,7 +18728,8 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1871518728 // for this DIFile
1871618729 root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
1871718730 buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));
18718 ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name), root_struct);
18731 ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name),
18732 namespace_name, root_struct);
1871918733
1872018734 ZigList<ErrorMsg *> errors = {0};
1872118735
......@@ -21668,10 +21682,11 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru
2166821682}
2166921683
2167021684static IrInstruction *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) {
21671 Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
21672 instruction->base.scope, instruction->base.source_node);
21685 Buf *bare_name = buf_alloc();
21686 Buf *full_name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
21687 instruction->base.scope, instruction->base.source_node, bare_name);
2167321688 ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,
21674 buf_ptr(name));
21689 buf_ptr(full_name), bare_name);
2167521690 return ir_const_type(ira, &instruction->base, result_type);
2167621691}
2167721692
test/compile_errors.zig+4-4
......@@ -1231,10 +1231,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12311231 \\
12321232 \\fn bar(x: *b.Foo) void {}
12331233 ,
1234 "tmp.zig:6:10: error: expected type '*Foo', found '*Foo'",
1235 "tmp.zig:6:10: note: pointer type child 'Foo' cannot cast into pointer type child 'Foo'",
1236 "a.zig:1:17: note: Foo declared here",
1237 "b.zig:1:17: note: Foo declared here",
1234 "tmp.zig:6:10: error: expected type '*b.Foo', found '*a.Foo'",
1235 "tmp.zig:6:10: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
1236 "a.zig:1:17: note: a.Foo declared here",
1237 "b.zig:1:17: note: b.Foo declared here",
12381238 );
12391239
12401240 tc.addSourceFile("a.zig",
test/tests.zig+3
......@@ -980,15 +980,18 @@ pub const TranslateCContext = struct {
980980 Term.Exited => |code| {
981981 if (code != 0) {
982982 warn("Compilation failed with exit code {}\n", code);
983 printInvocation(zig_args.toSliceConst());
983984 return error.TestFailed;
984985 }
985986 },
986987 Term.Signal => |code| {
987988 warn("Compilation failed with signal {}\n", code);
989 printInvocation(zig_args.toSliceConst());
988990 return error.TestFailed;
989991 },
990992 else => {
991993 warn("Compilation terminated unexpectedly\n");
994 printInvocation(zig_args.toSliceConst());
992995 return error.TestFailed;
993996 },
994997 }