22
22
#include " ResultPlan.h"
23
23
#include " Scope.h"
24
24
#include " SpecializedEmitter.h"
25
+ #include " StorageRefResult.h"
25
26
#include " Varargs.h"
26
27
#include " swift/AST/ASTContext.h"
27
28
#include " swift/AST/ConformanceLookup.h"
@@ -3234,105 +3235,6 @@ static void emitDelayedArguments(SILGenFunction &SGF,
3234
3235
}
3235
3236
}
3236
3237
3237
- namespace {
3238
- // / Container to hold the result of a search for the storage reference
3239
- // / when determining to emit a borrow.
3240
- struct StorageRefResult {
3241
- private:
3242
- Expr *storageRef;
3243
- Expr *transitiveRoot;
3244
-
3245
- public:
3246
- // Represents an empty result
3247
- StorageRefResult () : storageRef(nullptr ), transitiveRoot(nullptr ) {}
3248
- bool isEmpty () const { return transitiveRoot == nullptr ; }
3249
- operator bool () const { return !isEmpty (); }
3250
-
3251
- // / The root of the expression that accesses the storage in \c storageRef.
3252
- // / When in doubt, this is probably what you want, as it includes the
3253
- // / entire expression tree involving the reference.
3254
- Expr *getTransitiveRoot () const { return transitiveRoot; }
3255
-
3256
- // / The direct storage reference that was discovered.
3257
- Expr *getStorageRef () const { return storageRef; }
3258
-
3259
- StorageRefResult (Expr *storageRef, Expr *transitiveRoot)
3260
- : storageRef(storageRef), transitiveRoot(transitiveRoot) {
3261
- assert (storageRef && transitiveRoot && " use the zero-arg init for empty" );
3262
- }
3263
-
3264
- // Initializes a storage reference where the base matches the ref.
3265
- StorageRefResult (Expr *storageRef)
3266
- : StorageRefResult(storageRef, storageRef) {}
3267
-
3268
- StorageRefResult withTransitiveRoot (StorageRefResult refResult) const {
3269
- return withTransitiveRoot (refResult.transitiveRoot );
3270
- }
3271
-
3272
- StorageRefResult withTransitiveRoot (Expr *newRoot) const {
3273
- return StorageRefResult (storageRef, newRoot);
3274
- }
3275
- };
3276
- } // namespace
3277
-
3278
- static StorageRefResult findStorageReferenceExprForBorrow (Expr *e) {
3279
- e = e->getSemanticsProvidingExpr ();
3280
-
3281
- // These are basically defined as the cases implemented by SILGenLValue.
3282
-
3283
- // Direct storage references.
3284
- if (auto dre = dyn_cast<DeclRefExpr>(e)) {
3285
- if (isa<VarDecl>(dre->getDecl ()))
3286
- return dre;
3287
- } else if (auto mre = dyn_cast<MemberRefExpr>(e)) {
3288
- if (isa<VarDecl>(mre->getDecl ().getDecl ()))
3289
- return mre;
3290
- } else if (isa<SubscriptExpr>(e)) {
3291
- return e;
3292
- } else if (isa<OpaqueValueExpr>(e)) {
3293
- return e;
3294
- } else if (isa<KeyPathApplicationExpr>(e)) {
3295
- return e;
3296
-
3297
- // Transitive storage references. Look through these to see if the
3298
- // sub-expression is a storage reference, but don't return the
3299
- // sub-expression.
3300
- } else if (auto tue = dyn_cast<TupleElementExpr>(e)) {
3301
- if (auto result = findStorageReferenceExprForBorrow (tue->getBase ()))
3302
- return result.withTransitiveRoot (tue);
3303
-
3304
- } else if (auto fve = dyn_cast<ForceValueExpr>(e)) {
3305
- if (auto result = findStorageReferenceExprForBorrow (fve->getSubExpr ()))
3306
- return result.withTransitiveRoot (fve);
3307
-
3308
- } else if (auto boe = dyn_cast<BindOptionalExpr>(e)) {
3309
- if (auto result = findStorageReferenceExprForBorrow (boe->getSubExpr ()))
3310
- return result.withTransitiveRoot (boe);
3311
-
3312
- } else if (auto oe = dyn_cast<OpenExistentialExpr>(e)) {
3313
- if (findStorageReferenceExprForBorrow (oe->getExistentialValue ()))
3314
- if (auto result = findStorageReferenceExprForBorrow (oe->getSubExpr ()))
3315
- return result.withTransitiveRoot (oe);
3316
-
3317
- } else if (auto bie = dyn_cast<DotSyntaxBaseIgnoredExpr>(e)) {
3318
- if (auto result = findStorageReferenceExprForBorrow (bie->getRHS ()))
3319
- return result.withTransitiveRoot (bie);
3320
-
3321
- } else if (auto te = dyn_cast<AnyTryExpr>(e)) {
3322
- if (auto result = findStorageReferenceExprForBorrow (te->getSubExpr ()))
3323
- return result.withTransitiveRoot (te);
3324
-
3325
- } else if (auto ioe = dyn_cast<InOutExpr>(e)) {
3326
- if (auto result = findStorageReferenceExprForBorrow (ioe->getSubExpr ()))
3327
- return result.withTransitiveRoot (ioe);
3328
- } else if (auto le = dyn_cast<LoadExpr>(e)) {
3329
- if (auto result = findStorageReferenceExprForBorrow (le->getSubExpr ()))
3330
- return result.withTransitiveRoot (le);
3331
- }
3332
-
3333
- return StorageRefResult ();
3334
- }
3335
-
3336
3238
Expr *SILGenFunction::findStorageReferenceExprForMoveOnly (Expr *argExpr,
3337
3239
StorageReferenceOperationKind kind) {
3338
3240
ForceValueExpr *forceUnwrap = nullptr ;
@@ -3387,7 +3289,7 @@ Expr *SILGenFunction::findStorageReferenceExprForMoveOnly(Expr *argExpr,
3387
3289
}
3388
3290
}
3389
3291
3390
- auto result = ::findStorageReferenceExprForBorrow (argExpr);
3292
+ auto result = StorageRefResult ::findStorageReferenceExprForBorrow (argExpr);
3391
3293
3392
3294
if (!result)
3393
3295
return nullptr ;
@@ -3512,8 +3414,9 @@ SILGenFunction::findStorageReferenceExprForBorrowExpr(Expr *argExpr) {
3512
3414
if (!borrowExpr)
3513
3415
return nullptr ;
3514
3416
3515
- return ::findStorageReferenceExprForBorrow (borrowExpr->getSubExpr ())
3516
- .getTransitiveRoot ();
3417
+ return StorageRefResult::findStorageReferenceExprForBorrow (
3418
+ borrowExpr->getSubExpr ())
3419
+ .getTransitiveRoot ();
3517
3420
}
3518
3421
3519
3422
Expr *
@@ -3534,8 +3437,8 @@ Expr *ArgumentSource::findStorageReferenceExprForBorrow() && {
3534
3437
if (!isExpr ()) return nullptr ;
3535
3438
3536
3439
auto argExpr = asKnownExpr ();
3537
- auto *lvExpr =
3538
- ::findStorageReferenceExprForBorrow (argExpr) .getTransitiveRoot();
3440
+ auto *lvExpr = StorageRefResult::findStorageReferenceExprForBorrow (argExpr)
3441
+ .getTransitiveRoot ();
3539
3442
3540
3443
// Claim the value of this argument if we found a storage reference.
3541
3444
if (lvExpr) {
0 commit comments