Skip to content

Commit 24c80a3

Browse files
authored
Merge pull request swiftlang#34158 from slavapestov/prepare-to-disable-parser-lookup
Prepare to disable parser lookup
2 parents 72b6a00 + bd36100 commit 24c80a3

39 files changed

+4547
-125
lines changed

include/swift/AST/ASTScope.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,10 +1023,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10231023
public:
10241024
PatternBindingDecl *const decl;
10251025
const unsigned patternEntryIndex;
1026-
const bool isLocalBinding;
10271026

1028-
AbstractPatternEntryScope(PatternBindingDecl *, unsigned entryIndex,
1029-
bool);
1027+
AbstractPatternEntryScope(PatternBindingDecl *, unsigned entryIndex);
10301028
virtual ~AbstractPatternEntryScope() {}
10311029

10321030
const PatternBindingEntry &getPatternEntry() const;
@@ -1041,10 +1039,14 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10411039
};
10421040

10431041
class PatternEntryDeclScope final : public AbstractPatternEntryScope {
1042+
const bool isLocalBinding;
1043+
Optional<SourceLoc> endLoc;
1044+
10441045
public:
10451046
PatternEntryDeclScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1046-
bool isLocalBinding)
1047-
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding) {}
1047+
bool isLocalBinding, Optional<SourceLoc> endLoc)
1048+
: AbstractPatternEntryScope(pbDecl, entryIndex),
1049+
isLocalBinding(isLocalBinding), endLoc(endLoc) {}
10481050
virtual ~PatternEntryDeclScope() {}
10491051

10501052
protected:
@@ -1070,9 +1072,8 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
10701072
Expr *initAsWrittenWhenCreated;
10711073

10721074
public:
1073-
PatternEntryInitializerScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1074-
bool isLocalBinding)
1075-
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding),
1075+
PatternEntryInitializerScope(PatternBindingDecl *pbDecl, unsigned entryIndex)
1076+
: AbstractPatternEntryScope(pbDecl, entryIndex),
10761077
initAsWrittenWhenCreated(pbDecl->getOriginalInit(entryIndex)) {}
10771078
virtual ~PatternEntryInitializerScope() {}
10781079

