-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathast.cpp
More file actions
724 lines (609 loc) · 18 KB
/
ast.cpp
File metadata and controls
724 lines (609 loc) · 18 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#include <cassert>
#include <algorithm>
#include "artic/ast.h"
#include "artic/types.h"
namespace artic::ast {
bool Type::is_tuple() const { return isa<TupleType>(); }
bool Expr::is_tuple() const { return isa<TupleExpr>(); }
bool Ptrn::is_tuple() const { return isa<TuplePtrn>(); }
std::string PrimType::tag_to_string(Tag tag) {
switch (tag) {
case Bool: return "bool";
case I8: return "i8";
case I16: return "i16";
case I32: return "i32";
case I64: return "i64";
case U8: return "u8";
case U16: return "u16";
case U32: return "u32";
case U64: return "u64";
case F16: return "f16";
case F32: return "f32";
case F64: return "f64";
default:
assert(false);
return "";
}
}
PrimType::Tag PrimType::tag_from_token(const Token& token) {
static std::unordered_map<std::string, Tag> tag_map{
std::make_pair("bool", Bool),
std::make_pair("i8", I8),
std::make_pair("i16", I16),
std::make_pair("i32", I32),
std::make_pair("i64", I64),
std::make_pair("u8", U8),
std::make_pair("u16", U16),
std::make_pair("u32", U32),
std::make_pair("u64", U64),
std::make_pair("f16", F16),
std::make_pair("f32", F32),
std::make_pair("f64", F64),
};
auto it = tag_map.find(token.string());
return it != tag_map.end() ? it->second : Error;
}
std::string UnaryExpr::tag_to_string(Tag tag) {
switch (tag) {
case Not: return "!";
case Plus: return "+";
case Minus: return "-";
case PostInc:
case PreInc:
return "++";
case PostDec:
case PreDec:
return "--";
case AddrOf:
case AddrOfMut:
return "&";
case Deref: return "*";
case Known: return "?";
case Forget: return "$";
default:
assert(false);
return "";
}
}
UnaryExpr::Tag UnaryExpr::tag_from_token(const Token& token, bool prefix) {
switch (token.tag()) {
case Token::Not: return Not;
case Token::Add: return Plus;
case Token::Sub: return Minus;
case Token::Inc: return prefix ? PreInc : PostInc;
case Token::Dec: return prefix ? PreDec : PostDec;
case Token::And: return AddrOf;
case Token::Mul: return Deref;
case Token::QMark: return Known;
case Token::Dollar: return Forget;
default: return Error;
}
}
BinaryExpr::Tag BinaryExpr::remove_eq(Tag tag) {
switch (tag) {
case AddEq: return Add;
case SubEq: return Sub;
case MulEq: return Mul;
case DivEq: return Div;
case RemEq: return Rem;
case AndEq: return And;
case OrEq: return Or;
case XorEq: return Xor;
case LShftEq: return LShft;
case RShftEq: return RShft;
default:
return tag;
}
}
bool BinaryExpr::has_eq(Tag tag) {
switch (tag) {
case Eq:
case AddEq:
case SubEq:
case MulEq:
case DivEq:
case RemEq:
case AndEq:
case OrEq:
case XorEq:
case LShftEq:
case RShftEq:
return true;
default: return false;
}
}
bool BinaryExpr::has_cmp(Tag tag) {
switch (tag) {
case CmpLT:
case CmpGT:
case CmpLE:
case CmpGE:
case CmpEq:
case CmpNE:
return true;
default: return false;
}
}
bool BinaryExpr::is_logic(Tag tag) {
return tag == LogicAnd || tag == LogicOr;
}
int BinaryExpr::precedence(Tag tag) {
switch (tag) {
case Mul:
case Div:
case Rem:
return 1;
case Add:
case Sub:
return 2;
case LShft:
case RShft:
return 3;
case And: return 4;
case Xor: return 5;
case Or: return 6;
case CmpLT:
case CmpGT:
case CmpLE:
case CmpGE:
case CmpEq:
case CmpNE:
return 7;
case LogicAnd: return 8;
case LogicOr: return 9;
case Eq:
case AddEq:
case SubEq:
case MulEq:
case DivEq:
case RemEq:
case AndEq:
case OrEq:
case XorEq:
case LShftEq:
case RShftEq:
return 10;
default:
assert(false);
return 0;
}
}
int BinaryExpr::max_precedence() { return 10; }
std::string BinaryExpr::tag_to_string(Tag tag) {
switch (tag) {
case Eq: return "=";
case AddEq: return "+=";
case SubEq: return "-=";
case MulEq: return "*=";
case DivEq: return "/=";
case RemEq: return "%=";
case AndEq: return "&=";
case OrEq: return "|=";
case XorEq: return "^=";
case LShftEq: return "<<=";
case RShftEq: return ">>=";
case Add: return "+";
case Sub: return "-";
case Mul: return "*";
case Div: return "/";
case Rem: return "%";
case And: return "&";
case Or: return "|";
case Xor: return "^";
case LShft: return "<<";
case RShft: return ">>";
case LogicAnd: return "&&";
case LogicOr: return "||";
case CmpLT: return "<";
case CmpGT: return ">";
case CmpLE: return "<=";
case CmpGE: return ">=";
case CmpEq: return "==";
case CmpNE: return "!=";
default:
assert(false);
return "";
}
}
BinaryExpr::Tag BinaryExpr::tag_from_token(const Token& token) {
switch (token.tag()) {
case Token::Eq: return Eq;
case Token::AddEq: return AddEq;
case Token::SubEq: return SubEq;
case Token::MulEq: return MulEq;
case Token::DivEq: return DivEq;
case Token::RemEq: return RemEq;
case Token::AndEq: return AndEq;
case Token::OrEq: return OrEq;
case Token::XorEq: return XorEq;
case Token::LShftEq: return LShftEq;
case Token::RShftEq: return RShftEq;
case Token::Add: return Add;
case Token::Sub: return Sub;
case Token::Mul: return Mul;
case Token::Div: return Div;
case Token::Rem: return Rem;
case Token::And: return And;
case Token::Or: return Or;
case Token::Xor: return Xor;
case Token::LShft: return LShft;
case Token::RShft: return RShft;
case Token::LogicAnd: return LogicAnd;
case Token::LogicOr: return LogicOr;
case Token::CmpLT: return CmpLT;
case Token::CmpGT: return CmpGT;
case Token::CmpLE: return CmpLE;
case Token::CmpGE: return CmpGE;
case Token::CmpEq: return CmpEq;
case Token::CmpNE: return CmpNE;
default: return Error;
}
}
void ModDecl::set_super() {
for (auto& decl : decls) {
if (auto mod_decl = decl->isa<ModDecl>())
mod_decl->super = this;
}
}
// Attributes ----------------------------------------------------------------------
static const Attr* find(const PtrVector<Attr>& attrs, const std::string_view& name) {
for (auto& attr : attrs) {
if (attr->name == name)
return attr.get();
}
return nullptr;
}
const Attr* Attr::find(const std::string_view&) const {
return nullptr;
}
const Attr* NamedAttr::find(const std::string_view& name) const {
return ast::find(args, name);
}
// Statements ----------------------------------------------------------------------
bool DeclStmt::is_jumping() const {
return
decl->isa<LetDecl>() &&
decl->as<LetDecl>()->init &&
decl->as<LetDecl>()->init->is_jumping();
}
bool DeclStmt::needs_semicolon() const {
return false;
}
bool DeclStmt::has_side_effect() const {
return true;
}
bool ExprStmt::is_jumping() const {
return expr->is_jumping();
}
bool ExprStmt::needs_semicolon() const {
return
!expr->isa<BlockExpr>() &&
!expr->isa<IfExpr>() &&
!expr->isa<MatchExpr>() &&
!expr->isa<WhileExpr>() &&
!expr->isa<ForExpr>();
}
bool ExprStmt::has_side_effect() const {
return expr->has_side_effect();
}
// Expressions ---------------------------------------------------------------------
bool TypedExpr::is_jumping() const {
return expr->is_jumping();
}
bool TypedExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool TypedExpr::is_constant() const {
return expr->is_constant();
}
bool PathExpr::is_constant() const {
assert(type);
return !type->isa<artic::RefType>();
}
void PathExpr::write_to() const {
if (path.start_decl) {
if (auto ptrn_decl = path.start_decl->isa<PtrnDecl>(); ptrn_decl && ptrn_decl->is_mut)
ptrn_decl->written_to = true;
}
}
bool LiteralExpr::is_constant() const {
return true;
}
bool FieldExpr::is_jumping() const {
return expr->is_jumping();
}
bool FieldExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool FieldExpr::is_constant() const {
return expr->is_constant();
}
bool RecordExpr::is_jumping() const {
return (expr && expr->is_jumping()) || std::any_of(fields.begin(), fields.end(), [] (auto& field) {
return field->is_jumping();
});
}
bool RecordExpr::has_side_effect() const {
return (expr && expr->has_side_effect()) || std::any_of(fields.begin(), fields.end(), [] (auto& field) {
return field->has_side_effect();
});
}
bool RecordExpr::is_constant() const {
return (!expr || expr->is_constant()) && std::all_of(fields.begin(), fields.end(), [] (auto& field) {
return field->is_constant();
});
}
bool TupleExpr::is_jumping() const {
return std::any_of(args.begin(), args.end(), [] (auto& arg) {
return arg->is_jumping();
});
}
bool TupleExpr::has_side_effect() const {
return std::any_of(args.begin(), args.end(), [] (auto& arg) {
return arg->has_side_effect();
});
}
bool TupleExpr::is_constant() const {
return std::all_of(args.begin(), args.end(), [] (auto& arg) {
return arg->is_constant();
});
}
bool ArrayExpr::is_jumping() const {
return std::any_of(elems.begin(), elems.end(), [] (auto& elem) {
return elem->is_jumping();
});
}
bool ArrayExpr::has_side_effect() const {
return std::any_of(elems.begin(), elems.end(), [] (auto& elem) {
return elem->has_side_effect();
});
}
bool ArrayExpr::is_constant() const {
return std::all_of(elems.begin(), elems.end(), [] (auto& elem) {
return elem->is_constant();
});
}
bool RepeatArrayExpr::is_jumping() const {
return elem->is_jumping();
}
bool RepeatArrayExpr::has_side_effect() const {
return elem->has_side_effect();
}
bool RepeatArrayExpr::is_constant() const {
return elem->is_constant();
}
bool FnExpr::is_constant() const {
return true;
}
bool BlockExpr::is_jumping() const {
return std::any_of(stmts.begin(), stmts.end(), [] (auto& stmt) {
return stmt->is_jumping();
});
}
bool BlockExpr::has_side_effect() const {
return std::any_of(stmts.begin(), stmts.end(), [] (auto& stmt) {
return stmt->has_side_effect();
});
}
bool CallExpr::is_jumping() const {
assert(type);
return type->isa<artic::NoRetType>();
}
bool CallExpr::has_side_effect() const {
return true;
}
void CallExpr::write_to() const {
callee->write_to();
}
bool ProjExpr::is_jumping() const {
return expr->is_jumping();
}
bool ProjExpr::has_side_effect() const {
return expr->has_side_effect();
}
void ProjExpr::write_to() const {
expr->write_to();
}
bool IfExpr::is_jumping() const {
return
(cond && cond->is_jumping()) ||
(expr && expr->is_jumping()) ||
(if_true->is_jumping() && if_false && if_false->is_jumping());
}
bool IfExpr::has_side_effect() const {
return
(cond && cond->has_side_effect()) ||
(expr && expr->has_side_effect()) ||
if_true->has_side_effect() ||
(if_false && if_false->has_side_effect());
}
bool CaseExpr::is_jumping() const {
return expr->is_jumping();
}
bool CaseExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool MatchExpr::is_jumping() const {
return
arg->is_jumping() ||
std::all_of(cases.begin(), cases.end(), [] (auto& case_) {
return case_->is_jumping();
});
}
bool MatchExpr::has_side_effect() const {
return
arg->has_side_effect() ||
std::any_of(cases.begin(), cases.end(), [] (auto& case_) {
return case_->has_side_effect();
});
}
bool WhileExpr::is_jumping() const {
return false;
}
bool WhileExpr::has_side_effect() const {
return
(cond && cond->has_side_effect()) ||
(expr && expr->has_side_effect()) ||
body->has_side_effect();
}
bool ForExpr::is_jumping() const {
return call->is_jumping();
}
bool ForExpr::has_side_effect() const {
return call->has_side_effect();
}
bool UnaryExpr::is_jumping() const {
return arg->is_jumping();
}
bool UnaryExpr::has_side_effect() const {
return is_inc() || is_dec() || arg->has_side_effect();
}
bool UnaryExpr::is_constant() const {
switch (tag) {
case Plus:
case Minus:
case Known:
case Forget:
return arg->is_constant();
default:
return false;
}
}
bool BinaryExpr::is_jumping() const {
// Logical operators are lazy. So, to be sure that the expression jumps,
// we have to check that both arguments do.
return is_logic()
? left->is_jumping() && right->is_jumping()
: left->is_jumping() || right->is_jumping();
}
bool BinaryExpr::has_side_effect() const {
return has_eq() || left->has_side_effect() || right->has_side_effect();
}
bool BinaryExpr::is_constant() const {
return !has_eq() && left->is_constant() && right->is_constant();
}
bool FilterExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool CastExpr::is_jumping() const {
return expr->is_jumping();
}
bool CastExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool CastExpr::is_constant() const {
return expr->is_constant();
}
bool ImplicitCastExpr::is_jumping() const {
return expr->is_jumping();
}
bool ImplicitCastExpr::has_side_effect() const {
return expr->has_side_effect();
}
bool ImplicitCastExpr::is_constant() const {
assert(expr->type);
if (auto path_expr = expr->isa<PathExpr>();
path_expr && path_expr->path.elems.size() == 1 && path_expr->path.start_decl)
{
if (auto static_decl = path_expr->path.start_decl->isa<StaticDecl>()) {
// Allow using other constant static declarations as constants
return !static_decl->is_mut;
}
}
return expr->is_constant();
}
bool AsmExpr::has_side_effect() const {
return !outs.empty() || std::find(opts.begin(), opts.end(), "volatile") != opts.end();
}
// Patterns ------------------------------------------------------------------------
void Ptrn::collect_bound_ptrns(std::vector<const IdPtrn*>&) const {}
void TypedPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
ptrn->collect_bound_ptrns(bound_ptrns);
}
bool TypedPtrn::is_trivial() const {
return !ptrn || ptrn->is_trivial();
}
const Expr* TypedPtrn::to_expr(Arena& arena) {
if (!ptrn)
return nullptr;
return ptrn->to_expr(arena);
}
void IdPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
bound_ptrns.emplace_back(this);
if (sub_ptrn)
sub_ptrn->collect_bound_ptrns(bound_ptrns);
}
bool IdPtrn::is_trivial() const {
return !sub_ptrn || sub_ptrn->is_trivial();
}
const Expr* IdPtrn::to_expr(Arena& arena) {
if (as_expr)
return as_expr.get();
Identifier id = decl->id;
std::vector<Path::Elem> elems;
elems.push_back(Path::Elem( loc, std::move(id), {} ));
Path path = Path(loc, std::move(elems));
path.start_decl = decl.get();
path.is_value = true;
as_expr = arena.make_ptr<PathExpr>(std::move(path));
return as_expr.get();
}
bool LiteralPtrn::is_trivial() const {
return false;
}
const Expr* LiteralPtrn::to_expr(Arena& arena) {
if (as_expr)
return as_expr.get();
as_expr = arena.make_ptr<LiteralExpr>(loc, lit);
return as_expr.get();
}
bool ImplicitParamPtrn::is_trivial() const {
return underlying->is_trivial();
}
bool DefaultParamPtrn::is_trivial() const {
return underlying->is_trivial();
}
void FieldPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
if (ptrn)
ptrn->collect_bound_ptrns(bound_ptrns);
}
bool FieldPtrn::is_trivial() const {
return !ptrn || ptrn->is_trivial();
}
void RecordPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
for (auto& field : fields)
field->collect_bound_ptrns(bound_ptrns);
}
bool RecordPtrn::is_trivial() const {
assert(type);
return
match_app<StructType>(type).second &&
std::all_of(fields.begin(), fields.end(), [] (auto& field) {
return field->is_trivial();
});
}
void CtorPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
if (arg) arg->collect_bound_ptrns(bound_ptrns);
}
bool CtorPtrn::is_trivial() const {
assert(type);
return match_app<StructType>(type).second && (!arg || arg->is_trivial());
}
void TuplePtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
for (auto& arg : args)
arg->collect_bound_ptrns(bound_ptrns);
}
bool TuplePtrn::is_trivial() const {
return std::all_of(args.begin(), args.end(), [] (auto& arg) { return arg->is_trivial(); });
}
void ArrayPtrn::collect_bound_ptrns(std::vector<const IdPtrn*>& bound_ptrns) const {
for (auto& elem : elems)
elem->collect_bound_ptrns(bound_ptrns);
}
bool ArrayPtrn::is_trivial() const {
return std::all_of(elems.begin(), elems.end(), [] (auto& elem) { return elem->is_trivial(); });
}
bool ErrorPtrn::is_trivial() const {
return false;
}
} // namespace artic::ast