@@ -29,15 +29,11 @@ namespace interp {
29
29
template <class Emitter > class DeclScope final : public VariableScope<Emitter> {
30
30
public:
31
31
DeclScope (ByteCodeExprGen<Emitter> *Ctx, const ValueDecl *VD)
32
- : VariableScope<Emitter>(Ctx), Scope(Ctx->P, VD),
32
+ : VariableScope<Emitter>(Ctx, nullptr ), Scope(Ctx->P, VD),
33
33
OldGlobalDecl (Ctx->GlobalDecl) {
34
34
Ctx->GlobalDecl = Context::shouldBeGloballyIndexed (VD);
35
35
}
36
36
37
- void addExtended (const Scope::Local &Local) override {
38
- return this ->addLocal (Local);
39
- }
40
-
41
37
~DeclScope () { this ->Ctx ->GlobalDecl = OldGlobalDecl; }
42
38
43
39
private:
@@ -85,8 +81,7 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
85
81
std::optional<PrimType> SubExprT = classify (SubExpr->getType ());
86
82
// Prepare storage for the result.
87
83
if (!Initializing && !SubExprT) {
88
- std::optional<unsigned > LocalIndex =
89
- allocateLocal (SubExpr, /* IsExtended=*/ false );
84
+ std::optional<unsigned > LocalIndex = allocateLocal (SubExpr);
90
85
if (!LocalIndex)
91
86
return false ;
92
87
if (!this ->emitGetPtrLocal (*LocalIndex, CE))
@@ -362,8 +357,7 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
362
357
// We're creating a complex value here, so we need to
363
358
// allocate storage for it.
364
359
if (!Initializing) {
365
- std::optional<unsigned > LocalIndex =
366
- allocateLocal (CE, /* IsExtended=*/ true );
360
+ std::optional<unsigned > LocalIndex = allocateLocal (CE);
367
361
if (!LocalIndex)
368
362
return false ;
369
363
if (!this ->emitGetPtrLocal (*LocalIndex, CE))
@@ -390,8 +384,7 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
390
384
return this ->discard (SubExpr);
391
385
392
386
if (!Initializing) {
393
- std::optional<unsigned > LocalIndex =
394
- allocateLocal (CE, /* IsExtended=*/ true );
387
+ std::optional<unsigned > LocalIndex = allocateLocal (CE);
395
388
if (!LocalIndex)
396
389
return false ;
397
390
if (!this ->emitGetPtrLocal (*LocalIndex, CE))
@@ -492,7 +485,7 @@ bool ByteCodeExprGen<Emitter>::VisitImaginaryLiteral(
492
485
return true ;
493
486
494
487
if (!Initializing) {
495
- std::optional<unsigned > LocalIndex = allocateLocal (E, /* IsExtended= */ false );
488
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
496
489
if (!LocalIndex)
497
490
return false ;
498
491
if (!this ->emitGetPtrLocal (*LocalIndex, E))
@@ -561,7 +554,7 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
561
554
562
555
// We need a temporary variable holding our return value.
563
556
if (!Initializing) {
564
- std::optional<unsigned > ResultIndex = this ->allocateLocal (BO, false );
557
+ std::optional<unsigned > ResultIndex = this ->allocateLocal (BO);
565
558
if (!this ->emitGetPtrLocal (*ResultIndex, BO))
566
559
return false ;
567
560
}
@@ -784,7 +777,7 @@ template <class Emitter>
784
777
bool ByteCodeExprGen<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
785
778
// Prepare storage for result.
786
779
if (!Initializing) {
787
- std::optional<unsigned > LocalIndex = allocateLocal (E, /* IsExtended= */ false );
780
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
788
781
if (!LocalIndex)
789
782
return false ;
790
783
if (!this ->emitGetPtrLocal (*LocalIndex, E))
@@ -1841,11 +1834,12 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator(
1841
1834
template <class Emitter >
1842
1835
bool ByteCodeExprGen<Emitter>::VisitExprWithCleanups(
1843
1836
const ExprWithCleanups *E) {
1837
+ ExprScope<Emitter> ES (this );
1844
1838
const Expr *SubExpr = E->getSubExpr ();
1845
1839
1846
1840
assert (E->getNumObjects () == 0 && " TODO: Implement cleanups" );
1847
1841
1848
- return this ->delegate (SubExpr);
1842
+ return this ->delegate (SubExpr) && ES. destroyLocals () ;
1849
1843
}
1850
1844
1851
1845
template <class Emitter >
@@ -1910,9 +1904,8 @@ bool ByteCodeExprGen<Emitter>::VisitMaterializeTemporaryExpr(
1910
1904
return this ->emitGetPtrLocal (LocalIndex, E);
1911
1905
} else {
1912
1906
const Expr *Inner = E->getSubExpr ()->skipRValueSubobjectAdjustments ();
1913
-
1914
1907
if (std::optional<unsigned > LocalIndex =
1915
- allocateLocal (Inner, /* IsExtended= */ true )) {
1908
+ allocateLocal (Inner, E-> getExtendingDecl () )) {
1916
1909
if (!this ->emitGetPtrLocal (*LocalIndex, E))
1917
1910
return false ;
1918
1911
return this ->visitInitializer (SubExpr);
@@ -2119,8 +2112,7 @@ bool ByteCodeExprGen<Emitter>::VisitCXXConstructExpr(
2119
2112
// to allocate a variable and call the constructor and destructor.
2120
2113
if (DiscardResult) {
2121
2114
assert (!Initializing);
2122
- std::optional<unsigned > LocalIndex =
2123
- allocateLocal (E, /* IsExtended=*/ true );
2115
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
2124
2116
2125
2117
if (!LocalIndex)
2126
2118
return false ;
@@ -2300,8 +2292,7 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
2300
2292
2301
2293
if (const auto *CT = Ty->getAs <ComplexType>()) {
2302
2294
if (!Initializing) {
2303
- std::optional<unsigned > LocalIndex =
2304
- allocateLocal (E, /* IsExtended=*/ false );
2295
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
2305
2296
if (!LocalIndex)
2306
2297
return false ;
2307
2298
if (!this ->emitGetPtrLocal (*LocalIndex, E))
@@ -2324,8 +2315,7 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
2324
2315
if (const auto *VT = Ty->getAs <VectorType>()) {
2325
2316
// FIXME: Code duplication with the _Complex case above.
2326
2317
if (!Initializing) {
2327
- std::optional<unsigned > LocalIndex =
2328
- allocateLocal (E, /* IsExtended=*/ false );
2318
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
2329
2319
if (!LocalIndex)
2330
2320
return false ;
2331
2321
if (!this ->emitGetPtrLocal (*LocalIndex, E))
@@ -2513,7 +2503,7 @@ template <class Emitter> bool ByteCodeExprGen<Emitter>::visit(const Expr *E) {
2513
2503
// Create local variable to hold the return value.
2514
2504
if (!E->isGLValue () && !E->getType ()->isAnyComplexType () &&
2515
2505
!classify (E->getType ())) {
2516
- std::optional<unsigned > LocalIndex = allocateLocal (E, /* IsExtended= */ true );
2506
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
2517
2507
if (!LocalIndex)
2518
2508
return false ;
2519
2509
@@ -2767,7 +2757,8 @@ unsigned ByteCodeExprGen<Emitter>::allocateLocalPrimitive(DeclTy &&Src,
2767
2757
2768
2758
template <class Emitter >
2769
2759
std::optional<unsigned >
2770
- ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src, bool IsExtended) {
2760
+ ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src,
2761
+ const ValueDecl *ExtendingDecl) {
2771
2762
// Make sure we don't accidentally register the same decl twice.
2772
2763
if ([[maybe_unused]] const auto *VD =
2773
2764
dyn_cast_if_present<ValueDecl>(Src.dyn_cast <const Decl *>())) {
@@ -2800,7 +2791,10 @@ ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src, bool IsExtended) {
2800
2791
Scope::Local Local = this ->createLocal (D);
2801
2792
if (Key)
2802
2793
Locals.insert ({Key, Local});
2803
- VarScope->add (Local, IsExtended);
2794
+ if (ExtendingDecl)
2795
+ VarScope->addExtended (Local, ExtendingDecl);
2796
+ else
2797
+ VarScope->add (Local, false );
2804
2798
return Local.Offset ;
2805
2799
}
2806
2800
@@ -2835,14 +2829,14 @@ bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *E) {
2835
2829
if (E->getType ()->isVoidType ()) {
2836
2830
if (!visit (E))
2837
2831
return false ;
2838
- return this ->emitRetVoid (E);
2832
+ return this ->emitRetVoid (E) && RootScope. destroyLocals () ;
2839
2833
}
2840
2834
2841
2835
// Expressions with a primitive return type.
2842
2836
if (std::optional<PrimType> T = classify (E)) {
2843
2837
if (!visit (E))
2844
2838
return false ;
2845
- return this ->emitRet (*T, E);
2839
+ return this ->emitRet (*T, E) && RootScope. destroyLocals () ;
2846
2840
}
2847
2841
2848
2842
// Expressions with a composite return type.
@@ -2863,7 +2857,7 @@ bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *E) {
2863
2857
return this ->emitRetValue (E) && RootScope.destroyLocals ();
2864
2858
}
2865
2859
2866
- return false ;
2860
+ return RootScope. destroyLocals () ;
2867
2861
}
2868
2862
2869
2863
// / Toplevel visitDecl().
@@ -2954,7 +2948,7 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
2954
2948
2955
2949
return !Init || initGlobal (*GlobalIndex);
2956
2950
} else {
2957
- VariableScope<Emitter> LocalScope (this );
2951
+ VariableScope<Emitter> LocalScope (this , VD );
2958
2952
if (VarT) {
2959
2953
unsigned Offset = this ->allocateLocalPrimitive (
2960
2954
VD, *VarT, VD->getType ().isConstQualified ());
@@ -2971,6 +2965,7 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
2971
2965
return !Init || this ->visitLocalInitializer (Init, *Offset);
2972
2966
return false ;
2973
2967
}
2968
+
2974
2969
return true ;
2975
2970
}
2976
2971
@@ -3052,7 +3047,7 @@ bool ByteCodeExprGen<Emitter>::VisitBuiltinCallExpr(const CallExpr *E) {
3052
3047
3053
3048
// Non-primitive return type. Prepare storage.
3054
3049
if (!Initializing && !ReturnT && !ReturnType->isVoidType ()) {
3055
- std::optional<unsigned > LocalIndex = allocateLocal (E, /* IsExtended= */ false );
3050
+ std::optional<unsigned > LocalIndex = allocateLocal (E);
3056
3051
if (!LocalIndex)
3057
3052
return false ;
3058
3053
if (!this ->emitGetPtrLocal (*LocalIndex, E))
@@ -3466,8 +3461,7 @@ bool ByteCodeExprGen<Emitter>::VisitComplexUnaryOperator(
3466
3461
std::optional<PrimType> ResT = classify (E);
3467
3462
auto prepareResult = [=]() -> bool {
3468
3463
if (!ResT && !Initializing) {
3469
- std::optional<unsigned > LocalIndex =
3470
- allocateLocal (SubExpr, /* IsExtended=*/ false );
3464
+ std::optional<unsigned > LocalIndex = allocateLocal (SubExpr);
3471
3465
if (!LocalIndex)
3472
3466
return false ;
3473
3467
return this ->emitGetPtrLocal (*LocalIndex, E);
0 commit comments