mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 00:26:57 +00:00
@typeInfo for structs and opaque types is the bare name
This commit is contained in:
parent
a7ddcabb50
commit
5f7d9c5845
@ -1262,6 +1262,10 @@ enum OnePossibleValue {
|
||||
OnePossibleValueYes,
|
||||
};
|
||||
|
||||
struct ZigTypeOpaque {
|
||||
Buf *bare_name;
|
||||
};
|
||||
|
||||
struct ZigType {
|
||||
ZigTypeId id;
|
||||
Buf name;
|
||||
@ -1284,6 +1288,7 @@ struct ZigType {
|
||||
ZigTypeBoundFn bound_fn;
|
||||
ZigTypePromise promise;
|
||||
ZigTypeVector vector;
|
||||
ZigTypeOpaque opaque;
|
||||
} data;
|
||||
|
||||
// use these fields to make sure we don't duplicate type table entries for the same type
|
||||
@ -1941,6 +1946,7 @@ struct ScopeDecls {
|
||||
ZigType *import;
|
||||
// If this is a scope from a container, this is the type entry, otherwise null
|
||||
ZigType *container_type;
|
||||
Buf *bare_name;
|
||||
|
||||
bool safety_off;
|
||||
bool fast_math_on;
|
||||
|
@ -120,13 +120,16 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
|
||||
dest->parent = parent;
|
||||
}
|
||||
|
||||
ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import) {
|
||||
static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,
|
||||
ZigType *import, Buf *bare_name)
|
||||
{
|
||||
assert(node == nullptr || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
|
||||
ScopeDecls *scope = allocate<ScopeDecls>(1);
|
||||
init_scope(g, &scope->base, ScopeIdDecls, node, parent);
|
||||
scope->decl_table.init(4);
|
||||
scope->container_type = container_type;
|
||||
scope->import = import;
|
||||
scope->bare_name = bare_name;
|
||||
return scope;
|
||||
}
|
||||
|
||||
@ -225,9 +228,12 @@ ZigType *get_scope_import(Scope *scope) {
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *source_node, Scope *parent_scope) {
|
||||
static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *source_node, Scope *parent_scope,
|
||||
Buf *bare_name)
|
||||
{
|
||||
ZigType *entry = new_type_table_entry(id);
|
||||
*get_container_scope_ptr(entry) = create_decls_scope(g, source_node, parent_scope, entry, get_scope_import(parent_scope));
|
||||
*get_container_scope_ptr(entry) = create_decls_scope(g, source_node, parent_scope, entry,
|
||||
get_scope_import(parent_scope), bare_name);
|
||||
return entry;
|
||||
}
|
||||
|
||||
@ -1009,21 +1015,22 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
|
||||
return entry;
|
||||
}
|
||||
|
||||
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name) {
|
||||
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name) {
|
||||
ZigType *entry = new_type_table_entry(ZigTypeIdOpaque);
|
||||
|
||||
buf_init_from_str(&entry->name, name);
|
||||
buf_init_from_str(&entry->name, full_name);
|
||||
|
||||
ZigType *import = scope ? get_scope_import(scope) : nullptr;
|
||||
unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
|
||||
|
||||
entry->type_ref = LLVMInt8Type();
|
||||
entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
|
||||
ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
|
||||
ZigLLVMTag_DW_structure_type(), full_name,
|
||||
import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
|
||||
import ? import->data.structure.root_struct->di_file : nullptr,
|
||||
line);
|
||||
entry->zero_bits = false;
|
||||
entry->data.opaque.bare_name = bare_name;
|
||||
|
||||
return entry;
|
||||
}
|
||||
@ -1293,29 +1300,31 @@ static ZigTypeId container_to_type(ContainerKind kind) {
|
||||
}
|
||||
|
||||
// This is like get_partial_container_type except it's for the implicit root struct of files.
|
||||
ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct) {
|
||||
ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *bare_name,
|
||||
RootStruct *root_struct)
|
||||
{
|
||||
ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
|
||||
entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry);
|
||||
entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
|
||||
entry->data.structure.root_struct = root_struct;
|
||||
entry->data.structure.layout = ContainerLayoutAuto;
|
||||
entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
|
||||
entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
|
||||
|
||||
size_t line = 0; // root therefore first line
|
||||
unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
|
||||
|
||||
entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
|
||||
dwarf_kind, name,
|
||||
dwarf_kind, full_name,
|
||||
ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1));
|
||||
|
||||
buf_init_from_str(&entry->name, name);
|
||||
buf_init_from_str(&entry->name, full_name);
|
||||
return entry;
|
||||
}
|
||||
|
||||
ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
|
||||
AstNode *decl_node, const char *name, ContainerLayout layout)
|
||||
AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout)
|
||||
{
|
||||
ZigTypeId type_id = container_to_type(kind);
|
||||
ZigType *entry = new_container_type_entry(g, type_id, decl_node, scope);
|
||||
ZigType *entry = new_container_type_entry(g, type_id, decl_node, scope, bare_name);
|
||||
|
||||
switch (kind) {
|
||||
case ContainerKindStruct:
|
||||
@ -1336,13 +1345,13 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
|
||||
unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
|
||||
|
||||
ZigType *import = get_scope_import(scope);
|
||||
entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
|
||||
entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
|
||||
entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
|
||||
dwarf_kind, name,
|
||||
dwarf_kind, full_name,
|
||||
ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
|
||||
import->data.structure.root_struct->di_file, (unsigned)(line + 1));
|
||||
|
||||
buf_init_from_str(&entry->name, name);
|
||||
buf_init_from_str(&entry->name, full_name);
|
||||
|
||||
return entry;
|
||||
}
|
||||
@ -4501,6 +4510,8 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
|
||||
buf_len(&noextname) - (buf_len(&resolved_root_src_dir) + 1));
|
||||
buf_replace(&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR);
|
||||
}
|
||||
Buf *bare_name = buf_alloc();
|
||||
os_path_extname(src_basename, bare_name, nullptr);
|
||||
|
||||
RootStruct *root_struct = allocate<RootStruct>(1);
|
||||
root_struct->package = package;
|
||||
@ -4508,7 +4519,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
|
||||
root_struct->line_offsets = tokenization.line_offsets;
|
||||
root_struct->path = resolved_path;
|
||||
root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
|
||||
ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), root_struct);
|
||||
ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), bare_name, root_struct);
|
||||
if (source_kind == SourceKindRoot) {
|
||||
assert(g->root_import == nullptr);
|
||||
g->root_import = import_entry;
|
||||
|
@ -30,12 +30,13 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type);
|
||||
ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size);
|
||||
ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
|
||||
ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
|
||||
AstNode *decl_node, const char *name, ContainerLayout layout);
|
||||
ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct);
|
||||
AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
|
||||
ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *bare_name,
|
||||
RootStruct *root_struct);
|
||||
ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
|
||||
ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
|
||||
ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
|
||||
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name);
|
||||
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name);
|
||||
ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
|
||||
ZigType *field_types[], size_t field_count);
|
||||
ZigType *get_promise_type(CodeGen *g, ZigType *result_type);
|
||||
@ -117,7 +118,6 @@ ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent);
|
||||
ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
|
||||
ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
|
||||
ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
|
||||
ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import);
|
||||
Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
|
||||
Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent);
|
||||
Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
|
||||
|
@ -7123,7 +7123,8 @@ static void define_builtin_types(CodeGen *g) {
|
||||
g->builtin_types.entry_i64 = get_int_type(g, true, 64);
|
||||
|
||||
{
|
||||
g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void");
|
||||
g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void",
|
||||
buf_create_from_str("c_void"));
|
||||
g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
|
||||
}
|
||||
|
||||
@ -7943,19 +7944,18 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
|
||||
Buf noextname = BUF_INIT;
|
||||
os_path_extname(src_basename, &noextname, nullptr);
|
||||
|
||||
detect_libc(g);
|
||||
|
||||
init(g);
|
||||
|
||||
RootStruct *root_struct = allocate<RootStruct>(1);
|
||||
root_struct->source_code = nullptr;
|
||||
root_struct->path = full_path;
|
||||
root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
|
||||
|
||||
ZigType *import = get_root_container_type(g, buf_ptr(&noextname), root_struct);
|
||||
ZigType *import = get_root_container_type(g, buf_ptr(&noextname), &noextname, root_struct);
|
||||
g->root_import = import;
|
||||
|
||||
detect_libc(g);
|
||||
|
||||
init(g);
|
||||
|
||||
|
||||
ZigList<ErrorMsg *> errors = {0};
|
||||
Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
|
||||
|
||||
|
35
src/ir.cpp
35
src/ir.cpp
@ -6609,13 +6609,14 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
|
||||
}
|
||||
|
||||
static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
|
||||
Scope *scope, AstNode *source_node)
|
||||
Scope *scope, AstNode *source_node, Buf *out_bare_name)
|
||||
{
|
||||
if (exec->name) {
|
||||
ZigType *import = get_scope_import(scope);
|
||||
Buf *namespace_name = buf_create_from_buf(&import->name);
|
||||
if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
|
||||
buf_append_buf(namespace_name, exec->name);
|
||||
buf_init_from_buf(out_bare_name, exec->name);
|
||||
return namespace_name;
|
||||
} else if (exec->name_fn != nullptr) {
|
||||
Buf *name = buf_alloc();
|
||||
@ -6623,6 +6624,7 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
|
||||
buf_appendf(name, "(");
|
||||
render_instance_name_recursive(codegen, name, &exec->name_fn->fndef_scope->base, exec->begin_scope);
|
||||
buf_appendf(name, ")");
|
||||
buf_init_from_buf(out_bare_name, name);
|
||||
return name;
|
||||
} else {
|
||||
ZigType *import = get_scope_import(scope);
|
||||
@ -6630,6 +6632,7 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
|
||||
if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
|
||||
buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
|
||||
source_node->line + 1, source_node->column + 1);
|
||||
buf_init_from_buf(out_bare_name, namespace_name);
|
||||
return namespace_name;
|
||||
}
|
||||
}
|
||||
@ -6655,11 +6658,12 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
|
||||
assert(node->type == NodeTypeContainerDecl);
|
||||
|
||||
ContainerKind kind = node->data.container_decl.kind;
|
||||
Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node);
|
||||
Buf *bare_name = buf_alloc();
|
||||
Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node, bare_name);
|
||||
|
||||
ContainerLayout layout = node->data.container_decl.layout;
|
||||
ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,
|
||||
kind, node, buf_ptr(name), layout);
|
||||
kind, node, buf_ptr(name), bare_name, layout);
|
||||
ScopeDecls *child_scope = get_container_scope(container_type);
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
TldContainer *tld_container = allocate<TldContainer>(1);
|
||||
init_tld(&tld_container->base, TldIdContainer, name, VisibModPub, node, parent_scope);
|
||||
init_tld(&tld_container->base, TldIdContainer, bare_name, VisibModPub, node, parent_scope);
|
||||
tld_container->type_entry = container_type;
|
||||
tld_container->decls_scope = child_scope;
|
||||
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
|
||||
|
||||
uint32_t err_count = node->data.err_set_decl.decls.length;
|
||||
|
||||
Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node);
|
||||
Buf bare_name = BUF_INIT;
|
||||
Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node, &bare_name);
|
||||
ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
|
||||
buf_init_from_buf(&err_set_type->name, type_name);
|
||||
err_set_type->data.error_set.err_count = err_count;
|
||||
@ -18680,7 +18685,15 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc
|
||||
return ira->codegen->invalid_instruction;
|
||||
|
||||
if (!type_entry->cached_const_name_val) {
|
||||
type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, &type_entry->name);
|
||||
Buf *name;
|
||||
if (is_container(type_entry)) {
|
||||
name = get_container_scope(type_entry)->bare_name;
|
||||
} else if (type_entry->id == ZigTypeIdOpaque) {
|
||||
name = type_entry->data.opaque.bare_name;
|
||||
} else {
|
||||
name = &type_entry->name;
|
||||
}
|
||||
type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, name);
|
||||
}
|
||||
IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
|
||||
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
|
||||
// for this DIFile
|
||||
root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
|
||||
buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));
|
||||
ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name), root_struct);
|
||||
ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name),
|
||||
namespace_name, root_struct);
|
||||
|
||||
ZigList<ErrorMsg *> errors = {0};
|
||||
|
||||
@ -21668,10 +21682,11 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru
|
||||
}
|
||||
|
||||
static IrInstruction *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) {
|
||||
Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
|
||||
instruction->base.scope, instruction->base.source_node);
|
||||
Buf *bare_name = buf_alloc();
|
||||
Buf *full_name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
|
||||
instruction->base.scope, instruction->base.source_node, bare_name);
|
||||
ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,
|
||||
buf_ptr(name));
|
||||
buf_ptr(full_name), bare_name);
|
||||
return ir_const_type(ira, &instruction->base, result_type);
|
||||
}
|
||||
|
||||
|
@ -1231,10 +1231,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
\\
|
||||
\\fn bar(x: *b.Foo) void {}
|
||||
,
|
||||
"tmp.zig:6:10: error: expected type '*Foo', found '*Foo'",
|
||||
"tmp.zig:6:10: note: pointer type child 'Foo' cannot cast into pointer type child 'Foo'",
|
||||
"a.zig:1:17: note: Foo declared here",
|
||||
"b.zig:1:17: note: Foo declared here",
|
||||
"tmp.zig:6:10: error: expected type '*b.Foo', found '*a.Foo'",
|
||||
"tmp.zig:6:10: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
|
||||
"a.zig:1:17: note: a.Foo declared here",
|
||||
"b.zig:1:17: note: b.Foo declared here",
|
||||
);
|
||||
|
||||
tc.addSourceFile("a.zig",
|
||||
|
@ -980,15 +980,18 @@ pub const TranslateCContext = struct {
|
||||
Term.Exited => |code| {
|
||||
if (code != 0) {
|
||||
warn("Compilation failed with exit code {}\n", code);
|
||||
printInvocation(zig_args.toSliceConst());
|
||||
return error.TestFailed;
|
||||
}
|
||||
},
|
||||
Term.Signal => |code| {
|
||||
warn("Compilation failed with signal {}\n", code);
|
||||
printInvocation(zig_args.toSliceConst());
|
||||
return error.TestFailed;
|
||||
},
|
||||
else => {
|
||||
warn("Compilation terminated unexpectedly\n");
|
||||
printInvocation(zig_args.toSliceConst());
|
||||
return error.TestFailed;
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user