Repository navigation
Expand file tree
/
Copy pathSemanticAnalyzer.cpp
More file actions
806 lines (692 loc) · 28.5 KB
/
Copy pathSemanticAnalyzer.cpp
File metadata and controls
806 lines (692 loc) · 28.5 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
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
#include "SemanticAnalyzer.h"
#include <iostream>
#include <unordered_set>
SemanticAnalyzer::SemanticAnalyzer(DiagnosticEngine& diag)
: diag(diag) {
registerBuiltins();
}
void SemanticAnalyzer::registerBuiltins() {
auto voidType = Type::getVoid();
auto intType = Type::getInt();
auto charType = Type::getChar();
auto charPtrType = Type::getPointer(charType);
symbolTable.declareFunction("print_int", Type::getFunction(voidType, {intType}), nullptr);
symbolTable.declareFunction("print_char", Type::getFunction(voidType, {charType}), nullptr);
symbolTable.declareFunction("print_str", Type::getFunction(voidType, {charPtrType}), nullptr);
symbolTable.declareFunction("putchar", Type::getFunction(intType, {intType}), nullptr);
symbolTable.declareFunction("puts", Type::getFunction(intType, {charPtrType}), nullptr);
symbolTable.declareFunction("exit", Type::getFunction(voidType, {intType}), nullptr);
}
TypePtr SemanticAnalyzer::resolveType(TypePtr type, const SourceLocation& loc) {
if (!type) return Type::getInt();
if (type->isStruct()) {
auto st = std::static_pointer_cast<StructType>(type);
const Symbol* sym = symbolTable.lookupStruct(st->getName());
if (sym && sym->type) {
return sym->type;
}
return type;
}
if (type->isPointer()) {
auto pt = std::static_pointer_cast<PointerType>(type);
TypePtr resolvedPointee = resolveType(pt->getPointee(), loc);
return Type::getPointer(resolvedPointee);
}
if (type->isArray()) {
auto at = std::static_pointer_cast<ArrayType>(type);
TypePtr resolvedElem = resolveType(at->getElementType(), loc);
return Type::getArray(resolvedElem, at->getNumElements());
}
return type;
}
bool SemanticAnalyzer::checkTypeCompatibility(TypePtr target, TypePtr source, const SourceLocation& loc, const std::string& context) {
if (!target || !source) return false;
if (target->equals(*source)) return true;
// Both integers (char <-> int)
if (target->isInteger() && source->isInteger()) return true;
// Pointers
if (target->isPointer() && source->isPointer()) {
auto tPtr = std::static_pointer_cast<PointerType>(target);
auto sPtr = std::static_pointer_cast<PointerType>(source);
if (tPtr->getPointee()->isVoid() || sPtr->getPointee()->isVoid()) {
return true;
}
if (tPtr->getPointee()->equals(*sPtr->getPointee())) {
return true;
}
diag.warning(loc, "assignment from incompatible pointer type (" + source->toString() + " to " + target->toString() + ") in " + context);
return true;
}
// Pointer and integer 0 (NULL)
if (target->isPointer() && source->isInteger()) {
return true;
}
// Array decaying to pointer
if (target->isPointer() && source->isArray()) {
auto tPtr = std::static_pointer_cast<PointerType>(target);
auto sArr = std::static_pointer_cast<ArrayType>(source);
if (tPtr->getPointee()->equals(*sArr->getElementType()) || tPtr->getPointee()->isVoid()) {
return true;
}
}
diag.error(loc, "incompatible types: cannot convert '" + source->toString() + "' to '" + target->toString() + "' in " + context);
return false;
}
bool SemanticAnalyzer::isAssignableLValue(const Expr& expr) {
return expr.getIsLValue();
}
bool SemanticAnalyzer::analyze(Program& program) {
visit(program);
return !diag.hasErrors();
}
void SemanticAnalyzer::visit(Program& node) {
for (const auto& decl : node.getDeclarations()) {
decl->accept(*this);
}
}
void SemanticAnalyzer::visit(StructDecl& node) {
auto structType = std::static_pointer_cast<StructType>(node.getStructType());
std::vector<StructMember> resolvedMembers;
std::unordered_set<std::string> seenMembers;
for (const auto& member : structType->getMembers()) {
if (seenMembers.count(member.name)) {
diag.error(node.getLocation(), "duplicate member '" + member.name + "' in struct '" + node.getName() + "'");
}
seenMembers.insert(member.name);
TypePtr mType = resolveType(member.type, node.getLocation());
if (mType->isVoid()) {
diag.error(node.getLocation(), "member '" + member.name + "' declared void");
}
if (mType->isStruct()) {
auto st = std::static_pointer_cast<StructType>(mType);
if (!st->isComplete()) {
diag.error(node.getLocation(), "field '" + member.name + "' has incomplete type 'struct " + st->getName() + "'");
}
}
resolvedMembers.push_back({member.name, mType, 0});
}
structType->setMembers(resolvedMembers);
if (!symbolTable.declareStruct(node.getName(), structType, std::make_shared<StructDecl>(node))) {
diag.error(node.getLocation(), "redefinition of struct '" + node.getName() + "'");
}
}
void SemanticAnalyzer::visit(VarDecl& node) {
TypePtr type = resolveType(node.getType(), node.getLocation());
node.setType(type);
if (type->isVoid()) {
diag.error(node.getLocation(), "variable '" + node.getName() + "' declared void");
return;
}
if (type->isStruct()) {
auto st = std::static_pointer_cast<StructType>(type);
if (!st->isComplete()) {
diag.error(node.getLocation(), "variable '" + node.getName() + "' has incomplete type 'struct " + st->getName() + "'");
}
}
if (node.getInitExpr()) {
node.getInitExpr()->accept(*this);
checkTypeCompatibility(type, node.getInitExpr()->getEvalType(), node.getLocation(), "variable initialization");
if (node.getIsGlobal()) {
auto val = evaluateConstantExpr(node.getInitExpr());
if (!val.has_value()) {
diag.error(node.getLocation(), "initializer element is not a compile-time constant");
}
}
}
if (!symbolTable.declareVariable(node.getName(), type, std::make_shared<VarDecl>(node))) {
diag.error(node.getLocation(), "redeclaration of variable '" + node.getName() + "'");
}
}
void SemanticAnalyzer::visit(ParamDecl& node) {
TypePtr type = resolveType(node.getType(), node.getLocation());
if (type->isVoid()) {
diag.error(node.getLocation(), "parameter '" + node.getName() + "' declared void");
}
if (!symbolTable.declareVariable(node.getName(), type, nullptr)) {
diag.error(node.getLocation(), "duplicate parameter '" + node.getName() + "'");
}
}
void SemanticAnalyzer::visit(FunctionDecl& node) {
TypePtr retType = resolveType(node.getReturnType(), node.getLocation());
std::vector<TypePtr> paramTypes;
for (const auto& param : node.getParams()) {
TypePtr pType = resolveType(param->getType(), param->getLocation());
paramTypes.push_back(pType);
}
TypePtr funcType = Type::getFunction(retType, paramTypes);
if (!symbolTable.declareFunction(node.getName(), funcType, std::make_shared<FunctionDecl>(node))) {
diag.error(node.getLocation(), "redefinition of or conflicting types for function '" + node.getName() + "'");
}
if (node.getBody()) {
auto prevFn = symbolTable.getCurrentFunction();
symbolTable.setCurrentFunction(std::make_shared<FunctionDecl>(node));
symbolTable.enterScope(false, true);
for (const auto& param : node.getParams()) {
param->accept(*this);
}
// Visit body statements directly without double-entering scope for the outer compound
for (const auto& stmt : node.getBody()->getStatements()) {
stmt->accept(*this);
}
symbolTable.exitScope();
symbolTable.setCurrentFunction(prevFn);
}
}
void SemanticAnalyzer::visit(CompoundStmt& node) {
symbolTable.enterScope();
for (const auto& stmt : node.getStatements()) {
stmt->accept(*this);
}
symbolTable.exitScope();
}
void SemanticAnalyzer::visit(ExprStmt& node) {
if (node.getExpr()) {
node.getExpr()->accept(*this);
}
}
void SemanticAnalyzer::visit(IfStmt& node) {
node.getCond()->accept(*this);
if (!node.getCond()->getEvalType() || (!node.getCond()->getEvalType()->isScalar() && !node.getCond()->getEvalType()->isArray())) {
diag.error(node.getCond()->getLocation(), "condition in 'if' statement must be scalar");
}
node.getThenBranch()->accept(*this);
if (node.getElseBranch()) {
node.getElseBranch()->accept(*this);
}
}
void SemanticAnalyzer::visit(WhileStmt& node) {
node.getCond()->accept(*this);
if (!node.getCond()->getEvalType() || (!node.getCond()->getEvalType()->isScalar() && !node.getCond()->getEvalType()->isArray())) {
diag.error(node.getCond()->getLocation(), "condition in 'while' statement must be scalar");
}
symbolTable.enterScope(/*isLoop=*/true);
node.getBody()->accept(*this);
symbolTable.exitScope();
}
void SemanticAnalyzer::visit(DoWhileStmt& node) {
symbolTable.enterScope(/*isLoop=*/true);
node.getBody()->accept(*this);
symbolTable.exitScope();
node.getCond()->accept(*this);
if (!node.getCond()->getEvalType() || (!node.getCond()->getEvalType()->isScalar() && !node.getCond()->getEvalType()->isArray())) {
diag.error(node.getCond()->getLocation(), "condition in 'do-while' statement must be scalar");
}
}
void SemanticAnalyzer::visit(ForStmt& node) {
symbolTable.enterScope(/*isLoop=*/false);
if (node.getInit()) {
node.getInit()->accept(*this);
}
if (node.getCond()) {
node.getCond()->accept(*this);
if (!node.getCond()->getEvalType() || (!node.getCond()->getEvalType()->isScalar() && !node.getCond()->getEvalType()->isArray())) {
diag.error(node.getCond()->getLocation(), "condition in 'for' statement must be scalar");
}
}
if (node.getStep()) {
node.getStep()->accept(*this);
}
symbolTable.enterScope(/*isLoop=*/true);
node.getBody()->accept(*this);
symbolTable.exitScope();
symbolTable.exitScope();
}
void SemanticAnalyzer::visit(ReturnStmt& node) {
auto curFn = symbolTable.getCurrentFunction();
if (!curFn) {
diag.error(node.getLocation(), "'return' statement outside function");
return;
}
TypePtr retType = curFn->getReturnType();
if (node.getValue()) {
node.getValue()->accept(*this);
if (retType->isVoid()) {
diag.error(node.getLocation(), "void function cannot return a value");
} else {
checkTypeCompatibility(retType, node.getValue()->getEvalType(), node.getLocation(), "return statement");
}
} else {
if (!retType->isVoid()) {
diag.warning(node.getLocation(), "non-void function should return a value");
}
}
}
void SemanticAnalyzer::visit(BreakStmt& node) {
if (!symbolTable.isInLoop()) {
diag.error(node.getLocation(), "'break' statement not within loop");
}
}
void SemanticAnalyzer::visit(ContinueStmt& node) {
if (!symbolTable.isInLoop()) {
diag.error(node.getLocation(), "'continue' statement not within loop");
}
}
void SemanticAnalyzer::visit(DeclStmt& node) {
for (const auto& decl : node.getDeclarations()) {
decl->accept(*this);
}
}
void SemanticAnalyzer::visit(IntegerLiteralExpr& node) {
node.setEvalType(Type::getInt());
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(CharLiteralExpr& node) {
node.setEvalType(Type::getChar());
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(StringLiteralExpr& node) {
node.setEvalType(Type::getPointer(Type::getChar()));
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(VarExpr& node) {
const Symbol* sym = symbolTable.lookupVariable(node.getName());
if (sym && sym->kind == Symbol::Kind::Variable) {
node.setEvalType(sym->type);
node.setIsLValue(!sym->type->isArray());
node.setDecl(sym->varDecl);
return;
}
const Symbol* fnSym = symbolTable.lookupFunction(node.getName());
if (fnSym && fnSym->kind == Symbol::Kind::Function) {
node.setEvalType(fnSym->type);
node.setIsLValue(false);
return;
}
diag.error(node.getLocation(), "use of undeclared identifier '" + node.getName() + "'");
node.setEvalType(Type::getInt());
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(BinaryExpr& node) {
node.getLeft()->accept(*this);
node.getRight()->accept(*this);
node.setIsLValue(false);
TypePtr lType = node.getLeft()->getEvalType();
TypePtr rType = node.getRight()->getEvalType();
if (!lType || !rType) {
node.setEvalType(Type::getInt());
return;
}
// Array decay to pointer in binary expr
if (lType->isArray()) {
lType = Type::getPointer(std::static_pointer_cast<ArrayType>(lType)->getElementType());
}
if (rType->isArray()) {
rType = Type::getPointer(std::static_pointer_cast<ArrayType>(rType)->getElementType());
}
BinaryOp op = node.getOp();
// Addition
if (op == BinaryOp::Add) {
if (lType->isInteger() && rType->isInteger()) {
node.setEvalType(Type::getInt());
return;
}
if (lType->isPointer() && rType->isInteger()) {
node.setEvalType(lType);
return;
}
if (lType->isInteger() && rType->isPointer()) {
node.setEvalType(rType);
return;
}
diag.error(node.getLocation(), "invalid operands to binary '+' ('" + lType->toString() + "' and '" + rType->toString() + "')");
node.setEvalType(Type::getInt());
return;
}
// Subtraction
if (op == BinaryOp::Sub) {
if (lType->isInteger() && rType->isInteger()) {
node.setEvalType(Type::getInt());
return;
}
if (lType->isPointer() && rType->isInteger()) {
node.setEvalType(lType);
return;
}
if (lType->isPointer() && rType->isPointer()) {
node.setEvalType(Type::getInt()); // Pointer distance
return;
}
diag.error(node.getLocation(), "invalid operands to binary '-' ('" + lType->toString() + "' and '" + rType->toString() + "')");
node.setEvalType(Type::getInt());
return;
}
// Multiplication, Division, Modulo
if (op == BinaryOp::Mul || op == BinaryOp::Div || op == BinaryOp::Mod) {
if (!lType->isInteger() || !rType->isInteger()) {
diag.error(node.getLocation(), "operands to arithmetic operator must be integer");
}
node.setEvalType(Type::getInt());
return;
}
// Bitwise operators
if (op == BinaryOp::BitAnd || op == BinaryOp::BitOr || op == BinaryOp::BitXor ||
op == BinaryOp::ShiftLeft || op == BinaryOp::ShiftRight) {
if (!lType->isInteger() || !rType->isInteger()) {
diag.error(node.getLocation(), "operands to bitwise operator must be integer");
}
node.setEvalType(Type::getInt());
return;
}
// Relational and Equality
if (op == BinaryOp::Equal || op == BinaryOp::NotEqual ||
op == BinaryOp::Less || op == BinaryOp::LessEqual ||
op == BinaryOp::Greater || op == BinaryOp::GreaterEqual) {
if (!lType->isScalar() || !rType->isScalar()) {
diag.error(node.getLocation(), "operands to comparison operator must be scalar");
}
node.setEvalType(Type::getInt());
return;
}
// Logical And/Or
if (op == BinaryOp::LogicalAnd || op == BinaryOp::LogicalOr) {
if (!lType->isScalar() || !rType->isScalar()) {
diag.error(node.getLocation(), "operands to logical operator must be scalar");
}
node.setEvalType(Type::getInt());
return;
}
node.setEvalType(Type::getInt());
}
void SemanticAnalyzer::visit(UnaryExpr& node) {
node.getOperand()->accept(*this);
TypePtr opType = node.getOperand()->getEvalType();
if (!opType) {
node.setEvalType(Type::getInt());
node.setIsLValue(false);
return;
}
UnaryOp op = node.getOp();
switch (op) {
case UnaryOp::Plus:
case UnaryOp::Minus:
case UnaryOp::BitNot:
if (!opType->isInteger()) {
diag.error(node.getLocation(), "operand to unary arithmetic/bitwise operator must be integer");
}
node.setEvalType(Type::getInt());
node.setIsLValue(false);
break;
case UnaryOp::LogicalNot:
if (!opType->isScalar() && !opType->isArray()) {
diag.error(node.getLocation(), "operand to '!' must be scalar");
}
node.setEvalType(Type::getInt());
node.setIsLValue(false);
break;
case UnaryOp::PreInc:
case UnaryOp::PreDec:
case UnaryOp::PostInc:
case UnaryOp::PostDec:
if (!node.getOperand()->getIsLValue()) {
diag.error(node.getLocation(), "lvalue required as increment/decrement operand");
}
if (!opType->isScalar()) {
diag.error(node.getLocation(), "cannot increment/decrement non-scalar type");
}
node.setEvalType(opType);
node.setIsLValue(op == UnaryOp::PreInc || op == UnaryOp::PreDec);
break;
case UnaryOp::AddrOf:
if (!node.getOperand()->getIsLValue() && !opType->isArray()) {
diag.error(node.getLocation(), "cannot take address of non-lvalue");
}
node.setEvalType(Type::getPointer(opType));
node.setIsLValue(false);
break;
case UnaryOp::Deref: {
TypePtr effective = opType;
if (effective->isArray()) {
effective = Type::getPointer(std::static_pointer_cast<ArrayType>(effective)->getElementType());
}
if (!effective->isPointer()) {
diag.error(node.getLocation(), "cannot dereference non-pointer type '" + opType->toString() + "'");
node.setEvalType(Type::getInt());
node.setIsLValue(false);
} else {
auto pt = std::static_pointer_cast<PointerType>(effective);
if (pt->getPointee()->isVoid()) {
diag.error(node.getLocation(), "cannot dereference 'void*' pointer");
}
node.setEvalType(pt->getPointee());
node.setIsLValue(true);
}
break;
}
}
}
void SemanticAnalyzer::visit(AssignExpr& node) {
node.getLhs()->accept(*this);
node.getRhs()->accept(*this);
TypePtr lType = node.getLhs()->getEvalType();
TypePtr rType = node.getRhs()->getEvalType();
if (!node.getLhs()->getIsLValue()) {
diag.error(node.getLocation(), "lvalue required as left operand of assignment");
}
if (lType && rType) {
if (lType->isPointer()) {
if (node.getOp() == AssignOp::Assign) {
checkTypeCompatibility(lType, rType, node.getLocation(), "assignment");
} else if (node.getOp() == AssignOp::AddAssign || node.getOp() == AssignOp::SubAssign) {
if (!rType->isInteger()) {
diag.error(node.getLocation(), "cannot add or subtract non-integer from pointer");
}
} else {
diag.error(node.getLocation(), "invalid operands to binary expression ('" + lType->toString() + "' and '" + rType->toString() + "')");
}
} else {
checkTypeCompatibility(lType, rType, node.getLocation(), "assignment");
}
}
node.setEvalType(lType ? lType : Type::getInt());
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(CallExpr& node) {
for (const auto& arg : node.getArgs()) {
arg->accept(*this);
}
const Symbol* sym = symbolTable.lookupFunction(node.getCalleeName());
if (!sym || sym->kind != Symbol::Kind::Function) {
diag.error(node.getLocation(), "call to undeclared function '" + node.getCalleeName() + "'");
node.setEvalType(Type::getInt());
node.setIsLValue(false);
return;
}
auto fnType = std::static_pointer_cast<FunctionType>(sym->type);
node.setEvalType(fnType->getReturnType());
node.setIsLValue(false);
node.setDecl(sym->funcDecl);
const auto& params = fnType->getParamTypes();
if (node.getArgs().size() != params.size()) {
diag.error(node.getLocation(), "function '" + node.getCalleeName() + "' expects " +
std::to_string(params.size()) + " arguments, but got " + std::to_string(node.getArgs().size()));
return;
}
for (size_t i = 0; i < params.size(); ++i) {
checkTypeCompatibility(params[i], node.getArgs()[i]->getEvalType(), node.getArgs()[i]->getLocation(), "function argument");
}
}
void SemanticAnalyzer::visit(SubscriptExpr& node) {
node.getBase()->accept(*this);
node.getIndex()->accept(*this);
TypePtr baseType = node.getBase()->getEvalType();
TypePtr indexType = node.getIndex()->getEvalType();
if (!baseType || !indexType) {
node.setEvalType(Type::getInt());
node.setIsLValue(false);
return;
}
if (!indexType->isInteger()) {
diag.error(node.getIndex()->getLocation(), "array subscript is not an integer");
}
if (baseType->isArray()) {
auto at = std::static_pointer_cast<ArrayType>(baseType);
node.setEvalType(at->getElementType());
node.setIsLValue(true);
} else if (baseType->isPointer()) {
auto pt = std::static_pointer_cast<PointerType>(baseType);
node.setEvalType(pt->getPointee());
node.setIsLValue(true);
} else {
diag.error(node.getLocation(), "subscripted value is not an array or pointer ('" + baseType->toString() + "')");
node.setEvalType(Type::getInt());
node.setIsLValue(false);
}
}
void SemanticAnalyzer::visit(MemberExpr& node) {
node.getBase()->accept(*this);
TypePtr baseType = node.getBase()->getEvalType();
if (!baseType) {
node.setEvalType(Type::getInt());
node.setIsLValue(false);
return;
}
std::shared_ptr<StructType> st = nullptr;
if (node.getIsArrow()) {
if (!baseType->isPointer()) {
diag.error(node.getLocation(), "base of '->' must be a pointer to struct");
node.setEvalType(Type::getInt());
return;
}
auto pt = std::static_pointer_cast<PointerType>(baseType);
if (!pt->getPointee()->isStruct()) {
diag.error(node.getLocation(), "base of '->' does not point to a struct");
node.setEvalType(Type::getInt());
return;
}
st = std::static_pointer_cast<StructType>(resolveType(pt->getPointee(), node.getLocation()));
} else {
if (!baseType->isStruct()) {
diag.error(node.getLocation(), "base of '.' must be a struct");
node.setEvalType(Type::getInt());
return;
}
st = std::static_pointer_cast<StructType>(resolveType(baseType, node.getLocation()));
}
const StructMember* member = st->findMember(node.getMemberName());
if (!member) {
diag.error(node.getLocation(), "struct '" + st->getName() + "' has no member named '" + node.getMemberName() + "'");
node.setEvalType(Type::getInt());
node.setIsLValue(false);
return;
}
node.setMemberOffset(member->offset);
node.setEvalType(member->type);
node.setIsLValue(true);
}
void SemanticAnalyzer::visit(CastExpr& node) {
node.getOperand()->accept(*this);
TypePtr target = resolveType(node.getTargetType(), node.getLocation());
if (!target->isScalar() && !target->isVoid()) {
diag.error(node.getLocation(), "cannot cast to non-scalar type '" + target->toString() + "'");
}
node.setEvalType(target);
node.setIsLValue(false);
}
void SemanticAnalyzer::visit(SizeofExpr& node) {
node.setEvalType(Type::getInt());
node.setIsLValue(false);
if (node.getQueriedType()) {
TypePtr t = resolveType(node.getQueriedType(), node.getLocation());
if (t->isVoid()) {
diag.error(node.getLocation(), "invalid application of 'sizeof' to void type");
}
node.setQueriedType(t);
} else if (node.getQueriedExpr()) {
node.getQueriedExpr()->accept(*this);
}
}
void SemanticAnalyzer::visit(ConditionalExpr& node) {
node.getCond()->accept(*this);
if (!node.getCond()->getEvalType() || (!node.getCond()->getEvalType()->isScalar() && !node.getCond()->getEvalType()->isArray())) {
diag.error(node.getCond()->getLocation(), "condition in '?:' must be scalar");
}
node.getTrueExpr()->accept(*this);
node.getFalseExpr()->accept(*this);
node.setIsLValue(false);
TypePtr tType = node.getTrueExpr()->getEvalType();
TypePtr fType = node.getFalseExpr()->getEvalType();
if (!tType || !fType) {
node.setEvalType(Type::getInt());
return;
}
if (tType->isPointer() || fType->isPointer()) {
if (tType->isPointer()) node.setEvalType(tType);
else node.setEvalType(fType);
} else {
node.setEvalType(Type::getInt());
}
}
std::optional<int64_t> SemanticAnalyzer::evaluateConstantExpr(const ExprPtr& expr) {
if (!expr) return std::nullopt;
if (expr->getKind() == ASTKind::IntegerLiteralExpr) {
return std::static_pointer_cast<IntegerLiteralExpr>(expr)->getValue();
}
if (expr->getKind() == ASTKind::CharLiteralExpr) {
return static_cast<int64_t>(std::static_pointer_cast<CharLiteralExpr>(expr)->getValue());
}
if (expr->getKind() == ASTKind::SizeofExpr) {
auto se = std::static_pointer_cast<SizeofExpr>(expr);
if (se->getQueriedType()) {
TypePtr t = resolveType(se->getQueriedType(), se->getLocation());
return static_cast<int64_t>(t->getSize());
} else if (se->getQueriedExpr()) {
TypePtr t = se->getQueriedExpr()->getEvalType();
if (t) return static_cast<int64_t>(t->getSize());
}
return std::nullopt;
}
if (expr->getKind() == ASTKind::CastExpr) {
auto ce = std::static_pointer_cast<CastExpr>(expr);
auto val = evaluateConstantExpr(ce->getOperand());
if (!val) return std::nullopt;
TypePtr target = resolveType(ce->getTargetType(), ce->getLocation());
if (target->isChar()) return static_cast<int64_t>(static_cast<int8_t>(*val));
if (target->isInt()) return static_cast<int64_t>(static_cast<int32_t>(*val));
return *val;
}
if (expr->getKind() == ASTKind::UnaryExpr) {
auto ue = std::static_pointer_cast<UnaryExpr>(expr);
auto val = evaluateConstantExpr(ue->getOperand());
if (!val) return std::nullopt;
switch (ue->getOp()) {
case UnaryOp::Plus: return *val;
case UnaryOp::Minus: return -(*val);
case UnaryOp::BitNot: return ~(*val);
case UnaryOp::LogicalNot: return (*val == 0 ? 1 : 0);
default: return std::nullopt;
}
}
if (expr->getKind() == ASTKind::BinaryExpr) {
auto be = std::static_pointer_cast<BinaryExpr>(expr);
auto l = evaluateConstantExpr(be->getLeft());
auto r = evaluateConstantExpr(be->getRight());
if (!l || !r) return std::nullopt;
switch (be->getOp()) {
case BinaryOp::Add: return *l + *r;
case BinaryOp::Sub: return *l - *r;
case BinaryOp::Mul: return *l * *r;
case BinaryOp::Div: return *r != 0 ? *l / *r : 0;
case BinaryOp::Mod: return *r != 0 ? *l % *r : 0;
case BinaryOp::BitAnd: return *l & *r;
case BinaryOp::BitOr: return *l | *r;
case BinaryOp::BitXor: return *l ^ *r;
case BinaryOp::ShiftLeft: return *l << *r;
case BinaryOp::ShiftRight: return *l >> *r;
case BinaryOp::Equal: return *l == *r ? 1 : 0;
case BinaryOp::NotEqual: return *l != *r ? 1 : 0;
case BinaryOp::Less: return *l < *r ? 1 : 0;
case BinaryOp::LessEqual: return *l <= *r ? 1 : 0;
case BinaryOp::Greater: return *l > *r ? 1 : 0;
case BinaryOp::GreaterEqual: return *l >= *r ? 1 : 0;
case BinaryOp::LogicalAnd: return (*l != 0 && *r != 0) ? 1 : 0;
case BinaryOp::LogicalOr: return (*l != 0 || *r != 0) ? 1 : 0;
}
}
if (expr->getKind() == ASTKind::ConditionalExpr) {
auto ce = std::static_pointer_cast<ConditionalExpr>(expr);
auto c = evaluateConstantExpr(ce->getCond());
if (!c) return std::nullopt;
return (*c != 0) ? evaluateConstantExpr(ce->getTrueExpr()) : evaluateConstantExpr(ce->getFalseExpr());
}
return std::nullopt;
}