Skip to content

Commit 2c5a70d

Browse files
committed
Added initial ability for structs to be namespaced, as well as cleaned up some namespacing code
1 parent f56eba3 commit 2c5a70d

File tree

19 files changed

+86
-63
lines changed

19 files changed

+86
-63
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
mod parameters;
22

33
use super::{Stmt, Type};
4-
use crate::{source_files::Source, tag::Tag};
4+
use crate::{name::Name, source_files::Source, tag::Tag};
55
pub use parameters::{Parameter, Parameters};
66

77
#[derive(Clone, Debug)]
88
pub struct Function {
9-
pub name: String,
9+
pub name: Name,
1010
pub parameters: Parameters,
1111
pub return_type: Type,
1212
pub stmts: Vec<Stmt>,
1313
pub is_foreign: bool,
1414
pub source: Source,
1515
pub abide_abi: bool,
1616
pub tag: Option<Tag>,
17-
pub namespace: Option<String>,
1817
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::Type;
2-
use crate::source_files::Source;
2+
use crate::{name::Name, source_files::Source};
33
use indexmap::IndexMap;
44

55
#[derive(Clone, Debug)]
66
pub struct Structure {
7-
pub name: String,
7+
pub name: Name,
88
pub fields: IndexMap<String, Field>,
99
pub is_packed: bool,
1010
pub source: Source,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
ParameterDeclarationCore, ParameterTypeList, ParseError,
77
},
88
diagnostics::Diagnostics,
9+
name::Name,
910
};
1011
use std::collections::HashMap;
1112

@@ -72,15 +73,14 @@ pub fn declare_function(
7273
};
7374

7475
ast_file.functions.push(Function {
75-
name,
76+
name: Name::plain(name),
7677
parameters,
7778
return_type,
7879
stmts: vec![],
7980
is_foreign: true,
8081
source,
8182
abide_abi: true,
8283
tag: None,
83-
namespace: None,
8484
});
8585

8686
Ok(())

