1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* parse/types.cpp
* - Parsing for type usages
*/
#include "common.hpp"
#include "parseerror.hpp"
#include "../types.hpp"
#include "../ast/ast.hpp"
/// Mappings from internal type names to the core type enum
static const struct {
const char* name;
enum eCoreType type;
} CORETYPES[] = {
{"char", CORETYPE_CHAR},
{"f32", CORETYPE_F32},
{"f64", CORETYPE_F64},
{"i16", CORETYPE_I16},
{"i32", CORETYPE_I32},
{"i64", CORETYPE_I64},
{"i8", CORETYPE_I8},
{"int", CORETYPE_INT},
{"isize", CORETYPE_INT},
{"u16", CORETYPE_U16},
{"u32", CORETYPE_U32},
{"u64", CORETYPE_U64},
{"u8", CORETYPE_U8},
{"uint", CORETYPE_UINT},
{"usize", CORETYPE_UINT},
};
// === PROTOTYPES ===
TypeRef Parse_Type(TokenStream& lex);
TypeRef Parse_Type_Fn(TokenStream& lex, ::std::string abi);
// === CODE ===
TypeRef Parse_Type(TokenStream& lex)
{
//TRACE_FUNCTION;
Token tok;
switch( GET_TOK(tok, lex) )
{
// '_' = Wildcard (type inferrence variable)
case TOK_UNDERSCORE:
return TypeRef();
// 'extern' - A function type with an ABI
case TOK_RWORD_EXTERN: {
GET_CHECK_TOK(tok, lex, TOK_STRING);
::std::string abi = tok.str();
GET_CHECK_TOK(tok, lex, TOK_RWORD_FN);
return Parse_Type_Fn(lex, abi);
}
// 'fn' - Rust function
case TOK_RWORD_FN:
return Parse_Type_Fn(lex, "");
// '<' - An associated type cast
case TOK_LT: {
DEBUG("Associated type");
// <Type as Trait>::Inner
TypeRef base = Parse_Type(lex);
GET_CHECK_TOK(tok, lex, TOK_RWORD_AS);
TypeRef trait = Parse_Type(lex);
GET_CHECK_TOK(tok, lex, TOK_GT);
// TODO: Is just '<Type as Trait>' valid?
GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
GET_CHECK_TOK(tok, lex, TOK_IDENT);
::std::string inner_name = tok.str();
return TypeRef(TypeRef::TagAssoc(), ::std::move(base), ::std::move(trait), ::std::move(inner_name));
}
// <ident> - Either a primitive, or a path
case TOK_IDENT:
// or a primitive
for(unsigned int i = 0; i < sizeof(CORETYPES)/sizeof(CORETYPES[0]); i ++)
{
if( tok.str() < CORETYPES[i].name )
break;
if( tok.str() == CORETYPES[i].name )
return TypeRef(TypeRef::TagPrimitive(), CORETYPES[i].type);
}
lex.putback(tok);
return TypeRef(TypeRef::TagPath(), Parse_Path(lex, false, PATH_GENERIC_TYPE)); // relative path
// '::' - Absolute path
case TOK_DOUBLE_COLON:
// Path with generics
return TypeRef(TypeRef::TagPath(), Parse_Path(lex, true, PATH_GENERIC_TYPE));
// 'super' - Parent relative path
case TOK_RWORD_SUPER:
GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
return TypeRef(TypeRef::TagPath(), Parse_PathFrom(lex, AST::Path(AST::Path::TagSuper()), PATH_GENERIC_TYPE));
// HACK! Convert && into & &
case TOK_DOUBLE_AMP:
lex.putback(Token(TOK_AMP));
// '&' - Reference type
case TOK_AMP: {
::std::string lifetime;
// Reference
tok = lex.getToken();
if( tok.type() == TOK_LIFETIME ) {
lifetime = tok.str();
tok = lex.getToken();
}
if( tok.type() == TOK_RWORD_MUT ) {
// Mutable reference
return TypeRef(TypeRef::TagReference(), true, Parse_Type(lex));
}
else {
lex.putback(tok);
// Immutable reference
return TypeRef(TypeRef::TagReference(), false, Parse_Type(lex));
}
throw ParseError::BugCheck("Reached end of Parse_Type:AMP");
}
// '*' - Raw pointer
case TOK_STAR:
// Pointer
switch( GET_TOK(tok, lex) )
{
case TOK_RWORD_MUT:
// Mutable pointer
return TypeRef(TypeRef::TagPointer(), true, Parse_Type(lex));
case TOK_RWORD_CONST:
// Immutable pointer
return TypeRef(TypeRef::TagPointer(), false, Parse_Type(lex));
default:
throw ParseError::Unexpected(lex, tok, Token(TOK_RWORD_CONST));
}
throw ParseError::BugCheck("Reached end of Parse_Type:STAR");
// '[' - Array type
case TOK_SQUARE_OPEN: {
// Array
TypeRef inner = Parse_Type(lex);
if( GET_TOK(tok, lex) == TOK_SEMICOLON ) {
// Sized array
AST::Expr array_size = Parse_Expr(lex, true);
GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
return TypeRef(TypeRef::TagSizedArray(), inner, array_size.take_node());
}
else if( tok.type() == TOK_SQUARE_CLOSE )
{
return TypeRef(TypeRef::TagUnsizedArray(), inner);
}
else {
throw ParseError::Unexpected(lex, tok/*, "; or ]"*/);
}
}
// '(' - Tuple (or lifetime bounded trait)
case TOK_PAREN_OPEN: {
DEBUG("Tuple");
if( GET_TOK(tok, lex) == TOK_PAREN_CLOSE )
return TypeRef(TypeRef::TagTuple(), {});
lex.putback(tok);
TypeRef inner = Parse_Type(lex);
if( GET_TOK(tok, lex) == TOK_PLUS )
{
// Lifetime bounded type, NOT a tuple
GET_CHECK_TOK(tok, lex, TOK_LIFETIME);
::std::string lifetime = tok.str();
GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE);
// TODO: Actually use lifetime bound
DEBUG("TODO: Use lifetime bound '" << lifetime << " on type " << inner);
return ::std::move(inner);
}
else
{
::std::vector<TypeRef> types;
types.push_back( ::std::move(inner) );
lex.putback(tok);
while( GET_TOK(tok, lex) == TOK_COMMA )
{
if( GET_TOK(tok, lex) == TOK_PAREN_CLOSE )
break;
else
lex.putback(tok);
types.push_back(Parse_Type(lex));
}
CHECK_TOK(tok, TOK_PAREN_CLOSE);
return TypeRef(TypeRef::TagTuple(), types); }
}
case TOK_EXCLAM:
throw ParseError::Generic(lex, "! is not a real type");
default:
throw ParseError::Unexpected(lex, tok);
}
throw ParseError::BugCheck("Reached end of Parse_Type");
}
TypeRef Parse_Type_Fn(TokenStream& lex, ::std::string abi)
{
TRACE_FUNCTION;
Token tok;
::std::vector<TypeRef> args;
GET_CHECK_TOK(tok, lex, TOK_PAREN_OPEN);
while( LOOK_AHEAD(lex) != TOK_PAREN_CLOSE )
{
args.push_back( Parse_Type(lex) );
if( GET_TOK(tok, lex) != TOK_COMMA ) {
lex.putback(tok);
break;
}
}
GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE);
TypeRef ret_type = TypeRef(TypeRef::TagUnit());
if( GET_TOK(tok, lex) == TOK_THINARROW )
{
ret_type = Parse_Type(lex);
}
else {
lex.putback(tok);
}
return TypeRef(TypeRef::TagFunction(), ::std::move(abi), ::std::move(args), ::std::move(ret_type));
}
|