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/raw_ostream.h"
25#include "llvm/Target/TargetMachine.h"
26
27namespace Fortran::frontend {
28
44class CompilerInstance {
45
47 std::shared_ptr<CompilerInvocation> invocation;
48
50 std::shared_ptr<Fortran::parser::AllSources> allSources;
51
52 std::shared_ptr<Fortran::parser::AllCookedSources> allCookedSources;
53
54 std::shared_ptr<Fortran::parser::Parsing> parsing;
55
56 std::unique_ptr<Fortran::semantics::Semantics> semantics;
57
58 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> rtTyTables;
59
60 std::unique_ptr<Fortran::semantics::SemanticsContext> semaContext;
61
62 std::unique_ptr<llvm::TargetMachine> targetMachine;
63
65 std::vector<std::unique_ptr<llvm::PassPlugin>> passPlugins;
66
68 llvm::raw_ostream *semaOutputStream = &llvm::errs();
69
71 std::unique_ptr<llvm::raw_ostream> ownedSemaOutputStream;
72
74 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics;
75
77 struct OutputFile {
78 std::string filename;
79 OutputFile(std::string inputFilename)
80 : filename(std::move(inputFilename)) {}
81 };
82
84 std::list<OutputFile> outputFiles;
85
90 std::unique_ptr<llvm::raw_pwrite_stream> outputStream;
91
96 mlir::DefaultTimingManager timingMgr;
97
100 mlir::TimingScope timingScopeRoot;
101
108 std::unique_ptr<Fortran::support::string_ostream> timingStreamMLIR;
109 std::unique_ptr<Fortran::support::string_ostream> timingStreamLLVM;
110 std::unique_ptr<Fortran::support::string_ostream> timingStreamCodeGen;
112
113public:
114 explicit CompilerInstance();
115
116 ~CompilerInstance();
117
120
121 CompilerInvocation &getInvocation() {
122 assert(invocation && "Compiler instance has no invocation!");
123 return *invocation;
124 };
125
127 void setInvocation(std::shared_ptr<CompilerInvocation> value);
128
132
134 Fortran::parser::AllSources &getAllSources() const { return *allSources; }
135
136 bool hasAllSources() const { return allSources != nullptr; }
137
138 parser::AllCookedSources &getAllCookedSources() {
139 assert(allCookedSources && "Compiler instance has no AllCookedSources!");
140 return *allCookedSources;
141 };
142
146
148 Fortran::parser::Parsing &getParsing() const { return *parsing; }
149
153
154 Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
155 semaContext =
156 getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
157 return *semaContext;
158 }
159
160 Fortran::semantics::SemanticsContext &getSemanticsContext() {
161 return *semaContext;
162 }
163 const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
164 return *semaContext;
165 }
166
168 void setSemaOutputStream(llvm::raw_ostream &value);
169
171 void setSemaOutputStream(std::unique_ptr<llvm::raw_ostream> value);
172
174 llvm::raw_ostream &getSemaOutputStream() { return *semaOutputStream; }
175
176 Fortran::semantics::Semantics &getSemantics() { return *semantics; }
177 const Fortran::semantics::Semantics &getSemantics() const {
178 return *semantics;
179 }
180
181 void setSemantics(std::unique_ptr<Fortran::semantics::Semantics> sema) {
182 semantics = std::move(sema);
183 }
184
185 void setRtTyTables(
186 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> tables) {
187 rtTyTables = std::move(tables);
188 }
189
190 Fortran::semantics::RuntimeDerivedTypeTables &getRtTyTables() {
191 assert(rtTyTables && "Missing runtime derived type tables!");
192 return *rtTyTables;
193 }
194
198
203 bool executeAction(FrontendAction &act);
204
208
209 clang::DiagnosticOptions &getDiagnosticOpts() {
210 return invocation->getDiagnosticOpts();
211 }
212 const clang::DiagnosticOptions &getDiagnosticOpts() const {
213 return invocation->getDiagnosticOpts();
214 }
215
216 FrontendOptions &getFrontendOpts() { return invocation->getFrontendOpts(); }
217 const FrontendOptions &getFrontendOpts() const {
218 return invocation->getFrontendOpts();
219 }
220
221 PreprocessorOptions &preprocessorOpts() {
222 return invocation->getPreprocessorOpts();
223 }
224 const PreprocessorOptions &preprocessorOpts() const {
225 return invocation->getPreprocessorOpts();
226 }
227
231
232 bool hasDiagnostics() const { return diagnostics != nullptr; }
233
235 clang::DiagnosticsEngine &getDiagnostics() const {
236 assert(diagnostics && "Compiler instance has no diagnostics!");
237 return *diagnostics;
238 }
239
240 clang::DiagnosticConsumer &getDiagnosticClient() const {
241 assert(diagnostics && diagnostics->getClient() &&
242 "Compiler instance has no diagnostic client!");
243 return *diagnostics->getClient();
244 }
245
249
251 void clearOutputFiles(bool eraseFiles);
252
264 std::unique_ptr<llvm::raw_pwrite_stream>
265 createDefaultOutputFile(bool binary = true, llvm::StringRef baseInput = "",
266 llvm::StringRef extension = "");
267
271
272 void addPassPlugin(std::unique_ptr<llvm::PassPlugin> plugin) {
273 passPlugins.emplace_back(std::move(plugin));
274 }
275
277 return passPlugins;
278 }
279
283
285 const llvm::TargetMachine &getTargetMachine() const {
286 assert(targetMachine && "target machine was not set");
287 return *targetMachine;
288 }
289 llvm::TargetMachine &getTargetMachine() {
290 assert(targetMachine && "target machine was not set");
291 return *targetMachine;
292 }
293
295 bool setUpTargetMachine();
296
298 std::string getTargetFeatures();
299
303 bool isTimingEnabled() const { return timingMgr.isEnabled(); }
304
305 mlir::DefaultTimingManager &getTimingManager() { return timingMgr; }
306 const mlir::DefaultTimingManager &getTimingManager() const {
307 return timingMgr;
308 }
309
310 mlir::TimingScope &getTimingScopeRoot() { return timingScopeRoot; }
311 const mlir::TimingScope &getTimingScopeRoot() const {
312 return timingScopeRoot;
313 }
314
316 llvm::raw_ostream &getTimingStreamMLIR() {
317 assert(timingStreamMLIR && "Timing stream for MLIR was not set");
318 return *timingStreamMLIR;
319 }
320
322 llvm::raw_ostream &getTimingStreamLLVM() {
323 assert(timingStreamLLVM && "Timing stream for LLVM was not set");
324 return *timingStreamLLVM;
325 }
326
330 llvm::raw_ostream &getTimingStreamCodeGen() {
331 assert(timingStreamCodeGen && "Timing stream for codegen was not set");
332 return *timingStreamCodeGen;
333 }
334
335
336private:
342 llvm::Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
343 createOutputFileImpl(llvm::StringRef outputPath, bool binary);
344
345public:
349
365 static clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine>
366 createDiagnostics(clang::DiagnosticOptions &opts,
367 clang::DiagnosticConsumer *client = nullptr,
368 bool shouldOwnClient = true);
369 void createDiagnostics(clang::DiagnosticConsumer *client = nullptr,
370 bool shouldOwnClient = true);
371
375 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
376 outputStream = std::move(outStream);
377 }
378
379 bool isOutputStreamNull() { return (outputStream == nullptr); }
380
381 // Allow the frontend compiler to write in the output stream.
382 void writeOutputStream(const std::string &message) {
383 *outputStream << message;
384 }
385
387 llvm::raw_pwrite_stream &getOutputStream() {
388 assert(outputStream &&
389 "Compiler instance has no user-specified output stream!");
390 return *outputStream;
391 }
392};
393
394} // end namespace Fortran::frontend
395#endif // FORTRAN_FRONTEND_COMPILERINSTANCE_H
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:235
llvm::raw_pwrite_stream & getOutputStream()
Get the user specified output stream.
Definition CompilerInstance.h:387
Fortran::parser::AllSources & getAllSources() const
Return the current allSources.
Definition CompilerInstance.h:134
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:316
bool setUpTargetMachine()
Sets up LLVM's TargetMachine.
Definition CompilerInstance.cpp:342
const llvm::TargetMachine & getTargetMachine() const
Get the target machine.
Definition CompilerInstance.h:285
llvm::raw_ostream & getTimingStreamCodeGen()
Definition CompilerInstance.h:330
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:322
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:174
Fortran::parser::Parsing & getParsing() const
Return parsing to be used by Actions.
Definition CompilerInstance.h:148
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:1890
Definition provenance.h:281
Definition provenance.h:139
Definition parsing.h:24
Definition semantics.h:67
Definition semantics.h:399
Definition FIRType.h:92