Skip to content

Commit 55047cd

Browse files
committed
Start hiding the interface to PatternBindingEntry
Force callers to go through the pattern binding decl so we can control the API surface more effectively.
1 parent c849652 commit 55047cd

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

include/swift/AST/Decl.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,9 @@ class PatternBindingEntry {
19301930
CaptureInfo Captures;
19311931

19321932
friend class PatternBindingInitializer;
1933+
friend class PatternBindingDecl;
19331934

1935+
private:
19341936
// FIXME: This API is transitional. Once the callers of
19351937
// typeCheckPatternBinding are requestified, merge this bit with
19361938
// Flags::Checked.
@@ -1952,6 +1954,7 @@ class PatternBindingEntry {
19521954
: PatternAndFlags(P, {}), InitExpr({E, E, EqualLoc}),
19531955
InitContextAndFlags({InitContext, None}) {}
19541956

1957+
private:
19551958
Pattern *getPattern() const { return PatternAndFlags.getPointer(); }
19561959
void setPattern(Pattern *P) { PatternAndFlags.setPointer(P); }
19571960

@@ -2210,7 +2213,12 @@ class PatternBindingDecl final : public Decl,
22102213

22112214
/// Can the pattern at index i be default initialized?
22122215
bool isDefaultInitializable(unsigned i) const;
2213-
2216+
2217+
/// Does this pattern have a user-provided initializer expression?
2218+
bool isExplicitlyInitialized(unsigned i) const;
2219+
2220+
SourceLoc getEqualLoc(unsigned i) const;
2221+
22142222
/// When the pattern binding contains only a single variable with no
22152223
/// destructuring, retrieve that variable.
22162224
VarDecl *getSingleVar() const;
@@ -4981,15 +4989,19 @@ class VarDecl : public AbstractStorageDecl {
49814989
/// binding has no initial value, this returns null.
49824990
///
49834991
Expr *getParentInitializer() const {
4984-
if (auto *PBD = getParentPatternBinding())
4985-
return PBD->getPatternEntryForVarDecl(this).getInit();
4992+
if (auto *PBD = getParentPatternBinding()) {
4993+
const auto i = PBD->getPatternEntryIndexForVarDecl(this);
4994+
return PBD->getInit(i);
4995+
}
49864996
return nullptr;
49874997
}
49884998

49894999
/// Whether there exists an initializer for this \c VarDecl.
49905000
bool isParentInitialized() const {
4991-
if (auto *PBD = getParentPatternBinding())
4992-
return PBD->getPatternEntryForVarDecl(this).isInitialized();
5001+
if (auto *PBD = getParentPatternBinding()) {
5002+
const auto i = PBD->getPatternEntryIndexForVarDecl(this);
5003+
return PBD->isInitialized(i);
5004+
}
49935005
return false;
49945006
}
49955007

lib/AST/Decl.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,13 +1735,24 @@ bool PatternBindingDecl::isDefaultInitializable(unsigned i) const {
17351735
return false;
17361736
}
17371737

1738+
17381739
bool PatternBindingDecl::isComputingPatternBindingEntry(
17391740
const VarDecl *vd) const {
17401741
unsigned i = getPatternEntryIndexForVarDecl(vd);
17411742
return getASTContext().evaluator.hasActiveRequest(
17421743
PatternBindingEntryRequest{const_cast<PatternBindingDecl *>(this), i});
17431744
}
17441745

1746+
bool PatternBindingDecl::isExplicitlyInitialized(unsigned i) const {
1747+
const auto entry = getPatternList()[i];
1748+
return entry.isInitialized() && entry.getEqualLoc().isValid();
1749+
}
1750+
1751+
SourceLoc getEqualLoc(unsigned i) const {
1752+
const auto entry = getPatternList()[i];
1753+
return entry.getEqualLoc(i);
1754+
}
1755+
17451756
SourceLoc TopLevelCodeDecl::getStartLoc() const {
17461757
return Body->getStartLoc();
17471758
}
@@ -5282,9 +5293,11 @@ Stmt *VarDecl::getRecursiveParentPatternStmt() const {
52825293
///
52835294
Pattern *VarDecl::getParentPattern() const {
52845295
// If this has a PatternBindingDecl parent, use its pattern.
5285-
if (auto *PBD = getParentPatternBinding())
5286-
return PBD->getPatternEntryForVarDecl(this).getPattern();
5287-
5296+
if (auto *PBD = getParentPatternBinding()) {
5297+
const auto i = PBD->getPatternEntryIndexForVarDecl(this);
5298+
return PBD->getPattern(i);
5299+
}
5300+
52885301
// If this is a statement parent, dig the pattern out of it.
52895302
if (auto *stmt = getParentPatternStmt()) {
52905303
if (auto *FES = dyn_cast<ForEachStmt>(stmt))
@@ -5925,8 +5938,7 @@ Expr *swift::findOriginalPropertyWrapperInitialValue(VarDecl *var,
59255938
return nullptr;
59265939

59275940
// If there is no '=' on the pattern, there was no initial value.
5928-
if (PBD->getPatternList()[0].getEqualLoc().isInvalid()
5929-
&& !PBD->isDefaultInitializable())
5941+
if (PBD->getEqualLoc(0).isInvalid() && !PBD->isDefaultInitializable())
59305942
return nullptr;
59315943

59325944
ASTContext &ctx = var->getASTContext();

0 commit comments

Comments
 (0)