Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <[email protected]>

resolve conflicts

Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 committed Jun 3, 2024
1 parent 21392ee commit c0fd91d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
12 changes: 3 additions & 9 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 51 additions & 11 deletions kclvm/sema/src/namer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>,
}
Expand All @@ -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 {
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -178,6 +197,7 @@ impl<'ctx> Namer<'ctx> {
namer.define_symbols();

// namer.gs
// namer.gs
}

fn init_builtin_symbols(&mut self) {
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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()
Expand All @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -360,4 +400,4 @@ mod tests {
);
}
}
}
}

0 comments on commit c0fd91d

Please sign in to comment.