-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypechecker.cpp
836 lines (742 loc) · 25.9 KB
/
typechecker.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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#include "typechecker.h"
#include "SymbolTable.h"
#include "error.h"
#include "type.h"
#include "platform.h"
TypeChecker::TypeChecker(ErrorLog& err)
: errors_(err)
{
}
void TypeChecker::visit(IfStmt* node)
{
node->cond_->accept(*this);
checkAsCond(node->cond_);
node->then_->accept(*this);
if (node->else_)
node->else_->accept(*this);
}
void TypeChecker::visit(WhileStmt* node)
{
node->cond_->accept(*this);
checkAsCond(node->cond_);
node->body_->accept(*this);
}
void TypeChecker::visit(ForStmt* node)
{
if (node->init_)
node->init_->accept(*this);
if (node->cond_) {
node->cond_->accept(*this);
checkAsCond(node->cond_);
}
if (node->stepby_)
node->stepby_->accept(*this);
node->body_->accept(*this);
}
void TypeChecker::visit(DoWhileStmt* node)
{
node->cond_->accept(*this);
checkAsCond(node->cond_);
node->body_->accept(*this);
}
void TypeChecker::visit(LabelStmt* node)
{
// not implemented
}
void TypeChecker::visit(CaseStmt* node)
{
// not implemented
}
void TypeChecker::visit(SwitchStmt* node)
{
// not implemented
}
void TypeChecker::visit(ReturnStmt* node)
{
node->retExpr_->accept(*this);
node->func_ = currFunc_;
checkAssignE2T(node->retExpr_, node->func_->type_->retType(), true);
}
void TypeChecker::visit(BreakStmt* node)
{
// not implemented
}
void TypeChecker::visit(CompoundDecl* node)
{
// nothing to do
}
void TypeChecker::visit(VarDef* node)
{
auto ty = node->type_;
checkVarDef(ty, node->tok());
if (node->init_) {
node->init_->accept(*this);
checkAssignE2T(node->init_, ty, true);
}
}
void TypeChecker::visit(FuncDecl* node)
{
// nothing to do
}
void TypeChecker::visit(Block* node)
{
scopeStack_.push(&node->scope());
for (auto& item : node->stmts_) {
try {
item->accept(*this);
} catch (Error& err) {
errors_.add(err);
}
}
scopeStack_.pop();
}
void TypeChecker::visit(UnaryOpExpr* node)
{
// node->isLValue = false;
std::shared_ptr<Expr>& operand = node->operand_;
operand->accept(*this);
auto op = node->operator_;
switch (op) {
case Token::Add:
case Token::Sub: {
// unary + or -
if (!Type::isArithmetic(operand->evalType)) {
throw Error(operand->tok(), "the value can't be an operand of unary operator+/-");
} else {
node->evalType = operand->evalType;
}
break;
}
case Token::Not: {
checkAsCond(operand);
node->evalType = BuiltInType::charType();
break;
}
case Token::BitInv: {
if (!Type::isInteger(operand->evalType)) {
throw Error(operand->tok(), "the value can't be an operand of operator~");
} else {
node->evalType = operand->evalType;
}
break;
}
case Token::Mult: {
// pointer deref *p
if (operand->evalType->tag() == Type::Pointer) {
node->evalType = static_cast<PointerType*>(operand->evalType.get())->base();
} else if (operand->evalType->tag() == Type::Array) {
node->evalType = static_cast<ArrayType*>(operand->evalType.get())->base();
} else {
throw Error(operand->tok(), "can't dereference such a value");
}
node->isLValue = true;
break;
}
case Token::BitAnd: {
// get address &a
if (!operand->isLValue) {
throw Error(operand->tok(), "rvalues do not have an addresses");
}
node->evalType = std::make_shared<PointerType>(operand->evalType, 0);
// make sth dirty
makeInMemory(operand);
break;
}
default:
assert("wobuzhidaode unary operator" && false);
}
}
void TypeChecker::visit(BinaryOpExpr* node)
{
node->lhs_->accept(*this);
node->rhs_->accept(*this);
auto& lhs = node->lhs_;
auto& rhs = node->rhs_;
auto op = node->operator_;
switch (op) {
case Token::Mult:
case Token::Div:
case Token::Mod: {
if (!Type::isArithmetic(lhs->evalType) ||
!Type::isArithmetic(rhs->evalType)) {
throw Error(node->tok(), "invalid type for *, / or %");
}
checkArithmetic(node, lhs, rhs);
break;
}
case Token::Add:
case Token::Sub: {
if (Type::isArithmetic(lhs->evalType)
&& Type::isArithmetic(rhs->evalType)) {
checkArithmetic(node, lhs, rhs);
} else if (lhs->evalType->tag() == Type::Pointer
&& Type::isInteger(rhs->evalType)) {
checkPointerArith(node, lhs, rhs);
} else if (rhs->evalType->tag() == Type::Pointer
&& Type::isInteger(lhs->evalType)) {
checkPointerArith(node, rhs, lhs);
} else {
throw Error(node->tok(), "invalid type for +/-");
}
break;
}
case Token::SftL:
case Token::SftR: {
if (!Type::isInteger(lhs->evalType) || !Type::isInteger(rhs->evalType)) {
throw Error(node->tok(), "only integer can be used to shift");
}
auto rhsTy = static_cast<BuiltInType*>(rhs->evalType.get());
if (!rhsTy->isUnsigned()) {
auto beUnsigned = rhsTy->builtinClone();
beUnsigned->isUnsigned(true);
rhs->promotedTo = beUnsigned;
}
node->evalType = lhs->evalType;
break;
}
case Token::Grtr:
case Token::Smlr:
case Token::Ge:
case Token::Se:
case Token::Eq:
case Token::Ne: {
if (Type::isArithmetic(lhs->evalType) && Type::isArithmetic(rhs->evalType)) {
checkArithmetic(node, lhs, rhs);
node->evalType = BuiltInType::charType();
/// so the type set for node->evalType in checkArithmetic is discarded
} else if (lhs->evalType->tag() == Type::Pointer
&& rhs->evalType->tag() == Type::Pointer) {
checkPointerCompare(node, lhs, rhs);
} else {
throw Error(node->tok(), "can't compare lhs and rhs");
}
node->evalType = BuiltInType::charType();
break;
}
case Token::BitAnd:
case Token::BitXor:
case Token::BitOr: {
if (Type::isInteger(lhs->evalType) && Type::isInteger(rhs->evalType)) {
checkArithmetic(node, lhs, rhs);
} else {
throw Error(node->tok(), "bit opearations are only for integer types");
}
break;
}
case Token::And:
case Token::Or: {
checkAsCond(lhs);
checkAsCond(rhs);
node->evalType = BuiltInType::charType();
break;
}
case Token::Assign: {
// TODO: += -= *= .......
checkAssign(rhs, lhs, false);
node->evalType = BuiltInType::voidType(); // 我希望赋值操作不返回值
break;
}
/*case Token::AddAssign: {
break;
}
case Token::SubAssign: {
break;
}
case */
default: {
throw Error(node->tok(), "unknown binary operator expression");
}
}
}
void TypeChecker::visit(ConditionExpr* node)
{
// TODO not implemented
}
void TypeChecker::visit(FuncCallExpr* node)
{
auto ent = currScope().find(node->funcName_);
if (!ent || ent->type->tag() != Type::Tag::Func) {
throw Error(node->tok(), std::string("there is no function called ") + node->funcName_);
}
auto funcType = static_cast<FuncType*>(ent->type.get());
node->evalType = funcType->retType();
auto argIt = node->args_.begin();
auto paramIt = funcType->begin();
for ( ; argIt != node->args_.end() && paramIt != funcType->end();
++argIt, ++paramIt) {
(*argIt)->accept(*this);
checkAssignE2T(*argIt, *paramIt, true);
}
if (argIt != node->args_.end() && paramIt == funcType->end()) {
if (!funcType->hasVarArgs()) {
throw Error(node->tok(), "too much arguments");
} // else ok
} else if (argIt == node->args_.end() && paramIt != funcType->end()) {
throw Error(node->tok(), "too few arguments");
}
}
void TypeChecker::visit(MemberExpr* node)
{
node->compound_->accept(*this);
auto& lhs = node->compound_;
if (lhs->evalType->tag() != Type::UserDefined) {
throw Error(node->tok(), "don't have any data member");
}
auto typeRef = static_cast<UserDefinedTypeRef*>(lhs->evalType.get());
auto name = typeRef->typeName();
auto& scope = typeRef->scope();
auto typeEnt = scope.findInCurr(name);
if (!typeEnt) {
throw Error(node->tok(), "a nonexistent compound type????");
}
auto realType = typeEnt->type;
if (realType->tag() != Type::Compound) {
throw Error(node->tok(), "variables of this type don't have any member");
}
auto compound = static_cast<CompoundType*>(realType.get());
auto memberEnt = compound->members().find(node->memberName_);
if (!memberEnt) {
throw Error(node->tok(),
std::string("variables of such type don't have a member called")
+ node->memberName_);
}
node->isLValue = node->compound_->isLValue;
if (!lhs->evalType->isConst())
node->evalType = memberEnt->type;
else {
node->evalType = memberEnt->type->shallowCopy();
node->evalType->isConst(true);
}
}
void TypeChecker::visit(ArrayRefExpr* node)
{
node->head_->accept(*this);
node->index_->accept(*this);
auto& ty = node->head_->evalType;
if (ty->tag() == Type::Array
|| ty->tag() == Type::Pointer) {
if (ty->tag() == Type::Array) {
auto arrTy = static_cast<ArrayType*>(ty.get());
node->evalType = arrTy->base();
} else {
auto ptrTy = static_cast<PointerType*>(ty.get());
node->evalType = ptrTy->base();
}
node->isLValue = true;
} else {
throw Error(node->tok(), "operator[]: expect array or pointer");
}
}
void TypeChecker::visit(VarExpr* node)
{
auto ent = currScope().find(node->name_);
if (!ent) {
throw Error(node->tok(), std::string("variable ") + node->name_ + " does not exist");
}
auto& type = ent->type;
if (ent->textPosition > node->tok()) {
throw Error(node->tok(), "variable is used before declaration");
}
checkVoid(type, node->tok());
node->evalType = type;
node->isLValue = true;
}
void TypeChecker::visit(CastExpr* node)
{
// TODO: not implemented
node->expr_->accept(*this);
node->evalType = node->castTo_;
if (Type::isArithmetic(node->castTo_)
&& Type::isArithmetic(node->expr_->evalType)) {
// 算术类型互转,ok
} else if (Type::isPointer(node->castTo_)
&& Type::isPointer(node->expr_->evalType)) {
// 指针互转, ok
} else if (Type::isPointer(node->castTo_)
&& node->expr_->evalType->tag() == Type::Array) {
// 数组转指针,ok
} else if (Type::isInteger(node->castTo_)
&& Type::isPointer(node->expr_->evalType)) {
auto ty = static_cast<BuiltInType*>(node->castTo_.get());
if (ty->width() < PTRSIZE) {
throw Error(node->tok(),
"integer of such type is not big enough to hold a pointer");
}
} else if (Type::isPointer(node->castTo_) && Type::isInteger(node->expr_->evalType)) {
if (node->expr_->evalType->width() < PTRSIZE) {
errors_.add(Error(node->tok(), "warning: integer not wide enough to be a ptr"));
}
} else {
throw Error(node->tok(), "can't cast to such type");
}
}
void TypeChecker::visit(LiteralExpr* node)
{
auto literalType = node->tok_->type();
switch (literalType) {
case Token::IntLiteral:
node->evalType = BuiltInType::intType();
break;
case Token::UnsignedLiteral:
node->evalType = BuiltInType::uintType();
break;
case Token::CharLiteral:
node->evalType = BuiltInType::charType();
break;
case Token::DoubleLiteral:
node->evalType = BuiltInType::doubleType();
break;
case Token::StringLiteral:
node->evalType = PointerType::strLiteralType();
break;
default:
assert("other literals are not implemented" && false);
break;
}
}
void TypeChecker::visit(CompoundDef* node)
{
auto iter = node->type_->begin();
auto end = node->type_->end();
for (; iter != end; ++iter) {
checkVarDef(iter->second.type, node->tok());
}
}
void TypeChecker::visit(TypeAlias* node)
{
// TODO not implemented
}
void TypeChecker::visit(EnumDef* node)
{
// TODO not implemented
}
void TypeChecker::visit(FuncDef* node)
{
currFunc_ = node;
auto n = 1;
for (auto& param : node->params_) {
param.second.isParam = true;
param.second.seq = n++;
}
node->body_->accept(*this);
}
void TypeChecker::visit(ASTRoot* node)
{
scopeStack_.push(&node->scope());
for(auto& item : node->contents_) {
try {
item->accept(*this);
} catch (Error& err) {
errors_.add(err);
}
}
}
void TypeChecker::visit(SizeofExpr* node)
{
node->evalType = BuiltInType::maxUIntType();
}
void TypeChecker::visit(EmptyExpr* node)
{
}
void TypeChecker::checkArithmetic(Expr* parent,
const std::shared_ptr<Expr>& lhs, const std::shared_ptr<Expr>& rhs)
{
assert(Type::isArithmetic(lhs->evalType) && Type::isArithmetic(rhs->evalType));
auto lhsTy = static_cast<BuiltInType*>(lhs->evalType.get());
auto rhsTy = static_cast<BuiltInType*>(rhs->evalType.get());
if (lhsTy->isInteger() && rhsTy->isInteger()) {
bool isUnsigned = lhsTy->isUnsigned() || rhsTy->isUnsigned();
size_t width = std::max(lhsTy->width(), rhsTy->width());
auto bigger = std::make_shared<BuiltInType>(BuiltInType::Integer, isUnsigned, width, 0);
if (!bigger->equalUnqual(lhs->evalType)) {
lhs->promotedTo = bigger;
}
if (!bigger->equalUnqual(rhs->evalType)) {
rhs->promotedTo = bigger;
}
parent->evalType = bigger;
} else if (lhsTy->isInteger() && rhsTy->isFloating()) {
lhs->promotedTo = rhs->evalType;
parent->evalType = rhs->evalType;
} else if (lhsTy->isFloating() && rhsTy->isInteger()) {
rhs->promotedTo = lhs->evalType;
parent->evalType = lhs->evalType;
} else {
assert(lhsTy->isFloating() && rhsTy->isFloating());
if (lhsTy->width() > rhsTy->width()) {
rhs->promotedTo = lhs->evalType;
parent->evalType = lhs->evalType;
} else if (lhsTy->width() < rhsTy->width()) {
lhs->promotedTo = rhs->evalType;
parent->evalType = rhs->evalType;
}
}
}
void
TypeChecker::checkPointerArith(Expr* parent,
const std::shared_ptr<Expr>& pointer, const std::shared_ptr<Expr>& arith)
{
parent->evalType = pointer->evalType;
auto expanded = static_cast<BuiltInType*>(arith->evalType.get())->builtinClone();
expanded->width(PTRSIZE);
if (!arith->evalType->equalUnqual(expanded))
arith->promotedTo = std::move(expanded);
}
void TypeChecker::checkPointerCompare(Expr* parent,
const std::shared_ptr<Expr>& lhs, const std::shared_ptr<Expr>& rhs)
{
assert(lhs->evalType->tag() == Type::Pointer
&& rhs->evalType->tag() == Type::Pointer);
auto lhsTy = static_cast<PointerType*>(lhs->evalType.get());
auto rhsTy = static_cast<PointerType*>(rhs->evalType.get());
if (!Type::isVoid(lhsTy->base()) && !Type::isVoid(rhsTy->base())
&& !lhsTy->base()->equalUnqual(rhsTy->base())) {
throw Error(parent->tok(), "cannot compare between incompatible pointer types");
}
}
void TypeChecker::checkAsCond(const std::shared_ptr<Expr>& expr)
{
if (Type::isInteger(expr->evalType) || expr->evalType->tag() == Type::Pointer) {
// if (expr->evalType->width() < INTS)
expr->isCond = true;
/*auto prom = std::make_shared<BuiltInType>
(BuiltInType::Integer, true, PTRSIZE, 0);
if (!expr->evalType->equalUnqual(prom)) {
expr->promotedTo = std::move(prom);
}*/
} else {
throw Error(expr->tok(), "expression cannot be used in a boolean expression");
}
}
void TypeChecker::checkAssign(const std::shared_ptr<Expr>& from, const std::shared_ptr<Expr>& to, bool init)
{
if (!to->isLValue) {
throw Error(to->tok(), "can't assign to rvalue");
}
checkAssignE2T(from, to->evalType, init);
/*if (!init) {
if (to->evalType->isConst()) {
throw Error(to->tok(), "can't assign to const variable");
}
}
checkVoid(from->evalType, to->tok());
checkVoid(to->evalType, to->tok());
if (from->evalType->tag() == Type::Pointer && to->evalType->tag() == Type::Pointer) {
PointerType* fromTy = static_cast<PointerType*>(from->evalType.get());
PointerType* toTy = static_cast<PointerType*>(to->evalType.get());
if (Type::isVoid(fromTy->base())) {
// void* can be casted to any type of pointer
return;
}
// TODO 详细思考某些指针赋值,也许根本就大脚步呢。
if (fromTy->base()->equalUnqual(toTy->base())) {
if (fromTy->base()->isConst() && !toTy->base()->isConst()) {
throw Error(to->tok(), "can't assign between incompatible pointers");
}
} else {
throw Error(to->tok(), "can't assign between incompatible pointers");
}
} else if (from->evalType->tag() == Type::BuiltIn
&& to->evalType->tag() == Type::BuiltIn) {
// builtin but not void(checked before)
BuiltInType* fromTy = static_cast<BuiltInType*>(from->evalType.get());
BuiltInType* toTy = static_cast<BuiltInType*>(to->evalType.get());
if (fromTy->cat() == toTy->cat()) {
// 假设符号情况不同可以直接夏季把转型
if (fromTy->width() > toTy->width()) {
throw Error(to->tok(), "can't assign big type to small type (size)");
} else if (fromTy->width() < toTy->width()) {
from->promotedTo = to->evalType;
}
} else if (fromTy->isInteger() && toTy->isFloating()) {
from->promotedTo = to->evalType;
} else {
throw Error(to->tok(), "can't assign big type to small type (floating to integer)");
}
} else if (from->evalType->tag() == Type::UserDefined
&& to->evalType->tag() == Type::UserDefined) {
if (!from->evalType->equalUnqual(to->evalType)) {
throw Error(to->tok(), "can't assign between incompatible types");
}
} else if (from->evalType->tag() == Type::Array && to->evalType->tag() == Type::Pointer) {
auto fromTy = static_cast<ArrayType*>(from->evalType.get());
auto toTy = static_cast<PointerType*>(to->evalType.get());
if (fromTy->base()->equalUnqual(toTy->base())) {
if (!toTy->base()->isConst()) {
throw Error(to->tok(), "cannot assign array name to non-const pointer");
}
from->promotedTo = to->evalType;
} else {
throw Error(to->tok(), "cannot assign between incompatible array and pointer types");
}
} else {
throw Error(to->tok(), "cannot assign between incompatible types");
}*/
}
/** used by initialization */
void TypeChecker::checkAssignE2T(const std::shared_ptr<Expr>& from,
const std::shared_ptr<Type>& aimTy, bool init)
{
if (!init) {
if (aimTy->isConst()) {
throw Error(from->tok(), "can't assign to const variable");
}
}
checkVoid(from->evalType, from->tok());
checkVoid(aimTy, from->tok());
if (from->evalType->tag() == Type::Pointer && aimTy->tag() == Type::Pointer) {
PointerType* fromTy = static_cast<PointerType*>(from->evalType.get());
PointerType* toTy = static_cast<PointerType*>(aimTy.get());
if (Type::isVoid(fromTy->base())) {
// void* can be casted to any type of pointer
return;
}
// TODO 详细思考某些指针赋值,也许根本就大脚步呢。
if (fromTy->base()->equalUnqual(toTy->base())) {
if (fromTy->base()->isConst() && !toTy->base()->isConst()) {
throw Error(from->tok(), "can't assign between incompatible pointers");
}
} else {
throw Error(from->tok(), "can't assign between incompatible pointers");
}
} else if (from->evalType->tag() == Type::BuiltIn
&& aimTy->tag() == Type::BuiltIn) {
// builtin but not void(checked before)
BuiltInType* fromTy = static_cast<BuiltInType*>(from->evalType.get());
BuiltInType* toTy = static_cast<BuiltInType*>(aimTy.get());
if (fromTy->cat() == toTy->cat()) {
// 假设符号情况不同可以直接夏季把转型
if (fromTy->width() > toTy->width()) {
throw Error(from->tok(), "can't assign big type to small type (size)");
} else if (fromTy->width() < toTy->width()) {
from->promotedTo = aimTy;
}
} else if (fromTy->isInteger() && toTy->isFloating()) {
from->promotedTo = aimTy;
} else {
throw Error(from->tok(), "can't assign big type to small type (floating to integer)");
}
} else if (from->evalType->tag() == Type::UserDefined
&& aimTy->tag() == Type::UserDefined) {
if (!from->evalType->equalUnqual(aimTy)) {
throw Error(from->tok(), "can't assign between incompatible types");
}
} else if (from->evalType->tag() == Type::Array && aimTy->tag() == Type::Pointer) {
auto fromTy = static_cast<ArrayType*>(from->evalType.get());
auto toTy = static_cast<PointerType*>(aimTy.get());
if (fromTy->base()->equalUnqual(toTy->base())
&& (int)toTy->base()->isConst() >= (int)fromTy->base()->isConst()) {
// TODO this promotion may be not needed.
// from->promotedTo = aimTy;
} else {
throw Error(from->tok(), "cannot assign between incompatible array and pointer types");
}
} else if (Type::isInteger(from->evalType) && from->isLiteral() && aimTy->tag() == Type::Pointer) {
assert(from->tok()->type() == Token::IntLiteral);
int value = from->tok()->intLiteral();
if (value == 0) {
/// 0 is null, ok
} else {
throw Error(from->tok(), "can't assign non-zero integer to pointer");
}
} else {
throw Error(from->tok(), "cannot assign between incompatible types");
}
}
std::shared_ptr<Type>
TypeChecker::checkArrayRef(Token const* pos,
const std::shared_ptr<Type>& ty,
TypeChecker::VEiter curr, TypeChecker::VEiter end)
{
std::shared_ptr<Type> pty = ty;
while (curr != end) {
(*curr)->accept(*this);
if (!Type::isInteger((*curr)->evalType)) {
throw Error(pos, "array subscript should be integer");
}
(*curr)->promotedTo = BuiltInType::maxUIntType();
if (pty->tag() == Type::Array) {
pty = static_cast<ArrayType*>(pty.get())->base();
} else if (pty->tag() == Type::Pointer) {
pty = static_cast<PointerType*>(pty.get())->base();
} else {
// 还有维度,但是类型不是指针或者数组了。
throw Error(pos, "expecting array or pointer type");
}
++curr;
}
return pty;
/*if (curr != end) {
auto& currExpr = *curr;
currExpr->accept(*this);
if (!isInteger(currExpr->evalType)) {
throw Error(pos, "array subscript should be integer");
}
currExpr->promotedTo = Type::ULong;
if (ty->tag() == Type::Pointer) {
auto ptrType = static_cast<PointerType*>(ty.get());
return checkArrayRef(pos, ptrType->base(), curr + 1, end);
} else if (ty->tag() == Type::Array) {
auto arrType = static_cast<ArrayType*>(ty.get());
return checkArrayRef(pos, arrType->base(), curr + 1, end);
} else {
// 还有维度,但是类型不是指针或者数组了。
throw Error(pos, "expecting array or pointer type");
}
} else {
return ty;
}*/
}
void TypeChecker::checkVarDef(const std::shared_ptr<Type>& ty, Token const* pos)
{
if (Type::isVoid(ty)) {
throw Error(pos, "cannot declare a variable of type void");
} else if (ty->tag() == Type::UserDefined) {
auto realType = Type::derefIfIsUserDefinedType(ty);
if (realType->tag() == Type::Tag::Incomplete) {
throw Error(pos, "cannot declare a variable of incomplete type");
}
}
}
void TypeChecker::checkVoid(const std::shared_ptr<Type>& ty, Token const* pos)
{
if (Type::isVoid(ty)) {
throw Error(pos, "value of type void is not permitted");
}
}
void TypeChecker::makeInMemory(const std::shared_ptr<Expr>& expr)
{
assert(expr->isLValue);
switch (expr->tag()) {
case ExprTag::Var: {
auto varExpr = static_cast<VarExpr*>(expr.get());
auto ent = currScope().find(varExpr->name_);
if (!ent)
assert(false);
ent->ambiguous = true;
break;
}
case ExprTag::ArrayRef: {
auto arrRef = static_cast<ArrayRefExpr*>(expr.get());
if (arrRef->head_->tag() == ExprTag::Var) {
makeInMemory(arrRef->head_);
}
break;
}
case ExprTag::Member: {
auto memberExpr = static_cast<MemberExpr*>(expr.get());
makeInMemory(memberExpr->compound_);
break;
}
case ExprTag::Unary: {
auto unaryExpr = static_cast<UnaryOpExpr*>(expr.get());
if (unaryExpr->operator_ == Token::OperatorType::Mult) {
// should do nothing
} else
assert(false);
break;
}
default:
assert(false);
}
}