23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/LLVMContext.h" 26 #include "llvm/IR/Module.h" 29 using namespace clang;
30 using namespace CodeGen;
40 unsigned HandlingTopLevelDecls;
45 struct HandlingTopLevelDeclRAII {
46 CodeGeneratorImpl &Self;
48 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self,
49 bool EmitDeferred =
true)
50 : Self(Self), EmitDeferred(EmitDeferred) {
51 ++Self.HandlingTopLevelDecls;
53 ~HandlingTopLevelDeclRAII() {
54 unsigned Level = --Self.HandlingTopLevelDecls;
55 if (Level == 0 && EmitDeferred)
56 Self.EmitDeferredDecls();
63 std::unique_ptr<llvm::Module> M;
64 std::unique_ptr<CodeGen::CodeGenModule> Builder;
75 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
76 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
77 CoverageInfo(CoverageInfo), M(new
llvm::
Module(ModuleName, C)) {
78 C.setDiscardValueNames(CGO.DiscardValueNames);
81 ~CodeGeneratorImpl()
override {
83 assert(DeferredInlineMethodDefinitions.empty() ||
91 llvm::Module *GetModule() {
96 return Builder->getModuleDebugInfo();
99 llvm::Module *ReleaseModule() {
103 const Decl *GetDeclForMangledName(StringRef MangledName) {
105 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
108 if (
auto FD = dyn_cast<FunctionDecl>(D)) {
111 }
else if (
auto TD = dyn_cast<TagDecl>(D)) {
112 if (
auto Def = TD->getDefinition())
118 llvm::Constant *GetAddrOfGlobal(
GlobalDecl global,
bool isForDefinition) {
119 return Builder->GetAddrOfGlobal(global,
ForDefinition_t(isForDefinition));
122 llvm::Module *StartModule(llvm::StringRef ModuleName,
123 llvm::LLVMContext &C) {
124 assert(!M &&
"Replacing existing Module?");
125 M.reset(
new llvm::Module(ModuleName, C));
130 void Initialize(
ASTContext &Context)
override {
136 PreprocessorOpts, CodeGenOpts,
137 *M, Diags, CoverageInfo));
140 Builder->AddDependentLib(Lib);
142 Builder->AppendLinkerOptions(Opt);
145 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
149 Builder->HandleCXXStaticMemberVarInstantiation(VD);
156 HandlingTopLevelDeclRAII HandlingDecl(*
this);
160 Builder->EmitTopLevelDecl(*I);
165 void EmitDeferredDecls() {
166 if (DeferredInlineMethodDefinitions.empty())
172 HandlingTopLevelDeclRAII HandlingDecl(*
this);
173 for (
unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
174 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
175 DeferredInlineMethodDefinitions.clear();
178 void HandleInlineFunctionDefinition(
FunctionDecl *D)
override {
188 Builder->EmitTopLevelDecl(D);
193 auto MD = cast<CXXMethodDecl>(D);
203 DeferredInlineMethodDefinitions.push_back(MD);
208 if (!MD->getParent()->isDependentContext())
209 Builder->AddDeferredUnusedCoverageMapping(MD);
216 void HandleTagDeclDefinition(
TagDecl *D)
override {
222 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
224 Builder->UpdateCompletedType(D);
230 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
233 Builder->EmitGlobal(VD);
241 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
243 Builder->EmitGlobal(DRD);
249 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
255 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
258 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
259 DI->completeRequiredType(RD);
262 void HandleTranslationUnit(
ASTContext &Ctx)
override {
281 Builder->RefreshTypeCacheForClass(RD);
284 void CompleteTentativeDefinition(
VarDecl *D)
override {
288 Builder->EmitTentativeDefinition(D);
295 Builder->EmitVTable(RD);
300 void CodeGenerator::anchor() { }
303 return static_cast<CodeGeneratorImpl*
>(
this)->CGM();
307 return static_cast<CodeGeneratorImpl*
>(
this)->GetModule();
311 return static_cast<CodeGeneratorImpl*
>(
this)->ReleaseModule();
315 return static_cast<CodeGeneratorImpl*
>(
this)->getCGDebugInfo();
319 return static_cast<CodeGeneratorImpl*
>(
this)->GetDeclForMangledName(name);
323 bool isForDefinition) {
324 return static_cast<CodeGeneratorImpl*
>(
this)
325 ->GetAddrOfGlobal(global, isForDefinition);
329 llvm::LLVMContext &C) {
330 return static_cast<CodeGeneratorImpl*
>(
this)->StartModule(ModuleName, C);
338 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
339 PreprocessorOpts, CGO, C, CoverageInfo);
Defines the clang::ASTContext interface.
llvm::Constant * GetAddrOfGlobal(GlobalDecl decl, bool isForDefinition)
Return the LLVM address of the given global entity.
Represents a function declaration or definition.
bool hasErrorOccurred() const
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Decl - This represents one declaration (or definition), e.g.
GlobalDecl getCanonicalDecl() const
Stores additional source code information like skipped ranges which is required by the coverage mappi...
llvm::Module * StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C)
Create a new llvm::Module after calling HandleTranslationUnit.
const TargetInfo & getTargetInfo() const
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
llvm::Module * ReleaseModule()
Release ownership of the module to the caller.
Represents a variable declaration or definition.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Represents a struct/union/class.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
This declaration is a friend function.
bool isInIdentifierNamespace(unsigned NS) const
Describes a module or submodule.
Concrete class used by the front-end to report problems and issues.
Defines the Diagnostic-related interfaces.
CodeGenerator * CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
CreateLLVMCodeGen - Create a CodeGenerator instance.
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
The primary public interface to the Clang code generator.
std::vector< std::string > DependentLibraries
A list of dependent libraries.
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
llvm::Module * GetModule()
Return the module that this code generator is building into.
GlobalDecl - represents a global declaration.
CodeGen::CodeGenModule & CGM()
Return an opaque reference to the CodeGenModule object, which can be used in various secondary APIs...
const Decl * GetDeclForMangledName(llvm::StringRef MangledName)
Given a mangled name, return a declaration which mangles that way which has been added to this code g...
Represents the declaration of a struct/union/class/enum.
const Decl * getDecl() const
const llvm::DataLayout & getDataLayout() const
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
bool isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const
Returns true if this is an inline-initialized static data member which is treated as a definition for...
std::vector< std::string > LinkerOptions
A list of linker options to embed in the object file.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Represents a C++ struct/union/class.
Defines the clang::TargetInfo interface.
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
CodeGen::CGDebugInfo * getCGDebugInfo()
Return debug info code generator.
bool DeclMustBeEmitted(const Decl *D)
Determines if the decl can be CodeGen'ed or deserialized from PCH lazily, only when used; this is onl...
const LangOptions & getLangOpts() const