Skip to content

Commit 8f29f15

Browse files
authored
Merge pull request #70737 from rintaro/ast-localtypedecls
[AST] Requestify local type declarations
2 parents 46f8dfa + f7e35cb commit 8f29f15

File tree

11 files changed

+74
-30
lines changed

11 files changed

+74
-30
lines changed

include/swift/AST/SourceFile.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ class SourceFile final : public FileUnit {
313313
/// includes the SourceFileSyntax node corresponding to this source file.
314314
void *getExportedSourceFile() const;
315315

316-
/// The list of local type declarations in the source file.
317-
llvm::SetVector<TypeDecl *> LocalTypeDecls;
318-
319316
/// Defer type checking of `AFD` to the end of `Sema`
320317
void addDelayedFunction(AbstractFunctionDecl *AFD);
321318

@@ -762,6 +759,8 @@ class SourceFile final : public FileUnit {
762759
/// Returns true if the source file contains concurrency in the top-level
763760
bool isAsyncTopLevelSourceFile() const;
764761

762+
ArrayRef<TypeDecl *> getLocalTypeDecls() const;
763+
765764
private:
766765

767766
/// If not \c None, the underlying vector contains the parsed tokens of this

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,6 +4732,23 @@ class UniqueUnderlyingTypeSubstitutionsRequest
47324732
void cacheResult(llvm::Optional<SubstitutionMap>) const;
47334733
};
47344734

4735+
/// Collect all local type declarations in the SourceFile.
4736+
class LocalTypeDeclsRequest
4737+
: public SimpleRequest<LocalTypeDeclsRequest,
4738+
ArrayRef<TypeDecl *>(SourceFile *),
4739+
RequestFlags::Cached> {
4740+
public:
4741+
using SimpleRequest::SimpleRequest;
4742+
4743+
private:
4744+
friend SimpleRequest;
4745+
4746+
ArrayRef<TypeDecl *> evaluate(Evaluator &evaluator, SourceFile *sf) const;
4747+
4748+
public:
4749+
bool isCached() const { return true; }
4750+
};
4751+
47354752
#define SWIFT_TYPEID_ZONE TypeChecker
47364753
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
47374754
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,6 @@ SWIFT_REQUEST(TypeChecker, SemanticDeclAttrsRequest,
542542
SWIFT_REQUEST(TypeChecker, UniqueUnderlyingTypeSubstitutionsRequest,
543543
llvm::Optional<SubstitutionMap>(const Decl *),
544544
SeparatelyCached, NoLocationInfo)
545+
SWIFT_REQUEST(TypeChecker, LocalTypeDeclsRequest,
546+
ArrayRef<TypeDecl *>(SourceFile *),
547+
Cached, NoLocationInfo)

include/swift/Parse/Parser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,6 @@ class Parser {
10151015
/// 'isLine = true' indicates parsing #line instead of #sourcelocation
10161016
ParserStatus parseLineDirective(bool isLine = false);
10171017

1018-
void recordLocalType(TypeDecl *TD);
1019-
10201018
/// Skip an `#if` configuration block containing only attributes.
10211019
///
10221020
/// \returns true if the skipping was successful, false otherwise.

lib/AST/Decl.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9398,12 +9398,8 @@ void AbstractFunctionDecl::setBodyToBeReparsed(SourceRange bodyRange) {
93989398
BodyRange = bodyRange;
93999399
setBodyKind(BodyKind::Unparsed);
94009400

9401-
// Remove local type decls from the source file.
94029401
if (auto SF = getParentSourceFile()) {
9403-
SF->LocalTypeDecls.remove_if([&](TypeDecl *TD) {
9404-
auto dc = TD->getDeclContext();
9405-
return dc == this || dc->isChildContextOf(this);
9406-
});
9402+
SF->getASTContext().evaluator.clearCachedOutput(LocalTypeDeclsRequest{SF});
94079403
}
94089404
}
94099405

lib/AST/Module.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,8 @@ void SourceFile::getPrecedenceGroups(
14441444
}
14451445

14461446
void SourceFile::getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const {
1447-
Results.append(LocalTypeDecls.begin(), LocalTypeDecls.end());
1447+
auto decls = getLocalTypeDecls();
1448+
Results.append(decls.begin(), decls.end());
14481449
}
14491450

14501451
void
@@ -1456,7 +1457,7 @@ const {
14561457

14571458
TypeDecl *SourceFile::lookupLocalType(llvm::StringRef mangledName) const {
14581459
ASTContext &ctx = getASTContext();
1459-
for (auto typeDecl : LocalTypeDecls) {
1460+
for (auto typeDecl : getLocalTypeDecls()) {
14601461
auto typeMangledName = evaluateOrDefault(ctx.evaluator,
14611462
MangleLocalTypeDeclRequest { typeDecl },
14621463
std::string());
@@ -4245,6 +4246,50 @@ ASTNode GetSourceFileAsyncNode::evaluate(Evaluator &eval,
42454246
return ASTNode();
42464247
}
42474248

4249+
ArrayRef<TypeDecl *> SourceFile::getLocalTypeDecls() const {
4250+
auto *mutableThis = const_cast<SourceFile *>(this);
4251+
return evaluateOrDefault(getASTContext().evaluator,
4252+
LocalTypeDeclsRequest{mutableThis}, {});
4253+
}
4254+
4255+
namespace {
4256+
class LocalTypeDeclCollector : public ASTWalker {
4257+
SmallVectorImpl<TypeDecl *> &results;
4258+
4259+
public:
4260+
LocalTypeDeclCollector(SmallVectorImpl<TypeDecl *> &results)
4261+
: results(results) {}
4262+
4263+
MacroWalking getMacroWalkingBehavior() const override {
4264+
return MacroWalking::Expansion;
4265+
}
4266+
4267+
PreWalkAction walkToDeclPre(Decl *D) override {
4268+
switch (D->getKind()) {
4269+
case DeclKind::Enum:
4270+
case DeclKind::Struct:
4271+
case DeclKind::Class:
4272+
case DeclKind::Protocol:
4273+
case DeclKind::TypeAlias:
4274+
if (D->getDeclContext()->isLocalContext())
4275+
results.push_back(cast<TypeDecl>(D));
4276+
break;
4277+
default:
4278+
break;
4279+
}
4280+
return PreWalkAction::Continue;
4281+
}
4282+
};
4283+
} // namespace
4284+
4285+
ArrayRef<TypeDecl *> LocalTypeDeclsRequest::evaluate(Evaluator &evaluator,
4286+
SourceFile *sf) const {
4287+
SmallVector<TypeDecl *> results;
4288+
LocalTypeDeclCollector collector(results);
4289+
sf->walk(collector);
4290+
return sf->getASTContext().AllocateCopy(results);
4291+
}
4292+
42484293
//===----------------------------------------------------------------------===//
42494294
// SynthesizedFileUnit Implementation
42504295
//===----------------------------------------------------------------------===//

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static void countStatsOfSourceFile(UnifiedStatsReporter &Stats,
280280
auto &C = Stats.getFrontendCounters();
281281
auto &SM = Instance.getSourceMgr();
282282
C.NumDecls += SF->getTopLevelDecls().size();
283-
C.NumLocalTypeDecls += SF->LocalTypeDecls.size();
283+
C.NumLocalTypeDecls += SF->getLocalTypeDecls().size();
284284
C.NumObjCMethods += SF->ObjCMethods.size();
285285

286286
SmallVector<OperatorDecl *, 2> operators;

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
465465
emitGlobalDecl(decl);
466466
for (auto *decl : SF.getHoistedDecls())
467467
emitGlobalDecl(decl);
468-
for (auto *localDecl : SF.LocalTypeDecls)
468+
for (auto *localDecl : SF.getLocalTypeDecls())
469469
emitGlobalDecl(localDecl);
470470
for (auto *opaqueDecl : SF.getOpaqueReturnTypeDecls())
471471
maybeEmitOpaqueTypeDecl(opaqueDecl);

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5728,15 +5728,6 @@ void Parser::consumeDecl(ParserPosition BeginParserPosition, bool IsTopLevel) {
57285728
}
57295729
}
57305730

5731-
void Parser::recordLocalType(TypeDecl *TD) {
5732-
// If we're not in a local context, this is unnecessary.
5733-
if (!TD->getDeclContext()->isLocalContext())
5734-
return;
5735-
5736-
if (!InInactiveClauseEnvironment && !InFreestandingMacroArgument)
5737-
SF.getOutermostParentSourceFile()->LocalTypeDecls.insert(TD);
5738-
}
5739-
57405731
/// Set the original declaration in `@differentiable` attributes.
57415732
///
57425733
/// Necessary because `Parser::parseNewDeclAttribute` (which calls
@@ -7200,7 +7191,6 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
72007191

72017192
auto *TAD = new (Context) TypeAliasDecl(TypeAliasLoc, EqualLoc, Id, IdLoc,
72027193
genericParams, CurDeclContext);
7203-
recordLocalType(TAD);
72047194
ParserResult<TypeRepr> UnderlyingTy;
72057195

72067196
if (Tok.is(tok::colon) || Tok.is(tok::equal)) {
@@ -8885,7 +8875,6 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
88858875

88868876
EnumDecl *ED = new (Context) EnumDecl(EnumLoc, EnumName, EnumNameLoc,
88878877
{ }, GenericParams, CurDeclContext);
8888-
recordLocalType(ED);
88898878
ED->getAttrs() = Attributes;
88908879

88918880
ContextChange CC(*this, ED);
@@ -9152,7 +9141,6 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
91529141
{ },
91539142
GenericParams,
91549143
CurDeclContext);
9155-
recordLocalType(SD);
91569144
SD->getAttrs() = Attributes;
91579145

91589146
ContextChange CC(*this, SD);
@@ -9246,7 +9234,6 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
92469234
ClassDecl *CD = new (Context) ClassDecl(ClassLoc, ClassName, ClassNameLoc,
92479235
{ }, GenericParams, CurDeclContext,
92489236
isExplicitActorDecl);
9249-
recordLocalType(CD);
92509237
CD->getAttrs() = Attributes;
92519238

92529239
// Parsed classes never have missing vtable entries.
@@ -9425,7 +9412,6 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
94259412
ProtocolDecl(CurDeclContext, ProtocolLoc, NameLoc, ProtocolName,
94269413
Context.AllocateCopy(PrimaryAssociatedTypeNames),
94279414
Context.AllocateCopy(InheritedProtocols), TrailingWhere);
9428-
recordLocalType(Proto);
94299415

94309416
Proto->getAttrs() = Attributes;
94319417
if (whereClauseHadCodeCompletion && CodeCompletionCallbacks)

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ class SILGenModuleRAII {
20572057
SGM.visit(D);
20582058
}
20592059

2060-
for (TypeDecl *TD : sf->LocalTypeDecls) {
2060+
for (TypeDecl *TD : sf->getLocalTypeDecls()) {
20612061
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
20622062
"SILgen-tydecl", TD);
20632063
// FIXME: Delayed parsing would prevent these types from being added to

lib/SILGen/SILGenTopLevel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void SILGenTopLevel::visitSourceFile(SourceFile *SF) {
237237
visit(D);
238238
}
239239

240-
for (TypeDecl *TD : SF->LocalTypeDecls) {
240+
for (TypeDecl *TD : SF->getLocalTypeDecls()) {
241241
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
242242
continue;
243243
visit(TD);

0 commit comments

Comments
 (0)