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/Plugins/PassPlugin.h"
24#include "llvm/Support/LibraryOptionsContext.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/TargetMachine.h"
27
28namespace Fortran::frontend {
29
45class CompilerInstance {
46
48 std::shared_ptr<CompilerInvocation> invocation;
49
51 std::shared_ptr<Fortran::parser::AllSources> allSources;
52
53 std::shared_ptr<Fortran::parser::AllCookedSources> allCookedSources;
54
55 std::shared_ptr<Fortran::parser::Parsing> parsing;
56
57 std::unique_ptr<Fortran::semantics::Semantics> semantics;
58
59 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> rtTyTables;
60
61 std::unique_ptr<Fortran::semantics::SemanticsContext> semaContext;
62
64 llvm::cl::LibraryOptionsContext libraryOptions;
65
66 std::unique_ptr<llvm::TargetMachine> targetMachine;
67
69 std::vector<std::unique_ptr<llvm::PassPlugin>> passPlugins;
70
72 llvm::raw_ostream *semaOutputStream = &llvm::errs();
73
75 std::unique_ptr<llvm::raw_ostream> ownedSemaOutputStream;
76
78 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics;
79
81 struct OutputFile {
82 std::string filename;
83 OutputFile(std::string inputFilename)
84 : filename(std::move(inputFilename)) {}
85 };
86
88 std::list<OutputFile> outputFiles;
89
94 std::unique_ptr<llvm::raw_pwrite_stream> outputStream;
95
100 mlir::DefaultTimingManager timingMgr;
101
104 mlir::TimingScope timingScopeRoot;
105
112 std::unique_ptr<Fortran::support::string_ostream> timingStreamMLIR;
113 std::unique_ptr<Fortran::support::string_ostream> timingStreamLLVM;
114 std::unique_ptr<Fortran::support::string_ostream> timingStreamCodeGen;
116
117public:
118 CompilerInstance();
119 explicit CompilerInstance(std::shared_ptr<CompilerInvocation> invocation);
120
121 ~CompilerInstance();
122
125
126 CompilerInvocation &getInvocation() { return *invocation; };
127
131
133 Fortran::parser::AllSources &getAllSources() const { return *allSources; }
134
135 bool hasAllSources() const { return allSources != nullptr; }
136
137 parser::AllCookedSources &getAllCookedSources() {
138 assert(allCookedSources && "Compiler instance has no AllCookedSources!");
139 return *allCookedSources;
140 };
141
145
147 Fortran::parser::Parsing &getParsing() const { return *parsing; }
148
152
153 Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
154 semaContext =
155 getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
156 return *semaContext;
157 }
158
159 Fortran::semantics::SemanticsContext &getSemanticsContext() {
160 return *semaContext;
161 }
162 const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
163 return *semaContext;
164 }
165
167 void setSemaOutputStream(llvm::raw_ostream &value);
168
170 void setSemaOutputStream(std::unique_ptr<llvm::raw_ostream> value);
171
173 llvm::raw_ostream &getSemaOutputStream() { return *semaOutputStream; }
174
175 Fortran::semantics::Semantics &getSemantics() { return *semantics; }
176 const Fortran::semantics::Semantics &getSemantics() const {
177 return *semantics;
178 }
179
180 void setSemantics(std::unique_ptr<Fortran::semantics::Semantics> sema) {
181 semantics = std::move(sema);
182 }
183
184 void setRtTyTables(
185 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> tables) {
186 rtTyTables = std::move(tables);
187 }
188
189 Fortran::semantics::RuntimeDerivedTypeTables &getRtTyTables() {
190 assert(rtTyTables && "Missing runtime derived type tables!");
191 return *rtTyTables;
192 }
193
197
202 bool executeAction(FrontendAction &act);
203
207
208 clang::DiagnosticOptions &getDiagnosticOpts() {
209 return invocation->getDiagnosticOpts();
210 }
211 const clang::DiagnosticOptions &getDiagnosticOpts() const {
212 return invocation->getDiagnosticOpts();
213 }
214
215 FrontendOptions &getFrontendOpts() { return invocation->getFrontendOpts(); }
216 const FrontendOptions &getFrontendOpts() const {
217 return invocation->getFrontendOpts();
218 }
219
220 PreprocessorOptions &preprocessorOpts() {
221 return invocation->getPreprocessorOpts();
222 }
223 const PreprocessorOptions &preprocessorOpts() const {
224 return invocation->getPreprocessorOpts();
225 }
226
230
231 bool hasDiagnostics() const { return diagnostics != nullptr; }
232
234 clang::DiagnosticsEngine &getDiagnostics() const {
235 assert(diagnostics && "Compiler instance has no diagnostics!");
236 return *diagnostics;
237 }
238
239 clang::DiagnosticConsumer &getDiagnosticClient() const {
240 assert(diagnostics && diagnostics->getClient() &&
241 "Compiler instance has no diagnostic client!");
242 return *diagnostics->getClient();
243 }
244
248
250 void clearOutputFiles(bool eraseFiles);
251
263 std::unique_ptr<llvm::raw_pwrite_stream>
264 createDefaultOutputFile(bool binary = true, llvm::StringRef baseInput = "",
265 llvm::StringRef extension = "");
266
270
271 void addPassPlugin(std::unique_ptr<llvm::PassPlugin> plugin) {
272 passPlugins.emplace_back(std::move(plugin));
273 }
274
276 return passPlugins;
277 }
278
280 llvm::cl::LibraryOptionsContext &getLibraryOptions() {
281 return libraryOptions;
282 }
283 const llvm::cl::LibraryOptionsContext &getLibraryOptions() const {
284 return libraryOptions;
285 }
286
289
291 const llvm::TargetMachine &getTargetMachine() const {
292 assert(targetMachine && "target machine was not set");
293 return *targetMachine;
294 }
295 llvm::TargetMachine &getTargetMachine() {
296 assert(targetMachine && "target machine was not set");
297 return *targetMachine;
298 }
299
301 bool setUpTargetMachine();
302
304 std::string getTargetFeatures();
305
309 bool isTimingEnabled() const { return timingMgr.isEnabled(); }
310
311 mlir::DefaultTimingManager &getTimingManager() { return timingMgr; }
312 const mlir::DefaultTimingManager &getTimingManager() const {
313 return timingMgr;
314 }
315
316 mlir::TimingScope &getTimingScopeRoot() { return timingScopeRoot; }
317 const mlir::TimingScope &getTimingScopeRoot() const {
318 return timingScopeRoot;
319 }
320
322 llvm::raw_ostream &getTimingStreamMLIR() {
323 assert(timingStreamMLIR && "Timing stream for MLIR was not set");
324 return *timingStreamMLIR;
325 }
326
328 llvm::raw_ostream &getTimingStreamLLVM() {
329 assert(timingStreamLLVM && "Timing stream for LLVM was not set");
330 return *timingStreamLLVM;
331 }
332
336 llvm::raw_ostream &getTimingStreamCodeGen() {
337 assert(timingStreamCodeGen && "Timing stream for codegen was not set");
338 return *timingStreamCodeGen;
339 }
340
341
342private:
348 llvm::Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
349 createOutputFileImpl(llvm::StringRef outputPath, bool binary);
350
351public:
355
371 static clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine>
372 createDiagnostics(clang::DiagnosticOptions &opts,
373 clang::DiagnosticConsumer *client = nullptr,
374 bool shouldOwnClient = true);
375 void createDiagnostics(clang::DiagnosticConsumer *client = nullptr,
376 bool shouldOwnClient = true);
377
381 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
382 outputStream = std::move(outStream);
383 }
384
385 bool isOutputStreamNull() { return (outputStream == nullptr); }
386
387 // Allow the frontend compiler to write in the output stream.
388 void writeOutputStream(const std::string &message) {
389 *outputStream << message;
390 }
391
393 llvm::raw_pwrite_stream &getOutputStream() {
394 assert(outputStream &&
395 "Compiler instance has no user-specified output stream!");
396 return *outputStream;
397 }
398};
399
400} // end namespace Fortran::frontend
401#endif // FORTRAN_FRONTEND_COMPILERINSTANCE_H
clang::DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
Definition CompilerInstance.h:234
llvm::raw_pwrite_stream & getOutputStream()
Get the user specified output stream.
Definition CompilerInstance.h:393
Fortran::parser::AllSources & getAllSources() const
Return the current allSources.
Definition CompilerInstance.h:133
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:322
bool setUpTargetMachine()
Sets up LLVM's TargetMachine.
Definition CompilerInstance.cpp:359
const llvm::TargetMachine & getTargetMachine() const
Get the target machine.
Definition CompilerInstance.h:291
llvm::raw_ostream & getTimingStreamCodeGen()
Definition CompilerInstance.h:336
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:335
void setSemaOutputStream(llvm::raw_ostream &value)
Replace the current stream for verbose output.
llvm::cl::LibraryOptionsContext & getLibraryOptions()
}
Definition CompilerInstance.h:280
llvm::raw_ostream & getTimingStreamLLVM()
Get the timing stream for the new LLVM pass manager.
Definition CompilerInstance.h:328
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:173
Fortran::parser::Parsing & getParsing() const
Return parsing to be used by Actions.
Definition CompilerInstance.h:147
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:2125
Definition provenance.h:286
Definition provenance.h:139
Definition parsing.h:25
Definition semantics.h:71
Definition semantics.h:485
Definition FIRType.h:106