-
Notifications
You must be signed in to change notification settings - Fork 4
/
NacroParsers.cpp
314 lines (263 loc) · 7.55 KB
/
NacroParsers.cpp
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
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/DiagnosticSema.h"
#include "NacroParsers.h"
using namespace clang;
using llvm::StringRef;
using llvm::ArrayRef;
using llvm::Optional;
NacroRuleParser::NacroRuleParser(Preprocessor& PP, ArrayRef<Token> Params)
: NacroParser(PP, Params) {
IdentifierInfo* NameII = nullptr;
if(PragmaParams.size() > 0) {
auto NameTok = PragmaParams[0];
if(NameTok.isNot(tok::identifier)) {
PP.Diag(NameTok, diag::err_expected) << "rule name identifier";
HasEncounteredError = true;
} else {
NameII = NameTok.getIdentifierInfo();
assert(NameII);
}
}
CurrentRule = NacroRule::Create(NameII);
}
bool NacroRuleParser::ParseArgList() {
Token Tok;
Tok.startToken();
do {
PP.Lex(Tok);
if(Tok.isNot(tok::identifier)) {
PP.Diag(Tok, diag::err_expected) << "an identifier";
return false;
}
auto* ArgII = Tok.getIdentifierInfo();
assert(ArgII);
PP.Lex(Tok);
if(Tok.isNot(tok::colon)) {
PP.Diag(Tok, diag::err_expected) << tok::colon;
return false;
}
PP.Lex(Tok);
if(Tok.isNot(tok::identifier)) {
PP.Diag(Tok, diag::err_expected) << "argument type";
return false;
}
auto* TII = Tok.getIdentifierInfo();
assert(TII);
auto RT = NacroRule::GetReplacementTy(TII->getName());
if(RT == NacroRule::ReplacementTy::UNKNOWN) {
PP.Diag(Tok, diag::err_unknown_typename) << TII->getName();
return false;
}
bool isVarArgs = false;;
PP.Lex(Tok);
if(Tok.is(tok::star)) {
// VARARGS
isVarArgs = true;
PP.Lex(Tok);
}
CurrentRule->AddReplacement(ArgII, RT, isVarArgs);
if(Tok.is(tok::r_paren)) break;
if(Tok.isNot(tok::comma)) {
PP.Diag(Tok, diag::err_expected) << tok::comma;
return false;
}
} while(true);
return true;
}
bool NacroRuleParser::ParseStmts() {
if(CurTok.isNot(tok::l_brace)) {
PP.Diag(CurTok, diag::err_expected) << tok::l_brace;
return false;
}
CurrentRule->AddToken(CurTok);
while(true) {
Advance();
// Handle some special directives
if(CurTok.is(tok::identifier)) {
auto* II = CurTok.getIdentifierInfo();
assert(II);
if(II->isStr("$loop")) {
if(!ParseLoop()) return false;
continue;
} else if(II->isStr("$str")) {
if(!ParseStr()) return false;
continue;
}
}
if(CurTok.is(tok::eof)) {
PP.Diag(CurTok, diag::err_expected)
<< "'}'. Might be caused by mismatch braces";
return false;
}
if(CurTok.is(tok::l_brace)) {
if(!ParseStmts()) return false;
continue;
}
CurrentRule->AddToken(CurTok);
if(CurTok.is(tok::r_brace))
return true;
}
}
bool NacroRuleParser::ParseStr() {
assert(CurTok.is(tok::identifier));
assert(CurTok.getIdentifierInfo() &&
CurTok.getIdentifierInfo()->isStr("$str"));
PP.Lex(CurTok);
if(CurTok.isNot(tok::l_paren)) {
PP.Diag(CurTok, diag::err_expected) << tok::l_paren;
return false;
}
// We need their Location info
Token LParenTok = CurTok;
PP.Lex(CurTok);
Token StrTok = CurTok;
PP.Lex(CurTok);
if(CurTok.isNot(tok::r_paren)) {
PP.Diag(CurTok, diag::err_expected) << tok::r_paren;
return false;
}
LParenTok.setKind(tok::hash);
CurrentRule->AddToken(LParenTok);
CurrentRule->AddToken(StrTok);
return true;
}
Optional<NacroRule::Loop> NacroRuleParser::ParseLoopHeader() {
using llvm::None;
Token Tok;
PP.Lex(Tok);
if(Tok.isNot(tok::l_paren)) {
PP.Diag(Tok, diag::err_expected) << tok::l_paren;
return None;
}
// `$v in $range`
PP.Lex(Tok);
if(Tok.isNot(tok::identifier)) {
PP.Diag(Tok, diag::err_expected)
<< "an identifier as the induction variable";
return None;
}
auto* IV = Tok.getIdentifierInfo();
assert(IV);
PP.Lex(Tok);
if(Tok.isNot(tok::identifier) ||
!Tok.getIdentifierInfo() ||
!Tok.getIdentifierInfo()->isStr("in")) {
PP.Diag(Tok, diag::err_expected) << "keyword 'in'";
return None;
}
// TODO: For now we only support single (varargs) variable as
// the iteration range
PP.Lex(Tok);
if(Tok.isNot(tok::identifier)) {
PP.Diag(Tok, diag::err_expected)
<< "an identifier as the iteration range";
return None;
}
auto* IterRange = Tok.getIdentifierInfo();
assert(IterRange);
PP.Lex(Tok);
if(Tok.isNot(tok::r_paren)) {
PP.Diag(Tok, diag::err_expected) << tok::r_paren;
return None;
}
return NacroRule::Loop{IV, IterRange};
}
bool NacroRuleParser::ParseLoop() {
assert(CurTok.is(tok::identifier));
auto* II = CurTok.getIdentifierInfo();
assert(II && II->isStr("$loop"));
auto LH = ParseLoopHeader();
if(!LH) return false;
// Use everything (especially the SrcLoc) from `$loop`
// except the token kind
CurTok.setKind(tok::annot_pragma_loop_hint);
CurrentRule->AddToken(CurTok);
// Parse the loop body
Advance();
if(!ParseStmts()) return false;
Token LoopEndTok;
LoopEndTok.startToken();
LoopEndTok.setKind(tok::annot_pragma_loop_hint);
CurrentRule->AddToken(LoopEndTok);
CurrentRule->AddLoop(*LH);
return true;
}
/// Wrap body with correct enclosment symbols
/// based on generated type
void NacroRuleParser::WrapNacroBody() {
switch(CurrentRule->getGeneratedType()) {
case NacroRule::ReplacementTy::Expr: {
// replace with parens
Token FirstTok = CurrentRule->getToken(0),
LastTok = CurrentRule->token_back();
FirstTok.setKind(tok::l_paren);
LastTok.setKind(tok::r_paren);
auto Head = CurrentRule->erase_token(CurrentRule->token_begin());
CurrentRule->insert_token(Head, FirstTok);
auto Tail = CurrentRule->insert_token(CurrentRule->token_end(),
LastTok);
CurrentRule->erase_token(--Tail);
break;
}
case NacroRule::ReplacementTy::Stmt: {
// remove any enclosement and add semi-colon at the end
// if there hasn't any
CurrentRule->erase_token(CurrentRule->token_begin());
auto LastTok = CurrentRule->token_back();
LastTok.setKind(tok::semi);
auto Tail = CurrentRule->insert_token(CurrentRule->token_end(),
LastTok);
Tail = CurrentRule->erase_token(--Tail);
if((--Tail)->is(tok::semi)) {
// has duplicate, remove the last one
CurrentRule->erase_token(++Tail);
}
break;
}
case NacroRule::ReplacementTy::Block:
// block will remain the same
return;
}
}
bool NacroRuleParser::Parse() {
if(HasEncounteredError) return false;
Token Tok;
Tok.startToken();
// '('
PP.Lex(Tok);
if(Tok.isNot(tok::l_paren)) {
PP.Diag(Tok, diag::err_expected) << tok::l_paren;
return false;
}
// first token of the entire nacro, including argument list
auto FirstTok = Tok;
if(!ParseArgList()) return false;
// '->'
PP.Lex(Tok);
if(Tok.isNot(tok::arrow)) {
PP.Diag(Tok, diag::err_expected) << tok::arrow;
return false;
}
// generated type
Advance();
if(CurTok.is(tok::identifier)) {
// Explicitily specified the generated type
auto* GII = CurTok.getIdentifierInfo();
assert(GII);
auto GT = NacroRule::GetReplacementTy(GII->getName());
if(GT == NacroRule::ReplacementTy::UNKNOWN) {
PP.Diag(Tok, diag::err_unknown_typename) << GII->getName();
return false;
}
CurrentRule->setGeneratedType(GT);
Advance();
}
auto Res = ParseStmts();
if(Res) {
WrapNacroBody();
auto LastTok = CurrentRule->token_back();
CurrentRule->setSourceRange(SourceRange(FirstTok.getLocation(),
LastTok.getEndLoc()));
}
return Res;
}