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;
74 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
75 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
76 CoverageInfo(CoverageInfo), M(new
llvm::
Module(ModuleName, C)) {
77 C.setDiscardValueNames(CGO.DiscardValueNames);
80 ~CodeGeneratorImpl()
override {
82 assert(DeferredInlineMemberFuncDefs.empty() ||
90 llvm::Module *GetModule() {
95 return Builder->getModuleDebugInfo();
98 llvm::Module *ReleaseModule() {
102 const Decl *GetDeclForMangledName(StringRef MangledName) {
104 if (!Builder->lookupRepresentativeDecl(MangledName, Result))
107 if (
auto FD = dyn_cast<FunctionDecl>(D)) {
110 }
else if (
auto TD = dyn_cast<TagDecl>(D)) {
111 if (
auto Def = TD->getDefinition())
117 llvm::Constant *GetAddrOfGlobal(
GlobalDecl global,
bool isForDefinition) {
118 return Builder->GetAddrOfGlobal(global,
ForDefinition_t(isForDefinition));
121 llvm::Module *StartModule(llvm::StringRef ModuleName,
122 llvm::LLVMContext &C) {
123 assert(!M &&
"Replacing existing Module?");
124 M.reset(
new llvm::Module(ModuleName, C));
129 void Initialize(
ASTContext &Context)
override {
135 if (!SDKVersion.empty())
136 M->setSDKVersion(SDKVersion);
138 PreprocessorOpts, CodeGenOpts,
139 *M, Diags, CoverageInfo));
142 Builder->AddDependentLib(Lib);
144 Builder->AppendLinkerOptions(Opt);
147 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
151 Builder->HandleCXXStaticMemberVarInstantiation(VD);
158 HandlingTopLevelDeclRAII HandlingDecl(*
this);
162 Builder->EmitTopLevelDecl(*I);
167 void EmitDeferredDecls() {
168 if (DeferredInlineMemberFuncDefs.empty())
174 HandlingTopLevelDeclRAII HandlingDecl(*
this);
175 for (
unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I)
176 Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]);
177 DeferredInlineMemberFuncDefs.clear();
180 void HandleInlineFunctionDefinition(
FunctionDecl *D)
override {
194 DeferredInlineMemberFuncDefs.push_back(D);
200 Builder->AddDeferredUnusedCoverageMapping(D);
207 void HandleTagDeclDefinition(
TagDecl *D)
override {
213 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
215 Builder->UpdateCompletedType(D);
221 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
224 Builder->EmitGlobal(VD);
232 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
234 Builder->EmitGlobal(DRD);
240 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
246 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
249 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
250 DI->completeRequiredType(RD);
253 void HandleTranslationUnit(
ASTContext &Ctx)
override {
272 Builder->RefreshTypeCacheForClass(RD);
275 void CompleteTentativeDefinition(
VarDecl *D)
override {
279 Builder->EmitTentativeDefinition(D);
286 Builder->EmitVTable(RD);
291 void CodeGenerator::anchor() { }
294 return static_cast<CodeGeneratorImpl*
>(
this)->CGM();
298 return static_cast<CodeGeneratorImpl*
>(
this)->GetModule();
302 return static_cast<CodeGeneratorImpl*
>(
this)->ReleaseModule();
306 return static_cast<CodeGeneratorImpl*
>(
this)->getCGDebugInfo();
310 return static_cast<CodeGeneratorImpl*
>(
this)->GetDeclForMangledName(name);
314 bool isForDefinition) {
315 return static_cast<CodeGeneratorImpl*
>(
this)
316 ->GetAddrOfGlobal(global, isForDefinition);
320 llvm::LLVMContext &C) {
321 return static_cast<CodeGeneratorImpl*
>(
this)->StartModule(ModuleName, C);
329 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
330 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.
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