1 //===--- CodeGenAction.h - LLVM Code Generation Frontend Action -*- 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 #ifndef LLVM_CLANG_CODEGEN_CODEGENACTION_H 10 #define LLVM_CLANG_CODEGEN_CODEGENACTION_H 11 12 #include "clang/Frontend/FrontendAction.h" 13 #include <memory> 14 15 namespace llvm { 16 class LLVMContext; 17 class Module; 18 } 19 20 namespace clang { 21 class BackendConsumer; 22 23 class CodeGenAction : public ASTFrontendAction { 24 private: 25 // Let BackendConsumer access LinkModule. 26 friend class BackendConsumer; 27 28 /// Info about module to link into a module we're generating. 29 struct LinkModule { 30 /// The module to link in. 31 std::unique_ptr<llvm::Module> Module; 32 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. 36 bool PropagateAttrs; 37 38 /// If true, we use LLVM module internalizer. 39 bool Internalize; 40 41 /// Bitwise combination of llvm::LinkerFlags used when we link the module. 42 unsigned LinkFlags; 43 }; 44 45 unsigned Act; 46 std::unique_ptr<llvm::Module> TheModule; 47 48 /// Bitcode modules to link in to our module. 49 SmallVector<LinkModule, 4> LinkModules; 50 llvm::LLVMContext *VMContext; 51 bool OwnsVMContext; 52 53 std::unique_ptr<llvm::Module> loadModule(llvm::MemoryBufferRef MBRef); 54 55 protected: 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); 60 61 bool hasIRSupport() const override; 62 63 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 64 StringRef InFile) override; 65 66 void ExecuteAction() override; 67 68 void EndSourceFileAction() override; 69 70 public: 71 ~CodeGenAction() override; 72 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(); 76 77 /// Take the LLVM context used by this action. 78 llvm::LLVMContext *takeLLVMContext(); 79 80 BackendConsumer *BEConsumer; 81 }; 82 83 class EmitAssemblyAction : public CodeGenAction { 84 virtual void anchor(); 85 public: 86 EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr); 87 }; 88 89 class EmitBCAction : public CodeGenAction { 90 virtual void anchor(); 91 public: 92 EmitBCAction(llvm::LLVMContext *_VMContext = nullptr); 93 }; 94 95 class EmitLLVMAction : public CodeGenAction { 96 virtual void anchor(); 97 public: 98 EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr); 99 }; 100 101 class EmitLLVMOnlyAction : public CodeGenAction { 102 virtual void anchor(); 103 public: 104 EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 105 }; 106 107 class EmitCodeGenOnlyAction : public CodeGenAction { 108 virtual void anchor(); 109 public: 110 EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 111 }; 112 113 class EmitObjAction : public CodeGenAction { 114 virtual void anchor(); 115 public: 116 EmitObjAction(llvm::LLVMContext *_VMContext = nullptr); 117 }; 118 119 } 120 121 #endif 122