Skip to content

Commit 60eae88

Browse files
committed
Sink some parsing options into SourceFile::ParsingFlags
Sink the `BuildSyntaxTree` and `CollectParsedTokens` bits into `SourceFile::ParsingFlags`, with a static method to get the parsing options from the lang opts. Also add a parsing flag for enabling the interface hash, which can be used instead of calling `enableInterfaceHash`.
1 parent b5ab29b commit 60eae88

File tree

7 files changed

+68
-61
lines changed

7 files changed

+68
-61
lines changed

include/swift/AST/SourceFile.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class SourceFile final : public FileUnit {
3232
friend class ParseSourceFileRequest;
3333

3434
public:
35-
struct SourceFileSyntaxInfo;
36-
3735
/// Possible attributes for imports in source files.
3836
enum class ImportFlags {
3937
/// The imported module is exposed to anyone who imports the parent module.
@@ -110,12 +108,24 @@ class SourceFile final : public FileUnit {
110108
/// and adjust the client call 'performParseOnly'.
111109
DisablePoundIfEvaluation = 1 << 1,
112110

111+
/// Whether to build a syntax tree.
112+
BuildSyntaxTree = 1 << 2,
113+
114+
/// Whether to save the file's parsed tokens.
115+
CollectParsedTokens = 1 << 3,
116+
117+
/// Whether to compute the interface hash of the file.
118+
EnableInterfaceHash = 1 << 4,
119+
113120
/// Whether to suppress warnings when parsing. This is set for secondary
114121
/// files, as they get parsed multiple times.
115-
SuppressWarnings = 1 << 2
122+
SuppressWarnings = 1 << 5,
116123
};
117124
using ParsingOptions = OptionSet<ParsingFlags>;
118125

126+
/// Retrieve the parsing options specified in the LangOptions.
127+
static ParsingOptions getDefaultParsingOptions(const LangOptions &langOpts);
128+
119129
private:
120130
std::unique_ptr<SourceLookupCache> Cache;
121131
SourceLookupCache &getCache() const;
@@ -313,7 +323,6 @@ class SourceFile final : public FileUnit {
313323
llvm::StringMap<SourceFilePathInfo> getInfoForUsedFilePaths() const;
314324

315325
SourceFile(ModuleDecl &M, SourceFileKind K, Optional<unsigned> bufferID,
316-
bool KeepParsedTokens = false, bool KeepSyntaxTree = false,
317326
ParsingOptions parsingOpts = {}, bool isPrimary = false);
318327

319328
~SourceFile();
@@ -544,13 +553,9 @@ class SourceFile final : public FileUnit {
544553
/// Set the root refinement context for the file.
545554
void setTypeRefinementContext(TypeRefinementContext *TRC);
546555

547-
void enableInterfaceHash() {
548-
assert(!hasInterfaceHash());
549-
InterfaceHash.emplace();
550-
}
551-
556+
/// Whether this file has an interface hash available.
552557
bool hasInterfaceHash() const {
553-
return InterfaceHash.hasValue();
558+
return ParsingOpts.contains(ParsingFlags::EnableInterfaceHash);
554559
}
555560

556561
NullablePtr<llvm::MD5> getInterfaceHashPtr() {
@@ -608,7 +613,8 @@ class SourceFile final : public FileUnit {
608613
/// source file.
609614
Optional<std::vector<Token>> AllCollectedTokens;
610615

611-
std::unique_ptr<SourceFileSyntaxInfo> SyntaxInfo;
616+
/// The root of the syntax tree representing the source file.
617+
std::unique_ptr<syntax::SourceFileSyntax> SyntaxRoot;
612618
};
613619

614620
inline SourceFile::ParsingOptions operator|(SourceFile::ParsingFlags lhs,

lib/AST/Module.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,24 +1077,17 @@ LookupConformanceInModuleRequest::evaluate(
10771077
return ProtocolConformanceRef(conformance);
10781078
}
10791079

1080-
struct SourceFile::SourceFileSyntaxInfo {
1081-
const bool Enable;
1082-
/// The root of the syntax tree representing the source file.
1083-
Optional<syntax::SourceFileSyntax> SyntaxRoot;
1084-
SourceFileSyntaxInfo(bool Enable): Enable(Enable) {}
1085-
};
1086-
10871080
bool SourceFile::hasSyntaxRoot() const {
1088-
return SyntaxInfo->SyntaxRoot.hasValue();
1081+
return ParsingOpts.contains(ParsingFlags::BuildSyntaxTree);
10891082
}
10901083

10911084
syntax::SourceFileSyntax SourceFile::getSyntaxRoot() const {
10921085
assert(hasSyntaxRoot() && "no syntax root is set.");
1093-
return *SyntaxInfo->SyntaxRoot;
1086+
return *SyntaxRoot;
10941087
}
10951088

10961089
void SourceFile::setSyntaxRoot(syntax::SourceFileSyntax &&Root) {
1097-
SyntaxInfo->SyntaxRoot.emplace(Root);
1090+
SyntaxRoot = std::make_unique<syntax::SourceFileSyntax>(std::move(Root));
10981091
}
10991092

11001093
void DirectOperatorLookupRequest::writeDependencySink(
@@ -2216,11 +2209,9 @@ ModuleDecl::computeMagicFileStringMap(bool shouldDiagnose) const {
22162209

22172210
SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
22182211
Optional<unsigned> bufferID,
2219-
bool KeepParsedTokens, bool BuildSyntaxTree,
22202212
ParsingOptions parsingOpts, bool isPrimary)
22212213
: FileUnit(FileUnitKind::Source, M), BufferID(bufferID ? *bufferID : -1),
2222-
ParsingOpts(parsingOpts), IsPrimary(isPrimary), Kind(K),
2223-
SyntaxInfo(new SourceFileSyntaxInfo(BuildSyntaxTree)) {
2214+
ParsingOpts(parsingOpts), IsPrimary(isPrimary), Kind(K) {
22242215
M.getASTContext().addDestructorCleanup(*this);
22252216

22262217
assert(!IsPrimary || M.isMainModule() &&
@@ -2231,7 +2222,11 @@ SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
22312222
assert(!problem && "multiple main files?");
22322223
(void)problem;
22332224
}
2234-
if (KeepParsedTokens) {
2225+
2226+
if (hasInterfaceHash()) {
2227+
InterfaceHash.emplace();
2228+
}
2229+
if (shouldCollectTokens()) {
22352230
AllCollectedTokens = std::vector<Token>();
22362231
}
22372232
}
@@ -2241,25 +2236,29 @@ std::vector<Token> &SourceFile::getTokenVector() {
22412236
return *AllCollectedTokens;
22422237
}
22432238

2239+
SourceFile::ParsingOptions
2240+
SourceFile::getDefaultParsingOptions(const LangOptions &langOpts) {
2241+
ParsingOptions opts;
2242+
if (langOpts.BuildSyntaxTree)
2243+
opts |= ParsingFlags::BuildSyntaxTree;
2244+
if (langOpts.CollectParsedToken)
2245+
opts |= ParsingFlags::CollectParsedTokens;
2246+
return opts;
2247+
}
2248+
22442249
ArrayRef<Token> SourceFile::getAllTokens() const {
22452250
assert(shouldCollectTokens() && "Disabled");
22462251
return *AllCollectedTokens;
22472252
}
22482253

22492254
bool SourceFile::shouldCollectTokens() const {
2250-
switch (Kind) {
2251-
case SourceFileKind::Library:
2252-
case SourceFileKind::Main:
2253-
case SourceFileKind::Interface:
2254-
return (bool)AllCollectedTokens;
2255-
case SourceFileKind::SIL:
2256-
return false;
2257-
}
2258-
llvm_unreachable("unhandled kind");
2255+
return Kind != SourceFileKind::SIL &&
2256+
ParsingOpts.contains(ParsingFlags::CollectParsedTokens);
22592257
}
22602258

22612259
bool SourceFile::shouldBuildSyntaxTree() const {
2262-
return Kind != SourceFileKind::SIL && SyntaxInfo->Enable;
2260+
return Kind != SourceFileKind::SIL &&
2261+
ParsingOpts.contains(ParsingFlags::BuildSyntaxTree);
22632262
}
22642263

22652264
bool SourceFile::hasDelayedBodyParsing() const {

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -902,15 +902,17 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
902902
opts |= SourceFile::ParsingFlags::SuppressWarnings;
903903
}
904904

905-
SourceFile *inputFile = new (*Context)
906-
SourceFile(*mainModule, fileKind, bufferID,
907-
Invocation.getLangOptions().CollectParsedToken,
908-
Invocation.getLangOptions().BuildSyntaxTree, opts, isPrimary);
909-
MainModule->addFile(*inputFile);
910-
905+
// Enable interface hash computation for primaries, but not in WMO, as it's
906+
// only currently needed for incremental mode.
911907
if (isPrimary) {
912-
inputFile->enableInterfaceHash();
908+
opts |= SourceFile::ParsingFlags::EnableInterfaceHash;
913909
}
910+
opts |= SourceFile::getDefaultParsingOptions(getASTContext().LangOpts);
911+
912+
auto *inputFile = new (*Context)
913+
SourceFile(*mainModule, fileKind, bufferID, opts, isPrimary);
914+
MainModule->addFile(*inputFile);
915+
914916
return inputFile;
915917
}
916918

lib/IDE/CompletionInstance.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
328328
registerSILGenRequestFunctions(tmpCtx->evaluator);
329329
ModuleDecl *tmpM = ModuleDecl::create(Identifier(), *tmpCtx);
330330
SourceFile *tmpSF = new (*tmpCtx)
331-
SourceFile(*tmpM, oldSF->Kind, tmpBufferID, /*KeepParsedTokens=*/false,
332-
/*BuildSyntaxTree=*/false, oldSF->getParsingOptions());
333-
tmpSF->enableInterfaceHash();
331+
SourceFile(*tmpM, oldSF->Kind, tmpBufferID, oldSF->getParsingOptions());
334332

335333
// FIXME: Since we don't setup module loaders on the temporary AST context,
336334
// 'canImport()' conditional compilation directive always fails. That causes
@@ -441,10 +439,9 @@ bool CompletionInstance::performCachedOperationIfPossible(
441439
auto &Ctx = oldM->getASTContext();
442440
auto *newM = ModuleDecl::createMainModule(Ctx, oldM->getName(),
443441
oldM->getImplicitImportInfo());
444-
auto *newSF =
445-
new (Ctx) SourceFile(*newM, SourceFileKind::Main, newBufferID);
442+
auto *newSF = new (Ctx) SourceFile(*newM, SourceFileKind::Main, newBufferID,
443+
oldSF->getParsingOptions());
446444
newM->addFile(*newSF);
447-
newSF->enableInterfaceHash();
448445

449446
// Tell the compiler instance we've replaced the main module.
450447
CI.setMainModule(newM);

lib/Parse/Parser.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM,
109109

110110
using namespace swift;
111111
using namespace swift::syntax;
112+
using ParsingFlags = SourceFile::ParsingFlags;
112113

113114
void SILParserStateBase::anchor() { }
114115

@@ -1196,14 +1197,17 @@ struct ParserUnit::Implementation {
11961197
const LangOptions &Opts, const TypeCheckerOptions &TyOpts,
11971198
StringRef ModuleName,
11981199
std::shared_ptr<SyntaxParseActions> spActions)
1199-
: SPActions(std::move(spActions)),
1200-
LangOpts(Opts), TypeCheckerOpts(TyOpts), Diags(SM),
1201-
Ctx(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts, SM, Diags)),
1202-
SF(new (Ctx) SourceFile(
1203-
*ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx), SFKind,
1204-
BufferID, Opts.CollectParsedToken, Opts.BuildSyntaxTree,
1205-
SourceFile::ParsingFlags::DisableDelayedBodies |
1206-
SourceFile::ParsingFlags::DisablePoundIfEvaluation)) {}
1200+
: SPActions(std::move(spActions)), LangOpts(Opts),
1201+
TypeCheckerOpts(TyOpts), Diags(SM),
1202+
Ctx(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts, SM,
1203+
Diags)) {
1204+
auto parsingOpts = SourceFile::getDefaultParsingOptions(LangOpts);
1205+
parsingOpts |= ParsingFlags::DisableDelayedBodies;
1206+
parsingOpts |= ParsingFlags::DisablePoundIfEvaluation;
1207+
1208+
auto *M = ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx);
1209+
SF = new (Ctx) SourceFile(*M, SFKind, BufferID, parsingOpts);
1210+
}
12071211

12081212
~Implementation() {
12091213
// We need to delete the parser before the context so that it can finalize

lib/Sema/SourceLoader.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ ModuleDecl *SourceLoader::loadModule(SourceLoc importLoc,
120120
importMod->setResilienceStrategy(ResilienceStrategy::Resilient);
121121
Ctx.LoadedModules[moduleID.Item] = importMod;
122122

123-
auto *importFile = new (Ctx) SourceFile(*importMod, SourceFileKind::Library,
124-
bufferID,
125-
Ctx.LangOpts.CollectParsedToken,
126-
Ctx.LangOpts.BuildSyntaxTree);
123+
auto *importFile =
124+
new (Ctx) SourceFile(*importMod, SourceFileKind::Library, bufferID,
125+
SourceFile::getDefaultParsingOptions(Ctx.LangOpts));
127126
importMod->addFile(*importFile);
128127
performImportResolution(*importFile);
129128
importMod->setHasResolvedImports();

unittests/AST/TestContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TestContext::TestContext(ShouldDeclareOptionalTypes optionals)
4343
Ctx.LoadedModules[stdlibID] = module;
4444

4545
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
46-
/*buffer*/ None, /*keeps token*/ false);
46+
/*buffer*/ None);
4747
module->addFile(*FileForLookups);
4848

4949
if (optionals == DeclareOptionalTypes) {

0 commit comments

Comments
 (0)