Skip to content

Commit 19f0d52

Browse files
authored
Merge pull request swiftlang#28995 from CodaFi/absolutely-path-ological
[NFC] Hide SourceFile::Decls
2 parents 3d102b6 + 6784575 commit 19f0d52

25 files changed

+83
-54
lines changed

include/swift/AST/SourceFile.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,38 @@ class SourceFile final : public FileUnit {
125125
/// been validated.
126126
llvm::SetVector<ValueDecl *> UnvalidatedDeclsWithOpaqueReturnTypes;
127127

128+
/// The list of top-level declarations in the source file.
129+
std::vector<Decl *> Decls;
130+
128131
friend ASTContext;
129132
friend Impl;
133+
130134
public:
131-
/// The list of top-level declarations in the source file.
132-
std::vector<Decl*> Decls;
135+
/// Appends the given declaration to the end of the top-level decls list.
136+
void addTopLevelDecl(Decl *d) {
137+
Decls.push_back(d);
138+
}
139+
140+
/// Prepends a declaration to the top-level decls list.
141+
///
142+
/// FIXME: This entrypoint exists to support LLDB. Calls to this function are
143+
/// always a mistake, and additional uses should not be added.
144+
///
145+
/// See rdar://58355191
146+
void prependTopLevelDecl(Decl *d) {
147+
Decls.insert(Decls.begin(), d);
148+
}
149+
150+
/// Retrieves an immutable view of the list of top-level decls in this file.
151+
ArrayRef<Decl *> getTopLevelDecls() const {
152+
return Decls;
153+
}
154+
155+
/// Truncates the list of top-level decls so it contains \c count elements.
156+
void truncateTopLevelDecls(unsigned count) {
157+
assert(count <= Decls.size() && "Can only truncate top-level decls!");
158+
Decls.resize(count);
159+
}
133160

134161
/// A cache of syntax nodes that can be reused when creating the syntax tree
135162
/// for this file.

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ namespace {
794794
PrintWithColorRAII(OS, ASTNodeColor) << "source_file ";
795795
PrintWithColorRAII(OS, LocationColor) << '\"' << SF.getFilename() << '\"';
796796

797-
for (Decl *D : SF.Decls) {
797+
for (Decl *D : SF.getTopLevelDecls()) {
798798
if (D->isImplicit())
799799
continue;
800800

lib/AST/ASTScopeCreation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ AnnotatedInsertionPoint
11931193
ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint(
11941194
ScopeCreator &scopeCreator) {
11951195
ASTScopeAssert(SF, "Must already have a SourceFile.");
1196-
ArrayRef<Decl *> decls = SF->Decls;
1196+
ArrayRef<Decl *> decls = SF->getTopLevelDecls();
11971197
// Assume that decls are only added at the end, in source order
11981198
ArrayRef<Decl *> newDecls = decls.slice(numberOfDeclsAlreadySeen);
11991199
std::vector<ASTNode> newNodes(newDecls.begin(), newDecls.end());
@@ -1865,10 +1865,10 @@ void ASTScopeImpl::beCurrent() {}
18651865
bool ASTScopeImpl::isCurrentIfWasExpanded() const { return true; }
18661866

18671867
void ASTSourceFileScope::beCurrent() {
1868-
numberOfDeclsAlreadySeen = SF->Decls.size();
1868+
numberOfDeclsAlreadySeen = SF->getTopLevelDecls().size();
18691869
}
18701870
bool ASTSourceFileScope::isCurrentIfWasExpanded() const {
1871-
return SF->Decls.size() == numberOfDeclsAlreadySeen;
1871+
return SF->getTopLevelDecls().size() == numberOfDeclsAlreadySeen;
18721872
}
18731873

18741874
void IterableTypeScope::beCurrent() { portion->beCurrent(this); }

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,12 @@ SourceRange ASTSourceFileScope::getSourceRangeOfThisASTNode(
284284
return SourceRange(charRange.getStart(), charRange.getEnd());
285285
}
286286

287-
if (SF->Decls.empty())
287+
if (SF->getTopLevelDecls().empty())
288288
return SourceRange();
289289

290290
// Use the source ranges of the declarations in the file.
291-
return SourceRange(SF->Decls.front()->getStartLoc(),
292-
SF->Decls.back()->getEndLoc());
291+
return SourceRange(SF->getTopLevelDecls().front()->getStartLoc(),
292+
SF->getTopLevelDecls().back()->getEndLoc());
293293
}
294294

295295
SourceRange GenericTypeOrExtensionScope::getSourceRangeOfThisASTNode(

lib/AST/FineGrainedDependenciesSourceFileDepGraphConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct SourceFileDeclFinder {
191191
// clang-format off
192192
SourceFileDeclFinder(const SourceFile *const SF, const bool includePrivateDecls)
193193
: includePrivateDecls(includePrivateDecls) {
194-
for (const Decl *const D : SF->Decls) {
194+
for (const Decl *const D : SF->getTopLevelDecls()) {
195195
select<ExtensionDecl, DeclKind::Extension>(D, extensions, false) ||
196196
select<OperatorDecl, DeclKind::InfixOperator, DeclKind::PrefixOperator,
197197
DeclKind::PostfixOperator>(D, operators, false) ||

lib/AST/Module.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void SourceLookupCache::populateMemberCache(const SourceFile &SF) {
230230

231231
FrontendStatsTracer tracer(SF.getASTContext().Stats,
232232
"populate-source-file-class-member-cache");
233-
addToMemberCache(SF.Decls);
233+
addToMemberCache(SF.getTopLevelDecls());
234234
MemberCachePopulated = true;
235235
}
236236

@@ -243,7 +243,7 @@ void SourceLookupCache::populateMemberCache(const ModuleDecl &Mod) {
243243

244244
for (const FileUnit *file : Mod.getFiles()) {
245245
auto &SF = *cast<SourceFile>(file);
246-
addToMemberCache(SF.Decls);
246+
addToMemberCache(SF.getTopLevelDecls());
247247
}
248248

249249
MemberCachePopulated = true;
@@ -275,15 +275,15 @@ void SourceLookupCache::addToMemberCache(Range decls) {
275275
SourceLookupCache::SourceLookupCache(const SourceFile &SF) {
276276
FrontendStatsTracer tracer(SF.getASTContext().Stats,
277277
"source-file-populate-cache");
278-
addToUnqualifiedLookupCache(SF.Decls, false);
278+
addToUnqualifiedLookupCache(SF.getTopLevelDecls(), false);
279279
}
280280

281281
SourceLookupCache::SourceLookupCache(const ModuleDecl &M) {
282282
FrontendStatsTracer tracer(M.getASTContext().Stats,
283283
"module-populate-cache");
284284
for (const FileUnit *file : M.getFiles()) {
285285
auto &SF = *cast<SourceFile>(file);
286-
addToUnqualifiedLookupCache(SF.Decls, false);
286+
addToUnqualifiedLookupCache(SF.getTopLevelDecls(), false);
287287
}
288288
}
289289

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,7 @@ void FindLocalVal::checkGenericParams(GenericParamList *Params) {
24702470
}
24712471

24722472
void FindLocalVal::checkSourceFile(const SourceFile &SF) {
2473-
for (Decl *D : SF.Decls)
2473+
for (Decl *D : SF.getTopLevelDecls())
24742474
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D))
24752475
visitBraceStmt(TLCD->getBody(), /*isTopLevel=*/true);
24762476
}

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,13 +984,13 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
984984
// For SIL we actually have to interleave parsing and type checking
985985
// because the SIL parser expects to see fully type checked declarations.
986986
if (TheSILModule) {
987-
if (Done || CurTUElem < MainFile.Decls.size()) {
987+
if (Done || CurTUElem < MainFile.getTopLevelDecls().size()) {
988988
assert(mainIsPrimary);
989989
performTypeChecking(MainFile, CurTUElem);
990990
}
991991
}
992992

993-
CurTUElem = MainFile.Decls.size();
993+
CurTUElem = MainFile.getTopLevelDecls().size();
994994
} while (!Done);
995995

996996
if (!TheSILModule) {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static void countStatsOfSourceFile(UnifiedStatsReporter &Stats,
667667
SourceFile *SF) {
668668
auto &C = Stats.getFrontendCounters();
669669
auto &SM = Instance.getSourceMgr();
670-
C.NumDecls += SF->Decls.size();
670+
C.NumDecls += SF->getTopLevelDecls().size();
671671
C.NumLocalTypeDecls += SF->LocalTypeDecls.size();
672672
C.NumObjCMethods += SF->ObjCMethods.size();
673673
C.NumInfixOperators += SF->InfixOperators.size();

lib/FrontendTool/ReferenceDependencies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ ProvidesEmitter::emitTopLevelNames() const {
252252
out << providesTopLevel << ":\n";
253253

254254
CollectedDeclarations cpd;
255-
for (const Decl *D : SF->Decls)
255+
for (const Decl *D : SF->getTopLevelDecls())
256256
emitTopLevelDecl(D, cpd);
257257
for (auto *operatorFunction : cpd.memberOperatorDecls)
258258
out << "- \"" << escape(operatorFunction->getName()) << "\"\n";

0 commit comments

Comments
 (0)