FLANG
CompilerInstance.h
1//===-- CompilerInstance.h - Flang Compiler Instance ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_FRONTEND_COMPILERINSTANCE_H
14#define FORTRAN_FRONTEND_COMPILERINSTANCE_H
15
16#include "flang/Frontend/CompilerInvocation.h"
18#include "flang/Frontend/ParserActions.h"
20#include "flang/Semantics/runtime-type-info.h"
21#include "flang/Semantics/semantics.h"
22#include "flang/Support/StringOstream.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Target/TargetMachine.h"
25
26namespace Fortran::frontend {
27
44
46 std::shared_ptr<CompilerInvocation> invocation;
47
49 std::shared_ptr<Fortran::parser::AllSources> allSources;
50
51 std::shared_ptr<Fortran::parser::AllCookedSources> allCookedSources;
52
53 std::shared_ptr<Fortran::parser::Parsing> parsing;
54
55 std::unique_ptr<Fortran::semantics::Semantics> semantics;
56
57 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> rtTyTables;
58
59 std::unique_ptr<Fortran::semantics::SemanticsContext> semaContext;
60
61 std::unique_ptr<llvm::TargetMachine> targetMachine;
62
64 llvm::raw_ostream *semaOutputStream = &llvm::errs();
65
67 std::unique_ptr<llvm::raw_ostream> ownedSemaOutputStream;
68
70 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics;
71
73 struct OutputFile {
74 std::string filename;
75 OutputFile(std::string inputFilename)
76 : filename(std::move(inputFilename)) {}
77 };
78
80 std::list<OutputFile> outputFiles;
81
86 std::unique_ptr<llvm::raw_pwrite_stream> outputStream;
87
92 mlir::DefaultTimingManager timingMgr;
93
96 mlir::TimingScope timingScopeRoot;
97
104 std::unique_ptr<Fortran::support::string_ostream> timingStreamMLIR;
105 std::unique_ptr<Fortran::support::string_ostream> timingStreamLLVM;
106 std::unique_ptr<Fortran::support::string_ostream> timingStreamCodeGen;
108
109public:
110 explicit CompilerInstance();
111
113
116
117 CompilerInvocation &getInvocation() {
118 assert(invocation && "Compiler instance has no invocation!");
119 return *invocation;
120 };
121
123 void setInvocation(std::shared_ptr<CompilerInvocation> value);
124
128
130 Fortran::parser::AllSources &getAllSources() const { return *allSources; }
131
132 bool hasAllSources() const { return allSources != nullptr; }
133
134 parser::AllCookedSources &getAllCookedSources() {
135 assert(allCookedSources && "Compiler instance has no AllCookedSources!");
136 return *allCookedSources;
137 };
138
142
144 Fortran::parser::Parsing &getParsing() const { return *parsing; }
145
149
150 Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
151 semaContext =
152 getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
153 return *semaContext;
154 }
155
156 Fortran::semantics::SemanticsContext &getSemanticsContext() {
157 return *semaContext;
158 }
159 const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
160 return *semaContext;
161 }
162
164 void setSemaOutputStream(llvm::raw_ostream &value);
165
167 void setSemaOutputStream(std::unique_ptr<llvm::raw_ostream> value);
168
170 llvm::raw_ostream &getSemaOutputStream() { return *semaOutputStream; }
171
172 Fortran::semantics::Semantics &getSemantics() { return *semantics; }
173 const Fortran::semantics::Semantics &getSemantics() const {
174 return *semantics;
175 }
176
177 void setSemantics(std::unique_ptr<Fortran::semantics::Semantics> sema) {
178 semantics = std::move(sema);
179 }
180
181 void setRtTyTables(
182 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> tables) {
183 rtTyTables = std::move(tables);
184 }
185
187 assert(rtTyTables && "Missing runtime derived type tables!");
188 return *rtTyTables;
189 }
190
194
199 bool executeAction(FrontendAction &act);
200
204
205 clang::DiagnosticOptions &getDiagnosticOpts() {
206 return invocation->getDiagnosticOpts();
207 }
208 const clang::DiagnosticOptions &getDiagnosticOpts() const {
209 return invocation->getDiagnosticOpts();
210 }
211
212 FrontendOptions &getFrontendOpts() { return invocation->getFrontendOpts(); }
213 const FrontendOptions &getFrontendOpts() const {
214 return invocation->getFrontendOpts();
215 }
216
217 PreprocessorOptions &preprocessorOpts() {
218 return invocation->getPreprocessorOpts();
219 }
220 const PreprocessorOptions &preprocessorOpts() const {
221 return invocation->getPreprocessorOpts();
222 }
223
227
228 bool hasDiagnostics() const { return diagnostics != nullptr; }
229
231 clang::DiagnosticsEngine &getDiagnostics() const {
232 assert(diagnostics && "Compiler instance has no diagnostics!");
233 return *diagnostics;
234 }
235
236 clang::DiagnosticConsumer &getDiagnosticClient() const {
237 assert(diagnostics && diagnostics->getClient() &&
238 "Compiler instance has no diagnostic client!");
239 return *diagnostics->getClient();
240 }
241
245
247 void clearOutputFiles(bool eraseFiles);
248
260 std::unique_ptr<llvm::raw_pwrite_stream>
261 createDefaultOutputFile(bool binary = true, llvm::StringRef baseInput = "",
262 llvm::StringRef extension = "");
263
267
269 const llvm::TargetMachine &getTargetMachine() const {
270 assert(targetMachine && "target machine was not set");
271 return *targetMachine;
272 }
273 llvm::TargetMachine &getTargetMachine() {
274 assert(targetMachine && "target machine was not set");
275 return *targetMachine;
276 }
277
279 bool setUpTargetMachine();
280
282 std::string getTargetFeatures();
283
287 bool isTimingEnabled() const { return timingMgr.isEnabled(); }
288
289 mlir::DefaultTimingManager &getTimingManager() { return timingMgr; }
290 const mlir::DefaultTimingManager &getTimingManager() const {
291 return timingMgr;
292 }
293
294 mlir::TimingScope &getTimingScopeRoot() { return timingScopeRoot; }
295 const mlir::TimingScope &getTimingScopeRoot() const {
296 return timingScopeRoot;
297 }
298
300 llvm::raw_ostream &getTimingStreamMLIR() {
301 assert(timingStreamMLIR && "Timing stream for MLIR was not set");
302 return *timingStreamMLIR;
303 }
304
306 llvm::raw_ostream &getTimingStreamLLVM() {
307 assert(timingStreamLLVM && "Timing stream for LLVM was not set");
308 return *timingStreamLLVM;
309 }
310
314 llvm::raw_ostream &getTimingStreamCodeGen() {
315 assert(timingStreamCodeGen && "Timing stream for codegen was not set");
316 return *timingStreamCodeGen;
317 }
319
320private:
326 llvm::Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
327 createOutputFileImpl(llvm::StringRef outputPath, bool binary);
328
329public:
333
349 static clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine>
350 createDiagnostics(clang::DiagnosticOptions &opts,
351 clang::DiagnosticConsumer *client = nullptr,
352 bool shouldOwnClient = true);
353 void createDiagnostics(clang::DiagnosticConsumer *client = nullptr,
354 bool shouldOwnClient = true);
355
359 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
360 outputStream = std::move(outStream);
361 }
362
363 bool isOutputStreamNull() { return (outputStream == nullptr); }
364
365 // Allow the frontend compiler to write in the output stream.
366 void writeOutputStream(const std::string &message) {
367 *outputStream << message;
368 }
369
371 llvm::raw_pwrite_stream &getOutputStream() {
372 assert(outputStream &&
373 "Compiler instance has no user-specified output stream!");
374 return *outputStream;
375 }
376};
377
378} // end namespace Fortran::frontend
379#endif // FORTRAN_FRONTEND_COMPILERINSTANCE_H
Definition: CompilerInstance.h:43
void setInvocation(std::shared_ptr< CompilerInvocation > value)
Replace the current invocation.
Definition: CompilerInstance.cpp:50
clang::DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
Definition: CompilerInstance.h:231
llvm::raw_pwrite_stream & getOutputStream()
Get the user specified output stream.
Definition: CompilerInstance.h:371
Fortran::parser::AllSources & getAllSources() const
Return the current allSources.
Definition: CompilerInstance.h:130
static clang::IntrusiveRefCntPtr< clang::DiagnosticsEngine > createDiagnostics(clang::DiagnosticOptions &opts, clang::DiagnosticConsumer *client=nullptr, bool shouldOwnClient=true)
Definition: CompilerInstance.cpp:233
llvm::raw_ostream & getTimingStreamMLIR()
Get the timing stream for the MLIR pass manager.
Definition: CompilerInstance.h:300
bool setUpTargetMachine()
Sets up LLVM's TargetMachine.
Definition: CompilerInstance.cpp:342
const llvm::TargetMachine & getTargetMachine() const
Get the target machine.
Definition: CompilerInstance.h:269
llvm::raw_ostream & getTimingStreamCodeGen()
Definition: CompilerInstance.h:314
void clearOutputFiles(bool eraseFiles)
Clear the output file list.
Definition: CompilerInstance.cpp:144
bool executeAction(FrontendAction &act)
Definition: CompilerInstance.cpp:152
std::string getTargetFeatures()
Produces the string which represents target feature.
Definition: CompilerInstance.cpp:321
void setSemaOutputStream(llvm::raw_ostream &value)
Replace the current stream for verbose output.
llvm::raw_ostream & getTimingStreamLLVM()
Get the timing stream for the new LLVM pass manager.
Definition: CompilerInstance.h:306
std::unique_ptr< llvm::raw_pwrite_stream > createDefaultOutputFile(bool binary=true, llvm::StringRef baseInput="", llvm::StringRef extension="")
Definition: CompilerInstance.cpp:93
llvm::raw_ostream & getSemaOutputStream()
Get the current stream for verbose output.
Definition: CompilerInstance.h:170
Fortran::parser::Parsing & getParsing() const
Return parsing to be used by Actions.
Definition: CompilerInstance.h:144
void setSemaOutputStream(std::unique_ptr< llvm::raw_ostream > value)
Replace the current stream for verbose output.
Definition: CompilerInvocation.h:67
std::unique_ptr< Fortran::semantics::SemanticsContext > getSemanticsCtx(Fortran::parser::AllCookedSources &allCookedSources, const llvm::TargetMachine &)
Creates and configures semantics context based on the compilation flags.
Definition: CompilerInvocation.cpp:1847
Definition: provenance.h:281
Definition: provenance.h:139
Definition: parsing.h:24
Definition: semantics.h:67
Definition: semantics.h:395
Definition: runtime-type-info.h:30