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 {...@@ -1076,6 +1076,7 @@ enum ResolveStatus {
1076struct ZigPackage {1076struct ZigPackage {
1077 Buf root_src_dir;1077 Buf root_src_dir;
1078 Buf root_src_path; // relative to root_src_dir1078 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
1080 // reminder: hash tables must be initialized before use1081 // reminder: hash tables must be initialized before use
1081 HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;1082 HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;
...@@ -1089,7 +1090,6 @@ struct RootStruct {...@@ -1089,7 +1090,6 @@ struct RootStruct {
1089 Buf *source_code;1090 Buf *source_code;
1090 AstNode *c_import_node;1091 AstNode *c_import_node;
1091 ZigLLVMDIFile *di_file;1092 ZigLLVMDIFile *di_file;
1092 bool scanned;
1093};1093};
10941094
1095struct ZigTypeStruct {1095struct ZigTypeStruct {
...@@ -1678,8 +1678,6 @@ struct CodeGen {...@@ -1678,8 +1678,6 @@ struct CodeGen {
1678 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;1678 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
1679 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;1679 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;
1683 ZigList<Tld *> resolve_queue;1681 ZigList<Tld *> resolve_queue;
1684 size_t resolve_queue_index;1682 size_t resolve_queue_index;
1685 ZigList<AstNode *> use_queue;1683 ZigList<AstNode *> use_queue;
...@@ -3472,6 +3470,8 @@ static const size_t stack_trace_ptr_count = 30;...@@ -3472,6 +3470,8 @@ static const size_t stack_trace_ptr_count = 30;
3472#define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr"3470#define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr"
3473#define RESULT_PTR_FIELD_NAME "result_ptr"3471#define RESULT_PTR_FIELD_NAME "result_ptr"
34743472
3473#define NAMESPACE_SEP_CHAR '.'
3474#define NAMESPACE_SEP_STR "."
34753475
3476enum FloatMode {3476enum FloatMode {
3477 FloatModeStrict,3477 FloatModeStrict,
src/analyze.cpp+76-64
...@@ -1295,7 +1295,7 @@ static ZigTypeId container_to_type(ContainerKind kind) {...@@ -1295,7 +1295,7 @@ static ZigTypeId container_to_type(ContainerKind kind) {
1295// This is like get_partial_container_type except it's for the implicit root struct of files.1295// 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) {1296ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct) {
1297 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);1297 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);
1299 entry->data.structure.root_struct = root_struct;1299 entry->data.structure.root_struct = root_struct;
1300 entry->data.structure.layout = ContainerLayoutAuto;1300 entry->data.structure.layout = ContainerLayoutAuto;
1301 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);1301 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
...@@ -3230,27 +3230,16 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3230,27 +3230,16 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3230 return ErrorNone;3230 return ErrorNone;
3231}3231}
32323232
3233static void get_fully_qualified_decl_name_internal(Buf *buf, Scope *scope, uint8_t sep) {3233static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {
3234 if (!scope)3234 buf_resize(buf, 0);
3235 return;
3236
3237 if (scope->id == ScopeIdDecls) {
3238 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);
32393235
3240 ScopeDecls *scope_decls = (ScopeDecls *)scope;3236 Scope *scope = tld->parent_scope;
3241 if (scope_decls->container_type) {3237 while (scope->id != ScopeIdDecls) {
3242 buf_append_buf(buf, &scope_decls->container_type->name);3238 scope = scope->parent;
3243 buf_append_char(buf, sep);
3244 }
3245 return;
3246 }3239 }
32473240 ScopeDecls *decls_scope = reinterpret_cast<ScopeDecls *>(scope);
3248 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);3241 buf_append_buf(buf, &decls_scope->container_type->name);
3249}3242 buf_append_char(buf, NAMESPACE_SEP_CHAR);
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);
3254 buf_append_buf(buf, tld->name);3243 buf_append_buf(buf, tld->name);
3255}3244}
32563245
...@@ -3285,8 +3274,7 @@ static bool scope_is_root_decls(Scope *scope) {...@@ -3285,8 +3274,7 @@ static bool scope_is_root_decls(Scope *scope) {
3285 while (scope) {3274 while (scope) {
3286 if (scope->id == ScopeIdDecls) {3275 if (scope->id == ScopeIdDecls) {
3287 ScopeDecls *scope_decls = (ScopeDecls *)scope;3276 ScopeDecls *scope_decls = (ScopeDecls *)scope;
3288 return scope_decls->container_type == nullptr ||3277 return is_top_level_struct(scope_decls->container_type);
3289 is_top_level_struct(scope_decls->container_type);
3290 }3278 }
3291 scope = scope->parent;3279 scope = scope->parent;
3292 }3280 }
...@@ -3302,7 +3290,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) {...@@ -3302,7 +3290,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) {
3302 AstNode *fake_decl = allocate<AstNode>(1);3290 AstNode *fake_decl = allocate<AstNode>(1);
3303 *fake_decl = *panic_fn->proto_node;3291 *fake_decl = *panic_fn->proto_node;
3304 fake_decl->type = NodeTypeSymbol;3292 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
3307 // call this for the side effects of casting to panic_fn_type3295 // call this for the side effects of casting to panic_fn_type
3308 analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr);3296 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) {...@@ -3355,16 +3343,21 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3355 AstNode *fn_def_node = fn_proto->fn_def_node;3343 AstNode *fn_def_node = fn_proto->fn_def_node;
33563344
3357 ZigFn *fn_table_entry = create_fn(g, source_node);3345 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
3360 if (fn_proto->is_export) {3355 if (fn_proto->is_export) {
3361 bool ccc = (fn_proto->cc == CallingConventionUnspecified || fn_proto->cc == CallingConventionC);3356 bool ccc = (fn_proto->cc == CallingConventionUnspecified || fn_proto->cc == CallingConventionC);
3362 add_fn_export(g, fn_table_entry, &fn_table_entry->symbol_name, GlobalLinkageIdStrong, ccc);3357 add_fn_export(g, fn_table_entry, &fn_table_entry->symbol_name, GlobalLinkageIdStrong, ccc);
3363 }3358 }
33643359
3365 tld_fn->fn_entry = fn_table_entry;3360 if (!is_extern) {
3366
3367 if (fn_table_entry->body_node) {
3368 fn_table_entry->fndef_scope = create_fndef_scope(g,3361 fn_table_entry->fndef_scope = create_fndef_scope(g,
3369 fn_table_entry->body_node, tld_fn->base.parent_scope, fn_table_entry);3362 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) {...@@ -3405,10 +3398,10 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3405 if (scope_is_root_decls(tld_fn->base.parent_scope) &&3398 if (scope_is_root_decls(tld_fn->base.parent_scope) &&
3406 (import == g->root_import || import->data.structure.root_struct->package == g->panic_package))3399 (import == g->root_import || import->data.structure.root_struct->package == g->panic_package))
3407 {3400 {
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")) {
3409 g->main_fn = fn_table_entry;3402 g->main_fn = fn_table_entry;
3410 } else if ((import->data.structure.root_struct->package == g->panic_package || g->have_pub_panic) &&3403 } 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"))
3412 {3405 {
3413 g->panic_fn = fn_table_entry;3406 g->panic_fn = fn_table_entry;
3414 g->panic_tld_fn = tld_fn;3407 g->panic_tld_fn = tld_fn;
...@@ -3417,7 +3410,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -3417,7 +3410,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3417 } else if (source_node->type == NodeTypeTestDecl) {3410 } else if (source_node->type == NodeTypeTestDecl) {
3418 ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto);3411 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
3422 tld_fn->fn_entry = fn_table_entry;3415 tld_fn->fn_entry = fn_table_entry;
34233416
...@@ -3973,6 +3966,12 @@ ZigFn *scope_fn_entry(Scope *scope) {...@@ -3973,6 +3966,12 @@ ZigFn *scope_fn_entry(Scope *scope) {
3973 return nullptr;3966 return nullptr;
3974}3967}
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
3976TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {3975TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {
3977 assert(enum_type->id == ZigTypeIdEnum);3976 assert(enum_type->id == ZigTypeIdEnum);
3978 if (enum_type->data.enumeration.src_field_count == 0)3977 if (enum_type->data.enumeration.src_field_count == 0)
...@@ -4437,7 +4436,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) {...@@ -4437,7 +4436,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
4437 node->data.use.value = result;4436 node->data.use.value = result;
4438}4437}
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{
4441 if (g->verbose_tokenize) {4442 if (g->verbose_tokenize) {
4442 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));4443 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));
4443 fprintf(stderr, "----------------\n");4444 fprintf(stderr, "----------------\n");
...@@ -4470,14 +4471,29 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu...@@ -4470,14 +4471,29 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
4470 os_path_split(resolved_path, src_dirname, src_basename);4471 os_path_split(resolved_path, src_dirname, src_basename);
44714472
4472 Buf noextname = BUF_INIT;4473 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
4474 RootStruct *root_struct = allocate<RootStruct>(1);4485 RootStruct *root_struct = allocate<RootStruct>(1);
4475 root_struct->package = package;4486 root_struct->package = package;
4476 root_struct->source_code = source_code;4487 root_struct->source_code = source_code;
4477 root_struct->line_offsets = tokenization.line_offsets;4488 root_struct->line_offsets = tokenization.line_offsets;
4478 root_struct->path = resolved_path;4489 root_struct->path = resolved_path;
4479 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));4490 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
4482 AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);4498 AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
4483 assert(root_node != nullptr);4499 assert(root_node != nullptr);
...@@ -4488,48 +4504,44 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu...@@ -4488,48 +4504,44 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
4488 ast_print(stderr, root_node, 0);4504 ast_print(stderr, root_node, 0);
4489 }4505 }
44904506
4491 g->import_table.put(resolved_path, import_entry);4507 if (source_kind == SourceKindRoot || package == g->panic_package) {
4492 g->import_queue.append(import_entry);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) {4512 if (top_level_decl->type == NodeTypeFnDef) {
4495 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);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) {4517 bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub);
4498 AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;4518 if (is_pub) {
4499 assert(proto_node->type == NodeTypeFnProto);4519 if (buf_eql_str(proto_name, "main")) {
4500 Buf *proto_name = proto_node->data.fn_proto.name;4520 g->have_pub_main = true;
45014521 g->subsystem = TargetSubsystemConsole;
4502 bool is_pub = (proto_node->data.fn_proto.visib_mod == VisibModPub);4522 } else if (buf_eql_str(proto_name, "panic")) {
4503 bool ok_cc = (proto_node->data.fn_proto.cc == CallingConventionUnspecified ||4523 g->have_pub_panic = true;
4504 proto_node->data.fn_proto.cc == CallingConventionCold);4524 }
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;
4512 }4525 }
4513 }4526 }
4514 }4527 }
4515 }4528 }
45164529
4517 return import_entry;4530 for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
4518}4531 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
45194532 scan_decls(g, import_entry->data.structure.decls_scope, top_level_decl);
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);
4524 }4533 }
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;
4525}4542}
45264543
4527void semantic_analyze(CodeGen *g) {4544void 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
4533 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {4545 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {
4534 AstNode *use_decl_node = g->use_queue.at(g->use_queue_index);4546 AstNode *use_decl_node = g->use_queue.at(g->use_queue_index);
4535 preview_use_decl(g, use_decl_node);4547 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);...@@ -47,8 +47,12 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
47bool ptr_allows_addr_zero(ZigType *ptr_type);47bool ptr_allows_addr_zero(ZigType *ptr_type);
48bool type_is_nonnull_ptr(ZigType *type);48bool type_is_nonnull_ptr(ZigType *type);
4949
50ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code);50enum SourceKind {
5151 SourceKindRoot,
52 SourceKindNonRoot,
53};
54ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code,
55 SourceKind source_kind);
5256
53ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);57ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
54Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);58Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
...@@ -78,10 +82,10 @@ bool is_array_ref(ZigType *type_entry);...@@ -78,10 +82,10 @@ bool is_array_ref(ZigType *type_entry);
78bool is_container_ref(ZigType *type_entry);82bool is_container_ref(ZigType *type_entry);
79bool is_valid_vector_elem_type(ZigType *elem_type);83bool is_valid_vector_elem_type(ZigType *elem_type);
80void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);84void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);
81void scan_import(CodeGen *g, ZigType *import);
82void preview_use_decl(CodeGen *g, AstNode *node);85void preview_use_decl(CodeGen *g, AstNode *node);
83void resolve_use_decl(CodeGen *g, AstNode *node);86void resolve_use_decl(CodeGen *g, AstNode *node);
84ZigFn *scope_fn_entry(Scope *scope);87ZigFn *scope_fn_entry(Scope *scope);
88ZigPackage *scope_package(Scope *scope);
85ZigType *get_scope_import(Scope *scope);89ZigType *get_scope_import(Scope *scope);
86void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);90void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
87ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,91ZigVar *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) {...@@ -59,7 +59,7 @@ static inline void buf_deinit(Buf *buf) {
59static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {59static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {
60 assert(len != SIZE_MAX);60 assert(len != SIZE_MAX);
61 buf->list.resize(len + 1);61 buf->list.resize(len + 1);
62 safe_memcpy(buf_ptr(buf), ptr, len);62 memcpy(buf_ptr(buf), ptr, len);
63 buf->list.at(buf_len(buf)) = 0;63 buf->list.at(buf_len(buf)) = 0;
64}64}
6565
...@@ -98,7 +98,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {...@@ -98,7 +98,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {
98 assert(end <= buf_len(in_buf));98 assert(end <= buf_len(in_buf));
99 Buf *out_buf = allocate<Buf>(1);99 Buf *out_buf = allocate<Buf>(1);
100 out_buf->list.resize(end - start + 1);100 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);
102 out_buf->list.at(buf_len(out_buf)) = 0;102 out_buf->list.at(buf_len(out_buf)) = 0;
103 return out_buf;103 return out_buf;
104}104}
...@@ -108,7 +108,7 @@ static inline void buf_append_mem(Buf *buf, const char *mem, size_t mem_len) {...@@ -108,7 +108,7 @@ static inline void buf_append_mem(Buf *buf, const char *mem, size_t mem_len) {
108 assert(mem_len != SIZE_MAX);108 assert(mem_len != SIZE_MAX);
109 size_t old_len = buf_len(buf);109 size_t old_len = buf_len(buf);
110 buf_resize(buf, old_len + mem_len);110 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);
112 buf->list.at(buf_len(buf)) = 0;112 buf->list.at(buf_len(buf)) = 0;
113}113}
114114
src/codegen.cpp+20-17
...@@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) {...@@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) {
48 }48 }
49}49}
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) {
52 ZigPackage *entry = allocate<ZigPackage>(1);52 ZigPackage *entry = allocate<ZigPackage>(1);
53 entry->package_table.init(4);53 entry->package_table.init(4);
54 buf_init_from_str(&entry->root_src_dir, root_src_dir);54 buf_init_from_str(&entry->root_src_dir, root_src_dir);
55 buf_init_from_str(&entry->root_src_path, root_src_path);55 buf_init_from_str(&entry->root_src_path, root_src_path);
56 buf_init_from_str(&entry->pkg_path, pkg_path);
56 return entry;57 return entry;
57}58}
5859
59ZigPackage *new_anonymous_package(void) {60ZigPackage *new_anonymous_package() {
60 return new_package("", "");61 return new_package("", "", "");
61}62}
6263
63static const char *symbols_that_llvm_depends_on[] = {64static const char *symbols_that_llvm_depends_on[] = {
...@@ -141,11 +142,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out...@@ -141,11 +142,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
141 exit(1);142 exit(1);
142 }143 }
143144
144 g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename));145 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");146 g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig", "std");
146 g->root_package->package_table.put(buf_create_from_str("std"), g->std_package);147 g->root_package->package_table.put(buf_create_from_str("std"), g->std_package);
147 } else {148 } else {
148 g->root_package = new_package(".", "");149 g->root_package = new_package(".", "", "");
149 }150 }
150151
151 g->zig_std_special_dir = buf_alloc();152 g->zig_std_special_dir = buf_alloc();
...@@ -7742,12 +7743,12 @@ static Error define_builtin_compile_vars(CodeGen *g) {...@@ -7742,12 +7743,12 @@ static Error define_builtin_compile_vars(CodeGen *g) {
77427743
7743 assert(g->root_package);7744 assert(g->root_package);
7744 assert(g->std_package);7745 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");
7746 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);7747 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
7747 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);7748 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
7748 g->std_package->package_table.put(buf_create_from_str("std"), g->std_package);7749 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 g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents,
7750 scan_import(g, g->compile_var_import);7751 SourceKindNonRoot);
77517752
7752 return ErrorNone;7753 return ErrorNone;
7753}7754}
...@@ -7986,21 +7987,21 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba...@@ -7986,21 +7987,21 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba
7986 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));7987 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
7987 }7988 }
79887989
7989 return add_source_file(g, package, resolved_path, import_code);7990 return add_source_file(g, package, resolved_path, import_code, SourceKindNonRoot);
7990}7991}
79917992
7992static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) {7993static 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");
7994 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);7995 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
7995 return package;7996 return package;
7996}7997}
79977998
7998static ZigPackage *create_test_runner_pkg(CodeGen *g) {7999static 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");
8000}8001}
80018002
8002static ZigPackage *create_panic_pkg(CodeGen *g) {8003static 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");
8004}8005}
80058006
8006static void create_test_compile_var_and_add_test_runner(CodeGen *g) {8007static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
...@@ -8084,7 +8085,8 @@ static void gen_root_source(CodeGen *g) {...@@ -8084,7 +8085,8 @@ static void gen_root_source(CodeGen *g) {
8084 exit(1);8085 exit(1);
8085 }8086 }
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
8089 assert(g->root_out_name);8091 assert(g->root_out_name);
8090 assert(g->out_type != OutTypeUnknown);8092 assert(g->out_type != OutTypeUnknown);
...@@ -8098,7 +8100,6 @@ static void gen_root_source(CodeGen *g) {...@@ -8098,7 +8100,6 @@ static void gen_root_source(CodeGen *g) {
8098 g->panic_package = create_panic_pkg(g);8100 g->panic_package = create_panic_pkg(g);
8099 import_with_panic = add_special_code(g, g->panic_package, "panic.zig");8101 import_with_panic = add_special_code(g, g->panic_package, "panic.zig");
8100 }8102 }
8101 scan_import(g, import_with_panic);
8102 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));8103 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
8103 assert(panic_tld != nullptr);8104 assert(panic_tld != nullptr);
8104 resolve_top_level_decl(g, panic_tld, nullptr);8105 resolve_top_level_decl(g, panic_tld, nullptr);
...@@ -9021,9 +9022,11 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9021,9 +9022,11 @@ void codegen_build_and_link(CodeGen *g) {
9021 codegen_add_time_event(g, "Done");9022 codegen_add_time_event(g, "Done");
9022}9023}
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{
9025 init(g);9028 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);
9027 if (g->std_package != nullptr) {9030 if (g->std_package != nullptr) {
9028 assert(g->compile_var_package != nullptr);9031 assert(g->compile_var_package != nullptr);
9029 pkg->package_table.put(buf_create_from_str("std"), g->std_package);9032 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);...@@ -48,7 +48,8 @@ void codegen_print_timing_report(CodeGen *g, FILE *f);
48void codegen_link(CodeGen *g);48void codegen_link(CodeGen *g);
49void codegen_build_and_link(CodeGen *g);49void 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);
52void codegen_add_assembly(CodeGen *g, Buf *path);53void codegen_add_assembly(CodeGen *g, Buf *path);
53void codegen_add_object(CodeGen *g, Buf *object_path);54void 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...@@ -6608,9 +6608,15 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o
6608 return true;6608 return true;
6609}6609}
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{
6612 if (exec->name) {6614 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;
6614 } else if (exec->name_fn != nullptr) {6620 } else if (exec->name_fn != nullptr) {
6615 Buf *name = buf_alloc();6621 Buf *name = buf_alloc();
6616 buf_append_buf(name, &exec->name_fn->symbol_name);6622 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...@@ -6619,37 +6625,52 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
6619 buf_appendf(name, ")");6625 buf_appendf(name, ")");
6620 return name;6626 return name;
6621 } else {6627 } else {
6622 // Note: C-imports do not have valid location information6628 ZigPackage *cur_scope_pkg = scope_package(scope);
6623 // TODO this will get fixed by https://github.com/ziglang/zig/issues/20156629 Buf *namespace_name = buf_create_from_buf(&cur_scope_pkg->pkg_path);
6624 return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name,6630 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
6625 (source_node->owner->data.structure.root_struct->path != nullptr) ?6631 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
6626 buf_ptr(source_node->owner->data.structure.root_struct->path) :6632 source_node->line + 1, source_node->column + 1);
6627 "(null)", source_node->line + 1, source_node->column + 1);6633 return namespace_name;
6628 }6634 }
6629}6635}
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}
6631static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {6654static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
6632 assert(node->type == NodeTypeContainerDecl);6655 assert(node->type == NodeTypeContainerDecl);
66336656
6634 ContainerKind kind = node->data.container_decl.kind;6657 ContainerKind kind = node->data.container_decl.kind;
6635 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), node);6658 Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), parent_scope, 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);
66406659
6641 ContainerLayout layout = node->data.container_decl.layout;6660 ContainerLayout layout = node->data.container_decl.layout;
6642 ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,6661 ZigType *container_type = get_partial_container_type(irb->codegen, parent_scope,
6643 kind, node, buf_ptr(name), layout);6662 kind, node, buf_ptr(name), layout);
6644 ScopeDecls *child_scope = get_container_scope(container_type);6663 ScopeDecls *child_scope = get_container_scope(container_type);
66456664
6646 tld_container->type_entry = container_type;
6647 tld_container->decls_scope = child_scope;
6648
6649 for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {6665 for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {
6650 AstNode *child_node = node->data.container_decl.decls.at(i);6666 AstNode *child_node = node->data.container_decl.decls.at(i);
6651 scan_decls(irb->codegen, child_scope, child_node);6667 scan_decls(irb->codegen, child_scope, child_node);
6652 }6668 }
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;
6653 irb->codegen->resolve_queue.append(&tld_container->base);6674 irb->codegen->resolve_queue.append(&tld_container->base);
66546675
6655 // Add this to the list to mark as invalid if analyzing this exec fails.6676 // 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...@@ -6734,7 +6755,7 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
67346755
6735 uint32_t err_count = node->data.err_set_decl.decls.length;6756 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);
6738 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6759 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6739 buf_init_from_buf(&err_set_type->name, type_name);6760 buf_init_from_buf(&err_set_type->name, type_name);
6740 err_set_type->data.error_set.err_count = err_count;6761 err_set_type->data.error_set.err_count = err_count;
...@@ -17018,9 +17039,8 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio...@@ -17018,9 +17039,8 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
17018 }17039 }
17019 }17040 }
1702017041
17021 ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);17042 ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code,
1702217043 SourceKindNonRoot);
17023 scan_import(ira->codegen, target_import);
1702417044
17025 return ir_const_type(ira, &import_instruction->base, target_import);17045 return ir_const_type(ira, &import_instruction->base, target_import);
17026}17046}
...@@ -18658,14 +18678,20 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct...@@ -18658,14 +18678,20 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
18658 if (type_is_invalid(cimport_result->type))18678 if (type_is_invalid(cimport_result->type))
18659 return ira->codegen->invalid_instruction;18679 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
18661 RootStruct *root_struct = allocate<RootStruct>(1);18685 RootStruct *root_struct = allocate<RootStruct>(1);
18662 root_struct->package = new_anonymous_package();18686 root_struct->package = new_anonymous_package();
18663 root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);18687 root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
18664 root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);18688 root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
18665 root_struct->c_import_node = node;18689 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
18666 root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,18692 root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
18667 buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));18693 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
18670 ZigList<ErrorMsg *> errors = {0};18696 ZigList<ErrorMsg *> errors = {0};
1867118697
...@@ -21614,7 +21640,8 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru...@@ -21614,7 +21640,8 @@ static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstru
21614}21640}
2161521641
21616static IrInstruction *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) {21642static 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);
21618 ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,21645 ZigType *result_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node,
21619 buf_ptr(name));21646 buf_ptr(name));
21620 return ir_const_type(ira, &instruction->base, result_type);21647 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) {...@@ -223,7 +223,8 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) {
223 Buf *basename = buf_alloc();223 Buf *basename = buf_alloc();
224 os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename);224 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)));
227 auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);228 auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);
228 if (entry) {229 if (entry) {
229 ZigPackage *existing_pkg = entry->value;230 ZigPackage *existing_pkg = entry->value;
...@@ -544,7 +545,7 @@ int main(int argc, char **argv) {...@@ -544,7 +545,7 @@ int main(int argc, char **argv) {
544 }545 }
545546
546 ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),547 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");
548 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);549 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
549 g->enable_cache = get_cache_opt(enable_cache, true);550 g->enable_cache = get_cache_opt(enable_cache, true);
550 codegen_build_and_link(g);551 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) {...@@ -264,7 +264,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
264 buf_append_buf(out_full_path, basename);264 buf_append_buf(out_full_path, basename);
265}265}
266266
267int os_path_real(Buf *rel_path, Buf *out_abs_path) {267Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
268#if defined(ZIG_OS_WINDOWS)268#if defined(ZIG_OS_WINDOWS)
269 buf_resize(out_abs_path, 4096);269 buf_resize(out_abs_path, 4096);
270 if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) {270 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);...@@ -96,7 +96,7 @@ void os_path_dirname(Buf *full_path, Buf *out_dirname);
96void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename);96void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename);
97void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname);97void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname);
98void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path);98void 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);
100Buf os_path_resolve(Buf **paths_ptr, size_t paths_len);100Buf os_path_resolve(Buf **paths_ptr, size_t paths_len);
101bool os_path_is_absolute(Buf *path);101bool 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) {...@@ -93,18 +93,6 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
93 return ptr;93 return ptr;
94}94}
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
108template<typename T>96template<typename T>
109static inline T *reallocate(T *old, size_t old_count, size_t new_count) {97static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
110 T *ptr = reallocate_nonzero(old, old_count, new_count);98 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;...@@ -4,16 +4,16 @@ const expect = @import("std").testing.expect;
4comptime {4comptime {
5 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {5 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
6 asm volatile (6 asm volatile (
7 \\.globl aoeu;7 \\.globl this_is_my_alias;
8 \\.type aoeu, @function;8 \\.type this_is_my_alias, @function;
9 \\.set aoeu, derp;9 \\.set this_is_my_alias, derp;
10 );10 );
11 }11 }
12}12}
1313
14test "module level assembly" {14test "module level assembly" {
15 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {15 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
16 expect(aoeu() == 1234);16 expect(this_is_my_alias() == 1234);
17 }17 }
18}18}
1919
...@@ -85,7 +85,7 @@ test "sized integer/float in asm input" {...@@ -85,7 +85,7 @@ test "sized integer/float in asm input" {
85 );85 );
86}86}
8787
88extern fn aoeu() i32;88extern fn this_is_my_alias() i32;
8989
90export fn derp() i32 {90export fn derp() i32 {
91 return 1234;91 return 1234;
test/stage1/behavior/misc.zig+2-1
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;
3const mem = std.mem;4const mem = std.mem;
4const cstr = std.cstr;5const cstr = std.cstr;
5const builtin = @import("builtin");6const builtin = @import("builtin");
...@@ -488,7 +489,7 @@ test "@typeName" {...@@ -488,7 +489,7 @@ test "@typeName" {
488 expect(mem.eql(u8, @typeName(i64), "i64"));489 expect(mem.eql(u8, @typeName(i64), "i64"));
489 expect(mem.eql(u8, @typeName(*usize), "*usize"));490 expect(mem.eql(u8, @typeName(*usize), "*usize"));
490 // https://github.com/ziglang/zig/issues/675491 // 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)));
492 expect(mem.eql(u8, @typeName(Struct), "Struct"));493 expect(mem.eql(u8, @typeName(Struct), "Struct"));
493 expect(mem.eql(u8, @typeName(Union), "Union"));494 expect(mem.eql(u8, @typeName(Union), "Union"));
494 expect(mem.eql(u8, @typeName(Enum), "Enum"));495 expect(mem.eql(u8, @typeName(Enum), "Enum"));