diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index 265da861c..0a91c1691 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -802,15 +802,9 @@ impl SymbolData { Some(symbol_ref) } - pub fn alloc_function_symbol( - &mut self, - function: FunctionSymbol, - node_key: NodeKey, - ) -> SymbolRef { - self.symbols_info - .symbol_pos_set - .insert(function.end.clone()); - let symbol_id = self.functions.insert(function); + pub fn alloc_function_symbol(&mut self, func: FunctionSymbol, node_key: NodeKey) -> SymbolRef { + self.symbols_info.symbol_pos_set.insert(func.end.clone()); + let symbol_id = self.functions.insert(func); let symbol_ref = SymbolRef { id: symbol_id, kind: SymbolKind::Function, diff --git a/kclvm/sema/src/namer/mod.rs b/kclvm/sema/src/namer/mod.rs index c9978c914..1efeb807b 100644 --- a/kclvm/sema/src/namer/mod.rs +++ b/kclvm/sema/src/namer/mod.rs @@ -45,7 +45,9 @@ use crate::builtin::{ }; use crate::core::global_state::GlobalState; use crate::core::package::{ModuleInfo, PackageInfo}; -use crate::core::symbol::{FunctionSymbol, PackageSymbol, SymbolRef, BUILTIN_FUNCTION_PACKAGE, BUILTIN_STR_PACKAGE}; +use crate::core::symbol::{ + PackageSymbol, SymbolRef, FunctionSymbol, BUILTIN_FUNCTION_PACKAGE, BUILTIN_STR_PACKAGE, +}; use crate::resolver::scope::NodeKey; use indexmap::IndexSet; use kclvm_ast::ast::AstIndex; @@ -57,6 +59,7 @@ mod node; pub const BUILTIN_SYMBOL_PKG_PATH: &str = "@builtin"; pub struct Namer<'ctx> { + gs: &'ctx mut GlobalState, gs: &'ctx mut GlobalState, ctx: NamerContext<'ctx>, } @@ -83,6 +86,7 @@ impl<'ctx> NamerContext<'ctx> { } impl<'ctx> Namer<'ctx> { + fn new(program: &'ctx Program, gs: &'ctx mut GlobalState) -> Self { fn new(program: &'ctx Program, gs: &'ctx mut GlobalState) -> Self { Self { ctx: NamerContext { @@ -97,6 +101,7 @@ impl<'ctx> Namer<'ctx> { } // serial namer pass + pub fn find_symbols(program: &'ctx Program, gs: &'ctx mut GlobalState) { pub fn find_symbols(program: &'ctx Program, gs: &'ctx mut GlobalState) { let mut namer = Self::new(program, gs); namer.ctx.current_package_info = Some(PackageInfo::new( @@ -121,6 +126,16 @@ impl<'ctx> Namer<'ctx> { // add new pkgs to invalidate pkgs namer.gs.new_or_invalidate_pkgs.insert(name.clone()); + // new pkgs or invalidate pkg + if namer.gs.get_packages().get_package_info(name).is_some() + && !namer.gs.new_or_invalidate_pkgs.contains(name) + { + continue; + } + + // add new pkgs to invalidate pkgs + namer.gs.new_or_invalidate_pkgs.insert(name.clone()); + { if modules.is_empty() { continue; @@ -141,6 +156,10 @@ impl<'ctx> Namer<'ctx> { }; let pkg_symbol = PackageSymbol::new(name.clone(), pkg_pos.clone(), pkg_pos); + let symbol_ref = namer + .gs + .get_symbols_mut() + .alloc_package_symbol(pkg_symbol, name.to_string()); let symbol_ref = namer .gs .get_symbols_mut() @@ -178,6 +197,7 @@ impl<'ctx> Namer<'ctx> { namer.define_symbols(); // namer.gs + // namer.gs } fn init_builtin_symbols(&mut self) { @@ -193,10 +213,11 @@ impl<'ctx> Namer<'ctx> { func_symbol.sema_info.ty = Some(Arc::new(builtin_func.clone())); func_symbol.sema_info.doc = builtin_func.ty_doc(); - let symbol_ref = self - .gs - .get_symbols_mut() - .alloc_function_symbol(func_symbol, self.ctx.get_node_key(&AstIndex::default()), BUILTIN_FUNCTION_PACKAGE.to_string()); + let symbol_ref = self.gs.get_symbols_mut().alloc_function_symbol( + func_symbol, + self.ctx.get_node_key(&AstIndex::default()), + BUILTIN_FUNCTION_PACKAGE.to_string(), + ); self.gs .get_symbols_mut() .symbols_info @@ -206,6 +227,14 @@ impl<'ctx> Namer<'ctx> { //add system modules for system_pkg_name in STANDARD_SYSTEM_MODULES { + let package_symbol_ref = self.gs.get_symbols_mut().alloc_package_symbol( + PackageSymbol::new( + system_pkg_name.to_string(), + Position::dummy_pos(), + Position::dummy_pos(), + ), + system_pkg_name.to_string(), + ); let package_symbol_ref = self.gs.get_symbols_mut().alloc_package_symbol( PackageSymbol::new( system_pkg_name.to_string(), @@ -229,7 +258,7 @@ impl<'ctx> Namer<'ctx> { let func_symbol_ref = self.gs.get_symbols_mut().alloc_function_symbol( func_symbol, self.ctx.get_node_key(&AstIndex::default()), - system_pkg_name.to_string() + system_pkg_name.to_string(), ); self.gs .get_symbols_mut() @@ -250,6 +279,14 @@ impl<'ctx> Namer<'ctx> { ), BUILTIN_STR_PACKAGE.to_string(), ); + let package_symbol_ref = self.gs.get_symbols_mut().alloc_package_symbol( + PackageSymbol::new( + BUILTIN_STR_PACKAGE.to_string(), + Position::dummy_pos(), + Position::dummy_pos(), + ), + BUILTIN_STR_PACKAGE.to_string(), + ); for (name, builtin_func) in STRING_MEMBER_FUNCTIONS.iter() { let mut func_symbol = FunctionSymbol::new( name.to_string(), @@ -261,10 +298,11 @@ impl<'ctx> Namer<'ctx> { func_symbol.sema_info.ty = Some(Arc::new(builtin_func.clone())); func_symbol.sema_info.doc = builtin_func.ty_doc(); - let symbol_ref = self - .gs - .get_symbols_mut() - .alloc_function_symbol(func_symbol, self.ctx.get_node_key(&AstIndex::default()), BUILTIN_STR_PACKAGE.to_string()); + let symbol_ref = self.gs.get_symbols_mut().alloc_function_symbol( + func_symbol, + self.ctx.get_node_key(&AstIndex::default()), + BUILTIN_STR_PACKAGE.to_string(), + ); self.gs .get_symbols_mut() .packages @@ -302,6 +340,8 @@ mod tests { .program; let mut gs = GlobalState::default(); Namer::find_symbols(&program, &mut gs); + let mut gs = GlobalState::default(); + Namer::find_symbols(&program, &mut gs); let symbols = gs.get_symbols(); @@ -360,4 +400,4 @@ mod tests { ); } } -} +} \ No newline at end of file