Skip to content

Commit ae4d7ac

Browse files
authored
[flang][driver][nfc] Move the definition of SemanticsContext (llvm#73669)
Moves the defintion of `SemanticsContext` within the Flang driver. Rather than in `CompilerInvocation`, semantic context fits better within `CompilerInstance` that encapsulates the objects that are required to run the frontend. `CompilerInvocation` is better suited for objects encapsulating compiler configuration (e.g. set-up resulting from user input or host set-up).
1 parent 73f8aa0 commit ae4d7ac

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

flang/include/flang/Frontend/CompilerInstance.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class CompilerInstance {
5555

5656
std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> rtTyTables;
5757

58+
std::unique_ptr<Fortran::semantics::SemanticsContext> semaContext;
59+
5860
/// The stream for diagnostics from Semantics
5961
llvm::raw_ostream *semaOutputStream = &llvm::errs();
6062

@@ -121,6 +123,13 @@ class CompilerInstance {
121123
/// @name Semantic analysis
122124
/// {
123125

126+
Fortran::semantics::SemanticsContext &getSemanticsContext() {
127+
return *semaContext;
128+
}
129+
const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
130+
return *semaContext;
131+
}
132+
124133
/// Replace the current stream for verbose output.
125134
void setSemaOutputStream(llvm::raw_ostream &value);
126135

flang/include/flang/Frontend/CompilerInvocation.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ class CompilerInvocation : public CompilerInvocationBase {
8787
// intrinsic of iso_fortran_env.
8888
std::string allCompilerInvocOpts;
8989

90-
// Semantics context
91-
std::unique_ptr<Fortran::semantics::SemanticsContext> semanticsContext;
92-
9390
/// Semantic options
9491
// TODO: Merge with or translate to frontendOpts. We shouldn't need two sets
9592
// of options.
@@ -159,12 +156,9 @@ class CompilerInvocation : public CompilerInvocationBase {
159156
return loweringOpts;
160157
}
161158

162-
Fortran::semantics::SemanticsContext &getSemanticsContext() {
163-
return *semanticsContext;
164-
}
165-
const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
166-
return *semanticsContext;
167-
}
159+
/// Creates and configures semantics context based on the compilation flags.
160+
std::unique_ptr<Fortran::semantics::SemanticsContext>
161+
getSemanticsCtx(Fortran::parser::AllCookedSources &allCookedSources);
168162

169163
std::string &getModuleDir() { return moduleDir; }
170164
const std::string &getModuleDir() const { return moduleDir; }

flang/lib/Frontend/CompilerInstance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ bool CompilerInstance::executeAction(FrontendAction &act) {
156156
invoc.setFortranOpts();
157157
// Set the encoding to read all input files in based on user input.
158158
allSources->set_encoding(invoc.getFortranOpts().encoding);
159-
// Create the semantics context and set semantic options.
160-
invoc.setSemanticsOpts(*this->allCookedSources);
159+
// Create the semantics context
160+
semaContext = invoc.getSemanticsCtx(*allCookedSources);
161161
// Set options controlling lowering to FIR.
162162
invoc.setLoweringOptions();
163163

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,11 +1344,12 @@ void CompilerInvocation::setFortranOpts() {
13441344
fortranOptions.features.WarnOnAllUsage();
13451345
}
13461346

1347-
void CompilerInvocation::setSemanticsOpts(
1347+
std::unique_ptr<Fortran::semantics::SemanticsContext>
1348+
CompilerInvocation::getSemanticsCtx(
13481349
Fortran::parser::AllCookedSources &allCookedSources) {
13491350
auto &fortranOptions = getFortranOpts();
13501351

1351-
semanticsContext = std::make_unique<semantics::SemanticsContext>(
1352+
auto semanticsContext = std::make_unique<semantics::SemanticsContext>(
13521353
getDefaultKinds(), fortranOptions.features, allCookedSources);
13531354

13541355
semanticsContext->set_moduleDirectory(getModuleDir())
@@ -1372,6 +1373,8 @@ void CompilerInvocation::setSemanticsOpts(
13721373

13731374
if (targetTriple.isPPC())
13741375
semanticsContext->targetCharacteristics().set_isPPC(true);
1376+
1377+
return semanticsContext;
13751378
}
13761379

13771380
/// Set \p loweringOptions controlling lowering behavior based

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ bool FrontendAction::runSemanticChecks() {
171171

172172
// Prepare semantics
173173
ci.setSemantics(std::make_unique<Fortran::semantics::Semantics>(
174-
ci.getInvocation().getSemanticsContext(), *parseTree,
174+
ci.getSemanticsContext(), *parseTree,
175175
ci.getInvocation().getDebugModuleDir()));
176176
auto &semantics = ci.getSemantics();
177177

@@ -191,8 +191,7 @@ bool FrontendAction::runSemanticChecks() {
191191
bool FrontendAction::generateRtTypeTables() {
192192
getInstance().setRtTyTables(
193193
std::make_unique<Fortran::semantics::RuntimeDerivedTypeTables>(
194-
BuildRuntimeDerivedTypeTables(
195-
getInstance().getInvocation().getSemanticsContext())));
194+
BuildRuntimeDerivedTypeTables(getInstance().getSemanticsContext())));
196195

197196
// The runtime derived type information table builder may find additional
198197
// semantic errors. Report them.

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,13 @@ bool CodeGenAction::beginSourceFileAction() {
391391

392392
// Create a LoweringBridge
393393
const common::IntrinsicTypeDefaultKinds &defKinds =
394-
ci.getInvocation().getSemanticsContext().defaultKinds();
394+
ci.getSemanticsContext().defaultKinds();
395395
fir::KindMapping kindMap(mlirCtx.get(), llvm::ArrayRef<fir::KindTy>{
396396
fir::fromDefaultKinds(defKinds)});
397397
lower::LoweringBridge lb = Fortran::lower::LoweringBridge::create(
398-
*mlirCtx, ci.getInvocation().getSemanticsContext(), defKinds,
399-
ci.getInvocation().getSemanticsContext().intrinsics(),
400-
ci.getInvocation().getSemanticsContext().targetCharacteristics(),
398+
*mlirCtx, ci.getSemanticsContext(), defKinds,
399+
ci.getSemanticsContext().intrinsics(),
400+
ci.getSemanticsContext().targetCharacteristics(),
401401
ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple,
402402
kindMap, ci.getInvocation().getLoweringOpts(),
403403
ci.getInvocation().getFrontendOpts().envDefaults,
@@ -424,7 +424,7 @@ bool CodeGenAction::beginSourceFileAction() {
424424

425425
// Create a parse tree and lower it to FIR
426426
Fortran::parser::Program &parseTree{*ci.getParsing().parseTree()};
427-
lb.lower(parseTree, ci.getInvocation().getSemanticsContext());
427+
lb.lower(parseTree, ci.getSemanticsContext());
428428

429429
// Add target specific items like dependent libraries, target specific
430430
// constants etc.
@@ -697,8 +697,8 @@ void DebugPreFIRTreeAction::executeAction() {
697697
auto &parseTree{*ci.getParsing().parseTree()};
698698

699699
// Dump pre-FIR tree
700-
if (auto ast{Fortran::lower::createPFT(
701-
parseTree, ci.getInvocation().getSemanticsContext())}) {
700+
if (auto ast{
701+
Fortran::lower::createPFT(parseTree, ci.getSemanticsContext())}) {
702702
Fortran::lower::dumpPFT(llvm::outs(), *ast);
703703
} else {
704704
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
@@ -736,10 +736,8 @@ void GetDefinitionAction::executeAction() {
736736

737737
llvm::outs() << "String range: >" << charBlock->ToString() << "<\n";
738738

739-
auto *symbol{ci.getInvocation()
740-
.getSemanticsContext()
741-
.FindScope(*charBlock)
742-
.FindSymbol(*charBlock)};
739+
auto *symbol{
740+
ci.getSemanticsContext().FindScope(*charBlock).FindSymbol(*charBlock)};
743741
if (!symbol) {
744742
ci.getDiagnostics().Report(diagID);
745743
return;

0 commit comments

Comments
 (0)