Skip to content

Commit 6b8c91e

Browse files
committed
[NFC] Move StorageRefResult to a header
1 parent 4f33d52 commit 6b8c91e

File tree

4 files changed

+116
-106
lines changed

4 files changed

+116
-106
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,6 @@ NOTE(lifetime_outside_scope_escape, none,
12251225

12261226
ERROR(noncopyable_shared_case_block_unimplemented, none,
12271227
"matching a non-'Copyable' value using a case label that has multiple patterns is not implemented", ())
1228-
1228+
12291229
#define UNDEFINE_DIAGNOSTIC_MACROS
12301230
#include "DefineDiagnosticMacros.h"

lib/SILGen/SILGenApply.cpp

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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-
33363238
Expr *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

35193422
Expr *
@@ -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) {

lib/SILGen/SILGenStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include "RValue.h"
2121
#include "SILGen.h"
2222
#include "Scope.h"
23+
#include "StorageRefResult.h"
2324
#include "SwitchEnumBuilder.h"
2425
#include "swift/AST/ConformanceLookup.h"
2526
#include "swift/AST/DiagnosticsSIL.h"
2627
#include "swift/Basic/Assertions.h"
2728
#include "swift/Basic/ProfileCounter.h"
28-
#include "swift/SIL/BasicBlockUtils.h"
2929
#include "swift/SIL/AbstractionPatternGenerators.h"
30+
#include "swift/SIL/BasicBlockUtils.h"
3031
#include "swift/SIL/SILArgument.h"
3132
#include "swift/SIL/SILProfiler.h"
3233
#include "llvm/Support/SaveAndRestore.h"

lib/SILGen/StorageRefResult.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#ifndef SWIFT_SILGEN_STORAGEREFRESULT_H
2+
#define SWIFT_SILGEN_STORAGEREFRESULT_H
3+
4+
#include "swift/AST/Expr.h"
5+
6+
namespace swift {
7+
/// Container to hold the result of a search for the storage reference
8+
/// when determining to emit a borrow.
9+
struct StorageRefResult {
10+
private:
11+
Expr *storageRef;
12+
Expr *transitiveRoot;
13+
14+
public:
15+
// Represents an empty result
16+
StorageRefResult() : storageRef(nullptr), transitiveRoot(nullptr) {}
17+
bool isEmpty() const { return transitiveRoot == nullptr; }
18+
operator bool() const { return !isEmpty(); }
19+
20+
/// The root of the expression that accesses the storage in \c storageRef.
21+
/// When in doubt, this is probably what you want, as it includes the
22+
/// entire expression tree involving the reference.
23+
Expr *getTransitiveRoot() const { return transitiveRoot; }
24+
25+
/// The direct storage reference that was discovered.
26+
Expr *getStorageRef() const { return storageRef; }
27+
28+
StorageRefResult(Expr *storageRef, Expr *transitiveRoot)
29+
: storageRef(storageRef), transitiveRoot(transitiveRoot) {
30+
assert(storageRef && transitiveRoot && "use the zero-arg init for empty");
31+
}
32+
33+
// Initializes a storage reference where the base matches the ref.
34+
StorageRefResult(Expr *storageRef)
35+
: StorageRefResult(storageRef, storageRef) {}
36+
37+
StorageRefResult withTransitiveRoot(StorageRefResult refResult) const {
38+
return withTransitiveRoot(refResult.transitiveRoot);
39+
}
40+
41+
StorageRefResult withTransitiveRoot(Expr *newRoot) const {
42+
return StorageRefResult(storageRef, newRoot);
43+
}
44+
45+
static StorageRefResult findStorageReferenceExprForBorrow(Expr *e) {
46+
e = e->getSemanticsProvidingExpr();
47+
48+
// These are basically defined as the cases implemented by SILGenLValue.
49+
50+
// Direct storage references.
51+
if (auto dre = dyn_cast<DeclRefExpr>(e)) {
52+
if (isa<VarDecl>(dre->getDecl()))
53+
return dre;
54+
} else if (auto mre = dyn_cast<MemberRefExpr>(e)) {
55+
if (isa<VarDecl>(mre->getDecl().getDecl()))
56+
return mre;
57+
} else if (isa<SubscriptExpr>(e)) {
58+
return e;
59+
} else if (isa<OpaqueValueExpr>(e)) {
60+
return e;
61+
} else if (isa<KeyPathApplicationExpr>(e)) {
62+
return e;
63+
64+
// Transitive storage references. Look through these to see if the
65+
// sub-expression is a storage reference, but don't return the
66+
// sub-expression.
67+
} else if (auto tue = dyn_cast<TupleElementExpr>(e)) {
68+
if (auto result = findStorageReferenceExprForBorrow(tue->getBase()))
69+
return result.withTransitiveRoot(tue);
70+
71+
} else if (auto fve = dyn_cast<ForceValueExpr>(e)) {
72+
if (auto result = findStorageReferenceExprForBorrow(fve->getSubExpr()))
73+
return result.withTransitiveRoot(fve);
74+
75+
} else if (auto boe = dyn_cast<BindOptionalExpr>(e)) {
76+
if (auto result = findStorageReferenceExprForBorrow(boe->getSubExpr()))
77+
return result.withTransitiveRoot(boe);
78+
79+
} else if (auto oe = dyn_cast<OpenExistentialExpr>(e)) {
80+
if (findStorageReferenceExprForBorrow(oe->getExistentialValue()))
81+
if (auto result = findStorageReferenceExprForBorrow(oe->getSubExpr()))
82+
return result.withTransitiveRoot(oe);
83+
84+
} else if (auto bie = dyn_cast<DotSyntaxBaseIgnoredExpr>(e)) {
85+
if (auto result = findStorageReferenceExprForBorrow(bie->getRHS()))
86+
return result.withTransitiveRoot(bie);
87+
88+
} else if (auto te = dyn_cast<AnyTryExpr>(e)) {
89+
if (auto result = findStorageReferenceExprForBorrow(te->getSubExpr()))
90+
return result.withTransitiveRoot(te);
91+
92+
} else if (auto ioe = dyn_cast<InOutExpr>(e)) {
93+
if (auto result = findStorageReferenceExprForBorrow(ioe->getSubExpr()))
94+
return result.withTransitiveRoot(ioe);
95+
} else if (auto le = dyn_cast<LoadExpr>(e)) {
96+
if (auto result = findStorageReferenceExprForBorrow(le->getSubExpr()))
97+
return result.withTransitiveRoot(le);
98+
}
99+
100+
return StorageRefResult();
101+
}
102+
};
103+
104+
} // namespace swift
105+
106+
#endif

0 commit comments

Comments
 (0)