diff --git a/flang/include/flang/Frontend/CompilerInstance.h b/flang/include/flang/Frontend/CompilerInstance.h index ed71f038c4dd8..8ed416c2bbbf3 100644 --- a/flang/include/flang/Frontend/CompilerInstance.h +++ b/flang/include/flang/Frontend/CompilerInstance.h @@ -55,6 +55,8 @@ class CompilerInstance { std::unique_ptr rtTyTables; + std::unique_ptr semaContext; + /// The stream for diagnostics from Semantics llvm::raw_ostream *semaOutputStream = &llvm::errs(); @@ -121,6 +123,13 @@ class CompilerInstance { /// @name Semantic analysis /// { + Fortran::semantics::SemanticsContext &getSemanticsContext() { + return *semaContext; + } + const Fortran::semantics::SemanticsContext &getSemanticsContext() const { + return *semaContext; + } + /// Replace the current stream for verbose output. void setSemaOutputStream(llvm::raw_ostream &value); diff --git a/flang/include/flang/Frontend/CompilerInvocation.h b/flang/include/flang/Frontend/CompilerInvocation.h index 229aa75748f72..7ce072cd1ec09 100644 --- a/flang/include/flang/Frontend/CompilerInvocation.h +++ b/flang/include/flang/Frontend/CompilerInvocation.h @@ -87,9 +87,6 @@ class CompilerInvocation : public CompilerInvocationBase { // intrinsic of iso_fortran_env. std::string allCompilerInvocOpts; - // Semantics context - std::unique_ptr semanticsContext; - /// Semantic options // TODO: Merge with or translate to frontendOpts. We shouldn't need two sets // of options. @@ -159,12 +156,9 @@ class CompilerInvocation : public CompilerInvocationBase { return loweringOpts; } - Fortran::semantics::SemanticsContext &getSemanticsContext() { - return *semanticsContext; - } - const Fortran::semantics::SemanticsContext &getSemanticsContext() const { - return *semanticsContext; - } + /// Creates and configures semantics context based on the compilation flags. + std::unique_ptr + getSemanticsCtx(Fortran::parser::AllCookedSources &allCookedSources); std::string &getModuleDir() { return moduleDir; } const std::string &getModuleDir() const { return moduleDir; } diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp index 21ebf52f76410..328b3774bc291 100644 --- a/flang/lib/Frontend/CompilerInstance.cpp +++ b/flang/lib/Frontend/CompilerInstance.cpp @@ -156,8 +156,8 @@ bool CompilerInstance::executeAction(FrontendAction &act) { invoc.setFortranOpts(); // Set the encoding to read all input files in based on user input. allSources->set_encoding(invoc.getFortranOpts().encoding); - // Create the semantics context and set semantic options. - invoc.setSemanticsOpts(*this->allCookedSources); + // Create the semantics context + semaContext = invoc.getSemanticsCtx(*allCookedSources); // Set options controlling lowering to FIR. invoc.setLoweringOptions(); diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 30e53cec9d05d..e7730d50dc1bd 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -1344,11 +1344,12 @@ void CompilerInvocation::setFortranOpts() { fortranOptions.features.WarnOnAllUsage(); } -void CompilerInvocation::setSemanticsOpts( +std::unique_ptr +CompilerInvocation::getSemanticsCtx( Fortran::parser::AllCookedSources &allCookedSources) { auto &fortranOptions = getFortranOpts(); - semanticsContext = std::make_unique( + auto semanticsContext = std::make_unique( getDefaultKinds(), fortranOptions.features, allCookedSources); semanticsContext->set_moduleDirectory(getModuleDir()) @@ -1372,6 +1373,8 @@ void CompilerInvocation::setSemanticsOpts( if (targetTriple.isPPC()) semanticsContext->targetCharacteristics().set_isPPC(true); + + return semanticsContext; } /// Set \p loweringOptions controlling lowering behavior based diff --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp index 02052fc5ae41c..599b4e11f0cfb 100644 --- a/flang/lib/Frontend/FrontendAction.cpp +++ b/flang/lib/Frontend/FrontendAction.cpp @@ -171,7 +171,7 @@ bool FrontendAction::runSemanticChecks() { // Prepare semantics ci.setSemantics(std::make_unique( - ci.getInvocation().getSemanticsContext(), *parseTree, + ci.getSemanticsContext(), *parseTree, ci.getInvocation().getDebugModuleDir())); auto &semantics = ci.getSemantics(); @@ -191,8 +191,7 @@ bool FrontendAction::runSemanticChecks() { bool FrontendAction::generateRtTypeTables() { getInstance().setRtTyTables( std::make_unique( - BuildRuntimeDerivedTypeTables( - getInstance().getInvocation().getSemanticsContext()))); + BuildRuntimeDerivedTypeTables(getInstance().getSemanticsContext()))); // The runtime derived type information table builder may find additional // semantic errors. Report them. diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index 6663548cc81b6..1be95cc27f42c 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -391,13 +391,13 @@ bool CodeGenAction::beginSourceFileAction() { // Create a LoweringBridge const common::IntrinsicTypeDefaultKinds &defKinds = - ci.getInvocation().getSemanticsContext().defaultKinds(); + ci.getSemanticsContext().defaultKinds(); fir::KindMapping kindMap(mlirCtx.get(), llvm::ArrayRef{ fir::fromDefaultKinds(defKinds)}); lower::LoweringBridge lb = Fortran::lower::LoweringBridge::create( - *mlirCtx, ci.getInvocation().getSemanticsContext(), defKinds, - ci.getInvocation().getSemanticsContext().intrinsics(), - ci.getInvocation().getSemanticsContext().targetCharacteristics(), + *mlirCtx, ci.getSemanticsContext(), defKinds, + ci.getSemanticsContext().intrinsics(), + ci.getSemanticsContext().targetCharacteristics(), ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple, kindMap, ci.getInvocation().getLoweringOpts(), ci.getInvocation().getFrontendOpts().envDefaults, @@ -424,7 +424,7 @@ bool CodeGenAction::beginSourceFileAction() { // Create a parse tree and lower it to FIR Fortran::parser::Program &parseTree{*ci.getParsing().parseTree()}; - lb.lower(parseTree, ci.getInvocation().getSemanticsContext()); + lb.lower(parseTree, ci.getSemanticsContext()); // Add target specific items like dependent libraries, target specific // constants etc. @@ -697,8 +697,8 @@ void DebugPreFIRTreeAction::executeAction() { auto &parseTree{*ci.getParsing().parseTree()}; // Dump pre-FIR tree - if (auto ast{Fortran::lower::createPFT( - parseTree, ci.getInvocation().getSemanticsContext())}) { + if (auto ast{ + Fortran::lower::createPFT(parseTree, ci.getSemanticsContext())}) { Fortran::lower::dumpPFT(llvm::outs(), *ast); } else { unsigned diagID = ci.getDiagnostics().getCustomDiagID( @@ -736,10 +736,8 @@ void GetDefinitionAction::executeAction() { llvm::outs() << "String range: >" << charBlock->ToString() << "<\n"; - auto *symbol{ci.getInvocation() - .getSemanticsContext() - .FindScope(*charBlock) - .FindSymbol(*charBlock)}; + auto *symbol{ + ci.getSemanticsContext().FindScope(*charBlock).FindSymbol(*charBlock)}; if (!symbol) { ci.getDiagnostics().Report(diagID); return;