Skip to content

Commit c4dd3e6

Browse files
committed
Added ability for functions to exist in namespaces
1 parent bb2e685 commit c4dd3e6

File tree

10 files changed

+49
-6
lines changed

10 files changed

+49
-6
lines changed

Diff for: src/ast/function/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pub struct Function {
1414
pub source: Source,
1515
pub abide_abi: bool,
1616
pub tag: Option<Tag>,
17+
pub namespace: Option<String>,
1718
}

Diff for: src/c/translation/function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn declare_function(
8080
source,
8181
abide_abi: true,
8282
tag: None,
83+
namespace: None,
8384
});
8485

8586
Ok(())

Diff for: src/interpreter_env/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn thin_cstring_function(
4747
is_foreign: false,
4848
source,
4949
tag: None,
50+
namespace: None,
5051
}
5152
}
5253

@@ -73,6 +74,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
7374
source,
7475
abide_abi: false,
7576
tag: Some(Tag::InterpreterEntryPoint),
77+
namespace: None,
7678
});
7779

7880
file.enums.insert(
@@ -182,6 +184,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
182184
is_foreign: false,
183185
source,
184186
tag: None,
187+
namespace: None,
185188
});
186189
}
187190

Diff for: src/lower/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn lower_function(
161161
} else {
162162
return Err(LowerErrorKind::MustReturnValueOfTypeBeforeExitingFunction {
163163
return_type: function.return_type.to_string(),
164-
function: function.name.clone(),
164+
function: function.name.to_string(),
165165
}
166166
.at(function.source));
167167
}
@@ -189,10 +189,19 @@ fn lower_function(
189189
}
190190
}
191191

192+
eprintln!("warning: name mangling does not take all cases into account yet");
193+
let mangled_name = if function.name.plain() == "main" {
194+
"main".into()
195+
} else if function.is_foreign {
196+
function.name.plain().to_string()
197+
} else {
198+
function.name.to_string()
199+
};
200+
192201
ir_module.functions.insert(
193202
function_ref,
194203
ir::Function {
195-
mangled_name: function.name.clone(),
204+
mangled_name,
196205
basicblocks,
197206
parameters,
198207
return_type,

Diff for: src/name.rs

+18
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,21 @@ pub enum ResolvedName {
4242
Remote(Box<str>),
4343
Project(Box<str>),
4444
}
45+
46+
impl ResolvedName {
47+
pub fn plain(&self) -> &str {
48+
match self {
49+
ResolvedName::Remote(name) => &**name,
50+
ResolvedName::Project(name) => &**name,
51+
}
52+
}
53+
}
54+
55+
impl Display for ResolvedName {
56+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57+
match self {
58+
ResolvedName::Remote(name) => write!(f, "<remote>/{}", name),
59+
ResolvedName::Project(name) => write!(f, "<project>/{}", name),
60+
}
61+
}
62+
}

Diff for: src/parser/parse_function.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
1616

1717
let mut is_foreign = false;
1818
let mut abide_abi = false;
19+
let mut namespace = None;
1920

2021
for annotation in annotations {
2122
match annotation.kind {
2223
AnnotationKind::Foreign => is_foreign = true,
2324
AnnotationKind::AbideAbi => abide_abi = true,
25+
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
2426
_ => return Err(self.unexpected_annotation(&annotation, Some("for function"))),
2527
}
2628
}
@@ -63,6 +65,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
6365
source,
6466
abide_abi,
6567
tag: None,
68+
namespace,
6669
})
6770
}
6871
}

Diff for: src/pragma_section/parse.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl PragmaSection {
9090
source,
9191
abide_abi: false,
9292
tag: None,
93+
namespace: None,
9394
});
9495
} else {
9596
return Err(Box::new(ParseError::expected(

Diff for: src/resolve/expr/call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn resolve_call_expr(
4646
if resolved_required_ty != return_type {
4747
return Err(ResolveErrorKind::FunctionMustReturnType {
4848
of: required_ty.to_string(),
49-
function_name: function.name.clone(),
49+
function_name: function.name.to_string(),
5050
}
5151
.at(function.return_type.source));
5252
}
@@ -93,7 +93,7 @@ pub fn resolve_call_expr(
9393
return Err(ResolveErrorKind::BadTypeForArgumentToFunction {
9494
expected: preferred_type.to_string(),
9595
got: argument.resolved_type.to_string(),
96-
name: function.name.clone(),
96+
name: function.name.to_string(),
9797
i,
9898
}
9999
.at(source));

Diff for: src/resolve/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ pub fn resolve<'a>(
245245
let type_search_ctx = ctx.type_search_ctxs.get_mut(&file_id).unwrap();
246246

247247
for (function_i, function) in file.functions.iter().enumerate() {
248+
let name = if let Some(namespace) = function.namespace.as_ref() {
249+
ResolvedName::Project(format!("{}/{}", namespace, function.name).into_boxed_str())
250+
} else {
251+
ResolvedName::Project(function.name.clone().into_boxed_str())
252+
};
253+
248254
let function_ref = resolved_ast.functions.insert(resolved::Function {
249-
name: function.name.clone(),
255+
name,
250256
parameters: resolve_parameters(
251257
type_search_ctx,
252258
source_files,

Diff for: src/resolved/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use crate::ast::{
88
use crate::{
99
ast::fmt_c_integer,
1010
ir::InterpreterSyscallKind,
11+
name::ResolvedName,
1112
source_files::{Source, SourceFiles},
1213
tag::Tag,
1314
target::Target,
@@ -70,7 +71,7 @@ pub struct GlobalVar {
7071

7172
#[derive(Clone, Debug)]
7273
pub struct Function {
73-
pub name: String,
74+
pub name: ResolvedName,
7475
pub parameters: Parameters,
7576
pub return_type: Type,
7677
pub stmts: Vec<Stmt>,

0 commit comments

Comments
 (0)