authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-17 15:35:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-17 16:09:20-07:00
log731c35f15a1469c9523b71476f1e069d5bcfd979
tree00b0244c86df035ff721cb45216594426007ce96
parent3d99fb3352eca9da68b6033ad22a166ed68ed36a

stage2: get rid of NameHash

Previously, stage2 used a global decl_table for all Decl objects, keyed by a 16-byte name hash that was hopefully unique. Now, there is a tree of Namespace objects that own their named Decl objects.

6 files changed, 0 insertions(+), 49 deletions(-)

BRANCH_TODO-25
......@@ -3,21 +3,6 @@
33 their indexes starting at 0 so that we can use an array to store Sema
44 results rather than a map.
55
6 * get rid of NameHash
7 * handle decl collision with usingnamespace
8 * the decl doing the looking up needs to create a decl dependency
9 on each usingnamespace decl
10 * handle usingnamespace cycles
11
12 * compile error for return inside defer expression
13
14 * when block has noreturn statement
15 - avoid emitting defers
16 - compile error for unreachable code
17
18 * detect `return error.Foo` and emit ZIR that unconditionally generates errdefers
19 * `return`: check return operand and generate errdefers if necessary
20
216 * have failed_trees and just put the file in there
227 - this way we can emit all the parse errors not just the first one
238 - but maybe we want just the first one?
......@@ -58,13 +43,3 @@
5843 * repl: if you try `run` with -ofmt=c you get an access denied error because it
5944 tries to execute the .c file as a child process instead of executing `zig run`
6045 on it.
61
62=== file issues: ===
63
64 * C backend: honor the exported symbol name. Right now if you do `pub fn main`
65 it generates bogus C code because the `@export` name is not honored, and it allows
66 the `main` which should be not exported, to clobber the exported symbol name.
67
68 * get the test runner and `zig test` working
69 - get behavior tests passing for stage2
70
src/Compilation.zig-1
......@@ -3563,7 +3563,6 @@ fn buildOutputFromZig(
35633563 .handle = special_dir,
35643564 },
35653565 .root_src_path = src_basename,
3566 .namespace_hash = Package.root_namespace_hash,
35673566 };
35683567 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
35693568 const target = comp.getTarget();
src/Module.zig-7
......@@ -798,8 +798,6 @@ pub const Var = struct {
798798pub const Scope = struct {
799799 tag: Tag,
800800
801 pub const NameHash = [16]u8;
802
803801 pub fn cast(base: *Scope, comptime T: type) ?*T {
804802 if (base.tag != T.base_tag)
805803 return null;
......@@ -839,7 +837,6 @@ pub const Scope = struct {
839837 .namespace => return @fieldParentPtr(Namespace, "base", base).file_scope.sub_file_path,
840838 .file => return @fieldParentPtr(File, "base", base).sub_file_path,
841839 .block => unreachable,
842 .decl_ref => unreachable,
843840 }
844841 }
845842
......@@ -861,10 +858,6 @@ pub const Scope = struct {
861858 /// Namespace owned by structs, enums, unions, and opaques for decls.
862859 namespace,
863860 block,
864 /// Used for simple error reporting. Only contains a reference to a
865 /// `Decl` for use with `srcDecl` and `ownerDecl`.
866 /// Has no parents or children.
867 decl_ref,
868861 };
869862
870863 /// The container that structs, enums, unions, and opaques have.
src/Package.zig-11
......@@ -11,22 +11,15 @@ const Module = @import("Module.zig");
1111
1212pub const Table = std.StringHashMapUnmanaged(*Package);
1313
14pub const root_namespace_hash: Module.Scope.NameHash = .{
15 0, 0, 6, 6, 6, 0, 0, 0,
16 6, 9, 0, 0, 0, 4, 2, 0,
17};
18
1914root_src_directory: Compilation.Directory,
2015/// Relative to `root_src_directory`. May contain path separators.
2116root_src_path: []const u8,
2217table: Table = .{},
2318parent: ?*Package = null,
24namespace_hash: Module.Scope.NameHash,
2519/// Whether to free `root_src_directory` on `destroy`.
2620root_src_directory_owned: bool = false,
2721
2822/// Allocate a Package. No references to the slices passed are kept.
29/// Don't forget to set `namespace_hash` later.
3023pub fn create(
3124 gpa: *Allocator,
3225 /// Null indicates the current working directory
......@@ -50,7 +43,6 @@ pub fn create(
5043 },
5144 .root_src_path = owned_src_path,
5245 .root_src_directory_owned = true,
53 .namespace_hash = undefined,
5446 };
5547
5648 return ptr;
......@@ -82,14 +74,12 @@ pub fn createWithDir(
8274 },
8375 .root_src_directory_owned = true,
8476 .root_src_path = owned_src_path,
85 .namespace_hash = undefined,
8677 };
8778 } else {
8879 ptr.* = .{
8980 .root_src_directory = directory,
9081 .root_src_directory_owned = false,
9182 .root_src_path = owned_src_path,
92 .namespace_hash = undefined,
9383 };
9484 }
9585 return ptr;
......@@ -129,6 +119,5 @@ pub fn add(pkg: *Package, gpa: *Allocator, name: []const u8, package: *Package)
129119pub fn addAndAdopt(parent: *Package, gpa: *Allocator, name: []const u8, child: *Package) !void {
130120 assert(child.parent == null); // make up your mind, who is the parent??
131121 child.parent = parent;
132 child.namespace_hash = std.zig.hashName(parent.namespace_hash, ":", name);
133122 return parent.add(gpa, name, child);
134123}
src/main.zig-4
......@@ -630,7 +630,6 @@ fn buildOutputType(
630630 var pkg_tree_root: Package = .{
631631 .root_src_directory = .{ .path = null, .handle = fs.cwd() },
632632 .root_src_path = &[0]u8{},
633 .namespace_hash = Package.root_namespace_hash,
634633 };
635634 defer freePkgTree(gpa, &pkg_tree_root, false);
636635 var cur_pkg: *Package = &pkg_tree_root;
......@@ -1768,7 +1767,6 @@ fn buildOutputType(
17681767 if (root_pkg) |pkg| {
17691768 pkg.table = pkg_tree_root.table;
17701769 pkg_tree_root.table = .{};
1771 pkg.namespace_hash = pkg_tree_root.namespace_hash;
17721770 }
17731771
17741772 const self_exe_path = try fs.selfExePathAlloc(arena);
......@@ -2657,7 +2655,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
26572655 .handle = try zig_lib_directory.handle.openDir(std_special, .{}),
26582656 },
26592657 .root_src_path = "build_runner.zig",
2660 .namespace_hash = Package.root_namespace_hash,
26612658 };
26622659 defer root_pkg.root_src_directory.handle.close();
26632660
......@@ -2703,7 +2700,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27032700 var build_pkg: Package = .{
27042701 .root_src_directory = build_directory,
27052702 .root_src_path = build_zig_basename,
2706 .namespace_hash = undefined,
27072703 };
27082704 try root_pkg.addAndAdopt(arena, "@build", &build_pkg);
27092705
src/test.zig-1
......@@ -611,7 +611,6 @@ pub const TestContext = struct {
611611 var root_pkg: Package = .{
612612 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
613613 .root_src_path = tmp_src_path,
614 .namespace_hash = Package.root_namespace_hash,
615614 };
616615 defer root_pkg.table.deinit(allocator);
617616