Skip to content

Commit

Permalink
prog
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 3, 2025
1 parent 526a8c1 commit 62b300e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/ir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ fn lower_func(
locals: Vec::new(),
is_extern: func.decl.is_extern,
is_intrinsic,
name: if has_self.is_some() {
format!("{}_{}_{}", &func.decl.name.name, fn_id.program_id, fn_id.id)
name: if !func.decl.is_extern && func.decl.name.name != "main" {
ctx.get_mangled_name(module_id, &func.decl.name.name, fn_id)
.expect("failed to get mangled name")
} else {
func.decl.name.name.clone()
},
Expand Down
18 changes: 18 additions & 0 deletions src/ir/lowering/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,21 @@ impl FnBodyBuilder {
self.ctx.body.modules.get(&self.local_module).unwrap()
}
}

impl BuildCtx {
pub fn get_mangled_name(&self, module_id: DefId, fn_name: &str, fn_id: DefId) -> Option<String> {
let mut name_path: Vec<&str> = Vec::new();

let cur_module = &self.body.modules.get(&module_id)?;

for parent_id in &cur_module.parent_ids {
let module = &self.body.modules.get(parent_id)?;
name_path.push(module.name.as_ref());
}
name_path.push(cur_module.name.as_ref());

name_path.push(fn_name);

Some(format!("{}@{}", name_path.join("::"), fn_id.id))
}
}
4 changes: 4 additions & 0 deletions src/ir/lowering/prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn prepass_module(
module_id,
ModuleBody {
id: module_id,
name: mod_def.name.name.clone(),
parent_ids: vec![],
symbols: Default::default(),
modules: Default::default(),
Expand All @@ -29,6 +30,7 @@ pub fn prepass_module(
types: Default::default(),
constants: Default::default(),
imports: Default::default(),
span: mod_def.span,
},
);

Expand Down Expand Up @@ -173,13 +175,15 @@ pub fn prepass_sub_module(
let mut submodule = ModuleBody {
id,
parent_ids: parent_ids.to_vec(),
name: mod_def.name.name.clone(),
imports: Default::default(),
symbols: Default::default(),
modules: Default::default(),
functions: Default::default(),
structs: Default::default(),
types: Default::default(),
constants: Default::default(),
span: mod_def.span,
};

for ct in &mod_def.contents {
Expand Down
12 changes: 3 additions & 9 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct ProgramBody {
#[derive(Debug, Clone)]
pub struct ModuleBody {
pub id: DefId,
pub name: String,
pub parent_ids: Vec<DefId>,
pub symbols: SymbolTable,
/// Functions defined in this module.
Expand All @@ -65,6 +66,7 @@ pub struct ModuleBody {
pub modules: HashSet<DefId>,
/// Imported items. symbol -> id
pub imports: HashMap<String, DefId>,
pub span: Span,
}

/// Function body
Expand All @@ -87,15 +89,7 @@ impl FnBody {
}

pub fn get_mangled_name(&self) -> String {
if self.is_extern {
return self.name.clone();
}

if self.name == "main" {
"main".to_string()
} else {
format!("{}_{}_{}", self.name, self.id.program_id, self.id.id)
}
self.name.clone()
}
}

Expand Down

0 comments on commit 62b300e

Please sign in to comment.