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 class CodeGenerator; 23 24 class CodeGenAction : public ASTFrontendAction { 25 private: 26 // Let BackendConsumer access LinkModule. 27 friend class BackendConsumer; 28 29 /// Info about module to link into a module we're generating. 30 struct LinkModule { 31 /// The module to link in. 32 std::unique_ptr<llvm::Module> Module; 33 34 /// If true, we set attributes on Module's functions according to our 35 /// CodeGenOptions and LangOptions, as though we were generating the 36 /// function ourselves. 37 bool PropagateAttrs; 38 39 /// If true, we use LLVM module internalizer. 40 bool Internalize; 41 42 /// Bitwise combination of llvm::LinkerFlags used when we link the module. 43 unsigned LinkFlags; 44 }; 45 46 unsigned Act; 47 std::unique_ptr<llvm::Module> TheModule; 48 49 /// Bitcode modules to link in to our module. 50 SmallVector<LinkModule, 4> LinkModules; 51 llvm::LLVMContext *VMContext; 52 bool OwnsVMContext; 53 54 std::unique_ptr<llvm::Module> loadModule(llvm::MemoryBufferRef MBRef); 55 56 /// Load bitcode modules to link into our module from the options. 57 bool loadLinkModules(CompilerInstance &CI); 58 59 protected: 60 /// Create a new code generation action. If the optional \p _VMContext 61 /// parameter is supplied, the action uses it without taking ownership, 62 /// otherwise it creates a fresh LLVM context and takes ownership. 63 CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext = nullptr); 64 65 bool hasIRSupport() const override; 66 67 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 68 StringRef InFile) override; 69 70 void ExecuteAction() override; 71 72 void EndSourceFileAction() override; 73 74 public: 75 ~CodeGenAction() override; 76 77 /// Take the generated LLVM module, for use after the action has been run. 78 /// The result may be null on failure. 79 std::unique_ptr<llvm::Module> takeModule(); 80 81 /// Take the LLVM context used by this action. 82 llvm::LLVMContext *takeLLVMContext(); 83 84 CodeGenerator *getCodeGenerator() const; 85 86 BackendConsumer *BEConsumer = nullptr; 87 }; 88 89 class EmitAssemblyAction : public CodeGenAction { 90 virtual void anchor(); 91 public: 92 EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr); 93 }; 94 95 class EmitBCAction : public CodeGenAction { 96 virtual void anchor(); 97 public: 98 EmitBCAction(llvm::LLVMContext *_VMContext = nullptr); 99 }; 100 101 class EmitLLVMAction : public CodeGenAction { 102 virtual void anchor(); 103 public: 104 EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr); 105 }; 106 107 class EmitLLVMOnlyAction : public CodeGenAction { 108 virtual void anchor(); 109 public: 110 EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 111 }; 112 113 class EmitCodeGenOnlyAction : public CodeGenAction { 114 virtual void anchor(); 115 public: 116 EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr); 117 }; 118 119 class EmitObjAction : public CodeGenAction { 120 virtual void anchor(); 121 public: 122 EmitObjAction(llvm::LLVMContext *_VMContext = nullptr); 123 }; 124 125 } 126 127 #endif 128