Skip to content

Commit 5908cfc

Browse files
author
David Ungar
committedOct 8, 2019
First cut at ASTScope expansion requestification
1 parent e903595 commit 5908cfc

7 files changed

+79
-2
lines changed
 

‎include/swift/AST/ASTScope.h

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include "swift/AST/ASTNode.h"
3232
#include "swift/AST/NameLookup.h" // for DeclVisibilityKind
33+
// extractNearestSourceLoc, simple_display declarations must come before this
34+
// Alternatively, declare these in SimpleDisplay.h or elsewhere
35+
//#include "swift/AST/SimpleRequest.h"
3336
#include "swift/Basic/Compiler.h"
3437
#include "swift/Basic/LLVM.h"
3538
#include "swift/Basic/NullablePtr.h"
@@ -88,6 +91,12 @@ struct AnnotatedInsertionPoint {
8891
ASTScopeImpl *insertionPoint;
8992
const char *explanation;
9093
};
94+
} // namespace ast_scope
95+
96+
SourceLoc extractNearestSourceLoc(
97+
std::tuple<ast_scope::ASTScopeImpl *, ast_scope::ScopeCreator *>);
98+
99+
namespace ast_scope {
91100

92101
#pragma mark the root ASTScopeImpl class
93102

@@ -333,6 +342,7 @@ class ASTScopeImpl {
333342
public:
334343
/// expandScope me, sending deferred nodes to my descendants.
335344
/// Return the scope into which to place subsequent decls
345+
ASTScopeImpl *expandAndBeCurrentDetectingRecursion(ScopeCreator &);
336346
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);
337347

338348
unsigned getASTAncestorScopeCount() const { return astAncestorScopeCount; }

‎include/swift/AST/NameLookupRequests.h

+26
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class GenericContext;
3030
class GenericParamList;
3131
class TypeAliasDecl;
3232
class TypeDecl;
33+
namespace ast_scope {
34+
class ASTScopeImpl;
35+
class ScopeCreator;
36+
} // namespace ast_scope
3337

3438
/// Display a nominal type or extension thereof.
3539
void simple_display(
@@ -324,6 +328,28 @@ class LookupPrecedenceGroupRequest
324328
bool isCached() const { return true; }
325329
};
326330

331+
/// Expand the given ASTScope. Requestified to detect recursion.
332+
class ExpandASTScopeRequest
333+
: public SimpleRequest<ExpandASTScopeRequest,
334+
ast_scope::ASTScopeImpl *(ast_scope::ASTScopeImpl *,
335+
ast_scope::ScopeCreator *),
336+
CacheKind::Uncached> {
337+
public:
338+
using SimpleRequest::SimpleRequest;
339+
340+
private:
341+
friend SimpleRequest;
342+
343+
// Evaluation.
344+
llvm::Expected<ast_scope::ASTScopeImpl *>
345+
evaluate(Evaluator &evaluator, ast_scope::ASTScopeImpl *,
346+
ast_scope::ScopeCreator *) const;
347+
348+
public:
349+
// Caching
350+
bool isCached() const { return true; }
351+
};
352+
327353
#define SWIFT_TYPEID_ZONE NameLookup
328354
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
329355
#include "swift/Basic/DefineTypeIDZone.h"

‎include/swift/AST/NameLookupTypeIDZone.def

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
1919
NominalTypeDecl *(CustomAttr *, DeclContext *), Cached,
2020
NoLocationInfo)
21+
SWIFT_REQUEST(NameLookup, ExpandASTScopeRequest,
22+
ast_scope::ASTScopeImpl* (ast_scope::ASTScopeImpl*, ast_scope::ScopeCreator*),
23+
Uncached,
24+
NoLocationInfo)
2125
SWIFT_REQUEST(NameLookup, ExtendedNominalRequest,
2226
NominalTypeDecl *(ExtensionDecl *), SeparatelyCached,
2327
NoLocationInfo)

