@@ -564,12 +564,13 @@ class ConstStructBuilder {
564
564
565
565
public:
566
566
static llvm::Constant *BuildStruct (ConstantEmitter &Emitter,
567
- InitListExpr *ILE, QualType StructTy);
567
+ const InitListExpr *ILE,
568
+ QualType StructTy);
568
569
static llvm::Constant *BuildStruct (ConstantEmitter &Emitter,
569
570
const APValue &Value, QualType ValTy);
570
571
static bool UpdateStruct (ConstantEmitter &Emitter,
571
572
ConstantAggregateBuilder &Const, CharUnits Offset,
572
- InitListExpr *Updater);
573
+ const InitListExpr *Updater);
573
574
574
575
private:
575
576
ConstStructBuilder (ConstantEmitter &Emitter,
@@ -586,7 +587,7 @@ class ConstStructBuilder {
586
587
bool AppendBitField (const FieldDecl *Field, uint64_t FieldOffset,
587
588
llvm::ConstantInt *InitExpr, bool AllowOverwrite = false );
588
589
589
- bool Build (InitListExpr *ILE, bool AllowOverwrite);
590
+ bool Build (const InitListExpr *ILE, bool AllowOverwrite);
590
591
bool Build (const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
591
592
const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
592
593
llvm::Constant *Finalize (QualType Ty);
@@ -635,7 +636,7 @@ bool ConstStructBuilder::AppendBitField(
635
636
static bool EmitDesignatedInitUpdater (ConstantEmitter &Emitter,
636
637
ConstantAggregateBuilder &Const,
637
638
CharUnits Offset, QualType Type,
638
- InitListExpr *Updater) {
639
+ const InitListExpr *Updater) {
639
640
if (Type->isRecordType ())
640
641
return ConstStructBuilder::UpdateStruct (Emitter, Const, Offset, Updater);
641
642
@@ -647,7 +648,7 @@ static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
647
648
llvm::Type *ElemTy = Emitter.CGM .getTypes ().ConvertTypeForMem (ElemType);
648
649
649
650
llvm::Constant *FillC = nullptr ;
650
- if (Expr *Filler = Updater->getArrayFiller ()) {
651
+ if (const Expr *Filler = Updater->getArrayFiller ()) {
651
652
if (!isa<NoInitExpr>(Filler)) {
652
653
FillC = Emitter.tryEmitAbstractForMemory (Filler, ElemType);
653
654
if (!FillC)
@@ -658,7 +659,7 @@ static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
658
659
unsigned NumElementsToUpdate =
659
660
FillC ? CAT->getZExtSize () : Updater->getNumInits ();
660
661
for (unsigned I = 0 ; I != NumElementsToUpdate; ++I, Offset += ElemSize) {
661
- Expr *Init = nullptr ;
662
+ const Expr *Init = nullptr ;
662
663
if (I < Updater->getNumInits ())
663
664
Init = Updater->getInit (I);
664
665
@@ -667,7 +668,7 @@ static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
667
668
return false ;
668
669
} else if (!Init || isa<NoInitExpr>(Init)) {
669
670
continue ;
670
- } else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init)) {
671
+ } else if (const auto *ChildILE = dyn_cast<InitListExpr>(Init)) {
671
672
if (!EmitDesignatedInitUpdater (Emitter, Const, Offset, ElemType,
672
673
ChildILE))
673
674
return false ;
@@ -683,7 +684,7 @@ static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
683
684
return true ;
684
685
}
685
686
686
- bool ConstStructBuilder::Build (InitListExpr *ILE, bool AllowOverwrite) {
687
+ bool ConstStructBuilder::Build (const InitListExpr *ILE, bool AllowOverwrite) {
687
688
RecordDecl *RD = ILE->getType ()->castAs <RecordType>()->getDecl ();
688
689
const ASTRecordLayout &Layout = CGM.getContext ().getASTRecordLayout (RD);
689
690
@@ -711,7 +712,7 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
711
712
712
713
// Get the initializer. A struct can include fields without initializers,
713
714
// we just use explicit null values for them.
714
- Expr *Init = nullptr ;
715
+ const Expr *Init = nullptr ;
715
716
if (ElementNo < ILE->getNumInits ())
716
717
Init = ILE->getInit (ElementNo++);
717
718
if (Init && isa<NoInitExpr>(Init))
@@ -879,7 +880,7 @@ llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
879
880
}
880
881
881
882
llvm::Constant *ConstStructBuilder::BuildStruct (ConstantEmitter &Emitter,
882
- InitListExpr *ILE,
883
+ const InitListExpr *ILE,
883
884
QualType ValTy) {
884
885
ConstantAggregateBuilder Const (Emitter.CGM );
885
886
ConstStructBuilder Builder (Emitter, Const, CharUnits::Zero ());
@@ -906,7 +907,8 @@ llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
906
907
907
908
bool ConstStructBuilder::UpdateStruct (ConstantEmitter &Emitter,
908
909
ConstantAggregateBuilder &Const,
909
- CharUnits Offset, InitListExpr *Updater) {
910
+ CharUnits Offset,
911
+ const InitListExpr *Updater) {
910
912
return ConstStructBuilder (Emitter, Const, Offset)
911
913
.Build (Updater, /* AllowOverwrite*/ true );
912
914
}
@@ -1013,8 +1015,8 @@ EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
1013
1015
//
1014
1016
// Constant folding is currently missing support for a few features supported
1015
1017
// here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr.
1016
- class ConstExprEmitter :
1017
- public StmtVisitor <ConstExprEmitter, llvm::Constant*, QualType> {
1018
+ class ConstExprEmitter
1019
+ : public ConstStmtVisitor <ConstExprEmitter, llvm::Constant *, QualType> {
1018
1020
CodeGenModule &CGM;
1019
1021
ConstantEmitter &Emitter;
1020
1022
llvm::LLVMContext &VMContext;
@@ -1027,43 +1029,42 @@ class ConstExprEmitter :
1027
1029
// Visitor Methods
1028
1030
// ===--------------------------------------------------------------------===//
1029
1031
1030
- llvm::Constant *VisitStmt (Stmt *S, QualType T) {
1031
- return nullptr ;
1032
- }
1032
+ llvm::Constant *VisitStmt (const Stmt *S, QualType T) { return nullptr ; }
1033
1033
1034
- llvm::Constant *VisitConstantExpr (ConstantExpr *CE, QualType T) {
1034
+ llvm::Constant *VisitConstantExpr (const ConstantExpr *CE, QualType T) {
1035
1035
if (llvm::Constant *Result = Emitter.tryEmitConstantExpr (CE))
1036
1036
return Result;
1037
1037
return Visit (CE->getSubExpr (), T);
1038
1038
}
1039
1039
1040
- llvm::Constant *VisitParenExpr (ParenExpr *PE, QualType T) {
1040
+ llvm::Constant *VisitParenExpr (const ParenExpr *PE, QualType T) {
1041
1041
return Visit (PE->getSubExpr (), T);
1042
1042
}
1043
1043
1044
1044
llvm::Constant *
1045
- VisitSubstNonTypeTemplateParmExpr (SubstNonTypeTemplateParmExpr *PE,
1045
+ VisitSubstNonTypeTemplateParmExpr (const SubstNonTypeTemplateParmExpr *PE,
1046
1046
QualType T) {
1047
1047
return Visit (PE->getReplacement (), T);
1048
1048
}
1049
1049
1050
- llvm::Constant *VisitGenericSelectionExpr (GenericSelectionExpr *GE,
1050
+ llvm::Constant *VisitGenericSelectionExpr (const GenericSelectionExpr *GE,
1051
1051
QualType T) {
1052
1052
return Visit (GE->getResultExpr (), T);
1053
1053
}
1054
1054
1055
- llvm::Constant *VisitChooseExpr (ChooseExpr *CE, QualType T) {
1055
+ llvm::Constant *VisitChooseExpr (const ChooseExpr *CE, QualType T) {
1056
1056
return Visit (CE->getChosenSubExpr (), T);
1057
1057
}
1058
1058
1059
- llvm::Constant *VisitCompoundLiteralExpr (CompoundLiteralExpr *E, QualType T) {
1059
+ llvm::Constant *VisitCompoundLiteralExpr (const CompoundLiteralExpr *E,
1060
+ QualType T) {
1060
1061
return Visit (E->getInitializer (), T);
1061
1062
}
1062
1063
1063
- llvm::Constant *VisitCastExpr (CastExpr *E, QualType destType) {
1064
+ llvm::Constant *VisitCastExpr (const CastExpr *E, QualType destType) {
1064
1065
if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1065
1066
CGM.EmitExplicitCastExprType (ECE, Emitter.CGF );
1066
- Expr *subExpr = E->getSubExpr ();
1067
+ const Expr *subExpr = E->getSubExpr ();
1067
1068
1068
1069
switch (E->getCastKind ()) {
1069
1070
case CK_ToUnion: {
@@ -1117,7 +1118,8 @@ class ConstExprEmitter :
1117
1118
// interesting conversions should be done in Evaluate(). But as a
1118
1119
// special case, allow compound literals to support the gcc extension
1119
1120
// allowing "struct x {int x;} x = (struct x) {};".
1120
- if (auto *E = dyn_cast<CompoundLiteralExpr>(subExpr->IgnoreParens ()))
1121
+ if (const auto *E =
1122
+ dyn_cast<CompoundLiteralExpr>(subExpr->IgnoreParens ()))
1121
1123
return Visit (E->getInitializer (), destType);
1122
1124
return nullptr ;
1123
1125
}
@@ -1232,21 +1234,22 @@ class ConstExprEmitter :
1232
1234
llvm_unreachable (" Invalid CastKind" );
1233
1235
}
1234
1236
1235
- llvm::Constant *VisitCXXDefaultInitExpr (CXXDefaultInitExpr *DIE, QualType T) {
1237
+ llvm::Constant *VisitCXXDefaultInitExpr (const CXXDefaultInitExpr *DIE,
1238
+ QualType T) {
1236
1239
// No need for a DefaultInitExprScope: we don't handle 'this' in a
1237
1240
// constant expression.
1238
1241
return Visit (DIE->getExpr (), T);
1239
1242
}
1240
1243
1241
- llvm::Constant *VisitExprWithCleanups (ExprWithCleanups *E, QualType T) {
1244
+ llvm::Constant *VisitExprWithCleanups (const ExprWithCleanups *E, QualType T) {
1242
1245
return Visit (E->getSubExpr (), T);
1243
1246
}
1244
1247
1245
- llvm::Constant *VisitIntegerLiteral (IntegerLiteral *I, QualType T) {
1248
+ llvm::Constant *VisitIntegerLiteral (const IntegerLiteral *I, QualType T) {
1246
1249
return llvm::ConstantInt::get (CGM.getLLVMContext (), I->getValue ());
1247
1250
}
1248
1251
1249
- llvm::Constant *EmitArrayInitialization (InitListExpr *ILE, QualType T) {
1252
+ llvm::Constant *EmitArrayInitialization (const InitListExpr *ILE, QualType T) {
1250
1253
auto *CAT = CGM.getContext ().getAsConstantArrayType (ILE->getType ());
1251
1254
assert (CAT && " can't emit array init for non-constant-bound array" );
1252
1255
unsigned NumInitElements = ILE->getNumInits ();
@@ -1260,7 +1263,7 @@ class ConstExprEmitter :
1260
1263
1261
1264
// Initialize remaining array elements.
1262
1265
llvm::Constant *fillC = nullptr ;
1263
- if (Expr *filler = ILE->getArrayFiller ()) {
1266
+ if (const Expr *filler = ILE->getArrayFiller ()) {
1264
1267
fillC = Emitter.tryEmitAbstractForMemory (filler, EltType);
1265
1268
if (!fillC)
1266
1269
return nullptr ;
@@ -1275,7 +1278,7 @@ class ConstExprEmitter :
1275
1278
1276
1279
llvm::Type *CommonElementType = nullptr ;
1277
1280
for (unsigned i = 0 ; i < NumInitableElts; ++i) {
1278
- Expr *Init = ILE->getInit (i);
1281
+ const Expr *Init = ILE->getInit (i);
1279
1282
llvm::Constant *C = Emitter.tryEmitPrivateForMemory (Init, EltType);
1280
1283
if (!C)
1281
1284
return nullptr ;
@@ -1292,16 +1295,17 @@ class ConstExprEmitter :
1292
1295
fillC);
1293
1296
}
1294
1297
1295
- llvm::Constant *EmitRecordInitialization (InitListExpr *ILE, QualType T) {
1298
+ llvm::Constant *EmitRecordInitialization (const InitListExpr *ILE,
1299
+ QualType T) {
1296
1300
return ConstStructBuilder::BuildStruct (Emitter, ILE, T);
1297
1301
}
1298
1302
1299
- llvm::Constant *VisitImplicitValueInitExpr (ImplicitValueInitExpr* E,
1303
+ llvm::Constant *VisitImplicitValueInitExpr (const ImplicitValueInitExpr * E,
1300
1304
QualType T) {
1301
1305
return CGM.EmitNullConstant (T);
1302
1306
}
1303
1307
1304
- llvm::Constant *VisitInitListExpr (InitListExpr *ILE, QualType T) {
1308
+ llvm::Constant *VisitInitListExpr (const InitListExpr *ILE, QualType T) {
1305
1309
if (ILE->isTransparent ())
1306
1310
return Visit (ILE->getInit (0 ), T);
1307
1311
@@ -1314,8 +1318,9 @@ class ConstExprEmitter :
1314
1318
return nullptr ;
1315
1319
}
1316
1320
1317
- llvm::Constant *VisitDesignatedInitUpdateExpr (DesignatedInitUpdateExpr *E,
1318
- QualType destType) {
1321
+ llvm::Constant *
1322
+ VisitDesignatedInitUpdateExpr (const DesignatedInitUpdateExpr *E,
1323
+ QualType destType) {
1319
1324
auto C = Visit (E->getBase (), destType);
1320
1325
if (!C)
1321
1326
return nullptr ;
@@ -1329,12 +1334,13 @@ class ConstExprEmitter :
1329
1334
1330
1335
llvm::Type *ValTy = CGM.getTypes ().ConvertType (destType);
1331
1336
bool HasFlexibleArray = false ;
1332
- if (auto *RT = destType->getAs <RecordType>())
1337
+ if (const auto *RT = destType->getAs <RecordType>())
1333
1338
HasFlexibleArray = RT->getDecl ()->hasFlexibleArrayMember ();
1334
1339
return Const.build (ValTy, HasFlexibleArray);
1335
1340
}
1336
1341
1337
- llvm::Constant *VisitCXXConstructExpr (CXXConstructExpr *E, QualType Ty) {
1342
+ llvm::Constant *VisitCXXConstructExpr (const CXXConstructExpr *E,
1343
+ QualType Ty) {
1338
1344
if (!E->getConstructor ()->isTrivial ())
1339
1345
return nullptr ;
1340
1346
@@ -1344,13 +1350,13 @@ class ConstExprEmitter :
1344
1350
assert (E->getConstructor ()->isCopyOrMoveConstructor () &&
1345
1351
" trivial ctor has argument but isn't a copy/move ctor" );
1346
1352
1347
- Expr *Arg = E->getArg (0 );
1353
+ const Expr *Arg = E->getArg (0 );
1348
1354
assert (CGM.getContext ().hasSameUnqualifiedType (Ty, Arg->getType ()) &&
1349
1355
" argument to copy ctor is of wrong type" );
1350
1356
1351
1357
// Look through the temporary; it's just converting the value to an
1352
1358
// lvalue to pass it to the constructor.
1353
- if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Arg))
1359
+ if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Arg))
1354
1360
return Visit (MTE->getSubExpr (), Ty);
1355
1361
// Don't try to support arbitrary lvalue-to-rvalue conversions for now.
1356
1362
return nullptr ;
@@ -1359,12 +1365,12 @@ class ConstExprEmitter :
1359
1365
return CGM.EmitNullConstant (Ty);
1360
1366
}
1361
1367
1362
- llvm::Constant *VisitStringLiteral (StringLiteral *E, QualType T) {
1368
+ llvm::Constant *VisitStringLiteral (const StringLiteral *E, QualType T) {
1363
1369
// This is a string literal initializing an array in an initializer.
1364
1370
return CGM.GetConstantArrayFromStringLiteral (E);
1365
1371
}
1366
1372
1367
- llvm::Constant *VisitObjCEncodeExpr (ObjCEncodeExpr *E, QualType T) {
1373
+ llvm::Constant *VisitObjCEncodeExpr (const ObjCEncodeExpr *E, QualType T) {
1368
1374
// This must be an @encode initializing an array in a static initializer.
1369
1375
// Don't emit it as the address of the string, emit the string data itself
1370
1376
// as an inline array.
@@ -1383,14 +1389,14 @@ class ConstExprEmitter :
1383
1389
return Visit (E->getSubExpr (), T);
1384
1390
}
1385
1391
1386
- llvm::Constant *VisitUnaryMinus (UnaryOperator *U, QualType T) {
1392
+ llvm::Constant *VisitUnaryMinus (const UnaryOperator *U, QualType T) {
1387
1393
if (llvm::Constant *C = Visit (U->getSubExpr (), T))
1388
1394
if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
1389
1395
return llvm::ConstantInt::get (CGM.getLLVMContext (), -CI->getValue ());
1390
1396
return nullptr ;
1391
1397
}
1392
1398
1393
- llvm::Constant *VisitPackIndexingExpr (PackIndexingExpr *E, QualType T) {
1399
+ llvm::Constant *VisitPackIndexingExpr (const PackIndexingExpr *E, QualType T) {
1394
1400
return Visit (E->getSelectedExpr (), T);
1395
1401
}
1396
1402
@@ -1696,8 +1702,7 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
1696
1702
1697
1703
if (!destType->isReferenceType ()) {
1698
1704
QualType nonMemoryDestType = getNonMemoryType (CGM, destType);
1699
- if (llvm::Constant *C = ConstExprEmitter (*this ).Visit (const_cast <Expr *>(E),
1700
- nonMemoryDestType))
1705
+ if (llvm::Constant *C = ConstExprEmitter (*this ).Visit (E, nonMemoryDestType))
1701
1706
return emitForMemory (C, destType);
1702
1707
}
1703
1708
@@ -1777,8 +1782,7 @@ llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
1777
1782
assert (!destType->isVoidType () && " can't emit a void constant" );
1778
1783
1779
1784
if (!destType->isReferenceType ())
1780
- if (llvm::Constant *C =
1781
- ConstExprEmitter (*this ).Visit (const_cast <Expr *>(E), destType))
1785
+ if (llvm::Constant *C = ConstExprEmitter (*this ).Visit (E, destType))
1782
1786
return C;
1783
1787
1784
1788
Expr::EvalResult Result;
@@ -2022,7 +2026,7 @@ ConstantLValue
2022
2026
ConstantLValueEmitter::VisitObjCBoxedExpr (const ObjCBoxedExpr *E) {
2023
2027
assert (E->isExpressibleAsConstantInitializer () &&
2024
2028
" this boxed expression can't be emitted as a compile-time constant" );
2025
- auto *SL = cast<StringLiteral>(E->getSubExpr ()->IgnoreParenCasts ());
2029
+ const auto *SL = cast<StringLiteral>(E->getSubExpr ()->IgnoreParenCasts ());
2026
2030
return emitConstantObjCStringLiteral (SL, E->getType (), CGM);
2027
2031
}
2028
2032
@@ -2048,12 +2052,12 @@ ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
2048
2052
builtin != Builtin::BI__builtin___NSStringMakeConstantString)
2049
2053
return nullptr ;
2050
2054
2051
- auto literal = cast<StringLiteral>(E->getArg (0 )->IgnoreParenCasts ());
2055
+ const auto *Literal = cast<StringLiteral>(E->getArg (0 )->IgnoreParenCasts ());
2052
2056
if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
2053
- return CGM.getObjCRuntime ().GenerateConstantString (literal );
2057
+ return CGM.getObjCRuntime ().GenerateConstantString (Literal );
2054
2058
} else {
2055
2059
// FIXME: need to deal with UCN conversion issues.
2056
- return CGM.GetAddrOfConstantCFString (literal );
2060
+ return CGM.GetAddrOfConstantCFString (Literal );
2057
2061
}
2058
2062
}
2059
2063
0 commit comments