From 5424b4320def194b205dcfe8e937035d2d80ae09 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Feb 2019 10:11:32 -0500 Subject: remove namespace type; files are empty structs closes #1047 --- src/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index d2099a9f80..eb92db3576 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -215,7 +215,7 @@ struct CliPkg { CliPkg *parent; }; -static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) { +static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) { for (size_t i = 0; i < cli_pkg->children.length; i += 1) { CliPkg *child_cli_pkg = cli_pkg->children.at(i); @@ -223,10 +223,10 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) { Buf *basename = buf_alloc(); os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename); - PackageTableEntry *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)); auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg); if (entry) { - PackageTableEntry *existing_pkg = entry->value; + ZigPackage *existing_pkg = entry->value; Buf *full_path = buf_alloc(); os_path_join(&existing_pkg->root_src_dir, &existing_pkg->root_src_path, full_path); fprintf(stderr, "Unable to add package '%s'->'%s': already exists as '%s'\n", @@ -543,7 +543,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), + ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), buf_ptr(&build_file_basename)); g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg); g->enable_cache = get_cache_opt(enable_cache, true); @@ -1145,7 +1145,7 @@ int main(int argc, char **argv) { } } else if (cmd == CmdTranslateC) { codegen_translate_c(g, in_file_buf); - ast_render(g, stdout, g->root_import->root, 4); + ast_render(g, stdout, g->root_import->data.structure.decl_node, 4); if (timing_info) codegen_print_timing_report(g, stdout); return EXIT_SUCCESS; -- cgit v1.2.3 From 02f3a834b014da10e487cb3150120c0dc0a4c119 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Feb 2019 15:40:57 -0500 Subject: struct types get fully qualified names and function symbol names become fully qualified --- src/all_types.hpp | 6 +- src/analyze.cpp | 140 +++++++++++++++++++++++------------------- src/analyze.hpp | 10 ++- src/buffer.hpp | 6 +- src/codegen.cpp | 37 ++++++----- src/codegen.hpp | 3 +- src/ir.cpp | 71 ++++++++++++++------- src/main.cpp | 5 +- src/os.cpp | 2 +- src/os.hpp | 2 +- src/util.hpp | 12 ---- test/stage1/behavior/asm.zig | 10 +-- test/stage1/behavior/misc.zig | 3 +- 13 files changed, 172 insertions(+), 135 deletions(-) (limited to 'src/main.cpp') 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); +static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) { + buf_resize(buf, 0); - 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; + Scope *scope = tld->parent_scope; + while (scope->id != ScopeIdDecls) { + scope = scope->parent; } - - get_fully_qualified_decl_name_internal(buf, scope->parent, sep); -} - -static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) { - buf_resize(buf, 0); - get_fully_qualified_decl_name_internal(buf, tld->parent_scope, sep); + 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; + } } } } } - 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); + 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 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")); -- cgit v1.2.3 From 67b4de33d2729fdb21337e5a0e05f6273bce23ba Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 2 Mar 2019 10:38:27 -0500 Subject: compile error for import outside package path closes #2024 there's a new cli option `--main-pkg-path` which you can use to choose a different root package directory besides the one inferred from the root source file and a corresponding build.zig API: foo.setMainPkgPath(path) --- CMakeLists.txt | 2 +- src/analyze.cpp | 3 +++ src/codegen.cpp | 37 ++++++++++++++++++++++++-------- src/codegen.hpp | 4 ++-- src/ir.cpp | 12 +++++++++++ src/link.cpp | 2 +- src/main.cpp | 21 ++++++++++-------- std/build.zig | 11 ++++++++++ std/os/windows/tls.zig | 36 ------------------------------- std/special/bootstrap.zig | 2 +- std/special/bootstrap_windows_tls.zig | 36 +++++++++++++++++++++++++++++++ std/special/builtin.zig | 2 +- test/build_examples.zig | 1 + test/compile_errors.zig | 9 ++++++++ test/standalone/main_pkg_path/a/test.zig | 5 +++++ test/standalone/main_pkg_path/b.zig | 1 + test/standalone/main_pkg_path/build.zig | 9 ++++++++ 17 files changed, 133 insertions(+), 60 deletions(-) delete mode 100644 std/os/windows/tls.zig create mode 100644 std/special/bootstrap_windows_tls.zig create mode 100644 test/standalone/main_pkg_path/a/test.zig create mode 100644 test/standalone/main_pkg_path/b.zig create mode 100644 test/standalone/main_pkg_path/build.zig (limited to 'src/main.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 830a3456cf..f2d9f392ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -606,7 +606,6 @@ set(ZIG_STD_FILES "os/windows/ntdll.zig" "os/windows/ole32.zig" "os/windows/shell32.zig" - "os/windows/tls.zig" "os/windows/util.zig" "os/zen.zig" "pdb.zig" @@ -617,6 +616,7 @@ set(ZIG_STD_FILES "sort.zig" "special/bootstrap.zig" "special/bootstrap_lib.zig" + "special/bootstrap_windows_tls.zig" "special/build_runner.zig" "special/builtin.zig" "special/compiler_rt/addXf3.zig" diff --git a/src/analyze.cpp b/src/analyze.cpp index 089f72483f..30e147c62d 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4502,6 +4502,9 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu Buf *pkg_root_src_dir = &package->root_src_dir; Buf resolved_root_src_dir = os_path_resolve(&pkg_root_src_dir, 1); + + assert(buf_starts_with_buf(resolved_path, &resolved_root_src_dir)); + Buf namespace_name = BUF_INIT; buf_init_from_buf(&namespace_name, &package->pkg_path); if (source_kind == SourceKindNonRoot) { diff --git a/src/codegen.cpp b/src/codegen.cpp index 650cbee946..77832e215d 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -88,8 +88,8 @@ static const char *symbols_that_llvm_depends_on[] = { // TODO probably all of compiler-rt needs to go here }; -CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode, - Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc) +CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, + OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc) { CodeGen *g = allocate(1); @@ -133,16 +133,35 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out } if (root_src_path) { - Buf *src_basename = buf_alloc(); - Buf *src_dir = buf_alloc(); - os_path_split(root_src_path, src_dir, src_basename); + Buf *root_pkg_path; + Buf *rel_root_src_path; + if (main_pkg_path == nullptr) { + Buf *src_basename = buf_alloc(); + Buf *src_dir = buf_alloc(); + os_path_split(root_src_path, src_dir, src_basename); + + if (buf_len(src_basename) == 0) { + fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); + exit(1); + } + root_pkg_path = src_dir; + rel_root_src_path = src_basename; + } else { + Buf resolved_root_src_path = os_path_resolve(&root_src_path, 1); + Buf resolved_main_pkg_path = os_path_resolve(&main_pkg_path, 1); - if (buf_len(src_basename) == 0) { - fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); - exit(1); + if (!buf_starts_with_buf(&resolved_root_src_path, &resolved_main_pkg_path)) { + fprintf(stderr, "Root source path '%s' outside main package path '%s'", + buf_ptr(root_src_path), buf_ptr(main_pkg_path)); + exit(1); + } + root_pkg_path = main_pkg_path; + rel_root_src_path = buf_create_from_mem( + buf_ptr(&resolved_root_src_path) + buf_len(&resolved_main_pkg_path) + 1, + buf_len(&resolved_root_src_path) - buf_len(&resolved_main_pkg_path) - 1); } - g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename), ""); + g->root_package = new_package(buf_ptr(root_pkg_path), buf_ptr(rel_root_src_path), ""); 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 { diff --git a/src/codegen.hpp b/src/codegen.hpp index eb30eeb9a7..91598fa7be 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -15,8 +15,8 @@ #include -CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode, - Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc); +CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, + OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc); void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len); void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len); diff --git a/src/ir.cpp b/src/ir.cpp index 7c45a9d588..2452011d79 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -17026,6 +17026,18 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio return ir_const_type(ira, &import_instruction->base, import_entry->value); } + if (source_kind == SourceKindNonRoot) { + ZigPackage *cur_scope_pkg = scope_package(import_instruction->base.scope); + Buf *pkg_root_src_dir = &cur_scope_pkg->root_src_dir; + Buf resolved_root_src_dir = os_path_resolve(&pkg_root_src_dir, 1); + if (!buf_starts_with_buf(resolved_path, &resolved_root_src_dir)) { + ir_add_error_node(ira, source_node, + buf_sprintf("import of file outside package path: '%s'", + buf_ptr(import_target_path))); + return ira->codegen->invalid_instruction; + } + } + if ((err = file_fetch(ira->codegen, resolved_path, import_code))) { if (err == ErrorFileNotFound) { ir_add_error_node(ira, source_node, diff --git a/src/link.cpp b/src/link.cpp index 7eb8cb111f..2e30dc6230 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -41,7 +41,7 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) child_out_type = OutTypeObj; } - CodeGen *child_gen = codegen_create(full_path, parent_gen->zig_target, child_out_type, + CodeGen *child_gen = codegen_create(nullptr, full_path, parent_gen->zig_target, child_out_type, parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, parent_gen->libc); diff --git a/src/main.cpp b/src/main.cpp index 966d7d59d7..9078059dc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " --output-lib [file] override import library path\n" " --pkg-begin [name] [path] make pkg available to import and push current pkg\n" " --pkg-end pop current pkg\n" + " --main-pkg-path set the directory of the root package\n" " --release-fast build with optimizations on and safety off\n" " --release-safe build with optimizations on and safety on\n" " --release-small build with size optimizations on and safety off\n" @@ -438,6 +439,7 @@ int main(int argc, char **argv) { TargetSubsystem subsystem = TargetSubsystemAuto; bool is_single_threaded = false; Buf *override_std_dir = nullptr; + Buf *main_pkg_path = nullptr; ValgrindSupport valgrind_support = ValgrindSupportAuto; if (argc >= 2 && strcmp(argv[1], "build") == 0) { @@ -476,8 +478,8 @@ int main(int argc, char **argv) { ZigTarget target; get_native_target(&target); - CodeGen *g = codegen_create(build_runner_path, &target, OutTypeExe, BuildModeDebug, get_zig_lib_dir(), - override_std_dir, nullptr); + CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe, + BuildModeDebug, get_zig_lib_dir(), override_std_dir, nullptr); g->valgrind_support = valgrind_support; g->enable_time_report = timing_info; buf_init_from_str(&g->cache_dir, cache_dir ? cache_dir : default_zig_cache_name); @@ -567,8 +569,8 @@ int main(int argc, char **argv) { get_native_target(&target); Buf *fmt_runner_path = buf_alloc(); os_path_join(get_zig_special_dir(), buf_create_from_str("fmt_runner.zig"), fmt_runner_path); - CodeGen *g = codegen_create(fmt_runner_path, &target, OutTypeExe, BuildModeDebug, get_zig_lib_dir(), - nullptr, nullptr); + CodeGen *g = codegen_create(main_pkg_path, fmt_runner_path, &target, OutTypeExe, + BuildModeDebug, get_zig_lib_dir(), nullptr, nullptr); buf_init_from_str(&g->cache_dir, cache_dir ? cache_dir : default_zig_cache_name); g->valgrind_support = valgrind_support; g->is_single_threaded = true; @@ -729,6 +731,8 @@ int main(int argc, char **argv) { llvm_argv.append(argv[i]); } else if (strcmp(arg, "--override-std-dir") == 0) { override_std_dir = buf_create_from_str(argv[i]); + } else if (strcmp(arg, "--main-pkg-path") == 0) { + main_pkg_path = buf_create_from_str(argv[i]); } else if (strcmp(arg, "--library-path") == 0 || strcmp(arg, "-L") == 0) { lib_dirs.append(argv[i]); } else if (strcmp(arg, "--library") == 0) { @@ -908,8 +912,8 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } case CmdBuiltin: { - CodeGen *g = codegen_create(nullptr, &target, out_type, build_mode, get_zig_lib_dir(), override_std_dir, - nullptr); + CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, + out_type, build_mode, get_zig_lib_dir(), override_std_dir, nullptr); g->valgrind_support = valgrind_support; g->is_single_threaded = is_single_threaded; Buf *builtin_source = codegen_generate_builtin_source(g); @@ -1012,8 +1016,8 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } } - CodeGen *g = codegen_create(zig_root_source_file, &target, out_type, build_mode, get_zig_lib_dir(), - override_std_dir, libc); + CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode, + get_zig_lib_dir(), override_std_dir, libc); g->valgrind_support = valgrind_support; g->subsystem = subsystem; @@ -1096,7 +1100,6 @@ int main(int argc, char **argv) { if (out_file_lib != nullptr && out_type == OutTypeLib && !is_static) codegen_set_output_lib_path(g, buf_create_from_str(out_file_lib)); - add_package(g, cur_pkg, g->root_package); if (cmd == CmdBuild || cmd == CmdRun || cmd == CmdTest) { diff --git a/std/build.zig b/std/build.zig index e3fdfbccee..b7f76a53af 100644 --- a/std/build.zig +++ b/std/build.zig @@ -859,6 +859,7 @@ pub const LibExeObjStep = struct { verbose_cc: bool, c_std: Builder.CStd, override_std_dir: ?[]const u8, + main_pkg_path: ?[]const u8, exec_cmd_args: ?[]const ?[]const u8, name_prefix: []const u8, filter: ?[]const u8, @@ -950,6 +951,7 @@ pub const LibExeObjStep = struct { .c_std = Builder.CStd.C99, .system_linker_hack = false, .override_std_dir = null, + .main_pkg_path = null, .exec_cmd_args = null, .name_prefix = "", .filter = null, @@ -1098,6 +1100,10 @@ pub const LibExeObjStep = struct { self.override_std_dir = dir_path; } + pub fn setMainPkgPath(self: *LibExeObjStep, dir_path: []const u8) void { + self.main_pkg_path = dir_path; + } + pub fn setOutputPath(self: *LibExeObjStep, file_path: []const u8) void { self.output_path = file_path; @@ -1424,6 +1430,11 @@ pub const LibExeObjStep = struct { try zig_args.append(builder.pathFromRoot(dir)); } + if (self.main_pkg_path) |dir| { + try zig_args.append("--main-pkg-path"); + try zig_args.append(builder.pathFromRoot(dir)); + } + try builder.spawnChild(zig_args.toSliceConst()); if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) { diff --git a/std/os/windows/tls.zig b/std/os/windows/tls.zig deleted file mode 100644 index 9e62a7c5c6..0000000000 --- a/std/os/windows/tls.zig +++ /dev/null @@ -1,36 +0,0 @@ -const std = @import("../../index.zig"); - -export var _tls_index: u32 = std.os.windows.TLS_OUT_OF_INDEXES; -export var _tls_start: u8 linksection(".tls") = 0; -export var _tls_end: u8 linksection(".tls$ZZZ") = 0; -export var __xl_a: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null; -export var __xl_z: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null; - -// TODO this is how I would like it to be expressed -// TODO also note, ReactOS has a +1 on StartAddressOfRawData and AddressOfCallBacks. Investigate -// why they do that. -//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY { -// .StartAddressOfRawData = @ptrToInt(&_tls_start), -// .EndAddressOfRawData = @ptrToInt(&_tls_end), -// .AddressOfIndex = @ptrToInt(&_tls_index), -// .AddressOfCallBacks = @ptrToInt(__xl_a), -// .SizeOfZeroFill = 0, -// .Characteristics = 0, -//}; -// This is the workaround because we can't do @ptrToInt at comptime like that. -pub const IMAGE_TLS_DIRECTORY = extern struct { - StartAddressOfRawData: *c_void, - EndAddressOfRawData: *c_void, - AddressOfIndex: *c_void, - AddressOfCallBacks: *c_void, - SizeOfZeroFill: u32, - Characteristics: u32, -}; -export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY { - .StartAddressOfRawData = &_tls_start, - .EndAddressOfRawData = &_tls_end, - .AddressOfIndex = &_tls_index, - .AddressOfCallBacks = &__xl_a, - .SizeOfZeroFill = 0, - .Characteristics = 0, -}; diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 950fd5cbb9..04b29d6709 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -46,7 +46,7 @@ nakedcc fn _start() noreturn { extern fn WinMainCRTStartup() noreturn { @setAlignStack(16); if (!builtin.single_threaded) { - _ = @import("../os/windows/tls.zig"); + _ = @import("bootstrap_windows_tls.zig"); } std.os.windows.ExitProcess(callMain()); } diff --git a/std/special/bootstrap_windows_tls.zig b/std/special/bootstrap_windows_tls.zig new file mode 100644 index 0000000000..71165d355b --- /dev/null +++ b/std/special/bootstrap_windows_tls.zig @@ -0,0 +1,36 @@ +const std = @import("std"); + +export var _tls_index: u32 = std.os.windows.TLS_OUT_OF_INDEXES; +export var _tls_start: u8 linksection(".tls") = 0; +export var _tls_end: u8 linksection(".tls$ZZZ") = 0; +export var __xl_a: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null; +export var __xl_z: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null; + +// TODO this is how I would like it to be expressed +// TODO also note, ReactOS has a +1 on StartAddressOfRawData and AddressOfCallBacks. Investigate +// why they do that. +//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY { +// .StartAddressOfRawData = @ptrToInt(&_tls_start), +// .EndAddressOfRawData = @ptrToInt(&_tls_end), +// .AddressOfIndex = @ptrToInt(&_tls_index), +// .AddressOfCallBacks = @ptrToInt(__xl_a), +// .SizeOfZeroFill = 0, +// .Characteristics = 0, +//}; +// This is the workaround because we can't do @ptrToInt at comptime like that. +pub const IMAGE_TLS_DIRECTORY = extern struct { + StartAddressOfRawData: *c_void, + EndAddressOfRawData: *c_void, + AddressOfIndex: *c_void, + AddressOfCallBacks: *c_void, + SizeOfZeroFill: u32, + Characteristics: u32, +}; +export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{ + .StartAddressOfRawData = &_tls_start, + .EndAddressOfRawData = &_tls_end, + .AddressOfIndex = &_tls_index, + .AddressOfCallBacks = &__xl_a, + .SizeOfZeroFill = 0, + .Characteristics = 0, +}; diff --git a/std/special/builtin.zig b/std/special/builtin.zig index ad50eecc27..38c1493ba2 100644 --- a/std/special/builtin.zig +++ b/std/special/builtin.zig @@ -134,7 +134,7 @@ nakedcc fn clone() void { } } -const math = @import("../math/index.zig"); +const math = std.math; export fn fmodf(x: f32, y: f32) f32 { return generic_fmod(f32, x, y); diff --git a/test/build_examples.zig b/test/build_examples.zig index c8a47bb093..3931648f19 100644 --- a/test/build_examples.zig +++ b/test/build_examples.zig @@ -7,6 +7,7 @@ pub fn addCases(cases: *tests.BuildExamplesContext) void { cases.addC("example/hello_world/hello_libc.zig"); cases.add("example/cat/main.zig"); cases.add("example/guess_number/main.zig"); + cases.addBuildFile("test/standalone/main_pkg_path/build.zig"); cases.addBuildFile("example/shared_library/build.zig"); cases.addBuildFile("example/mix_o_files/build.zig"); if (builtin.os != builtin.Os.macosx) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 3f82f2d223..92df4d2d14 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,15 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "import outside package path", + \\comptime{ + \\ _ = @import("../a.zig"); + \\} + , + "tmp.zig:2:9: error: import of file outside package path: '../a.zig'", + ); + cases.add( "bogus compile var", \\const x = @import("builtin").bogus; diff --git a/test/standalone/main_pkg_path/a/test.zig b/test/standalone/main_pkg_path/a/test.zig new file mode 100644 index 0000000000..286489912b --- /dev/null +++ b/test/standalone/main_pkg_path/a/test.zig @@ -0,0 +1,5 @@ +const b = @import("../b.zig"); + +test "main pkg path" { + b.foo(); +} diff --git a/test/standalone/main_pkg_path/b.zig b/test/standalone/main_pkg_path/b.zig new file mode 100644 index 0000000000..87d61fa63e --- /dev/null +++ b/test/standalone/main_pkg_path/b.zig @@ -0,0 +1 @@ +pub fn foo() void {} diff --git a/test/standalone/main_pkg_path/build.zig b/test/standalone/main_pkg_path/build.zig new file mode 100644 index 0000000000..c4ac18f967 --- /dev/null +++ b/test/standalone/main_pkg_path/build.zig @@ -0,0 +1,9 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const test_exe = b.addTest("a/test.zig"); + test_exe.setMainPkgPath("."); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&test_exe.step); +} -- cgit v1.2.3