23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
28 using namespace clang;
38 unsigned HandlingTopLevelDecls;
39 struct HandlingTopLevelDeclRAII {
40 CodeGeneratorImpl &Self;
41 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
42 ++Self.HandlingTopLevelDecls;
44 ~HandlingTopLevelDeclRAII() {
45 if (--Self.HandlingTopLevelDecls == 0)
46 Self.EmitDeferredDecls();
53 std::unique_ptr<llvm::Module> M;
54 std::unique_ptr<CodeGen::CodeGenModule>
Builder;
65 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
66 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
67 CoverageInfo(CoverageInfo),
68 M(new llvm::
Module(ModuleName, C)) {}
70 ~CodeGeneratorImpl()
override {
72 assert(DeferredInlineMethodDefinitions.empty() ||
73 Diags.hasErrorOccurred());
76 llvm::Module* GetModule()
override {
80 const Decl *GetDeclForMangledName(StringRef MangledName)
override {
82 if (!
Builder->lookupRepresentativeDecl(MangledName, Result))
85 if (
auto FD = dyn_cast<FunctionDecl>(D)) {
88 }
else if (
auto TD = dyn_cast<TagDecl>(D)) {
89 if (
auto Def = TD->getDefinition())
95 llvm::Module *ReleaseModule()
override {
return M.release(); }
100 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
101 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
103 PreprocessorOpts, CodeGenOpts,
104 *M, Diags, CoverageInfo));
106 for (
size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
107 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
110 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
111 if (Diags.hasErrorOccurred())
114 Builder->HandleCXXStaticMemberVarInstantiation(VD);
118 if (Diags.hasErrorOccurred())
121 HandlingTopLevelDeclRAII HandlingDecl(*
this);
130 void EmitDeferredDecls() {
131 if (DeferredInlineMethodDefinitions.empty())
137 HandlingTopLevelDeclRAII HandlingDecl(*
this);
138 for (
unsigned I = 0;
I != DeferredInlineMethodDefinitions.size(); ++
I)
139 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[
I]);
140 DeferredInlineMethodDefinitions.clear();
143 void HandleInlineMethodDefinition(
CXXMethodDecl *D)
override {
144 if (Diags.hasErrorOccurred())
157 DeferredInlineMethodDefinitions.push_back(D);
163 Builder->AddDeferredUnusedCoverageMapping(D);
170 void HandleTagDeclDefinition(
TagDecl *D)
override {
171 if (Diags.hasErrorOccurred())
174 Builder->UpdateCompletedType(D);
178 if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
180 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
181 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
182 Ctx->DeclMustBeEmitted(VD)) {
190 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
191 if (Diags.hasErrorOccurred())
195 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
196 DI->completeRequiredType(RD);
199 void HandleTranslationUnit(
ASTContext &Ctx)
override {
200 if (Diags.hasErrorOccurred()) {
211 void CompleteTentativeDefinition(
VarDecl *D)
override {
212 if (Diags.hasErrorOccurred())
215 Builder->EmitTentativeDefinition(D);
219 if (Diags.hasErrorOccurred())
225 void HandleLinkerOptionPragma(llvm::StringRef Opts)
override {
226 Builder->AppendLinkerOptions(Opts);
229 void HandleDetectMismatch(llvm::StringRef
Name,
230 llvm::StringRef
Value)
override {
231 Builder->AddDetectMismatch(Name, Value);
234 void HandleDependentLibrary(llvm::StringRef Lib)
override {
240 void CodeGenerator::anchor() { }
247 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
248 PreprocessorOpts, CGO, C, CoverageInfo);
Defines the clang::ASTContext interface.
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.
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...
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.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const Decl * getDecl() const
Describes a module or submodule.
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Concrete class used by the front-end to report problems and issues.
detail::InMemoryDirectory::const_iterator I
The result type of a method or function.
GlobalDecl - represents a global declaration.
bool doesThisDeclarationHaveABody() const
doesThisDeclarationHaveABody - Returns whether this specific declaration of the function has a body -...
TagDecl - Represents the declaration of a struct/union/class/enum.
Represents a static or instance method of a struct/union/class.
ClassTemplateDecl * getDescribedClassTemplate() const
Retrieves the class template that is described by this class declaration.
This class organizes the cross-function state that is used while generating LLVM code.
detail::InMemoryDirectory::const_iterator E
Defines the Diagnostic-related interfaces.
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
CodeGenerator * CreateLLVMCodeGen(DiagnosticsEngine &Diags, const std::string &ModuleName, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
CreateLLVMCodeGen - Create a CodeGenerator instance.
Defines the clang::TargetInfo interface.