From 6554e9b52bfa2971a7c94d0ccda3b2f36f9a699c Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Sat, 1 Jun 2024 02:29:44 +0530 Subject: [PATCH] update function symbol def Signed-off-by: shruti2522 --- kclvm/sema/src/core/symbol.rs | 115 ++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 55 deletions(-) diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index ac877df99..91f284541 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -713,27 +713,28 @@ impl SymbolData { } 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); - let symbol_ref = SymbolRef { - id: symbol_id, - kind: SymbolKind::Function, - }; - self.symbols_info - .node_symbol_map - .insert(node_key.clone(), symbol_ref); - self.symbols_info - .symbol_node_map - .insert(symbol_ref, node_key); - self.functions.get_mut(symbol_id).unwrap().id = Some(symbol_ref); - symbol_ref - } + &mut self, + func: FunctionSymbol, + node_key: NodeKey, +) -> Option { + if self.symbols_info.symbol_pos_set.contains(&func.end) { + return None; + } + 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, + }; + self.symbols_info + .node_symbol_map + .insert(node_key.clone(), symbol_ref); + self.symbols_info + .symbol_node_map + .insert(symbol_ref, node_key); + self.functions.get_mut(symbol_id).unwrap().id = Some(symbol_ref); + Some(symbol_ref) +} #[inline] pub fn get_node_symbol_map(&self) -> &IndexMap { @@ -2117,25 +2118,21 @@ impl DecoratorSymbol { } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct FunctionSymbol { - pub id: Option, - pub name: String, - pub parameters: Vec, - pub return_type: Option>, - pub sema_info: KCLSymbolSemanticInfo, - pub start: Position, - pub end: Position, + pub(crate) id: Option, + pub(crate) name: String, + pub(crate) start: Position, + pub(crate) end: Position, + pub(crate) owner: Option, + pub(crate) sema_info: KCLSymbolSemanticInfo, + pub(crate) parameters: Vec<(String, String)>, } impl Symbol for FunctionSymbol { - type SymbolData = SymbolData; + type SymbolData = SymbolData; type SemanticInfo = KCLSymbolSemanticInfo; - fn get_sema_info(&self) -> &Self::SemanticInfo { - &self.sema_info - } - fn is_global(&self) -> bool { true } @@ -2145,7 +2142,7 @@ impl Symbol for FunctionSymbol { } fn get_owner(&self) -> Option { - None + self.owner.clone() } fn get_definition(&self) -> Option { @@ -2166,16 +2163,16 @@ impl Symbol for FunctionSymbol { _data: &Self::SymbolData, _module_info: Option<&ModuleInfo>, ) -> Option { - None + None } fn has_attribute( &self, - _name: &str, - _data: &Self::SymbolData, - _module_info: Option<&ModuleInfo>, + name: &str, + data: &Self::SymbolData, + module_info: Option<&ModuleInfo>, ) -> bool { - false + self.get_attribute(name, data, module_info).is_some() } fn get_all_attributes( @@ -2183,12 +2180,13 @@ impl Symbol for FunctionSymbol { _data: &Self::SymbolData, _module_info: Option<&ModuleInfo>, ) -> Vec { - vec![] + vec![] } fn simple_dump(&self) -> String { let mut output = "{\n".to_string(); output.push_str("\"kind\": \"FunctionSymbol\",\n"); + output.push_str(&format!("\"name\":\"{}\",\n", self.name)); output.push_str(&format!( "\"range\": \"{}:{}", self.start.filename, self.start.line @@ -2201,38 +2199,45 @@ impl Symbol for FunctionSymbol { if let Some(end_col) = self.end.column { output.push_str(&format!(":{}", end_col)); } - output.push_str(&format!("name :{}", self.get_name())); - output.push_str("\"\n}"); + output.push_str("\",\n"); + + output.push_str("\"parameters\": ["); + for (i, (param_name, param_type)) in self.parameters.iter().enumerate() { + if i > 0 { + output.push_str(", "); + } + output.push_str(&format!("{{\"name\": \"{}\", \"type\": \"{}\"}}", param_name, param_type)); + } + output.push_str("]\n}"); + output } fn full_dump(&self, _data: &Self::SymbolData) -> Option { Some(self.simple_dump()) } + + fn get_sema_info(&self) -> &Self::SemanticInfo { + &self.sema_info + } } impl FunctionSymbol { pub fn new( - id: Option, name: String, - parameters: Vec, - return_type: Option>, - sema_info: KCLSymbolSemanticInfo, start: Position, end: Position, + owner: Option, + parameters: Vec<(String, String)>, ) -> Self { Self { - id, + id: None, name, - parameters, - return_type, - sema_info, start, end, + owner, + sema_info: KCLSymbolSemanticInfo::default(), + parameters, } } - - pub fn name(&self) -> String { - self.name.clone() - } }