1 //===--- CodeGenAction.h - LLVM Code Generation Frontend Action -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_CLANG_CODEGEN_CODE_GEN_ACTION_H 11 #define LLVM_CLANG_CODEGEN_CODE_GEN_ACTION_H 12 13 #include "clang/Frontend/FrontendAction.h" 14 #include <memory> 15 16 namespace llvm { 17 class LLVMContext; 18 class Module; 19 } 20 21 namespace clang { 22 class BackendConsumer; 23 24 class CodeGenAction : public ASTFrontendAction { 25 private: 26 unsigned Act; 27 std::unique_ptr<llvm::Module> TheModule; 28 llvm::Module *LinkModule; 29 llvm::LLVMContext *VMContext; 30 bool OwnsVMContext; 31 32 protected: 33 /// Create a new code generation action. If the optional \p _VMContext 34 /// parameter is supplied, the action uses it without taking ownership, 35 /// otherwise it creates a fresh LLVM context and takes ownership. 36 CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext = nullptr); 37 38 bool hasIRSupport() const override; 39 40 ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 41 StringRef InFile) override; 42 43 void ExecuteAction() override; 44 45 void EndSourceFileAction() override; 46 47 public: 48 ~CodeGenAction(); 49 50 /// setLinkModule - Set the link module to be used by this action. If a link 51 /// module is not provided, and CodeGenOptions::LinkBitcodeFile is non-empty, 52 /// the action will load it from the specified file. setLinkModule(llvm::Module * Mod)53 void setLinkModule(llvm::Module *Mod) { LinkModule = Mod; } 54 55 /// takeModule - Take the generated LLVM module, for use after the action has 56 /// been run. The result may be null on failure. 57 llvm::Module *takeModule(); 58 59 /// Take the LLVM context used by this action. 60 llvm::LLVMContext *takeLLVMContext(); 61 62 BackendConsumer *BEConsumer; 63 }; 64 65 class EmitAssemblyAction : public CodeGenAction { 66 virtual void anchor(); 67 public: 68 EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr); 69 }; 70 71 class EmitBCAction : public CodeGenAction { 72 virtual void anchor(); 73 public: 74 EmitBCAction(llvm::LLVMContext *_VMContext = nullptr); 75 }; 76 77 class EmitLLVMAction : public CodeGenAction { 78 virtual void anchor(); 79 public: 80 EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr); 81 }; 82 83 class EmitLLVMOnlyAction : public CodeGenAction { 84 virtual void anchor(); 85 public: 86 EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 87 }; 88 89 class EmitCodeGenOnlyAction : public CodeGenAction { 90 virtual void anchor(); 91 public: 92 EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 93 }; 94 95 class EmitObjAction : public CodeGenAction { 96 virtual void anchor(); 97 public: 98 EmitObjAction(llvm::LLVMContext *_VMContext = nullptr); 99 }; 100 101 } 102 103 #endif 104