Diff for: src/c/translation/types/composite.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn make_composite(
9090
let is_packed = false;
9191

9292
if let Some(name) = &composite.name {
93-
let name = format!("struct<{}>", name);
93+
let name = Name::plain(format!("struct<{}>", name));
9494

9595
ast_file.structures.push(Structure {
9696
name: name.clone(),
@@ -99,7 +99,7 @@ pub fn make_composite(
9999
source: composite.source,
100100
});
101101

102-
Ok(TypeKind::Named(Name::plain(name)))
102+
Ok(TypeKind::Named(name))
103103
} else {
104104
let anonymous_struct = AnonymousStruct { fields, is_packed };
105105

Diff for: src/interpreter_env/mod.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use crate::{
1515
};
1616
use indexmap::IndexMap;
1717

18-
fn thin_void_function(name: impl ToString, syscall_kind: InterpreterSyscallKind) -> Function {
18+
fn thin_void_function(name: impl Into<String>, syscall_kind: InterpreterSyscallKind) -> Function {
1919
let source = Source::internal();
2020
let void = TypeKind::Void.at(Source::internal());
2121

2222
Function {
23-
name: name.to_string(),
23+
name: Name::plain(name),
2424
parameters: Parameters {
2525
required: vec![],
2626
is_cstyle_vararg: false,
@@ -39,33 +39,29 @@ fn thin_void_function(name: impl ToString, syscall_kind: InterpreterSyscallKind)
3939
is_foreign: false,
4040
source,
4141
tag: None,
42-
namespace: None,
4342
}
4443
}
4544

4645
fn thin_cstring_function(
47-
name: impl ToString,
48-
param_name: impl ToString,
46+
name: impl Into<String>,
47+
param_name: impl Into<String>,
4948
syscall_kind: InterpreterSyscallKind,
5049
) -> Function {
5150
let source = Source::internal();
5251
let void = TypeKind::Void.at(Source::internal());
5352
let ptr_char = TypeKind::Pointer(Box::new(TypeKind::char().at(source))).at(source);
54-
53+
let param_name = param_name.into();
5554
Function {
56-
name: name.to_string(),
55+
name: Name::plain(name.into()),
5756
parameters: Parameters {
58-
required: vec![Parameter::new(param_name.to_string(), ptr_char.clone())],
57+
required: vec![Parameter::new(param_name.clone(), ptr_char.clone())],
5958
is_cstyle_vararg: false,
6059
},
6160
return_type: void.clone(),
6261
stmts: vec![StmtKind::Expr(
6362
ExprKind::InterpreterSyscall(Box::new(InterpreterSyscall {
6463
kind: syscall_kind,
65-
args: vec![(
66-
ptr_char.clone(),
67-
ExprKind::Variable(param_name.to_string()).at(source),
68-
)],
64+
args: vec![(ptr_char.clone(), ExprKind::Variable(param_name).at(source))],
6965
result_type: void.clone(),
7066
}))
7167
.at(source),
@@ -75,7 +71,6 @@ fn thin_cstring_function(
7571
is_foreign: false,
7672
source,
7773
tag: None,
78-
namespace: None,
7974
}
8075
}
8176

@@ -94,15 +89,14 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
9489
.at(Source::internal());
9590

9691
file.functions.push(Function {
97-
name: "<interpreter entry point>".into(),
92+
name: Name::plain("<interpreter entry point>"),
9893
parameters: Parameters::default(),
9994
return_type: void.clone(),
10095
stmts: vec![StmtKind::Return(Some(call)).at(Source::internal())],
10196
is_foreign: false,
10297
source,
10398
abide_abi: false,
10499
tag: Some(Tag::InterpreterEntryPoint),
105-
namespace: None,
106100
});
107101

108102
file.enums.insert(
@@ -130,7 +124,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
130124
);
131125

132126
file.structures.push(Structure {
133-
name: "Project".into(),
127+
name: Name::plain("Project"),
134128
fields: IndexMap::from_iter([(
135129
"kind".into(),
136130
Field {
@@ -185,7 +179,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
185179
));
186180

187181
file.functions.push(Function {
188-
name: "project".into(),
182+
name: Name::plain("project"),
189183
parameters: Parameters {
190184
required: vec![
191185
Parameter::new("name".into(), ptr_char.clone()),
@@ -223,7 +217,6 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
223217
is_foreign: false,
224218
source,
225219
tag: None,
226-
namespace: None,
227220
});
228221
}
229222

Diff for: src/lexer/identifier_state.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ impl IdentifierState {
1616

1717
if let Some(last_slash) = self.last_slash {
1818
let basename = identifier.split_off(last_slash + 1);
19+
20+
// Remove trailing slash
21+
identifier.pop();
22+
1923
let namespace = identifier;
2024

2125
return TokenKind::NamespacedIdentifier(Name {

Diff for: src/lower/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ fn lower_function(
189189
}
190190
}
191191

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

Diff for: src/name.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ pub struct Name {
77
}
88

99
impl Name {
10+
pub fn new(namespace: Option<impl Into<String>>, basename: impl Into<String>) -> Self {
11+
Self {
12+
namespace: namespace
13+
.map(|namespace| namespace.into())
14+
.unwrap_or_default(),
15+
basename: basename.into(),
16+
}
17+
}
18+
1019
pub fn plain(basename: impl Into<String>) -> Self {
1120
Self {
1221
namespace: "".into(),
@@ -29,11 +38,19 @@ impl Name {
2938
None
3039
}
3140
}
41+
42+
pub fn fullname(&self) -> String {
43+
if self.namespace.is_empty() {
44+
self.basename.clone()
45+
} else {
46+
format!("{}/{}", self.namespace, self.basename)
47+
}
48+
}
3249
}
3350

3451
impl Display for Name {
3552
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36-
write!(f, "{}{}", self.namespace, self.basename)
53+
write!(f, "{}", self.fullname())
3754
}
3855
}
3956

@@ -44,6 +61,10 @@ pub enum ResolvedName {
4461
}
4562

4663
impl ResolvedName {
64+
pub fn new(name: &Name) -> Self {
65+
Self::Project(name.fullname().into_boxed_str())
66+
}
67+
4768
pub fn plain(&self) -> &str {
4869
match self {
4970
ResolvedName::Remote(name) => &**name,

Diff for: src/parser/parse_function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{
66
use crate::{
77
ast::{Function, Parameters, TypeKind},
88
inflow::Inflow,
9+
name::Name,
910
token::{Token, TokenKind},
1011
};
1112

@@ -57,15 +58,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
5758
.unwrap_or_default();
5859

5960
Ok(Function {
60-
name,
61+
name: Name::new(namespace, name),
6162
parameters,
6263
return_type,
6364
stmts,
6465
is_foreign,
6566
source,
6667
abide_abi,
6768
tag: None,
68-
namespace,
6969
})
7070
}
7171
}

Diff for: src/parser/parse_structure.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{
66
use crate::{
77
ast::{Field, Structure},
88
inflow::Inflow,
9+
name::Name,
910
token::{Token, TokenKind},
1011
};
1112
use indexmap::IndexMap;
@@ -22,11 +23,13 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2223
self.ignore_newlines();
2324

2425
let mut is_packed = false;
26+
let mut namespace = None;
2527

2628
for annotation in annotations {
2729
match annotation.kind {
2830
AnnotationKind::Packed => is_packed = true,
29-
_ => return Err(self.unexpected_annotation(&annotation, Some("for structure"))),
31+
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
32+
_ => return Err(self.unexpected_annotation(&annotation, Some("for struct"))),
3033
}
3134
}
3235

@@ -61,7 +64,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
6164
self.parse_token(TokenKind::CloseParen, Some("to end struct fields"))?;
6265

6366
Ok(Structure {
64-
name,
67+
name: Name::new(namespace, name),
6568
fields,
6669
is_packed,
6770
source,

Diff for: src/pragma_section/parse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
ast::{AstFile, Expr, ExprKind, Function, Parameters, Stmt, StmtKind, TypeKind},
44
diagnostics::ErrorDiagnostic,
55
inflow::Inflow,
6+
name::Name,
67
parser::{self, error::ParseError, Input},
78
show::{into_show, Show},
89
source_files::Source,
@@ -79,7 +80,7 @@ impl PragmaSection {
7980
}
8081

8182
ast_file.functions.push(Function {
82-
name: "main".into(),
83+
name: Name::plain("main"),
8384
parameters: Parameters {
8485
required: vec![],
8586
is_cstyle_vararg: false,
@@ -90,7 +91,6 @@ impl PragmaSection {
9091
source,
9192
abide_abi: false,
9293
tag: None,
93-
namespace: None,
9494
});
9595
} else {
9696
return Err(Box::new(ParseError::expected(

Diff for: src/pragma_section/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl PragmaSection {
7070
else {
7171
return Err(into_show(
7272
ParseErrorKind::Other {
73-
message: "No Adept version was specifed for module!".into(),
73+
message: "No Adept version was specifed for module! Use `pragma => adept(\"3.0\")` at the top of the module file".into(),
7474
}
7575
.at(self.pragma_source),
7676
));

Diff for: src/resolve/core_structure_info.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::error::{ResolveError, ResolveErrorKind};
22
use crate::{
3+
name::ResolvedName,
34
resolved::{self, StructureRef},
45
source_files::Source,
56
};
67

78
pub fn get_core_structure_info(
89
resolved_type: &resolved::Type,
910
source: Source,
10-
) -> Result<(&str, StructureRef), ResolveError> {
11+
) -> Result<(&ResolvedName, StructureRef), ResolveError> {
1112
match &resolved_type.kind {
1213
resolved::TypeKind::Structure(name, structure_ref) => Ok((name, *structure_ref)),
1314
_ => Err(ResolveErrorKind::CannotCreateStructLiteralForNonStructure {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn resolve_struct_literal_expr(
4747
let (struct_name, structure_ref) = get_core_structure_info(&resolved_type, source)?;
4848

4949
let structure_type =
50-
resolved::TypeKind::Structure(struct_name.to_string(), structure_ref).at(source);
50+
resolved::TypeKind::Structure(struct_name.clone(), structure_ref).at(source);
5151

5252
let mut next_index = 0;
5353
let mut resolved_fields = IndexMap::new();

0 commit comments

Comments
 (0)