clang  3.8.0
ModuleBuilder.cpp
Go to the documentation of this file.
1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This builds an AST and converts it to LLVM Code.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "CGDebugInfo.h"
16 #include "CodeGenModule.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include <memory>
28 using namespace clang;
29 
30 namespace {
31  class CodeGeneratorImpl : public CodeGenerator {
32  DiagnosticsEngine &Diags;
33  ASTContext *Ctx;
34  const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
35  const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
36  const CodeGenOptions CodeGenOpts; // Intentionally copied in.
37 
38  unsigned HandlingTopLevelDecls;
39  struct HandlingTopLevelDeclRAII {
40  CodeGeneratorImpl &Self;
41  HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
42  ++Self.HandlingTopLevelDecls;
43  }
44  ~HandlingTopLevelDeclRAII() {
45  if (--Self.HandlingTopLevelDecls == 0)
46  Self.EmitDeferredDecls();
47  }
48  };
49 
50  CoverageSourceInfo *CoverageInfo;
51 
52  protected:
53  std::unique_ptr<llvm::Module> M;
54  std::unique_ptr<CodeGen::CodeGenModule> Builder;
55 
56  private:
57  SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
58 
59  public:
60  CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string &ModuleName,
61  const HeaderSearchOptions &HSO,
62  const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
63  llvm::LLVMContext &C,
64  CoverageSourceInfo *CoverageInfo = nullptr)
65  : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
66  PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
67  CoverageInfo(CoverageInfo),
68  M(new llvm::Module(ModuleName, C)) {}
69 
70  ~CodeGeneratorImpl() override {
71  // There should normally not be any leftover inline method definitions.
72  assert(DeferredInlineMethodDefinitions.empty() ||
73  Diags.hasErrorOccurred());
74  }
75 
76  llvm::Module* GetModule() override {
77  return M.get();
78  }
79 
80  const Decl *GetDeclForMangledName(StringRef MangledName) override {
82  if (!Builder->lookupRepresentativeDecl(MangledName, Result))
83  return nullptr;
84  const Decl *D = Result.getCanonicalDecl().getDecl();
85  if (auto FD = dyn_cast<FunctionDecl>(D)) {
86  if (FD->hasBody(FD))
87  return FD;
88  } else if (auto TD = dyn_cast<TagDecl>(D)) {
89  if (auto Def = TD->getDefinition())
90  return Def;
91  }
92  return D;
93  }
94 
95  llvm::Module *ReleaseModule() override { return M.release(); }
96 
97  void Initialize(ASTContext &Context) override {
98  Ctx = &Context;
99 
100  M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
101  M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
102  Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
103  PreprocessorOpts, CodeGenOpts,
104  *M, Diags, CoverageInfo));
105 
106  for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
107  HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
108  }
109 
110  void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
111  if (Diags.hasErrorOccurred())
112  return;
113 
114  Builder->HandleCXXStaticMemberVarInstantiation(VD);
115  }
116 
117  bool HandleTopLevelDecl(DeclGroupRef DG) override {
118  if (Diags.hasErrorOccurred())
119  return true;
120 
121  HandlingTopLevelDeclRAII HandlingDecl(*this);
122 
123  // Make sure to emit all elements of a Decl.
124  for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
125  Builder->EmitTopLevelDecl(*I);
126 
127  return true;
128  }
129 
130  void EmitDeferredDecls() {
131  if (DeferredInlineMethodDefinitions.empty())
132  return;
133 
134  // Emit any deferred inline method definitions. Note that more deferred
135  // methods may be added during this loop, since ASTConsumer callbacks
136  // can be invoked if AST inspection results in declarations being added.
137  HandlingTopLevelDeclRAII HandlingDecl(*this);
138  for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
139  Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
140  DeferredInlineMethodDefinitions.clear();
141  }
142 
143  void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
144  if (Diags.hasErrorOccurred())
145  return;
146 
147  assert(D->doesThisDeclarationHaveABody());
148 
149  // We may want to emit this definition. However, that decision might be
150  // based on computing the linkage, and we have to defer that in case we
151  // are inside of something that will change the method's final linkage,
152  // e.g.
153  // typedef struct {
154  // void bar();
155  // void foo() { bar(); }
156  // } A;
157  DeferredInlineMethodDefinitions.push_back(D);
158 
159  // Provide some coverage mapping even for methods that aren't emitted.
160  // Don't do this for templated classes though, as they may not be
161  // instantiable.
163  Builder->AddDeferredUnusedCoverageMapping(D);
164  }
165 
166  /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
167  /// to (e.g. struct, union, enum, class) is completed. This allows the
168  /// client hack on the type, which can occur at any point in the file
169  /// (because these can be defined in declspecs).
170  void HandleTagDeclDefinition(TagDecl *D) override {
171  if (Diags.hasErrorOccurred())
172  return;
173 
174  Builder->UpdateCompletedType(D);
175 
176  // For MSVC compatibility, treat declarations of static data members with
177  // inline initializers as definitions.
178  if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
179  for (Decl *Member : D->decls()) {
180  if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
181  if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
182  Ctx->DeclMustBeEmitted(VD)) {
183  Builder->EmitGlobal(VD);
184  }
185  }
186  }
187  }
188  }
189 
190  void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
191  if (Diags.hasErrorOccurred())
192  return;
193 
194  if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
195  if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
196  DI->completeRequiredType(RD);
197  }
198 
199  void HandleTranslationUnit(ASTContext &Ctx) override {
200  if (Diags.hasErrorOccurred()) {
201  if (Builder)
202  Builder->clear();
203  M.reset();
204  return;
205  }
206 
207  if (Builder)
208  Builder->Release();
209  }
210 
211  void CompleteTentativeDefinition(VarDecl *D) override {
212  if (Diags.hasErrorOccurred())
213  return;
214 
215  Builder->EmitTentativeDefinition(D);
216  }
217 
218  void HandleVTable(CXXRecordDecl *RD) override {
219  if (Diags.hasErrorOccurred())
220  return;
221 
222  Builder->EmitVTable(RD);
223  }
224 
225  void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
226  Builder->AppendLinkerOptions(Opts);
227  }
228 
229  void HandleDetectMismatch(llvm::StringRef Name,
230  llvm::StringRef Value) override {
231  Builder->AddDetectMismatch(Name, Value);
232  }
233 
234  void HandleDependentLibrary(llvm::StringRef Lib) override {
235  Builder->AddDependentLib(Lib);
236  }
237  };
238 }
239 
240 void CodeGenerator::anchor() { }
241 
243  DiagnosticsEngine &Diags, const std::string &ModuleName,
244  const HeaderSearchOptions &HeaderSearchOpts,
245  const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
246  llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
247  return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
248  PreprocessorOpts, CGO, C, CoverageInfo);
249 }
Defines the clang::ASTContext interface.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:1439
iterator end()
Definition: DeclGroup.h:108
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:77
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...
Definition: Decl.h:699
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:51
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:52
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3166
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:91
const Decl * getDecl() const
Definition: GlobalDecl.h:60
Describes a module or submodule.
Definition: Basic/Module.h:47
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:1801
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
iterator begin()
Definition: DeclGroup.h:102
detail::InMemoryDirectory::const_iterator I
ASTContext * Context
The result type of a method or function.
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:28
bool doesThisDeclarationHaveABody() const
doesThisDeclarationHaveABody - Returns whether this specific declaration of the function has a body -...
Definition: Decl.h:1732
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2644
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1701
ClassTemplateDecl * getDescribedClassTemplate() const
Retrieves the class template that is described by this class declaration.
Definition: DeclCXX.cpp:1235
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.
Definition: DeclCXX.h:285
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.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...