authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 15:40:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 15:40:57-05:00
log02f3a834b014da10e487cb3150120c0dc0a4c119
tree8e734bd4a9452b504d96f419f4766cbfe664ad97
parent5424b4320def194b205dcfe8e937035d2d80ae09
signature Commit is signed but in an unrecognized format.

struct types get fully qualified names

and function symbol names become fully qualified

13 files changed, 172 insertions(+), 135 deletions(-)

src/all_types.hpp+3-3
......@@ -1076,6 +1076,7 @@ enum ResolveStatus {
10761076struct ZigPackage {
10771077 Buf root_src_dir;
10781078 Buf root_src_path; // relative to root_src_dir
1079 Buf pkg_path; // a.b.c.d which follows the package dependency chain from the root package
10791080
10801081 // reminder: hash tables must be initialized before use
10811082 HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;
......@@ -1089,7 +1090,6 @@ struct RootStruct {
10891090 Buf *source_code;
10901091 AstNode *c_import_node;
10911092 ZigLLVMDIFile *di_file;
1092 bool scanned;
10931093};
10941094
10951095struct ZigTypeStruct {
......@@ -1678,8 +1678,6 @@ struct CodeGen {
16781678 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
16791679 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
16801680
1681 ZigList<ZigType *> import_queue;
1682 size_t import_queue_index;
16831681 ZigList<Tld *> resolve_queue;
16841682 size_t resolve_queue_index;
16851683 ZigList<AstNode *> use_queue;
......@@ -3472,6 +3470,8 @@ static const size_t stack_trace_ptr_count = 30;
34723470#define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr"
34733471#define RESULT_PTR_FIELD_NAME "result_ptr"
34743472
3473#define NAMESPACE_SEP_CHAR '.'
3474#define NAMESPACE_SEP_STR "."
34753475
34763476enum FloatMode {
34773477 FloatModeStrict,
src/analyze.cpp+76-64
......@@ -1295,7 +1295,7 @@ static ZigTypeId container_to_type(ContainerKind kind) {
12951295// This is like get_partial_container_type except it's for the implicit root struct of files.
12961296ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct) {
12971297 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
1298 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, entry);
1298 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry);
12991299 entry->data.structure.root_struct = root_struct;
13001300 entry->data.structure.layout = ContainerLayoutAuto;
13011301 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
......@@ -3230,27 +3230,16 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
32303230 return ErrorNone;
32313231}
32323232
3233static void get_fully_qualified_decl_name_internal(Buf *buf, Scope *scope, uint8_t sep) {
3234 if (!scope)
3235 return;
3236
3237 if (scope->id == ScopeIdDecls) {
3238 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);
3233static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {
3234 buf_resize(buf, 0);
32393235
3240 ScopeDecls *scope_decls = (ScopeDecls *)scope;
3241 if (scope_decls->container_type) {
3242 buf_append_buf(buf, &scope_decls->container_type->name);
3243 buf_append_char(buf, sep);
3244 }
3245 return;
3236 Scope *scope = tld->parent_scope;
3237 while (scope->id != ScopeIdDecls) {
3238 scope = scope->parent;
32463239 }
3247
3248 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);
3249}
3250
3251static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) {
3252 buf_resize(buf, 0);
3253 get_fully_qualified_decl_name_internal(buf, tld->parent_scope, sep);
3240 ScopeDecls *decls_scope = reinterpret_cast<ScopeDecls *>(scope);
3241 buf_append_buf(buf, &decls_scope->container_type->name);
3242 buf_append_char(buf, NAMESPACE_SEP_CHAR);
32543243 buf_append_buf(buf, tld->name);
32553244}
32563245
......@@ -3285,8 +3274,7 @@ static bool scope_is_root_decls(Scope *scope) {
32853274 while (scope) {
32863275 if (scope->id == ScopeIdDecls) {
32873276 ScopeDecls *scope_decls = (ScopeDecls *)scope;
3288 return scope_decls->container_type == nullptr ||
3289 is_top_level_struct(scope_decls->container_type);
3277 return is_top_level_struct(scope_decls->container_type);
32903278 }
32913279 scope = scope->parent;
32923280 }
......@@ -3302,7 +3290,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) {
33023290 AstNode *fake_decl = allocate<AstNode>(1);
33033291 *fake_decl = *panic_fn->proto_node;
33043292 fake_decl->type = NodeTypeSymbol;
3305 fake_decl->data.symbol_expr.symbol = &panic_fn->symbol_name;
3293 fake_decl->data.symbol_expr.symbol = tld_fn->base.name;
33063294
33073295 // call this for the side effects of casting to panic_fn_type
33083296 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) {
33553343 AstNode *fn_def_node = fn_proto->fn_def_node;
33563344
33573345 ZigFn *fn_table_entry = create_fn(g, source_node);
3358 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
3346 tld_fn->fn_entry = fn_table_entry;
3347
3348 bool is_extern = (fn_table_entry->body_node == nullptr);
3349 if (fn_proto->is_export || is_extern) {
3350 buf_init_from_buf(&fn_table_entry->symbol_name, tld_fn->base.name);
3351 } else {
3352 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base);
3353 }
33593354
33603355 if (fn_proto->is_export) {
33613356 bool ccc = (fn_proto->cc == CallingConventionUnspecified || fn_proto->cc == CallingConventionC);
33623357 add_fn_export(g, fn_table_entry, &fn_table_entry->symbol_name, GlobalLinkageIdStrong, ccc);
33633358 }
33643359
3365 tld_fn->fn_entry = fn_table_entry;
3366
3367 if (fn_table_entry->body_node) {
3360 if (!is_extern) {
33683361 fn_table_entry->fndef_scope = create_fndef_scope(g,
33693362 fn_table_entry->body_node, tld_fn->base.parent_scope, fn_table_entry);
33703363
......@@ -3405,10 +3398,10 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
34053398 if (scope_is_root_decls(tld_fn->base.parent_scope) &&
34063399 (import == g->root_import || import->data.structure.root_struct->package == g->panic_package))
34073400 {
3408 if (g->have_pub_main && buf_eql_str(&fn_table_entry->symbol_name, "main")) {
3401 if (g->have_pub_main && buf_eql_str(tld_fn->base.name, "main")) {
34093402 g->main_fn = fn_table_entry;
34103403 } else if ((import->data.structure.root_struct->package == g->panic_package || g->have_pub_panic) &&
3411 buf_eql_str(&fn_table_entry->symbol_name, "panic"))
3404 buf_eql_str(tld_fn->base.name, "panic"))
34123405 {
34133406 g->panic_fn = fn_table_entry;
34143407 g->panic_tld_fn = tld_fn;
......@@ -3417,7 +3410,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
34173410 } else if (source_node->type == NodeTypeTestDecl) {
34183411 ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto);
34193412
3420 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
3413 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base);
34213414
34223415 tld_fn->fn_entry = fn_table_entry;
34233416
......@@ -3973,6 +3966,12 @@ ZigFn *scope_fn_entry(Scope *scope) {
39733966 return nullptr;
39743967}
39753968
3969ZigPackage *scope_package(Scope *scope) {
3970 ZigType *import = get_scope_import(scope);
3971 assert(is_top_level_struct(import));
3972 return import->data.structure.root_struct->package;
3973}
3974
39763975TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {
39773976 assert(enum_type->id == ZigTypeIdEnum);
39783977 if (enum_type->data.enumeration.src_field_count == 0)
......@@ -4437,7 +4436,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
44374436 node->data.use.value = result;
44384437}
44394438
4440ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code) {
4439ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code,
4440 SourceKind source_kind)
4441{
44414442 if (g->verbose_tokenize) {
44424443 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));
44434444 fprintf(stderr, "----------------\n");
......@@ -4470,14 +4471,29 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
44704471 os_path_split(resolved_path, src_dirname, src_basename);
44714472
44724473 Buf noextname = BUF_INIT;
4473 os_path_extname(src_basename, &noextname, nullptr);
4474 os_path_extname(resolved_path, &noextname, nullptr);
4475
4476 Buf *pkg_root_src_dir = &package->root_src_dir;
4477 Buf resolved_root_src_dir = os_path_resolve(&pkg_root_src_dir, 1);
4478 Buf namespace_name = BUF_INIT;
4479 buf_init_from_buf(&namespace_name, &package->pkg_path);
4480 if (buf_len(&namespace_name) != 0) buf_append_char(&namespace_name, NAMESPACE_SEP_CHAR);
4481 buf_append_mem(&namespace_name, buf_ptr(&noextname) + buf_len(&resolved_root_src_dir) + 1,
4482 buf_len(&noextname) - (buf_len(&resolved_root_src_dir) + 1));
4483 buf_replace(&namespace_name, ZIG_OS_SEP_CHAR, NAMESPACE_SEP_CHAR);
4484
44744485 RootStruct *root_struct = allocate<RootStruct>(1);
44754486 root_struct->package = package;
44764487 root_struct->source_code = source_code;
44774488 root_struct->line_offsets = tokenization.line_offsets;
44784489 root_struct->path = resolved_path;
44794490 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
4480 ZigType *import_entry = get_root_container_type(g, buf_ptr(&noextname), root_struct);
4491 ZigType *import_entry = get_root_container_type(g, buf_ptr(&namespace_name), root_struct);
4492 if (source_kind == SourceKindRoot) {
4493 assert(g->root_import == nullptr);
4494 g->root_import = import_entry;
4495 }
4496 g->import_table.put(resolved_path, import_entry);
44814497
44824498 AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
44834499 assert(root_node != nullptr);
......@@ -4488,48 +4504,44 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
44884504 ast_print(stderr, root_node, 0);
44894505 }
44904506
4491 g->import_table.put(resolved_path, import_entry);
4492 g->import_queue.append(import_entry);
4507 if (source_kind == SourceKindRoot || package == g->panic_package) {
4508 // Look for panic and main
4509 for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
4510 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
44934511
4494 for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
4495 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
4512 if (top_level_decl->type == NodeTypeFnDef) {
4513 AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;
4514 assert(proto_node->type == NodeTypeFnProto);
4515 Buf *proto_name = proto_node->data.fn_proto.name;
44964516
4497 if (top_level_decl->type == NodeTypeFnDef) {
4498 AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;
4499 assert(proto_node->type == NodeTypeFnProto);
4500 Buf *proto_name = proto_node->data.fn_proto.name;
4501
4502 bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub);
4503 bool ok_cc = (proto_node->data.fn_proto.cc == CallingConventionUnspecified ||
4504 proto_node->data.fn_proto.cc == CallingConventionCold);
4505
4506 if (is_pub && ok_cc) {
4507 if (buf_eql_str(proto_name, "main")) {
4508 g->have_pub_main = true;
4509 g->subsystem = TargetSubsystemConsole;
4510 } else if (buf_eql_str(proto_name, "panic")) {
4511 g->have_pub_panic = true;
4517 bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub);
4518 if (is_pub) {
4519 if (buf_eql_str(proto_name, "main")) {
4520 g->have_pub_main = true;
4521 g->subsystem = TargetSubsystemConsole;
4522 } else if (buf_eql_str(proto_name, "panic")) {
4523 g->have_pub_panic = true;
4524 }
45124525 }
45134526 }
45144527 }
45154528 }
45164529
4517 return import_entry;
4518}
4519
4520void scan_import(CodeGen *g, ZigType *import) {
4521 if (!import->data.structure.root_struct->scanned) {
4522 import->data.structure.root_struct->scanned = true;
4523 scan_decls(g, import->data.structure.decls_scope, import->data.structure.decl_node);
4530 for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
4531 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
4532 scan_decls(g, import_entry->data.structure.decls_scope, top_level_decl);
45244533 }
4534
4535 TldContainer *tld_container = allocate<TldContainer>(1);
4536 init_tld(&tld_container->base, TldIdContainer, &namespace_name, VisibModPub, root_node, nullptr);
4537 tld_container->type_entry = import_entry;
4538 tld_container->decls_scope = import_entry->data.structure.decls_scope;
4539 g->resolve_queue.append(&tld_container->base);
4540
4541 return import_entry;
45254542}
45264543
45274544void semantic_analyze(CodeGen *g) {
4528 for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) {
4529 ZigType *import = g->import_queue.at(g->import_queue_index);
4530 scan_import(g, import);
4531 }
4532
45334545 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {
45344546 AstNode *use_decl_node = g->use_queue.at(g->use_queue_index);
45354547 preview_use_decl(g, use_decl_node);
src/analyze.hpp+7-3
......@@ -47,8 +47,12 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
4747bool ptr_allows_addr_zero(ZigType *ptr_type);
4848bool type_is_nonnull_ptr(ZigType *type);
4949
50ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code);
51
50enum SourceKind {
51 SourceKindRoot,
52 SourceKindNonRoot,
53};
54ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code,
55 SourceKind source_kind);
5256
5357ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
5458Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
......@@ -78,10 +82,10 @@ bool is_array_ref(ZigType *type_entry);
7882bool is_container_ref(ZigType *type_entry);
7983bool is_valid_vector_elem_type(ZigType *elem_type);
8084void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);
81void scan_import(CodeGen *g, ZigType *import);
8285void preview_use_decl(CodeGen *g, AstNode *node);
8386void resolve_use_decl(CodeGen *g, AstNode *node);
8487ZigFn *scope_fn_entry(Scope *scope);
88ZigPackage *scope_package(Scope *scope);
8589ZigType *get_scope_import(Scope *scope);
8690void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
8791ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
src/buffer.hpp+3-3
......@@ -59,7 +59,7 @@ static inline void buf_deinit(Buf *buf) {
5959static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {
6060 assert(len != SIZE_MAX);
6161 buf->list.resize(len + 1);
62 safe_memcpy(buf_ptr(buf), ptr, len);
62 memcpy(buf_ptr(buf), ptr, len);
6363 buf->list.at(buf_len(buf)) = 0;
6464}
6565
......@@ -98,7 +98,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {
9898 assert(end <= buf_len(in_buf));
9999 Buf *out_buf = allocate<Buf>(1);
100100 out_buf->list.resize(end - start + 1);
101 safe_memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);
101 memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);
102102 out_buf->list.at(buf_len(out_buf)) = 0;
103103 return out_buf;
104104}
......@@ -108,7 +108,7 @@ static inline void buf_append_mem(Buf *buf, const char *mem, size_t mem_len) {
108108 assert(mem_len != SIZE_MAX);
109109 size_t old_len = buf_len(buf);
110110 buf_resize(buf, old_len + mem_len);
111 safe_memcpy(buf_ptr(buf) + old_len, mem, mem_len);
111 memcpy(buf_ptr(buf) + old_len, mem, mem_len);
112112 buf->list.at(buf_len(buf)) = 0;
113113}
114114
src/codegen.cpp+20-17
......@@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) {
4848 }
4949}
5050
51static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path) {
51static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) {
5252 ZigPackage *entry = allocate<ZigPackage>(1);
5353 entry->package_table.init(4);
5454 buf_init_from_str(&entry->root_src_dir, root_src_dir);
5555 buf_init_from_str(&entry->root_src_path, root_src_path);
56 buf_init_from_str(&entry->pkg_path, pkg_path);
5657 return entry;
5758}
5859
59ZigPackage *new_anonymous_package(void) {
60 return new_package("", "");
60ZigPackage *new_anonymous_package() {
61 return new_package("", "", "");
6162}
6263
6364static const char *symbols_that_llvm_depends_on[] = {
......@@ -141,11 +142,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
141142 exit(1);
142143 }
143144
144 g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename));
145 g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig");
145 g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename), "");
146 g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig", "std");
146147 g->root_package->package_table.put(buf_create_from_str("std"), g->std_package);
147148 } else {
148 g->root_package = new_package(".", "");
149 g->root_package = new_package(".", "", "");
149150 }
150151
151152 g->zig_std_special_dir = buf_alloc();
......@@ -7742,12 +7743,12 @@ static Error define_builtin_compile_vars(CodeGen *g) {
77427743
77437744 assert(g->root_package);
77447745 assert(g->std_package);
7745 g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename);
7746 g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename, "builtin");
77467747 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
77477748 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
77487749 g->std_package->package_table.put(buf_create_from_str("std"), g->std_package);
7749 g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents);
7750 scan_import(g, g->compile_var_import);
7750 g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents,
7751 SourceKindNonRoot);
77517752
77527753 return ErrorNone;
77537754}
......@@ -7986,21 +7987,21 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba
79867987 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
79877988 }
79887989
7989 return add_source_file(g, package, resolved_path, import_code);
7990 return add_source_file(g, package, resolved_path, import_code, SourceKindNonRoot);
79907991}
79917992
79927993static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) {
7993 ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
7994 ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special");
79947995 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
79957996 return package;
79967997}
79977998
79987999static ZigPackage *create_test_runner_pkg(CodeGen *g) {
7999 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");
8000 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special");
80008001}
80018002
80028003static ZigPackage *create_panic_pkg(CodeGen *g) {
8003 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig");
8004 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special");
80048005}
80058006
80068007static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
......@@ -8084,7 +8085,8 @@ static void gen_root_source(CodeGen *g) {
80848085 exit(1);
80858086 }
80868087
8087 g->root_import = add_source_file(g, g->root_package, resolved_path, source_code);
8088 ZigType *root_import_alias = add_source_file(g, g->root_package, resolved_path, source_code, SourceKindRoot);
8089 assert(root_import_alias == g->root_import);
80888090
80898091 assert(g->root_out_name);
80908092 assert(g->out_type != OutTypeUnknown);
......@@ -8098,7 +8100,6 @@ static void gen_root_source(CodeGen *g) {
80988100 g->panic_package = create_panic_pkg(g);
80998101 import_with_panic = add_special_code(g, g->panic_package, "panic.zig");
81008102 }
8101 scan_import(g, import_with_panic);
81028103 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
81038104 assert(panic_tld != nullptr);
81048105 resolve_top_level_decl(g, panic_tld, nullptr);
......@@ -9021,9 +9022,11 @@ void codegen_build_and_link(CodeGen *g) {
90219022 codegen_add_time_event(g, "Done");
90229023}
90239024
9024ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) {
9025ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path,
9026 const char *pkg_path)
9027{
90259028 init(g);
9026 ZigPackage *pkg = new_package(root_src_dir, root_src_path);
9029 ZigPackage *pkg = new_package(root_src_dir, root_src_path, pkg_path);
90279030 if (g->std_package != nullptr) {
90289031 assert(g->compile_var_package != nullptr);
90299032 pkg->package_table.put(buf_create_from_str("std"), g->std_package);
src/codegen.hpp+2-1
......@@ -48,7 +48,8 @@ void codegen_print_timing_report(CodeGen *g, FILE *f);
4848void codegen_link(CodeGen *g);
4949void codegen_build_and_link(CodeGen *g);
5050
51ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path);
51ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path,
52 const char *pkg_path);
5253void codegen_add_assembly(CodeGen *g, Buf *path);
5354void codegen_add_object(CodeGen *g, Buf *object_path);
5455
src/ir.cpp+49-22
......@@ -6608,9 +6608,15 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
66086608 return true;
66096609}
66106610
6611static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name, AstNode *source_node) {
6611static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
6612 Scope *scope, AstNode *source_node)
6613{
66126614 if (exec->name) {
6613 return exec->name;
6615 ZigPackage *cur_scope_pkg = scope_package(scope);
6616 Buf *namespace_name = buf_create_from_buf(&cur_scope_pkg->pkg_path);
6617 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
6618 buf_append_buf(namespace_name, exec->name);
6619 return namespace_name;
66146620 } else if (exec->name_fn != nullptr) {
66156621 Buf *name = buf_alloc();
66166622 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
66196625 buf_appendf(name, ")");
66206626 return name;
66216627 } else {
6622 // Note: C-imports do not have valid location information
6623 // TODO this will get fixed by https://github.com/ziglang/zig/issues/2015
6624 return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name,
6625 (source_node->owner->data.structure.root_struct->path != nullptr) ?
6626 buf_ptr(source_node->owner->data.structure.root_struct->path) :
6627 "(null)", source_node->line + 1, source_node->column + 1);
6628 ZigPackage *cur_scope_pkg = scope_package(scope);
6629 Buf *namespace_name = buf_create_from_buf(&cur_scope_pkg->pkg_path);
6630 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
6631 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
6632 source_node->line + 1, source_node->column + 1);
6633 return namespace_name;
66286634 }
66296635}
66306636
6637static void get_namespace_name(Buf *buf, Scope *scope, uint8_t sep) {
6638 if (!scope)
6639 return;
6640
6641 if (scope->id == ScopeIdDecls) {
6642 get_namespace_name(buf, scope->parent, sep);
6643
6644 ScopeDecls *scope_decls = (ScopeDecls *)scope;
6645 if (scope_decls->container_type) {
6646 buf_append_buf(buf, &scope_decls->container_type->name);
6647 buf_append_char(buf, sep);
6648 }
6649 return;
6650 }
6651
6652 get_namespace_name(buf, scope->parent, sep);
6653}
66316654static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
66326655 assert(node->type == NodeTypeContainerDecl);
66336656
66346657 ContainerKind kind = node->data.container_decl.kind;
6635 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), node);
6636
6637 VisibMod visib_mod = VisibModPub;
6638 TldContainer *tld_container = allocate<TldContainer>(1);
6639 init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope);
6658 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, node);
66406659
66416660 ContainerLayout layout = node->data.container_decl.layout;
66426661 ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,
66436662 kind, node, buf_ptr(name), layout);
66446663 ScopeDecls *child_scope = get_container_scope(container_type);
66456664
6646 tld_container->type_entry = container_type;
6647 tld_container->decls_scope = child_scope;
6648
66496665 for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {
66506666 AstNode *child_node = node->data.container_decl.decls.at(i);
66516667 scan_decls(irb->codegen, child_scope, child_node);
66526668 }
6669
6670 TldContainer *tld_container = allocate<TldContainer>(1);
6671 init_tld(&tld_container->base, TldIdContainer, name, VisibModPub, node, parent_scope);
6672 tld_container->type_entry = container_type;
6673 tld_container->decls_scope = child_scope;
66536674 irb->codegen->resolve_queue.append(&tld_container->base);
66546675
66556676 // 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
67346755
67356756 uint32_t err_count = node->data.err_set_decl.decls.length;
67366757
6737 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error set", node);
6758 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", parent_scope, node);
67386759 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
67396760 buf_init_from_buf(&err_set_type->name, type_name);
67406761 err_set_type->data.error_set.err_count = err_count;
......@@ -17018,9 +17039,8 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
1701817039 }
1701917040 }
1702017041
17021 ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);
17022
17023 scan_import(ira->codegen, target_import);
17042 ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code,
17043 SourceKindNonRoot);
1702417044
1702517045 return ir_const_type(ira, &import_instruction->base, target_import);
1702617046}
......@@ -18658,14 +18678,20 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1865818678 if (type_is_invalid(cimport_result->type))
1865918679 return ira->codegen->invalid_instruction;
1866018680
18681 ZigPackage *cur_scope_pkg = scope_package(instruction->base.scope);
18682 Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,
18683 buf_ptr(&cur_scope_pkg->pkg_path), node->line + 1, node->column + 1);
18684
1866118685 RootStruct *root_struct = allocate<RootStruct>(1);
1866218686 root_struct->package = new_anonymous_package();
1866318687 root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
1866418688 root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
1866518689 root_struct->c_import_node = node;
18690 // TODO create namespace_name file in zig-cache instead of /tmp and use it
18691 // for this DIFile
1866618692 root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
1866718693 buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));
18668 ZigType *child_import = get_root_container_type(ira->codegen, "cimport", root_struct);
18694 ZigType *child_import = get_root_container_type(ira->codegen, buf_ptr(namespace_name), root_struct);
1866918695
1867018696 ZigList<ErrorMsg *> errors = {0};
1867118697
......@@ -21614,7 +21640,8 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru
2161421640}
2161521641
2161621642static IrInstruction *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) {
21617 Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque", instruction->base.source_node);
21643 Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque",
21644 instruction->base.scope, instruction->base.source_node);
2161821645 ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,
2161921646 buf_ptr(name));
2162021647 return ir_const_type(ira, &instruction->base, result_type);
src/main.cpp+3-2
......@@ -223,7 +223,8 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) {
223223 Buf *basename = buf_alloc();
224224 os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename);
225225
226 ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename));
226 ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename),
227 buf_ptr(buf_sprintf("%s.%s", buf_ptr(&pkg->pkg_path), child_cli_pkg->name)));
227228 auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);
228229 if (entry) {
229230 ZigPackage *existing_pkg = entry->value;
......@@ -544,7 +545,7 @@ int main(int argc, char **argv) {
544545 }
545546
546547 ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
547 buf_ptr(&build_file_basename));
548 buf_ptr(&build_file_basename), "std.special");
548549 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
549550 g->enable_cache = get_cache_opt(enable_cache, true);
550551 codegen_build_and_link(g);
src/os.cpp+1-1
......@@ -264,7 +264,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
264264 buf_append_buf(out_full_path, basename);
265265}
266266
267int os_path_real(Buf *rel_path, Buf *out_abs_path) {
267Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
268268#if defined(ZIG_OS_WINDOWS)
269269 buf_resize(out_abs_path, 4096);
270270 if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) {
src/os.hpp+1-1
......@@ -96,7 +96,7 @@ void os_path_dirname(Buf *full_path, Buf *out_dirname);
9696void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename);
9797void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname);
9898void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path);
99int os_path_real(Buf *rel_path, Buf *out_abs_path);
99Error os_path_real(Buf *rel_path, Buf *out_abs_path);
100100Buf os_path_resolve(Buf **paths_ptr, size_t paths_len);
101101bool os_path_is_absolute(Buf *path);
102102
src/util.hpp-12
......@@ -93,18 +93,6 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
9393 return ptr;
9494}
9595
96template<typename T>
97static inline void safe_memcpy(T *dest, const T *src, size_t count) {
98#ifdef NDEBUG
99 memcpy(dest, src, count * sizeof(T));
100#else
101 // manually assign every elment to trigger compile error for non-copyable structs
102 for (size_t i = 0; i < count; i += 1) {
103 dest[i] = src[i];
104 }
105#endif
106}
107
10896template<typename T>
10997static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
11098 T *ptr = reallocate_nonzero(old, old_count, new_count);
test/stage1/behavior/asm.zig+5-5
......@@ -4,16 +4,16 @@ const expect = @import("std").testing.expect;
44comptime {
55 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
66 asm volatile (
7 \\.globl aoeu;
8 \\.type aoeu, @function;
9 \\.set aoeu, derp;
7 \\.globl this_is_my_alias;
8 \\.type this_is_my_alias, @function;
9 \\.set this_is_my_alias, derp;
1010 );
1111 }
1212}
1313
1414test "module level assembly" {
1515 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
16 expect(aoeu() == 1234);
16 expect(this_is_my_alias() == 1234);
1717 }
1818}
1919
......@@ -85,7 +85,7 @@ test "sized integer/float in asm input" {
8585 );
8686}
8787
88extern fn aoeu() i32;
88extern fn this_is_my_alias() i32;
8989
9090export fn derp() i32 {
9191 return 1234;
test/stage1/behavior/misc.zig+2-1
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;
34const mem = std.mem;
45const cstr = std.cstr;
56const builtin = @import("builtin");
......@@ -488,7 +489,7 @@ test "@typeName" {
488489 expect(mem.eql(u8, @typeName(i64), "i64"));
489490 expect(mem.eql(u8, @typeName(*usize), "*usize"));
490491 // https://github.com/ziglang/zig/issues/675
491 expect(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)"));
492 expectEqualSlices(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8)));
492493 expect(mem.eql(u8, @typeName(Struct), "Struct"));
493494 expect(mem.eql(u8, @typeName(Union), "Union"));
494495 expect(mem.eql(u8, @typeName(Enum), "Enum"));