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 @@...@@ -3,21 +3,6 @@
3 their indexes starting at 0 so that we can use an array to store Sema3 their indexes starting at 0 so that we can use an array to store Sema
4 results rather than a map.4 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
21 * have failed_trees and just put the file in there6 * have failed_trees and just put the file in there
22 - this way we can emit all the parse errors not just the first one7 - this way we can emit all the parse errors not just the first one
23 - but maybe we want just the first one?8 - but maybe we want just the first one?
...@@ -58,13 +43,3 @@...@@ -58,13 +43,3 @@
58 * repl: if you try `run` with -ofmt=c you get an access denied error because it43 * repl: if you try `run` with -ofmt=c you get an access denied error because it
59 tries to execute the .c file as a child process instead of executing `zig run`44 tries to execute the .c file as a child process instead of executing `zig run`
60 on it.45 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(...@@ -3563,7 +3563,6 @@ fn buildOutputFromZig(
3563 .handle = special_dir,3563 .handle = special_dir,
3564 },3564 },
3565 .root_src_path = src_basename,3565 .root_src_path = src_basename,
3566 .namespace_hash = Package.root_namespace_hash,
3567 };3566 };
3568 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];3567 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
3569 const target = comp.getTarget();3568 const target = comp.getTarget();
src/Module.zig-7
...@@ -798,8 +798,6 @@ pub const Var = struct {...@@ -798,8 +798,6 @@ pub const Var = struct {
798pub const Scope = struct {798pub const Scope = struct {
799 tag: Tag,799 tag: Tag,
800800
801 pub const NameHash = [16]u8;
802
803 pub fn cast(base: *Scope, comptime T: type) ?*T {801 pub fn cast(base: *Scope, comptime T: type) ?*T {
804 if (base.tag != T.base_tag)802 if (base.tag != T.base_tag)
805 return null;803 return null;
...@@ -839,7 +837,6 @@ pub const Scope = struct {...@@ -839,7 +837,6 @@ pub const Scope = struct {
839 .namespace => return @fieldParentPtr(Namespace, "base", base).file_scope.sub_file_path,837 .namespace => return @fieldParentPtr(Namespace, "base", base).file_scope.sub_file_path,
840 .file => return @fieldParentPtr(File, "base", base).sub_file_path,838 .file => return @fieldParentPtr(File, "base", base).sub_file_path,
841 .block => unreachable,839 .block => unreachable,
842 .decl_ref => unreachable,
843 }840 }
844 }841 }
845842
...@@ -861,10 +858,6 @@ pub const Scope = struct {...@@ -861,10 +858,6 @@ pub const Scope = struct {
861 /// Namespace owned by structs, enums, unions, and opaques for decls.858 /// Namespace owned by structs, enums, unions, and opaques for decls.
862 namespace,859 namespace,
863 block,860 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,
868 };861 };
869862
870 /// The container that structs, enums, unions, and opaques have.863 /// The container that structs, enums, unions, and opaques have.
src/Package.zig-11
...@@ -11,22 +11,15 @@ const Module = @import("Module.zig");...@@ -11,22 +11,15 @@ const Module = @import("Module.zig");
1111
12pub const Table = std.StringHashMapUnmanaged(*Package);12pub 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
19root_src_directory: Compilation.Directory,14root_src_directory: Compilation.Directory,
20/// Relative to `root_src_directory`. May contain path separators.15/// Relative to `root_src_directory`. May contain path separators.
21root_src_path: []const u8,16root_src_path: []const u8,
22table: Table = .{},17table: Table = .{},
23parent: ?*Package = null,18parent: ?*Package = null,
24namespace_hash: Module.Scope.NameHash,
25/// Whether to free `root_src_directory` on `destroy`.19/// Whether to free `root_src_directory` on `destroy`.
26root_src_directory_owned: bool = false,20root_src_directory_owned: bool = false,
2721
28/// Allocate a Package. No references to the slices passed are kept.22/// Allocate a Package. No references to the slices passed are kept.
29/// Don't forget to set `namespace_hash` later.
30pub fn create(23pub fn create(
31 gpa: *Allocator,24 gpa: *Allocator,
32 /// Null indicates the current working directory25 /// Null indicates the current working directory
...@@ -50,7 +43,6 @@ pub fn create(...@@ -50,7 +43,6 @@ pub fn create(
50 },43 },
51 .root_src_path = owned_src_path,44 .root_src_path = owned_src_path,
52 .root_src_directory_owned = true,45 .root_src_directory_owned = true,
53 .namespace_hash = undefined,
54 };46 };
5547
56 return ptr;48 return ptr;
...@@ -82,14 +74,12 @@ pub fn createWithDir(...@@ -82,14 +74,12 @@ pub fn createWithDir(
82 },74 },
83 .root_src_directory_owned = true,75 .root_src_directory_owned = true,
84 .root_src_path = owned_src_path,76 .root_src_path = owned_src_path,
85 .namespace_hash = undefined,
86 };77 };
87 } else {78 } else {
88 ptr.* = .{79 ptr.* = .{
89 .root_src_directory = directory,80 .root_src_directory = directory,
90 .root_src_directory_owned = false,81 .root_src_directory_owned = false,
91 .root_src_path = owned_src_path,82 .root_src_path = owned_src_path,
92 .namespace_hash = undefined,
93 };83 };
94 }84 }
95 return ptr;85 return ptr;
...@@ -129,6 +119,5 @@ pub fn add(pkg: *Package, gpa: *Allocator, name: []const u8, package: *Package)...@@ -129,6 +119,5 @@ pub fn add(pkg: *Package, gpa: *Allocator, name: []const u8, package: *Package)
129pub fn addAndAdopt(parent: *Package, gpa: *Allocator, name: []const u8, child: *Package) !void {119pub fn addAndAdopt(parent: *Package, gpa: *Allocator, name: []const u8, child: *Package) !void {
130 assert(child.parent == null); // make up your mind, who is the parent??120 assert(child.parent == null); // make up your mind, who is the parent??
131 child.parent = parent;121 child.parent = parent;
132 child.namespace_hash = std.zig.hashName(parent.namespace_hash, ":", name);
133 return parent.add(gpa, name, child);122 return parent.add(gpa, name, child);
134}123}
src/main.zig-4
...@@ -630,7 +630,6 @@ fn buildOutputType(...@@ -630,7 +630,6 @@ fn buildOutputType(
630 var pkg_tree_root: Package = .{630 var pkg_tree_root: Package = .{
631 .root_src_directory = .{ .path = null, .handle = fs.cwd() },631 .root_src_directory = .{ .path = null, .handle = fs.cwd() },
632 .root_src_path = &[0]u8{},632 .root_src_path = &[0]u8{},
633 .namespace_hash = Package.root_namespace_hash,
634 };633 };
635 defer freePkgTree(gpa, &pkg_tree_root, false);634 defer freePkgTree(gpa, &pkg_tree_root, false);
636 var cur_pkg: *Package = &pkg_tree_root;635 var cur_pkg: *Package = &pkg_tree_root;
...@@ -1768,7 +1767,6 @@ fn buildOutputType(...@@ -1768,7 +1767,6 @@ fn buildOutputType(
1768 if (root_pkg) |pkg| {1767 if (root_pkg) |pkg| {
1769 pkg.table = pkg_tree_root.table;1768 pkg.table = pkg_tree_root.table;
1770 pkg_tree_root.table = .{};1769 pkg_tree_root.table = .{};
1771 pkg.namespace_hash = pkg_tree_root.namespace_hash;
1772 }1770 }
17731771
1774 const self_exe_path = try fs.selfExePathAlloc(arena);1772 const self_exe_path = try fs.selfExePathAlloc(arena);
...@@ -2657,7 +2655,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2657,7 +2655,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2657 .handle = try zig_lib_directory.handle.openDir(std_special, .{}),2655 .handle = try zig_lib_directory.handle.openDir(std_special, .{}),
2658 },2656 },
2659 .root_src_path = "build_runner.zig",2657 .root_src_path = "build_runner.zig",
2660 .namespace_hash = Package.root_namespace_hash,
2661 };2658 };
2662 defer root_pkg.root_src_directory.handle.close();2659 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...@@ -2703,7 +2700,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2703 var build_pkg: Package = .{2700 var build_pkg: Package = .{
2704 .root_src_directory = build_directory,2701 .root_src_directory = build_directory,
2705 .root_src_path = build_zig_basename,2702 .root_src_path = build_zig_basename,
2706 .namespace_hash = undefined,
2707 };2703 };
2708 try root_pkg.addAndAdopt(arena, "@build", &build_pkg);2704 try root_pkg.addAndAdopt(arena, "@build", &build_pkg);
27092705
src/test.zig-1
...@@ -611,7 +611,6 @@ pub const TestContext = struct {...@@ -611,7 +611,6 @@ pub const TestContext = struct {
611 var root_pkg: Package = .{611 var root_pkg: Package = .{
612 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },612 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
613 .root_src_path = tmp_src_path,613 .root_src_path = tmp_src_path,
614 .namespace_hash = Package.root_namespace_hash,
615 };614 };
616 defer root_pkg.table.deinit(allocator);615 defer root_pkg.table.deinit(allocator);
617616