Skip to content

Commit

Permalink
update function symbol def
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 committed May 31, 2024
1 parent 67cb3c8 commit 6554e9b
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SymbolRef> {
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<NodeKey, SymbolRef> {
Expand Down Expand Up @@ -2117,25 +2118,21 @@ impl DecoratorSymbol {
}
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct FunctionSymbol {
pub id: Option<SymbolRef>,
pub name: String,
pub parameters: Vec<TypeRef>,
pub return_type: Option<Arc<Type>>,
pub sema_info: KCLSymbolSemanticInfo,
pub start: Position,
pub end: Position,
pub(crate) id: Option<SymbolRef>,
pub(crate) name: String,
pub(crate) start: Position,
pub(crate) end: Position,
pub(crate) owner: Option<SymbolRef>,
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
}
Expand All @@ -2145,7 +2142,7 @@ impl Symbol for FunctionSymbol {
}

fn get_owner(&self) -> Option<SymbolRef> {
None
self.owner.clone()
}

fn get_definition(&self) -> Option<SymbolRef> {
Expand All @@ -2166,29 +2163,30 @@ impl Symbol for FunctionSymbol {
_data: &Self::SymbolData,
_module_info: Option<&ModuleInfo>,
) -> Option<SymbolRef> {
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(
&self,
_data: &Self::SymbolData,
_module_info: Option<&ModuleInfo>,
) -> Vec<SymbolRef> {
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
Expand All @@ -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<String> {
Some(self.simple_dump())
}

fn get_sema_info(&self) -> &Self::SemanticInfo {
&self.sema_info
}
}

impl FunctionSymbol {
pub fn new(
id: Option<SymbolRef>,
name: String,
parameters: Vec<TypeRef>,
return_type: Option<Arc<Type>>,
sema_info: KCLSymbolSemanticInfo,
start: Position,
end: Position,
owner: Option<SymbolRef>,
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()
}
}

0 comments on commit 6554e9b

Please sign in to comment.