Skip to content

Commit

Permalink
Added ability for functions to exist in namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Sep 20, 2024
1 parent bb2e685 commit c4dd3e6
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ast/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub struct Function {
pub source: Source,
pub abide_abi: bool,
pub tag: Option<Tag>,
pub namespace: Option<String>,
}
1 change: 1 addition & 0 deletions src/c/translation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn declare_function(
source,
abide_abi: true,
tag: None,
namespace: None,
});

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn thin_cstring_function(
is_foreign: false,
source,
tag: None,
namespace: None,
}
}

Expand All @@ -73,6 +74,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
source,
abide_abi: false,
tag: Some(Tag::InterpreterEntryPoint),
namespace: None,
});

file.enums.insert(
Expand Down Expand Up @@ -182,6 +184,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
is_foreign: false,
source,
tag: None,
namespace: None,
});
}

Expand Down
13 changes: 11 additions & 2 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn lower_function(
} else {
return Err(LowerErrorKind::MustReturnValueOfTypeBeforeExitingFunction {
return_type: function.return_type.to_string(),
function: function.name.clone(),
function: function.name.to_string(),
}
.at(function.source));
}
Expand Down Expand Up @@ -189,10 +189,19 @@ fn lower_function(
}
}

eprintln!("warning: name mangling does not take all cases into account yet");
let mangled_name = if function.name.plain() == "main" {
"main".into()
} else if function.is_foreign {
function.name.plain().to_string()
} else {
function.name.to_string()
};

ir_module.functions.insert(
function_ref,
ir::Function {
mangled_name: function.name.clone(),
mangled_name,
basicblocks,
parameters,
return_type,
Expand Down
18 changes: 18 additions & 0 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ pub enum ResolvedName {
Remote(Box<str>),
Project(Box<str>),
}

impl ResolvedName {
pub fn plain(&self) -> &str {
match self {
ResolvedName::Remote(name) => &**name,
ResolvedName::Project(name) => &**name,
}
}
}

impl Display for ResolvedName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ResolvedName::Remote(name) => write!(f, "<remote>/{}", name),
ResolvedName::Project(name) => write!(f, "<project>/{}", name),
}
}
}
3 changes: 3 additions & 0 deletions src/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {

let mut is_foreign = false;
let mut abide_abi = false;
let mut namespace = None;

for annotation in annotations {
match annotation.kind {
AnnotationKind::Foreign => is_foreign = true,
AnnotationKind::AbideAbi => abide_abi = true,
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
_ => return Err(self.unexpected_annotation(&annotation, Some("for function"))),
}
}
Expand Down Expand Up @@ -63,6 +65,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
source,
abide_abi,
tag: None,
namespace,
})
}
}
1 change: 1 addition & 0 deletions src/pragma_section/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl PragmaSection {
source,
abide_abi: false,
tag: None,
namespace: None,
});
} else {
return Err(Box::new(ParseError::expected(
Expand Down
4 changes: 2 additions & 2 deletions src/resolve/expr/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn resolve_call_expr(
if resolved_required_ty != return_type {
return Err(ResolveErrorKind::FunctionMustReturnType {
of: required_ty.to_string(),
function_name: function.name.clone(),
function_name: function.name.to_string(),
}
.at(function.return_type.source));
}
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn resolve_call_expr(
return Err(ResolveErrorKind::BadTypeForArgumentToFunction {
expected: preferred_type.to_string(),
got: argument.resolved_type.to_string(),
name: function.name.clone(),
name: function.name.to_string(),
i,
}
.at(source));
Expand Down
8 changes: 7 additions & 1 deletion src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,14 @@ pub fn resolve<'a>(
let type_search_ctx = ctx.type_search_ctxs.get_mut(&file_id).unwrap();

for (function_i, function) in file.functions.iter().enumerate() {
let name = if let Some(namespace) = function.namespace.as_ref() {
ResolvedName::Project(format!("{}/{}", namespace, function.name).into_boxed_str())
} else {
ResolvedName::Project(function.name.clone().into_boxed_str())
};

let function_ref = resolved_ast.functions.insert(resolved::Function {
name: function.name.clone(),
name,
parameters: resolve_parameters(
type_search_ctx,
source_files,
Expand Down
3 changes: 2 additions & 1 deletion src/resolved/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use crate::ast::{
use crate::{
ast::fmt_c_integer,
ir::InterpreterSyscallKind,
name::ResolvedName,
source_files::{Source, SourceFiles},
tag::Tag,
target::Target,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub struct GlobalVar {

#[derive(Clone, Debug)]
pub struct Function {
pub name: String,
pub name: ResolvedName,
pub parameters: Parameters,
pub return_type: Type,
pub stmts: Vec<Stmt>,
Expand Down

0 comments on commit c4dd3e6

Please sign in to comment.