2222#include " ResultPlan.h"
2323#include " Scope.h"
2424#include " SpecializedEmitter.h"
25+ #include " StorageRefResult.h"
2526#include " Varargs.h"
2627#include " swift/AST/ASTContext.h"
2728#include " swift/AST/ConformanceLookup.h"
@@ -3234,105 +3235,6 @@ static void emitDelayedArguments(SILGenFunction &SGF,
32343235 }
32353236}
32363237
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-
33363238Expr *SILGenFunction::findStorageReferenceExprForMoveOnly (Expr *argExpr,
33373239 StorageReferenceOperationKind kind) {
33383240 ForceValueExpr *forceUnwrap = nullptr ;
@@ -3387,7 +3289,7 @@ Expr *SILGenFunction::findStorageReferenceExprForMoveOnly(Expr *argExpr,
33873289 }
33883290 }
33893291
3390- auto result = ::findStorageReferenceExprForBorrow (argExpr);
3292+ auto result = StorageRefResult ::findStorageReferenceExprForBorrow (argExpr);
33913293
33923294 if (!result)
33933295 return nullptr ;
@@ -3512,8 +3414,9 @@ SILGenFunction::findStorageReferenceExprForBorrowExpr(Expr *argExpr) {
35123414 if (!borrowExpr)
35133415 return nullptr ;
35143416
3515- return ::findStorageReferenceExprForBorrow (borrowExpr->getSubExpr ())
3516- .getTransitiveRoot ();
3417+ return StorageRefResult::findStorageReferenceExprForBorrow (
3418+ borrowExpr->getSubExpr ())
3419+ .getTransitiveRoot ();
35173420}
35183421
35193422Expr *
@@ -3534,8 +3437,8 @@ Expr *ArgumentSource::findStorageReferenceExprForBorrow() && {
35343437 if (!isExpr ()) return nullptr ;
35353438
35363439 auto argExpr = asKnownExpr ();
3537- auto *lvExpr =
3538- ::findStorageReferenceExprForBorrow (argExpr) .getTransitiveRoot();
3440+ auto *lvExpr = StorageRefResult::findStorageReferenceExprForBorrow (argExpr)
3441+ .getTransitiveRoot ();
35393442
35403443 // Claim the value of this argument if we found a storage reference.
35413444 if (lvExpr) {
0 commit comments