Skip to content

Commit

Permalink
fix: use kfile_paths to determine if root scope contains the pos
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverRaR committed Nov 23, 2023
1 parent 971555f commit 6472c00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
17 changes: 13 additions & 4 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
└─────────────────────┘
*/

use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use kclvm_error::Position;

use crate::{
Expand Down Expand Up @@ -112,7 +112,11 @@ impl<'ctx> AdvancedResolver<'ctx> {
if !advanced_resolver.ctx.scopes.is_empty() {
advanced_resolver.ctx.scopes.clear();
}
advanced_resolver.enter_root_scope(name.clone(), pkg_info.pkg_filepath.clone());
advanced_resolver.enter_root_scope(
name.clone(),
pkg_info.pkg_filepath.clone(),
pkg_info.kfile_paths.clone(),
);
for module in modules.iter() {
advanced_resolver.ctx.current_filename = Some(module.filename.clone());
advanced_resolver.walk_module(module);
Expand All @@ -125,14 +129,19 @@ impl<'ctx> AdvancedResolver<'ctx> {
advanced_resolver.gs
}

fn enter_root_scope(&mut self, pkgpath: String, filename: String) {
fn enter_root_scope(
&mut self,
pkgpath: String,
filename: String,
kfile_paths: IndexSet<String>,
) {
let package_ref = self
.gs
.get_symbols_mut()
.get_symbol_by_fully_qualified_name(&pkgpath)
.unwrap();

let root_scope = RootSymbolScope::new(pkgpath, filename, package_ref);
let root_scope = RootSymbolScope::new(pkgpath, filename, package_ref, kfile_paths);
let scope_ref = self.gs.get_scopes_mut().alloc_root_scope(root_scope);
self.ctx.scopes.push(scope_ref);
}
Expand Down
34 changes: 12 additions & 22 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::Path};
use std::collections::HashMap;

use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use kclvm_error::Position;

use crate::core::symbol::SymbolRef;
Expand Down Expand Up @@ -145,6 +145,8 @@ pub struct RootSymbolScope {

pub(crate) filename: String,

pub(crate) kfile_path: IndexSet<String>,

/// PackageSymbol of this scope
pub(crate) owner: SymbolRef,

Expand Down Expand Up @@ -173,25 +175,7 @@ impl Scope for RootSymbolScope {
}

fn contains_pos(&self, pos: &Position) -> bool {
let real_pkg_path = if self.filename.ends_with(".k") {
Path::new(self.filename.strip_suffix(".k").unwrap())
} else {
Path::new(&self.filename)
};
let real_pos_path = if pos.filename.ends_with(".k") {
Path::new(pos.filename.strip_suffix(".k").unwrap())
} else {
Path::new(&pos.filename)
};
if real_pkg_path != real_pos_path {
if let Some(parent) = real_pos_path.parent() {
real_pkg_path == parent
} else {
false
}
} else {
true
}
self.kfile_path.contains(&pos.filename)
}
fn get_owner(&self) -> Option<SymbolRef> {
Some(self.owner)
Expand Down Expand Up @@ -269,9 +253,15 @@ impl Scope for RootSymbolScope {
}

impl RootSymbolScope {
pub fn new(pkgpath: String, filename: String, owner: SymbolRef) -> Self {
pub fn new(
pkgpath: String,
filename: String,
owner: SymbolRef,
kfile_path: IndexSet<String>,
) -> Self {
Self {
pkgpath,
kfile_path,
filename,
owner,
children: IndexMap::default(),
Expand Down

0 comments on commit 6472c00

Please sign in to comment.