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(DeferredInlineMemberFuncDefs.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 if (!SDKVersion.empty())
137 M->setSDKVersion(SDKVersion);
139 PreprocessorOpts, CodeGenOpts,
140 *M, Diags, CoverageInfo));
143 Builder->AddDependentLib(Lib);
145 Builder->AppendLinkerOptions(Opt);
148 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
152 Builder->HandleCXXStaticMemberVarInstantiation(VD);
159 HandlingTopLevelDeclRAII HandlingDecl(*
this);
163 Builder->EmitTopLevelDecl(*I);
168 void EmitDeferredDecls() {
169 if (DeferredInlineMemberFuncDefs.empty())
175 HandlingTopLevelDeclRAII HandlingDecl(*
this);
176 for (
unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I)
177 Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]);
178 DeferredInlineMemberFuncDefs.clear();
181 void HandleInlineFunctionDefinition(
FunctionDecl *D)
override {
195 DeferredInlineMemberFuncDefs.push_back(D);
201 Builder->AddDeferredUnusedCoverageMapping(D);
208 void HandleTagDeclDefinition(
TagDecl *D)
override {
214 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
216 Builder->UpdateCompletedType(D);
222 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
225 Builder->EmitGlobal(VD);
233 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
235 Builder->EmitGlobal(DRD);
241 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
247 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
250 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
251 DI->completeRequiredType(RD);
254 void HandleTranslationUnit(
ASTContext &Ctx)
override {
273 Builder->RefreshTypeCacheForClass(RD);
276 void CompleteTentativeDefinition(
VarDecl *D)
override {
280 Builder->EmitTentativeDefinition(D);
287 Builder->EmitVTable(RD);
292 void CodeGenerator::anchor() { }
295 return static_cast<CodeGeneratorImpl*
>(
this)->CGM();
299 return static_cast<CodeGeneratorImpl*
>(
this)->GetModule();
303 return static_cast<CodeGeneratorImpl*
>(
this)->ReleaseModule();
307 return static_cast<CodeGeneratorImpl*
>(
this)->getCGDebugInfo();
311 return static_cast<CodeGeneratorImpl*
>(
this)->GetDeclForMangledName(name);
315 bool isForDefinition) {
316 return static_cast<CodeGeneratorImpl*
>(
this)
317 ->GetAddrOfGlobal(global, isForDefinition);
321 llvm::LLVMContext &C) {
322 return static_cast<CodeGeneratorImpl*
>(
this)->StartModule(ModuleName, C);
330 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
331 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 ...
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 llvm::VersionTuple & getSDKVersion() const
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