diff --git a/src/all_types.hpp b/src/all_types.hpp index e094f0f8eb..8129fa8e6b 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1076,6 +1076,7 @@ enum ResolveStatus { struct ZigPackage { Buf root_src_dir; Buf root_src_path; // relative to root_src_dir + Buf pkg_path; // a.b.c.d which follows the package dependency chain from the root package // reminder: hash tables must be initialized before use HashMap package_table; @@ -1089,7 +1090,6 @@ struct RootStruct { Buf *source_code; AstNode *c_import_node; ZigLLVMDIFile *di_file; - bool scanned; }; struct ZigTypeStruct { @@ -1678,8 +1678,6 @@ struct CodeGen { HashMap string_literals_table; HashMap type_info_cache; - ZigList import_queue; - size_t import_queue_index; ZigList resolve_queue; size_t resolve_queue_index; ZigList use_queue; @@ -3472,6 +3470,8 @@ static const size_t stack_trace_ptr_count = 30; #define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr" #define RESULT_PTR_FIELD_NAME "result_ptr" +#define NAMESPACE_SEP_CHAR '.' +#define NAMESPACE_SEP_STR "." enum FloatMode { FloatModeStrict, diff --git a/src/analyze.cpp b/src/analyze.cpp index a6944e8514..5019f7c7cd 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1295,7 +1295,7 @@ 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 *entry = new_type_table_entry(ZigTypeIdStruct); - entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, entry); + entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry); entry->data.structure.root_struct = root_struct; entry->data.structure.layout = ContainerLayoutAuto; entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name); @@ -3230,27 +3230,16 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { return ErrorNone; } -static void get_fully_qualified_decl_name_internal(Buf *buf, Scope *scope, uint8_t sep) { - if (!scope) - return; - - if (scope->id == ScopeIdDecls) { - get_fully_qualified_decl_name_internal(buf, scope->parent, sep); - - ScopeDecls *scope_decls = (ScopeDecls *)scope; - if (scope_decls->container_type) { - buf_append_buf(buf, &scope_decls->container_type->name); - buf_append_char(buf, sep); - } - return; - } - - get_fully_qualified_decl_name_internal(buf, scope->parent, sep); -} - -static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) { +static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) { buf_resize(buf, 0); - get_fully_qualified_decl_name_internal(buf, tld->parent_scope, sep); + + Scope *scope = tld->parent_scope; + while (scope->id != ScopeIdDecls) { + scope = scope->parent; + } + ScopeDecls *decls_scope = reinterpret_cast(scope); + buf_append_buf(buf, &decls_scope->container_type->name); + buf_append_char(buf, NAMESPACE_SEP_CHAR); buf_append_buf(buf, tld->name); } @@ -3285,8 +3274,7 @@ static bool scope_is_root_decls(Scope *scope) { while (scope) { if (scope->id == ScopeIdDecls) { ScopeDecls *scope_decls = (ScopeDecls *)scope; - return scope_decls->container_type == nullptr || - is_top_level_struct(scope_decls->container_type); + return is_top_level_struct(scope_decls->container_type); } scope = scope->parent; } @@ -3302,7 +3290,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) { AstNode *fake_decl = allocate(1); *fake_decl = *panic_fn->proto_node; fake_decl->type = NodeTypeSymbol; - fake_decl->data.symbol_expr.symbol = &panic_fn->symbol_name; + fake_decl->data.symbol_expr.symbol = tld_fn->base.name; // call this for the side effects of casting to panic_fn_type analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr); @@ -3355,16 +3343,21 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { AstNode *fn_def_node = fn_proto->fn_def_node; ZigFn *fn_table_entry = create_fn(g, source_node); - get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_'); + tld_fn->fn_entry = fn_table_entry; + + bool is_extern = (fn_table_entry->body_node == nullptr); + if (fn_proto->is_export || is_extern) { + buf_init_from_buf(&fn_table_entry->symbol_name, tld_fn->base.name); + } else { + get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base); + } if (fn_proto->is_export) { bool ccc = (fn_proto->cc == CallingConventionUnspecified || fn_proto->cc == CallingConventionC); add_fn_export(g, fn_table_entry, &fn_table_entry->symbol_name, GlobalLinkageIdStrong, ccc); } - tld_fn->fn_entry = fn_table_entry; - - if (fn_table_entry->body_node) { + if (!is_extern) { fn_table_entry->fndef_scope = create_fndef_scope(g, fn_table_entry->body_node, tld_fn->base.parent_scope, fn_table_entry); @@ -3405,10 +3398,10 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { if (scope_is_root_decls(tld_fn->base.parent_scope) && (import == g->root_import || import->data.structure.root_struct->package == g->panic_package)) { - if (g->have_pub_main && buf_eql_str(&fn_table_entry->symbol_name, "main")) { + if (g->have_pub_main && buf_eql_str(tld_fn->base.name, "main")) { g->main_fn = fn_table_entry; } else if ((import->data.structure.root_struct->package == g->panic_package || g->have_pub_panic) && - buf_eql_str(&fn_table_entry->symbol_name, "panic")) + buf_eql_str(tld_fn->base.name, "panic")) { g->panic_fn = fn_table_entry; g->panic_tld_fn = tld_fn; @@ -3417,7 +3410,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { } else if (source_node->type == NodeTypeTestDecl) { ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto); - get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_'); + get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base); tld_fn->fn_entry = fn_table_entry; @@ -3973,6 +3966,12 @@ ZigFn *scope_fn_entry(Scope *scope) { return nullptr; } +ZigPackage *scope_package(Scope *scope) { + ZigType *import = get_scope_import(scope); + assert(is_top_level_struct(import)); + return import->data.structure.root_struct->package; +} + TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) { assert(enum_type->id == ZigTypeIdEnum); if (enum_type->data.enumeration.src_field_count == 0) @@ -4437,7 +4436,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) { node->data.use.value = result; } -ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code) { +ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code, + SourceKind source_kind) +{ if (g->verbose_tokenize) { fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path)); fprintf(stderr, "----------------\n"); @@ -4470,14 +4471,29 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu os_path_split(resolved_path, src_dirname, src_basename); Buf noextname = BUF_INIT; - os_path_extname(src_basename, &noextname, nullptr); + os_path_extname(resolved_path, &noextname, nullptr); + + Buf *pkg_root_src_dir = &package->root_src_dir; + Buf resolved_root_src_dir = os_path_resolve(&pkg_root_src_dir, 1); + Buf namespace_name = BUF_INIT; + buf_init_from_buf(&namespace_name, &package->pkg_path); + if (buf_len(&namespace_name) != 0) buf_append_char(&namespace_name, NAMESPACE_SEP_CHAR); + buf_append_mem(&namespace_name, buf_ptr(&noextname) + buf_len(&resolved_root_src_dir) + 1, + buf_len(&noextname) - (buf_len(&resolved_root_src_dir) + 1)); + buf_replace(&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR); + RootStruct *root_struct = allocate(1); root_struct->package = package; root_struct->source_code = source_code; 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(&noextname), root_struct); + ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), root_struct); + if (source_kind == SourceKindRoot) { + assert(g->root_import == nullptr); + g->root_import = import_entry; + } + g->import_table.put(resolved_path, import_entry); AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color); assert(root_node != nullptr); @@ -4488,48 +4504,44 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu ast_print(stderr, root_node, 0); } - g->import_table.put(resolved_path, import_entry); - g->import_queue.append(import_entry); + if (source_kind == SourceKindRoot || package == g->panic_package) { + // Look for panic and main + for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) { + AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i); - for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) { - AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i); + if (top_level_decl->type == NodeTypeFnDef) { + AstNode *proto_node = top_level_decl->data.fn_def.fn_proto; + assert(proto_node->type == NodeTypeFnProto); + Buf *proto_name = proto_node->data.fn_proto.name; - if (top_level_decl->type == NodeTypeFnDef) { - AstNode *proto_node = top_level_decl->data.fn_def.fn_proto; - assert(proto_node->type == NodeTypeFnProto); - Buf *proto_name = proto_node->data.fn_proto.name; - - bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub); - bool ok_cc = (proto_node->data.fn_proto.cc == CallingConventionUnspecified || - proto_node->data.fn_proto.cc == CallingConventionCold); - - if (is_pub && ok_cc) { - if (buf_eql_str(proto_name, "main")) { - g->have_pub_main = true; - g->subsystem = TargetSubsystemConsole; - } else if (buf_eql_str(proto_name, "panic")) { - g->have_pub_panic = true; + bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub); + if (is_pub) { + if (buf_eql_str(proto_name, "main")) { + g->have_pub_main = true; + g->subsystem = TargetSubsystemConsole; + } else if (buf_eql_str(proto_name, "panic")) { + g->have_pub_panic = true; + } } } } } + for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) { + AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i); + scan_decls(g, import_entry->data.structure.decls_scope, top_level_decl); + } + + TldContainer *tld_container = allocate(1); + init_tld(&tld_container->base, TldIdContainer, &namespace_name, VisibModPub, root_node, nullptr); + tld_container->type_entry = import_entry; + tld_container->decls_scope = import_entry->data.structure.decls_scope; + g->resolve_queue.append(&tld_container->base); + return import_entry; } -void scan_import(CodeGen *g, ZigType *import) { - if (!import->data.structure.root_struct->scanned) { - import->data.structure.root_struct->scanned = true; - scan_decls(g, import->data.structure.decls_scope, import->data.structure.decl_node); - } -} - void semantic_analyze(CodeGen *g) { - for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) { - ZigType *import = g->import_queue.at(g->import_queue_index); - scan_import(g, import); - } - for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) { AstNode *use_decl_node = g->use_queue.at(g->use_queue_index); preview_use_decl(g, use_decl_node); diff --git a/src/analyze.hpp b/src/analyze.hpp index 58b27d71de..05216dfe46 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -47,8 +47,12 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry); bool ptr_allows_addr_zero(ZigType *ptr_type); bool type_is_nonnull_ptr(ZigType *type); -ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code); - +enum SourceKind { + SourceKindRoot, + SourceKindNonRoot, +}; +ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code, + SourceKind source_kind); ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope); Tld *find_decl(CodeGen *g, Scope *scope, Buf *name); @@ -78,10 +82,10 @@ bool is_array_ref(ZigType *type_entry); bool is_container_ref(ZigType *type_entry); bool is_valid_vector_elem_type(ZigType *elem_type); void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node); -void scan_import(CodeGen *g, ZigType *import); void preview_use_decl(CodeGen *g, AstNode *node); void resolve_use_decl(CodeGen *g, AstNode *node); ZigFn *scope_fn_entry(Scope *scope); +ZigPackage *scope_package(Scope *scope); ZigType *get_scope_import(Scope *scope); void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope); ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name, diff --git a/src/buffer.hpp b/src/buffer.hpp index e286f7dc63..789abea3e9 100644 --- a/src/buffer.hpp +++ b/src/buffer.hpp @@ -59,7 +59,7 @@ static inline void buf_deinit(Buf *buf) { static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) { assert(len != SIZE_MAX); buf->list.resize(len + 1); - safe_memcpy(buf_ptr(buf), ptr, len); + memcpy(buf_ptr(buf), ptr, len); buf->list.at(buf_len(buf)) = 0; } @@ -98,7 +98,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) { assert(end <= buf_len(in_buf)); Buf *out_buf = allocate(1); out_buf->list.resize(end - start + 1); - safe_memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start); + memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start); out_buf->list.at(buf_len(out_buf)) = 0; return out_buf; } @@ -108,7 +108,7 @@ static inline void buf_append_mem(Buf *buf, const char *mem, size_t mem_len) { assert(mem_len != SIZE_MAX); size_t old_len = buf_len(buf); buf_resize(buf, old_len + mem_len); - safe_memcpy(buf_ptr(buf) + old_len, mem, mem_len); + memcpy(buf_ptr(buf) + old_len, mem, mem_len); buf->list.at(buf_len(buf)) = 0; } diff --git a/src/codegen.cpp b/src/codegen.cpp index ed486c0d43..e43628fcfc 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) { } } -static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path) { +static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) { ZigPackage *entry = allocate(1); entry->package_table.init(4); buf_init_from_str(&entry->root_src_dir, root_src_dir); buf_init_from_str(&entry->root_src_path, root_src_path); + buf_init_from_str(&entry->pkg_path, pkg_path); return entry; } -ZigPackage *new_anonymous_package(void) { - return new_package("", ""); +ZigPackage *new_anonymous_package() { + return new_package("", "", ""); } static const char *symbols_that_llvm_depends_on[] = { @@ -141,11 +142,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out exit(1); } - g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename)); - g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig"); + g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename), ""); + g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig", "std"); g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); } else { - g->root_package = new_package(".", ""); + g->root_package = new_package(".", "", ""); } g->zig_std_special_dir = buf_alloc(); @@ -7742,12 +7743,12 @@ static Error define_builtin_compile_vars(CodeGen *g) { assert(g->root_package); assert(g->std_package); - g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename); + g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename, "builtin"); g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); g->std_package->package_table.put(buf_create_from_str("std"), g->std_package); - g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents); - scan_import(g, g->compile_var_import); + g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents, + SourceKindNonRoot); return ErrorNone; } @@ -7986,21 +7987,21 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err)); } - return add_source_file(g, package, resolved_path, import_code); + return add_source_file(g, package, resolved_path, import_code, SourceKindNonRoot); } static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) { - ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig"); + ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special"); package->package_table.put(buf_create_from_str("@root"), pkg_with_main); return package; } static ZigPackage *create_test_runner_pkg(CodeGen *g) { - return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig"); + return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special"); } static ZigPackage *create_panic_pkg(CodeGen *g) { - return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig"); + return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special"); } static void create_test_compile_var_and_add_test_runner(CodeGen *g) { @@ -8084,7 +8085,8 @@ static void gen_root_source(CodeGen *g) { exit(1); } - g->root_import = add_source_file(g, g->root_package, resolved_path, source_code); + ZigType *root_import_alias = add_source_file(g, g->root_package, resolved_path, source_code, SourceKindRoot); + assert(root_import_alias == g->root_import); assert(g->root_out_name); assert(g->out_type != OutTypeUnknown); @@ -8098,7 +8100,6 @@ static void gen_root_source(CodeGen *g) { g->panic_package = create_panic_pkg(g); import_with_panic = add_special_code(g, g->panic_package, "panic.zig"); } - scan_import(g, import_with_panic); Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic")); assert(panic_tld != nullptr); resolve_top_level_decl(g, panic_tld, nullptr); @@ -9021,9 +9022,11 @@ void codegen_build_and_link(CodeGen *g) { codegen_add_time_event(g, "Done"); } -ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) { +ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path, + const char *pkg_path) +{ init(g); - ZigPackage *pkg = new_package(root_src_dir, root_src_path); + ZigPackage *pkg = new_package(root_src_dir, root_src_path, pkg_path); if (g->std_package != nullptr) { assert(g->compile_var_package != nullptr); pkg->package_table.put(buf_create_from_str("std"), g->std_package); diff --git a/src/codegen.hpp b/src/codegen.hpp index 147b5d2db4..eb30eeb9a7 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -48,7 +48,8 @@ void codegen_print_timing_report(CodeGen *g, FILE *f); void codegen_link(CodeGen *g); void codegen_build_and_link(CodeGen *g); -ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path); +ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path, + const char *pkg_path); void codegen_add_assembly(CodeGen *g, Buf *path); void codegen_add_object(CodeGen *g, Buf *object_path); diff --git a/src/ir.cpp b/src/ir.cpp index c7c16bd07e..65db8054ff 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6608,9 +6608,15 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o return true; } -static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name, AstNode *source_node) { +static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name, + Scope *scope, AstNode *source_node) +{ if (exec->name) { - return exec->name; + ZigPackage *cur_scope_pkg = scope_package(scope); + Buf *namespace_name = buf_create_from_buf(&cur_scope_pkg->pkg_path); + if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR); + buf_append_buf(namespace_name, exec->name); + return namespace_name; } else if (exec->name_fn != nullptr) { Buf *name = buf_alloc(); buf_append_buf(name, &exec->name_fn->symbol_name); @@ -6619,37 +6625,52 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char buf_appendf(name, ")"); return name; } else { - // Note: C-imports do not have valid location information - // TODO this will get fixed by https://github.com/ziglang/zig/issues/2015 - return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name, - (source_node->owner->data.structure.root_struct->path != nullptr) ? - buf_ptr(source_node->owner->data.structure.root_struct->path) : - "(null)", source_node->line + 1, source_node->column + 1); + ZigPackage *cur_scope_pkg = scope_package(scope); + Buf *namespace_name = buf_create_from_buf(&cur_scope_pkg->pkg_path); + 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); + return namespace_name; } } +static void get_namespace_name(Buf *buf, Scope *scope, uint8_t sep) { + if (!scope) + return; + + if (scope->id == ScopeIdDecls) { + get_namespace_name(buf, scope->parent, sep); + + ScopeDecls *scope_decls = (ScopeDecls *)scope; + if (scope_decls->container_type) { + buf_append_buf(buf, &scope_decls->container_type->name); + buf_append_char(buf, sep); + } + return; + } + + get_namespace_name(buf, scope->parent, sep); +} static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) { assert(node->type == NodeTypeContainerDecl); ContainerKind kind = node->data.container_decl.kind; - Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), node); - - VisibMod visib_mod = VisibModPub; - TldContainer *tld_container = allocate(1); - init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope); + Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node); ContainerLayout layout = node->data.container_decl.layout; ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope, kind, node, buf_ptr(name), layout); ScopeDecls *child_scope = get_container_scope(container_type); - tld_container->type_entry = container_type; - tld_container->decls_scope = child_scope; - for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) { AstNode *child_node = node->data.container_decl.decls.at(i); scan_decls(irb->codegen, child_scope, child_node); } + + TldContainer *tld_container = allocate(1); + init_tld(&tld_container->base, TldIdContainer, 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); // Add this to the list to mark as invalid if analyzing this exec fails. @@ -6734,7 +6755,7 @@ 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 set", node); + Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node); 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; @@ -17018,9 +17039,8 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio } } - ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code); - - scan_import(ira->codegen, target_import); + ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code, + SourceKindNonRoot); return ir_const_type(ira, &import_instruction->base, target_import); } @@ -18658,14 +18678,20 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct if (type_is_invalid(cimport_result->type)) return ira->codegen->invalid_instruction; + ZigPackage *cur_scope_pkg = scope_package(instruction->base.scope); + Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, + buf_ptr(&cur_scope_pkg->pkg_path), node->line + 1, node->column + 1); + RootStruct *root_struct = allocate(1); root_struct->package = new_anonymous_package(); root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package); root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package); root_struct->c_import_node = node; + // TODO create namespace_name file in zig-cache instead of /tmp and use it + // 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, "cimport", root_struct); + ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name), root_struct); ZigList errors = {0}; @@ -21614,7 +21640,8 @@ 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.source_node); + Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque", + instruction->base.scope, instruction->base.source_node); ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node, buf_ptr(name)); return ir_const_type(ira, &instruction->base, result_type); diff --git a/src/main.cpp b/src/main.cpp index eb92db3576..966d7d59d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -223,7 +223,8 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) { Buf *basename = buf_alloc(); os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename); - ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename)); + ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename), + buf_ptr(buf_sprintf("%s.%s", buf_ptr(&pkg->pkg_path), child_cli_pkg->name))); auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg); if (entry) { ZigPackage *existing_pkg = entry->value; @@ -544,7 +545,7 @@ int main(int argc, char **argv) { } ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), - buf_ptr(&build_file_basename)); + buf_ptr(&build_file_basename), "std.special"); g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg); g->enable_cache = get_cache_opt(enable_cache, true); codegen_build_and_link(g); diff --git a/src/os.cpp b/src/os.cpp index 732baea359..e418aa2c29 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -264,7 +264,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) { buf_append_buf(out_full_path, basename); } -int os_path_real(Buf *rel_path, Buf *out_abs_path) { +Error os_path_real(Buf *rel_path, Buf *out_abs_path) { #if defined(ZIG_OS_WINDOWS) buf_resize(out_abs_path, 4096); if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) { diff --git a/src/os.hpp b/src/os.hpp index c4fae1d62b..2e0b1ea88f 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -96,7 +96,7 @@ void os_path_dirname(Buf *full_path, Buf *out_dirname); void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename); void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname); void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path); -int os_path_real(Buf *rel_path, Buf *out_abs_path); +Error os_path_real(Buf *rel_path, Buf *out_abs_path); Buf os_path_resolve(Buf **paths_ptr, size_t paths_len); bool os_path_is_absolute(Buf *path); diff --git a/src/util.hpp b/src/util.hpp index a0e759567e..abf4d37c88 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -93,18 +93,6 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) { return ptr; } -template -static inline void safe_memcpy(T *dest, const T *src, size_t count) { -#ifdef NDEBUG - memcpy(dest, src, count * sizeof(T)); -#else - // manually assign every elment to trigger compile error for non-copyable structs - for (size_t i = 0; i < count; i += 1) { - dest[i] = src[i]; - } -#endif -} - template static inline T *reallocate(T *old, size_t old_count, size_t new_count) { T *ptr = reallocate_nonzero(old, old_count, new_count); diff --git a/test/stage1/behavior/asm.zig b/test/stage1/behavior/asm.zig index 845d80777a..414fa42e1e 100644 --- a/test/stage1/behavior/asm.zig +++ b/test/stage1/behavior/asm.zig @@ -4,16 +4,16 @@ const expect = @import("std").testing.expect; comptime { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { asm volatile ( - \\.globl aoeu; - \\.type aoeu, @function; - \\.set aoeu, derp; + \\.globl this_is_my_alias; + \\.type this_is_my_alias, @function; + \\.set this_is_my_alias, derp; ); } } test "module level assembly" { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { - expect(aoeu() == 1234); + expect(this_is_my_alias() == 1234); } } @@ -85,7 +85,7 @@ test "sized integer/float in asm input" { ); } -extern fn aoeu() i32; +extern fn this_is_my_alias() i32; export fn derp() i32 { return 1234; diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index 1ff9687836..a2d752457c 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -1,5 +1,6 @@ const std = @import("std"); const expect = std.testing.expect; +const expectEqualSlices = std.testing.expectEqualSlices; const mem = std.mem; const cstr = std.cstr; const builtin = @import("builtin"); @@ -488,7 +489,7 @@ test "@typeName" { expect(mem.eql(u8, @typeName(i64), "i64")); expect(mem.eql(u8, @typeName(*usize), "*usize")); // https://github.com/ziglang/zig/issues/675 - expect(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)")); + expectEqualSlices(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))); expect(mem.eql(u8, @typeName(Struct), "Struct")); expect(mem.eql(u8, @typeName(Union), "Union")); expect(mem.eql(u8, @typeName(Enum), "Enum"));