1 //===--- CodeGenAction.h - LLVM Code Generation Frontend Action -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_CODEGEN_CODEGENACTION_H
10 #define LLVM_CLANG_CODEGEN_CODEGENACTION_H
12 #include "clang/Frontend/FrontendAction.h"
21 class BackendConsumer;
23 class CodeGenAction : public ASTFrontendAction {
25 // Let BackendConsumer access LinkModule.
26 friend class BackendConsumer;
28 /// Info about module to link into a module we're generating.
30 /// The module to link in.
31 std::unique_ptr<llvm::Module> Module;
33 /// If true, we set attributes on Module's functions according to our
34 /// CodeGenOptions and LangOptions, as though we were generating the
35 /// function ourselves.
38 /// If true, we use LLVM module internalizer.
41 /// Bitwise combination of llvm::LinkerFlags used when we link the module.
46 std::unique_ptr<llvm::Module> TheModule;
48 /// Bitcode modules to link in to our module.
49 SmallVector<LinkModule, 4> LinkModules;
50 llvm::LLVMContext *VMContext;
53 std::unique_ptr<llvm::Module> loadModule(llvm::MemoryBufferRef MBRef);
56 /// Create a new code generation action. If the optional \p _VMContext
57 /// parameter is supplied, the action uses it without taking ownership,
58 /// otherwise it creates a fresh LLVM context and takes ownership.
59 CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext = nullptr);
61 bool hasIRSupport() const override;
63 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
64 StringRef InFile) override;
66 void ExecuteAction() override;
68 void EndSourceFileAction() override;
71 ~CodeGenAction() override;
73 /// Take the generated LLVM module, for use after the action has been run.
74 /// The result may be null on failure.
75 std::unique_ptr<llvm::Module> takeModule();
77 /// Take the LLVM context used by this action.
78 llvm::LLVMContext *takeLLVMContext();
80 BackendConsumer *BEConsumer;
83 class EmitAssemblyAction : public CodeGenAction {
84 virtual void anchor();
86 EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr);
89 class EmitBCAction : public CodeGenAction {
90 virtual void anchor();
92 EmitBCAction(llvm::LLVMContext *_VMContext = nullptr);
95 class EmitLLVMAction : public CodeGenAction {
96 virtual void anchor();
98 EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr);
101 class EmitLLVMOnlyAction : public CodeGenAction {
102 virtual void anchor();
104 EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
107 class EmitCodeGenOnlyAction : public CodeGenAction {
108 virtual void anchor();
110 EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
113 class EmitObjAction : public CodeGenAction {
114 virtual void anchor();
116 EmitObjAction(llvm::LLVMContext *_VMContext = nullptr);