Skip to content

More ASTScope cleanups #34076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions include/swift/AST/ASTScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ class ASTScopeImpl {
public:
virtual NullablePtr<ClosureExpr> getClosureIfClosureScope() const;
virtual ASTContext &getASTContext() const;
virtual NullablePtr<DeclContext> getDeclContext() const;
virtual NullablePtr<Decl> getDeclIfAny() const { return nullptr; };
virtual NullablePtr<Stmt> getStmtIfAny() const { return nullptr; };
virtual NullablePtr<Expr> getExprIfAny() const { return nullptr; };
Expand Down Expand Up @@ -354,7 +353,7 @@ class ASTScopeImpl {

/// Entry point into ASTScopeImpl-land for lookups
static void
unqualifiedLookup(SourceFile *, DeclNameRef, SourceLoc, DeclConsumer);
unqualifiedLookup(SourceFile *, SourceLoc, DeclConsumer);

/// Entry point into ASTScopeImpl-land for labeled statement lookups.
static llvm::SmallVector<LabeledStmt *, 4>
Expand All @@ -366,7 +365,6 @@ class ASTScopeImpl {
#pragma mark - - lookup- starting point
private:
static const ASTScopeImpl *findStartingScopeForLookup(SourceFile *,
const DeclNameRef name,
const SourceLoc where);

protected:
Expand Down Expand Up @@ -487,8 +485,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
void printSpecifics(llvm::raw_ostream &out) const override;

public:
NullablePtr<DeclContext> getDeclContext() const override;

void buildFullyExpandedTree();
void
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals();
Expand All @@ -497,7 +493,8 @@ class ASTSourceFileScope final : public ASTScopeImpl {

const SourceFile *getSourceFile() const override;
NullablePtr<const void> addressForPrinting() const override { return SF; }
bool crossCheckWithAST();

ASTContext &getASTContext() const override;

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
Expand Down Expand Up @@ -684,7 +681,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
// Returns the where clause scope, or the parent if none
virtual ASTScopeImpl *createTrailingWhereClauseScope(ASTScopeImpl *parent,
ScopeCreator &);
NullablePtr<DeclContext> getDeclContext() const override;
virtual NullablePtr<NominalTypeDecl> getCorrespondingNominalTypeDecl() const {
return nullptr;
}
Expand Down Expand Up @@ -829,7 +825,6 @@ class GenericParamScope final : public ASTScopeImpl {

/// Actually holder is always a GenericContext, need to test if
/// ProtocolDecl or SubscriptDecl but will refactor later.
NullablePtr<DeclContext> getDeclContext() const override;
NullablePtr<const void> getReferrent() const override;
std::string getClassName() const override;
SourceRange
Expand Down Expand Up @@ -870,8 +865,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
void printSpecifics(llvm::raw_ostream &out) const override;

public:
virtual NullablePtr<DeclContext> getDeclContext() const override;

virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }

Expand Down Expand Up @@ -906,7 +899,6 @@ class ParameterListScope final : public ASTScopeImpl {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;

NullablePtr<const void> addressForPrinting() const override { return params; }
};
Expand All @@ -929,9 +921,6 @@ class FunctionBodyScope : public ASTScopeImpl {
public:
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override {
return decl;
}
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }

Expand Down Expand Up @@ -959,7 +948,6 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
};
Expand Down Expand Up @@ -1002,7 +990,6 @@ class AttachedPropertyWrapperScope final : public ASTScopeImpl {
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
NullablePtr<const void> addressForPrinting() const override { return decl; }
virtual NullablePtr<DeclContext> getDeclContext() const override;

NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return attr;
Expand Down Expand Up @@ -1101,7 +1088,6 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;

protected:
bool lookupLocalsOrMembers(DeclConsumer) const override;
Expand Down Expand Up @@ -1189,7 +1175,6 @@ class CaptureListScope final : public ASTScopeImpl {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
NullablePtr<DeclContext> getDeclContext() const override;
NullablePtr<Expr> getExprIfAny() const override { return expr; }
Expr *getExpr() const { return expr; }
NullablePtr<const void> getReferrent() const override;
Expand All @@ -1212,9 +1197,6 @@ class ClosureParametersScope final : public ASTScopeImpl {
NullablePtr<ClosureExpr> getClosureIfClosureScope() const override {
return closureExpr;
}
NullablePtr<DeclContext> getDeclContext() const override {
return closureExpr;
}
NullablePtr<Expr> getExprIfAny() const override { return closureExpr; }
Expr *getExpr() const { return closureExpr; }
NullablePtr<const void> getReferrent() const override;
Expand Down Expand Up @@ -1247,9 +1229,6 @@ class TopLevelCodeScope final : public ASTScopeImpl {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override {
return decl;
}
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
NullablePtr<const void> getReferrent() const override;
Expand Down Expand Up @@ -1336,9 +1315,6 @@ class SubscriptDeclScope final : public ASTScopeImpl {
void printSpecifics(llvm::raw_ostream &out) const override;

public:
virtual NullablePtr<DeclContext> getDeclContext() const override {
return decl;
}
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
NullablePtr<const void> getReferrent() const override;
Expand All @@ -1358,7 +1334,6 @@ class EnumElementScope : public ASTScopeImpl {

std::string getClassName() const override;
ASTScopeImpl *expandSpecifically(ScopeCreator &) override;
NullablePtr<DeclContext> getDeclContext() const override { return decl; }
NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }

Expand Down Expand Up @@ -1700,7 +1675,6 @@ class BraceStmtScope final : public AbstractStmtScope {
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;

NullablePtr<ClosureExpr> parentClosureIfAny() const; // public??
Stmt *getStmt() const override { return stmt; }
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/NameLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ class ASTScope {
/// Flesh out the tree for dumping
void buildFullyExpandedTree();

static void unqualifiedLookup(SourceFile *, DeclNameRef, SourceLoc,
static void unqualifiedLookup(SourceFile *, SourceLoc,
namelookup::AbstractASTScopeDeclConsumer &);

/// Entry point to record the visible statement labels from the given
Expand Down
61 changes: 6 additions & 55 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ using namespace ast_scope;
#pragma mark ASTScope

void ASTScope::unqualifiedLookup(
SourceFile *SF, DeclNameRef name, SourceLoc loc,
SourceFile *SF, SourceLoc loc,
namelookup::AbstractASTScopeDeclConsumer &consumer) {
if (auto *s = SF->getASTContext().Stats)
++s->getFrontendCounters().NumASTScopeLookups;
ASTScopeImpl::unqualifiedLookup(SF, name, loc, consumer);
ASTScopeImpl::unqualifiedLookup(SF, loc, consumer);
}

llvm::SmallVector<LabeledStmt *, 4> ASTScope::lookupLabeledStmts(
Expand Down Expand Up @@ -116,62 +116,9 @@ LabeledConditionalStmt *GuardStmtScope::getLabeledConditionalStmt() const {
ASTContext &ASTScopeImpl::getASTContext() const {
if (auto d = getDeclIfAny())
return d.get()->getASTContext();
if (auto dc = getDeclContext())
return dc.get()->getASTContext();
return getParent().get()->getASTContext();
}

#pragma mark getDeclContext

NullablePtr<DeclContext> ASTScopeImpl::getDeclContext() const {
return nullptr;
}

NullablePtr<DeclContext> ASTSourceFileScope::getDeclContext() const {
return NullablePtr<DeclContext>(SF);
}

NullablePtr<DeclContext> GenericTypeOrExtensionScope::getDeclContext() const {
return getGenericContext();
}

NullablePtr<DeclContext> GenericParamScope::getDeclContext() const {
return dyn_cast<DeclContext>(holder);
}

NullablePtr<DeclContext> PatternEntryInitializerScope::getDeclContext() const {
return getPatternEntry().getInitContext();
}

NullablePtr<DeclContext> BraceStmtScope::getDeclContext() const {
return getParent().get()->getDeclContext();
}

NullablePtr<DeclContext>
DefaultArgumentInitializerScope::getDeclContext() const {
auto *dc = decl->getDefaultArgumentInitContext();
ASTScopeAssert(dc, "If scope exists, this must exist");
return dc;
}

// When asked for a loc in an intializer in a capture list, the asked-for
// context is the closure.
NullablePtr<DeclContext> CaptureListScope::getDeclContext() const {
return expr->getClosureBody();
}

NullablePtr<DeclContext> AttachedPropertyWrapperScope::getDeclContext() const {
return decl->getParentPatternBinding()->getInitContext(0);
}

NullablePtr<DeclContext> AbstractFunctionDeclScope::getDeclContext() const {
return decl;
}

NullablePtr<DeclContext> ParameterListScope::getDeclContext() const {
return matchingContext;
}

#pragma mark getClassName

std::string GenericTypeOrExtensionScope::getClassName() const {
Expand Down Expand Up @@ -224,6 +171,10 @@ const SourceFile *ASTScopeImpl::getSourceFile() const {

const SourceFile *ASTSourceFileScope::getSourceFile() const { return SF; }

ASTContext &ASTSourceFileScope::getASTContext() const {
return SF->getASTContext();
}

SourceRange ExtensionScope::getBraces() const { return decl->getBraces(); }

SourceRange NominalTypeScope::getBraces() const { return decl->getBraces(); }
Expand Down
Loading