Skip to content

Commit cc8e1b9

Browse files
committed
Started working on namespace resolution
1 parent 009fe49 commit cc8e1b9

File tree

23 files changed

+198
-169
lines changed

23 files changed

+198
-169
lines changed

src/ast/datatype/kind/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
AnonymousEnum, AnonymousStruct, AnoymousUnion, CInteger, FixedArray, FloatSize,
66
FunctionPointer, IntegerBits, IntegerSign, Type,
77
};
8-
use crate::source_files::Source;
8+
use crate::{name::Name, source_files::Source};
99

1010
#[derive(Clone, Debug)]
1111
pub enum TypeKind {
@@ -16,7 +16,7 @@ pub enum TypeKind {
1616
Pointer(Box<Type>),
1717
FixedArray(Box<FixedArray>),
1818
Void,
19-
Named(String),
19+
Named(Name),
2020
AnonymousStruct(AnonymousStruct),
2121
AnonymousUnion(AnoymousUnion),
2222
AnonymousEnum(AnonymousEnum),
@@ -30,9 +30,10 @@ impl TypeKind {
3030

3131
pub fn allow_indirect_undefined(&self) -> bool {
3232
if let TypeKind::Named(name) = self {
33-
if name.starts_with("struct<")
34-
|| name.starts_with("union<")
35-
|| name.starts_with("enum<")
33+
let basename = &name.basename;
34+
if basename.starts_with("struct<")
35+
|| basename.starts_with("union<")
36+
|| basename.starts_with("enum<")
3637
{
3738
return true;
3839
}

src/ast/expr/call.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use super::Expr;
2-
use crate::ast::{CompileTimeArgument, Type};
2+
use crate::{
3+
ast::{CompileTimeArgument, Type},
4+
name::Name,
5+
};
36

47
#[derive(Clone, Debug)]
58
pub struct Call {
6-
pub function_name: String,
9+
pub function_name: Name,
710
pub arguments: Vec<Expr>,
811
pub expected_to_return: Option<Type>,
912
pub generics: Vec<CompileTimeArgument>,

src/c/translation/types/composite.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
MemberDeclaration, MemberDeclarator, ParseError,
77
},
88
diagnostics::Diagnostics,
9+
name::Name,
910
};
1011
use indexmap::IndexMap;
1112
use std::collections::HashMap;
@@ -30,8 +31,8 @@ pub fn make_composite(
3031
})?;
3132

3233
return Ok(match &composite.kind {
33-
CompositeKind::Struct => TypeKind::Named(format!("struct<{}>", name)),
34-
CompositeKind::Union => TypeKind::Named(format!("union<{}>", name)),
34+
CompositeKind::Struct => TypeKind::Named(Name::plain(format!("struct<{}>", name))),
35+
CompositeKind::Union => TypeKind::Named(Name::plain(format!("union<{}>", name))),
3536
});
3637
};
3738

@@ -98,7 +99,7 @@ pub fn make_composite(
9899
source: composite.source,
99100
});
100101

101-
Ok(TypeKind::Named(name))
102+
Ok(TypeKind::Named(Name::plain(name)))
102103
} else {
103104
let anonymous_struct = AnonymousStruct { fields, is_packed };
104105

src/c/translation/types/enumeration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
translation::eval::evaluate_to_const_integer,
66
},
77
index_map_ext::IndexMapExt,
8+
name::Name,
89
};
910
use indexmap::IndexMap;
1011
use num_bigint::BigInt;
@@ -91,7 +92,10 @@ pub fn make_anonymous_enum(
9192
todo!("support enum type specifiers")
9293
}
9394

94-
Ok(TypeKind::Named(format!("enum<{}>", named.name)))
95+
Ok(TypeKind::Named(Name::plain(format!(
96+
"enum<{}>",
97+
named.name
98+
))))
9599
}
96100
}
97101
}

src/interpreter_env/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
Interpreter, InterpreterError,
99
},
1010
ir::{self, InterpreterSyscallKind},
11+
name::Name,
1112
resolved,
1213
source_files::Source,
1314
tag::Tag,
@@ -56,7 +57,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
5657

