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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
|
//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for expressions.
//
//===----------------------------------------------------------------------===//
#include "Sema.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
using namespace clang;
/// ParseStringLiteral - The specified tokens were lexed as pasted string
/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
/// multiple tokens. However, the common case is that StringToks points to one
/// string.
///
Action::ExprResult
Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) {
assert(NumStringToks && "Must have at least one string!");
StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
if (Literal.hadError)
return ExprResult(true);
llvm::SmallVector<SourceLocation, 4> StringTokLocs;
for (unsigned i = 0; i != NumStringToks; ++i)
StringTokLocs.push_back(StringToks[i].getLocation());
// FIXME: handle wchar_t
QualType t = Context.getPointerType(Context.CharTy);
// Pass &StringTokLocs[0], StringTokLocs.size() to factory!
return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Literal.AnyWide, t, StringToks[0].getLocation(),
StringToks[NumStringToks-1].getLocation());
}
/// ParseIdentifierExpr - The parser read an identifier in expression context,
/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
/// identifier is used in an function call context.
Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
IdentifierInfo &II,
bool HasTrailingLParen) {
// Could be enum-constant or decl.
Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
if (D == 0) {
// Otherwise, this could be an implicitly declared function reference (legal
// in C90, extension in C99).
if (HasTrailingLParen &&
// Not in C++.
!getLangOptions().CPlusPlus)
D = ImplicitlyDefineFunction(Loc, II, S);
else {
// If this name wasn't predeclared and if this is not a function call,
// diagnose the problem.
return Diag(Loc, diag::err_undeclared_var_use, II.getName());
}
}
if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
return new DeclRefExpr(VD, VD->getType(), Loc);
if (isa<TypedefDecl>(D))
return Diag(Loc, diag::err_unexpected_typedef, II.getName());
assert(0 && "Invalid decl");
abort();
}
Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc,
tok::TokenKind Kind) {
PreDefinedExpr::IdentType IT;
switch (Kind) {
default:
assert(0 && "Unknown simple primary expr!");
case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
IT = PreDefinedExpr::Func;
break;
case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
IT = PreDefinedExpr::Function;
break;
case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
IT = PreDefinedExpr::PrettyFunction;
break;
}
// Pre-defined identifiers are always of type char *.
return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
}
Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
llvm::SmallString<16> CharBuffer;
CharBuffer.resize(Tok.getLength());
const char *ThisTokBegin = &CharBuffer[0];
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Tok.getLocation(), PP);
if (Literal.hadError())
return ExprResult(true);
return new CharacterLiteral(Literal.getValue(), Context.IntTy,
Tok.getLocation());
}
Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
// fast path for a single digit (which is quite common). A single digit
// cannot have a trigraph, escaped newline, radix prefix, or type suffix.
if (Tok.getLength() == 1) {
const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
Context.IntTy,
Tok.getLocation()));
}
llvm::SmallString<512> IntegerBuffer;
IntegerBuffer.resize(Tok.getLength());
const char *ThisTokBegin = &IntegerBuffer[0];
// Get the spelling of the token, which eliminates trigraphs, etc.
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Tok.getLocation(), PP);
if (Literal.hadError)
return ExprResult(true);
Expr *Res;
if (Literal.isFloatingLiteral()) {
// FIXME: handle float values > 32 (including compute the real type...).
QualType Ty = Literal.isFloat ? Context.FloatTy : Context.DoubleTy;
Res = new FloatingLiteral(Literal.GetFloatValue(), Ty, Tok.getLocation());
} else if (!Literal.isIntegerLiteral()) {
return ExprResult(true);
} else {
QualType t;
// Get the value in the widest-possible width.
llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
if (Literal.GetIntegerValue(ResultVal)) {
// If this value didn't fit into uintmax_t, warn and force to ull.
Diag(Tok.getLocation(), diag::warn_integer_too_large);
t = Context.UnsignedLongLongTy;
assert(Context.getTypeSize(t, Tok.getLocation()) ==
ResultVal.getBitWidth() && "long long is not intmax_t?");
} else {
// If this value fits into a ULL, try to figure out what else it fits into
// according to the rules of C99 6.4.4.1p5.
// Octal, Hexadecimal, and integers with a U suffix are allowed to
// be an unsigned int.
bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
// Check from smallest to largest, picking the smallest type we can.
if (!Literal.isLong && !Literal.isLongLong) {
// Are int/unsigned possibilities?
unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
// Does it fit in a unsigned int?
if (ResultVal.isIntN(IntSize)) {
// Does it fit in a signed int?
if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
t = Context.IntTy;
else if (AllowUnsigned)
t = Context.UnsignedIntTy;
}
if (!t.isNull())
ResultVal.trunc(IntSize);
}
// Are long/unsigned long possibilities?
if (t.isNull() && !Literal.isLongLong) {
unsigned LongSize = Context.getTypeSize(Context.LongTy,
Tok.getLocation());
// Does it fit in a unsigned long?
if (ResultVal.isIntN(LongSize)) {
// Does it fit in a signed long?
if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
t = Context.LongTy;
else if (AllowUnsigned)
t = Context.UnsignedLongTy;
}
if (!t.isNull())
ResultVal.trunc(LongSize);
}
// Finally, check long long if needed.
if (t.isNull()) {
unsigned LongLongSize =
Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
// Does it fit in a unsigned long long?
if (ResultVal.isIntN(LongLongSize)) {
// Does it fit in a signed long long?
if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
t = Context.LongLongTy;
else if (AllowUnsigned)
t = Context.UnsignedLongLongTy;
}
}
// If we still couldn't decide a type, we probably have something that
// does not fit in a signed long long, but has no U suffix.
if (t.isNull()) {
Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
t = Context.UnsignedLongLongTy;
}
}
Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
}
// If this is an imaginary literal, create the ImaginaryLiteral wrapper.
if (Literal.isImaginary)
Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
return Res;
}
Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
ExprTy *Val) {
Expr *e = (Expr *)Val;
assert((e != 0) && "ParseParenExpr() missing expr");
return new ParenExpr(L, R, e);
}
/// The UsualUnaryConversions() function is *not* called by this routine.
/// See C99 6.3.2.1p[2-4] for more details.
QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
SourceLocation OpLoc, bool isSizeof) {
// C99 6.5.3.4p1:
if (isa<FunctionType>(exprType) && isSizeof)
// alignof(function) is allowed.
Diag(OpLoc, diag::ext_sizeof_function_type);
else if (exprType->isVoidType())
Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
else if (exprType->isIncompleteType()) {
Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
diag::err_alignof_incomplete_type,
exprType.getAsString());
return QualType(); // error
}
// C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
return Context.getSizeType();
}
Action::ExprResult Sema::
ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
SourceLocation LPLoc, TypeTy *Ty,
SourceLocation RPLoc) {
// If error parsing type, ignore.
if (Ty == 0) return true;
// Verify that this is a valid expression.
QualType ArgTy = QualType::getFromOpaquePtr(Ty);
QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
if (resultType.isNull())
return true;
return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
}
QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
DefaultFunctionArrayConversion(V);
// These operators return the element type of a complex type.
if (const ComplexType *CT = V->getType()->getAsComplexType())
return CT->getElementType();
// Otherwise they pass through real integer and floating point types here.
if (V->getType()->isArithmeticType())
return V->getType();
// Reject anything else.
Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
return QualType();
}
Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
tok::TokenKind Kind,
ExprTy *Input) {
UnaryOperator::Opcode Opc;
switch (Kind) {
default: assert(0 && "Unknown unary op!");
case tok::plusplus: Opc = UnaryOperator::PostInc; break;
case tok::minusminus: Opc = UnaryOperator::PostDec; break;
}
QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
if (result.isNull())
return true;
return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
}
Action::ExprResult Sema::
ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
ExprTy *Idx, SourceLocation RLoc) {
Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
// Perform default conversions.
DefaultFunctionArrayConversion(LHSExp);
DefaultFunctionArrayConversion(RHSExp);
QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
// C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
// to the expression *((e1)+(e2)). This means the array "Base" may actually be
// in the subscript position. As a result, we need to derive the array base
// and index from the expression types.
Expr *BaseExpr, *IndexExpr;
QualType ResultType;
if (const PointerType *PTy = LHSTy->getAsPointerType()) {
BaseExpr = LHSExp;
IndexExpr = RHSExp;
// FIXME: need to deal with const...
ResultType = PTy->getPointeeType();
} else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
// Handle the uncommon case of "123[Ptr]".
BaseExpr = RHSExp;
IndexExpr = LHSExp;
// FIXME: need to deal with const...
ResultType = PTy->getPointeeType();
} else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
BaseExpr = LHSExp; // vectors: V[123]
IndexExpr = RHSExp;
// Component access limited to variables (reject vec4.rg[1]).
if (!isa<DeclRefExpr>(BaseExpr))
return Diag(LLoc, diag::err_ocuvector_component_access,
SourceRange(LLoc, RLoc));
// FIXME: need to deal with const...
ResultType = VTy->getElementType();
} else {
return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
RHSExp->getSourceRange());
}
// C99 6.5.2.1p1
if (!IndexExpr->getType()->isIntegerType())
return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
IndexExpr->getSourceRange());
// C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
// the following check catches trying to index a pointer to a function (e.g.
// void (*)(int)). Functions are not objects in C99.
if (!ResultType->isObjectType())
return Diag(BaseExpr->getLocStart(),
diag::err_typecheck_subscript_not_object,
BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
}
QualType Sema::
CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
IdentifierInfo &CompName, SourceLocation CompLoc) {
const OCUVectorType *vecType = baseType->getAsOCUVectorType();
// The vector accessor can't exceed the number of elements.
const char *compStr = CompName.getName();
if (strlen(compStr) > vecType->getNumElements()) {
Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
baseType.getAsString(), SourceRange(CompLoc));
return QualType();
}
// The component names must come from the same set.
if (vecType->getPointAccessorIdx(*compStr) != -1) {
do
compStr++;
while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
} else if (vecType->getColorAccessorIdx(*compStr) != -1) {
do
compStr++;
while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
} else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
do
compStr++;
while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
}
if (*compStr) {
// We didn't get to the end of the string. This means the component names
// didn't come from the same set *or* we encountered an illegal name.
Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
std::string(compStr,compStr+1), SourceRange(CompLoc));
return QualType();
}
// Each component accessor can't exceed the vector type.
compStr = CompName.getName();
while (*compStr) {
if (vecType->isAccessorWithinNumElements(*compStr))
compStr++;
else
break;
}
if (*compStr) {
// We didn't get to the end of the string. This means a component accessor
// exceeds the number of elements in the vector.
Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
baseType.getAsString(), SourceRange(CompLoc));
return QualType();
}
// The component accessor looks fine - now we need to compute the actual type.
// The vector type is implied by the component accessor. For example,
// vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
unsigned CompSize = strlen(CompName.getName());
if (CompSize == 1)
return vecType->getElementType();
QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
// Now look up the TypeDefDecl from the vector type. Without this,
// diagostics look bad. We want OCU vector types to appear built-in.
for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
if (OCUVectorDecls[i]->getUnderlyingType() == VT)
return Context.getTypedefType(OCUVectorDecls[i]);
}
return VT; // should never get here (a typedef type should always be found).
}
Action::ExprResult Sema::
ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
tok::TokenKind OpKind, SourceLocation MemberLoc,
IdentifierInfo &Member) {
Expr *BaseExpr = static_cast<Expr *>(Base);
assert(BaseExpr && "no record expression");
QualType BaseType = BaseExpr->getType();
assert(!BaseType.isNull() && "no type for member expression");
if (OpKind == tok::arrow) {
if (const PointerType *PT = BaseType->getAsPointerType())
BaseType = PT->getPointeeType();
else
return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
SourceRange(MemberLoc));
}
// The base type is either a record or an OCUVectorType.
if (const RecordType *RTy = BaseType->getAsRecordType()) {
RecordDecl *RDecl = RTy->getDecl();
if (RTy->isIncompleteType())
return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
BaseExpr->getSourceRange());
// The record definition is complete, now make sure the member is valid.
FieldDecl *MemberDecl = RDecl->getMember(&Member);
if (!MemberDecl)
return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
SourceRange(MemberLoc));
return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
} else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
// Component access limited to variables (reject vec4.rg.g).
if (!isa<DeclRefExpr>(BaseExpr))
return Diag(OpLoc, diag::err_ocuvector_component_access,
SourceRange(MemberLoc));
QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
if (ret.isNull())
return true;
return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
} else
return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
SourceRange(MemberLoc));
}
/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
/// This provides the location of the left/right parens and a list of comma
/// locations.
Action::ExprResult Sema::
ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
ExprTy **args, unsigned NumArgsInCall,
SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Expr *Fn = static_cast<Expr *>(fn);
Expr **Args = reinterpret_cast<Expr**>(args);
assert(Fn && "no function call expression");
UsualUnaryConversions(Fn);
QualType funcType = Fn->getType();
// C99 6.5.2.2p1 - "The expression that denotes the called function shall have
// type pointer to function".
const PointerType *PT = funcType->getAsPointerType();
if (PT == 0)
return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
SourceRange(Fn->getLocStart(), RParenLoc));
const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
if (funcT == 0)
return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
SourceRange(Fn->getLocStart(), RParenLoc));
// If a prototype isn't declared, the parser implicitly defines a func decl
QualType resultType = funcT->getResultType();
if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
// C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
// assignment, to the types of the corresponding parameter, ...
unsigned NumArgsInProto = proto->getNumArgs();
unsigned NumArgsToCheck = NumArgsInCall;
if (NumArgsInCall < NumArgsInProto)
Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Fn->getSourceRange());
else if (NumArgsInCall > NumArgsInProto) {
if (!proto->isVariadic()) {
Diag(Args[NumArgsInProto]->getLocStart(),
diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
SourceRange(Args[NumArgsInProto]->getLocStart(),
Args[NumArgsInCall-1]->getLocEnd()));
}
NumArgsToCheck = NumArgsInProto;
}
// Continue to check argument types (even if we have too few/many args).
for (unsigned i = 0; i < NumArgsToCheck; i++) {
Expr *argExpr = Args[i];
assert(argExpr && "ParseCallExpr(): missing argument expression");
QualType lhsType = proto->getArgType(i);
QualType rhsType = argExpr->getType();
// If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
if (const ArrayType *ary = lhsType->getAsArrayType())
lhsType = Context.getPointerType(ary->getElementType());
else if (lhsType->isFunctionType())
lhsType = Context.getPointerType(lhsType);
AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
argExpr);
if (Args[i] != argExpr) // The expression was converted.
Args[i] = argExpr; // Make sure we store the converted expression.
SourceLocation l = argExpr->getLocStart();
// decode the result (notice that AST's are still created for extensions).
switch (result) {
case Compatible:
break;
case PointerFromInt:
// check for null pointer constant (C99 6.3.2.3p3)
if (!argExpr->isNullPointerConstant(Context)) {
Diag(l, diag::ext_typecheck_passing_pointer_int,
lhsType.getAsString(), rhsType.getAsString(),
Fn->getSourceRange(), argExpr->getSourceRange());
}
break;
case IntFromPointer:
Diag(l, diag::ext_typecheck_passing_pointer_int,
lhsType.getAsString(), rhsType.getAsString(),
Fn->getSourceRange(), argExpr->getSourceRange());
break;
case IncompatiblePointer:
Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
rhsType.getAsString(), lhsType.getAsString(),
Fn->getSourceRange(), argExpr->getSourceRange());
break;
case CompatiblePointerDiscardsQualifiers:
Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
rhsType.getAsString(), lhsType.getAsString(),
Fn->getSourceRange(), argExpr->getSourceRange());
break;
case Incompatible:
return Diag(l, diag::err_typecheck_passing_incompatible,
rhsType.getAsString(), lhsType.getAsString(),
Fn->getSourceRange(), argExpr->getSourceRange());
}
}
// Even if the types checked, bail if we had the wrong number of arguments.
if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
return true;
}
// Do special checking on direct calls to functions.
if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, NumArgsInCall))
return true;
return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
}
Action::ExprResult Sema::
ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc, ExprTy *InitExpr) {
assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
QualType literalType = QualType::getFromOpaquePtr(Ty);
// FIXME: put back this assert when initializers are worked out.
//assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
Expr *literalExpr = static_cast<Expr*>(InitExpr);
// FIXME: add semantic analysis (C99 6.5.2.5).
return new CompoundLiteralExpr(literalType, literalExpr);
}
Action::ExprResult Sema::
ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
SourceLocation RParenLoc) {
// FIXME: add semantic analysis (C99 6.7.8). This involves
// knowledge of the object being intialized. As a result, the code for
// doing the semantic analysis will likely be located elsewhere (i.e. in
// consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
return false; // FIXME instantiate an InitListExpr.
}
Action::ExprResult Sema::
ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc, ExprTy *Op) {
assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
Expr *castExpr = static_cast<Expr*>(Op);
QualType castType = QualType::getFromOpaquePtr(Ty);
// C99 6.5.4p2: the cast type needs to be void or scalar and the expression
// type needs to be scalar.
if (!castType->isScalarType() && !castType->isVoidType()) {
return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
}
if (!castExpr->getType()->isScalarType()) {
return Diag(castExpr->getLocStart(),
diag::err_typecheck_expect_scalar_operand,
castExpr->getType().getAsString(), castExpr->getSourceRange());
}
return new CastExpr(castType, castExpr, LParenLoc);
}
inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
UsualUnaryConversions(cond);
UsualUnaryConversions(lex);
UsualUnaryConversions(rex);
QualType condT = cond->getType();
QualType lexT = lex->getType();
QualType rexT = rex->getType();
// first, check the condition.
if (!condT->isScalarType()) { // C99 6.5.15p2
Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
condT.getAsString());
return QualType();
}
// now check the two expressions.
if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
UsualArithmeticConversions(lex, rex);
return lex->getType();
}
if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
if (const RecordType *RHSRT = rexT->getAsRecordType()) {
if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
return lexT;
Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
lexT.getAsString(), rexT.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
return QualType();
}
}
// C99 6.5.15p3
if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
return lexT;
if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
return rexT;
if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
if (const PointerType *RHSPT = rexT->getAsPointerType()) {
// get the "pointed to" types
QualType lhptee = LHSPT->getPointeeType();
QualType rhptee = RHSPT->getPointeeType();
// ignore qualifiers on void (C99 6.5.15p3, clause 6)
if (lhptee->isVoidType() &&
(rhptee->isObjectType() || rhptee->isIncompleteType()))
return lexT;
if (rhptee->isVoidType() &&
(lhptee->isObjectType() || lhptee->isIncompleteType()))
return rexT;
if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
rhptee.getUnqualifiedType())) {
Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
lexT.getAsString(), rexT.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
return lexT; // FIXME: this is an _ext - is this return o.k?
}
// The pointer types are compatible.
// C99 6.5.15p6: If both operands are pointers to compatible types *or* to
// differently qualified versions of compatible types, the result type is a
// pointer to an appropriately qualified version of the *composite* type.
return lexT; // FIXME: Need to return the composite type.
}
}
if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
return lexT;
Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
lexT.getAsString(), rexT.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
return QualType();
}
/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
/// in the case of a the GNU conditional expr extension.
Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
SourceLocation ColonLoc,
ExprTy *Cond, ExprTy *LHS,
ExprTy *RHS) {
Expr *CondExpr = (Expr *) Cond;
Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
RHSExpr, QuestionLoc);
if (result.isNull())
return true;
return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
}
// promoteExprToType - a helper function to ensure we create exactly one
// ImplicitCastExpr. As a convenience (to the caller), we return the type.
static void promoteExprToType(Expr *&expr, QualType type) {
if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
impCast->setType(type);
else
expr = new ImplicitCastExpr(type, expr);
return;
}
/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
void Sema::DefaultFunctionArrayConversion(Expr *&e) {
QualType t = e->getType();
assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
if (const ReferenceType *ref = t->getAsReferenceType()) {
promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
t = e->getType();
}
if (t->isFunctionType())
promoteExprToType(e, Context.getPointerType(t));
else if (const ArrayType *ary = t->getAsArrayType())
promoteExprToType(e, Context.getPointerType(ary->getElementType()));
}
/// UsualUnaryConversion - Performs various conversions that are common to most
/// operators (C99 6.3). The conversions of array and function types are
/// sometimes surpressed. For example, the array->pointer conversion doesn't
/// apply if the array is an argument to the sizeof or address (&) operators.
/// In these instances, this routine should *not* be called.
void Sema::UsualUnaryConversions(Expr *&expr) {
QualType t = expr->getType();
assert(!t.isNull() && "UsualUnaryConversions - missing type");
if (const ReferenceType *ref = t->getAsReferenceType()) {
promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
t = expr->getType();
}
if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
promoteExprToType(expr, Context.IntTy);
else
DefaultFunctionArrayConversion(expr);
}
/// UsualArithmeticConversions - Performs various conversions that are common to
/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
/// routine returns the first non-arithmetic type found. The client is
/// responsible for emitting appropriate error diagnostics.
QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
bool isCompAssign) {
if (!isCompAssign) {
UsualUnaryConversions(lhsExpr);
UsualUnaryConversions(rhsExpr);
}
QualType lhs = lhsExpr->getType();
QualType rhs = rhsExpr->getType();
// If both types are identical, no conversion is needed.
if (lhs == rhs)
return lhs;
// If either side is a non-arithmetic type (e.g. a pointer), we are done.
// The caller can deal with this (e.g. pointer + int).
if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
return lhs;
// At this point, we have two different arithmetic types.
// Handle complex types first (C99 6.3.1.8p1).
if (lhs->isComplexType() || rhs->isComplexType()) {
// if we have an integer operand, the result is the complex type.
if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
return lhs;
}
if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
return rhs;
}
// This handles complex/complex, complex/float, or float/complex.
// When both operands are complex, the shorter operand is converted to the
// type of the longer, and that is the type of the result. This corresponds
// to what is done when combining two real floating-point operands.
// The fun begins when size promotion occur across type domains.
// From H&S 6.3.4: When one operand is complex and the other is a real
// floating-point type, the less precise type is converted, within it's
// real or complex domain, to the precision of the other type. For example,
// when combining a "long double" with a "double _Complex", the
// "double _Complex" is promoted to "long double _Complex".
if (Context.maxFloatingType(lhs, rhs) == lhs) {
// The left side is bigger, convert rhs within it's domain.
QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
if (!isCompAssign) promoteExprToType(rhsExpr, tMax);
return tMax;
}
// The right side is bigger, convert lhs within it's domain.
QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
if (!isCompAssign) promoteExprToType(lhsExpr, tMax);
return tMax;
}
// Now handle "real" floating types (i.e. float, double, long double).
if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
// if we have an integer operand, the result is the real floating type.
if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
return lhs;
}
if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
return rhs;
}
// We have two real floating types, float/complex combos were handled above.
// Convert the smaller operand to the bigger result.
if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
return lhs;
}
if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
return rhs;
}
// Finally, we have two differing integer types.
if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
return lhs;
}
if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
return rhs;
}
// CheckPointerTypesForAssignment - This is a very tricky routine (despite
// being closely modeled after the C99 spec:-). The odd characteristic of this
// routine is it effectively iqnores the qualifiers on the top level pointee.
// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
// FIXME: add a couple examples in this comment.
Sema::AssignmentCheckResult
Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
QualType lhptee, rhptee;
// get the "pointed to" type (ignoring qualifiers at the top level)
lhptee = lhsType->getAsPointerType()->getPointeeType();
rhptee = rhsType->getAsPointerType()->getPointeeType();
// make sure we operate on the canonical type
lhptee = lhptee.getCanonicalType();
rhptee = rhptee.getCanonicalType();
AssignmentCheckResult r = Compatible;
// C99 6.5.16.1p1: This following citation is common to constraints
// 3 & 4 (below). ...and the type *pointed to* by the left has all the
// qualifiers of the type *pointed to* by the right;
if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
rhptee.getQualifiers())
r = CompatiblePointerDiscardsQualifiers;
// C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
// incomplete type and the other is a pointer to a qualified or unqualified
// version of void...
if (lhptee.getUnqualifiedType()->isVoidType() &&
(rhptee->isObjectType() || rhptee->isIncompleteType()))
;
else if (rhptee.getUnqualifiedType()->isVoidType() &&
(lhptee->isObjectType() || lhptee->isIncompleteType()))
;
// C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
// unqualified versions of compatible types, ...
else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
rhptee.getUnqualifiedType()))
r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
return r;
}
/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
/// has code to accommodate several GCC extensions when type checking
/// pointers. Here are some objectionable examples that GCC considers warnings:
///
/// int a, *pint;
/// short *pshort;
/// struct foo *pfoo;
///
/// pint = pshort; // warning: assignment from incompatible pointer type
/// a = pint; // warning: assignment makes integer from pointer without a cast
/// pint = a; // warning: assignment makes pointer from integer without a cast
/// pint = pfoo; // warning: assignment from incompatible pointer type
///
/// As a result, the code for dealing with pointers is more complex than the
/// C99 spec dictates.
/// Note: the warning above turn into errors when -pedantic-errors is enabled.
///
Sema::AssignmentCheckResult
Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
if (lhsType == rhsType) // common case, fast path...
return Compatible;
if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
if (lhsType->isVectorType() || rhsType->isVectorType()) {
if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
return Incompatible;
}
return Compatible;
} else if (lhsType->isPointerType()) {
if (rhsType->isIntegerType())
return PointerFromInt;
if (rhsType->isPointerType())
return CheckPointerTypesForAssignment(lhsType, rhsType);
} else if (rhsType->isPointerType()) {
// C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
return IntFromPointer;
if (lhsType->isPointerType())
return CheckPointerTypesForAssignment(lhsType, rhsType);
} else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
if (Type::tagTypesAreCompatible(lhsType, rhsType))
return Compatible;
} else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
if (Type::referenceTypesAreCompatible(lhsType, rhsType))
return Compatible;
}
return Incompatible;
}
Sema::AssignmentCheckResult
Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
// This check seems unnatural, however it is necessary to insure the proper
// conversion of functions/arrays. If the conversion were done for all
// DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
// expressions that surpress this implicit conversion (&, sizeof).
DefaultFunctionArrayConversion(rExpr);
Sema::AssignmentCheckResult result;
result = CheckAssignmentConstraints(lhsType, rExpr->getType());
// C99 6.5.16.1p2: The value of the right operand is converted to the
// type of the assignment expression.
if (rExpr->getType() != lhsType)
promoteExprToType(rExpr, lhsType);
return result;
}
Sema::AssignmentCheckResult
Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
return CheckAssignmentConstraints(lhsType, rhsType);
}
inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Diag(loc, diag::err_typecheck_invalid_operands,
lex->getType().getAsString(), rex->getType().getAsString(),
lex->getSourceRange(), rex->getSourceRange());
}
inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
Expr *&rex) {
QualType lhsType = lex->getType(), rhsType = rex->getType();
// make sure the vector types are identical.
if (lhsType == rhsType)
return lhsType;
// You cannot convert between vector values of different size.
Diag(loc, diag::err_typecheck_vector_not_convertable,
lex->getType().getAsString(), rex->getType().getAsString(),
lex->getSourceRange(), rex->getSourceRange());
return QualType();
}
inline QualType Sema::CheckMultiplyDivideOperands(
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
QualType lhsType = lex->getType(), rhsType = rex->getType();
if (lhsType->isVectorType() || rhsType->isVectorType())
return CheckVectorOperands(loc, lex, rex);
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
return compType;
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckRemainderOperands(
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
QualType lhsType = lex->getType(), rhsType = rex->getType();
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
return compType;
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
return CheckVectorOperands(loc, lex, rex);
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
// handle the common case first (both operands are arithmetic).
if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
return compType;
if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
return lex->getType();
if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
return rex->getType();
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
return CheckVectorOperands(loc, lex, rex);
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
// handle the common case first (both operands are arithmetic).
if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
return compType;
if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
return compType;
if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
return Context.getPointerDiffType();
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
// FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
// for int << longlong -> the result type should be int, not long long.
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
// handle the common case first (both operands are arithmetic).
if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
return compType;
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckCompareOperands( // C99 6.5.8
Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
{
// C99 6.5.8p3 / C99 6.5.9p4
if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
UsualArithmeticConversions(lex, rex);
else {
UsualUnaryConversions(lex);
UsualUnaryConversions(rex);
}
QualType lType = lex->getType();
QualType rType = rex->getType();
if (isRelational) {
if (lType->isRealType() && rType->isRealType())
return Context.IntTy;
} else {
if (lType->isArithmeticType() && rType->isArithmeticType())
return Context.IntTy;
}
bool LHSIsNull = lex->isNullPointerConstant(Context);
bool RHSIsNull = rex->isNullPointerConstant(Context);
// All of the following pointer related warnings are GCC extensions, except
// when handling null pointer constants. One day, we can consider making them
// errors (when -pedantic-errors is enabled).
if (lType->isPointerType() && rType->isPointerType()) {
if (!LHSIsNull && !RHSIsNull &&
!Type::pointerTypesAreCompatible(lType, rType)) {
Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
lType.getAsString(), rType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
}
promoteExprToType(rex, lType); // promote the pointer to pointer
return Context.IntTy;
}
if (lType->isPointerType() && rType->isIntegerType()) {
if (!RHSIsNull)
Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
lType.getAsString(), rType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
promoteExprToType(rex, lType); // promote the integer to pointer
return Context.IntTy;
}
if (lType->isIntegerType() && rType->isPointerType()) {
if (!LHSIsNull)
Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
lType.getAsString(), rType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
promoteExprToType(lex, rType); // promote the integer to pointer
return Context.IntTy;
}
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckBitwiseOperands(
Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
{
if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
return CheckVectorOperands(loc, lex, rex);
QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
return compType;
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Expr *&lex, Expr *&rex, SourceLocation loc)
{
UsualUnaryConversions(lex);
UsualUnaryConversions(rex);
if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
return Context.IntTy;
InvalidOperands(loc, lex, rex);
return QualType();
}
inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
{
QualType lhsType = lex->getType();
QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
bool hadError = false;
Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
switch (mlval) { // C99 6.5.16p2
case Expr::MLV_Valid:
break;
case Expr::MLV_ConstQualified:
Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
hadError = true;
break;
case Expr::MLV_ArrayType:
Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
lhsType.getAsString(), lex->getSourceRange());
return QualType();
case Expr::MLV_NotObjectType:
Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
lhsType.getAsString(), lex->getSourceRange());
return QualType();
case Expr::MLV_InvalidExpression:
Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
lex->getSourceRange());
return QualType();
case Expr::MLV_IncompleteType:
case Expr::MLV_IncompleteVoidType:
Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
lhsType.getAsString(), lex->getSourceRange());
return QualType();
case Expr::MLV_DuplicateVectorComponents:
Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
lex->getSourceRange());
return QualType();
}
AssignmentCheckResult result;
if (compoundType.isNull())
result = CheckSingleAssignmentConstraints(lhsType, rex);
else
result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
// decode the result (notice that extensions still return a type).
switch (result) {
case Compatible:
break;
case Incompatible:
Diag(loc, diag::err_typecheck_assign_incompatible,
lhsType.getAsString(), rhsType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
hadError = true;
break;
case PointerFromInt:
// check for null pointer constant (C99 6.3.2.3p3)
if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Diag(loc, diag::ext_typecheck_assign_pointer_int,
lhsType.getAsString(), rhsType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
}
break;
case IntFromPointer:
Diag(loc, diag::ext_typecheck_assign_pointer_int,
lhsType.getAsString(), rhsType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
break;
case IncompatiblePointer:
Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
lhsType.getAsString(), rhsType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
break;
case CompatiblePointerDiscardsQualifiers:
Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
lhsType.getAsString(), rhsType.getAsString(),
lex->getSourceRange(), rex->getSourceRange());
break;
}
// C99 6.5.16p3: The type of an assignment expression is the type of the
// left operand unless the left operand has qualified type, in which case
// it is the unqualified version of the type of the left operand.
// C99 6.5.16.1p2: In simple assignment, the value of the right operand
// is converted to the type of the assignment expression (above).
// C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
return hadError ? QualType() : lhsType.getUnqualifiedType();
}
inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Expr *&lex, Expr *&rex, SourceLocation loc) {
UsualUnaryConversions(rex);
return rex->getType();
}
/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
QualType resType = op->getType();
assert(!resType.isNull() && "no type for increment/decrement expression");
// C99 6.5.2.4p1: We allow complex as a GCC extension.
if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
resType.getAsString(), op->getSourceRange());
return QualType();
}
} else if (!resType->isRealType()) {
if (resType->isComplexType())
// C99 does not support ++/-- on complex types.
Diag(OpLoc, diag::ext_integer_increment_complex,
resType.getAsString(), op->getSourceRange());
else {
Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
resType.getAsString(), op->getSourceRange());
return QualType();
}
}
// At this point, we know we have a real, complex or pointer type.
// Now make sure the operand is a modifiable lvalue.
Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
if (mlval != Expr::MLV_Valid) {
// FIXME: emit a more precise diagnostic...
Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
op->getSourceRange());
return QualType();
}
return resType;
}
/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
/// This routine allows us to typecheck complex/recursive expressions
/// where the declaration is needed for type checking. Here are some
/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
static Decl *getPrimaryDeclaration(Expr *e) {
switch (e->getStmtClass()) {
case Stmt::DeclRefExprClass:
return cast<DeclRefExpr>(e)->getDecl();
case Stmt::MemberExprClass:
return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
case Stmt::ArraySubscriptExprClass:
return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
case Stmt::CallExprClass:
return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
case Stmt::UnaryOperatorClass:
return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
case Stmt::ParenExprClass:
return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
default:
return 0;
}
}
/// CheckAddressOfOperand - The operand of & must be either a function
/// designator or an lvalue designating an object. If it is an lvalue, the
/// object cannot be declared with storage class register or be a bit field.
/// Note: The usual conversions are *not* applied to the operand of the &
/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Decl *dcl = getPrimaryDeclaration(op);
Expr::isLvalueResult lval = op->isLvalue();
if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
;
else { // FIXME: emit more specific diag...
Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
op->getSourceRange());
return QualType();
}
} else if (dcl) {
// We have an lvalue with a decl. Make sure the decl is not declared
// with the register storage-class specifier.
if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
if (vd->getStorageClass() == VarDecl::Register) {
Diag(OpLoc, diag::err_typecheck_address_of_register,
op->getSourceRange());
return QualType();
}
} else
assert(0 && "Unknown/unexpected decl type");
// FIXME: add check for bitfields!
}
// If the operand has type "type", the result has type "pointer to type".
return Context.getPointerType(op->getType());
}
QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
UsualUnaryConversions(op);
QualType qType = op->getType();
if (const PointerType *PT = qType->getAsPointerType()) {
QualType ptype = PT->getPointeeType();
// C99 6.5.3.2p4. "if it points to an object,...".
if (ptype->isIncompleteType()) { // An incomplete type is not an object
// GCC compat: special case 'void *' (treat as warning).
if (ptype->isVoidType()) {
Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
qType.getAsString(), op->getSourceRange());
} else {
Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
ptype.getAsString(), op->getSourceRange());
return QualType();
}
}
return ptype;
}
Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
qType.getAsString(), op->getSourceRange());
return QualType();
}
static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
tok::TokenKind Kind) {
BinaryOperator::Opcode Opc;
switch (Kind) {
default: assert(0 && "Unknown binop!");
case tok::star: Opc = BinaryOperator::Mul; break;
case tok::slash: Opc = BinaryOperator::Div; break;
case tok::percent: Opc = BinaryOperator::Rem; break;
case tok::plus: Opc = BinaryOperator::Add; break;
case tok::minus: Opc = BinaryOperator::Sub; break;
case tok::lessless: Opc = BinaryOperator::Shl; break;
case tok::greatergreater: Opc = BinaryOperator::Shr; break;
case tok::lessequal: Opc = BinaryOperator::LE; break;
case tok::less: Opc = BinaryOperator::LT; break;
case tok::greaterequal: Opc = BinaryOperator::GE; break;
case tok::greater: Opc = BinaryOperator::GT; break;
case tok::exclaimequal: Opc = BinaryOperator::NE; break;
case tok::equalequal: Opc = BinaryOperator::EQ; break;
case tok::amp: Opc = BinaryOperator::And; break;
case tok::caret: Opc = BinaryOperator::Xor; break;
case tok::pipe: Opc = BinaryOperator::Or; break;
case tok::ampamp: Opc = BinaryOperator::LAnd; break;
case tok::pipepipe: Opc = BinaryOperator::LOr; break;
case tok::equal: Opc = BinaryOperator::Assign; break;
case tok::starequal: Opc = BinaryOperator::MulAssign; break;
case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
case tok::comma: Opc = BinaryOperator::Comma; break;
}
return Opc;
}
static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
tok::TokenKind Kind) {
UnaryOperator::Opcode Opc;
switch (Kind) {
default: assert(0 && "Unknown unary op!");
case tok::plusplus: Opc = UnaryOperator::PreInc; break;
case tok::minusminus: Opc = UnaryOperator::PreDec; break;
case tok::amp: Opc = UnaryOperator::AddrOf; break;
case tok::star: Opc = UnaryOperator::Deref; break;
case tok::plus: Opc = UnaryOperator::Plus; break;
case tok::minus: Opc = UnaryOperator::Minus; break;
case tok::tilde: Opc = UnaryOperator::Not; break;
case tok::exclaim: Opc = UnaryOperator::LNot; break;
case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
case tok::kw___real: Opc = UnaryOperator::Real; break;
case tok::kw___imag: Opc = UnaryOperator::Imag; break;
case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
}
return Opc;
}
// Binary Operators. 'Tok' is the token for the operator.
Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
ExprTy *LHS, ExprTy *RHS) {
BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
assert((lhs != 0) && "ParseBinOp(): missing left expression");
assert((rhs != 0) && "ParseBinOp(): missing right expression");
QualType ResultTy; // Result type of the binary operator.
QualType CompTy; // Computation type for compound assignments (e.g. '+=')
switch (Opc) {
default:
assert(0 && "Unknown binary expr!");
case BinaryOperator::Assign:
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
break;
case BinaryOperator::Mul:
case BinaryOperator::Div:
ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::Rem:
ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::Add:
ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::Sub:
ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::Shl:
case BinaryOperator::Shr:
ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::LE:
case BinaryOperator::LT:
case BinaryOperator::GE:
case BinaryOperator::GT:
ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
break;
case BinaryOperator::EQ:
case BinaryOperator::NE:
ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
break;
case BinaryOperator::And:
case BinaryOperator::Xor:
case BinaryOperator::Or:
ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::LAnd:
case BinaryOperator::LOr:
ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
break;
case BinaryOperator::MulAssign:
case BinaryOperator::DivAssign:
CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::RemAssign:
CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::AddAssign:
CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::SubAssign:
CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::ShlAssign:
case BinaryOperator::ShrAssign:
CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::AndAssign:
case BinaryOperator::XorAssign:
case BinaryOperator::OrAssign:
CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
if (!CompTy.isNull())
ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
break;
case BinaryOperator::Comma:
ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
break;
}
if (ResultTy.isNull())
return true;
if (CompTy.isNull())
return new BinaryOperator(lhs, rhs, Opc, ResultTy);
else
return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
}
// Unary Operators. 'Tok' is the token for the operator.
Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
ExprTy *input) {
Expr *Input = (Expr*)input;
UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
QualType resultType;
switch (Opc) {
default:
assert(0 && "Unimplemented unary expr!");
case UnaryOperator::PreInc:
case UnaryOperator::PreDec:
resultType = CheckIncrementDecrementOperand(Input, OpLoc);
break;
case UnaryOperator::AddrOf:
resultType = CheckAddressOfOperand(Input, OpLoc);
break;
case UnaryOperator::Deref:
resultType = CheckIndirectionOperand(Input, OpLoc);
break;
case UnaryOperator::Plus:
case UnaryOperator::Minus:
UsualUnaryConversions(Input);
resultType = Input->getType();
if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
return Diag(OpLoc, diag::err_typecheck_unary_expr,
resultType.getAsString());
break;
case UnaryOperator::Not: // bitwise complement
UsualUnaryConversions(Input);
resultType = Input->getType();
// C99 6.5.3.3p1. We allow complex as a GCC extension.
if (!resultType->isIntegerType()) {
if (resultType->isComplexType())
// C99 does not support '~' for complex conjugation.
Diag(OpLoc, diag::ext_integer_complement_complex,
resultType.getAsString());
else
return Diag(OpLoc, diag::err_typecheck_unary_expr,
resultType.getAsString());
}
break;
case UnaryOperator::LNot: // logical negation
// Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
DefaultFunctionArrayConversion(Input);
resultType = Input->getType();
if (!resultType->isScalarType()) // C99 6.5.3.3p1
return Diag(OpLoc, diag::err_typecheck_unary_expr,
resultType.getAsString());
// LNot always has type int. C99 6.5.3.3p5.
resultType = Context.IntTy;
break;
case UnaryOperator::SizeOf:
resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
break;
case UnaryOperator::AlignOf:
resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
break;
case UnaryOperator::Real:
case UnaryOperator::Imag:
resultType = CheckRealImagOperand(Input, OpLoc);
break;
case UnaryOperator::Extension:
resultType = Input->getType();
break;
}
if (resultType.isNull())
return true;
return new UnaryOperator(Input, Opc, resultType, OpLoc);
}
/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
SourceLocation LabLoc,
IdentifierInfo *LabelII) {
// Look up the record for this label identifier.
LabelStmt *&LabelDecl = LabelMap[LabelII];
// If we haven't seen this label yet, create a forward reference.
if (LabelDecl == 0)
LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
// Create the AST node. The address of a label always has type 'void*'.
return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
Context.getPointerType(Context.VoidTy));
}
Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
SourceLocation RPLoc) { // "({..})"
Stmt *SubStmt = static_cast<Stmt*>(substmt);
assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
// FIXME: there are a variety of strange constraints to enforce here, for
// example, it is not possible to goto into a stmt expression apparently.
// More semantic analysis is needed.
// FIXME: the last statement in the compount stmt has its value used. We
// should not warn about it being unused.
// If there are sub stmts in the compound stmt, take the type of the last one
// as the type of the stmtexpr.
QualType Ty = Context.VoidTy;
if (!Compound->body_empty())
if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
Ty = LastExpr->getType();
return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
}
Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
TypeTy *arg1, TypeTy *arg2,
SourceLocation RPLoc) {
QualType argT1 = QualType::getFromOpaquePtr(arg1);
QualType argT2 = QualType::getFromOpaquePtr(arg2);
assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
}
Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
ExprTy *expr1, ExprTy *expr2,
SourceLocation RPLoc) {
Expr *CondExpr = static_cast<Expr*>(cond);
Expr *LHSExpr = static_cast<Expr*>(expr1);
Expr *RHSExpr = static_cast<Expr*>(expr2);
assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
// The conditional expression is required to be a constant expression.
llvm::APSInt condEval(32);
SourceLocation ExpLoc;
if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
CondExpr->getSourceRange());
// If the condition is > zero, then the AST type is the same as the LSHExpr.
QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
RHSExpr->getType();
return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
}
// TODO: Move this to SemaObjC.cpp
Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
StringLiteral* S = static_cast<StringLiteral *>(string);
if (CheckBuiltinCFStringArgument(S))
return true;
QualType t = Context.getCFConstantStringType();
t = t.getQualifiedType(QualType::Const);
t = Context.getPointerType(t);
return new ObjCStringLiteral(S, t);
}
Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
SourceLocation LParenLoc,
TypeTy *Ty,
SourceLocation RParenLoc) {
QualType EncodedType = QualType::getFromOpaquePtr(Ty);
QualType t = Context.getPointerType(Context.CharTy);
return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
}
|