@@ -1400,7 +1401,8 @@ class WhileStmtScope final : public LabeledConditionalStmtScope {
14001401
class GuardStmtScope final : public LabeledConditionalStmtScope {
14011402
public:
14021403
GuardStmt *const stmt;
1403-
GuardStmtScope(GuardStmt *e) : stmt(e) {}
1404+
SourceLoc endLoc;
1405+
GuardStmtScope(GuardStmt *e, SourceLoc endLoc) : stmt(e), endLoc(endLoc) {}
14041406
virtual ~GuardStmtScope() {}
14051407

14061408
protected:
@@ -1413,6 +1415,8 @@ class GuardStmtScope final : public LabeledConditionalStmtScope {
14131415
public:
14141416
std::string getClassName() const override;
14151417
LabeledConditionalStmt *getLabeledConditionalStmt() const override;
1418+
SourceRange
1419+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
14161420
};
14171421

14181422
/// A scope after a guard statement that follows lookups into the conditions
@@ -1427,9 +1431,11 @@ class LookupParentDiversionScope final : public ASTScopeImpl {
14271431
public:
14281432
ASTScopeImpl *const lookupParent;
14291433
const SourceLoc startLoc;
1434+
const SourceLoc endLoc;
14301435

1431-
LookupParentDiversionScope(ASTScopeImpl *lookupParent, SourceLoc startLoc)
1432-
: lookupParent(lookupParent), startLoc(startLoc) {}
1436+
LookupParentDiversionScope(ASTScopeImpl *lookupParent,
1437+
SourceLoc startLoc, SourceLoc endLoc)
1438+
: lookupParent(lookupParent), startLoc(startLoc), endLoc(endLoc) {}
14331439

14341440
SourceRange
14351441
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;

include/swift/Option/Options.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,11 @@ def scan_clang_dependencies : Flag<["-"], "scan-clang-dependencies">,
11021102

11031103
def disable_parser_lookup : Flag<["-"], "disable-parser-lookup">,
11041104
Flags<[FrontendOption]>,
1105-
HelpText<"Disable parser lookup & use ast scope lookup only (experimental)">;
1105+
HelpText<"Disable parser lookup & use ASTScope lookup only (experimental)">;
1106+
1107+
def enable_parser_lookup : Flag<["-"], "enable-parser-lookup">,
1108+
Flags<[FrontendOption]>,
1109+
HelpText<"Enable parser lookup">;
11061110

11071111
def enable_request_based_incremental_dependencies : Flag<["-"],
11081112
"enable-request-based-incremental-dependencies">,

lib/AST/ASTScopeCreation.cpp

Lines changed: 90 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,17 @@ class ScopeCreator final {
213213

214214
/// Given an array of ASTNodes or Decl pointers, add them
215215
/// Return the resultant insertionPoint
216+
///
217+
/// \param endLoc The end location for any "scopes until the end" that
218+
/// we introduce here, such as PatternEntryDeclScope and GuardStmtScope
216219
ASTScopeImpl *
217220
addSiblingsToScopeTree(ASTScopeImpl *const insertionPoint,
218-
ASTScopeImpl *const organicInsertionPoint,
219-
ArrayRef<ASTNode> nodesOrDeclsToAdd) {
221+
ArrayRef<ASTNode> nodesOrDeclsToAdd,
222+
Optional<SourceLoc> endLoc) {
220223
auto *ip = insertionPoint;
221-
for (auto nd : sortBySourceRange(cull(nodesOrDeclsToAdd))) {
224+
for (auto nd : nodesOrDeclsToAdd) {
222225
auto *const newIP =
223-
addToScopeTreeAndReturnInsertionPoint(nd, ip).getPtrOr(ip);
226+
addToScopeTreeAndReturnInsertionPoint(nd, ip, endLoc).getPtrOr(ip);
224227
ip = newIP;
225228
}
226229
return ip;
@@ -229,12 +232,16 @@ class ScopeCreator final {
229232
public:
230233
/// For each of searching, call this unless the insertion point is needed
231234
void addToScopeTree(ASTNode n, ASTScopeImpl *parent) {
232-
(void)addToScopeTreeAndReturnInsertionPoint(n, parent);
235+
(void)addToScopeTreeAndReturnInsertionPoint(n, parent, None);
233236
}
234237
/// Return new insertion point if the scope was not a duplicate
235238
/// For ease of searching, don't call unless insertion point is needed
239+
///
240+
/// \param endLoc The end location for any "scopes until the end" that
241+
/// we introduce here, such as PatternEntryDeclScope and GuardStmtScope
236242
NullablePtr<ASTScopeImpl>
237-
addToScopeTreeAndReturnInsertionPoint(ASTNode, ASTScopeImpl *parent);
243+
addToScopeTreeAndReturnInsertionPoint(ASTNode, ASTScopeImpl *parent,
244+
Optional<SourceLoc> endLoc);
238245

239246
bool isWorthTryingToCreateScopeFor(ASTNode n) const {
240247
if (!n)
@@ -419,9 +426,18 @@ class ScopeCreator final {
419426
void addChildrenForKnownAttributes(ValueDecl *decl,
420427
ASTScopeImpl *parent);
421428

422-
public:
429+
/// Add PatternEntryDeclScopes for each pattern binding entry.
430+
///
431+
/// Returns the new insertion point.
432+
///
433+
/// \param endLoc Must be valid iff the pattern binding is in a local
434+
/// scope, in which case this is the last source location where the
435+
/// pattern bindings are going to be visible.
436+
NullablePtr<ASTScopeImpl>
437+
addPatternBindingToScopeTree(PatternBindingDecl *patternBinding,
438+
ASTScopeImpl *parent,
439+
Optional<SourceLoc> endLoc);
423440

424-
private:
425441
/// Remove VarDecls because we'll find them when we expand the
426442
/// PatternBindingDecls. Remove EnunCases
427443
/// because they overlap EnumElements and AST includes the elements in the
@@ -463,7 +479,6 @@ class ScopeCreator final {
463479
return -1 == signum;
464480
}
465481

466-
public:
467482
SWIFT_DEBUG_DUMP { print(llvm::errs()); }
468483

469484
void print(raw_ostream &out) const {
@@ -545,7 +560,10 @@ class NodeAdder
545560
: public ASTVisitor<NodeAdder, NullablePtr<ASTScopeImpl>,
546561
NullablePtr<ASTScopeImpl>, NullablePtr<ASTScopeImpl>,
547562
void, void, void, ASTScopeImpl *, ScopeCreator &> {
563+
Optional<SourceLoc> endLoc;
564+
548565
public:
566+
explicit NodeAdder(Optional<SourceLoc> endLoc) : endLoc(endLoc) {}
549567

550568
#pragma mark ASTNodes that do not create scopes
551569

@@ -637,7 +655,9 @@ class NodeAdder
637655
// the deferred nodes.
638656
NullablePtr<ASTScopeImpl> visitGuardStmt(GuardStmt *e, ASTScopeImpl *p,
639657
ScopeCreator &scopeCreator) {
640-
return scopeCreator.ifUniqueConstructExpandAndInsert<GuardStmtScope>(p, e);
658+
ASTScopeAssert(endLoc.hasValue(), "GuardStmt outside of a BraceStmt?");
659+
return scopeCreator.ifUniqueConstructExpandAndInsert<GuardStmtScope>(
660+
p, e, *endLoc);
641661
}
642662
NullablePtr<ASTScopeImpl> visitTopLevelCodeDecl(TopLevelCodeDecl *d,
643663
ASTScopeImpl *p,
@@ -698,28 +718,8 @@ class NodeAdder
698718
visitPatternBindingDecl(PatternBindingDecl *patternBinding,
699719
ASTScopeImpl *parentScope,
700720
ScopeCreator &scopeCreator) {
701-
if (auto *var = patternBinding->getSingleVar())
702-
scopeCreator.addChildrenForKnownAttributes(var, parentScope);
703-
704-
auto *insertionPoint = parentScope;
705-
for (auto i : range(patternBinding->getNumPatternEntries())) {
706-
bool isLocalBinding = false;
707-
if (auto *varDecl = patternBinding->getAnchoringVarDecl(i)) {
708-
isLocalBinding = varDecl->getDeclContext()->isLocalContext();
709-
}
710-
711-
insertionPoint =
712-
scopeCreator
713-
.ifUniqueConstructExpandAndInsert<PatternEntryDeclScope>(
714-
insertionPoint, patternBinding, i, isLocalBinding)
715-
.getPtrOr(insertionPoint);
716-
717-
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope,
718-
"Bindings at the top-level or members of types should "
719-
"not change the insertion point");
720-
}
721-
722-
return insertionPoint;
721+
return scopeCreator.addPatternBindingToScopeTree(
722+
patternBinding, parentScope, endLoc);
723723
}
724724

725725
NullablePtr<ASTScopeImpl> visitEnumElementDecl(EnumElementDecl *eed,
@@ -773,15 +773,18 @@ class NodeAdder
773773
// NodeAdder
774774
NullablePtr<ASTScopeImpl>
775775
ScopeCreator::addToScopeTreeAndReturnInsertionPoint(ASTNode n,
776-
ASTScopeImpl *parent) {
776+
ASTScopeImpl *parent,
777+
Optional<SourceLoc> endLoc) {
777778
if (!isWorthTryingToCreateScopeFor(n))
778779
return parent;
780+
781+
NodeAdder adder(endLoc);
779782
if (auto *p = n.dyn_cast<Decl *>())
780-
return NodeAdder().visit(p, parent, *this);
783+
return adder.visit(p, parent, *this);
781784
if (auto *p = n.dyn_cast<Expr *>())
782-
return NodeAdder().visit(p, parent, *this);
785+
return adder.visit(p, parent, *this);
783786
auto *p = n.get<Stmt *>();
784-
return NodeAdder().visit(p, parent, *this);
787+
return adder.visit(p, parent, *this);
785788
}
786789

787790
void ScopeCreator::addChildrenForAllLocalizableAccessorsInSourceOrder(
@@ -831,6 +834,41 @@ void ScopeCreator::addChildrenForKnownAttributes(ValueDecl *decl,
831834
}
832835
}
833836

837+
NullablePtr<ASTScopeImpl>
838+
ScopeCreator::addPatternBindingToScopeTree(PatternBindingDecl *patternBinding,
839+
ASTScopeImpl *parentScope,
840+
Optional<SourceLoc> endLoc) {
841+
if (auto *var = patternBinding->getSingleVar())
842+
addChildrenForKnownAttributes(var, parentScope);
843+
844+
auto *insertionPoint = parentScope;
845+
for (auto i : range(patternBinding->getNumPatternEntries())) {
846+
bool isLocalBinding = false;
847+
if (auto *varDecl = patternBinding->getAnchoringVarDecl(i)) {
848+
isLocalBinding = varDecl->getDeclContext()->isLocalContext();
849+
}
850+
851+
Optional<SourceLoc> endLocForBinding = None;
852+
if (isLocalBinding) {
853+
endLocForBinding = endLoc;
854+
ASTScopeAssert(endLoc.hasValue() && endLoc->isValid(),
855+
"PatternBindingDecl in local context outside of BraceStmt?");
856+
}
857+
858+
insertionPoint =
859+
ifUniqueConstructExpandAndInsert<PatternEntryDeclScope>(
860+
insertionPoint, patternBinding, i,
861+
isLocalBinding, endLocForBinding)
862+
.getPtrOr(insertionPoint);
863+
864+
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope,
865+
"Bindings at the top-level or members of types should "
866+
"not change the insertion point");
867+
}
868+
869+
return insertionPoint;
870+
}
871+
834872
#pragma mark creation helpers
835873

836874
void ASTScopeImpl::addChild(ASTScopeImpl *child, ASTContext &ctx) {
@@ -950,7 +988,10 @@ ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint(
950988
// Assume that decls are only added at the end, in source order
951989
std::vector<ASTNode> newNodes(decls.begin(), decls.end());
952990
insertionPoint =
953-
scopeCreator.addSiblingsToScopeTree(insertionPoint, this, newNodes);
991+
scopeCreator.addSiblingsToScopeTree(insertionPoint,
992+
scopeCreator.sortBySourceRange(
993+
scopeCreator.cull(newNodes)),
994+
None);
954995
// Too slow to perform all the time:
955996
// ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
956997
// "ASTScope tree missed some DeclContexts or made some up");
@@ -991,7 +1032,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
9911032
"Original inits are always after the '='");
9921033
scopeCreator
9931034
.constructExpandAndInsertUncheckable<PatternEntryInitializerScope>(
994-
this, decl, patternEntryIndex, isLocalBinding);
1035+
this, decl, patternEntryIndex);
9951036
}
9961037

9971038
// Add accessors for the variables in this pattern.
@@ -1050,7 +1091,7 @@ GuardStmtScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
10501091
auto *const lookupParentDiversionScope =
10511092
scopeCreator
10521093
.constructExpandAndInsertUncheckable<LookupParentDiversionScope>(
1053-
this, conditionLookupParent, stmt->getEndLoc());
1094+
this, conditionLookupParent, stmt->getEndLoc(), endLoc);
10541095
return {lookupParentDiversionScope,
10551096
"Succeeding code must be in scope of guard variables"};
10561097
}
@@ -1068,7 +1109,11 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
10681109
// TODO: remove the sort after fixing parser to create brace statement
10691110
// elements in source order
10701111
auto *insertionPoint =
1071-
scopeCreator.addSiblingsToScopeTree(this, this, stmt->getElements());
1112+
scopeCreator.addSiblingsToScopeTree(this,
1113+
scopeCreator.sortBySourceRange(
1114+
scopeCreator.cull(
1115+
stmt->getElements())),
1116+
stmt->getEndLoc());
10721117
if (auto *s = scopeCreator.getASTContext().Stats)
10731118
++s->getFrontendCounters().NumBraceStmtASTScopeExpansions;
10741119
return {
@@ -1082,7 +1127,7 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
10821127

10831128
if (auto *body =
10841129
scopeCreator
1085-
.addToScopeTreeAndReturnInsertionPoint(decl->getBody(), this)
1130+
.addToScopeTreeAndReturnInsertionPoint(decl->getBody(), this, None)
10861131
.getPtrOrNull())
10871132
return {body, "So next top level code scope and put its decls in its body "
10881133
"under a guard statement scope (etc) from the last top level "
@@ -1377,10 +1422,8 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
13771422
}
13781423

13791424
AbstractPatternEntryScope::AbstractPatternEntryScope(
1380-
PatternBindingDecl *declBeingScoped, unsigned entryIndex,
1381-
bool isLocalBinding)
1382-
: decl(declBeingScoped), patternEntryIndex(entryIndex),
1383-
isLocalBinding(isLocalBinding) {
1425+
PatternBindingDecl *declBeingScoped, unsigned entryIndex)
1426+
: decl(declBeingScoped), patternEntryIndex(entryIndex) {
13841427
ASTScopeAssert(entryIndex < declBeingScoped->getPatternList().size(),
13851428
"out of bounds");
13861429
}
@@ -1414,7 +1457,8 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
14141457

14151458
void IterableTypeScope::expandBody(ScopeCreator &scopeCreator) {
14161459
auto nodes = asNodeVector(getIterableDeclContext().get()->getMembers());
1417-
scopeCreator.addSiblingsToScopeTree(this, this, nodes);
1460+
nodes = scopeCreator.sortBySourceRange(scopeCreator.cull(nodes));
1461+
scopeCreator.addSiblingsToScopeTree(this, nodes, None);
14181462
if (auto *s = scopeCreator.getASTContext().Stats)
14191463
++s->getFrontendCounters().NumIterableTypeBodyASTScopeExpansions;
14201464
}

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,13 @@ SourceRange DefaultArgumentInitializerScope::getSourceRangeOfThisASTNode(
248248

249249
SourceRange PatternEntryDeclScope::getSourceRangeOfThisASTNode(
250250
const bool omitAssertions) const {
251-
return getPatternEntry().getSourceRange();
251+
SourceRange range = getPatternEntry().getSourceRange();
252+
if (endLoc.hasValue()) {
253+
ASTScopeAssert(endLoc->isValid(),
254+
"BraceStmt ends before pattern binding entry?");
255+
range.End = *endLoc;
256+
}
257+
return range;
252258
}
253259

254260
SourceRange PatternEntryInitializerScope::getSourceRangeOfThisASTNode(
@@ -445,9 +451,14 @@ SourceRange AttachedPropertyWrapperScope::getSourceRangeOfThisASTNode(
445451
return sourceRangeWhenCreated;
446452
}
447453

454+
SourceRange GuardStmtScope::getSourceRangeOfThisASTNode(
455+
const bool omitAssertions) const {
456+
return SourceRange(getStmt()->getStartLoc(), endLoc);
457+
}
458+
448459
SourceRange LookupParentDiversionScope::getSourceRangeOfThisASTNode(
449460
const bool omitAssertions) const {
450-
return SourceRange(startLoc);
461+
return SourceRange(startLoc, endLoc);
451462
}
452463

453464
#pragma mark source range caching

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
269269
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
270270
inputArgs.AddLastArg(arguments, options::OPT_diagnostic_style);
271271
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);
272+
inputArgs.AddLastArg(arguments, options::OPT_enable_parser_lookup);
272273
inputArgs.AddLastArg(arguments,
273274
options::OPT_enable_experimental_concise_pound_file);
274275
inputArgs.AddLastArg(

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
425425
= A->getOption().matches(OPT_enable_target_os_checking);
426426
}
427427

428-
Opts.DisableParserLookup |= Args.hasArg(OPT_disable_parser_lookup);
428+
Opts.DisableParserLookup |= Args.hasFlag(OPT_disable_parser_lookup,
429+
OPT_enable_parser_lookup,
430+
/*default*/ false);
429431
Opts.EnableNewOperatorLookup = Args.hasFlag(OPT_enable_new_operator_lookup,
430432
OPT_disable_new_operator_lookup,
431433
/*default*/ false);

0 commit comments

Comments
 (0)