Skip to content

Commit 19f0d52

Browse files
authored
Merge pull request #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";

lib/IDE/CompletionInstance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
8888
auto *parentDC = newDC->getParent();
8989
unsigned N;
9090
if (auto parentSF = dyn_cast<SourceFile>(parentDC))
91-
N = findIndexInRange(D, parentSF->Decls);
91+
N = findIndexInRange(D, parentSF->getTopLevelDecls());
9292
else if (auto parentIDC =
9393
dyn_cast<IterableDeclContext>(parentDC->getAsDecl()))
9494
N = findIndexInRange(D, parentIDC->getMembers());
@@ -114,7 +114,7 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
114114
auto N = IndexStack.pop_back_val();
115115
Decl *D;
116116
if (auto parentSF = dyn_cast<SourceFile>(newDC))
117-
D = getElementAt(parentSF->Decls, N);
117+
D = getElementAt(parentSF->getTopLevelDecls(), N);
118118
else if (auto parentIDC = dyn_cast<IterableDeclContext>(newDC->getAsDecl()))
119119
D = getElementAt(parentIDC->getMembers(), N);
120120
else

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void swift::ide::typeCheckContextUntil(DeclContext *DC, SourceLoc Loc) {
111111
// Here, 'value' is '<error type>' unless we explicitly typecheck the
112112
// 'guard' statement.
113113
SourceFile *SF = DC->getParentSourceFile();
114-
for (auto *D : SF->Decls) {
114+
for (auto *D : SF->getTopLevelDecls()) {
115115
if (auto Code = dyn_cast<TopLevelCodeDecl>(D)) {
116116
typeCheckTopLevelCodeDecl(Code);
117117
if (Code == TLCD)

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static SourceLoc getDeclStartPosition(SourceFile &File) {
667667
return false;
668668
};
669669

670-
for (auto D : File.Decls) {
670+
for (auto D : File.getTopLevelDecls()) {
671671
if (tryUpdateStart(D->getStartLoc())) {
672672
tryUpdateStart(D->getAttrs().getStartLoc());
673673
auto RawComment = D->getRawComment();

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
205205
Ctx.SourceMgr.setCodeCompletionPoint(*BufferID, CodeCompletionOffset);
206206

207207
// Parse, typecheck and temporarily insert the incomplete code into the AST.
208-
const unsigned OriginalDeclCount = SF.Decls.size();
208+
const unsigned OriginalDeclCount = SF.getTopLevelDecls().size();
209209

210210
PersistentParserState PersistentState;
211211
bool Done;
@@ -218,7 +218,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
218218

219219
// Now we are done with code completion. Remove the declarations we
220220
// temporarily inserted.
221-
SF.Decls.resize(OriginalDeclCount);
221+
SF.truncateTopLevelDecls(OriginalDeclCount);
222222

223223
// Reset the error state because it's only relevant to the code that we just
224224
// processed, which now gets thrown away.

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
431431
llvm::SaveAndRestore<SourceFile *> SetCurSourceFile(CurSourceFile, &SF);
432432

433433
// Emit types and other global decls.
434-
for (auto *decl : SF.Decls)
434+
for (auto *decl : SF.getTopLevelDecls())
435435
emitGlobalDecl(decl);
436436
for (auto *localDecl : SF.LocalTypeDecls)
437437
emitGlobalDecl(localDecl);

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ namespace {
175175
DebuggerClient *debug_client = getDebuggerClient();
176176
assert (debug_client);
177177
debug_client->didGlobalize(D);
178-
SF->Decls.push_back(D);
178+
SF->addTopLevelDecl(D);
179179
P.markWasHandled(D);
180180
}
181181
};
@@ -250,7 +250,7 @@ void Parser::parseTopLevel() {
250250
for (auto Item : Items) {
251251
if (auto *D = Item.dyn_cast<Decl*>()) {
252252
assert(!isa<AccessorDecl>(D) && "accessors should not be added here");
253-
SF.Decls.push_back(D);
253+
SF.addTopLevelDecl(D);
254254
}
255255
}
256256

lib/Parse/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void Parser::performCodeCompletionSecondPassImpl(
182182
} else if (auto *ED = dyn_cast<ExtensionDecl>(DC)) {
183183
ED->addMember(D);
184184
} else if (auto *SF = dyn_cast<SourceFile>(DC)) {
185-
SF->Decls.push_back(D);
185+
SF->addTopLevelDecl(D);
186186
} else {
187187
llvm_unreachable("invalid decl context kind");
188188
}

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ class SourceFileScope {
16701670
void SILGenModule::emitSourceFile(SourceFile *sf) {
16711671
SourceFileScope scope(*this, sf);
16721672
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-file", sf);
1673-
for (Decl *D : sf->Decls) {
1673+
for (Decl *D : sf->getTopLevelDecls()) {
16741674
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-decl", D);
16751675
visit(D);
16761676
}

lib/Sema/DebuggerTestingTransform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ void swift::performDebuggerTestingTransform(SourceFile &SF) {
261261
// Walk over all decls in the file to find the next available closure
262262
// discriminator.
263263
DiscriminatorFinder DF;
264-
for (Decl *D : SF.Decls)
264+
for (Decl *D : SF.getTopLevelDecls())
265265
D->walk(DF);
266266

267267
// Instrument the decls with checkExpect() sanity-checks.
268-
for (Decl *D : SF.Decls) {
268+
for (Decl *D : SF.getTopLevelDecls()) {
269269
DebuggerTestingTransform Transform{D->getASTContext(), DF};
270270
D->walk(Transform);
271271
swift::verify(D);

lib/Sema/NameBinding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ void swift::performNameBinding(SourceFile &SF, unsigned StartElem) {
402402

403403
// Make sure we skip adding the standard library imports if the
404404
// source file is empty.
405-
if (SF.ASTStage == SourceFile::NameBound || SF.Decls.empty()) {
405+
if (SF.ASTStage == SourceFile::NameBound || SF.getTopLevelDecls().empty()) {
406406
SF.ASTStage = SourceFile::NameBound;
407407
return;
408408
}
@@ -417,7 +417,7 @@ void swift::performNameBinding(SourceFile &SF, unsigned StartElem) {
417417

418418
// Do a prepass over the declarations to find and load the imported modules
419419
// and map operator decls.
420-
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {
420+
for (auto D : SF.getTopLevelDecls().slice(StartElem)) {
421421
if (auto *ID = dyn_cast<ImportDecl>(D)) {
422422
Binder.addImport(ImportedModules, ID);
423423
} else if (auto *OD = dyn_cast<PrefixOperatorDecl>(D)) {

lib/Sema/PCMacro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ void swift::performPCMacro(SourceFile &SF) {
705705
};
706706

707707
ExpressionFinder EF;
708-
for (Decl *D : SF.Decls) {
708+
for (Decl *D : SF.getTopLevelDecls()) {
709709
D->walk(EF);
710710
}
711711
}

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ void TypeChecker::buildTypeRefinementContextHierarchy(SourceFile &SF,
604604
// Build refinement contexts, if necessary, for all declarations starting
605605
// with StartElem.
606606
TypeRefinementContextBuilder Builder(RootTRC, Context);
607-
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {
607+
for (auto D : SF.getTopLevelDecls().slice(StartElem)) {
608608
Builder.build(D);
609609
}
610610
}
@@ -904,8 +904,9 @@ static const Decl *findContainingDeclaration(SourceRange ReferenceRange,
904904
if (!SF)
905905
return nullptr;
906906

907-
auto BestTopLevelDecl = llvm::find_if(SF->Decls, ContainsReferenceRange);
908-
if (BestTopLevelDecl != SF->Decls.end())
907+
auto BestTopLevelDecl = llvm::find_if(SF->getTopLevelDecls(),
908+
ContainsReferenceRange);
909+
if (BestTopLevelDecl != SF->getTopLevelDecls().end())
909910
return *BestTopLevelDecl;
910911

911912
return nullptr;

0 commit comments

Comments
 (0)