5758
// Call to function we actually care about
5859
let call = ExprKind::Call(Box::new(Call {
59-
function_name: "main".into(),
60+
function_name: Name::plain("main"),
6061
arguments: vec![],
6162
expected_to_return: Some(void.clone()),
6263
generics: vec![],
@@ -103,7 +104,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
103104
fields: IndexMap::from_iter([(
104105
"kind".into(),
105106
Field {
106-
ast_type: TypeKind::Named("ProjectKind".into()).at(source),
107+
ast_type: TypeKind::Named(Name::plain("ProjectKind")).at(source),
107108
privacy: Privacy::Public,
108109
source,
109110
},
@@ -149,7 +150,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
149150
Parameter::new("name".into(), ptr_char.clone()),
150151
Parameter::new(
151152
"project".into(),
152-
TypeKind::Named("Project".into()).at(source),
153+
TypeKind::Named(Name::plain("Project")).at(source),
153154
),
154155
],
155156
is_cstyle_vararg: false,
@@ -164,7 +165,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
164165
ExprKind::Variable("name".into()).at(source),
165166
),
166167
(
167-
TypeKind::Named("ProjectKind".into()).at(source),
168+
TypeKind::Named(Name::plain("ProjectKind")).at(source),
168169
ExprKind::Member(
169170
Box::new(ExprKind::Variable("project".into()).at(source)),
170171
"kind".into(),

src/lexer/identifier_state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
2+
name::Name,
23
source_files::Source,
3-
token::{NamespacedIndentifier, Token, TokenKind},
4+
token::{Token, TokenKind},
45
};
56

67
pub struct IdentifierState {
@@ -17,7 +18,7 @@ impl IdentifierState {
1718
let basename = identifier.split_off(last_slash + 1);
1819
let namespace = identifier;
1920

20-
return TokenKind::NamespacedIdentifier(NamespacedIndentifier {
21+
return TokenKind::NamespacedIdentifier(Name {
2122
namespace,
2223
basename,
2324
})

src/lexer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<T: Text + Send> Lexer<T> {
269269
}
270270
',' => Has(TokenKind::Comma.at(source)),
271271
':' if self.characters.eat('=') => Has(TokenKind::DeclareAssign.at(source)),
272-
':' if self.characters.eat(':') => Has(TokenKind::Namespace.at(source)),
272+
':' if self.characters.eat(':') => Has(TokenKind::StaticMember.at(source)),
273273
':' => Has(TokenKind::Colon.at(source)),
274274
'#' => Has(TokenKind::Hash.at(source)),
275275
'\"' => {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod llvm_backend;
3030
mod logic;
3131
mod look_ahead;
3232
mod lower;
33+
mod name;
3334
mod parser;
3435
mod path;
3536
mod pragma_section;

src/parser/annotation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum AnnotationKind {
1919
ThreadLocal,
2020
Packed,
2121
AbideAbi,
22+
Namespace(String),
2223
}
2324

2425
impl AnnotationKind {
@@ -34,6 +35,7 @@ impl Display for AnnotationKind {
3435
Self::ThreadLocal => "thread_local",
3536
Self::Packed => "packed",
3637
Self::AbideAbi => "abide_abi",
38+
Self::Namespace(_) => "namespace",
3739
})
3840
}
3941
}

src/parser/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub enum ParseErrorKind {
8484
word_for_nth: String,
8585
},
8686
GenericsNotSupportedHere,
87+
NamespaceNotAllowedHere,
8788
Other {
8889
message: String,
8990
},
@@ -207,6 +208,9 @@ impl Display for ParseErrorKind {
207208
ParseErrorKind::GenericsNotSupportedHere => {
208209
write!(f, "Generics not supported here")?;
209210
}
211+
ParseErrorKind::NamespaceNotAllowedHere => {
212+
write!(f, "Namespace not allowed here")?;
213+
}
210214
ParseErrorKind::Other { message } | ParseErrorKind::Lexical { message } => {
211215
write!(f, "{}", message)?;
212216
}

0 commit comments

Comments
 (0)