From f458192e56b13500ff6eb7c3e94dcf48240f4170 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 9 Apr 2021 23:17:50 -0700 Subject: stage2: entry point via std lib and proper updated file detection Instead of Module setting up the root_scope with the root source file, instead, Module relies on the package table graph being set up properly, and inside `update()`, it does the equivalent of `_ = @import("std");`. This, in term, imports start.zig, which has the logic to call main (or not). `Module` no longer has `root_scope` - the root source file is no longer special, it's just in the package table mapped to "root". I also went ahead and implemented proper detection of updated files. mtime, inode, size, and source hash are kept in `Scope.File`. During an update, iterate over `import_table` and stat each file to find out which ones are updated. The source hash is redundant with the source hash used by the struct decl that corresponds to the file, so it should be removed in a future commit before merging the branch. * AstGen: add "previously declared here" notes for variables shadowing decls. * Parse imports as structs. Module now calls `AstGen.structDeclInner`, which is called by `AstGen.containerDecl`. - `importFile` is a bit kludgy with how it handles the top level Decl that kinda gets merged into the struct decl at the end of the function. Be on the look out for bugs related to that as well as possibly cleaner ways to implement this. * Module: factor out lookupDeclName into lookupIdentifier and lookupNa * Rename `Scope.Container` to `Scope.Namespace`. * Delete some dead code. This branch won't work until `usingnamespace` is implemented because it relies on `@import("builtin").OutputMode` and `OutputMode` comes from a `usingnamespace`. --- src/type.zig | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index 74c38f7e0d..d05fa0f5e3 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2052,11 +2052,11 @@ pub const Type = extern union { (self.isSinglePointer() and self.elemType().zigTypeTag() == .Array); } - /// Returns null if the type has no container. - pub fn getContainerScope(self: Type) ?*Module.Scope.Container { + /// Returns null if the type has no namespace. + pub fn getNamespace(self: Type) ?*Module.Scope.Namespace { return switch (self.tag()) { - .@"struct" => &self.castTag(.@"struct").?.data.container, - .enum_full => &self.castTag(.enum_full).?.data.container, + .@"struct" => &self.castTag(.@"struct").?.data.namespace, + .enum_full => &self.castTag(.enum_full).?.data.namespace, .empty_struct => self.castTag(.empty_struct).?.data, .@"opaque" => &self.castTag(.@"opaque").?.data, @@ -2226,6 +2226,29 @@ pub const Type = extern union { } } + pub fn getOwnerDecl(ty: Type) *Module.Decl { + switch (ty.tag()) { + .enum_full, .enum_nonexhaustive => { + const enum_full = ty.cast(Payload.EnumFull).?.data; + return enum_full.owner_decl; + }, + .enum_simple => { + const enum_simple = ty.castTag(.enum_simple).?.data; + return enum_simple.owner_decl; + }, + .@"struct" => { + const struct_obj = ty.castTag(.@"struct").?.data; + return struct_obj.owner_decl; + }, + .error_set => { + const error_set = ty.castTag(.error_set).?.data; + return error_set.owner_decl; + }, + .@"opaque" => @panic("TODO"), + else => unreachable, + } + } + /// Asserts the type is an enum. pub fn enumHasInt(ty: Type, int: Value, target: Target) bool { const S = struct { @@ -2564,12 +2587,12 @@ pub const Type = extern union { /// Most commonly used for files. pub const ContainerScope = struct { base: Payload, - data: *Module.Scope.Container, + data: *Module.Scope.Namespace, }; pub const Opaque = struct { base: Payload = .{ .tag = .@"opaque" }, - data: Module.Scope.Container, + data: Module.Scope.Namespace, }; pub const Struct = struct { -- cgit v1.2.3