@@ -511,6 +511,58 @@ bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr(
511
511
return this ->emitArrayElemPtrPop (IndexT, E);
512
512
}
513
513
514
+ template <class Emitter >
515
+ bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
516
+ const Expr *E) {
517
+ assert (E->getType ()->isRecordType ());
518
+ const Record *R = getRecord (E->getType ());
519
+
520
+ unsigned InitIndex = 0 ;
521
+ for (const Expr *Init : Inits) {
522
+ if (!this ->emitDupPtr (E))
523
+ return false ;
524
+
525
+ if (std::optional<PrimType> T = classify (Init)) {
526
+ const Record::Field *FieldToInit = R->getField (InitIndex);
527
+ if (!this ->visit (Init))
528
+ return false ;
529
+ if (!this ->emitInitField (*T, FieldToInit->Offset , E))
530
+ return false ;
531
+ if (!this ->emitPopPtr (E))
532
+ return false ;
533
+ ++InitIndex;
534
+ } else {
535
+ // Initializer for a direct base class.
536
+ if (const Record::Base *B = R->getBase (Init->getType ())) {
537
+ if (!this ->emitGetPtrBasePop (B->Offset , Init))
538
+ return false ;
539
+
540
+ if (!this ->visitInitializer (Init))
541
+ return false ;
542
+
543
+ if (!this ->emitPopPtr (E))
544
+ return false ;
545
+ // Base initializers don't increase InitIndex, since they don't count
546
+ // into the Record's fields.
547
+ } else {
548
+ const Record::Field *FieldToInit = R->getField (InitIndex);
549
+ // Non-primitive case. Get a pointer to the field-to-initialize
550
+ // on the stack and recurse into visitInitializer().
551
+ if (!this ->emitGetPtrField (FieldToInit->Offset , Init))
552
+ return false ;
553
+
554
+ if (!this ->visitInitializer (Init))
555
+ return false ;
556
+
557
+ if (!this ->emitPopPtr (E))
558
+ return false ;
559
+ ++InitIndex;
560
+ }
561
+ }
562
+ }
563
+ return true ;
564
+ }
565
+
514
566
template <class Emitter >
515
567
bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
516
568
// Handle discarding first.
@@ -530,56 +582,8 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
530
582
}
531
583
532
584
QualType T = E->getType ();
533
- if (T->isRecordType ()) {
534
- const Record *R = getRecord (T);
535
-
536
- unsigned InitIndex = 0 ;
537
- for (const Expr *Init : E->inits ()) {
538
- if (!this ->emitDupPtr (E))
539
- return false ;
540
-
541
- if (std::optional<PrimType> T = classify (Init)) {
542
- const Record::Field *FieldToInit = R->getField (InitIndex);
543
- if (!this ->visit (Init))
544
- return false ;
545
-
546
- if (!this ->emitInitField (*T, FieldToInit->Offset , E))
547
- return false ;
548
-
549
- if (!this ->emitPopPtr (E))
550
- return false ;
551
- ++InitIndex;
552
- } else {
553
- // Initializer for a direct base class.
554
- if (const Record::Base *B = R->getBase (Init->getType ())) {
555
- if (!this ->emitGetPtrBasePop (B->Offset , Init))
556
- return false ;
557
-
558
- if (!this ->visitInitializer (Init))
559
- return false ;
560
-
561
- if (!this ->emitPopPtr (E))
562
- return false ;
563
- // Base initializers don't increase InitIndex, since they don't count
564
- // into the Record's fields.
565
- } else {
566
- const Record::Field *FieldToInit = R->getField (InitIndex);
567
- // Non-primitive case. Get a pointer to the field-to-initialize
568
- // on the stack and recurse into visitInitializer().
569
- if (!this ->emitGetPtrField (FieldToInit->Offset , Init))
570
- return false ;
571
-
572
- if (!this ->visitInitializer (Init))
573
- return false ;
574
-
575
- if (!this ->emitPopPtr (E))
576
- return false ;
577
- ++InitIndex;
578
- }
579
- }
580
- }
581
- return true ;
582
- }
585
+ if (T->isRecordType ())
586
+ return this ->visitInitList (E->inits (), E);
583
587
584
588
if (T->isArrayType ()) {
585
589
// FIXME: Array fillers.
@@ -612,6 +616,21 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
612
616
return false ;
613
617
}
614
618
619
+ template <class Emitter >
620
+ bool ByteCodeExprGen<Emitter>::VisitCXXParenListInitExpr(
621
+ const CXXParenListInitExpr *E) {
622
+ if (DiscardResult) {
623
+ for (const Expr *Init : E->getInitExprs ()) {
624
+ if (!this ->discard (Init))
625
+ return false ;
626
+ }
627
+ return true ;
628
+ }
629
+
630
+ assert (E->getType ()->isRecordType ());
631
+ return this ->visitInitList (E->getInitExprs (), E);
632
+ }
633
+
615
634
template <class Emitter >
616
635
bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(
617
636
const SubstNonTypeTemplateParmExpr *E) {
@@ -1383,6 +1402,62 @@ bool ByteCodeExprGen<Emitter>::VisitCXXConstructExpr(
1383
1402
return false ;
1384
1403
}
1385
1404
1405
+ template <class Emitter >
1406
+ bool ByteCodeExprGen<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
1407
+ if (DiscardResult)
1408
+ return true ;
1409
+
1410
+ const APValue Val =
1411
+ E->EvaluateInContext (Ctx.getASTContext (), SourceLocDefaultExpr);
1412
+
1413
+ // Things like __builtin_LINE().
1414
+ if (E->getType ()->isIntegerType ()) {
1415
+ assert (Val.isInt ());
1416
+ const APSInt &I = Val.getInt ();
1417
+ return this ->emitConst (I, E);
1418
+ }
1419
+ // Otherwise, the APValue is an LValue, with only one element.
1420
+ // Theoretically, we don't need the APValue at all of course.
1421
+ assert (E->getType ()->isPointerType ());
1422
+ assert (Val.isLValue ());
1423
+ const APValue::LValueBase &Base = Val.getLValueBase ();
1424
+ if (const Expr *LValueExpr = Base.dyn_cast <const Expr *>())
1425
+ return this ->visit (LValueExpr);
1426
+
1427
+ // Otherwise, we have a decl (which is the case for
1428
+ // __builtin_source_location).
1429
+ assert (Base.is <const ValueDecl *>());
1430
+ assert (Val.getLValuePath ().size () == 0 );
1431
+ const auto *BaseDecl = Base.dyn_cast <const ValueDecl *>();
1432
+ assert (BaseDecl);
1433
+
1434
+ auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
1435
+
1436
+ std::optional<unsigned > GlobalIndex = P.getOrCreateGlobal (UGCD);
1437
+ if (!GlobalIndex)
1438
+ return false ;
1439
+
1440
+ if (!this ->emitGetPtrGlobal (*GlobalIndex, E))
1441
+ return false ;
1442
+
1443
+ const Record *R = getRecord (E->getType ());
1444
+ const APValue &V = UGCD->getValue ();
1445
+ for (unsigned I = 0 , N = R->getNumFields (); I != N; ++I) {
1446
+ const Record::Field *F = R->getField (I);
1447
+ const APValue &FieldValue = V.getStructField (I);
1448
+
1449
+ PrimType FieldT = classifyPrim (F->Decl ->getType ());
1450
+
1451
+ if (!this ->visitAPValue (FieldValue, FieldT, E))
1452
+ return false ;
1453
+ if (!this ->emitInitField (FieldT, F->Offset , E))
1454
+ return false ;
1455
+ }
1456
+
1457
+ // Leave the pointer to the global on the stack.
1458
+ return true ;
1459
+ }
1460
+
1386
1461
template <class Emitter > bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
1387
1462
if (E->containsErrors ())
1388
1463
return false ;
@@ -1694,8 +1769,8 @@ bool ByteCodeExprGen<Emitter>::dereferenceVar(
1694
1769
1695
1770
template <class Emitter >
1696
1771
template <typename T>
1697
- bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
1698
- switch (classifyPrim (E-> getType ()) ) {
1772
+ bool ByteCodeExprGen<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
1773
+ switch (Ty ) {
1699
1774
case PT_Sint8:
1700
1775
return this ->emitConstSint8 (Value, E);
1701
1776
case PT_Uint8:
@@ -1724,10 +1799,22 @@ bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
1724
1799
}
1725
1800
1726
1801
template <class Emitter >
1727
- bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
1802
+ template <typename T>
1803
+ bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
1804
+ return this ->emitConst (Value, classifyPrim (E->getType ()), E);
1805
+ }
1806
+
1807
+ template <class Emitter >
1808
+ bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
1809
+ const Expr *E) {
1728
1810
if (Value.isSigned ())
1729
- return this ->emitConst (Value.getSExtValue (), E);
1730
- return this ->emitConst (Value.getZExtValue (), E);
1811
+ return this ->emitConst (Value.getSExtValue (), Ty, E);
1812
+ return this ->emitConst (Value.getZExtValue (), Ty, E);
1813
+ }
1814
+
1815
+ template <class Emitter >
1816
+ bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
1817
+ return this ->emitConst (Value, classifyPrim (E->getType ()), E);
1731
1818
}
1732
1819
1733
1820
template <class Emitter >
@@ -1923,6 +2010,22 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
1923
2010
return false ;
1924
2011
}
1925
2012
2013
+ template <class Emitter >
2014
+ bool ByteCodeExprGen<Emitter>::visitAPValue(const APValue &Val,
2015
+ PrimType ValType, const Expr *E) {
2016
+ assert (!DiscardResult);
2017
+ if (Val.isInt ())
2018
+ return this ->emitConst (Val.getInt (), ValType, E);
2019
+
2020
+ if (Val.isLValue ()) {
2021
+ APValue::LValueBase Base = Val.getLValueBase ();
2022
+ if (const Expr *BaseExpr = Base.dyn_cast <const Expr *>())
2023
+ return this ->visit (BaseExpr);
2024
+ }
2025
+
2026
+ return false ;
2027
+ }
2028
+
1926
2029
template <class Emitter >
1927
2030
bool ByteCodeExprGen<Emitter>::VisitBuiltinCallExpr(const CallExpr *E) {
1928
2031
const Function *Func = getFunction (E->getDirectCallee ());
@@ -2054,14 +2157,16 @@ bool ByteCodeExprGen<Emitter>::VisitCXXDefaultInitExpr(
2054
2157
return this ->visitInitializer (E->getExpr ());
2055
2158
2056
2159
assert (classify (E->getType ()));
2160
+ SourceLocScope<Emitter> SLS (this , E);
2057
2161
return this ->visit (E->getExpr ());
2058
2162
}
2059
2163
2060
2164
template <class Emitter >
2061
2165
bool ByteCodeExprGen<Emitter>::VisitCXXDefaultArgExpr(
2062
2166
const CXXDefaultArgExpr *E) {
2063
- const Expr *SubExpr = E-> getExpr ( );
2167
+ SourceLocScope<Emitter> SLS ( this , E );
2064
2168
2169
+ const Expr *SubExpr = E->getExpr ();
2065
2170
if (std::optional<PrimType> T = classify (E->getExpr ()))
2066
2171
return this ->visit (SubExpr);
2067
2172
0 commit comments