Skip to content

Commit

Permalink
fix: fix advanced_resolver schema def scope. reomve duplicate scopes
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Jul 8, 2024
1 parent 4a5951e commit e3188d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
45 changes: 45 additions & 0 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ impl<'ctx> AdvancedResolver<'ctx> {
self.ctx.scopes.push(scope_ref);
}

fn enter_schema_def_scope(
&mut self,
name: &str,
filepath: &str,
start: Position,
end: Position,
kind: LocalSymbolScopeKind,
) {
let parent = *self.ctx.scopes.last().unwrap();
let local_scope = LocalSymbolScope::new(parent, start, end, kind);
let fqn_name = format!("{filepath}.{name}");

let scope_ref = match self.gs.get_scopes().schema_scope_map.get(&fqn_name) {
Some(scope_ref) => scope_ref.clone(),
None => {
let scope_ref = self.gs.get_scopes_mut().alloc_local_scope(local_scope);
self.gs
.get_scopes_mut()
.schema_scope_map
.insert(fqn_name, scope_ref);

match parent.get_kind() {
ScopeKind::Root => {
self.gs
.get_scopes_mut()
.roots
.get_mut(parent.get_id())
.unwrap()
.add_child(filepath, scope_ref);
}
ScopeKind::Local => {
self.gs
.get_scopes_mut()
.locals
.get_mut(parent.get_id())
.unwrap()
.add_child(scope_ref);
}
}
scope_ref
}
};
self.ctx.scopes.push(scope_ref);
}

fn leave_scope(&mut self) {
self.ctx.scopes.pop();
}
Expand Down
3 changes: 2 additions & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {

let mut last_end_pos = start.clone();

self.enter_local_scope(
self.enter_schema_def_scope(
&schema_ty.into_schema_type().name,
&self.ctx.current_filename.clone().unwrap(),
start,
end.clone(),
Expand Down
4 changes: 4 additions & 0 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl ScopeRef {
pub struct ScopeData {
/// map pkgpath to root_scope
pub(crate) root_map: IndexMap<String, ScopeRef>,
/// map schema fully qualified name to schema local scope
pub(crate) schema_scope_map: IndexMap<String, ScopeRef>,
pub(crate) locals: generational_arena::Arena<LocalSymbolScope>,
pub(crate) roots: generational_arena::Arena<RootSymbolScope>,
}
Expand Down Expand Up @@ -208,6 +210,8 @@ impl ScopeData {
self.clear_scope_and_child(scope_ref);
self.roots.remove(scope_ref.get_id());
}
self.schema_scope_map
.retain(|key, _| !key.starts_with(invalidate_pkg));
}
}

Expand Down

0 comments on commit e3188d6

Please sign in to comment.