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