Skip to content

Commit cc8e1b9

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

23 files changed

+198
-169
lines changed

Diff for: src/ast/datatype/kind/mod.rs

+6-5
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
}

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

+5-2
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>,

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

+4-3
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

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

+5-1
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
}

Diff for: src/interpreter_env/mod.rs

+5-4
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(),

Diff for: src/lexer/identifier_state.rs

+3-2
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
})

Diff for: src/lexer/mod.rs

+1-1
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
'\"' => {

Diff for: src/main.rs

+1
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;

Diff for: src/parser/annotation.rs

+2
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
}

Diff for: src/parser/error.rs

+4
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
}

Diff for: src/parser/parse_annotation.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
1919
let (annotation_name, source) =
2020
self.parse_identifier_keep_location(Some("for annotation name"))?;
2121

22-
self.parse_token(TokenKind::CloseBracket, Some("to close annotation body"))?;
23-
24-
Ok(match annotation_name.as_str() {
22+
let annotation = match annotation_name.as_str() {
2523
"foreign" => AnnotationKind::Foreign,
2624
"thread_local" => AnnotationKind::ThreadLocal,
2725
"packed" => AnnotationKind::Packed,
2826
"abide_abi" => AnnotationKind::AbideAbi,
27+
"namespace" => {
28+
let namespace = self.parse_identifier(Some("for namespace name"))?;
29+
AnnotationKind::Namespace(namespace)
30+
}
2931
_ => {
3032
return Err(ParseErrorKind::UnrecognizedAnnotation {
3133
name: annotation_name,
3234
}
3335
.at(source))
3436
}
3537
}
36-
.at(source))
38+
.at(source);
39+
40+
self.parse_token(TokenKind::CloseBracket, Some("to close annotation body"))?;
41+
Ok(annotation)
3742
}
3843
}

Diff for: src/parser/parse_expr/post/member.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::Parser;
22
use crate::{
33
ast::{Expr, ExprKind},
44
inflow::Inflow,
5-
parser::error::ParseError,
5+
parser::{error::ParseError, parse_util::into_plain_name},
66
token::{Token, TokenKind},
77
};
88

@@ -12,13 +12,16 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
1212
// ^
1313

1414
let source = self.parse_token(TokenKind::Member, Some("for member expression"))?;
15-
let member_name = self.parse_identifier(Some("for member name"))?;
15+
let member_name = self.parse_name(Some("for member name"))?;
1616

1717
if self.input.peek_is(TokenKind::OpenParen) || self.input.peek_is(TokenKind::OpenAngle) {
1818
let generics = self.parse_generics()?;
1919
self.parse_call_with(member_name, generics, vec![subject], source)
2020
} else {
21-
Ok(ExprKind::Member(Box::new(subject), member_name).at(source))
21+
Ok(
22+
ExprKind::Member(Box::new(subject), into_plain_name(member_name, source)?)
23+
.at(source),
24+
)
2225
}
2326
}
2427
}

Diff for: src/parser/parse_expr/primary/call.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::Parser;
22
use crate::{
33
ast::{Call, CompileTimeArgument, Expr, ExprKind},
44
inflow::Inflow,
5+
name::Name,
56
parser::error::ParseError,
67
source_files::Source,
78
token::{Token, TokenKind},
@@ -10,7 +11,7 @@ use crate::{
1011
impl<'a, I: Inflow<Token>> Parser<'a, I> {
1112
pub fn parse_call(
1213
&mut self,
13-
function_name: String,
14+
function_name: Name,
1415
generics: Vec<CompileTimeArgument>,
1516
source: Source,
1617
) -> Result<Expr, ParseError> {
@@ -22,7 +23,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2223

2324
pub fn parse_call_with(
2425
&mut self,
25-
function_name: String,
26+
function_name: Name,
2627
generics: Vec<CompileTimeArgument>,
2728
prefix_args: Vec<Expr>,
2829
source: Source,

Diff for: src/parser/parse_expr/primary/enum_member_literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
1616
// EnumName::EnumVariant
1717
// ^
1818

19-
self.parse_token(TokenKind::Namespace, Some("for enum member literal"))?;
19+
self.parse_token(TokenKind::StaticMember, Some("for enum member literal"))?;
2020

2121
let variant_source = self.source_here();
2222
let variant_name = self

Diff for: src/parser/parse_expr/primary/mod.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
UnaryOperator, While,
1212
},
1313
inflow::Inflow,
14-
parser::{array_last, error::ParseErrorKind},
14+
parser::{array_last, error::ParseErrorKind, parse_util::into_plain_name},
1515
token::{StringLiteral, StringModifier, Token, TokenKind},
1616
};
1717
use std::ffi::CString;
@@ -69,19 +69,20 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
6969
TokenKind::StructKeyword | TokenKind::UnionKeyword | TokenKind::EnumKeyword => {
7070
self.parse_structure_literal()
7171
}
72-
TokenKind::Identifier(_) => {
72+
TokenKind::Identifier(_) | TokenKind::NamespacedIdentifier(_) => {
7373
// TODO: CLEANUP: This should be cleaned up once we have proper
7474
// namespaces and generic parsing that applies to all cases
7575

76-
let name = self.input.eat_identifier().unwrap();
76+
let name = self.parse_name(None::<&str>).unwrap();
7777
let generics = self.parse_generics()?;
7878

7979
match self.input.peek().kind {
80-
TokenKind::Namespace => {
80+
TokenKind::StaticMember => {
8181
if !generics.is_empty() {
8282
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
8383
}
8484

85+
let name = into_plain_name(name, source)?;
8586
self.parse_enum_member_literal(name, source)
8687
}
8788
TokenKind::OpenCurly => {
@@ -101,7 +102,10 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
101102
self.parse_type_from_parts(name, generics, source)?;
102103
self.parse_structure_literal_with(ast_type)
103104
}
104-
_ => Ok(Expr::new(ExprKind::Variable(name), source)),
105+
_ => Ok(Expr::new(
106+
ExprKind::Variable(into_plain_name(name, source)?),
107+
source,
108+
)),
105109
}
106110
}
107111
}
@@ -111,14 +115,17 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
111115
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
112116
}
113117

114-
self.parse_declare_assign(name, source)
118+
self.parse_declare_assign(into_plain_name(name, source)?, source)
115119
}
116120
_ => {
117121
if !generics.is_empty() {
118122
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
119123
}
120124

121-
Ok(Expr::new(ExprKind::Variable(name), source))
125+
Ok(Expr::new(
126+
ExprKind::Variable(into_plain_name(name, source)?),
127+
source,
128+
))
122129
}
123130
}
124131
}

Diff for: src/parser/parse_stmt/parse_declaration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
99
pub fn parse_declaration(&mut self) -> Result<Stmt, ParseError> {
1010
let (name, source) = self.parse_identifier_keep_location(Some("for variable name"))?;
1111

12-
let variable_type = self.parse_type(None::<&str>, Some("for variable type"))?;
12+
let variable_type = self.parse_type(None::<&str>, Some("for variable"))?;
1313

1414
let initial_value = self
1515
.input

0 commit comments

Comments
 (0)