22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/Module.h" 28 using namespace clang;
29 using namespace CodeGen;
39 unsigned HandlingTopLevelDecls;
44 struct HandlingTopLevelDeclRAII {
45 CodeGeneratorImpl &Self;
47 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self,
48 bool EmitDeferred =
true)
49 : Self(Self), EmitDeferred(EmitDeferred) {
50 ++Self.HandlingTopLevelDecls;
52 ~HandlingTopLevelDeclRAII() {
53 unsigned Level = --Self.HandlingTopLevelDecls;
54 if (Level == 0 && EmitDeferred)
55 Self.EmitDeferredDecls();
62 std::unique_ptr<llvm::Module> M;
63 std::unique_ptr<CodeGen::CodeGenModule> Builder;
68 static llvm::StringRef ExpandModuleName(llvm::StringRef ModuleName,
81 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
82 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
83 CoverageInfo(CoverageInfo),
84 M(new
llvm::
Module(ExpandModuleName(ModuleName, CGO), C)) {
85 C.setDiscardValueNames(CGO.DiscardValueNames);
88 ~CodeGeneratorImpl()
override {
90 assert(DeferredInlineMemberFuncDefs.empty() ||
98 llvm::Module *GetModule() {
103 return Builder->getModuleDebugInfo();
106 llvm::Module *ReleaseModule() {
110 const Decl *GetDeclForMangledName(StringRef MangledName) {
112 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
115 if (
auto FD = dyn_cast<FunctionDecl>(D)) {
118 }
else if (
auto TD = dyn_cast<TagDecl>(D)) {
119 if (
auto Def = TD->getDefinition())
125 llvm::Constant *GetAddrOfGlobal(
GlobalDecl global,
bool isForDefinition) {
126 return Builder->GetAddrOfGlobal(global,
ForDefinition_t(isForDefinition));
129 llvm::Module *StartModule(llvm::StringRef ModuleName,
130 llvm::LLVMContext &C) {
131 assert(!M &&
"Replacing existing Module?");
132 M.reset(
new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C));
137 void Initialize(
ASTContext &Context)
override {
143 if (!SDKVersion.empty())
144 M->setSDKVersion(SDKVersion);
146 PreprocessorOpts, CodeGenOpts,
147 *M, Diags, CoverageInfo));
150 Builder->AddDependentLib(Lib);
152 Builder->AppendLinkerOptions(Opt);
155 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
159 Builder->HandleCXXStaticMemberVarInstantiation(VD);
166 HandlingTopLevelDeclRAII HandlingDecl(*
this);
170 Builder->EmitTopLevelDecl(*I);
175 void EmitDeferredDecls() {
176 if (DeferredInlineMemberFuncDefs.empty())
182 HandlingTopLevelDeclRAII HandlingDecl(*
this);
183 for (
unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I)
184 Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]);
185 DeferredInlineMemberFuncDefs.clear();
188 void HandleInlineFunctionDefinition(
FunctionDecl *D)
override {
202 DeferredInlineMemberFuncDefs.push_back(D);
208 Builder->AddDeferredUnusedCoverageMapping(D);
215 void HandleTagDeclDefinition(
TagDecl *D)
override {
221 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
223 Builder->UpdateCompletedType(D);
229 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
232 Builder->EmitGlobal(VD);
240 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
242 Builder->EmitGlobal(DRD);
243 }
else if (
auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Member)) {
245 Builder->EmitGlobal(DMD);
251 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
257 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
260 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
261 DI->completeRequiredType(RD);
264 void HandleTranslationUnit(
ASTContext &Ctx)
override {
283 Builder->RefreshTypeCacheForClass(RD);
286 void CompleteTentativeDefinition(
VarDecl *D)
override {
290 Builder->EmitTentativeDefinition(D);
293 void CompleteExternalDeclaration(
VarDecl *D)
override {
294 Builder->EmitExternalDeclaration(D);
301 Builder->EmitVTable(RD);
306 void CodeGenerator::anchor() { }
309 return static_cast<CodeGeneratorImpl*
>(
this)->CGM();
313 return static_cast<CodeGeneratorImpl*
>(
this)->GetModule();
317 return static_cast<CodeGeneratorImpl*
>(
this)->ReleaseModule();
321 return static_cast<CodeGeneratorImpl*
>(
this)->getCGDebugInfo();
325 return static_cast<CodeGeneratorImpl*
>(
this)->GetDeclForMangledName(name);
329 bool isForDefinition) {
330 return static_cast<CodeGeneratorImpl*
>(
this)
331 ->GetAddrOfGlobal(global, isForDefinition);
335 llvm::LLVMContext &C) {
336 return static_cast<CodeGeneratorImpl*
>(
this)->StartModule(ModuleName, C);
344 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
345 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
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
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.
std::string MainFileName
The user provided name for the "main file", if non-empty.
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