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 {...@@ -660,6 +660,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
660 .source = .{ .unloaded = {} },660 .source = .{ .unloaded = {} },
661 .contents = .{ .not_available = {} },661 .contents = .{ .not_available = {} },
662 .status = .never_loaded,662 .status = .never_loaded,
663 .pkg = root_pkg,
663 .root_container = .{664 .root_container = .{
664 .file_scope = root_scope,665 .file_scope = root_scope,
665 .decls = .{},666 .decls = .{},
src/Module.zig+21-3
...@@ -469,6 +469,22 @@ pub const Scope = struct {...@@ -469,6 +469,22 @@ pub const Scope = struct {
469 }469 }
470 }470 }
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
472 /// Asserts the scope is a namespace Scope and removes the Decl from the namespace.488 /// Asserts the scope is a namespace Scope and removes the Decl from the namespace.
473 pub fn removeDecl(base: *Scope, child: *Decl) void {489 pub fn removeDecl(base: *Scope, child: *Decl) void {
474 switch (base.tag) {490 switch (base.tag) {
...@@ -576,6 +592,8 @@ pub const Scope = struct {...@@ -576,6 +592,8 @@ pub const Scope = struct {
576 unloaded_parse_failure,592 unloaded_parse_failure,
577 loaded_success,593 loaded_success,
578 },594 },
595 /// Package that this file is a part of, managed externally.
596 pkg: *Package,
579597
580 root_container: Container,598 root_container: Container,
581599
...@@ -614,7 +632,7 @@ pub const Scope = struct {...@@ -614,7 +632,7 @@ pub const Scope = struct {
614 pub fn getSource(self: *File, module: *Module) ![:0]const u8 {632 pub fn getSource(self: *File, module: *Module) ![:0]const u8 {
615 switch (self.source) {633 switch (self.source) {
616 .unloaded => {634 .unloaded => {
617 const source = try module.root_pkg.root_src_directory.handle.readFileAllocOptions(635 const source = try self.pkg.root_src_directory.handle.readFileAllocOptions(
618 module.gpa,636 module.gpa,
619 self.sub_file_path,637 self.sub_file_path,
620 std.math.maxInt(u32),638 std.math.maxInt(u32),
...@@ -2400,8 +2418,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,...@@ -2400,8 +2418,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,
2400}2418}
24012419
2402pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {2420pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {
2403 // TODO scope.getCurPkg();2421 const cur_pkg = scope.getOwnerPkg();
2404 const cur_pkg = self.root_pkg;
2405 const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";2422 const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";
2406 const found_pkg = cur_pkg.table.get(target_string);2423 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: []...@@ -2437,6 +2454,7 @@ pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []
2437 .source = .{ .unloaded = {} },2454 .source = .{ .unloaded = {} },
2438 .contents = .{ .not_available = {} },2455 .contents = .{ .not_available = {} },
2439 .status = .never_loaded,2456 .status = .never_loaded,
2457 .pkg = found_pkg orelse cur_pkg,
2440 .root_container = .{2458 .root_container = .{
2441 .file_scope = file_scope,2459 .file_scope = file_scope,
2442 .decls = .{},2460 .decls = .{},