| ... | @@ -469,6 +469,22 @@ pub const Scope = struct { | ... | @@ -469,6 +469,22 @@ pub const Scope = struct { |
| 469 | } | 469 | } |
| 470 | } | 470 | } |
| 471 | | 471 | |
| | 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, |
| 579 | | 597 | |
| 580 | root_container: Container, | 598 | root_container: Container, |
| 581 | | 599 | |
| ... | @@ -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 | } |
| 2401 | | 2419 | |
| 2402 | pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File { | 2420 | pub 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); |
| 2407 | | 2424 | |
| ... | @@ -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 = .{}, |