Namespace all functions, except those that are explicitly extern

This commit is contained in:
Sofia 2025-07-28 21:13:53 +03:00
parent 5026013df3
commit 13be3e9c02
5 changed files with 39 additions and 20 deletions

View File

@ -8,23 +8,16 @@ fn main() {
let module = context.module("test", true);
let main = module.function("main", Type::I32, Vec::new(), FunctionFlags::default());
let main = module.function("main", None, Type::I32, Vec::new(), FunctionFlags::default());
let mut m_entry = main.block("entry");
let fibonacci = module.function(
"fibonacci",
Type::I32,
vec![Type::I32],
FunctionFlags::default(),
);
let fibonacci = module.function("fibonacci", None, Type::I32, vec![Type::I32], FunctionFlags::default());
let arg = m_entry.build_named("const", Constant(I32(5))).unwrap();
let fibonacci_call = m_entry
.build_named("const", FunctionCall(fibonacci.value(), vec![arg]))
.unwrap();
m_entry
.terminate(TerminatorKind::Ret(fibonacci_call))
.unwrap();
m_entry.terminate(TerminatorKind::Ret(fibonacci_call)).unwrap();
let mut f_entry = fibonacci.block("entry");

View File

@ -590,7 +590,7 @@ impl FunctionHolder {
let name = if self.data.flags.is_main {
c"main"
} else {
&into_cstring(&self.data.name)
&into_cstring(&self.data.linkage_name.clone().unwrap_or(self.data.name.clone()))
};
let fn_type = LLVMFunctionType(ret_type, param_ptr, param_len as u32, 0);
@ -605,7 +605,6 @@ impl FunctionHolder {
let metadata = if let Some(debug) = debug {
if let Some(scope_value) = &self.debug_info {
let scope_data = debug.info.get_scope_data(scope_value).unwrap();
dbg!(&debug.info.get_scope());
let mangled_length_ptr = &mut 0;
let mangled_name = LLVMGetValueName2(function_ref, mangled_length_ptr);

View File

@ -66,7 +66,14 @@ pub struct Module<'ctx> {
}
impl<'ctx> Module<'ctx> {
pub fn function(&self, name: &str, ret: Type, params: Vec<Type>, flags: FunctionFlags) -> Function<'ctx> {
pub fn function(
&self,
name: &str,
linkage: Option<String>,
ret: Type,
params: Vec<Type>,
flags: FunctionFlags,
) -> Function<'ctx> {
unsafe {
Function {
phantom: PhantomData,
@ -75,6 +82,7 @@ impl<'ctx> Module<'ctx> {
&self.value,
FunctionData {
name: name.to_owned(),
linkage_name: linkage,
ret,
params,
flags,
@ -126,6 +134,7 @@ impl<'ctx> Drop for Module<'ctx> {
#[derive(Debug, Clone, Hash)]
pub struct FunctionData {
name: String,
linkage_name: Option<String>,
ret: Type,
params: Vec<Type>,
flags: FunctionFlags,

View File

@ -374,7 +374,7 @@ pub struct IntrinsicMalloc(TypeKind);
impl IntrinsicFunction for IntrinsicMalloc {
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
let amount = params.get(0).unwrap();
let function = scope.block.find_function(&"malloc".to_owned()).unwrap();
let function = scope.block.find_function(&MALLOC_IDENT.to_owned()).unwrap();
let instr = scope
.block
.build(Instr::FunctionCall(function, vec![amount.instr()]))

View File

@ -192,9 +192,7 @@ impl mir::Module {
FunctionDefinitionKind::Extern(i) => !i,
_ => false,
};
let module_prefix = if is_true_extern {
String::new()
} else if let Some(module) = function.source {
let module_prefix = if let Some(module) = function.source {
if module == self.module_id {
format!("reid.{}.", self.name)
} else {
@ -203,14 +201,17 @@ impl mir::Module {
} else {
format!("reid.intrinsic.")
};
let linkage_name = format!(
let linkage_name = function.linkage_name.clone().unwrap_or(function.name.clone());
let full_name = format!(
"{}{}",
module_prefix,
function.linkage_name.clone().unwrap_or(function.name.clone())
);
let func = match &function.kind {
mir::FunctionDefinitionKind::Local(_, _) => Some(module.function(
&linkage_name,
&full_name,
None,
function.return_type.get_type(&type_values),
param_types,
FunctionFlags {
@ -221,7 +222,20 @@ impl mir::Module {
},
)),
mir::FunctionDefinitionKind::Extern(imported) => Some(module.function(
&linkage_name,
if function.source == None {
&function.name
} else {
&full_name
},
if function.source == None {
Some(function.linkage_name.clone().unwrap())
} else {
if !*imported {
Some(linkage_name.clone())
} else {
None
}
},
function.return_type.get_type(&type_values),
param_types,
FunctionFlags {
@ -260,6 +274,7 @@ impl mir::Module {
let func = match &function.kind {
mir::FunctionDefinitionKind::Local(_, _) => Some(module.function(
&format!("{}.{}.{}", module_name, ty, function.name),
None,
function.return_type.get_type(&type_values),
param_types,
FunctionFlags {
@ -271,6 +286,7 @@ impl mir::Module {
)),
mir::FunctionDefinitionKind::Extern(imported) => Some(module.function(
&function.linkage_name.clone().unwrap_or(function.name.clone()),
None,
function.return_type.get_type(&type_values),
param_types,
FunctionFlags {
@ -308,6 +324,7 @@ impl mir::Module {
FunctionDefinitionKind::Local(..) => {
let ir_function = module.function(
&binop_fn_name,
None,
binop.return_type.get_type(&type_values),
vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)],
FunctionFlags {
@ -373,6 +390,7 @@ impl mir::Module {
}
FunctionDefinitionKind::Extern(imported) => ScopeFunctionKind::UserGenerated(module.function(
&binop_fn_name,
None,
binop.return_type.get_type(&type_values),
vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)],
FunctionFlags {