Home | History | Annotate | Download | only in CodeGen
      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_CODEGENACTION_H
     11 #define LLVM_CLANG_CODEGEN_CODEGENACTION_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   // Vector of {Linker::Flags, Module*} pairs to specify bitcode
     29   // modules to link in using corresponding linker flags.
     30   SmallVector<std::pair<unsigned, llvm::Module *>, 4> LinkModules;
     31   llvm::LLVMContext *VMContext;
     32   bool OwnsVMContext;
     33 
     34 protected:
     35   /// Create a new code generation action.  If the optional \p _VMContext
     36   /// parameter is supplied, the action uses it without taking ownership,
     37   /// otherwise it creates a fresh LLVM context and takes ownership.
     38   CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext = nullptr);
     39 
     40   bool hasIRSupport() const override;
     41 
     42   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     43                                                  StringRef InFile) override;
     44 
     45   void ExecuteAction() override;
     46 
     47   void EndSourceFileAction() override;
     48 
     49 public:
     50   ~CodeGenAction() override;
     51 
     52   /// setLinkModule - Set the link module to be used by this action.  If a link
     53   /// module is not provided, and CodeGenOptions::LinkBitcodeFile is non-empty,
     54   /// the action will load it from the specified file.
     55   void addLinkModule(llvm::Module *Mod, unsigned LinkFlags) {
     56     LinkModules.push_back(std::make_pair(LinkFlags, Mod));
     57   }
     58 
     59   /// Take the generated LLVM module, for use after the action has been run.
     60   /// The result may be null on failure.
     61   std::unique_ptr<llvm::Module> takeModule();
     62 
     63   /// Take the LLVM context used by this action.
     64   llvm::LLVMContext *takeLLVMContext();
     65 
     66   BackendConsumer *BEConsumer;
     67 };
     68 
     69 class EmitAssemblyAction : public CodeGenAction {
     70   virtual void anchor();
     71 public:
     72   EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr);
     73 };
     74 
     75 class EmitBCAction : public CodeGenAction {
     76   virtual void anchor();
     77 public:
     78   EmitBCAction(llvm::LLVMContext *_VMContext = nullptr);
     79 };
     80 
     81 class EmitLLVMAction : public CodeGenAction {
     82   virtual void anchor();
     83 public:
     84   EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr);
     85 };
     86 
     87 class EmitLLVMOnlyAction : public CodeGenAction {
     88   virtual void anchor();
     89 public:
     90   EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
     91 };
     92 
     93 class EmitCodeGenOnlyAction : public CodeGenAction {
     94   virtual void anchor();
     95 public:
     96   EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
     97 };
     98 
     99 class EmitObjAction : public CodeGenAction {
    100   virtual void anchor();
    101 public:
    102   EmitObjAction(llvm::LLVMContext *_VMContext = nullptr);
    103 };
    104 
    105 }
    106 
    107 #endif
    108