blob: 81354b86cab5dd18e22519ed265656e69bae6f99 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
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 coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie9dfd2432013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
40using namespace clang;
41using namespace clang::CodeGen;
42
43CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher688cf5b2013-07-14 21:12:44 +000044 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
45 DBuilder(CGM.getModule()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000046 CreateCompileUnit();
47}
48
49CGDebugInfo::~CGDebugInfo() {
50 assert(LexicalBlockStack.empty() &&
51 "Region stack mismatch, stack not empty!");
52}
53
Adrian Prantled6bbe42013-07-18 00:28:02 +000054
55NoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B)
56 : DI(CGF.getDebugInfo()), Builder(B) {
57 if (DI) {
58 SavedLoc = DI->getLocation();
59 DI->CurLoc = SourceLocation();
60 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
61 }
62}
63
64NoLocation::~NoLocation() {
65 if (DI) {
66 assert(Builder.getCurrentDebugLocation().isUnknown());
67 DI->CurLoc = SavedLoc;
68 }
69}
70
Adrian Prantlb061ce22013-07-18 01:36:04 +000071ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Adrian Prantled6bbe42013-07-18 00:28:02 +000072 : DI(CGF.getDebugInfo()), Builder(B) {
73 if (DI) {
74 SavedLoc = DI->getLocation();
Adrian Prantlb6cdc962013-07-24 20:34:39 +000075 DI->CurLoc = SourceLocation();
76 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
77 }
78}
79
80void ArtificialLocation::Emit() {
81 if (DI) {
Adrian Prantled6bbe42013-07-18 00:28:02 +000082 // Sync the Builder.
83 DI->EmitLocation(Builder, SavedLoc);
84 DI->CurLoc = SourceLocation();
85 // Construct a location that has a valid scope, but no line info.
Adrian Prantlb6cdc962013-07-24 20:34:39 +000086 assert(!DI->LexicalBlockStack.empty());
87 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
Adrian Prantled6bbe42013-07-18 00:28:02 +000088 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
89 }
90}
91
Adrian Prantlb061ce22013-07-18 01:36:04 +000092ArtificialLocation::~ArtificialLocation() {
Adrian Prantled6bbe42013-07-18 00:28:02 +000093 if (DI) {
94 assert(Builder.getCurrentDebugLocation().getLine() == 0);
95 DI->CurLoc = SavedLoc;
96 }
97}
98
Guy Benyei7f92f2d2012-12-18 14:30:41 +000099void CGDebugInfo::setLocation(SourceLocation Loc) {
100 // If the new location isn't valid return.
Adrian Prantl5f4554f2013-07-18 00:27:56 +0000101 if (Loc.isInvalid()) return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000102
103 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
104
105 // If we've changed files in the middle of a lexical scope go ahead
106 // and create a new lexical scope with file node if it's different
107 // from the one in the scope.
108 if (LexicalBlockStack.empty()) return;
109
110 SourceManager &SM = CGM.getContext().getSourceManager();
111 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
112 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
113
114 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
115 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
116 return;
117
118 llvm::MDNode *LB = LexicalBlockStack.back();
119 llvm::DIScope Scope = llvm::DIScope(LB);
120 if (Scope.isLexicalBlockFile()) {
121 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB);
122 llvm::DIDescriptor D
123 = DBuilder.createLexicalBlockFile(LBF.getScope(),
124 getOrCreateFile(CurLoc));
125 llvm::MDNode *N = D;
126 LexicalBlockStack.pop_back();
127 LexicalBlockStack.push_back(N);
David Blaikiea6504852013-01-26 22:16:26 +0000128 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000129 llvm::DIDescriptor D
130 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
131 llvm::MDNode *N = D;
132 LexicalBlockStack.pop_back();
133 LexicalBlockStack.push_back(N);
134 }
135}
136
137/// getContextDescriptor - Get context info for the decl.
David Blaikiebb000792013-04-19 06:56:38 +0000138llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000139 if (!Context)
140 return TheCU;
141
142 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
143 I = RegionMap.find(Context);
144 if (I != RegionMap.end()) {
145 llvm::Value *V = I->second;
David Blaikiebb000792013-04-19 06:56:38 +0000146 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000147 }
148
149 // Check namespace.
150 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebb000792013-04-19 06:56:38 +0000151 return getOrCreateNameSpace(NSDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000152
David Blaikiebb000792013-04-19 06:56:38 +0000153 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
154 if (!RDecl->isDependentType())
155 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000156 getOrCreateMainFile());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000157 return TheCU;
158}
159
160/// getFunctionName - Get function name for the given FunctionDecl. If the
161/// name is constructred on demand (e.g. C++ destructor) then the name
162/// is stored on the side.
163StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
164 assert (FD && "Invalid FunctionDecl!");
165 IdentifierInfo *FII = FD->getIdentifier();
166 FunctionTemplateSpecializationInfo *Info
167 = FD->getTemplateSpecializationInfo();
168 if (!Info && FII)
169 return FII->getName();
170
171 // Otherwise construct human readable name for debug info.
Benjamin Kramer5eada842013-02-22 15:46:01 +0000172 SmallString<128> NS;
173 llvm::raw_svector_ostream OS(NS);
174 FD->printName(OS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000175
176 // Add any template specialization args.
177 if (Info) {
178 const TemplateArgumentList *TArgs = Info->TemplateArguments;
179 const TemplateArgument *Args = TArgs->data();
180 unsigned NumArgs = TArgs->size();
181 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer5eada842013-02-22 15:46:01 +0000182 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
183 Policy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000184 }
185
186 // Copy this name on the side and use its reference.
Benjamin Kramer5eada842013-02-22 15:46:01 +0000187 OS.flush();
188 char *StrPtr = DebugInfoNames.Allocate<char>(NS.size());
189 memcpy(StrPtr, NS.data(), NS.size());
190 return StringRef(StrPtr, NS.size());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000191}
192
193StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
194 SmallString<256> MethodName;
195 llvm::raw_svector_ostream OS(MethodName);
196 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
197 const DeclContext *DC = OMD->getDeclContext();
Eric Christopher6537f082013-05-16 00:45:12 +0000198 if (const ObjCImplementationDecl *OID =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000199 dyn_cast<const ObjCImplementationDecl>(DC)) {
200 OS << OID->getName();
Eric Christopher6537f082013-05-16 00:45:12 +0000201 } else if (const ObjCInterfaceDecl *OID =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000202 dyn_cast<const ObjCInterfaceDecl>(DC)) {
203 OS << OID->getName();
Eric Christopher6537f082013-05-16 00:45:12 +0000204 } else if (const ObjCCategoryImplDecl *OCD =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000205 dyn_cast<const ObjCCategoryImplDecl>(DC)){
206 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
207 OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb5092242013-05-17 23:58:45 +0000208 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl687ecae2013-05-17 23:49:10 +0000209 // We can extract the type of the class from the self pointer.
210 if (ImplicitParamDecl* SelfDecl = OMD->getSelfDecl()) {
211 QualType ClassTy =
212 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
213 ClassTy.print(OS, PrintingPolicy(LangOptions()));
214 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000215 }
216 OS << ' ' << OMD->getSelector().getAsString() << ']';
217
218 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
219 memcpy(StrPtr, MethodName.begin(), OS.tell());
220 return StringRef(StrPtr, OS.tell());
221}
222
223/// getSelectorName - Return selector name. This is used for debugging
224/// info.
225StringRef CGDebugInfo::getSelectorName(Selector S) {
226 const std::string &SName = S.getAsString();
227 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
228 memcpy(StrPtr, SName.data(), SName.size());
229 return StringRef(StrPtr, SName.size());
230}
231
232/// getClassName - Get class name including template argument list.
Eric Christopher6537f082013-05-16 00:45:12 +0000233StringRef
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000234CGDebugInfo::getClassName(const RecordDecl *RD) {
235 const ClassTemplateSpecializationDecl *Spec
236 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
237 if (!Spec)
238 return RD->getName();
239
240 const TemplateArgument *Args;
241 unsigned NumArgs;
242 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
243 const TemplateSpecializationType *TST =
244 cast<TemplateSpecializationType>(TAW->getType());
245 Args = TST->getArgs();
246 NumArgs = TST->getNumArgs();
247 } else {
248 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
249 Args = TemplateArgs.data();
250 NumArgs = TemplateArgs.size();
251 }
252 StringRef Name = RD->getIdentifier()->getName();
253 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer5eada842013-02-22 15:46:01 +0000254 SmallString<128> TemplateArgList;
255 {
256 llvm::raw_svector_ostream OS(TemplateArgList);
257 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
258 Policy);
259 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000260
261 // Copy this name on the side and use its reference.
262 size_t Length = Name.size() + TemplateArgList.size();
263 char *StrPtr = DebugInfoNames.Allocate<char>(Length);
264 memcpy(StrPtr, Name.data(), Name.size());
265 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size());
266 return StringRef(StrPtr, Length);
267}
268
269/// getOrCreateFile - Get the file debug info descriptor for the input location.
270llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
271 if (!Loc.isValid())
272 // If Location is not valid then use main input file.
273 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
274
275 SourceManager &SM = CGM.getContext().getSourceManager();
276 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
277
278 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
279 // If the location is not valid then use main input file.
280 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
281
282 // Cache the results.
283 const char *fname = PLoc.getFilename();
284 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
285 DIFileCache.find(fname);
286
287 if (it != DIFileCache.end()) {
288 // Verify that the information still exists.
289 if (llvm::Value *V = it->second)
290 return llvm::DIFile(cast<llvm::MDNode>(V));
291 }
292
293 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
294
295 DIFileCache[fname] = F;
296 return F;
297}
298
299/// getOrCreateMainFile - Get the file info for main compile unit.
300llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
301 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
302}
303
304/// getLineNumber - Get line number for the location. If location is invalid
305/// then use current location.
306unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
307 if (Loc.isInvalid() && CurLoc.isInvalid())
308 return 0;
309 SourceManager &SM = CGM.getContext().getSourceManager();
310 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
311 return PLoc.isValid()? PLoc.getLine() : 0;
312}
313
314/// getColumnNumber - Get column number for the location.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000315unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000316 // We may not want column information at all.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000317 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000318 return 0;
319
320 // If the location is invalid then use the current column.
321 if (Loc.isInvalid() && CurLoc.isInvalid())
322 return 0;
323 SourceManager &SM = CGM.getContext().getSourceManager();
324 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
325 return PLoc.isValid()? PLoc.getColumn() : 0;
326}
327
328StringRef CGDebugInfo::getCurrentDirname() {
329 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
330 return CGM.getCodeGenOpts().DebugCompilationDir;
331
332 if (!CWDName.empty())
333 return CWDName;
334 SmallString<256> CWD;
335 llvm::sys::fs::current_path(CWD);
336 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
337 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
338 return CWDName = StringRef(CompDirnamePtr, CWD.size());
339}
340
341/// CreateCompileUnit - Create new compile unit.
342void CGDebugInfo::CreateCompileUnit() {
343
344 // Get absolute path name.
345 SourceManager &SM = CGM.getContext().getSourceManager();
346 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
347 if (MainFileName.empty())
348 MainFileName = "<unknown>";
349
350 // The main file name provided via the "-main-file-name" option contains just
351 // the file name itself with no path information. This file name may have had
352 // a relative path, so we look into the actual file entry for the main
353 // file to determine the real absolute path for the file.
354 std::string MainFileDir;
355 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
356 MainFileDir = MainFile->getDir()->getName();
357 if (MainFileDir != ".")
358 MainFileName = MainFileDir + "/" + MainFileName;
359 }
360
361 // Save filename string.
362 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
363 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
364 StringRef Filename(FilenamePtr, MainFileName.length());
Eric Christopherff971d72013-02-22 23:50:16 +0000365
366 // Save split dwarf file string.
367 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
368 char *SplitDwarfPtr = DebugInfoNames.Allocate<char>(SplitDwarfFile.length());
369 memcpy(SplitDwarfPtr, SplitDwarfFile.c_str(), SplitDwarfFile.length());
370 StringRef SplitDwarfFilename(SplitDwarfPtr, SplitDwarfFile.length());
Eric Christopher6537f082013-05-16 00:45:12 +0000371
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000372 unsigned LangTag;
373 const LangOptions &LO = CGM.getLangOpts();
374 if (LO.CPlusPlus) {
375 if (LO.ObjC1)
376 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
377 else
378 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
379 } else if (LO.ObjC1) {
380 LangTag = llvm::dwarf::DW_LANG_ObjC;
381 } else if (LO.C99) {
382 LangTag = llvm::dwarf::DW_LANG_C99;
383 } else {
384 LangTag = llvm::dwarf::DW_LANG_C89;
385 }
386
387 std::string Producer = getClangFullVersion();
388
389 // Figure out which version of the ObjC runtime we have.
390 unsigned RuntimeVers = 0;
391 if (LO.ObjC1)
392 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
393
394 // Create new compile unit.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000395 // FIXME - Eliminate TheCU.
Eric Christopher8fed3f42013-07-19 00:51:58 +0000396 TheCU = DBuilder.createCompileUnit(LangTag, Filename, getCurrentDirname(),
397 Producer, LO.Optimize,
398 CGM.getCodeGenOpts().DwarfDebugFlags,
399 RuntimeVers, SplitDwarfFilename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000400}
401
402/// CreateType - Get the Basic type from the cache or create a new
403/// one if necessary.
404llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
405 unsigned Encoding = 0;
406 StringRef BTName;
407 switch (BT->getKind()) {
408#define BUILTIN_TYPE(Id, SingletonId)
409#define PLACEHOLDER_TYPE(Id, SingletonId) \
410 case BuiltinType::Id:
411#include "clang/AST/BuiltinTypes.def"
412 case BuiltinType::Dependent:
413 llvm_unreachable("Unexpected builtin type");
414 case BuiltinType::NullPtr:
Peter Collingbourne24118f52013-06-27 22:51:01 +0000415 return DBuilder.createNullPtrType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000416 case BuiltinType::Void:
417 return llvm::DIType();
418 case BuiltinType::ObjCClass:
Eric Christopherb2d13922013-07-18 00:52:50 +0000419 if (ClassTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000420 return ClassTy;
421 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
422 "objc_class", TheCU,
423 getOrCreateMainFile(), 0);
424 return ClassTy;
425 case BuiltinType::ObjCId: {
426 // typedef struct objc_class *Class;
427 // typedef struct objc_object {
428 // Class isa;
429 // } *id;
430
Eric Christopherb2d13922013-07-18 00:52:50 +0000431 if (ObjTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000432 return ObjTy;
433
Eric Christopherb2d13922013-07-18 00:52:50 +0000434 if (!ClassTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000435 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
436 "objc_class", TheCU,
437 getOrCreateMainFile(), 0);
438
439 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopher6537f082013-05-16 00:45:12 +0000440
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000441 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
442
Eric Christopherf068c922013-04-02 22:59:11 +0000443 ObjTy =
David Blaikiec1d0af12013-02-25 01:07:08 +0000444 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
445 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000446
Eric Christopherf068c922013-04-02 22:59:11 +0000447 ObjTy.setTypeArray(DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
448 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000449 return ObjTy;
450 }
451 case BuiltinType::ObjCSel: {
Eric Christopherb2d13922013-07-18 00:52:50 +0000452 if (SelTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000453 return SelTy;
454 SelTy =
455 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
456 "objc_selector", TheCU, getOrCreateMainFile(),
457 0);
458 return SelTy;
459 }
Guy Benyeib13621d2012-12-18 14:38:23 +0000460
461 case BuiltinType::OCLImage1d:
462 return getOrCreateStructPtrType("opencl_image1d_t",
463 OCLImage1dDITy);
464 case BuiltinType::OCLImage1dArray:
Eric Christopher6537f082013-05-16 00:45:12 +0000465 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeib13621d2012-12-18 14:38:23 +0000466 OCLImage1dArrayDITy);
467 case BuiltinType::OCLImage1dBuffer:
468 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
469 OCLImage1dBufferDITy);
470 case BuiltinType::OCLImage2d:
471 return getOrCreateStructPtrType("opencl_image2d_t",
472 OCLImage2dDITy);
473 case BuiltinType::OCLImage2dArray:
474 return getOrCreateStructPtrType("opencl_image2d_array_t",
475 OCLImage2dArrayDITy);
476 case BuiltinType::OCLImage3d:
477 return getOrCreateStructPtrType("opencl_image3d_t",
478 OCLImage3dDITy);
Guy Benyei21f18c42013-02-07 10:55:47 +0000479 case BuiltinType::OCLSampler:
480 return DBuilder.createBasicType("opencl_sampler_t",
481 CGM.getContext().getTypeSize(BT),
482 CGM.getContext().getTypeAlign(BT),
483 llvm::dwarf::DW_ATE_unsigned);
Guy Benyeie6b9d802013-01-20 12:31:11 +0000484 case BuiltinType::OCLEvent:
485 return getOrCreateStructPtrType("opencl_event_t",
486 OCLEventDITy);
Guy Benyeib13621d2012-12-18 14:38:23 +0000487
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000488 case BuiltinType::UChar:
489 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
490 case BuiltinType::Char_S:
491 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
492 case BuiltinType::Char16:
493 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
494 case BuiltinType::UShort:
495 case BuiltinType::UInt:
496 case BuiltinType::UInt128:
497 case BuiltinType::ULong:
498 case BuiltinType::WChar_U:
499 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
500 case BuiltinType::Short:
501 case BuiltinType::Int:
502 case BuiltinType::Int128:
503 case BuiltinType::Long:
504 case BuiltinType::WChar_S:
505 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
506 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
507 case BuiltinType::Half:
508 case BuiltinType::Float:
509 case BuiltinType::LongDouble:
510 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
511 }
512
513 switch (BT->getKind()) {
514 case BuiltinType::Long: BTName = "long int"; break;
515 case BuiltinType::LongLong: BTName = "long long int"; break;
516 case BuiltinType::ULong: BTName = "long unsigned int"; break;
517 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
518 default:
519 BTName = BT->getName(CGM.getLangOpts());
520 break;
521 }
522 // Bit size, align and offset of the type.
523 uint64_t Size = CGM.getContext().getTypeSize(BT);
524 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christopher6537f082013-05-16 00:45:12 +0000525 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000526 DBuilder.createBasicType(BTName, Size, Align, Encoding);
527 return DbgTy;
528}
529
530llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
531 // Bit size, align and offset of the type.
532 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
533 if (Ty->isComplexIntegerType())
534 Encoding = llvm::dwarf::DW_ATE_lo_user;
535
536 uint64_t Size = CGM.getContext().getTypeSize(Ty);
537 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopher6537f082013-05-16 00:45:12 +0000538 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000539 DBuilder.createBasicType("complex", Size, Align, Encoding);
540
541 return DbgTy;
542}
543
544/// CreateCVRType - Get the qualified type from the cache or create
545/// a new one if necessary.
Eric Christopher56b108a2013-06-07 22:54:39 +0000546llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit,
547 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000548 QualifierCollector Qc;
549 const Type *T = Qc.strip(Ty);
550
551 // Ignore these qualifiers for now.
552 Qc.removeObjCGCAttr();
553 Qc.removeAddressSpace();
554 Qc.removeObjCLifetime();
555
556 // We will create one Derived type for one qualifier and recurse to handle any
557 // additional ones.
558 unsigned Tag;
559 if (Qc.hasConst()) {
560 Tag = llvm::dwarf::DW_TAG_const_type;
561 Qc.removeConst();
562 } else if (Qc.hasVolatile()) {
563 Tag = llvm::dwarf::DW_TAG_volatile_type;
564 Qc.removeVolatile();
565 } else if (Qc.hasRestrict()) {
566 Tag = llvm::dwarf::DW_TAG_restrict_type;
567 Qc.removeRestrict();
568 } else {
569 assert(Qc.empty() && "Unknown type qualifier for debug info");
570 return getOrCreateType(QualType(T, 0), Unit);
571 }
572
Eric Christopher56b108a2013-06-07 22:54:39 +0000573 llvm::DIType FromTy =
574 getOrCreateType(Qc.apply(CGM.getContext(), T), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000575
576 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
577 // CVR derived types.
578 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopher6537f082013-05-16 00:45:12 +0000579
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000580 return DbgTy;
581}
582
583llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
584 llvm::DIFile Unit) {
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000585
586 // The frontend treats 'id' as a typedef to an ObjCObjectType,
587 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
588 // debug info, we want to emit 'id' in both cases.
589 if (Ty->isObjCQualifiedIdType())
590 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
591
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000592 llvm::DIType DbgTy =
Eric Christopher6537f082013-05-16 00:45:12 +0000593 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000594 Ty->getPointeeType(), Unit);
595 return DbgTy;
596}
597
598llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
599 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +0000600 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000601 Ty->getPointeeType(), Unit);
602}
603
604// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie951094b2013-08-15 18:59:40 +0000605llvm::DICompositeType
606CGDebugInfo::getOrCreateRecordFwdDecl(const RecordDecl *RD,
607 llvm::DIDescriptor Ctx) {
608 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
609 if (T)
610 return T;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000611 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
612 unsigned Line = getLineNumber(RD->getLocation());
613 StringRef RDName = getClassName(RD);
614
615 unsigned Tag = 0;
616 if (RD->isStruct() || RD->isInterface())
617 Tag = llvm::dwarf::DW_TAG_structure_type;
618 else if (RD->isUnion())
619 Tag = llvm::dwarf::DW_TAG_union_type;
620 else {
621 assert(RD->isClass());
622 Tag = llvm::dwarf::DW_TAG_class_type;
623 }
624
625 // Create the type.
626 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
627}
628
629// Walk up the context chain and create forward decls for record decls,
630// and normal descriptors for namespaces.
631llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
632 if (!Context)
633 return TheCU;
634
635 // See if we already have the parent.
636 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
637 I = RegionMap.find(Context);
638 if (I != RegionMap.end()) {
639 llvm::Value *V = I->second;
640 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
641 }
Eric Christopher6537f082013-05-16 00:45:12 +0000642
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000643 // Check namespace.
644 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
645 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
646
647 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
648 if (!RD->isDependentType()) {
David Blaikie4a077162013-08-12 22:24:20 +0000649 llvm::DIType Ty = getOrCreateLimitedType(
650 CGM.getContext().getRecordType(RD)->castAs<RecordType>(), getOrCreateMainFile());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000651 return llvm::DIDescriptor(Ty);
652 }
653 }
654 return TheCU;
655}
656
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000657llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
Eric Christopher6537f082013-05-16 00:45:12 +0000658 const Type *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000659 QualType PointeeTy,
660 llvm::DIFile Unit) {
661 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
662 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikieb0f77b02013-05-24 21:33:22 +0000663 return DBuilder.createReferenceType(
David Blaikieb3c23772013-08-12 23:14:36 +0000664 Tag, getOrCreateType(PointeeTy, Unit, true));
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000665
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000666 // Bit size, align and offset of the type.
667 // Size is always the size of a pointer. We can't use getTypeSize here
668 // because that does not return the correct value for references.
669 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +0000670 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000671 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
672
David Blaikieb3c23772013-08-12 23:14:36 +0000673 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit, true),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000674 Size, Align);
675}
676
Eric Christopherf0890c42013-05-16 00:52:20 +0000677llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
678 llvm::DIType &Cache) {
Eric Christopherb2d13922013-07-18 00:52:50 +0000679 if (Cache)
Guy Benyeib13621d2012-12-18 14:38:23 +0000680 return Cache;
David Blaikie1e97c1e2013-05-21 17:58:54 +0000681 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
682 TheCU, getOrCreateMainFile(), 0);
683 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
684 Cache = DBuilder.createPointerType(Cache, Size);
685 return Cache;
Guy Benyeib13621d2012-12-18 14:38:23 +0000686}
687
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000688llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
689 llvm::DIFile Unit) {
Eric Christopherb2d13922013-07-18 00:52:50 +0000690 if (BlockLiteralGeneric)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000691 return BlockLiteralGeneric;
692
693 SmallVector<llvm::Value *, 8> EltTys;
694 llvm::DIType FieldTy;
695 QualType FType;
696 uint64_t FieldSize, FieldOffset;
697 unsigned FieldAlign;
698 llvm::DIArray Elements;
699 llvm::DIType EltTy, DescTy;
700
701 FieldOffset = 0;
702 FType = CGM.getContext().UnsignedLongTy;
703 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
704 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
705
706 Elements = DBuilder.getOrCreateArray(EltTys);
707 EltTys.clear();
708
709 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
710 unsigned LineNo = getLineNumber(CurLoc);
711
712 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
713 Unit, LineNo, FieldOffset, 0,
David Blaikiec1d0af12013-02-25 01:07:08 +0000714 Flags, llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000715
716 // Bit size, align and offset of the type.
717 uint64_t Size = CGM.getContext().getTypeSize(Ty);
718
719 DescTy = DBuilder.createPointerType(EltTy, Size);
720
721 FieldOffset = 0;
722 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
723 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
724 FType = CGM.getContext().IntTy;
725 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
726 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
727 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
728 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
729
730 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
731 FieldTy = DescTy;
732 FieldSize = CGM.getContext().getTypeSize(Ty);
733 FieldAlign = CGM.getContext().getTypeAlign(Ty);
734 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
735 LineNo, FieldSize, FieldAlign,
736 FieldOffset, 0, FieldTy);
737 EltTys.push_back(FieldTy);
738
739 FieldOffset += FieldSize;
740 Elements = DBuilder.getOrCreateArray(EltTys);
741
742 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
743 Unit, LineNo, FieldOffset, 0,
David Blaikiec1d0af12013-02-25 01:07:08 +0000744 Flags, llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000745
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000746 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
747 return BlockLiteralGeneric;
748}
749
David Blaikie5f6e2f42013-06-05 05:32:23 +0000750llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit,
751 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000752 // Typedefs are derived from some other type. If we have a typedef of a
753 // typedef, make sure to emit the whole chain.
David Blaikieb0f77b02013-05-24 21:33:22 +0000754 llvm::DIType Src =
David Blaikie5f6e2f42013-06-05 05:32:23 +0000755 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit, Declaration);
Eric Christopherb2d13922013-07-18 00:52:50 +0000756 if (!Src)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000757 return llvm::DIType();
758 // We don't set size information, but do specify where the typedef was
759 // declared.
760 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
761 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher6537f082013-05-16 00:45:12 +0000762
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000763 llvm::DIDescriptor TypedefContext =
764 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher6537f082013-05-16 00:45:12 +0000765
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000766 return
767 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
768}
769
770llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
771 llvm::DIFile Unit) {
772 SmallVector<llvm::Value *, 16> EltTys;
773
774 // Add the result type at least.
775 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
776
777 // Set up remainder of arguments if there is a prototype.
778 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
779 if (isa<FunctionNoProtoType>(Ty))
780 EltTys.push_back(DBuilder.createUnspecifiedParameter());
781 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
782 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
783 EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit));
784 }
785
786 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
787 return DBuilder.createSubroutineType(Unit, EltTypeArray);
788}
789
790
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000791llvm::DIType CGDebugInfo::createFieldType(StringRef name,
792 QualType type,
793 uint64_t sizeInBitsOverride,
794 SourceLocation loc,
795 AccessSpecifier AS,
796 uint64_t offsetInBits,
797 llvm::DIFile tunit,
798 llvm::DIDescriptor scope) {
799 llvm::DIType debugType = getOrCreateType(type, tunit);
800
801 // Get the location for the field.
802 llvm::DIFile file = getOrCreateFile(loc);
803 unsigned line = getLineNumber(loc);
804
805 uint64_t sizeInBits = 0;
806 unsigned alignInBits = 0;
807 if (!type->isIncompleteArrayType()) {
808 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
809
810 if (sizeInBitsOverride)
811 sizeInBits = sizeInBitsOverride;
812 }
813
814 unsigned flags = 0;
815 if (AS == clang::AS_private)
816 flags |= llvm::DIDescriptor::FlagPrivate;
817 else if (AS == clang::AS_protected)
818 flags |= llvm::DIDescriptor::FlagProtected;
819
820 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
821 alignInBits, offsetInBits, flags, debugType);
822}
823
Eric Christopher0395de32013-01-16 01:22:32 +0000824/// CollectRecordLambdaFields - Helper for CollectRecordFields.
825void CGDebugInfo::
826CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
827 SmallVectorImpl<llvm::Value *> &elements,
828 llvm::DIType RecordTy) {
829 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
830 // has the name and the location of the variable so we should iterate over
831 // both concurrently.
832 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
833 RecordDecl::field_iterator Field = CXXDecl->field_begin();
834 unsigned fieldno = 0;
835 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
836 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
837 const LambdaExpr::Capture C = *I;
838 if (C.capturesVariable()) {
839 VarDecl *V = C.getCapturedVar();
840 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
841 StringRef VName = V->getName();
842 uint64_t SizeInBitsOverride = 0;
843 if (Field->isBitField()) {
844 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
845 assert(SizeInBitsOverride && "found named 0-width bitfield");
846 }
847 llvm::DIType fieldType
848 = createFieldType(VName, Field->getType(), SizeInBitsOverride,
849 C.getLocation(), Field->getAccess(),
850 layout.getFieldOffset(fieldno), VUnit, RecordTy);
851 elements.push_back(fieldType);
852 } else {
853 // TODO: Need to handle 'this' in some way by probably renaming the
854 // this of the lambda class and having a field member of 'this' or
855 // by using AT_object_pointer for the function and having that be
856 // used as 'this' for semantic references.
857 assert(C.capturesThis() && "Field that isn't captured and isn't this?");
858 FieldDecl *f = *Field;
859 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
860 QualType type = f->getType();
861 llvm::DIType fieldType
862 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
863 layout.getFieldOffset(fieldno), VUnit, RecordTy);
864
865 elements.push_back(fieldType);
866 }
867 }
868}
869
870/// CollectRecordStaticField - Helper for CollectRecordFields.
871void CGDebugInfo::
872CollectRecordStaticField(const VarDecl *Var,
873 SmallVectorImpl<llvm::Value *> &elements,
874 llvm::DIType RecordTy) {
875 // Create the descriptor for the static variable, with or without
876 // constant initializers.
877 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
878 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
879
880 // Do not describe enums as static members.
881 if (VTy.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
882 return;
883
884 unsigned LineNumber = getLineNumber(Var->getLocation());
885 StringRef VName = Var->getName();
David Blaikiea89701b2013-01-20 01:19:17 +0000886 llvm::Constant *C = NULL;
Eric Christopher0395de32013-01-16 01:22:32 +0000887 if (Var->getInit()) {
888 const APValue *Value = Var->evaluateValue();
David Blaikiea89701b2013-01-20 01:19:17 +0000889 if (Value) {
890 if (Value->isInt())
891 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
892 if (Value->isFloat())
893 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
894 }
Eric Christopher0395de32013-01-16 01:22:32 +0000895 }
896
897 unsigned Flags = 0;
898 AccessSpecifier Access = Var->getAccess();
899 if (Access == clang::AS_private)
900 Flags |= llvm::DIDescriptor::FlagPrivate;
901 else if (Access == clang::AS_protected)
902 Flags |= llvm::DIDescriptor::FlagProtected;
903
904 llvm::DIType GV = DBuilder.createStaticMemberType(RecordTy, VName, VUnit,
David Blaikiea89701b2013-01-20 01:19:17 +0000905 LineNumber, VTy, Flags, C);
Eric Christopher0395de32013-01-16 01:22:32 +0000906 elements.push_back(GV);
907 StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV);
908}
909
910/// CollectRecordNormalField - Helper for CollectRecordFields.
911void CGDebugInfo::
912CollectRecordNormalField(const FieldDecl *field, uint64_t OffsetInBits,
913 llvm::DIFile tunit,
914 SmallVectorImpl<llvm::Value *> &elements,
915 llvm::DIType RecordTy) {
916 StringRef name = field->getName();
917 QualType type = field->getType();
918
919 // Ignore unnamed fields unless they're anonymous structs/unions.
920 if (name.empty() && !type->isRecordType())
921 return;
922
923 uint64_t SizeInBitsOverride = 0;
924 if (field->isBitField()) {
925 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
926 assert(SizeInBitsOverride && "found named 0-width bitfield");
927 }
928
929 llvm::DIType fieldType
930 = createFieldType(name, type, SizeInBitsOverride,
931 field->getLocation(), field->getAccess(),
932 OffsetInBits, tunit, RecordTy);
933
934 elements.push_back(fieldType);
935}
936
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000937/// CollectRecordFields - A helper function to collect debug info for
938/// record fields. This is used while creating debug info entry for a Record.
939void CGDebugInfo::
940CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
941 SmallVectorImpl<llvm::Value *> &elements,
942 llvm::DIType RecordTy) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000943 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
944
Eric Christopher0395de32013-01-16 01:22:32 +0000945 if (CXXDecl && CXXDecl->isLambda())
946 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
947 else {
948 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000949
Eric Christopher0395de32013-01-16 01:22:32 +0000950 // Field number for non-static fields.
Eric Christopherfd5ac0d2013-01-04 17:59:07 +0000951 unsigned fieldNo = 0;
Eric Christopher0395de32013-01-16 01:22:32 +0000952
Eric Christopher0395de32013-01-16 01:22:32 +0000953 // Static and non-static members should appear in the same order as
954 // the corresponding declarations in the source program.
955 for (RecordDecl::decl_iterator I = record->decls_begin(),
956 E = record->decls_end(); I != E; ++I)
957 if (const VarDecl *V = dyn_cast<VarDecl>(*I))
958 CollectRecordStaticField(V, elements, RecordTy);
959 else if (FieldDecl *field = dyn_cast<FieldDecl>(*I)) {
Eric Christopher0395de32013-01-16 01:22:32 +0000960 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo),
961 tunit, elements, RecordTy);
962
963 // Bump field number for next field.
964 ++fieldNo;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000965 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000966 }
967}
968
969/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
970/// function type is not updated to include implicit "this" pointer. Use this
971/// routine to get a method type which includes "this" pointer.
David Blaikie9a845292013-05-22 23:22:42 +0000972llvm::DICompositeType
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000973CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
974 llvm::DIFile Unit) {
David Blaikie9c78f9b2013-01-07 23:06:35 +0000975 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie67f8b5e2013-01-07 22:24:59 +0000976 if (Method->isStatic())
David Blaikie9a845292013-05-22 23:22:42 +0000977 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie9c78f9b2013-01-07 23:06:35 +0000978 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
979 Func, Unit);
980}
David Blaikie67f8b5e2013-01-07 22:24:59 +0000981
David Blaikie9a845292013-05-22 23:22:42 +0000982llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie9c78f9b2013-01-07 23:06:35 +0000983 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000984 // Add "this" pointer.
David Blaikie9c78f9b2013-01-07 23:06:35 +0000985 llvm::DIArray Args = llvm::DICompositeType(
986 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000987 assert (Args.getNumElements() && "Invalid number of arguments!");
988
989 SmallVector<llvm::Value *, 16> Elts;
990
991 // First element is always return type. For 'void' functions it is NULL.
992 Elts.push_back(Args.getElement(0));
993
David Blaikie67f8b5e2013-01-07 22:24:59 +0000994 // "this" pointer is always first argument.
David Blaikie9c78f9b2013-01-07 23:06:35 +0000995 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie67f8b5e2013-01-07 22:24:59 +0000996 if (isa<ClassTemplateSpecializationDecl>(RD)) {
997 // Create pointer type directly in this case.
998 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
999 QualType PointeeTy = ThisPtrTy->getPointeeType();
1000 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +00001001 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001002 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1003 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopherf0890c42013-05-16 00:52:20 +00001004 llvm::DIType ThisPtrType =
1005 DBuilder.createPointerType(PointeeType, Size, Align);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001006 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1007 // TODO: This and the artificial type below are misleading, the
1008 // types aren't artificial the argument is, but the current
1009 // metadata doesn't represent that.
1010 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1011 Elts.push_back(ThisPtrType);
1012 } else {
1013 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
1014 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1015 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1016 Elts.push_back(ThisPtrType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001017 }
1018
1019 // Copy rest of the arguments.
1020 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1021 Elts.push_back(Args.getElement(i));
1022
1023 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1024
1025 return DBuilder.createSubroutineType(Unit, EltTypeArray);
1026}
1027
Eric Christopher6537f082013-05-16 00:45:12 +00001028/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001029/// inside a function.
1030static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1031 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1032 return isFunctionLocalClass(NRD);
1033 if (isa<FunctionDecl>(RD->getDeclContext()))
1034 return true;
1035 return false;
1036}
1037
1038/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1039/// a single member function GlobalDecl.
1040llvm::DISubprogram
1041CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
1042 llvm::DIFile Unit,
1043 llvm::DIType RecordTy) {
Eric Christopher6537f082013-05-16 00:45:12 +00001044 bool IsCtorOrDtor =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001045 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopher6537f082013-05-16 00:45:12 +00001046
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001047 StringRef MethodName = getFunctionName(Method);
David Blaikie9a845292013-05-22 23:22:42 +00001048 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001049
1050 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1051 // make sense to give a single ctor/dtor a linkage name.
1052 StringRef MethodLinkageName;
1053 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1054 MethodLinkageName = CGM.getMangledName(Method);
1055
1056 // Get the location for the method.
1057 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
1058 unsigned MethodLine = getLineNumber(Method->getLocation());
1059
1060 // Collect virtual method info.
1061 llvm::DIType ContainingType;
Eric Christopher6537f082013-05-16 00:45:12 +00001062 unsigned Virtuality = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001063 unsigned VIndex = 0;
Eric Christopher6537f082013-05-16 00:45:12 +00001064
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001065 if (Method->isVirtual()) {
1066 if (Method->isPure())
1067 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1068 else
1069 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopher6537f082013-05-16 00:45:12 +00001070
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001071 // It doesn't make sense to give a virtual destructor a vtable index,
1072 // since a single destructor has two entries in the vtable.
1073 if (!isa<CXXDestructorDecl>(Method))
1074 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
1075 ContainingType = RecordTy;
1076 }
1077
1078 unsigned Flags = 0;
1079 if (Method->isImplicit())
1080 Flags |= llvm::DIDescriptor::FlagArtificial;
1081 AccessSpecifier Access = Method->getAccess();
1082 if (Access == clang::AS_private)
1083 Flags |= llvm::DIDescriptor::FlagPrivate;
1084 else if (Access == clang::AS_protected)
1085 Flags |= llvm::DIDescriptor::FlagProtected;
1086 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1087 if (CXXC->isExplicit())
1088 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopher6537f082013-05-16 00:45:12 +00001089 } else if (const CXXConversionDecl *CXXC =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001090 dyn_cast<CXXConversionDecl>(Method)) {
1091 if (CXXC->isExplicit())
1092 Flags |= llvm::DIDescriptor::FlagExplicit;
1093 }
1094 if (Method->hasPrototype())
1095 Flags |= llvm::DIDescriptor::FlagPrototyped;
1096
1097 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1098 llvm::DISubprogram SP =
Eric Christopher6537f082013-05-16 00:45:12 +00001099 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001100 MethodDefUnit, MethodLine,
Eric Christopher6537f082013-05-16 00:45:12 +00001101 MethodTy, /*isLocalToUnit=*/false,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001102 /* isDefinition=*/ false,
1103 Virtuality, VIndex, ContainingType,
1104 Flags, CGM.getLangOpts().Optimize, NULL,
1105 TParamsArray);
Eric Christopher6537f082013-05-16 00:45:12 +00001106
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001107 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
1108
1109 return SP;
1110}
1111
1112/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopher6537f082013-05-16 00:45:12 +00001113/// C++ member functions. This is used while creating debug info entry for
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001114/// a Record.
1115void CGDebugInfo::
1116CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
1117 SmallVectorImpl<llvm::Value *> &EltTys,
1118 llvm::DIType RecordTy) {
1119
1120 // Since we want more than just the individual member decls if we
1121 // have templated functions iterate over every declaration to gather
1122 // the functions.
1123 for(DeclContext::decl_iterator I = RD->decls_begin(),
1124 E = RD->decls_end(); I != E; ++I) {
1125 Decl *D = *I;
David Blaikied6d5d692013-08-09 17:20:05 +00001126 if (D->isImplicit())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001127 continue;
1128
1129 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1130 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001131 }
Eric Christopher6537f082013-05-16 00:45:12 +00001132}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001133
1134/// CollectCXXFriends - A helper function to collect debug info for
1135/// C++ base classes. This is used while creating debug info entry for
1136/// a Record.
1137void CGDebugInfo::
1138CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
1139 SmallVectorImpl<llvm::Value *> &EltTys,
1140 llvm::DIType RecordTy) {
1141 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
1142 BE = RD->friend_end(); BI != BE; ++BI) {
1143 if ((*BI)->isUnsupportedFriend())
1144 continue;
1145 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Eric Christopher6537f082013-05-16 00:45:12 +00001146 EltTys.push_back(DBuilder.createFriend(RecordTy,
1147 getOrCreateType(TInfo->getType(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001148 Unit)));
1149 }
1150}
1151
1152/// CollectCXXBases - A helper function to collect debug info for
Eric Christopher6537f082013-05-16 00:45:12 +00001153/// C++ base classes. This is used while creating debug info entry for
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001154/// a Record.
1155void CGDebugInfo::
1156CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
1157 SmallVectorImpl<llvm::Value *> &EltTys,
1158 llvm::DIType RecordTy) {
1159
1160 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1161 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1162 BE = RD->bases_end(); BI != BE; ++BI) {
1163 unsigned BFlags = 0;
1164 uint64_t BaseOffset;
Eric Christopher6537f082013-05-16 00:45:12 +00001165
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001166 const CXXRecordDecl *Base =
1167 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
Eric Christopher6537f082013-05-16 00:45:12 +00001168
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001169 if (BI->isVirtual()) {
1170 // virtual base offset offset is -ve. The code generator emits dwarf
1171 // expression where it expects +ve number.
Eric Christopher6537f082013-05-16 00:45:12 +00001172 BaseOffset =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001173 0 - CGM.getVTableContext()
1174 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
1175 BFlags = llvm::DIDescriptor::FlagVirtual;
1176 } else
1177 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1178 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1179 // BI->isVirtual() and bits when not.
Eric Christopher6537f082013-05-16 00:45:12 +00001180
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001181 AccessSpecifier Access = BI->getAccessSpecifier();
1182 if (Access == clang::AS_private)
1183 BFlags |= llvm::DIDescriptor::FlagPrivate;
1184 else if (Access == clang::AS_protected)
1185 BFlags |= llvm::DIDescriptor::FlagProtected;
Eric Christopher6537f082013-05-16 00:45:12 +00001186
1187 llvm::DIType DTy =
1188 DBuilder.createInheritance(RecordTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001189 getOrCreateType(BI->getType(), Unit),
1190 BaseOffset, BFlags);
1191 EltTys.push_back(DTy);
1192 }
1193}
1194
1195/// CollectTemplateParams - A helper function to collect template parameters.
1196llvm::DIArray CGDebugInfo::
1197CollectTemplateParams(const TemplateParameterList *TPList,
David Blaikie35178dc2013-06-22 18:59:18 +00001198 ArrayRef<TemplateArgument> TAList,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001199 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001200 SmallVector<llvm::Value *, 16> TemplateParams;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001201 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1202 const TemplateArgument &TA = TAList[i];
David Blaikie35178dc2013-06-22 18:59:18 +00001203 StringRef Name;
1204 if (TPList)
1205 Name = TPList->getParam(i)->getName();
David Blaikie9dfd2432013-05-10 21:53:14 +00001206 switch (TA.getKind()) {
1207 case TemplateArgument::Type: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001208 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1209 llvm::DITemplateTypeParameter TTP =
David Blaikie35178dc2013-06-22 18:59:18 +00001210 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001211 TemplateParams.push_back(TTP);
David Blaikie9dfd2432013-05-10 21:53:14 +00001212 } break;
1213 case TemplateArgument::Integral: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001214 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1215 llvm::DITemplateValueParameter TVP =
David Blaikie9dfd2432013-05-10 21:53:14 +00001216 DBuilder.createTemplateValueParameter(
David Blaikie35178dc2013-06-22 18:59:18 +00001217 TheCU, Name, TTy,
David Blaikie9dfd2432013-05-10 21:53:14 +00001218 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1219 TemplateParams.push_back(TVP);
1220 } break;
1221 case TemplateArgument::Declaration: {
1222 const ValueDecl *D = TA.getAsDecl();
1223 bool InstanceMember = D->isCXXInstanceMember();
1224 QualType T = InstanceMember
1225 ? CGM.getContext().getMemberPointerType(
1226 D->getType(), cast<RecordDecl>(D->getDeclContext())
1227 ->getTypeForDecl())
1228 : CGM.getContext().getPointerType(D->getType());
1229 llvm::DIType TTy = getOrCreateType(T, Unit);
1230 llvm::Value *V = 0;
1231 // Variable pointer template parameters have a value that is the address
1232 // of the variable.
1233 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
1234 V = CGM.GetAddrOfGlobalVar(VD);
1235 // Member function pointers have special support for building them, though
1236 // this is currently unsupported in LLVM CodeGen.
David Blaikief8aa1552013-05-13 06:57:50 +00001237 if (InstanceMember) {
David Blaikie9dfd2432013-05-10 21:53:14 +00001238 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(D))
1239 V = CGM.getCXXABI().EmitMemberPointer(method);
David Blaikief8aa1552013-05-13 06:57:50 +00001240 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1241 V = CGM.GetAddrOfFunction(FD);
David Blaikie9dfd2432013-05-10 21:53:14 +00001242 // Member data pointers have special handling too to compute the fixed
1243 // offset within the object.
1244 if (isa<FieldDecl>(D)) {
1245 // These five lines (& possibly the above member function pointer
1246 // handling) might be able to be refactored to use similar code in
1247 // CodeGenModule::getMemberPointerConstant
1248 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1249 CharUnits chars =
1250 CGM.getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1251 V = CGM.getCXXABI().EmitMemberDataPointer(
1252 cast<MemberPointerType>(T.getTypePtr()), chars);
1253 }
1254 llvm::DITemplateValueParameter TVP =
David Blaikie35178dc2013-06-22 18:59:18 +00001255 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie9dfd2432013-05-10 21:53:14 +00001256 TemplateParams.push_back(TVP);
1257 } break;
1258 case TemplateArgument::NullPtr: {
1259 QualType T = TA.getNullPtrType();
1260 llvm::DIType TTy = getOrCreateType(T, Unit);
1261 llvm::Value *V = 0;
1262 // Special case member data pointer null values since they're actually -1
1263 // instead of zero.
1264 if (const MemberPointerType *MPT =
1265 dyn_cast<MemberPointerType>(T.getTypePtr()))
1266 // But treat member function pointers as simple zero integers because
1267 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1268 // CodeGen grows handling for values of non-null member function
1269 // pointers then perhaps we could remove this special case and rely on
1270 // EmitNullMemberPointer for member function pointers.
1271 if (MPT->isMemberDataPointer())
1272 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1273 if (!V)
1274 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1275 llvm::DITemplateValueParameter TVP =
David Blaikie35178dc2013-06-22 18:59:18 +00001276 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie9dfd2432013-05-10 21:53:14 +00001277 TemplateParams.push_back(TVP);
1278 } break;
David Blaikie35178dc2013-06-22 18:59:18 +00001279 case TemplateArgument::Template: {
1280 llvm::DITemplateValueParameter TVP =
1281 DBuilder.createTemplateTemplateParameter(
1282 TheCU, Name, llvm::DIType(),
1283 TA.getAsTemplate().getAsTemplateDecl()
1284 ->getQualifiedNameAsString());
1285 TemplateParams.push_back(TVP);
1286 } break;
1287 case TemplateArgument::Pack: {
1288 llvm::DITemplateValueParameter TVP =
1289 DBuilder.createTemplateParameterPack(
1290 TheCU, Name, llvm::DIType(),
1291 CollectTemplateParams(NULL, TA.getPackAsArray(), Unit));
1292 TemplateParams.push_back(TVP);
1293 } break;
David Blaikiee8065122013-05-10 23:36:06 +00001294 // And the following should never occur:
David Blaikie9dfd2432013-05-10 21:53:14 +00001295 case TemplateArgument::Expression:
1296 case TemplateArgument::TemplateExpansion:
David Blaikie9dfd2432013-05-10 21:53:14 +00001297 case TemplateArgument::Null:
1298 llvm_unreachable(
1299 "These argument types shouldn't exist in concrete types");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001300 }
1301 }
1302 return DBuilder.getOrCreateArray(TemplateParams);
1303}
1304
1305/// CollectFunctionTemplateParams - A helper function to collect debug
1306/// info for function template parameters.
1307llvm::DIArray CGDebugInfo::
1308CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
1309 if (FD->getTemplatedKind() ==
1310 FunctionDecl::TK_FunctionTemplateSpecialization) {
1311 const TemplateParameterList *TList =
1312 FD->getTemplateSpecializationInfo()->getTemplate()
1313 ->getTemplateParameters();
David Blaikie35178dc2013-06-22 18:59:18 +00001314 return CollectTemplateParams(
1315 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001316 }
1317 return llvm::DIArray();
1318}
1319
1320/// CollectCXXTemplateParams - A helper function to collect debug info for
1321/// template parameters.
1322llvm::DIArray CGDebugInfo::
1323CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1324 llvm::DIFile Unit) {
1325 llvm::PointerUnion<ClassTemplateDecl *,
1326 ClassTemplatePartialSpecializationDecl *>
1327 PU = TSpecial->getSpecializedTemplateOrPartial();
Eric Christopher6537f082013-05-16 00:45:12 +00001328
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001329 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1330 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1331 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1332 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
David Blaikie35178dc2013-06-22 18:59:18 +00001333 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001334}
1335
1336/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1337llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1338 if (VTablePtrType.isValid())
1339 return VTablePtrType;
1340
1341 ASTContext &Context = CGM.getContext();
1342
1343 /* Function type */
1344 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
1345 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
1346 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1347 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
1348 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
1349 "__vtbl_ptr_type");
1350 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1351 return VTablePtrType;
1352}
1353
1354/// getVTableName - Get vtable name for the given Class.
1355StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
1356 // Construct gdb compatible name name.
1357 std::string Name = "_vptr$" + RD->getNameAsString();
1358
1359 // Copy this name on the side and use its reference.
1360 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
1361 memcpy(StrPtr, Name.data(), Name.length());
1362 return StringRef(StrPtr, Name.length());
1363}
1364
1365
1366/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1367/// debug info entry in EltTys vector.
1368void CGDebugInfo::
1369CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1370 SmallVectorImpl<llvm::Value *> &EltTys) {
1371 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1372
1373 // If there is a primary base then it will hold vtable info.
1374 if (RL.getPrimaryBase())
1375 return;
1376
1377 // If this class is not dynamic then there is not any vtable info to collect.
1378 if (!RD->isDynamicClass())
1379 return;
1380
1381 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1382 llvm::DIType VPTR
1383 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Eric Christopherf0890c42013-05-16 00:52:20 +00001384 0, Size, 0, 0,
1385 llvm::DIDescriptor::FlagArtificial,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001386 getOrCreateVTablePtrType(Unit));
1387 EltTys.push_back(VPTR);
1388}
1389
Eric Christopher6537f082013-05-16 00:45:12 +00001390/// getOrCreateRecordType - Emit record type's standalone debug info.
1391llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001392 SourceLocation Loc) {
Eric Christopher13c97672013-05-16 00:45:23 +00001393 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001394 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1395 return T;
1396}
1397
1398/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1399/// debug info.
1400llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001401 SourceLocation Loc) {
Eric Christopher13c97672013-05-16 00:45:23 +00001402 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001403 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001404 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405 return T;
1406}
1407
1408/// CreateType - get structure or union type.
David Blaikie5f6e2f42013-06-05 05:32:23 +00001409llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001410 RecordDecl *RD = Ty->getDecl();
Adrian Prantl776bfa12013-06-18 23:32:21 +00001411 // Limited debug info should only remove struct definitions that can
1412 // safely be replaced by a forward declaration in the source code.
David Blaikie658cd2c2013-07-13 21:08:14 +00001413 if (DebugKind <= CodeGenOptions::LimitedDebugInfo && Declaration &&
David Blaikieac00b792013-08-01 20:57:40 +00001414 !RD->isCompleteDefinitionRequired() && CGM.getLangOpts().CPlusPlus) {
Adrian Prantl776bfa12013-06-18 23:32:21 +00001415 // FIXME: This implementation is problematic; there are some test
1416 // cases where we violate the above principle, such as
1417 // test/CodeGen/debug-info-records.c .
David Blaikie5f6e2f42013-06-05 05:32:23 +00001418 llvm::DIDescriptor FDContext =
1419 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
David Blaikie951094b2013-08-15 18:59:40 +00001420 llvm::DIType RetTy = getOrCreateRecordFwdDecl(RD, FDContext);
David Blaikie5f6e2f42013-06-05 05:32:23 +00001421 return RetTy;
1422 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001423
1424 // Get overall information about the record type for the debug info.
1425 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1426
1427 // Records and classes and unions can all be recursive. To handle them, we
1428 // first generate a debug descriptor for the struct as a forward declaration.
1429 // Then (if it is a definition) we go through and get debug info for all of
1430 // its members. Finally, we create a descriptor for the complete type (which
1431 // may refer to the forward decl if the struct is recursive) and replace all
1432 // uses of the forward declaration with the final definition.
1433
David Blaikie4a077162013-08-12 22:24:20 +00001434 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Renb6b0a712013-07-02 19:01:53 +00001435 assert(FwdDecl.isCompositeType() &&
David Blaikie9a845292013-05-22 23:22:42 +00001436 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001437
1438 if (FwdDecl.isForwardDecl())
1439 return FwdDecl;
1440
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001441 // Push the struct on region stack.
Eric Christopherf068c922013-04-02 22:59:11 +00001442 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001443 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1444
Adrian Prantl4919de62013-03-06 22:03:30 +00001445 // Add this to the completed-type cache while we're completing it recursively.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001446 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1447
1448 // Convert all the elements.
1449 SmallVector<llvm::Value *, 16> EltTys;
1450
1451 // Note: The split of CXXDecl information here is intentional, the
1452 // gdb tests will depend on a certain ordering at printout. The debug
1453 // information offsets are still correct if we merge them all together
1454 // though.
1455 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1456 if (CXXDecl) {
1457 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1458 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1459 }
1460
Eric Christopher0395de32013-01-16 01:22:32 +00001461 // Collect data fields (including static variables and any initializers).
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001462 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001463 if (CXXDecl) {
1464 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1465 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001466 }
1467
1468 LexicalBlockStack.pop_back();
1469 RegionMap.erase(Ty->getDecl());
1470
1471 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
David Blaikie80588332013-08-01 20:31:40 +00001472 FwdDecl.setTypeArray(Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001473
Eric Christopherf068c922013-04-02 22:59:11 +00001474 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1475 return FwdDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001476}
1477
1478/// CreateType - get objective-c object type.
1479llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1480 llvm::DIFile Unit) {
1481 // Ignore protocols.
1482 return getOrCreateType(Ty->getBaseType(), Unit);
1483}
1484
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001485
1486/// \return true if Getter has the default name for the property PD.
1487static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1488 const ObjCMethodDecl *Getter) {
1489 assert(PD);
1490 if (!Getter)
1491 return true;
1492
1493 assert(Getter->getDeclName().isObjCZeroArgSelector());
1494 return PD->getName() ==
1495 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
1496}
1497
1498/// \return true if Setter has the default name for the property PD.
1499static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1500 const ObjCMethodDecl *Setter) {
1501 assert(PD);
1502 if (!Setter)
1503 return true;
1504
1505 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantl80e8ea92013-06-07 22:29:12 +00001506 return SelectorTable::constructSetterName(PD->getName()) ==
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001507 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
1508}
1509
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001510/// CreateType - get objective-c interface type.
1511llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1512 llvm::DIFile Unit) {
1513 ObjCInterfaceDecl *ID = Ty->getDecl();
1514 if (!ID)
1515 return llvm::DIType();
1516
1517 // Get overall information about the record type for the debug info.
1518 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1519 unsigned Line = getLineNumber(ID->getLocation());
1520 unsigned RuntimeLang = TheCU.getLanguage();
1521
1522 // If this is just a forward declaration return a special forward-declaration
1523 // debug type since we won't be able to lay out the entire type.
1524 ObjCInterfaceDecl *Def = ID->getDefinition();
1525 if (!Def) {
1526 llvm::DIType FwdDecl =
1527 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001528 ID->getName(), TheCU, DefUnit, Line,
1529 RuntimeLang);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001530 return FwdDecl;
1531 }
1532
1533 ID = Def;
1534
1535 // Bit size, align and offset of the type.
1536 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1537 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1538
1539 unsigned Flags = 0;
1540 if (ID->getImplementation())
1541 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1542
Eric Christopherf068c922013-04-02 22:59:11 +00001543 llvm::DICompositeType RealDecl =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001544 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1545 Line, Size, Align, Flags,
David Blaikiec1d0af12013-02-25 01:07:08 +00001546 llvm::DIType(), llvm::DIArray(), RuntimeLang);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001547
1548 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1549 // will find it and we're emitting the complete type.
Adrian Prantl4919de62013-03-06 22:03:30 +00001550 QualType QualTy = QualType(Ty, 0);
1551 CompletedTypeCache[QualTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001552
Eric Christopherd3003dc2013-07-14 21:00:07 +00001553 // Push the struct on region stack.
Eric Christopherf068c922013-04-02 22:59:11 +00001554 LexicalBlockStack.push_back(static_cast<llvm::MDNode*>(RealDecl));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001555 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1556
1557 // Convert all the elements.
1558 SmallVector<llvm::Value *, 16> EltTys;
1559
1560 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1561 if (SClass) {
1562 llvm::DIType SClassTy =
1563 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
1564 if (!SClassTy.isValid())
1565 return llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001566
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001567 llvm::DIType InhTag =
1568 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
1569 EltTys.push_back(InhTag);
1570 }
1571
Eric Christopherd3003dc2013-07-14 21:00:07 +00001572 // Create entries for all of the properties.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001573 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1574 E = ID->prop_end(); I != E; ++I) {
1575 const ObjCPropertyDecl *PD = *I;
1576 SourceLocation Loc = PD->getLocation();
1577 llvm::DIFile PUnit = getOrCreateFile(Loc);
1578 unsigned PLine = getLineNumber(Loc);
1579 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1580 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1581 llvm::MDNode *PropertyNode =
1582 DBuilder.createObjCProperty(PD->getName(),
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001583 PUnit, PLine,
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001584 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001585 getSelectorName(PD->getGetterName()),
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001586 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001587 getSelectorName(PD->getSetterName()),
1588 PD->getPropertyAttributes(),
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001589 getOrCreateType(PD->getType(), PUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001590 EltTys.push_back(PropertyNode);
1591 }
1592
1593 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1594 unsigned FieldNo = 0;
1595 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1596 Field = Field->getNextIvar(), ++FieldNo) {
1597 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1598 if (!FieldTy.isValid())
1599 return llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001600
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001601 StringRef FieldName = Field->getName();
1602
1603 // Ignore unnamed fields.
1604 if (FieldName.empty())
1605 continue;
1606
1607 // Get the location for the field.
1608 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1609 unsigned FieldLine = getLineNumber(Field->getLocation());
1610 QualType FType = Field->getType();
1611 uint64_t FieldSize = 0;
1612 unsigned FieldAlign = 0;
1613
1614 if (!FType->isIncompleteArrayType()) {
1615
1616 // Bit size, align and offset of the type.
1617 FieldSize = Field->isBitField()
Eric Christopherd3003dc2013-07-14 21:00:07 +00001618 ? Field->getBitWidthValue(CGM.getContext())
1619 : CGM.getContext().getTypeSize(FType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001620 FieldAlign = CGM.getContext().getTypeAlign(FType);
1621 }
1622
1623 uint64_t FieldOffset;
1624 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1625 // We don't know the runtime offset of an ivar if we're using the
1626 // non-fragile ABI. For bitfields, use the bit offset into the first
1627 // byte of storage of the bitfield. For other fields, use zero.
1628 if (Field->isBitField()) {
1629 FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset(
1630 CGM, ID, Field);
1631 FieldOffset %= CGM.getContext().getCharWidth();
1632 } else {
1633 FieldOffset = 0;
1634 }
1635 } else {
1636 FieldOffset = RL.getFieldOffset(FieldNo);
1637 }
1638
1639 unsigned Flags = 0;
1640 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1641 Flags = llvm::DIDescriptor::FlagProtected;
1642 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1643 Flags = llvm::DIDescriptor::FlagPrivate;
1644
1645 llvm::MDNode *PropertyNode = NULL;
1646 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopher6537f082013-05-16 00:45:12 +00001647 if (ObjCPropertyImplDecl *PImpD =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1649 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001650 SourceLocation Loc = PD->getLocation();
1651 llvm::DIFile PUnit = getOrCreateFile(Loc);
1652 unsigned PLine = getLineNumber(Loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001653 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1654 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1655 PropertyNode =
1656 DBuilder.createObjCProperty(PD->getName(),
1657 PUnit, PLine,
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001658 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001659 getSelectorName(PD->getGetterName()),
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001660 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001661 getSelectorName(PD->getSetterName()),
1662 PD->getPropertyAttributes(),
1663 getOrCreateType(PD->getType(), PUnit));
1664 }
1665 }
1666 }
1667 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1668 FieldLine, FieldSize, FieldAlign,
1669 FieldOffset, Flags, FieldTy,
1670 PropertyNode);
1671 EltTys.push_back(FieldTy);
1672 }
1673
1674 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherf068c922013-04-02 22:59:11 +00001675 RealDecl.setTypeArray(Elements);
Adrian Prantl4919de62013-03-06 22:03:30 +00001676
1677 // If the implementation is not yet set, we do not want to mark it
1678 // as complete. An implementation may declare additional
1679 // private ivars that we would miss otherwise.
1680 if (ID->getImplementation() == 0)
1681 CompletedTypeCache.erase(QualTy.getAsOpaquePtr());
Eric Christopher6537f082013-05-16 00:45:12 +00001682
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001683 LexicalBlockStack.pop_back();
Eric Christopherf068c922013-04-02 22:59:11 +00001684 return RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001685}
1686
1687llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1688 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1689 int64_t Count = Ty->getNumElements();
1690 if (Count == 0)
1691 // If number of elements are not known then this is an unbounded array.
1692 // Use Count == -1 to express such arrays.
1693 Count = -1;
1694
1695 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1696 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1697
1698 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1699 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1700
1701 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1702}
1703
1704llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1705 llvm::DIFile Unit) {
1706 uint64_t Size;
1707 uint64_t Align;
1708
1709 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1710 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1711 Size = 0;
1712 Align =
1713 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
1714 } else if (Ty->isIncompleteArrayType()) {
1715 Size = 0;
1716 if (Ty->getElementType()->isIncompleteType())
1717 Align = 0;
1718 else
1719 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikie089db2e2013-05-09 20:48:12 +00001720 } else if (Ty->isIncompleteType()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001721 Size = 0;
1722 Align = 0;
1723 } else {
1724 // Size and align of the whole array, not the element type.
1725 Size = CGM.getContext().getTypeSize(Ty);
1726 Align = CGM.getContext().getTypeAlign(Ty);
1727 }
1728
1729 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1730 // interior arrays, do we care? Why aren't nested arrays represented the
1731 // obvious/recursive way?
1732 SmallVector<llvm::Value *, 8> Subscripts;
1733 QualType EltTy(Ty, 0);
1734 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1735 // If the number of elements is known, then count is that number. Otherwise,
1736 // it's -1. This allows us to represent a subrange with an array of 0
1737 // elements, like this:
1738 //
1739 // struct foo {
1740 // int x[0];
1741 // };
1742 int64_t Count = -1; // Count == -1 is an unbounded array.
1743 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1744 Count = CAT->getSize().getZExtValue();
Eric Christopher6537f082013-05-16 00:45:12 +00001745
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001746 // FIXME: Verify this is right for VLAs.
1747 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1748 EltTy = Ty->getElementType();
1749 }
1750
1751 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1752
Eric Christopher6537f082013-05-16 00:45:12 +00001753 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001754 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1755 SubscriptArray);
1756 return DbgTy;
1757}
1758
Eric Christopher6537f082013-05-16 00:45:12 +00001759llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001760 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001761 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001762 Ty, Ty->getPointeeType(), Unit);
1763}
1764
Eric Christopher6537f082013-05-16 00:45:12 +00001765llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001766 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001767 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001768 Ty, Ty->getPointeeType(), Unit);
1769}
1770
Eric Christopher6537f082013-05-16 00:45:12 +00001771llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001772 llvm::DIFile U) {
David Blaikiee8d75142013-01-19 19:20:56 +00001773 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1774 if (!Ty->getPointeeType()->isFunctionType())
1775 return DBuilder.createMemberPointerType(
David Blaikieb3c23772013-08-12 23:14:36 +00001776 getOrCreateType(Ty->getPointeeType(), U, true), ClassType);
David Blaikiee8d75142013-01-19 19:20:56 +00001777 return DBuilder.createMemberPointerType(getOrCreateInstanceMethodType(
1778 CGM.getContext().getPointerType(
1779 QualType(Ty->getClass(), Ty->getPointeeType().getCVRQualifiers())),
1780 Ty->getPointeeType()->getAs<FunctionProtoType>(), U),
1781 ClassType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001782}
1783
Eric Christopher6537f082013-05-16 00:45:12 +00001784llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001785 llvm::DIFile U) {
1786 // Ignore the atomic wrapping
1787 // FIXME: What is the correct representation?
1788 return getOrCreateType(Ty->getValueType(), U);
1789}
1790
1791/// CreateEnumType - get enumeration type.
1792llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1793 uint64_t Size = 0;
1794 uint64_t Align = 0;
1795 if (!ED->getTypeForDecl()->isIncompleteType()) {
1796 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1797 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1798 }
1799
1800 // If this is just a forward declaration, construct an appropriately
1801 // marked node and just return it.
1802 if (!ED->getDefinition()) {
1803 llvm::DIDescriptor EDContext;
1804 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1805 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1806 unsigned Line = getLineNumber(ED->getLocation());
1807 StringRef EDName = ED->getName();
1808 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1809 EDName, EDContext, DefUnit, Line, 0,
1810 Size, Align);
1811 }
1812
1813 // Create DIEnumerator elements for each enumerator.
1814 SmallVector<llvm::Value *, 16> Enumerators;
1815 ED = ED->getDefinition();
1816 for (EnumDecl::enumerator_iterator
1817 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1818 Enum != EnumEnd; ++Enum) {
1819 Enumerators.push_back(
1820 DBuilder.createEnumerator(Enum->getName(),
David Blaikieac8f43c2013-06-24 07:13:13 +00001821 Enum->getInitVal().getSExtValue()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001822 }
1823
1824 // Return a CompositeType for the enum itself.
1825 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1826
1827 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1828 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00001829 llvm::DIDescriptor EnumContext =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001830 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Adrian Prantl59d6a712013-04-19 19:56:39 +00001831 llvm::DIType ClassTy = ED->isFixed() ?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001832 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001833 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001834 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1835 Size, Align, EltArray,
1836 ClassTy);
1837 return DbgTy;
1838}
1839
David Blaikie4b12be62013-01-21 04:37:12 +00001840static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1841 Qualifiers Quals;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001842 do {
David Blaikie4b12be62013-01-21 04:37:12 +00001843 Quals += T.getLocalQualifiers();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001844 QualType LastT = T;
1845 switch (T->getTypeClass()) {
1846 default:
David Blaikie4b12be62013-01-21 04:37:12 +00001847 return C.getQualifiedType(T.getTypePtr(), Quals);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001848 case Type::TemplateSpecialization:
1849 T = cast<TemplateSpecializationType>(T)->desugar();
1850 break;
1851 case Type::TypeOfExpr:
1852 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1853 break;
1854 case Type::TypeOf:
1855 T = cast<TypeOfType>(T)->getUnderlyingType();
1856 break;
1857 case Type::Decltype:
1858 T = cast<DecltypeType>(T)->getUnderlyingType();
1859 break;
1860 case Type::UnaryTransform:
1861 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1862 break;
1863 case Type::Attributed:
1864 T = cast<AttributedType>(T)->getEquivalentType();
1865 break;
1866 case Type::Elaborated:
1867 T = cast<ElaboratedType>(T)->getNamedType();
1868 break;
1869 case Type::Paren:
1870 T = cast<ParenType>(T)->getInnerType();
1871 break;
David Blaikie4b12be62013-01-21 04:37:12 +00001872 case Type::SubstTemplateTypeParm:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001873 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001874 break;
1875 case Type::Auto:
David Blaikie91296482013-05-24 21:24:35 +00001876 QualType DT = cast<AutoType>(T)->getDeducedType();
1877 if (DT.isNull())
1878 return T;
1879 T = DT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001880 break;
1881 }
Eric Christopher6537f082013-05-16 00:45:12 +00001882
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001883 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumid24c9ab2013-01-21 10:51:28 +00001884 (void)LastT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001885 } while (true);
1886}
1887
Eric Christopherf0890c42013-05-16 00:52:20 +00001888/// getType - Get the type from the cache or return null type if it doesn't
1889/// exist.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001890llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
1891
1892 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001893 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopher6537f082013-05-16 00:45:12 +00001894
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001895 // Check for existing entry.
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001896 if (Ty->getTypeClass() == Type::ObjCInterface) {
1897 llvm::Value *V = getCachedInterfaceTypeOrNull(Ty);
1898 if (V)
1899 return llvm::DIType(cast<llvm::MDNode>(V));
1900 else return llvm::DIType();
1901 }
1902
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001903 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1904 TypeCache.find(Ty.getAsOpaquePtr());
1905 if (it != TypeCache.end()) {
1906 // Verify that the debug info still exists.
1907 if (llvm::Value *V = it->second)
1908 return llvm::DIType(cast<llvm::MDNode>(V));
1909 }
1910
1911 return llvm::DIType();
1912}
1913
1914/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1915/// doesn't exist.
1916llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1917
1918 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001919 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001920
1921 // Check for existing entry.
Adrian Prantl4919de62013-03-06 22:03:30 +00001922 llvm::Value *V = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001923 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1924 CompletedTypeCache.find(Ty.getAsOpaquePtr());
Adrian Prantl4919de62013-03-06 22:03:30 +00001925 if (it != CompletedTypeCache.end())
1926 V = it->second;
1927 else {
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001928 V = getCachedInterfaceTypeOrNull(Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001929 }
1930
Adrian Prantl4919de62013-03-06 22:03:30 +00001931 // Verify that any cached debug info still exists.
David Blaikie00383082013-08-13 04:21:38 +00001932 return llvm::DIType(cast_or_null<llvm::MDNode>(V));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001933}
1934
David Blaikieeab6a362013-06-21 00:40:50 +00001935void CGDebugInfo::completeFwdDecl(const RecordDecl &RD) {
1936 // In limited debug info we only want to do this if the complete type was
1937 // required.
David Blaikieac00b792013-08-01 20:57:40 +00001938 if (DebugKind <= CodeGenOptions::LimitedDebugInfo &&
1939 CGM.getLangOpts().CPlusPlus)
David Blaikieeab6a362013-06-21 00:40:50 +00001940 return;
1941
David Blaikie076f51f2013-06-21 00:59:44 +00001942 QualType QTy = CGM.getContext().getRecordType(&RD);
1943 llvm::DIType T = getTypeOrNull(QTy);
David Blaikieeab6a362013-06-21 00:40:50 +00001944
Eric Christopherb2d13922013-07-18 00:52:50 +00001945 if (T && T.isForwardDecl())
David Blaikie076f51f2013-06-21 00:59:44 +00001946 getOrCreateType(QTy, getOrCreateFile(RD.getLocation()));
David Blaikieeab6a362013-06-21 00:40:50 +00001947}
1948
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001949/// getCachedInterfaceTypeOrNull - Get the type from the interface
1950/// cache, unless it needs to regenerated. Otherwise return null.
1951llvm::Value *CGDebugInfo::getCachedInterfaceTypeOrNull(QualType Ty) {
1952 // Is there a cached interface that hasn't changed?
1953 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
1954 ::iterator it1 = ObjCInterfaceCache.find(Ty.getAsOpaquePtr());
1955
1956 if (it1 != ObjCInterfaceCache.end())
1957 if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty))
1958 if (Checksum(Decl) == it1->second.second)
1959 // Return cached forward declaration.
1960 return it1->second.first;
1961
1962 return 0;
1963}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001964
1965/// getOrCreateType - Get the type from the cache or create a new
1966/// one if necessary.
Eric Christopher56b108a2013-06-07 22:54:39 +00001967llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit,
1968 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001969 if (Ty.isNull())
1970 return llvm::DIType();
1971
1972 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001973 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001974
David Blaikie00383082013-08-13 04:21:38 +00001975 if (llvm::DIType T = getCompletedTypeOrNull(Ty)) {
David Blaikief0c31d92013-06-21 21:03:11 +00001976 // If we're looking for a definition, make sure we have definitions of any
1977 // underlying types.
1978 if (const TypedefType* TTy = dyn_cast<TypedefType>(Ty))
1979 getOrCreateType(TTy->getDecl()->getUnderlyingType(), Unit, Declaration);
1980 if (Ty.hasLocalQualifiers())
1981 getOrCreateType(QualType(Ty.getTypePtr(), 0), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001982 return T;
David Blaikief0c31d92013-06-21 21:03:11 +00001983 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001984
1985 // Otherwise create the type.
David Blaikie5f6e2f42013-06-05 05:32:23 +00001986 llvm::DIType Res = CreateTypeNode(Ty, Unit, Declaration);
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001987 void* TyPtr = Ty.getAsOpaquePtr();
1988
1989 // And update the type cache.
1990 TypeCache[TyPtr] = Res;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001991
1992 llvm::DIType TC = getTypeOrNull(Ty);
Eric Christopherb2d13922013-07-18 00:52:50 +00001993 if (TC && TC.isForwardDecl())
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001994 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
1995 else if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty)) {
1996 // Interface types may have elements added to them by a
1997 // subsequent implementation or extension, so we keep them in
1998 // the ObjCInterfaceCache together with a checksum. Instead of
Adrian Prantlf06989b2013-05-08 23:37:22 +00001999 // the (possibly) incomplete interface type, we return a forward
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002000 // declaration that gets RAUW'd in CGDebugInfo::finalize().
David Blaikiee2eb89a2013-05-21 18:29:40 +00002001 std::pair<llvm::WeakVH, unsigned> &V = ObjCInterfaceCache[TyPtr];
2002 if (V.first)
2003 return llvm::DIType(cast<llvm::MDNode>(V.first));
2004 TC = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2005 Decl->getName(), TheCU, Unit,
2006 getLineNumber(Decl->getLocation()),
2007 TheCU.getLanguage());
2008 // Store the forward declaration in the cache.
2009 V.first = TC;
2010 V.second = Checksum(Decl);
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002011
David Blaikiee2eb89a2013-05-21 18:29:40 +00002012 // Register the type for replacement in finalize().
2013 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
2014
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002015 return TC;
Adrian Prantl4919de62013-03-06 22:03:30 +00002016 }
2017
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002018 if (!Res.isForwardDecl())
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002019 CompletedTypeCache[TyPtr] = Res;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002020
2021 return Res;
2022}
2023
Adrian Prantlb5a50072013-06-07 01:10:41 +00002024/// Currently the checksum of an interface includes the number of
2025/// ivars and property accessors.
Eric Christopher56b108a2013-06-07 22:54:39 +00002026unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl4f97f852013-06-07 01:10:48 +00002027 // The assumption is that the number of ivars can only increase
2028 // monotonically, so it is safe to just use their current number as
2029 // a checksum.
Adrian Prantlb5a50072013-06-07 01:10:41 +00002030 unsigned Sum = 0;
2031 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
2032 Ivar != 0; Ivar = Ivar->getNextIvar())
2033 ++Sum;
2034
2035 return Sum;
Adrian Prantl4919de62013-03-06 22:03:30 +00002036}
2037
2038ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2039 switch (Ty->getTypeClass()) {
2040 case Type::ObjCObjectPointer:
Eric Christopherf0890c42013-05-16 00:52:20 +00002041 return getObjCInterfaceDecl(cast<ObjCObjectPointerType>(Ty)
2042 ->getPointeeType());
Adrian Prantl4919de62013-03-06 22:03:30 +00002043 case Type::ObjCInterface:
2044 return cast<ObjCInterfaceType>(Ty)->getDecl();
2045 default:
2046 return 0;
2047 }
2048}
2049
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002050/// CreateTypeNode - Create a new debug type node.
Eric Christopher56b108a2013-06-07 22:54:39 +00002051llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit,
2052 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002053 // Handle qualifiers, which recursively handles what they refer to.
2054 if (Ty.hasLocalQualifiers())
David Blaikie5f6e2f42013-06-05 05:32:23 +00002055 return CreateQualifiedType(Ty, Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002056
2057 const char *Diag = 0;
Eric Christopher6537f082013-05-16 00:45:12 +00002058
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002059 // Work out details of type.
2060 switch (Ty->getTypeClass()) {
2061#define TYPE(Class, Base)
2062#define ABSTRACT_TYPE(Class, Base)
2063#define NON_CANONICAL_TYPE(Class, Base)
2064#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2065#include "clang/AST/TypeNodes.def"
2066 llvm_unreachable("Dependent types cannot show up in debug information");
2067
2068 case Type::ExtVector:
2069 case Type::Vector:
2070 return CreateType(cast<VectorType>(Ty), Unit);
2071 case Type::ObjCObjectPointer:
2072 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2073 case Type::ObjCObject:
2074 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2075 case Type::ObjCInterface:
2076 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2077 case Type::Builtin:
2078 return CreateType(cast<BuiltinType>(Ty));
2079 case Type::Complex:
2080 return CreateType(cast<ComplexType>(Ty));
2081 case Type::Pointer:
2082 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner12df2462013-06-24 17:51:48 +00002083 case Type::Decayed:
2084 // Decayed types are just pointers in LLVM and DWARF.
2085 return CreateType(
2086 cast<PointerType>(cast<DecayedType>(Ty)->getDecayedType()), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002087 case Type::BlockPointer:
2088 return CreateType(cast<BlockPointerType>(Ty), Unit);
2089 case Type::Typedef:
David Blaikie5f6e2f42013-06-05 05:32:23 +00002090 return CreateType(cast<TypedefType>(Ty), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002091 case Type::Record:
David Blaikie5f6e2f42013-06-05 05:32:23 +00002092 return CreateType(cast<RecordType>(Ty), Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002093 case Type::Enum:
2094 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
2095 case Type::FunctionProto:
2096 case Type::FunctionNoProto:
2097 return CreateType(cast<FunctionType>(Ty), Unit);
2098 case Type::ConstantArray:
2099 case Type::VariableArray:
2100 case Type::IncompleteArray:
2101 return CreateType(cast<ArrayType>(Ty), Unit);
2102
2103 case Type::LValueReference:
2104 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2105 case Type::RValueReference:
2106 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2107
2108 case Type::MemberPointer:
2109 return CreateType(cast<MemberPointerType>(Ty), Unit);
2110
2111 case Type::Atomic:
2112 return CreateType(cast<AtomicType>(Ty), Unit);
2113
2114 case Type::Attributed:
2115 case Type::TemplateSpecialization:
2116 case Type::Elaborated:
2117 case Type::Paren:
2118 case Type::SubstTemplateTypeParm:
2119 case Type::TypeOfExpr:
2120 case Type::TypeOf:
2121 case Type::Decltype:
2122 case Type::UnaryTransform:
David Blaikie226399c2013-07-13 21:08:08 +00002123 case Type::PackExpansion:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002124 llvm_unreachable("type should have been unwrapped!");
David Blaikie91296482013-05-24 21:24:35 +00002125 case Type::Auto:
2126 Diag = "auto";
2127 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002128 }
Eric Christopher6537f082013-05-16 00:45:12 +00002129
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002130 assert(Diag && "Fall through without a diagnostic?");
2131 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2132 "debug information for %0 is not yet supported");
2133 CGM.getDiags().Report(DiagID)
2134 << Diag;
2135 return llvm::DIType();
2136}
2137
2138/// getOrCreateLimitedType - Get the type from the cache or create a new
2139/// limited type if necessary.
David Blaikie4a077162013-08-12 22:24:20 +00002140llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002141 llvm::DIFile Unit) {
David Blaikie4a077162013-08-12 22:24:20 +00002142 QualType QTy(Ty, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002143
David Blaikie4a077162013-08-12 22:24:20 +00002144 llvm::DIType T = getTypeOrNull(QTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002145
2146 // We may have cached a forward decl when we could have created
2147 // a non-forward decl. Go ahead and create a non-forward decl
2148 // now.
Eric Christopherb2d13922013-07-18 00:52:50 +00002149 if (T && !T.isForwardDecl()) return T;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002150
2151 // Otherwise create the type.
David Blaikie4a077162013-08-12 22:24:20 +00002152 llvm::DIType Res = CreateLimitedType(Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002153
Eric Christopherb2d13922013-07-18 00:52:50 +00002154 if (T && T.isForwardDecl())
David Blaikie4a077162013-08-12 22:24:20 +00002155 ReplaceMap.push_back(
2156 std::make_pair(QTy.getAsOpaquePtr(), static_cast<llvm::Value *>(T)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002157
2158 // And update the type cache.
David Blaikie4a077162013-08-12 22:24:20 +00002159 TypeCache[QTy.getAsOpaquePtr()] = Res;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002160 return Res;
2161}
2162
2163// TODO: Currently used for context chains when limiting debug info.
2164llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
2165 RecordDecl *RD = Ty->getDecl();
Eric Christopher6537f082013-05-16 00:45:12 +00002166
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002167 // Get overall information about the record type for the debug info.
2168 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2169 unsigned Line = getLineNumber(RD->getLocation());
2170 StringRef RDName = getClassName(RD);
2171
2172 llvm::DIDescriptor RDContext;
Eric Christopher13c97672013-05-16 00:45:23 +00002173 if (DebugKind == CodeGenOptions::LimitedDebugInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002174 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
2175 else
2176 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
2177
2178 // If this is just a forward declaration, construct an appropriately
2179 // marked node and just return it.
2180 if (!RD->getDefinition())
David Blaikie951094b2013-08-15 18:59:40 +00002181 return getOrCreateRecordFwdDecl(RD, RDContext);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002182
2183 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2184 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie2fcadbe2013-03-26 23:47:35 +00002185 llvm::DICompositeType RealDecl;
Eric Christopher6537f082013-05-16 00:45:12 +00002186
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002187 if (RD->isUnion())
2188 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002189 Size, Align, 0, llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002190 else if (RD->isClass()) {
2191 // FIXME: This could be a struct type giving a default visibility different
2192 // than C++ class type, but needs llvm metadata changes first.
2193 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002194 Size, Align, 0, 0, llvm::DIType(),
2195 llvm::DIArray(), llvm::DIType(),
2196 llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002197 } else
2198 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
Eric Christopherf0890c42013-05-16 00:52:20 +00002199 Size, Align, 0, llvm::DIType(),
2200 llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002201
2202 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie2fcadbe2013-03-26 23:47:35 +00002203 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002204
David Blaikie841b37c2013-08-01 18:23:24 +00002205 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002206 // A class's primary base or the class itself contains the vtable.
David Blaikie2fcadbe2013-03-26 23:47:35 +00002207 llvm::DICompositeType ContainingType;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002208 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2209 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
2210 // Seek non virtual primary base root.
2211 while (1) {
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002212 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2213 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2214 if (PBT && !BRL.isPrimaryBaseVirtual())
2215 PBase = PBT;
2216 else
2217 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002218 }
David Blaikie2fcadbe2013-03-26 23:47:35 +00002219 ContainingType = llvm::DICompositeType(
2220 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit));
2221 } else if (CXXDecl->isDynamicClass())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002222 ContainingType = RealDecl;
2223
David Blaikie2fcadbe2013-03-26 23:47:35 +00002224 RealDecl.setContainingType(ContainingType);
David Blaikie80588332013-08-01 20:31:40 +00002225 if (const ClassTemplateSpecializationDecl *TSpecial =
2226 dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl))
2227 RealDecl.setTypeArray(llvm::DIArray(),
2228 CollectCXXTemplateParams(TSpecial, DefUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002229 }
2230 return llvm::DIType(RealDecl);
2231}
2232
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002233/// CreateMemberType - Create new member and increase Offset by FType's size.
2234llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
2235 StringRef Name,
2236 uint64_t *Offset) {
2237 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2238 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2239 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
2240 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
2241 FieldSize, FieldAlign,
2242 *Offset, 0, FieldTy);
2243 *Offset += FieldSize;
2244 return Ty;
2245}
2246
David Blaikie9faebd22013-05-20 04:58:53 +00002247llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
2248 // We only need a declaration (not a definition) of the type - so use whatever
2249 // we would otherwise do to get a type for a pointee. (forward declarations in
2250 // limited debug info, full definitions (if the type definition is available)
2251 // in unlimited debug info)
David Blaikieb3c23772013-08-12 23:14:36 +00002252 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2253 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
2254 getOrCreateFile(TD->getLocation()), true);
David Blaikie9faebd22013-05-20 04:58:53 +00002255 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2256 // This doesn't handle providing declarations (for functions or variables) for
2257 // entities without definitions in this TU, nor when the definition proceeds
2258 // the call to this function.
2259 // FIXME: This should be split out into more specific maps with support for
2260 // emitting forward declarations and merging definitions with declarations,
2261 // the same way as we do for types.
2262 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2263 DeclCache.find(D->getCanonicalDecl());
2264 if (I == DeclCache.end())
2265 return llvm::DIDescriptor();
2266 llvm::Value *V = I->second;
2267 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
2268}
2269
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002270/// getFunctionDeclaration - Return debug info descriptor to describe method
2271/// declaration for the given method definition.
2272llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
David Blaikie23e66db2013-06-22 00:09:36 +00002273 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2274 return llvm::DISubprogram();
2275
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002276 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2277 if (!FD) return llvm::DISubprogram();
2278
2279 // Setup context.
David Blaikied6d5d692013-08-09 17:20:05 +00002280 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002281
2282 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2283 MI = SPCache.find(FD->getCanonicalDecl());
David Blaikied6d5d692013-08-09 17:20:05 +00002284 if (MI == SPCache.end()) {
2285 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
2286 llvm::DICompositeType T(S);
2287 llvm::DISubprogram SP = CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
2288 T.addMember(SP);
2289 return SP;
2290 }
2291 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002292 if (MI != SPCache.end()) {
2293 llvm::Value *V = MI->second;
2294 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie23e66db2013-06-22 00:09:36 +00002295 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002296 return SP;
2297 }
2298
2299 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
2300 E = FD->redecls_end(); I != E; ++I) {
2301 const FunctionDecl *NextFD = *I;
2302 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2303 MI = SPCache.find(NextFD->getCanonicalDecl());
2304 if (MI != SPCache.end()) {
2305 llvm::Value *V = MI->second;
2306 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie23e66db2013-06-22 00:09:36 +00002307 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002308 return SP;
2309 }
2310 }
2311 return llvm::DISubprogram();
2312}
2313
2314// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2315// implicit parameter "this".
David Blaikie9a845292013-05-22 23:22:42 +00002316llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2317 QualType FnType,
2318 llvm::DIFile F) {
David Blaikie23e66db2013-06-22 00:09:36 +00002319 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2320 // Create fake but valid subroutine type. Otherwise
2321 // llvm::DISubprogram::Verify() would return false, and
2322 // subprogram DIE will miss DW_AT_decl_file and
2323 // DW_AT_decl_line fields.
2324 return DBuilder.createSubroutineType(F, DBuilder.getOrCreateArray(None));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002325
2326 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2327 return getOrCreateMethodType(Method, F);
2328 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2329 // Add "self" and "_cmd"
2330 SmallVector<llvm::Value *, 16> Elts;
2331
2332 // First element is always return type. For 'void' functions it is NULL.
Adrian Prantl0cb00022013-05-22 21:37:49 +00002333 QualType ResultTy = OMethod->getResultType();
2334
2335 // Replace the instancetype keyword with the actual type.
2336 if (ResultTy == CGM.getContext().getObjCInstanceType())
2337 ResultTy = CGM.getContext().getPointerType(
2338 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
2339
Adrian Prantl566a9c32013-05-10 21:08:31 +00002340 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002341 // "self" pointer is always first argument.
Adrian Prantle86fcc42013-03-29 19:20:29 +00002342 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2343 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2344 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002345 // "_cmd" pointer is always second argument.
2346 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2347 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2348 // Get rest of the arguments.
Eric Christopher6537f082013-05-16 00:45:12 +00002349 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002350 PE = OMethod->param_end(); PI != PE; ++PI)
2351 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2352
2353 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2354 return DBuilder.createSubroutineType(F, EltTypeArray);
2355 }
David Blaikie9a845292013-05-22 23:22:42 +00002356 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002357}
2358
2359/// EmitFunctionStart - Constructs the debug code for entering a function.
2360void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
2361 llvm::Function *Fn,
2362 CGBuilderTy &Builder) {
2363
2364 StringRef Name;
2365 StringRef LinkageName;
2366
2367 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2368
2369 const Decl *D = GD.getDecl();
2370 // Function may lack declaration in source code if it is created by Clang
2371 // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
2372 bool HasDecl = (D != 0);
2373 // Use the location of the declaration.
2374 SourceLocation Loc;
2375 if (HasDecl)
2376 Loc = D->getLocation();
2377
2378 unsigned Flags = 0;
2379 llvm::DIFile Unit = getOrCreateFile(Loc);
2380 llvm::DIDescriptor FDContext(Unit);
2381 llvm::DIArray TParamsArray;
2382 if (!HasDecl) {
2383 // Use llvm function name.
2384 Name = Fn->getName();
2385 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2386 // If there is a DISubprogram for this function available then use it.
2387 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2388 FI = SPCache.find(FD->getCanonicalDecl());
2389 if (FI != SPCache.end()) {
2390 llvm::Value *V = FI->second;
2391 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2392 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2393 llvm::MDNode *SPN = SP;
2394 LexicalBlockStack.push_back(SPN);
2395 RegionMap[D] = llvm::WeakVH(SP);
2396 return;
2397 }
2398 }
2399 Name = getFunctionName(FD);
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002400 // Use mangled name as linkage name for C/C++ functions.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002401 if (FD->hasPrototype()) {
2402 LinkageName = CGM.getMangledName(GD);
2403 Flags |= llvm::DIDescriptor::FlagPrototyped;
2404 }
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002405 // No need to replicate the linkage name if it isn't different from the
2406 // subprogram name, no need to have it at all unless coverage is enabled or
2407 // debug is set to more than just line tables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002408 if (LinkageName == Name ||
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002409 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2410 !CGM.getCodeGenOpts().EmitGcovNotes &&
Eric Christopher13c97672013-05-16 00:45:23 +00002411 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002412 LinkageName = StringRef();
2413
Eric Christopher13c97672013-05-16 00:45:23 +00002414 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002415 if (const NamespaceDecl *NSDecl =
2416 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2417 FDContext = getOrCreateNameSpace(NSDecl);
2418 else if (const RecordDecl *RDecl =
2419 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2420 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
2421
2422 // Collect template parameters.
2423 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2424 }
2425 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2426 Name = getObjCMethodName(OMD);
2427 Flags |= llvm::DIDescriptor::FlagPrototyped;
2428 } else {
2429 // Use llvm function name.
2430 Name = Fn->getName();
2431 Flags |= llvm::DIDescriptor::FlagPrototyped;
2432 }
2433 if (!Name.empty() && Name[0] == '\01')
2434 Name = Name.substr(1);
2435
2436 unsigned LineNo = getLineNumber(Loc);
2437 if (!HasDecl || D->isImplicit())
2438 Flags |= llvm::DIDescriptor::FlagArtificial;
2439
David Blaikie23e66db2013-06-22 00:09:36 +00002440 llvm::DISubprogram SP = DBuilder.createFunction(
2441 FDContext, Name, LinkageName, Unit, LineNo,
2442 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2443 true /*definition*/, getLineNumber(CurLoc), Flags,
2444 CGM.getLangOpts().Optimize, Fn, TParamsArray, getFunctionDeclaration(D));
David Blaikie9faebd22013-05-20 04:58:53 +00002445 if (HasDecl)
2446 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002447
2448 // Push function on region stack.
2449 llvm::MDNode *SPN = SP;
2450 LexicalBlockStack.push_back(SPN);
2451 if (HasDecl)
2452 RegionMap[D] = llvm::WeakVH(SP);
2453}
2454
2455/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl18a0cd52013-07-18 00:27:59 +00002456/// information in the source file. If the location is invalid, the
2457/// previous location will be reused.
Adrian Prantl00df5ea2013-03-12 20:43:25 +00002458void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
2459 bool ForceColumnInfo) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002460 // Update our current location
2461 setLocation(Loc);
2462
2463 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
2464
2465 // Don't bother if things are the same as last time.
2466 SourceManager &SM = CGM.getContext().getSourceManager();
2467 if (CurLoc == PrevLoc ||
2468 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2469 // New Builder may not be in sync with CGDebugInfo.
David Blaikie0a0f93c2013-02-01 19:09:49 +00002470 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2471 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
2472 LexicalBlockStack.back())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002473 return;
Eric Christopher6537f082013-05-16 00:45:12 +00002474
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002475 // Update last state.
2476 PrevLoc = CurLoc;
2477
2478 llvm::MDNode *Scope = LexicalBlockStack.back();
Adrian Prantl00df5ea2013-03-12 20:43:25 +00002479 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get
2480 (getLineNumber(CurLoc),
2481 getColumnNumber(CurLoc, ForceColumnInfo),
2482 Scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002483}
2484
2485/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2486/// the stack.
2487void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
2488 llvm::DIDescriptor D =
2489 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
2490 llvm::DIDescriptor() :
2491 llvm::DIDescriptor(LexicalBlockStack.back()),
2492 getOrCreateFile(CurLoc),
2493 getLineNumber(CurLoc),
2494 getColumnNumber(CurLoc));
2495 llvm::MDNode *DN = D;
2496 LexicalBlockStack.push_back(DN);
2497}
2498
2499/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2500/// region - beginning of a DW_TAG_lexical_block.
Eric Christopherf0890c42013-05-16 00:52:20 +00002501void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2502 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002503 // Set our current location.
2504 setLocation(Loc);
2505
2506 // Create a new lexical block and push it on the stack.
2507 CreateLexicalBlock(Loc);
2508
2509 // Emit a line table change for the current location inside the new scope.
2510 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
2511 getColumnNumber(Loc),
2512 LexicalBlockStack.back()));
2513}
2514
2515/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2516/// region - end of a DW_TAG_lexical_block.
Eric Christopherf0890c42013-05-16 00:52:20 +00002517void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2518 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002519 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2520
2521 // Provide an entry in the line table for the end of the block.
2522 EmitLocation(Builder, Loc);
2523
2524 LexicalBlockStack.pop_back();
2525}
2526
2527/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2528void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2529 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2530 unsigned RCount = FnBeginRegionCount.back();
2531 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2532
2533 // Pop all regions for this function.
2534 while (LexicalBlockStack.size() != RCount)
2535 EmitLexicalBlockEnd(Builder, CurLoc);
2536 FnBeginRegionCount.pop_back();
2537}
2538
Eric Christopher6537f082013-05-16 00:45:12 +00002539// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002540// See BuildByRefType.
2541llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2542 uint64_t *XOffset) {
2543
2544 SmallVector<llvm::Value *, 5> EltTys;
2545 QualType FType;
2546 uint64_t FieldSize, FieldOffset;
2547 unsigned FieldAlign;
Eric Christopher6537f082013-05-16 00:45:12 +00002548
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002549 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00002550 QualType Type = VD->getType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002551
2552 FieldOffset = 0;
2553 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2554 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2555 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2556 FType = CGM.getContext().IntTy;
2557 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2558 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2559
2560 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2561 if (HasCopyAndDispose) {
2562 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2563 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2564 &FieldOffset));
2565 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2566 &FieldOffset));
2567 }
2568 bool HasByrefExtendedLayout;
2569 Qualifiers::ObjCLifetime Lifetime;
2570 if (CGM.getContext().getByrefLifetime(Type,
2571 Lifetime, HasByrefExtendedLayout)
Adrian Prantl1f437912013-07-23 00:12:14 +00002572 && HasByrefExtendedLayout) {
2573 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002574 EltTys.push_back(CreateMemberType(Unit, FType,
2575 "__byref_variable_layout",
2576 &FieldOffset));
Adrian Prantl1f437912013-07-23 00:12:14 +00002577 }
Eric Christopher6537f082013-05-16 00:45:12 +00002578
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002579 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2580 if (Align > CGM.getContext().toCharUnitsFromBits(
John McCall64aa4b32013-04-16 22:48:15 +00002581 CGM.getTarget().getPointerAlign(0))) {
Eric Christopher6537f082013-05-16 00:45:12 +00002582 CharUnits FieldOffsetInBytes
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002583 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2584 CharUnits AlignedOffsetInBytes
2585 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2586 CharUnits NumPaddingBytes
2587 = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopher6537f082013-05-16 00:45:12 +00002588
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002589 if (NumPaddingBytes.isPositive()) {
2590 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2591 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2592 pad, ArrayType::Normal, 0);
2593 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2594 }
2595 }
Eric Christopher6537f082013-05-16 00:45:12 +00002596
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002597 FType = Type;
2598 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2599 FieldSize = CGM.getContext().getTypeSize(FType);
2600 FieldAlign = CGM.getContext().toBits(Align);
2601
Eric Christopher6537f082013-05-16 00:45:12 +00002602 *XOffset = FieldOffset;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002603 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
2604 0, FieldSize, FieldAlign,
2605 FieldOffset, 0, FieldTy);
2606 EltTys.push_back(FieldTy);
2607 FieldOffset += FieldSize;
Eric Christopher6537f082013-05-16 00:45:12 +00002608
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002609 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher6537f082013-05-16 00:45:12 +00002610
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002611 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopher6537f082013-05-16 00:45:12 +00002612
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002613 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikiec1d0af12013-02-25 01:07:08 +00002614 llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002615}
2616
2617/// EmitDeclare - Emit local variable declaration debug info.
2618void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Eric Christopher6537f082013-05-16 00:45:12 +00002619 llvm::Value *Storage,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002620 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002621 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002622 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2623
2624 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2625 llvm::DIType Ty;
2626 uint64_t XOffset = 0;
2627 if (VD->hasAttr<BlocksAttr>())
2628 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00002629 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002630 Ty = getOrCreateType(VD->getType(), Unit);
2631
2632 // If there is no debug info for this type then do not emit debug info
2633 // for this variable.
2634 if (!Ty)
2635 return;
2636
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002637 // Get location information.
2638 unsigned Line = getLineNumber(VD->getLocation());
2639 unsigned Column = getColumnNumber(VD->getLocation());
2640 unsigned Flags = 0;
2641 if (VD->isImplicit())
2642 Flags |= llvm::DIDescriptor::FlagArtificial;
2643 // If this is the first argument and it is implicit then
2644 // give it an object pointer flag.
2645 // FIXME: There has to be a better way to do this, but for static
2646 // functions there won't be an implicit param at arg1 and
2647 // otherwise it is 'self' or 'this'.
2648 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2649 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikie41c9bae2013-06-19 21:53:53 +00002650 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopher7dab97b2013-07-17 22:52:53 +00002651 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2652 !VD->getType()->isPointerType())
David Blaikie41c9bae2013-06-19 21:53:53 +00002653 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002654
2655 llvm::MDNode *Scope = LexicalBlockStack.back();
2656
2657 StringRef Name = VD->getName();
2658 if (!Name.empty()) {
2659 if (VD->hasAttr<BlocksAttr>()) {
2660 CharUnits offset = CharUnits::fromQuantity(32);
2661 SmallVector<llvm::Value *, 9> addr;
2662 llvm::Type *Int64Ty = CGM.Int64Ty;
2663 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2664 // offset of __forwarding field
2665 offset = CGM.getContext().toCharUnitsFromBits(
John McCall64aa4b32013-04-16 22:48:15 +00002666 CGM.getTarget().getPointerWidth(0));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002667 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2668 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2669 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2670 // offset of x field
2671 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2672 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2673
2674 // Create the descriptor for the variable.
2675 llvm::DIVariable D =
Eric Christopher6537f082013-05-16 00:45:12 +00002676 DBuilder.createComplexVariable(Tag,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002677 llvm::DIDescriptor(Scope),
2678 VD->getName(), Unit, Line, Ty,
2679 addr, ArgNo);
Eric Christopher6537f082013-05-16 00:45:12 +00002680
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002681 // Insert an llvm.dbg.declare into the current block.
2682 llvm::Instruction *Call =
2683 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2684 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2685 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002686 }
David Blaikie436653b2013-01-05 05:58:35 +00002687 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2688 // If VD is an anonymous union then Storage represents value for
2689 // all union fields.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002690 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikied8180cf2013-01-05 20:03:07 +00002691 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002692 for (RecordDecl::field_iterator I = RD->field_begin(),
2693 E = RD->field_end();
2694 I != E; ++I) {
2695 FieldDecl *Field = *I;
2696 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2697 StringRef FieldName = Field->getName();
Eric Christopher6537f082013-05-16 00:45:12 +00002698
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002699 // Ignore unnamed fields. Do not ignore unnamed records.
2700 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2701 continue;
Eric Christopher6537f082013-05-16 00:45:12 +00002702
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002703 // Use VarDecl's Tag, Scope and Line number.
2704 llvm::DIVariable D =
2705 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Eric Christopher6537f082013-05-16 00:45:12 +00002706 FieldName, Unit, Line, FieldTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002707 CGM.getLangOpts().Optimize, Flags,
2708 ArgNo);
Eric Christopher6537f082013-05-16 00:45:12 +00002709
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002710 // Insert an llvm.dbg.declare into the current block.
2711 llvm::Instruction *Call =
2712 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2713 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2714 }
David Blaikied8180cf2013-01-05 20:03:07 +00002715 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002716 }
2717 }
David Blaikie436653b2013-01-05 05:58:35 +00002718
2719 // Create the descriptor for the variable.
2720 llvm::DIVariable D =
2721 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2722 Name, Unit, Line, Ty,
2723 CGM.getLangOpts().Optimize, Flags, ArgNo);
2724
2725 // Insert an llvm.dbg.declare into the current block.
2726 llvm::Instruction *Call =
2727 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2728 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002729}
2730
2731void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2732 llvm::Value *Storage,
2733 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002734 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002735 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2736}
2737
Adrian Prantle86fcc42013-03-29 19:20:29 +00002738/// Look up the completed type for a self pointer in the TypeCache and
2739/// create a copy of it with the ObjectPointer and Artificial flags
2740/// set. If the type is not cached, a new one is created. This should
2741/// never happen though, since creating a type for the implicit self
2742/// argument implies that we already parsed the interface definition
2743/// and the ivar declarations in the implementation.
Eric Christopherf0890c42013-05-16 00:52:20 +00002744llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2745 llvm::DIType Ty) {
Adrian Prantle86fcc42013-03-29 19:20:29 +00002746 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christopherb2d13922013-07-18 00:52:50 +00002747 if (CachedTy) Ty = CachedTy;
Adrian Prantle86fcc42013-03-29 19:20:29 +00002748 else DEBUG(llvm::dbgs() << "No cached type for self.");
2749 return DBuilder.createObjectPointerType(Ty);
2750}
2751
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002752void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2753 llvm::Value *Storage,
2754 CGBuilderTy &Builder,
2755 const CGBlockInfo &blockInfo) {
Eric Christopher13c97672013-05-16 00:45:23 +00002756 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002757 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopher6537f082013-05-16 00:45:12 +00002758
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002759 if (Builder.GetInsertBlock() == 0)
2760 return;
Eric Christopher6537f082013-05-16 00:45:12 +00002761
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002762 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopher6537f082013-05-16 00:45:12 +00002763
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002764 uint64_t XOffset = 0;
2765 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2766 llvm::DIType Ty;
2767 if (isByRef)
2768 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00002769 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002770 Ty = getOrCreateType(VD->getType(), Unit);
2771
2772 // Self is passed along as an implicit non-arg variable in a
2773 // block. Mark it as the object pointer.
2774 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantle86fcc42013-03-29 19:20:29 +00002775 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002776
2777 // Get location information.
2778 unsigned Line = getLineNumber(VD->getLocation());
2779 unsigned Column = getColumnNumber(VD->getLocation());
2780
2781 const llvm::DataLayout &target = CGM.getDataLayout();
2782
2783 CharUnits offset = CharUnits::fromQuantity(
2784 target.getStructLayout(blockInfo.StructureType)
2785 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2786
2787 SmallVector<llvm::Value *, 9> addr;
2788 llvm::Type *Int64Ty = CGM.Int64Ty;
Adrian Prantl9b97adf2013-03-29 19:20:35 +00002789 if (isa<llvm::AllocaInst>(Storage))
2790 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002791 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2792 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2793 if (isByRef) {
2794 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2795 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2796 // offset of __forwarding field
2797 offset = CGM.getContext()
2798 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
2799 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2800 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2801 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2802 // offset of x field
2803 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2804 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2805 }
2806
2807 // Create the descriptor for the variable.
2808 llvm::DIVariable D =
Eric Christopher6537f082013-05-16 00:45:12 +00002809 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002810 llvm::DIDescriptor(LexicalBlockStack.back()),
2811 VD->getName(), Unit, Line, Ty, addr);
Adrian Prantl9b97adf2013-03-29 19:20:35 +00002812
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002813 // Insert an llvm.dbg.declare into the current block.
2814 llvm::Instruction *Call =
2815 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
2816 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2817 LexicalBlockStack.back()));
2818}
2819
2820/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2821/// variable declaration.
2822void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2823 unsigned ArgNo,
2824 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002825 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002826 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2827}
2828
2829namespace {
2830 struct BlockLayoutChunk {
2831 uint64_t OffsetInBits;
2832 const BlockDecl::Capture *Capture;
2833 };
2834 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2835 return l.OffsetInBits < r.OffsetInBits;
2836 }
2837}
2838
2839void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl836e7c92013-03-14 17:53:33 +00002840 llvm::Value *Arg,
2841 llvm::Value *LocalAddr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002842 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002843 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002844 ASTContext &C = CGM.getContext();
2845 const BlockDecl *blockDecl = block.getBlockDecl();
2846
2847 // Collect some general information about the block's location.
2848 SourceLocation loc = blockDecl->getCaretLocation();
2849 llvm::DIFile tunit = getOrCreateFile(loc);
2850 unsigned line = getLineNumber(loc);
2851 unsigned column = getColumnNumber(loc);
Eric Christopher6537f082013-05-16 00:45:12 +00002852
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002853 // Build the debug-info type for the block literal.
2854 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2855
2856 const llvm::StructLayout *blockLayout =
2857 CGM.getDataLayout().getStructLayout(block.StructureType);
2858
2859 SmallVector<llvm::Value*, 16> fields;
2860 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2861 blockLayout->getElementOffsetInBits(0),
2862 tunit, tunit));
2863 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2864 blockLayout->getElementOffsetInBits(1),
2865 tunit, tunit));
2866 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2867 blockLayout->getElementOffsetInBits(2),
2868 tunit, tunit));
2869 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2870 blockLayout->getElementOffsetInBits(3),
2871 tunit, tunit));
2872 fields.push_back(createFieldType("__descriptor",
2873 C.getPointerType(block.NeedsCopyDispose ?
2874 C.getBlockDescriptorExtendedType() :
2875 C.getBlockDescriptorType()),
2876 0, loc, AS_public,
2877 blockLayout->getElementOffsetInBits(4),
2878 tunit, tunit));
2879
2880 // We want to sort the captures by offset, not because DWARF
2881 // requires this, but because we're paranoid about debuggers.
2882 SmallVector<BlockLayoutChunk, 8> chunks;
2883
2884 // 'this' capture.
2885 if (blockDecl->capturesCXXThis()) {
2886 BlockLayoutChunk chunk;
2887 chunk.OffsetInBits =
2888 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2889 chunk.Capture = 0;
2890 chunks.push_back(chunk);
2891 }
2892
2893 // Variable captures.
2894 for (BlockDecl::capture_const_iterator
2895 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2896 i != e; ++i) {
2897 const BlockDecl::Capture &capture = *i;
2898 const VarDecl *variable = capture.getVariable();
2899 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2900
2901 // Ignore constant captures.
2902 if (captureInfo.isConstant())
2903 continue;
2904
2905 BlockLayoutChunk chunk;
2906 chunk.OffsetInBits =
2907 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2908 chunk.Capture = &capture;
2909 chunks.push_back(chunk);
2910 }
2911
2912 // Sort by offset.
2913 llvm::array_pod_sort(chunks.begin(), chunks.end());
2914
2915 for (SmallVectorImpl<BlockLayoutChunk>::iterator
2916 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2917 uint64_t offsetInBits = i->OffsetInBits;
2918 const BlockDecl::Capture *capture = i->Capture;
2919
2920 // If we have a null capture, this must be the C++ 'this' capture.
2921 if (!capture) {
2922 const CXXMethodDecl *method =
2923 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2924 QualType type = method->getThisType(C);
2925
2926 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
2927 offsetInBits, tunit, tunit));
2928 continue;
2929 }
2930
2931 const VarDecl *variable = capture->getVariable();
2932 StringRef name = variable->getName();
2933
2934 llvm::DIType fieldType;
2935 if (capture->isByRef()) {
2936 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2937
2938 // FIXME: this creates a second copy of this type!
2939 uint64_t xoffset;
2940 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2941 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
2942 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
2943 ptrInfo.first, ptrInfo.second,
2944 offsetInBits, 0, fieldType);
2945 } else {
2946 fieldType = createFieldType(name, variable->getType(), 0,
2947 loc, AS_public, offsetInBits, tunit, tunit);
2948 }
2949 fields.push_back(fieldType);
2950 }
2951
2952 SmallString<36> typeName;
2953 llvm::raw_svector_ostream(typeName)
2954 << "__block_literal_" << CGM.getUniqueBlockCount();
2955
2956 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
2957
2958 llvm::DIType type =
2959 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2960 CGM.getContext().toBits(block.BlockSize),
2961 CGM.getContext().toBits(block.BlockAlign),
David Blaikiec1d0af12013-02-25 01:07:08 +00002962 0, llvm::DIType(), fieldsArray);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002963 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2964
2965 // Get overall information about the block.
2966 unsigned flags = llvm::DIDescriptor::FlagArtificial;
2967 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002968
2969 // Create the descriptor for the parameter.
2970 llvm::DIVariable debugVar =
2971 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
Eric Christopher6537f082013-05-16 00:45:12 +00002972 llvm::DIDescriptor(scope),
Adrian Prantl836e7c92013-03-14 17:53:33 +00002973 Arg->getName(), tunit, line, type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002974 CGM.getLangOpts().Optimize, flags,
Adrian Prantl836e7c92013-03-14 17:53:33 +00002975 cast<llvm::Argument>(Arg)->getArgNo() + 1);
2976
Adrian Prantlbea407c2013-03-14 21:52:59 +00002977 if (LocalAddr) {
Adrian Prantl836e7c92013-03-14 17:53:33 +00002978 // Insert an llvm.dbg.value into the current block.
Adrian Prantlbea407c2013-03-14 21:52:59 +00002979 llvm::Instruction *DbgVal =
2980 DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar,
Eric Christopherf068c922013-04-02 22:59:11 +00002981 Builder.GetInsertBlock());
Adrian Prantlbea407c2013-03-14 21:52:59 +00002982 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2983 }
Adrian Prantl836e7c92013-03-14 17:53:33 +00002984
Adrian Prantlbea407c2013-03-14 21:52:59 +00002985 // Insert an llvm.dbg.declare into the current block.
2986 llvm::Instruction *DbgDecl =
2987 DBuilder.insertDeclare(Arg, debugVar, Builder.GetInsertBlock());
2988 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002989}
2990
Eric Christopher0395de32013-01-16 01:22:32 +00002991/// getStaticDataMemberDeclaration - If D is an out-of-class definition of
2992/// a static data member of a class, find its corresponding in-class
2993/// declaration.
2994llvm::DIDerivedType CGDebugInfo::getStaticDataMemberDeclaration(const Decl *D) {
2995 if (cast<VarDecl>(D)->isStaticDataMember()) {
2996 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
2997 MI = StaticDataMemberCache.find(D->getCanonicalDecl());
2998 if (MI != StaticDataMemberCache.end())
2999 // Verify the info still exists.
3000 if (llvm::Value *V = MI->second)
3001 return llvm::DIDerivedType(cast<llvm::MDNode>(V));
3002 }
3003 return llvm::DIDerivedType();
3004}
3005
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003006/// EmitGlobalVariable - Emit information about a global variable.
3007void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3008 const VarDecl *D) {
Eric Christopher13c97672013-05-16 00:45:23 +00003009 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003010 // Create global variable debug descriptor.
3011 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
3012 unsigned LineNo = getLineNumber(D->getLocation());
3013
3014 setLocation(D->getLocation());
3015
3016 QualType T = D->getType();
3017 if (T->isIncompleteArrayType()) {
3018
3019 // CodeGen turns int[] into int[1] so we'll do the same here.
3020 llvm::APInt ConstVal(32, 1);
3021 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3022
3023 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3024 ArrayType::Normal, 0);
3025 }
3026 StringRef DeclName = D->getName();
3027 StringRef LinkageName;
3028 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
3029 && !isa<ObjCMethodDecl>(D->getDeclContext()))
3030 LinkageName = Var->getName();
3031 if (LinkageName == DeclName)
3032 LinkageName = StringRef();
Eric Christopher6537f082013-05-16 00:45:12 +00003033 llvm::DIDescriptor DContext =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003034 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Eric Christopher56b108a2013-06-07 22:54:39 +00003035 llvm::DIGlobalVariable GV =
3036 DBuilder.createStaticVariable(DContext, DeclName, LinkageName, Unit,
3037 LineNo, getOrCreateType(T, Unit),
3038 Var->hasInternalLinkage(), Var,
3039 getStaticDataMemberDeclaration(D));
David Blaikie9faebd22013-05-20 04:58:53 +00003040 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003041}
3042
3043/// EmitGlobalVariable - Emit information about an objective-c interface.
3044void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3045 ObjCInterfaceDecl *ID) {
Eric Christopher13c97672013-05-16 00:45:23 +00003046 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003047 // Create global variable debug descriptor.
3048 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
3049 unsigned LineNo = getLineNumber(ID->getLocation());
3050
3051 StringRef Name = ID->getName();
3052
3053 QualType T = CGM.getContext().getObjCInterfaceType(ID);
3054 if (T->isIncompleteArrayType()) {
3055
3056 // CodeGen turns int[] into int[1] so we'll do the same here.
3057 llvm::APInt ConstVal(32, 1);
3058 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3059
3060 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3061 ArrayType::Normal, 0);
3062 }
3063
3064 DBuilder.createGlobalVariable(Name, Unit, LineNo,
3065 getOrCreateType(T, Unit),
3066 Var->hasInternalLinkage(), Var);
3067}
3068
3069/// EmitGlobalVariable - Emit global variable's debug info.
Eric Christopher6537f082013-05-16 00:45:12 +00003070void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003071 llvm::Constant *Init) {
Eric Christopher13c97672013-05-16 00:45:23 +00003072 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003073 // Create the descriptor for the variable.
3074 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3075 StringRef Name = VD->getName();
3076 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3077 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3078 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3079 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3080 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3081 }
3082 // Do not use DIGlobalVariable for enums.
3083 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3084 return;
Eric Christopher56b108a2013-06-07 22:54:39 +00003085 llvm::DIGlobalVariable GV =
3086 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
3087 getLineNumber(VD->getLocation()), Ty, true,
3088 Init, getStaticDataMemberDeclaration(VD));
David Blaikie9faebd22013-05-20 04:58:53 +00003089 DeclCache.insert(std::make_pair(VD->getCanonicalDecl(), llvm::WeakVH(GV)));
3090}
3091
3092llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3093 if (!LexicalBlockStack.empty())
3094 return llvm::DIScope(LexicalBlockStack.back());
3095 return getContextDescriptor(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003096}
3097
David Blaikie957dac52013-04-22 06:13:21 +00003098void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikie9faebd22013-05-20 04:58:53 +00003099 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3100 return;
David Blaikie957dac52013-04-22 06:13:21 +00003101 DBuilder.createImportedModule(
David Blaikie9faebd22013-05-20 04:58:53 +00003102 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3103 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie957dac52013-04-22 06:13:21 +00003104 getLineNumber(UD.getLocation()));
3105}
3106
David Blaikie9faebd22013-05-20 04:58:53 +00003107void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3108 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3109 return;
3110 assert(UD.shadow_size() &&
3111 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3112 // Emitting one decl is sufficient - debuggers can detect that this is an
3113 // overloaded name & provide lookup for all the overloads.
3114 const UsingShadowDecl &USD = **UD.shadow_begin();
Eric Christopher56b108a2013-06-07 22:54:39 +00003115 if (llvm::DIDescriptor Target =
3116 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikie9faebd22013-05-20 04:58:53 +00003117 DBuilder.createImportedDeclaration(
3118 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3119 getLineNumber(USD.getLocation()));
3120}
3121
David Blaikiefc46ebc2013-05-20 22:50:41 +00003122llvm::DIImportedEntity
3123CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3124 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3125 return llvm::DIImportedEntity(0);
3126 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3127 if (VH)
3128 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
3129 llvm::DIImportedEntity R(0);
3130 if (const NamespaceAliasDecl *Underlying =
3131 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3132 // This could cache & dedup here rather than relying on metadata deduping.
3133 R = DBuilder.createImportedModule(
3134 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3135 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3136 NA.getName());
3137 else
3138 R = DBuilder.createImportedModule(
3139 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3140 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3141 getLineNumber(NA.getLocation()), NA.getName());
3142 VH = R;
3143 return R;
3144}
3145
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003146/// getOrCreateNamesSpace - Return namespace descriptor for the given
3147/// namespace decl.
Eric Christopher6537f082013-05-16 00:45:12 +00003148llvm::DINameSpace
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003149CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Eric Christopher6537f082013-05-16 00:45:12 +00003150 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003151 NameSpaceCache.find(NSDecl);
3152 if (I != NameSpaceCache.end())
3153 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopher6537f082013-05-16 00:45:12 +00003154
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003155 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3156 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00003157 llvm::DIDescriptor Context =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003158 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3159 llvm::DINameSpace NS =
3160 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3161 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3162 return NS;
3163}
3164
3165void CGDebugInfo::finalize() {
3166 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
3167 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
3168 llvm::DIType Ty, RepTy;
3169 // Verify that the debug info still exists.
3170 if (llvm::Value *V = VI->second)
3171 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher6537f082013-05-16 00:45:12 +00003172
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003173 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
3174 TypeCache.find(VI->first);
3175 if (it != TypeCache.end()) {
3176 // Verify that the debug info still exists.
3177 if (llvm::Value *V = it->second)
3178 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
3179 }
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003180
Eric Christopherb2d13922013-07-18 00:52:50 +00003181 if (Ty && Ty.isForwardDecl() && RepTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003182 Ty.replaceAllUsesWith(RepTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003183 }
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003184
3185 // We keep our own list of retained types, because we need to look
3186 // up the final type in the type cache.
3187 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3188 RE = RetainedTypes.end(); RI != RE; ++RI)
3189 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
3190
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003191 DBuilder.finalize();
3192}