Skip to content

Commit 7d1f024

Browse files
author
David Ungar
committed
WIP unfmt
1 parent 2baa473 commit 7d1f024

File tree

6 files changed

+50
-111
lines changed

6 files changed

+50
-111
lines changed

include/swift/AST/ASTScope.h

+10-45
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class ASTScopeImpl;
7171
class GenericTypeOrExtensionScope;
7272
class IterableTypeScope;
7373
class TypeAliasScope;
74-
class StatementConditionElementPatternScope;
7574
class ScopeCreator;
7675

7776
#pragma mark the root ASTScopeImpl class
@@ -262,7 +261,6 @@ class ASTScopeImpl {
262261

263262
bool isATypeDeclScope() const;
264263

265-
virtual ASTScopeImpl *getStatementConditionIfAny();
266264

267265
#pragma mark - - creation queries
268266
protected:
@@ -1012,15 +1010,15 @@ class ConditionalClauseScope final : public ASTScopeImpl {
10121010
/// The index of the conditional clause.
10131011
const unsigned index;
10141012

1015-
/// The statement after the conditions: body or then.
1016-
Stmt *const stmtAfterAllConditions;
1013+
const SourceLoc useScopeStart;
10171014

1018-
NullablePtr<ASTScopeImpl> statementConditionElementPatternScope;
1015+
/// If I have a let ..., this holds the pattern
1016+
NullablePtr<Pattern> pattern;
10191017

10201018
ConditionalClauseScope(LabeledConditionalStmt *enclosingStmt, unsigned index,
1021-
Stmt *stmtAfterAllConditions)
1019+
SourceLoc useScopeStart)
10221020
: enclosingStmt(enclosingStmt), index(index),
1023-
stmtAfterAllConditions(stmtAfterAllConditions) {}
1021+
useScopeStart(useScopeStart) {}
10241022
virtual ~ConditionalClauseScope() {}
10251023

10261024
ASTScopeImpl *expandMe(ScopeCreator &scopeCreator) override;
@@ -1038,11 +1036,6 @@ class ConditionalClauseScope final : public ASTScopeImpl {
10381036
NullablePtr<const void> addressForPrinting() const override {
10391037
return enclosingStmt;
10401038
}
1041-
void createSubtreeForCondition(ScopeCreator &);
1042-
SourceLoc startLocAccordingToCondition() const;
1043-
1044-
ASTScopeImpl *getStatementConditionIfAny() override;
1045-
10461039
SourceRange getChildlessSourceRange() const override;
10471040
};
10481041

@@ -1051,11 +1044,11 @@ class ConditionalClauseScope final : public ASTScopeImpl {
10511044
/// the normal lookup rule to pass the lookup scope into the deepest conditional
10521045
/// clause.
10531046
class ConditionalClauseUseScope final : public ASTScopeImpl {
1054-
ASTScopeImpl *const lookupParent;
1047+
ConditionalClauseScope *const lookupParent;
10551048
const SourceLoc startLoc;
10561049

10571050
public:
1058-
ConditionalClauseUseScope(ASTScopeImpl *lookupParent, SourceLoc startLoc)
1051+
ConditionalClauseUseScope(ConditionalClauseScope *lookupParent, SourceLoc startLoc)
10591052
: lookupParent(lookupParent), startLoc(startLoc) {}
10601053

10611054
SourceRange getChildlessSourceRange() const override;
@@ -1064,42 +1057,14 @@ class ConditionalClauseUseScope final : public ASTScopeImpl {
10641057
ASTScopeImpl *expandMe(ScopeCreator &) override;
10651058

10661059
protected:
1060+
bool lookupLocalBindings(Optional<bool>, DeclConsumer) const override;
1061+
10671062
void printSpecifics(llvm::raw_ostream &out) const override;
10681063
NullablePtr<const ASTScopeImpl> getLookupParent() const override {
10691064
return lookupParent;
10701065
}
10711066
};
10721067

1073-
/// Within a ConditionalClauseScope, there may be a pattern binding
1074-
/// StmtConditionElement. If so, it splits the scope into two scopes: one
1075-
/// containing the definitions and the other containing the initializer. We must
1076-
/// split it because the initializer must not be in scope of the definitions:
1077-
/// e.g.: if let a = a {}
1078-
/// We need to be able to lookup either a and the second a must not bind to the
1079-
/// first one. This scope represents the scope of the variable being
1080-
/// initialized.
1081-
class StatementConditionElementPatternScope final : public ASTScopeImpl {
1082-
public:
1083-
Pattern *const pattern;
1084-
StatementConditionElementPatternScope(Pattern *e) : pattern(e) {}
1085-
virtual ~StatementConditionElementPatternScope() {}
1086-
1087-
std::string getClassName() const override;
1088-
SourceRange getChildlessSourceRange() const override;
1089-
1090-
ASTScopeImpl *expandMe(ScopeCreator &) override;
1091-
1092-
protected:
1093-
void printSpecifics(llvm::raw_ostream &out) const override;
1094-
1095-
public:
1096-
NullablePtr<const void> addressForPrinting() const override {
1097-
return pattern;
1098-
}
1099-
1100-
protected:
1101-
bool lookupLocalBindings(Optional<bool>, DeclConsumer) const override;
1102-
};
11031068

11041069
/// Capture lists may contain initializer expressions
11051070
/// No local bindings here (other than closures in initializers);
@@ -1336,7 +1301,7 @@ class LabeledConditionalStmtScope : public AbstractStmtScope {
13361301

13371302
protected:
13381303
/// Return the lookupParent required to search these.
1339-
ASTScopeImpl *createCondScopes(ScopeCreator &);
1304+
ASTScopeImpl *createNestedConditionalClauseScopes(ScopeCreator &, SourceLoc lastUseScopeStart);
13401305
virtual Stmt *getStmtAfterTheConditions() const = 0;
13411306
};
13421307

lib/AST/ASTScope.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ DEFINE_GET_CLASS_NAME(PatternEntryInitializerScope)
204204
DEFINE_GET_CLASS_NAME(PatternEntryUseScope)
205205
DEFINE_GET_CLASS_NAME(ConditionalClauseScope)
206206
DEFINE_GET_CLASS_NAME(ConditionalClauseUseScope)
207-
DEFINE_GET_CLASS_NAME(StatementConditionElementPatternScope)
208207
DEFINE_GET_CLASS_NAME(CaptureListScope)
209208
DEFINE_GET_CLASS_NAME(WholeClosureScope)
210209
DEFINE_GET_CLASS_NAME(ClosureParametersScope)

lib/AST/ASTScopeCreation.cpp

+29-41
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ NO_EXPANSION(GenericParamScope)
610610
NO_EXPANSION(ASTSourceFileScope)
611611
NO_EXPANSION(ClosureParametersScope)
612612
NO_EXPANSION(SpecializeAttributeScope)
613+
// no accessors, unlike PatternEntryUseScope
613614
NO_EXPANSION(ConditionalClauseUseScope)
614615
NO_EXPANSION(AttachedPropertyWrapperScope)
615616
NO_EXPANSION(StatementConditionElementPatternScope)
@@ -673,14 +674,27 @@ ASTScopeImpl *PatternEntryUseScope::expandAScopeThatCreatesANewInsertionPoint(
673674
}
674675

675676
ASTScopeImpl *ConditionalClauseScope::expandAScopeThatCreatesANewInsertionPoint(
676-
ScopeCreator &scopeCreator) {
677-
createSubtreeForCondition(scopeCreator);
678-
return this;
677+
ScopeCreator &scopeCreator) {
678+
const auto &cond = enclosingStmt->getCond()[index];
679+
switch (cond.getKind()) {
680+
case StmtConditionElement::CK_Availability:
681+
return this;
682+
case StmtConditionElement::CK_Boolean:
683+
ASTVisitorForScopeCreation().visitExpr(cond.getBoolean(), this,
684+
scopeCreator);
685+
return this;
686+
case StmtConditionElement::CK_PatternBinding:
687+
pattern = cond.getPattern());
688+
ASTVisitorForScopeCreation().visitExpr(cond.getInitializer(), this,
689+
scopeCreator);
690+
return scopeCreator.createSubtree<ConditionalClauseUseScope>(this, this, cond.getEndLoc());
691+
}
679692
}
680693

681694
ASTScopeImpl *GuardStmtScope::expandAScopeThatCreatesANewInsertionPoint(
682695
ScopeCreator &scopeCreator) {
683-
ASTScopeImpl *lookupParent = createCondScopes(scopeCreator);
696+
697+
ASTScopeImpl *lookupParent = createNestedConditionalClauseScopes(scopeCreator, stmt->getBody()->getEndLoc();
684698
// Add a child for the 'guard' body, which always exits.
685699
// Parent is whole guard stmt scope, NOT the cond scopes
686700
scopeCreator.createScopeFor(stmt->getBody(), this);
@@ -746,19 +760,19 @@ void AbstractFunctionBodyScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
746760

747761
void IfStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
748762
ScopeCreator &scopeCreator) {
749-
ASTScopeImpl *lookupParent = createCondScopes(scopeCreator);
763+
ASTScopeImpl *insertionPoint = createNestedConditionalClauseScopes(scopeCreator, stmt->getThenStmt()->getStartLoc());
750764

751765
// The 'then' branch
752-
scopeCreator.createScopeFor(stmt->getThenStmt(), lookupParent);
766+
scopeCreator.createScopeFor(stmt->getThenStmt(), insertionPoint);
753767

754768
// Add the 'else' branch, if needed.
755769
scopeCreator.createScopeFor(stmt->getElseStmt(), this);
756770
}
757771

758772
void WhileStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
759773
ScopeCreator &scopeCreator) {
760-
ASTScopeImpl *lookupParent = createCondScopes(scopeCreator);
761-
scopeCreator.createScopeFor(stmt->getBody(), lookupParent);
774+
ASTScopeImpl *insertionPoint = createNestedConditionalClauseScopes(scopeCreator, stmt->getBody()->getStartLoc());
775+
scopeCreator.createScopeFor(stmt->getBody(), insertionPoint);
762776
}
763777

764778
void RepeatWhileScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
@@ -964,21 +978,16 @@ TypeAliasScope::createTrailingWhereClauseScope(ASTScopeImpl *parent,
964978
#pragma mark misc
965979

966980
ASTScopeImpl *
967-
LabeledConditionalStmtScope::createCondScopes(ScopeCreator &scopeCreator) {
981+
LabeledConditionalStmtScope::createNestedConditionalClauseScopes(ScopeCreator &scopeCreator, SourceLoc lastUseScopeStart) {
968982
auto *stmt = getLabeledConditionalStmt();
969983
ASTScopeImpl *insertionPoint = this;
970-
for (unsigned i = 0; i < stmt->getCond().size(); ++i)
984+
for (unsigned i = 0; i < stmt->getCond().size(); ++i) {
985+
// If cond has a pattern, it needs to know where to start the use scope
986+
const SourceLoc useScopeStart = i + 1 < stmt->getCond().size() ? stmt->getCond()[i+1].getStartLoc() : lastUseStart;
971987
insertionPoint = scopeCreator.createSubtree<ConditionalClauseScope>(
972-
insertionPoint, stmt, i, getStmtAfterTheConditions());
973-
return insertionPoint->getStatementConditionIfAny();
974-
}
975-
976-
ASTScopeImpl *ASTScopeImpl::getStatementConditionIfAny() { return this; }
977-
978-
ASTScopeImpl *ConditionalClauseScope::getStatementConditionIfAny() {
979-
return statementConditionElementPatternScope
980-
? statementConditionElementPatternScope.get()
981-
: this;
988+
insertionPoint, stmt, i, useScopeStart);
989+
}
990+
return insertionPoint;
982991
}
983992

984993
AbstractPatternEntryScope::AbstractPatternEntryScope(
@@ -1002,27 +1011,6 @@ void AbstractPatternEntryScope::forEachVarDeclWithExplicitAccessors(
10021011
});
10031012
}
10041013

1005-
1006-
void ConditionalClauseScope::createSubtreeForCondition(
1007-
ScopeCreator &scopeCreator) {
1008-
const auto &cond = enclosingStmt->getCond()[index];
1009-
switch (cond.getKind()) {
1010-
case StmtConditionElement::CK_Availability:
1011-
return;
1012-
case StmtConditionElement::CK_Boolean:
1013-
ASTVisitorForScopeCreation().visitExpr(cond.getBoolean(), this,
1014-
scopeCreator);
1015-
return;
1016-
case StmtConditionElement::CK_PatternBinding:
1017-
statementConditionElementPatternScope =
1018-
scopeCreator.createSubtree<StatementConditionElementPatternScope>(
1019-
this, cond.getPattern());
1020-
ASTVisitorForScopeCreation().visitExpr(cond.getInitializer(), this,
1021-
scopeCreator);
1022-
return;
1023-
}
1024-
}
1025-
10261014
bool AbstractPatternEntryScope::isLastEntry() const {
10271015
return patternEntryIndex + 1 == decl->getPatternList().size();
10281016
}

lib/AST/ASTScopeLookup.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ bool PatternEntryUseScope::lookupLocalBindings(Optional<bool> isCascadingUse,
342342
consumer);
343343
}
344344

345+
#error use scope stuff here
345346
bool StatementConditionElementPatternScope::lookupLocalBindings(
346347
Optional<bool> isCascadingUse, DeclConsumer consumer) const {
347348
return lookupLocalBindingsInPattern(
@@ -468,6 +469,13 @@ bool ClosureParametersScope::lookupLocalBindings(Optional<bool> isCascadingUse,
468469
return false;
469470
}
470471

472+
bool ConditionalClauseUseScope::lookupLocalBindings(Optional<bool> isCascadingUse, DeclConsumer consumer) const {
473+
if (auto *pattern = lookupParent->pattern.getPtrOrNull()) {
474+
return lookupLocalBindingsInPattern(pattern, isCascadingUse, DeclVisibilityKind::LocalVariable, consumer);
475+
}
476+
return false;
477+
}
478+
471479
bool ASTScopeImpl::lookupLocalBindingsInPattern(Pattern *p,
472480
Optional<bool> isCascadingUse,
473481
DeclVisibilityKind vis,

lib/AST/ASTScopePrinting.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void AbstractPatternEntryScope::printSpecifics(llvm::raw_ostream &out) const {
167167
});
168168
}
169169

170-
170+
#error ruse scope
171171
void StatementConditionElementPatternScope::printSpecifics(
172172
llvm::raw_ostream &out) const {
173173
pattern->print(out);
@@ -178,9 +178,6 @@ void ConditionalClauseScope::printSpecifics(llvm::raw_ostream &out) const {
178178
out << " in ";
179179
printSourceRange(out, enclosingStmt->getSourceRange(), getSourceManager());
180180
out << " index " << index;
181-
out << " before ";
182-
printSourceRange(out, stmtAfterAllConditions->getSourceRange(),
183-
getSourceManager());
184181
out << " ";
185182
}
186183

lib/AST/ASTScopeSourceRange.cpp

+2-20
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ SourceRange AbstractFunctionBodyScope::getChildlessSourceRange() const {
159159
return decl->getBodySourceRange();
160160
}
161161

162-
SourceRange
163-
StatementConditionElementPatternScope::getChildlessSourceRange() const {
164-
return pattern->getSourceRange();
165-
}
166162

167163
SourceRange TopLevelCodeScope::getChildlessSourceRange() const {
168164
return decl->getSourceRange();
@@ -349,26 +345,12 @@ SourceRange BraceStmtScope::getChildlessSourceRange() const {
349345
return stmt->getSourceRange();
350346
}
351347

352-
SourceLoc ConditionalClauseScope::startLocAccordingToCondition() const {
353-
// Determine the start location if the condition can provide it.
354-
auto conditionals = enclosingStmt->getCond();
355-
const StmtConditionElement cond = conditionals[index];
356-
switch (cond.getKind()) {
357-
case StmtConditionElement::CK_Boolean:
358-
case StmtConditionElement::CK_Availability:
359-
return cond.getStartLoc();
360-
case StmtConditionElement::CK_PatternBinding:
361-
return index + 1 < conditionals.size()
362-
? conditionals[index + 1].getStartLoc()
363-
: SourceLoc();
364-
}
365-
}
366348

367349
SourceRange ConditionalClauseScope::getChildlessSourceRange() const {
368350
// From the start of this particular condition to the start of the
369351
// then/body part.
370-
const auto startLoc = startLocAccordingToCondition();
371-
const auto endLoc = stmtAfterAllConditions->getStartLoc();
352+
const auto startLoc = enclosingStmt->getCond()[index].getStartLoc();
353+
const auto endLoc = useScopeStart;
372354
return startLoc.isValid() ? SourceRange(startLoc, endLoc)
373355
: SourceRange(endLoc);
374356
}

0 commit comments

Comments
 (0)