diff --git a/flang/include/flang/Frontend/CompilerInstance.h b/flang/include/flang/Frontend/CompilerInstance.h index ed71f038c4dd85..8ed416c2bbbf3f 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 229aa75748f725..7ce072cd1ec098 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 21ebf52f76410b..328b3774bc2917 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 1c09ae9c281eb4..654eee6a6e6258 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -1335,11 +1335,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()) @@ -1363,6 +1364,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 02052fc5ae41c4..599b4e11f0cfbd 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 f573ac82c91cd8..9a35b396ed383b 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -330,13 +330,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, @@ -363,7 +363,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 dependent libraries addDepdendentLibs(*mlirModule, ci); @@ -634,8 +634,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( @@ -673,10 +673,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;