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"
19#include "flang/Parser/parsing.h"
20#include "flang/Parser/provenance.h"
21#include "flang/Semantics/runtime-type-info.h"
22#include "flang/Semantics/semantics.h"
23#include "flang/Support/StringOstream.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/Target/TargetMachine.h"
26
27namespace Fortran::frontend {
28
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 llvm::raw_ostream *semaOutputStream = &llvm::errs();
66
68 std::unique_ptr<llvm::raw_ostream> ownedSemaOutputStream;
69
71 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diagnostics;
72
74 struct OutputFile {
75 std::string filename;
76 OutputFile(std::string inputFilename)
77 : filename(std::move(inputFilename)) {}
78 };
79
81 std::list<OutputFile> outputFiles;
82
87 std::unique_ptr<llvm::raw_pwrite_stream> outputStream;
88
93 mlir::DefaultTimingManager timingMgr;
94
97 mlir::TimingScope timingScopeRoot;
98
105 std::unique_ptr<Fortran::support::string_ostream> timingStreamMLIR;
106 std::unique_ptr<Fortran::support::string_ostream> timingStreamLLVM;
107 std::unique_ptr<Fortran::support::string_ostream> timingStreamCodeGen;
109
110public:
111 explicit CompilerInstance();
112
114
117
118 CompilerInvocation &getInvocation() {
119 assert(invocation && "Compiler instance has no invocation!");
120 return *invocation;
121 };
122
124 void setInvocation(std::shared_ptr<CompilerInvocation> value);
125
129
131 Fortran::parser::AllSources &getAllSources() const { return *allSources; }
132
133 bool hasAllSources() const { return allSources != nullptr; }
134
135 parser::AllCookedSources &getAllCookedSources() {
136 assert(allCookedSources && "Compiler instance has no AllCookedSources!");
137 return *allCookedSources;
138 };
139
143
145 Fortran::parser::Parsing &getParsing() const { return *parsing; }
146
150
151 Fortran::semantics::SemanticsContext &getSemanticsContext() {
152 return *semaContext;
153 }
154 const Fortran::semantics::SemanticsContext &getSemanticsContext() const {
155 return *semaContext;
156 }
157
159 void setSemaOutputStream(llvm::raw_ostream &value);
160
162 void setSemaOutputStream(std::unique_ptr<llvm::raw_ostream> value);
163
165 llvm::raw_ostream &getSemaOutputStream() { return *semaOutputStream; }
166
167 Fortran::semantics::Semantics &getSemantics() { return *semantics; }
168 const Fortran::semantics::Semantics &getSemantics() const {
169 return *semantics;
170 }
171
172 void setSemantics(std::unique_ptr<Fortran::semantics::Semantics> sema) {
173 semantics = std::move(sema);
174 }
175
176 void setRtTyTables(
177 std::unique_ptr<Fortran::semantics::RuntimeDerivedTypeTables> tables) {
178 rtTyTables = std::move(tables);
179 }
180
182 assert(rtTyTables && "Missing runtime derived type tables!");
183 return *rtTyTables;
184 }
185
189
194 bool executeAction(FrontendAction &act);
195
199
200 clang::DiagnosticOptions &getDiagnosticOpts() {
201 return invocation->getDiagnosticOpts();
202 }
203 const clang::DiagnosticOptions &getDiagnosticOpts() const {
204 return invocation->getDiagnosticOpts();
205 }
206
207 FrontendOptions &getFrontendOpts() { return invocation->getFrontendOpts(); }
208 const FrontendOptions &getFrontendOpts() const {
209 return invocation->getFrontendOpts();
210 }
211
212 PreprocessorOptions &preprocessorOpts() {
213 return invocation->getPreprocessorOpts();
214 }
215 const PreprocessorOptions &preprocessorOpts() const {
216 return invocation->getPreprocessorOpts();
217 }
218
222
223 bool hasDiagnostics() const { return diagnostics != nullptr; }
224
226 clang::DiagnosticsEngine &getDiagnostics() const {
227 assert(diagnostics && "Compiler instance has no diagnostics!");
228 return *diagnostics;
229 }
230
231 clang::DiagnosticConsumer &getDiagnosticClient() const {
232 assert(diagnostics && diagnostics->getClient() &&
233 "Compiler instance has no diagnostic client!");
234 return *diagnostics->getClient();
235 }
236
240
242 void clearOutputFiles(bool eraseFiles);
243
255 std::unique_ptr<llvm::raw_pwrite_stream>
256 createDefaultOutputFile(bool binary = true, llvm::StringRef baseInput = "",
257 llvm::StringRef extension = "");
258
262
264 const llvm::TargetMachine &getTargetMachine() const {
265 assert(targetMachine && "target machine was not set");
266 return *targetMachine;
267 }
268 llvm::TargetMachine &getTargetMachine() {
269 assert(targetMachine && "target machine was not set");
270 return *targetMachine;
271 }
272
274 bool setUpTargetMachine();
275
277 std::string getTargetFeatures();
278
282 bool isTimingEnabled() const { return timingMgr.isEnabled(); }
283
284 mlir::DefaultTimingManager &getTimingManager() { return timingMgr; }
285 const mlir::DefaultTimingManager &getTimingManager() const {
286 return timingMgr;
287 }
288
289 mlir::TimingScope &getTimingScopeRoot() { return timingScopeRoot; }
290 const mlir::TimingScope &getTimingScopeRoot() const {
291 return timingScopeRoot;
292 }
293
295 llvm::raw_ostream &getTimingStreamMLIR() {
296 assert(timingStreamMLIR && "Timing stream for MLIR was not set");
297 return *timingStreamMLIR;
298 }
299
301 llvm::raw_ostream &getTimingStreamLLVM() {
302 assert(timingStreamLLVM && "Timing stream for LLVM was not set");
303 return *timingStreamLLVM;
304 }
305
309 llvm::raw_ostream &getTimingStreamCodeGen() {
310 assert(timingStreamCodeGen && "Timing stream for codegen was not set");
311 return *timingStreamCodeGen;
312 }
314
315private:
321 llvm::Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
322 createOutputFileImpl(llvm::StringRef outputPath, bool binary);
323
324public:
328
344 static clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine>
345 createDiagnostics(clang::DiagnosticOptions *opts,
346 clang::DiagnosticConsumer *client = nullptr,
347 bool shouldOwnClient = true);
348 void createDiagnostics(clang::DiagnosticConsumer *client = nullptr,
349 bool shouldOwnClient = true);
350
354 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
355 outputStream = std::move(outStream);
356 }
357
358 bool isOutputStreamNull() { return (outputStream == nullptr); }
359
360 // Allow the frontend compiler to write in the output stream.
361 void writeOutputStream(const std::string &message) {
362 *outputStream << message;
363 }
364
366 llvm::raw_pwrite_stream &getOutputStream() {
367 assert(outputStream &&
368 "Compiler instance has no user-specified output stream!");
369 return *outputStream;
370 }
371};
372
373} // end namespace Fortran::frontend
374#endif // FORTRAN_FRONTEND_COMPILERINSTANCE_H
Definition: CompilerInstance.h:44
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:226
llvm::raw_pwrite_stream & getOutputStream()
Get the user specified output stream.
Definition: CompilerInstance.h:366
Fortran::parser::AllSources & getAllSources() const
Return the current allSources.
Definition: CompilerInstance.h:131
llvm::raw_ostream & getTimingStreamMLIR()
Get the timing stream for the MLIR pass manager.
Definition: CompilerInstance.h:295
bool setUpTargetMachine()
Sets up LLVM's TargetMachine.
Definition: CompilerInstance.cpp:353
const llvm::TargetMachine & getTargetMachine() const
Get the target machine.
Definition: CompilerInstance.h:264
llvm::raw_ostream & getTimingStreamCodeGen()
Definition: CompilerInstance.h:309
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:332
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:301
static clang::IntrusiveRefCntPtr< clang::DiagnosticsEngine > createDiagnostics(clang::DiagnosticOptions *opts, clang::DiagnosticConsumer *client=nullptr, bool shouldOwnClient=true)
Definition: CompilerInstance.cpp:239
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:165
Fortran::parser::Parsing & getParsing() const
Return parsing to be used by Actions.
Definition: CompilerInstance.h:145
void setSemaOutputStream(std::unique_ptr< llvm::raw_ostream > value)
Replace the current stream for verbose output.
Definition: CompilerInvocation.h:67
Definition: provenance.h:281
Definition: provenance.h:139
Definition: parsing.h:47
Definition: semantics.h:67
Definition: semantics.h:341
Definition: runtime-type-info.h:30