authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-06 13:56:26+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-30 15:58:11+02:00
loga1d7f0053d6fa56bcc879e83987babd42bb21a20
tree24281b7496a83f45074a0634b86da6ce370b18f0
parent8421b8a8983b5531b1cc299461f7747c829b054a
signaturelock-open Commit is signed but in an unrecognized format.

stage2: support imports inside packages


2 files changed, 22 insertions(+), 3 deletions(-)

src/Compilation.zig+1
......@@ -660,6 +660,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
660660 .source = .{ .unloaded = {} },
661661 .contents = .{ .not_available = {} },
662662 .status = .never_loaded,
663 .pkg = root_pkg,
663664 .root_container = .{
664665 .file_scope = root_scope,
665666 .decls = .{},
src/Module.zig+21-3
......@@ -469,6 +469,22 @@ pub const Scope = struct {
469469 }
470470 }
471471
472 pub fn getOwnerPkg(base: *Scope) *Package {
473 var cur = base;
474 while (true) {
475 cur = switch (cur.tag) {
476 .container => return @fieldParentPtr(Container, "base", cur).file_scope.pkg,
477 .file => return @fieldParentPtr(File, "base", cur).pkg,
478 .zir_module => unreachable, // TODO are zir modules allowed to import packages?
479 .gen_zir => @fieldParentPtr(GenZIR, "base", cur).parent,
480 .local_val => @fieldParentPtr(LocalVal, "base", cur).parent,
481 .local_ptr => @fieldParentPtr(LocalPtr, "base", cur).parent,
482 .block => @fieldParentPtr(Block, "base", cur).decl.scope,
483 .decl => @fieldParentPtr(DeclAnalysis, "base", cur).decl.scope,
484 };
485 }
486 }
487
472488 /// Asserts the scope is a namespace Scope and removes the Decl from the namespace.
473489 pub fn removeDecl(base: *Scope, child: *Decl) void {
474490 switch (base.tag) {
......@@ -576,6 +592,8 @@ pub const Scope = struct {
576592 unloaded_parse_failure,
577593 loaded_success,
578594 },
595 /// Package that this file is a part of, managed externally.
596 pkg: *Package,
579597
580598 root_container: Container,
581599
......@@ -614,7 +632,7 @@ pub const Scope = struct {
614632 pub fn getSource(self: *File, module: *Module) ![:0]const u8 {
615633 switch (self.source) {
616634 .unloaded => {
617 const source = try module.root_pkg.root_src_directory.handle.readFileAllocOptions(
635 const source = try self.pkg.root_src_directory.handle.readFileAllocOptions(
618636 module.gpa,
619637 self.sub_file_path,
620638 std.math.maxInt(u32),
......@@ -2400,8 +2418,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,
24002418}
24012419
24022420pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {
2403 // TODO scope.getCurPkg();
2404 const cur_pkg = self.root_pkg;
2421 const cur_pkg = scope.getOwnerPkg();
24052422 const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";
24062423 const found_pkg = cur_pkg.table.get(target_string);
24072424
......@@ -2437,6 +2454,7 @@ pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []
24372454 .source = .{ .unloaded = {} },
24382455 .contents = .{ .not_available = {} },
24392456 .status = .never_loaded,
2457 .pkg = found_pkg orelse cur_pkg,
24402458 .root_container = .{
24412459 .file_scope = file_scope,
24422460 .decls = .{},