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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* parse/types.cpp
* - Parsing for type usages
*/
#include "common.hpp"
#include "parseerror.hpp"
#include <ast/types.hpp>
#include <ast/ast.hpp>
// === PROTOTYPES ===
//TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list);
TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list);
TypeRef Parse_Type_Fn(TokenStream& lex, AST::HigherRankedBounds hrbs = {});
TypeRef Parse_Type_Path(TokenStream& lex, AST::HigherRankedBounds hrbs, bool allow_trait_list);
TypeRef Parse_Type_ErasedType(TokenStream& lex, bool allow_trait_list);
// === CODE ===
TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list)
{
ProtoSpan ps = lex.start_span();
TypeRef rv = Parse_Type_Int(lex, allow_trait_list);
//rv.set_span(lex.end_span(ps));
return rv;
}
TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list)
{
//TRACE_FUNCTION;
auto ps = lex.start_span();
Token tok;
switch( GET_TOK(tok, lex) )
{
case TOK_INTERPOLATED_TYPE:
return mv$(tok.frag_type());
// '!' - Only ever used as part of function prototypes, but is kinda a type... not allowed here though
case TOK_EXCLAM:
return TypeRef( Span(tok.get_pos()), TypeData::make_Bang({}) );
// '_' = Wildcard (type inferrence variable)
case TOK_UNDERSCORE:
return TypeRef(Span(tok.get_pos()));
// 'unsafe' - An unsafe function type
case TOK_RWORD_UNSAFE:
// 'extern' - A function type with an ABI
case TOK_RWORD_EXTERN:
// 'fn' - Rust function
case TOK_RWORD_FN:
PUTBACK(tok, lex);
return Parse_Type_Fn(lex);
case TOK_RWORD_IMPL:
return Parse_Type_ErasedType(lex, allow_trait_list);
// '<' - An associated type cast
case TOK_LT:
case TOK_DOUBLE_LT: {
PUTBACK(tok, lex);
auto path = Parse_Path(lex, PATH_GENERIC_TYPE);
return TypeRef(TypeRef::TagPath(), lex.end_span(ps), mv$(path));
}
//
case TOK_RWORD_FOR: {
auto hrls = Parse_HRB(lex);
switch(LOOK_AHEAD(lex))
{
case TOK_RWORD_UNSAFE:
case TOK_RWORD_EXTERN:
case TOK_RWORD_FN:
// TODO: Handle HRLS in fn types
return Parse_Type_Fn(lex, hrls);
default:
return Parse_Type_Path(lex, hrls, true);
}
}
// <ident> - Either a primitive, or a path
case TOK_IDENT:
if( lex.lookahead(0) == TOK_EXCLAM )
{
lex.getToken();
// TODO: path macros
return TypeRef(TypeRef::TagMacro(), Parse_MacroInvocation(ps, mv$(tok.str()), lex));
}
// or a primitive
//if( auto ct = coretype_fromstring(tok.str()) )
//{
// return TypeRef(TypeRef::TagPrimitive(), Span(tok.get_pos()), ct);
//}
PUTBACK(tok, lex);
return Parse_Type_Path(lex, {}, allow_trait_list);
// - Fall through to path handling
// '::' - Absolute path
case TOK_DOUBLE_COLON:
// 'self' - This relative path
case TOK_RWORD_SELF:
// 'super' - Parent relative path
case TOK_RWORD_SUPER:
// ':path' fragment
case TOK_INTERPOLATED_PATH:
PUTBACK(tok, lex);
return Parse_Type_Path(lex, {}, allow_trait_list);
// HACK! Convert && into & &
case TOK_DOUBLE_AMP:
lex.putback(Token(TOK_AMP));
// '&' - Reference type
case TOK_AMP: {
AST::LifetimeRef lifetime;
// Reference
tok = lex.getToken();
if( tok.type() == TOK_LIFETIME ) {
lifetime = AST::LifetimeRef(/*lex.point_span(), */lex.get_ident(::std::move(tok)));
tok = lex.getToken();
}
bool is_mut = false;
if( tok.type() == TOK_RWORD_MUT ) {
is_mut = true;
}
else {
PUTBACK(tok, lex);
}
return TypeRef(TypeRef::TagReference(), lex.end_span(ps), ::std::move(lifetime), is_mut, Parse_Type(lex, false));
}
// '*' - Raw pointer
case TOK_STAR:
// Pointer
switch( GET_TOK(tok, lex) )
{
case TOK_RWORD_MUT:
// Mutable pointer
return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), true, Parse_Type(lex, false));
case TOK_RWORD_CONST:
// Immutable pointer
return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), false, Parse_Type(lex, false));
default:
throw ParseError::Unexpected(lex, tok, {TOK_RWORD_CONST, TOK_RWORD_MUT});
}
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);
GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE);
return TypeRef(TypeRef::TagSizedArray(), lex.end_span(ps), mv$(inner), array_size.take_node());
}
else if( tok.type() == TOK_SQUARE_CLOSE )
{
return TypeRef(TypeRef::TagUnsizedArray(), lex.end_span(ps), mv$(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.end_span(ps), {});
PUTBACK(tok, lex);
TypeRef inner = Parse_Type(lex, true);
if( LOOK_AHEAD(lex) == TOK_PAREN_CLOSE )
{
// Type in parens, NOT a tuple
GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE);
return inner;
}
else
{
::std::vector<TypeRef> types;
types.push_back( mv$(inner) );
while( GET_TOK(tok, lex) == TOK_COMMA )
{
if( GET_TOK(tok, lex) == TOK_PAREN_CLOSE )
break;
else
PUTBACK(tok, lex);
types.push_back( Parse_Type(lex) );
}
CHECK_TOK(tok, TOK_PAREN_CLOSE);
return TypeRef(TypeRef::TagTuple(), lex.end_span(ps), mv$(types)); }
}
default:
throw ParseError::Unexpected(lex, tok);
}
throw ParseError::BugCheck("Reached end of Parse_Type");
}
TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs)
{
auto ps = lex.start_span();
// TODO: HRLs
TRACE_FUNCTION;
Token tok;
::std::string abi = "";
bool is_unsafe = false;
GET_TOK(tok, lex);
if( tok.type() == TOK_RWORD_UNSAFE )
{
is_unsafe = true;
GET_TOK(tok, lex);
}
if( tok.type() == TOK_RWORD_EXTERN )
{
if( GET_TOK(tok, lex) == TOK_STRING ) {
abi = tok.str();
if( abi == "" )
ERROR(lex.point_span(), E0000, "Empty ABI");
GET_TOK(tok, lex);
}
else {
abi = "C";
}
}
CHECK_TOK(tok, TOK_RWORD_FN);
::std::vector<TypeRef> args;
bool is_variadic = false;
GET_CHECK_TOK(tok, lex, TOK_PAREN_OPEN);
while( LOOK_AHEAD(lex) != TOK_PAREN_CLOSE )
{
if( LOOK_AHEAD(lex) == TOK_TRIPLE_DOT ) {
GET_TOK(tok, lex);
is_variadic = true;
break;
}
// Handle `ident: `
if( (lex.lookahead(0) == TOK_IDENT || lex.lookahead(0) == TOK_UNDERSCORE) && lex.lookahead(1) == TOK_COLON ) {
GET_TOK(tok, lex);
GET_TOK(tok, lex);
}
args.push_back( Parse_Type(lex) );
if( GET_TOK(tok, lex) != TOK_COMMA ) {
PUTBACK(tok, lex);
break;
}
}
GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE);
TypeRef ret_type = TypeRef(TypeRef::TagUnit(), Span(tok.get_pos()));
if( GET_TOK(tok, lex) == TOK_THINARROW )
{
ret_type = Parse_Type(lex, false);
}
else {
PUTBACK(tok, lex);
}
return TypeRef(TypeRef::TagFunction(), lex.end_span(ps), is_unsafe, mv$(abi), mv$(args), is_variadic, mv$(ret_type));
}
TypeRef Parse_Type_Path(TokenStream& lex, ::AST::HigherRankedBounds hrbs, bool allow_trait_list)
{
Token tok;
auto ps = lex.start_span();
if( hrbs.empty() && !allow_trait_list )
{
return TypeRef(TypeRef::TagPath(), lex.end_span(ps), Parse_Path(lex, PATH_GENERIC_TYPE));
}
else
{
::std::vector<Type_TraitPath> traits;
::std::vector<AST::LifetimeRef> lifetimes;
traits.push_back(Type_TraitPath { mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) });
if( allow_trait_list )
{
while( GET_TOK(tok, lex) == TOK_PLUS )
{
if( LOOK_AHEAD(lex) == TOK_LIFETIME ) {
GET_TOK(tok, lex);
lifetimes.push_back(AST::LifetimeRef( /*lex.point_span(),*/ lex.get_ident(mv$(tok)) ));
}
else
{
if( lex.lookahead(0) == TOK_RWORD_FOR )
{
hrbs = Parse_HRB(lex);
}
traits.push_back({ mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) });
}
}
PUTBACK(tok, lex);
}
if( !traits[0].hrbs.empty() || traits.size() > 1 || lifetimes.size() > 0 )
{
if( lifetimes.empty())
lifetimes.push_back(AST::LifetimeRef());
return TypeRef(lex.end_span(ps), mv$(traits), mv$(lifetimes));
}
else
{
return TypeRef(TypeRef::TagPath(), lex.end_span(ps), mv$(traits.at(0).path));
}
}
}
TypeRef Parse_Type_ErasedType(TokenStream& lex, bool allow_trait_list)
{
Token tok;
auto ps = lex.start_span();
::std::vector<Type_TraitPath> traits;
::std::vector<AST::LifetimeRef> lifetimes;
do {
if( LOOK_AHEAD(lex) == TOK_LIFETIME ) {
GET_TOK(tok, lex);
lifetimes.push_back(AST::LifetimeRef( /*lex.point_span(),*/ lex.get_ident(mv$(tok)) ));
}
else
{
AST::HigherRankedBounds hrbs;
if( lex.lookahead(0) == TOK_RWORD_FOR )
{
hrbs = Parse_HRB(lex);
}
traits.push_back({ mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) });
}
} while( GET_TOK(tok, lex) == TOK_PLUS );
PUTBACK(tok, lex);
return TypeRef(lex.end_span(ps), TypeData::make_ErasedType({ mv$(traits), mv$(lifetimes) }));
}
|