clang 20.0.0git
CIRGenAction.cpp
Go to the documentation of this file.
1//===--- CIRGenAction.cpp - LLVM Code generation Frontend Action ---------===//
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
12
13#include "mlir/IR/MLIRContext.h"
14#include "mlir/IR/OwningOpRef.h"
15
16using namespace cir;
17using namespace clang;
18
19namespace cir {
20
22
23 virtual void anchor();
24
26
27 std::unique_ptr<raw_pwrite_stream> OutputStream;
28
29 ASTContext *Context{nullptr};
31 std::unique_ptr<CIRGenerator> Gen;
32
33public:
41 const FrontendOptions &FEOptions,
42 std::unique_ptr<raw_pwrite_stream> OS)
43 : Action(Action), OutputStream(std::move(OS)), FS(VFS),
44 Gen(std::make_unique<CIRGenerator>(DiagnosticsEngine, std::move(VFS),
46
47 void Initialize(ASTContext &Ctx) override {
48 assert(!Context && "initialized multiple times");
49 Context = &Ctx;
50 Gen->Initialize(Ctx);
51 }
52
54 Gen->HandleTopLevelDecl(D);
55 return true;
56 }
57
59 Gen->HandleTranslationUnit(C);
60 mlir::ModuleOp MlirModule = Gen->getModule();
61 switch (Action) {
63 if (OutputStream && MlirModule) {
64 mlir::OpPrintingFlags Flags;
65 Flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
66 MlirModule->print(*OutputStream, Flags);
67 }
68 break;
69 }
70 }
71};
72} // namespace cir
73
74void CIRGenConsumer::anchor() {}
75
76CIRGenAction::CIRGenAction(OutputType Act, mlir::MLIRContext *MLIRCtx)
77 : MLIRCtx(MLIRCtx ? MLIRCtx : new mlir::MLIRContext), Action(Act) {}
78
79CIRGenAction::~CIRGenAction() { MLIRMod.release(); }
80
81static std::unique_ptr<raw_pwrite_stream>
82getOutputStream(CompilerInstance &CI, StringRef InFile,
84 switch (Action) {
86 return CI.createDefaultOutputFile(false, InFile, "cir");
87 }
88 llvm_unreachable("Invalid CIRGenAction::OutputType");
89}
90
91std::unique_ptr<ASTConsumer>
93 std::unique_ptr<llvm::raw_pwrite_stream> Out = CI.takeOutputStream();
94
95 if (!Out)
96 Out = getOutputStream(CI, InFile, Action);
97
98 auto Result = std::make_unique<cir::CIRGenConsumer>(
101 CI.getLangOpts(), CI.getFrontendOpts(), std::move(Out));
102
103 return Result;
104}
105
106void EmitCIRAction::anchor() {}
107EmitCIRAction::EmitCIRAction(mlir::MLIRContext *MLIRCtx)
108 : CIRGenAction(OutputType::EmitCIR, MLIRCtx) {}
static std::unique_ptr< raw_pwrite_stream > getOutputStream(CompilerInstance &CI, StringRef InFile, CIRGenAction::OutputType Action)
const Decl * D
CIRGenAction(OutputType Action, mlir::MLIRContext *MLIRCtx=nullptr)
OutputType Action
Definition: CIRGenAction.h:48
~CIRGenAction() override
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef InFile) override
CIRGenConsumer(CIRGenAction::OutputType Action, DiagnosticsEngine &DiagnosticsEngine, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, const HeaderSearchOptions &HeaderSearchOptions, const CodeGenOptions &CodeGenOptions, const TargetOptions &TargetOptions, const LangOptions &LangOptions, const FrontendOptions &FEOptions, std::unique_ptr< raw_pwrite_stream > OS)
void Initialize(ASTContext &Ctx) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void HandleTranslationUnit(ASTContext &C) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
EmitCIRAction(mlir::MLIRContext *MLIRCtx=nullptr)
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:34
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
TargetOptions & getTargetOpts()
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
FrontendOptions & getFrontendOpts()
HeaderSearchOptions & getHeaderSearchOpts()
llvm::vfs::FileSystem & getVirtualFileSystem() const
LangOptions & getLangOpts()
CodeGenOptions & getCodeGenOpts()
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
FrontendOptions - Options for controlling the behavior of the frontend.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
Options for controlling the target.
Definition: TargetOptions.h:26
The JSON file list parser is used to communicate input to InstallAPI.