‎include/swift/Basic/SimpleDisplay.h

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ namespace swift {
5454

5555
#undef HAS_TRIVIAL_DISPLAY
5656

57+
namespace ast_scope {
58+
class ASTScopeImpl;
59+
class ScopeCreator;
60+
} // namespace ast_scope
61+
void simple_display(llvm::raw_ostream &out, const ast_scope::ASTScopeImpl *);
62+
void simple_display(llvm::raw_ostream &out, const ast_scope::ScopeCreator *);
63+
5764
template<typename T>
5865
typename std::enable_if<HasTrivialDisplay<T>::value>::type
5966
simple_display(llvm::raw_ostream &out, const T &value) {

‎lib/AST/ASTScopeCreation.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Initializer.h"
2424
#include "swift/AST/LazyResolver.h"
2525
#include "swift/AST/Module.h"
26+
#include "swift/AST/NameLookupRequests.h"
2627
#include "swift/AST/ParameterList.h"
2728
#include "swift/AST/Pattern.h"
2829
#include "swift/AST/SourceFile.h"
@@ -340,7 +341,8 @@ class ScopeCreator final {
340341
if (auto *ip = child->insertionPointForDeferredExpansion().getPtrOrNull())
341342
return ip;
342343
}
343-
ASTScopeImpl *insertionPoint = child->expandAndBeCurrent(*this);
344+
ASTScopeImpl *insertionPoint =
345+
child->expandAndBeCurrentDetectingRecursion(*this);
344346
ASTScopeAssert(child->verifyThatThisNodeComeAfterItsPriorSibling(),
345347
"Ensure search will work");
346348
return insertionPoint;
@@ -1083,6 +1085,18 @@ void ASTScopeImpl::disownDescendants(ScopeCreator &scopeCreator) {
10831085

10841086
#pragma mark implementations of expansion
10851087

1088+
ASTScopeImpl *
1089+
ASTScopeImpl::expandAndBeCurrentDetectingRecursion(ScopeCreator &scopeCreator) {
1090+
return evaluateOrDefault(scopeCreator.getASTContext().evaluator,
1091+
ExpandASTScopeRequest{this, &scopeCreator}, nullptr);
1092+
}
1093+
1094+
llvm::Expected<ASTScopeImpl *>
1095+
ExpandASTScopeRequest::evaluate(Evaluator &evaluator, ASTScopeImpl *parent,
1096+
ScopeCreator *scopeCreator) const {
1097+
return parent->expandAndBeCurrent(*scopeCreator);
1098+
}
1099+
10861100
ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
10871101
auto *insertionPoint = expandSpecifically(scopeCreator);
10881102
if (scopeCreator.shouldBeLazy()) {
@@ -1755,7 +1769,7 @@ void ASTScopeImpl::reexpand(ScopeCreator &scopeCreator) {
17551769
disownDescendants(scopeCreator);
17561770
// If the expansion recurses back into the tree for lookup, the ASTAncestor
17571771
// scopes will have already been rescued and won't be found!
1758-
expandAndBeCurrent(scopeCreator);
1772+
expandAndBeCurrentDetectingRecursion(scopeCreator);
17591773
replaceASTAncestorScopes(astAncestorScopes);
17601774
}
17611775

@@ -2073,3 +2087,8 @@ ScopeCreator::findLocalizableDeclContextsInAST() const {
20732087
bool ASTSourceFileScope::crossCheckWithAST() {
20742088
return scopeCreator->containsAllDeclContextsFromAST();
20752089
}
2090+
2091+
void swift::simple_display(llvm::raw_ostream &out,
2092+
const ast_scope::ScopeCreator *scopeCreator) {
2093+
scopeCreator->print(out);
2094+
}

‎lib/AST/ASTScopePrinting.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,8 @@ bool GenericTypeOrExtensionScope::doesDeclHaveABody() const { return false; }
201201
bool IterableTypeScope::doesDeclHaveABody() const {
202202
return getBraces().Start != getBraces().End;
203203
}
204+
205+
void swift::simple_display(llvm::raw_ostream &out,
206+
const ast_scope::ASTScopeImpl *scope) {
207+
scope->print(out);
208+
}

‎lib/AST/ASTScopeSourceRange.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,9 @@ SourceLoc getLocAfterExtendedNominal(const ExtensionDecl *const ext) {
756756
return Lexer::getCharSourceRangeFromSourceRange(SM, etr->getSourceRange())
757757
.getEnd();
758758
}
759+
760+
SourceLoc swift::extractNearestSourceLoc(
761+
std::tuple<ASTScopeImpl *, ScopeCreator *> scopeAndCreator) {
762+
const ASTScopeImpl *scope = std::get<0>(scopeAndCreator);
763+
return scope->getSourceRangeOfThisASTNode().Start;
764+
}

0 commit comments

Comments
 (0)
Please sign in to comment.