From 4ea7685a5bf2abc504571939984b9eda7441ef5d Mon Sep 17 00:00:00 2001 From: emekoi Date: Tue, 11 Jun 2019 16:05:56 -0500 Subject: made root package available to all other packages --- src/codegen.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 12b07ea6bc..cb767bcea7 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8505,10 +8505,8 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba return add_source_file(g, package, resolved_path, import_code, SourceKindPkgMain); } -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", "std.special"); - package->package_table.put(buf_create_from_str("@root"), pkg_with_main); - return package; +static ZigPackage *create_bootstrap_pkg(CodeGen *g) { + return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special"); } static ZigPackage *create_test_runner_pkg(CodeGen *g) { @@ -8632,12 +8630,12 @@ static void gen_root_source(CodeGen *g) { !g->have_c_main && !g->have_winmain && !g->have_winmain_crt_startup && ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe)) { - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap.zig"); } if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup && g->out_type == OutTypeLib && g->is_dynamic) { - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap_lib.zig"); } if (!g->error_during_imports) { @@ -8645,7 +8643,7 @@ static void gen_root_source(CodeGen *g) { } if (g->is_test_build) { create_test_compile_var_and_add_test_runner(g); - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->test_runner_package), "bootstrap.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap.zig"); if (!g->error_during_imports) { semantic_analyze(g); @@ -9629,6 +9627,13 @@ ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const c if (g->std_package != nullptr) { assert(g->compile_var_package != nullptr); pkg->package_table.put(buf_create_from_str("std"), g->std_package); + + if (g->is_test_build) { + pkg->package_table.put(buf_create_from_str("@root"), g->test_runner_package); + } else { + pkg->package_table.put(buf_create_from_str("@root"), g->root_package); + } + pkg->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); } return pkg; -- cgit v1.2.3 From e12c7d88b28200bc64a538cd4812bfe58a6b6b78 Mon Sep 17 00:00:00 2001 From: emekoi Date: Tue, 11 Jun 2019 16:06:24 -0500 Subject: made root package available to itself --- src/codegen.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index cb767bcea7..a50a416e40 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -180,6 +180,8 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget g->root_package = new_package(".", "", ""); } + g->root_package->package_table.put(buf_create_from_str("@root"), g->root_package); + g->zig_std_special_dir = buf_alloc(); os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); -- cgit v1.2.3 From 3ed6acd2d20de0a2a08ddf6549fb06a66687d96a Mon Sep 17 00:00:00 2001 From: emekoi Date: Tue, 11 Jun 2019 18:40:55 -0500 Subject: fixed infinite loop when caching packages --- src/codegen.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index a50a416e40..56bc9827c5 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -9373,6 +9373,8 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) { // TODO: I think we need a more sophisticated detection of // packages we have already seen if (entry->value != pkg) { + auto root = pkg->package_table.maybe_get(buf_create_from_str("@root")); + if (root != nullptr && entry->value == root->value) continue; cache_buf(ch, entry->key); add_cache_pkg(g, ch, entry->value); } -- cgit v1.2.3 From 69c7c5de09e16e96a6ea5207ef8b3a21e9d119f9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 27 Jun 2019 19:15:33 -0400 Subject: fixups * better detection for already seen packages * "root" instead of "@root" --- src/all_types.hpp | 2 ++ src/codegen.cpp | 30 ++++++++++++++---------------- std/special/bootstrap.zig | 5 ++--- 3 files changed, 18 insertions(+), 19 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 774a00fdb7..bb5bef04bb 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1110,6 +1110,8 @@ struct ZigPackage { // reminder: hash tables must be initialized before use HashMap package_table; + + bool added_to_cache; }; // Stuff that only applies to a struct which is the implicit root struct of a file diff --git a/src/codegen.cpp b/src/codegen.cpp index 1a01e135d2..6ad779fd24 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -180,7 +180,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget g->root_package = new_package(".", "", ""); } - g->root_package->package_table.put(buf_create_from_str("@root"), g->root_package); + g->root_package->package_table.put(buf_create_from_str("root"), g->root_package); g->zig_std_special_dir = buf_alloc(); os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); @@ -8055,6 +8055,8 @@ static Error define_builtin_compile_vars(CodeGen *g) { 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->std_package->package_table.put(buf_create_from_str("root"), + g->is_test_build ? g->test_runner_package : g->root_package); g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents, SourceKindPkgMain); @@ -8522,8 +8524,10 @@ static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *ba return add_source_file(g, package, resolved_path, import_code, SourceKindPkgMain); } -static ZigPackage *create_bootstrap_pkg(CodeGen *g) { - return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special"); +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", "std.special"); + package->package_table.put(buf_create_from_str("root"), pkg_with_main); + return package; } static ZigPackage *create_test_runner_pkg(CodeGen *g) { @@ -8647,12 +8651,12 @@ static void gen_root_source(CodeGen *g) { !g->have_c_main && !g->have_winmain && !g->have_winmain_crt_startup && ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe)) { - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); } if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup && g->out_type == OutTypeLib && g->is_dynamic) { - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap_lib.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); } if (!g->error_during_imports) { @@ -8660,7 +8664,7 @@ static void gen_root_source(CodeGen *g) { } if (g->is_test_build) { create_test_compile_var_and_add_test_runner(g); - g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap.zig"); + g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->test_runner_package), "bootstrap.zig"); if (!g->error_during_imports) { semantic_analyze(g); @@ -9375,6 +9379,7 @@ void codegen_add_time_event(CodeGen *g, const char *name) { static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) { if (buf_len(&pkg->root_src_path) == 0) return; + pkg->added_to_cache = true; Buf *rel_full_path = buf_alloc(); os_path_join(&pkg->root_src_dir, &pkg->root_src_path, rel_full_path); @@ -9386,11 +9391,7 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) { if (!entry) break; - // TODO: I think we need a more sophisticated detection of - // packages we have already seen - if (entry->value != pkg) { - auto root = pkg->package_table.maybe_get(buf_create_from_str("@root")); - if (root != nullptr && entry->value == root->value) continue; + if (!pkg->added_to_cache) { cache_buf(ch, entry->key); add_cache_pkg(g, ch, entry->value); } @@ -9648,11 +9649,8 @@ ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const c assert(g->compile_var_package != nullptr); pkg->package_table.put(buf_create_from_str("std"), g->std_package); - if (g->is_test_build) { - pkg->package_table.put(buf_create_from_str("@root"), g->test_runner_package); - } else { - pkg->package_table.put(buf_create_from_str("@root"), g->root_package); - } + ZigPackage *main_pkg = g->is_test_build ? g->test_runner_package : g->root_package; + pkg->package_table.put(buf_create_from_str("root"), main_pkg); pkg->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); } diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index f1286bd659..7177f58b8a 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -1,7 +1,6 @@ -// This file is in a package which has the root source file exposed as "@root". -// It is included in the compilation unit when exporting an executable. +// This file is included in the compilation unit when exporting an executable. -const root = @import("@root"); +const root = @import("root"); const std = @import("std"); const builtin = @import("builtin"); const assert = std.debug.assert; -- cgit v1.2.3