clang  14.0.0git
CodeGenAction.h
Go to the documentation of this file.
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 
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 
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 protected:
57  /// Create a new code generation action. If the optional \p _VMContext
58  /// parameter is supplied, the action uses it without taking ownership,
59  /// otherwise it creates a fresh LLVM context and takes ownership.
60  CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext = nullptr);
61 
62  bool hasIRSupport() const override;
63 
64  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
65  StringRef InFile) override;
66 
67  void ExecuteAction() override;
68 
69  void EndSourceFileAction() override;
70 
71 public:
72  ~CodeGenAction() override;
73 
74  /// Take the generated LLVM module, for use after the action has been run.
75  /// The result may be null on failure.
76  std::unique_ptr<llvm::Module> takeModule();
77 
78  /// Take the LLVM context used by this action.
79  llvm::LLVMContext *takeLLVMContext();
80 
82 
84 };
85 
87  virtual void anchor();
88 public:
89  EmitAssemblyAction(llvm::LLVMContext *_VMContext = nullptr);
90 };
91 
92 class EmitBCAction : public CodeGenAction {
93  virtual void anchor();
94 public:
95  EmitBCAction(llvm::LLVMContext *_VMContext = nullptr);
96 };
97 
98 class EmitLLVMAction : public CodeGenAction {
99  virtual void anchor();
100 public:
101  EmitLLVMAction(llvm::LLVMContext *_VMContext = nullptr);
102 };
103 
105  virtual void anchor();
106 public:
107  EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
108 };
109 
111  virtual void anchor();
112 public:
113  EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext = nullptr);
114 };
115 
116 class EmitObjAction : public CodeGenAction {
117  virtual void anchor();
118 public:
119  EmitObjAction(llvm::LLVMContext *_VMContext = nullptr);
120 };
121 
122 }
123 
124 #endif
clang::CodeGenAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: CodeGenAction.cpp:1102
clang::CodeGenAction::EndSourceFileAction
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: CodeGenAction.cpp:932
clang::CodeGenerator
The primary public interface to the Clang code generator.
Definition: ModuleBuilder.h:43
llvm
Definition: Dominators.h:30
llvm::SmallVector< LinkModule, 4 >
clang::EmitLLVMAction
Definition: CodeGenAction.h:98
clang::EmitAssemblyAction::EmitAssemblyAction
EmitAssemblyAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1183
clang::EmitCodeGenOnlyAction
Definition: CodeGenAction.h:110
clang::EmitAssemblyAction
Definition: CodeGenAction.h:86
clang::EmitObjAction
Definition: CodeGenAction.h:116
clang::CodeGenAction::~CodeGenAction
~CodeGenAction() override
Definition: CodeGenAction.cpp:924
clang::CodeGenAction::hasIRSupport
bool hasIRSupport() const override
Does this action support use with IR files?
Definition: CodeGenAction.cpp:930
FrontendAction.h
clang::EmitCodeGenOnlyAction::EmitCodeGenOnlyAction
EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1199
clang::Module
Describes a module or submodule.
Definition: Module.h:96
clang::EmitLLVMAction::EmitLLVMAction
EmitLLVMAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1191
clang::CodeGenAction
Definition: CodeGenAction.h:24
clang::index::SymbolKind::Module
@ Module
clang::EmitLLVMOnlyAction::EmitLLVMOnlyAction
EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1195
clang::CodeGenAction::BEConsumer
BackendConsumer * BEConsumer
Definition: CodeGenAction.h:83
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:74
clang::CodeGenAction::getCodeGenerator
CodeGenerator * getCodeGenerator() const
Definition: CodeGenAction.cpp:950
clang::EmitBCAction
Definition: CodeGenAction.h:92
clang::CodeGenAction::takeModule
std::unique_ptr< llvm::Module > takeModule()
Take the generated LLVM module, for use after the action has been run.
Definition: CodeGenAction.cpp:941
clang::EmitLLVMOnlyAction
Definition: CodeGenAction.h:104
clang::CodeGenAction::takeLLVMContext
llvm::LLVMContext * takeLLVMContext()
Take the LLVM context used by this action.
Definition: CodeGenAction.cpp:945
clang::CodeGenAction::CodeGenAction
CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext=nullptr)
Create a new code generation action.
Definition: CodeGenAction.cpp:920
clang::CodeGenAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: CodeGenAction.cpp:975
clang
Definition: CalledOnceCheck.h:17
clang::EmitObjAction::EmitObjAction
EmitObjAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1203
clang::EmitBCAction::EmitBCAction
EmitBCAction(llvm::LLVMContext *_VMContext=nullptr)
Definition: CodeGenAction.cpp:1187
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::BackendConsumer
Definition: CodeGenAction.cpp:103