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() ||
84 Diags.hasErrorOccurred());
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) {
125 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
126 M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
128 PreprocessorOpts, CodeGenOpts,
129 *M, Diags, CoverageInfo));
131 for (
auto &&Lib : CodeGenOpts.DependentLibraries)
133 for (
auto &&Opt : CodeGenOpts.LinkerOptions)
134 Builder->AppendLinkerOptions(Opt);
137 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
138 if (Diags.hasErrorOccurred())
141 Builder->HandleCXXStaticMemberVarInstantiation(VD);
145 if (Diags.hasErrorOccurred())
148 HandlingTopLevelDeclRAII HandlingDecl(*
this);
157 void EmitDeferredDecls() {
158 if (DeferredInlineMethodDefinitions.empty())
164 HandlingTopLevelDeclRAII HandlingDecl(*
this);
165 for (
unsigned I = 0;
I != DeferredInlineMethodDefinitions.size(); ++
I)
166 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[
I]);
167 DeferredInlineMethodDefinitions.clear();
170 void HandleInlineFunctionDefinition(
FunctionDecl *D)
override {
171 if (Diags.hasErrorOccurred())
178 if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()
185 auto MD = cast<CXXMethodDecl>(D);
195 DeferredInlineMethodDefinitions.push_back(MD);
200 if (!MD->getParent()->isDependentContext())
201 Builder->AddDeferredUnusedCoverageMapping(MD);
208 void HandleTagDeclDefinition(
TagDecl *D)
override {
209 if (Diags.hasErrorOccurred())
214 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
216 Builder->UpdateCompletedType(D);
220 if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
222 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
223 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
224 Ctx->DeclMustBeEmitted(VD)) {
231 if (Ctx->getLangOpts().OpenMP) {
233 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
234 if (Ctx->DeclMustBeEmitted(DRD))
241 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
242 if (Diags.hasErrorOccurred())
247 HandlingTopLevelDeclRAII HandlingDecl(*
this,
false);
250 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
251 DI->completeRequiredType(RD);
254 void HandleTranslationUnit(
ASTContext &Ctx)
override {
256 if (!Diags.hasErrorOccurred() &&
Builder)
261 if (Diags.hasErrorOccurred()) {
270 if (Diags.hasErrorOccurred())
273 Builder->RefreshTypeCacheForClass(RD);
276 void CompleteTentativeDefinition(
VarDecl *D)
override {
277 if (Diags.hasErrorOccurred())
280 Builder->EmitTentativeDefinition(D);
284 if (Diags.hasErrorOccurred())
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);
325 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
326 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.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Stores additional source code information like skipped ranges which is required by the coverage mappi...
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.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
GlobalDecl getCanonicalDecl() const
RecordDecl - Represents a struct/union/class.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
bool isInIdentifierNamespace(unsigned NS) const
This declaration is a friend function.
const Decl * getDecl() 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.
detail::InMemoryDirectory::const_iterator I
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...
bool doesThisDeclarationHaveABody() const
doesThisDeclarationHaveABody - Returns whether this specific declaration of the function has a body -...
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...
TagDecl - Represents the declaration of a struct/union/class/enum.
This class organizes the cross-function state that is used while generating LLVM code.
detail::InMemoryDirectory::const_iterator E
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Represents a C++ struct/union/class.
BoundNodesTreeBuilder *const Builder
Defines the clang::TargetInfo interface.
CodeGen::CGDebugInfo * getCGDebugInfo()
Return debug info code generator.