aboutsummaryrefslogtreecommitdiff
path: root/src/Compilation.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-04-08 19:05:05 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-04-08 19:05:05 -0700
commit482b995a4963e045860c7fb1a4e11c48cf4de880 (patch)
treee00079980426580bbefd547e879d890dab842511 /src/Compilation.zig
parentb9e508c410cd077d704a73418281f6d7839df241 (diff)
downloadzig-482b995a4963e045860c7fb1a4e11c48cf4de880.tar.gz
zig-482b995a4963e045860c7fb1a4e11c48cf4de880.zip
stage2: blaze the trail for std lib integration
This branch adds "builtin" and "std" to the import table when using the self-hosted backend. "builtin" gains one additional item: ``` pub const zig_is_stage2 = true; // false when using stage1 backend ``` This allows the std lib to do conditional compilation based on detecting which backend is being used. This will be removed from builtin as soon as self-hosted catches up to feature parity with stage1. Keep a sharp eye out - people are going to be tempted to abuse this. The general rule of thumb is do not use `builtin.zig_is_stage2`. However this commit breaks the rule so that we can gain limited start.zig support as we incrementally improve the self-hosted compiler. This commit also implements `fullyQualifiedNameHash` and related functionality, which effectively puts all Decls in their proper namespaces. `fullyQualifiedName` is not yet implemented. Stop printing "todo" log messages for test decls unless we are in test mode. Add "previous definition here" error notes for Decl name collisions. This commit does not bring us yet to a newly passing test case. Here's what I'm working towards: ```zig const std = @import("std"); export fn main() c_int { const a = std.fs.base64_alphabet[0]; return a - 'A'; } ``` Current output: ``` $ ./zig-cache/bin/zig build-exe test.zig test.zig:3:1: error: TODO implement more analyze elemptr zig-cache/lib/zig/std/start.zig:38:46: error: TODO implement structInitExpr ty ``` So the next steps are clear: * Sema: improve elemptr * AstGen: implement structInitExpr
Diffstat (limited to 'src/Compilation.zig')
-rw-r--r--src/Compilation.zig89
1 files changed, 59 insertions, 30 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index 080d4bddaa..73fafa0976 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -906,38 +906,61 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
artifact_sub_dir,
};
- // TODO when we implement serialization and deserialization of incremental compilation metadata,
- // this is where we would load it. We have open a handle to the directory where
- // the output either already is, or will be.
+ // If we rely on stage1, we must not redundantly add these packages.
+ const use_stage1 = build_options.is_stage1 and use_llvm;
+ if (!use_stage1) {
+ const builtin_pkg = try Package.createWithDir(
+ gpa,
+ zig_cache_artifact_directory,
+ null,
+ "builtin.zig",
+ );
+ errdefer builtin_pkg.destroy(gpa);
+
+ const std_pkg = try Package.createWithDir(
+ gpa,
+ options.zig_lib_directory,
+ "std",
+ "std.zig",
+ );
+ errdefer std_pkg.destroy(gpa);
+
+ try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
+ try root_pkg.add(gpa, "root", root_pkg);
+ try root_pkg.addAndAdopt(gpa, "std", std_pkg);
+
+ try std_pkg.add(gpa, "builtin", builtin_pkg);
+ try std_pkg.add(gpa, "root", root_pkg);
+ }
+
+ // TODO when we implement serialization and deserialization of incremental
+ // compilation metadata, this is where we would load it. We have open a handle
+ // to the directory where the output either already is, or will be.
// However we currently do not have serialization of such metadata, so for now
// we set up an empty Module that does the entire compilation fresh.
- const root_scope = rs: {
- if (mem.endsWith(u8, root_pkg.root_src_path, ".zig")) {
- const root_scope = try gpa.create(Module.Scope.File);
- const struct_ty = try Type.Tag.empty_struct.create(
- gpa,
- &root_scope.root_container,
- );
- root_scope.* = .{
- // TODO this is duped so it can be freed in Container.deinit
- .sub_file_path = try gpa.dupe(u8, root_pkg.root_src_path),
- .source = .{ .unloaded = {} },
- .tree = undefined,
- .status = .never_loaded,
- .pkg = root_pkg,
- .root_container = .{
- .file_scope = root_scope,
- .decls = .{},
- .ty = struct_ty,
- },
- };
- break :rs root_scope;
- } else if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) {
- return error.ZirFilesUnsupported;
- } else {
- unreachable;
- }
+ // TODO remove CLI support for .zir files and then we can remove this error
+ // handling and assertion.
+ if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) return error.ZirFilesUnsupported;
+ assert(mem.endsWith(u8, root_pkg.root_src_path, ".zig"));
+
+ const root_scope = try gpa.create(Module.Scope.File);
+ errdefer gpa.destroy(root_scope);
+
+ const struct_ty = try Type.Tag.empty_struct.create(gpa, &root_scope.root_container);
+ root_scope.* = .{
+ // TODO this is duped so it can be freed in Container.deinit
+ .sub_file_path = try gpa.dupe(u8, root_pkg.root_src_path),
+ .source = .{ .unloaded = {} },
+ .tree = undefined,
+ .status = .never_loaded,
+ .pkg = root_pkg,
+ .root_container = .{
+ .file_scope = root_scope,
+ .decls = .{},
+ .ty = struct_ty,
+ .parent_name_hash = root_pkg.namespace_hash,
+ },
};
const module = try arena.create(Module);
@@ -1339,7 +1362,8 @@ pub fn update(self: *Compilation) !void {
self.c_object_work_queue.writeItemAssumeCapacity(entry.key);
}
- const use_stage1 = build_options.omit_stage2 or build_options.is_stage1 and self.bin_file.options.use_llvm;
+ const use_stage1 = build_options.omit_stage2 or
+ (build_options.is_stage1 and self.bin_file.options.use_llvm);
if (!use_stage1) {
if (self.bin_file.options.module) |module| {
module.compile_log_text.shrinkAndFree(module.gpa, 0);
@@ -2840,6 +2864,8 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
const target = comp.getTarget();
const generic_arch_name = target.cpu.arch.genericName();
+ const use_stage1 = build_options.omit_stage2 or
+ (build_options.is_stage1 and comp.bin_file.options.use_llvm);
@setEvalBranchQuota(4000);
try buffer.writer().print(
@@ -2852,6 +2878,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
\\/// Zig version. When writing code that supports multiple versions of Zig, prefer
\\/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
\\pub const zig_version = try @import("std").SemanticVersion.parse("{s}");
+ \\pub const zig_is_stage2 = {};
\\
\\pub const output_mode = OutputMode.{};
\\pub const link_mode = LinkMode.{};
@@ -2865,6 +2892,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
\\
, .{
build_options.version,
+ !use_stage1,
std.zig.fmtId(@tagName(comp.bin_file.options.output_mode)),
std.zig.fmtId(@tagName(comp.bin_file.options.link_mode)),
comp.bin_file.options.is_test,
@@ -3074,6 +3102,7 @@ fn buildOutputFromZig(
.handle = special_dir,
},
.root_src_path = src_basename,
+ .namespace_hash = Package.root_namespace_hash,
};
const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
const target = comp.getTarget();