blob: 31422830f19f34c2079f629d03d60c3f7ed35508 [file] [log] [blame]
Guy Benyei11169dd2012-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 Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-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 Carruthffd55512013-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 Benyei11169dd2012-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 Christopher324bbbd2013-07-14 21:12:44 +000044 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
45 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-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 Prantl2e0637f2013-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 Prantlb75016d2013-07-18 01:36:04 +000071ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Adrian Prantl2e0637f2013-07-18 00:28:02 +000072 : DI(CGF.getDebugInfo()), Builder(B) {
73 if (DI) {
74 SavedLoc = DI->getLocation();
Adrian Prantl49a78562013-07-24 20:34:39 +000075 DI->CurLoc = SourceLocation();
76 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
77 }
78}
79
80void ArtificialLocation::Emit() {
81 if (DI) {
Adrian Prantl2e0637f2013-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 Prantl49a78562013-07-24 20:34:39 +000086 assert(!DI->LexicalBlockStack.empty());
87 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
Adrian Prantl2e0637f2013-07-18 00:28:02 +000088 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
89 }
90}
91
Adrian Prantlb75016d2013-07-18 01:36:04 +000092ArtificialLocation::~ArtificialLocation() {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000093 if (DI) {
94 assert(Builder.getCurrentDebugLocation().getLine() == 0);
95 DI->CurLoc = SavedLoc;
96 }
97}
98
Guy Benyei11169dd2012-12-18 14:30:41 +000099void CGDebugInfo::setLocation(SourceLocation Loc) {
100 // If the new location isn't valid return.
Adrian Prantlb1b3bfc2013-07-18 00:27:56 +0000101 if (Loc.isInvalid()) return;
Guy Benyei11169dd2012-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 Blaikie0a21d0d2013-01-26 22:16:26 +0000128 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Guy Benyei11169dd2012-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 Blaikiebfa52742013-04-19 06:56:38 +0000138llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-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 Blaikiebfa52742013-04-19 06:56:38 +0000146 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 }
148
149 // Check namespace.
150 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000151 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000152
David Blaikiebfa52742013-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 Benyei11169dd2012-12-18 14:30:41 +0000156 getOrCreateMainFile());
Guy Benyei11169dd2012-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 Kramer9170e912013-02-22 15:46:01 +0000172 SmallString<128> NS;
173 llvm::raw_svector_ostream OS(NS);
174 FD->printName(OS);
Guy Benyei11169dd2012-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 Kramer9170e912013-02-22 15:46:01 +0000182 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
183 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 }
185
186 // Copy this name on the side and use its reference.
Benjamin Kramer9170e912013-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 Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000198 if (const ObjCImplementationDecl *OID =
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 dyn_cast<const ObjCImplementationDecl>(DC)) {
200 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000201 } else if (const ObjCInterfaceDecl *OID =
Guy Benyei11169dd2012-12-18 14:30:41 +0000202 dyn_cast<const ObjCInterfaceDecl>(DC)) {
203 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000204 } else if (const ObjCCategoryImplDecl *OCD =
Guy Benyei11169dd2012-12-18 14:30:41 +0000205 dyn_cast<const ObjCCategoryImplDecl>(DC)){
206 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
207 OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000208 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-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 Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000233StringRef
Guy Benyei11169dd2012-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 Kramer9170e912013-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 Benyei11169dd2012-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 Prantlc7822422013-03-12 20:43:25 +0000315unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000316 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000317 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-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 Christopherf1545832013-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 Christopherb2a008c2013-05-16 00:45:12 +0000371
Guy Benyei11169dd2012-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 Benyei11169dd2012-12-18 14:30:41 +0000395 // FIXME - Eliminate TheCU.
Eric Christopher978c8392013-07-19 00:51:58 +0000396 TheCU = DBuilder.createCompileUnit(LangTag, Filename, getCurrentDirname(),
397 Producer, LO.Optimize,
398 CGM.getCodeGenOpts().DwarfDebugFlags,
399 RuntimeVers, SplitDwarfFilename);
Guy Benyei11169dd2012-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 Collingbourne5c5e6172013-06-27 22:51:01 +0000415 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 case BuiltinType::Void:
417 return llvm::DIType();
418 case BuiltinType::ObjCClass:
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000419 if (ClassTy)
Guy Benyei11169dd2012-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 Christopherf8bc4d82013-07-18 00:52:50 +0000431 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000432 return ObjTy;
433
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000434 if (!ClassTy)
Guy Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000440
Guy Benyei11169dd2012-12-18 14:30:41 +0000441 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
442
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000443 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000444 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
445 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000446
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000447 ObjTy.setTypeArray(DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
448 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000449 return ObjTy;
450 }
451 case BuiltinType::ObjCSel: {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000452 if (SelTy)
Guy Benyei11169dd2012-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 Benyeid8a08ea2012-12-18 14:38:23 +0000460
461 case BuiltinType::OCLImage1d:
462 return getOrCreateStructPtrType("opencl_image1d_t",
463 OCLImage1dDITy);
464 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000465 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-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 Benyei61054192013-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 Benyei1b4fb3e2013-01-20 12:31:11 +0000484 case BuiltinType::OCLEvent:
485 return getOrCreateStructPtrType("opencl_event_t",
486 OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000487
Guy Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000525 llvm::DIType DbgTy =
Guy Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000538 llvm::DIType DbgTy =
Guy Benyei11169dd2012-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 Christopher1ecc5632013-06-07 22:54:39 +0000546llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit,
547 bool Declaration) {
Guy Benyei11169dd2012-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 Christopher1ecc5632013-06-07 22:54:39 +0000573 llvm::DIType FromTy =
574 getOrCreateType(Qc.apply(CGM.getContext(), T), Unit, Declaration);
Guy Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000579
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 return DbgTy;
581}
582
583llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
584 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-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 Benyei11169dd2012-12-18 14:30:41 +0000592 llvm::DIType DbgTy =
Eric Christopherb2a008c2013-05-16 00:45:12 +0000593 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-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 Christopherb2a008c2013-05-16 00:45:12 +0000600 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 Ty->getPointeeType(), Unit);
602}
603
604// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie4e7ef802013-08-15 20:17:25 +0000605llvm::DIType CGDebugInfo::getOrCreateRecordFwdDecl(const RecordDecl *RD,
606 llvm::DIDescriptor Ctx) {
607 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikied816e2d2013-08-15 18:59:40 +0000608 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
610 unsigned Line = getLineNumber(RD->getLocation());
611 StringRef RDName = getClassName(RD);
612
613 unsigned Tag = 0;
614 if (RD->isStruct() || RD->isInterface())
615 Tag = llvm::dwarf::DW_TAG_structure_type;
616 else if (RD->isUnion())
617 Tag = llvm::dwarf::DW_TAG_union_type;
618 else {
619 assert(RD->isClass());
620 Tag = llvm::dwarf::DW_TAG_class_type;
621 }
622
623 // Create the type.
624 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
625}
626
627// Walk up the context chain and create forward decls for record decls,
628// and normal descriptors for namespaces.
629llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
630 if (!Context)
631 return TheCU;
632
633 // See if we already have the parent.
634 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
635 I = RegionMap.find(Context);
636 if (I != RegionMap.end()) {
637 llvm::Value *V = I->second;
638 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
639 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000640
Guy Benyei11169dd2012-12-18 14:30:41 +0000641 // Check namespace.
642 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
643 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
644
645 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
646 if (!RD->isDependentType()) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +0000647 llvm::DIType Ty = getOrCreateLimitedType(
648 CGM.getContext().getRecordType(RD)->castAs<RecordType>(), getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000649 return llvm::DIDescriptor(Ty);
650 }
651 }
652 return TheCU;
653}
654
Guy Benyei11169dd2012-12-18 14:30:41 +0000655llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000656 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000657 QualType PointeeTy,
658 llvm::DIFile Unit) {
659 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
660 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie4583bea2013-05-24 21:33:22 +0000661 return DBuilder.createReferenceType(
David Blaikie6b7d060c2013-08-12 23:14:36 +0000662 Tag, getOrCreateType(PointeeTy, Unit, true));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000663
Guy Benyei11169dd2012-12-18 14:30:41 +0000664 // Bit size, align and offset of the type.
665 // Size is always the size of a pointer. We can't use getTypeSize here
666 // because that does not return the correct value for references.
667 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000668 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
670
David Blaikie6b7d060c2013-08-12 23:14:36 +0000671 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit, true),
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 Size, Align);
673}
674
Eric Christopher0fdcb312013-05-16 00:52:20 +0000675llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
676 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000677 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000678 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000679 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
680 TheCU, getOrCreateMainFile(), 0);
681 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
682 Cache = DBuilder.createPointerType(Cache, Size);
683 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000684}
685
Guy Benyei11169dd2012-12-18 14:30:41 +0000686llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
687 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000688 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 return BlockLiteralGeneric;
690
691 SmallVector<llvm::Value *, 8> EltTys;
692 llvm::DIType FieldTy;
693 QualType FType;
694 uint64_t FieldSize, FieldOffset;
695 unsigned FieldAlign;
696 llvm::DIArray Elements;
697 llvm::DIType EltTy, DescTy;
698
699 FieldOffset = 0;
700 FType = CGM.getContext().UnsignedLongTy;
701 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
702 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
703
704 Elements = DBuilder.getOrCreateArray(EltTys);
705 EltTys.clear();
706
707 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
708 unsigned LineNo = getLineNumber(CurLoc);
709
710 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
711 Unit, LineNo, FieldOffset, 0,
David Blaikie6d4fe152013-02-25 01:07:08 +0000712 Flags, llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000713
714 // Bit size, align and offset of the type.
715 uint64_t Size = CGM.getContext().getTypeSize(Ty);
716
717 DescTy = DBuilder.createPointerType(EltTy, Size);
718
719 FieldOffset = 0;
720 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
721 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
722 FType = CGM.getContext().IntTy;
723 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
724 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
725 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
726 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
727
728 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
729 FieldTy = DescTy;
730 FieldSize = CGM.getContext().getTypeSize(Ty);
731 FieldAlign = CGM.getContext().getTypeAlign(Ty);
732 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
733 LineNo, FieldSize, FieldAlign,
734 FieldOffset, 0, FieldTy);
735 EltTys.push_back(FieldTy);
736
737 FieldOffset += FieldSize;
738 Elements = DBuilder.getOrCreateArray(EltTys);
739
740 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
741 Unit, LineNo, FieldOffset, 0,
David Blaikie6d4fe152013-02-25 01:07:08 +0000742 Flags, llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000743
Guy Benyei11169dd2012-12-18 14:30:41 +0000744 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
745 return BlockLiteralGeneric;
746}
747
David Blaikiee36464c2013-06-05 05:32:23 +0000748llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit,
749 bool Declaration) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000750 // Typedefs are derived from some other type. If we have a typedef of a
751 // typedef, make sure to emit the whole chain.
David Blaikie4583bea2013-05-24 21:33:22 +0000752 llvm::DIType Src =
David Blaikiee36464c2013-06-05 05:32:23 +0000753 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit, Declaration);
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000754 if (!Src)
Guy Benyei11169dd2012-12-18 14:30:41 +0000755 return llvm::DIType();
756 // We don't set size information, but do specify where the typedef was
757 // declared.
758 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
759 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000760
Guy Benyei11169dd2012-12-18 14:30:41 +0000761 llvm::DIDescriptor TypedefContext =
762 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000763
Guy Benyei11169dd2012-12-18 14:30:41 +0000764 return
765 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
766}
767
768llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
769 llvm::DIFile Unit) {
770 SmallVector<llvm::Value *, 16> EltTys;
771
772 // Add the result type at least.
773 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
774
775 // Set up remainder of arguments if there is a prototype.
776 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
777 if (isa<FunctionNoProtoType>(Ty))
778 EltTys.push_back(DBuilder.createUnspecifiedParameter());
779 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
780 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
781 EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit));
782 }
783
784 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
785 return DBuilder.createSubroutineType(Unit, EltTypeArray);
786}
787
788
Guy Benyei11169dd2012-12-18 14:30:41 +0000789llvm::DIType CGDebugInfo::createFieldType(StringRef name,
790 QualType type,
791 uint64_t sizeInBitsOverride,
792 SourceLocation loc,
793 AccessSpecifier AS,
794 uint64_t offsetInBits,
795 llvm::DIFile tunit,
796 llvm::DIDescriptor scope) {
797 llvm::DIType debugType = getOrCreateType(type, tunit);
798
799 // Get the location for the field.
800 llvm::DIFile file = getOrCreateFile(loc);
801 unsigned line = getLineNumber(loc);
802
803 uint64_t sizeInBits = 0;
804 unsigned alignInBits = 0;
805 if (!type->isIncompleteArrayType()) {
806 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
807
808 if (sizeInBitsOverride)
809 sizeInBits = sizeInBitsOverride;
810 }
811
812 unsigned flags = 0;
813 if (AS == clang::AS_private)
814 flags |= llvm::DIDescriptor::FlagPrivate;
815 else if (AS == clang::AS_protected)
816 flags |= llvm::DIDescriptor::FlagProtected;
817
818 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
819 alignInBits, offsetInBits, flags, debugType);
820}
821
Eric Christopher91a31902013-01-16 01:22:32 +0000822/// CollectRecordLambdaFields - Helper for CollectRecordFields.
823void CGDebugInfo::
824CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
825 SmallVectorImpl<llvm::Value *> &elements,
826 llvm::DIType RecordTy) {
827 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
828 // has the name and the location of the variable so we should iterate over
829 // both concurrently.
830 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
831 RecordDecl::field_iterator Field = CXXDecl->field_begin();
832 unsigned fieldno = 0;
833 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
834 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
835 const LambdaExpr::Capture C = *I;
836 if (C.capturesVariable()) {
837 VarDecl *V = C.getCapturedVar();
838 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
839 StringRef VName = V->getName();
840 uint64_t SizeInBitsOverride = 0;
841 if (Field->isBitField()) {
842 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
843 assert(SizeInBitsOverride && "found named 0-width bitfield");
844 }
845 llvm::DIType fieldType
846 = createFieldType(VName, Field->getType(), SizeInBitsOverride,
847 C.getLocation(), Field->getAccess(),
848 layout.getFieldOffset(fieldno), VUnit, RecordTy);
849 elements.push_back(fieldType);
850 } else {
851 // TODO: Need to handle 'this' in some way by probably renaming the
852 // this of the lambda class and having a field member of 'this' or
853 // by using AT_object_pointer for the function and having that be
854 // used as 'this' for semantic references.
855 assert(C.capturesThis() && "Field that isn't captured and isn't this?");
856 FieldDecl *f = *Field;
857 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
858 QualType type = f->getType();
859 llvm::DIType fieldType
860 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
861 layout.getFieldOffset(fieldno), VUnit, RecordTy);
862
863 elements.push_back(fieldType);
864 }
865 }
866}
867
David Blaikie7d5d7c72013-08-17 00:06:55 +0000868/// CollectRecordStaticField - Helper for CollectRecordFields.
David Blaikieae019462013-08-15 22:50:29 +0000869llvm::DIDerivedType
870CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
871 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000872 // Create the descriptor for the static variable, with or without
873 // constant initializers.
874 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
875 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
876
Eric Christopher91a31902013-01-16 01:22:32 +0000877 unsigned LineNumber = getLineNumber(Var->getLocation());
878 StringRef VName = Var->getName();
David Blaikied42917f2013-01-20 01:19:17 +0000879 llvm::Constant *C = NULL;
Eric Christopher91a31902013-01-16 01:22:32 +0000880 if (Var->getInit()) {
881 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000882 if (Value) {
883 if (Value->isInt())
884 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
885 if (Value->isFloat())
886 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
887 }
Eric Christopher91a31902013-01-16 01:22:32 +0000888 }
889
890 unsigned Flags = 0;
891 AccessSpecifier Access = Var->getAccess();
892 if (Access == clang::AS_private)
893 Flags |= llvm::DIDescriptor::FlagPrivate;
894 else if (Access == clang::AS_protected)
895 Flags |= llvm::DIDescriptor::FlagProtected;
896
David Blaikieae019462013-08-15 22:50:29 +0000897 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
898 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Eric Christopher91a31902013-01-16 01:22:32 +0000899 StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV);
David Blaikieae019462013-08-15 22:50:29 +0000900 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000901}
902
903/// CollectRecordNormalField - Helper for CollectRecordFields.
904void CGDebugInfo::
905CollectRecordNormalField(const FieldDecl *field, uint64_t OffsetInBits,
906 llvm::DIFile tunit,
907 SmallVectorImpl<llvm::Value *> &elements,
908 llvm::DIType RecordTy) {
909 StringRef name = field->getName();
910 QualType type = field->getType();
911
912 // Ignore unnamed fields unless they're anonymous structs/unions.
913 if (name.empty() && !type->isRecordType())
914 return;
915
916 uint64_t SizeInBitsOverride = 0;
917 if (field->isBitField()) {
918 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
919 assert(SizeInBitsOverride && "found named 0-width bitfield");
920 }
921
922 llvm::DIType fieldType
923 = createFieldType(name, type, SizeInBitsOverride,
924 field->getLocation(), field->getAccess(),
925 OffsetInBits, tunit, RecordTy);
926
927 elements.push_back(fieldType);
928}
929
Guy Benyei11169dd2012-12-18 14:30:41 +0000930/// CollectRecordFields - A helper function to collect debug info for
931/// record fields. This is used while creating debug info entry for a Record.
David Blaikieab255bb2013-08-16 20:40:25 +0000932void CGDebugInfo::CollectRecordFields(const RecordDecl *record,
933 llvm::DIFile tunit,
934 SmallVectorImpl<llvm::Value *> &elements,
935 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000936 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
937
Eric Christopher91a31902013-01-16 01:22:32 +0000938 if (CXXDecl && CXXDecl->isLambda())
939 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
940 else {
941 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000942
Eric Christopher91a31902013-01-16 01:22:32 +0000943 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000944 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000945
Eric Christopher91a31902013-01-16 01:22:32 +0000946 // Static and non-static members should appear in the same order as
947 // the corresponding declarations in the source program.
948 for (RecordDecl::decl_iterator I = record->decls_begin(),
949 E = record->decls_end(); I != E; ++I)
950 if (const VarDecl *V = dyn_cast<VarDecl>(*I))
David Blaikie7d5d7c72013-08-17 00:06:55 +0000951 elements.push_back(CreateRecordStaticField(V, RecordTy));
Eric Christopher91a31902013-01-16 01:22:32 +0000952 else if (FieldDecl *field = dyn_cast<FieldDecl>(*I)) {
Eric Christopher91a31902013-01-16 01:22:32 +0000953 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo),
954 tunit, elements, RecordTy);
955
956 // Bump field number for next field.
957 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000958 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 }
960}
961
962/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
963/// function type is not updated to include implicit "this" pointer. Use this
964/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +0000965llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +0000966CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
967 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +0000968 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +0000969 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +0000970 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +0000971 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
972 Func, Unit);
973}
David Blaikie2aaf0652013-01-07 22:24:59 +0000974
David Blaikie469f0792013-05-22 23:22:42 +0000975llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +0000976 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000977 // Add "this" pointer.
David Blaikie7eb06852013-01-07 23:06:35 +0000978 llvm::DIArray Args = llvm::DICompositeType(
979 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +0000980 assert (Args.getNumElements() && "Invalid number of arguments!");
981
982 SmallVector<llvm::Value *, 16> Elts;
983
984 // First element is always return type. For 'void' functions it is NULL.
985 Elts.push_back(Args.getElement(0));
986
David Blaikie2aaf0652013-01-07 22:24:59 +0000987 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +0000988 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +0000989 if (isa<ClassTemplateSpecializationDecl>(RD)) {
990 // Create pointer type directly in this case.
991 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
992 QualType PointeeTy = ThisPtrTy->getPointeeType();
993 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000994 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +0000995 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
996 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +0000997 llvm::DIType ThisPtrType =
998 DBuilder.createPointerType(PointeeType, Size, Align);
David Blaikie2aaf0652013-01-07 22:24:59 +0000999 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1000 // TODO: This and the artificial type below are misleading, the
1001 // types aren't artificial the argument is, but the current
1002 // metadata doesn't represent that.
1003 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1004 Elts.push_back(ThisPtrType);
1005 } else {
1006 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
1007 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1008 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1009 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 }
1011
1012 // Copy rest of the arguments.
1013 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1014 Elts.push_back(Args.getElement(i));
1015
1016 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1017
1018 return DBuilder.createSubroutineType(Unit, EltTypeArray);
1019}
1020
Eric Christopherb2a008c2013-05-16 00:45:12 +00001021/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001022/// inside a function.
1023static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1024 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1025 return isFunctionLocalClass(NRD);
1026 if (isa<FunctionDecl>(RD->getDeclContext()))
1027 return true;
1028 return false;
1029}
1030
1031/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1032/// a single member function GlobalDecl.
1033llvm::DISubprogram
1034CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
1035 llvm::DIFile Unit,
1036 llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001037 bool IsCtorOrDtor =
Guy Benyei11169dd2012-12-18 14:30:41 +00001038 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001039
Guy Benyei11169dd2012-12-18 14:30:41 +00001040 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001041 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001042
1043 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1044 // make sense to give a single ctor/dtor a linkage name.
1045 StringRef MethodLinkageName;
1046 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1047 MethodLinkageName = CGM.getMangledName(Method);
1048
1049 // Get the location for the method.
1050 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
1051 unsigned MethodLine = getLineNumber(Method->getLocation());
1052
1053 // Collect virtual method info.
1054 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001055 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001056 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001057
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 if (Method->isVirtual()) {
1059 if (Method->isPure())
1060 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1061 else
1062 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001063
Guy Benyei11169dd2012-12-18 14:30:41 +00001064 // It doesn't make sense to give a virtual destructor a vtable index,
1065 // since a single destructor has two entries in the vtable.
1066 if (!isa<CXXDestructorDecl>(Method))
1067 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
1068 ContainingType = RecordTy;
1069 }
1070
1071 unsigned Flags = 0;
1072 if (Method->isImplicit())
1073 Flags |= llvm::DIDescriptor::FlagArtificial;
1074 AccessSpecifier Access = Method->getAccess();
1075 if (Access == clang::AS_private)
1076 Flags |= llvm::DIDescriptor::FlagPrivate;
1077 else if (Access == clang::AS_protected)
1078 Flags |= llvm::DIDescriptor::FlagProtected;
1079 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1080 if (CXXC->isExplicit())
1081 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001082 } else if (const CXXConversionDecl *CXXC =
Guy Benyei11169dd2012-12-18 14:30:41 +00001083 dyn_cast<CXXConversionDecl>(Method)) {
1084 if (CXXC->isExplicit())
1085 Flags |= llvm::DIDescriptor::FlagExplicit;
1086 }
1087 if (Method->hasPrototype())
1088 Flags |= llvm::DIDescriptor::FlagPrototyped;
1089
1090 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1091 llvm::DISubprogram SP =
Eric Christopherb2a008c2013-05-16 00:45:12 +00001092 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Guy Benyei11169dd2012-12-18 14:30:41 +00001093 MethodDefUnit, MethodLine,
Eric Christopherb2a008c2013-05-16 00:45:12 +00001094 MethodTy, /*isLocalToUnit=*/false,
Guy Benyei11169dd2012-12-18 14:30:41 +00001095 /* isDefinition=*/ false,
1096 Virtuality, VIndex, ContainingType,
1097 Flags, CGM.getLangOpts().Optimize, NULL,
1098 TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001099
Guy Benyei11169dd2012-12-18 14:30:41 +00001100 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
1101
1102 return SP;
1103}
1104
1105/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001106/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001107/// a Record.
1108void CGDebugInfo::
1109CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
1110 SmallVectorImpl<llvm::Value *> &EltTys,
1111 llvm::DIType RecordTy) {
1112
1113 // Since we want more than just the individual member decls if we
1114 // have templated functions iterate over every declaration to gather
1115 // the functions.
1116 for(DeclContext::decl_iterator I = RD->decls_begin(),
1117 E = RD->decls_end(); I != E; ++I) {
1118 Decl *D = *I;
David Blaikiefd07c602013-08-09 17:20:05 +00001119 if (D->isImplicit())
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 continue;
1121
David Blaikie7d5d7c72013-08-17 00:06:55 +00001122 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1123 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001125}
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
1127/// CollectCXXFriends - A helper function to collect debug info for
1128/// C++ base classes. This is used while creating debug info entry for
1129/// a Record.
1130void CGDebugInfo::
1131CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
1132 SmallVectorImpl<llvm::Value *> &EltTys,
1133 llvm::DIType RecordTy) {
1134 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
1135 BE = RD->friend_end(); BI != BE; ++BI) {
1136 if ((*BI)->isUnsupportedFriend())
1137 continue;
1138 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
David Blaikie50438152013-08-18 04:50:23 +00001139 EltTys.push_back(DBuilder.createFriend(
1140 RecordTy, getOrCreateType(TInfo->getType(), Unit, true)));
Guy Benyei11169dd2012-12-18 14:30:41 +00001141 }
1142}
1143
1144/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001145/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001146/// a Record.
1147void CGDebugInfo::
1148CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
1149 SmallVectorImpl<llvm::Value *> &EltTys,
1150 llvm::DIType RecordTy) {
1151
1152 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1153 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1154 BE = RD->bases_end(); BI != BE; ++BI) {
1155 unsigned BFlags = 0;
1156 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001157
Guy Benyei11169dd2012-12-18 14:30:41 +00001158 const CXXRecordDecl *Base =
1159 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001160
Guy Benyei11169dd2012-12-18 14:30:41 +00001161 if (BI->isVirtual()) {
1162 // virtual base offset offset is -ve. The code generator emits dwarf
1163 // expression where it expects +ve number.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001164 BaseOffset =
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 0 - CGM.getVTableContext()
1166 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
1167 BFlags = llvm::DIDescriptor::FlagVirtual;
1168 } else
1169 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1170 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1171 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001172
Guy Benyei11169dd2012-12-18 14:30:41 +00001173 AccessSpecifier Access = BI->getAccessSpecifier();
1174 if (Access == clang::AS_private)
1175 BFlags |= llvm::DIDescriptor::FlagPrivate;
1176 else if (Access == clang::AS_protected)
1177 BFlags |= llvm::DIDescriptor::FlagProtected;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001178
1179 llvm::DIType DTy =
1180 DBuilder.createInheritance(RecordTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001181 getOrCreateType(BI->getType(), Unit),
1182 BaseOffset, BFlags);
1183 EltTys.push_back(DTy);
1184 }
1185}
1186
1187/// CollectTemplateParams - A helper function to collect template parameters.
1188llvm::DIArray CGDebugInfo::
1189CollectTemplateParams(const TemplateParameterList *TPList,
David Blaikie47c11502013-06-22 18:59:18 +00001190 ArrayRef<TemplateArgument> TAList,
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001192 SmallVector<llvm::Value *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1194 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001195 StringRef Name;
1196 if (TPList)
1197 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001198 switch (TA.getKind()) {
1199 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001200 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1201 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001202 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001203 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001204 } break;
1205 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001206 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1207 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001208 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001209 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001210 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1211 TemplateParams.push_back(TVP);
1212 } break;
1213 case TemplateArgument::Declaration: {
1214 const ValueDecl *D = TA.getAsDecl();
1215 bool InstanceMember = D->isCXXInstanceMember();
1216 QualType T = InstanceMember
1217 ? CGM.getContext().getMemberPointerType(
1218 D->getType(), cast<RecordDecl>(D->getDeclContext())
1219 ->getTypeForDecl())
1220 : CGM.getContext().getPointerType(D->getType());
1221 llvm::DIType TTy = getOrCreateType(T, Unit);
1222 llvm::Value *V = 0;
1223 // Variable pointer template parameters have a value that is the address
1224 // of the variable.
1225 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
1226 V = CGM.GetAddrOfGlobalVar(VD);
1227 // Member function pointers have special support for building them, though
1228 // this is currently unsupported in LLVM CodeGen.
David Blaikied900f982013-05-13 06:57:50 +00001229 if (InstanceMember) {
David Blaikie38079fd2013-05-10 21:53:14 +00001230 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(D))
1231 V = CGM.getCXXABI().EmitMemberPointer(method);
David Blaikied900f982013-05-13 06:57:50 +00001232 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1233 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001234 // Member data pointers have special handling too to compute the fixed
1235 // offset within the object.
1236 if (isa<FieldDecl>(D)) {
1237 // These five lines (& possibly the above member function pointer
1238 // handling) might be able to be refactored to use similar code in
1239 // CodeGenModule::getMemberPointerConstant
1240 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1241 CharUnits chars =
1242 CGM.getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1243 V = CGM.getCXXABI().EmitMemberDataPointer(
1244 cast<MemberPointerType>(T.getTypePtr()), chars);
1245 }
1246 llvm::DITemplateValueParameter TVP =
David Blaikie47c11502013-06-22 18:59:18 +00001247 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie38079fd2013-05-10 21:53:14 +00001248 TemplateParams.push_back(TVP);
1249 } break;
1250 case TemplateArgument::NullPtr: {
1251 QualType T = TA.getNullPtrType();
1252 llvm::DIType TTy = getOrCreateType(T, Unit);
1253 llvm::Value *V = 0;
1254 // Special case member data pointer null values since they're actually -1
1255 // instead of zero.
1256 if (const MemberPointerType *MPT =
1257 dyn_cast<MemberPointerType>(T.getTypePtr()))
1258 // But treat member function pointers as simple zero integers because
1259 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1260 // CodeGen grows handling for values of non-null member function
1261 // pointers then perhaps we could remove this special case and rely on
1262 // EmitNullMemberPointer for member function pointers.
1263 if (MPT->isMemberDataPointer())
1264 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1265 if (!V)
1266 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1267 llvm::DITemplateValueParameter TVP =
David Blaikie47c11502013-06-22 18:59:18 +00001268 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie38079fd2013-05-10 21:53:14 +00001269 TemplateParams.push_back(TVP);
1270 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001271 case TemplateArgument::Template: {
1272 llvm::DITemplateValueParameter TVP =
1273 DBuilder.createTemplateTemplateParameter(
1274 TheCU, Name, llvm::DIType(),
1275 TA.getAsTemplate().getAsTemplateDecl()
1276 ->getQualifiedNameAsString());
1277 TemplateParams.push_back(TVP);
1278 } break;
1279 case TemplateArgument::Pack: {
1280 llvm::DITemplateValueParameter TVP =
1281 DBuilder.createTemplateParameterPack(
1282 TheCU, Name, llvm::DIType(),
1283 CollectTemplateParams(NULL, TA.getPackAsArray(), Unit));
1284 TemplateParams.push_back(TVP);
1285 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001286 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001287 case TemplateArgument::Expression:
1288 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001289 case TemplateArgument::Null:
1290 llvm_unreachable(
1291 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001292 }
1293 }
1294 return DBuilder.getOrCreateArray(TemplateParams);
1295}
1296
1297/// CollectFunctionTemplateParams - A helper function to collect debug
1298/// info for function template parameters.
1299llvm::DIArray CGDebugInfo::
1300CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
1301 if (FD->getTemplatedKind() ==
1302 FunctionDecl::TK_FunctionTemplateSpecialization) {
1303 const TemplateParameterList *TList =
1304 FD->getTemplateSpecializationInfo()->getTemplate()
1305 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001306 return CollectTemplateParams(
1307 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001308 }
1309 return llvm::DIArray();
1310}
1311
1312/// CollectCXXTemplateParams - A helper function to collect debug info for
1313/// template parameters.
1314llvm::DIArray CGDebugInfo::
1315CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1316 llvm::DIFile Unit) {
1317 llvm::PointerUnion<ClassTemplateDecl *,
1318 ClassTemplatePartialSpecializationDecl *>
1319 PU = TSpecial->getSpecializedTemplateOrPartial();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001320
Guy Benyei11169dd2012-12-18 14:30:41 +00001321 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1322 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1323 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1324 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001325 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001326}
1327
1328/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1329llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1330 if (VTablePtrType.isValid())
1331 return VTablePtrType;
1332
1333 ASTContext &Context = CGM.getContext();
1334
1335 /* Function type */
1336 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
1337 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
1338 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1339 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
1340 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
1341 "__vtbl_ptr_type");
1342 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1343 return VTablePtrType;
1344}
1345
1346/// getVTableName - Get vtable name for the given Class.
1347StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
1348 // Construct gdb compatible name name.
1349 std::string Name = "_vptr$" + RD->getNameAsString();
1350
1351 // Copy this name on the side and use its reference.
1352 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
1353 memcpy(StrPtr, Name.data(), Name.length());
1354 return StringRef(StrPtr, Name.length());
1355}
1356
1357
1358/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1359/// debug info entry in EltTys vector.
1360void CGDebugInfo::
1361CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1362 SmallVectorImpl<llvm::Value *> &EltTys) {
1363 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1364
1365 // If there is a primary base then it will hold vtable info.
1366 if (RL.getPrimaryBase())
1367 return;
1368
1369 // If this class is not dynamic then there is not any vtable info to collect.
1370 if (!RD->isDynamicClass())
1371 return;
1372
1373 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1374 llvm::DIType VPTR
1375 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Eric Christopher0fdcb312013-05-16 00:52:20 +00001376 0, Size, 0, 0,
1377 llvm::DIDescriptor::FlagArtificial,
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 getOrCreateVTablePtrType(Unit));
1379 EltTys.push_back(VPTR);
1380}
1381
Eric Christopherb2a008c2013-05-16 00:45:12 +00001382/// getOrCreateRecordType - Emit record type's standalone debug info.
1383llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001384 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001385 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001386 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1387 return T;
1388}
1389
1390/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1391/// debug info.
1392llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001393 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001394 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001396 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 return T;
1398}
1399
David Blaikieb2e86eb2013-08-15 20:49:17 +00001400void CGDebugInfo::completeType(const RecordDecl *RD) {
1401 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1402 !CGM.getLangOpts().CPlusPlus)
1403 completeRequiredType(RD);
1404}
1405
1406void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1407 QualType Ty = CGM.getContext().getRecordType(RD);
1408 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie7d5d7c72013-08-17 00:06:55 +00001409 if (!T || !T.isForwardDecl())
1410 return;
David Blaikieb2e86eb2013-08-15 20:49:17 +00001411 void* TyPtr = Ty.getAsOpaquePtr();
1412 if (CompletedTypeCache.count(TyPtr))
1413 return;
1414 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1415 assert(!Res.isForwardDecl());
1416 CompletedTypeCache[TyPtr] = Res;
1417 TypeCache[TyPtr] = Res;
1418}
1419
Guy Benyei11169dd2012-12-18 14:30:41 +00001420/// CreateType - get structure or union type.
David Blaikiee36464c2013-06-05 05:32:23 +00001421llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, bool Declaration) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 RecordDecl *RD = Ty->getDecl();
Adrian Prantl1f56b2a2013-06-18 23:32:21 +00001423 // Limited debug info should only remove struct definitions that can
1424 // safely be replaced by a forward declaration in the source code.
David Blaikie7d5d7c72013-08-17 00:06:55 +00001425 if (DebugKind <= CodeGenOptions::LimitedDebugInfo && Declaration &&
1426 !RD->isCompleteDefinitionRequired() && CGM.getLangOpts().CPlusPlus) {
David Blaikiee36464c2013-06-05 05:32:23 +00001427 llvm::DIDescriptor FDContext =
1428 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
David Blaikied816e2d2013-08-15 18:59:40 +00001429 llvm::DIType RetTy = getOrCreateRecordFwdDecl(RD, FDContext);
David Blaikie7d5d7c72013-08-17 00:06:55 +00001430 return RetTy;
David Blaikiee36464c2013-06-05 05:32:23 +00001431 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001432
David Blaikieb2e86eb2013-08-15 20:49:17 +00001433 return CreateTypeDefinition(Ty);
1434}
1435
1436llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1437 RecordDecl *RD = Ty->getDecl();
1438
Guy Benyei11169dd2012-12-18 14:30:41 +00001439 // Get overall information about the record type for the debug info.
1440 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1441
1442 // Records and classes and unions can all be recursive. To handle them, we
1443 // first generate a debug descriptor for the struct as a forward declaration.
1444 // Then (if it is a definition) we go through and get debug info for all of
1445 // its members. Finally, we create a descriptor for the complete type (which
1446 // may refer to the forward decl if the struct is recursive) and replace all
1447 // uses of the forward declaration with the final definition.
1448
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001449 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001450 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001451 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001452
1453 if (FwdDecl.isForwardDecl())
1454 return FwdDecl;
1455
David Blaikieadfbf992013-08-18 16:55:33 +00001456 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1457 CollectContainingType(CXXDecl, FwdDecl);
1458
Guy Benyei11169dd2012-12-18 14:30:41 +00001459 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001460 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1462
Adrian Prantla03a85a2013-03-06 22:03:30 +00001463 // Add this to the completed-type cache while we're completing it recursively.
Guy Benyei11169dd2012-12-18 14:30:41 +00001464 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1465
1466 // Convert all the elements.
1467 SmallVector<llvm::Value *, 16> EltTys;
1468
1469 // Note: The split of CXXDecl information here is intentional, the
1470 // gdb tests will depend on a certain ordering at printout. The debug
1471 // information offsets are still correct if we merge them all together
1472 // though.
1473 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1474 if (CXXDecl) {
1475 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1476 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1477 }
1478
Eric Christopher91a31902013-01-16 01:22:32 +00001479 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001480 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 if (CXXDecl) {
1482 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1483 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 }
1485
1486 LexicalBlockStack.pop_back();
1487 RegionMap.erase(Ty->getDecl());
1488
1489 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
David Blaikie4a5b8952013-08-01 20:31:40 +00001490 FwdDecl.setTypeArray(Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001491
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001492 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1493 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001494}
1495
1496/// CreateType - get objective-c object type.
1497llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1498 llvm::DIFile Unit) {
1499 // Ignore protocols.
1500 return getOrCreateType(Ty->getBaseType(), Unit);
1501}
1502
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001503
1504/// \return true if Getter has the default name for the property PD.
1505static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1506 const ObjCMethodDecl *Getter) {
1507 assert(PD);
1508 if (!Getter)
1509 return true;
1510
1511 assert(Getter->getDeclName().isObjCZeroArgSelector());
1512 return PD->getName() ==
1513 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
1514}
1515
1516/// \return true if Setter has the default name for the property PD.
1517static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1518 const ObjCMethodDecl *Setter) {
1519 assert(PD);
1520 if (!Setter)
1521 return true;
1522
1523 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001524 return SelectorTable::constructSetterName(PD->getName()) ==
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001525 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
1526}
1527
Guy Benyei11169dd2012-12-18 14:30:41 +00001528/// CreateType - get objective-c interface type.
1529llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1530 llvm::DIFile Unit) {
1531 ObjCInterfaceDecl *ID = Ty->getDecl();
1532 if (!ID)
1533 return llvm::DIType();
1534
1535 // Get overall information about the record type for the debug info.
1536 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1537 unsigned Line = getLineNumber(ID->getLocation());
1538 unsigned RuntimeLang = TheCU.getLanguage();
1539
1540 // If this is just a forward declaration return a special forward-declaration
1541 // debug type since we won't be able to lay out the entire type.
1542 ObjCInterfaceDecl *Def = ID->getDefinition();
1543 if (!Def) {
1544 llvm::DIType FwdDecl =
1545 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001546 ID->getName(), TheCU, DefUnit, Line,
1547 RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 return FwdDecl;
1549 }
1550
1551 ID = Def;
1552
1553 // Bit size, align and offset of the type.
1554 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1555 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1556
1557 unsigned Flags = 0;
1558 if (ID->getImplementation())
1559 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1560
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001561 llvm::DICompositeType RealDecl =
Guy Benyei11169dd2012-12-18 14:30:41 +00001562 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1563 Line, Size, Align, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00001564 llvm::DIType(), llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001565
1566 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1567 // will find it and we're emitting the complete type.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001568 QualType QualTy = QualType(Ty, 0);
1569 CompletedTypeCache[QualTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001570
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001571 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001572 LexicalBlockStack.push_back(static_cast<llvm::MDNode*>(RealDecl));
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1574
1575 // Convert all the elements.
1576 SmallVector<llvm::Value *, 16> EltTys;
1577
1578 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1579 if (SClass) {
1580 llvm::DIType SClassTy =
1581 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
1582 if (!SClassTy.isValid())
1583 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001584
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 llvm::DIType InhTag =
1586 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
1587 EltTys.push_back(InhTag);
1588 }
1589
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001590 // Create entries for all of the properties.
Guy Benyei11169dd2012-12-18 14:30:41 +00001591 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1592 E = ID->prop_end(); I != E; ++I) {
1593 const ObjCPropertyDecl *PD = *I;
1594 SourceLocation Loc = PD->getLocation();
1595 llvm::DIFile PUnit = getOrCreateFile(Loc);
1596 unsigned PLine = getLineNumber(Loc);
1597 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1598 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1599 llvm::MDNode *PropertyNode =
1600 DBuilder.createObjCProperty(PD->getName(),
Eric Christopherc0c5d462013-02-21 22:35:08 +00001601 PUnit, PLine,
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001602 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 getSelectorName(PD->getGetterName()),
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001604 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001605 getSelectorName(PD->getSetterName()),
1606 PD->getPropertyAttributes(),
Eric Christopherc0c5d462013-02-21 22:35:08 +00001607 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001608 EltTys.push_back(PropertyNode);
1609 }
1610
1611 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1612 unsigned FieldNo = 0;
1613 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1614 Field = Field->getNextIvar(), ++FieldNo) {
1615 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1616 if (!FieldTy.isValid())
1617 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001618
Guy Benyei11169dd2012-12-18 14:30:41 +00001619 StringRef FieldName = Field->getName();
1620
1621 // Ignore unnamed fields.
1622 if (FieldName.empty())
1623 continue;
1624
1625 // Get the location for the field.
1626 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1627 unsigned FieldLine = getLineNumber(Field->getLocation());
1628 QualType FType = Field->getType();
1629 uint64_t FieldSize = 0;
1630 unsigned FieldAlign = 0;
1631
1632 if (!FType->isIncompleteArrayType()) {
1633
1634 // Bit size, align and offset of the type.
1635 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001636 ? Field->getBitWidthValue(CGM.getContext())
1637 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001638 FieldAlign = CGM.getContext().getTypeAlign(FType);
1639 }
1640
1641 uint64_t FieldOffset;
1642 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1643 // We don't know the runtime offset of an ivar if we're using the
1644 // non-fragile ABI. For bitfields, use the bit offset into the first
1645 // byte of storage of the bitfield. For other fields, use zero.
1646 if (Field->isBitField()) {
1647 FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset(
1648 CGM, ID, Field);
1649 FieldOffset %= CGM.getContext().getCharWidth();
1650 } else {
1651 FieldOffset = 0;
1652 }
1653 } else {
1654 FieldOffset = RL.getFieldOffset(FieldNo);
1655 }
1656
1657 unsigned Flags = 0;
1658 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1659 Flags = llvm::DIDescriptor::FlagProtected;
1660 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1661 Flags = llvm::DIDescriptor::FlagPrivate;
1662
1663 llvm::MDNode *PropertyNode = NULL;
1664 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001665 if (ObjCPropertyImplDecl *PImpD =
Guy Benyei11169dd2012-12-18 14:30:41 +00001666 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1667 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001668 SourceLocation Loc = PD->getLocation();
1669 llvm::DIFile PUnit = getOrCreateFile(Loc);
1670 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001671 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1672 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1673 PropertyNode =
1674 DBuilder.createObjCProperty(PD->getName(),
1675 PUnit, PLine,
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001676 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001677 getSelectorName(PD->getGetterName()),
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001678 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001679 getSelectorName(PD->getSetterName()),
1680 PD->getPropertyAttributes(),
1681 getOrCreateType(PD->getType(), PUnit));
1682 }
1683 }
1684 }
1685 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1686 FieldLine, FieldSize, FieldAlign,
1687 FieldOffset, Flags, FieldTy,
1688 PropertyNode);
1689 EltTys.push_back(FieldTy);
1690 }
1691
1692 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001693 RealDecl.setTypeArray(Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001694
1695 // If the implementation is not yet set, we do not want to mark it
1696 // as complete. An implementation may declare additional
1697 // private ivars that we would miss otherwise.
1698 if (ID->getImplementation() == 0)
1699 CompletedTypeCache.erase(QualTy.getAsOpaquePtr());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001700
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001702 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001703}
1704
1705llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1706 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1707 int64_t Count = Ty->getNumElements();
1708 if (Count == 0)
1709 // If number of elements are not known then this is an unbounded array.
1710 // Use Count == -1 to express such arrays.
1711 Count = -1;
1712
1713 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1714 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1715
1716 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1717 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1718
1719 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1720}
1721
1722llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1723 llvm::DIFile Unit) {
1724 uint64_t Size;
1725 uint64_t Align;
1726
1727 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1728 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1729 Size = 0;
1730 Align =
1731 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
1732 } else if (Ty->isIncompleteArrayType()) {
1733 Size = 0;
1734 if (Ty->getElementType()->isIncompleteType())
1735 Align = 0;
1736 else
1737 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001738 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 Size = 0;
1740 Align = 0;
1741 } else {
1742 // Size and align of the whole array, not the element type.
1743 Size = CGM.getContext().getTypeSize(Ty);
1744 Align = CGM.getContext().getTypeAlign(Ty);
1745 }
1746
1747 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1748 // interior arrays, do we care? Why aren't nested arrays represented the
1749 // obvious/recursive way?
1750 SmallVector<llvm::Value *, 8> Subscripts;
1751 QualType EltTy(Ty, 0);
1752 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1753 // If the number of elements is known, then count is that number. Otherwise,
1754 // it's -1. This allows us to represent a subrange with an array of 0
1755 // elements, like this:
1756 //
1757 // struct foo {
1758 // int x[0];
1759 // };
1760 int64_t Count = -1; // Count == -1 is an unbounded array.
1761 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1762 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001763
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 // FIXME: Verify this is right for VLAs.
1765 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1766 EltTy = Ty->getElementType();
1767 }
1768
1769 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1770
Eric Christopherb2a008c2013-05-16 00:45:12 +00001771 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1773 SubscriptArray);
1774 return DbgTy;
1775}
1776
Eric Christopherb2a008c2013-05-16 00:45:12 +00001777llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001779 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
Guy Benyei11169dd2012-12-18 14:30:41 +00001780 Ty, Ty->getPointeeType(), Unit);
1781}
1782
Eric Christopherb2a008c2013-05-16 00:45:12 +00001783llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001784 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001785 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 Ty, Ty->getPointeeType(), Unit);
1787}
1788
Eric Christopherb2a008c2013-05-16 00:45:12 +00001789llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001791 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1792 if (!Ty->getPointeeType()->isFunctionType())
1793 return DBuilder.createMemberPointerType(
David Blaikie6b7d060c2013-08-12 23:14:36 +00001794 getOrCreateType(Ty->getPointeeType(), U, true), ClassType);
David Blaikie2c705ca2013-01-19 19:20:56 +00001795 return DBuilder.createMemberPointerType(getOrCreateInstanceMethodType(
1796 CGM.getContext().getPointerType(
1797 QualType(Ty->getClass(), Ty->getPointeeType().getCVRQualifiers())),
1798 Ty->getPointeeType()->getAs<FunctionProtoType>(), U),
1799 ClassType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001800}
1801
Eric Christopherb2a008c2013-05-16 00:45:12 +00001802llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001803 llvm::DIFile U) {
1804 // Ignore the atomic wrapping
1805 // FIXME: What is the correct representation?
1806 return getOrCreateType(Ty->getValueType(), U);
1807}
1808
1809/// CreateEnumType - get enumeration type.
1810llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1811 uint64_t Size = 0;
1812 uint64_t Align = 0;
1813 if (!ED->getTypeForDecl()->isIncompleteType()) {
1814 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1815 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1816 }
1817
1818 // If this is just a forward declaration, construct an appropriately
1819 // marked node and just return it.
1820 if (!ED->getDefinition()) {
1821 llvm::DIDescriptor EDContext;
1822 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1823 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1824 unsigned Line = getLineNumber(ED->getLocation());
1825 StringRef EDName = ED->getName();
1826 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1827 EDName, EDContext, DefUnit, Line, 0,
1828 Size, Align);
1829 }
1830
1831 // Create DIEnumerator elements for each enumerator.
1832 SmallVector<llvm::Value *, 16> Enumerators;
1833 ED = ED->getDefinition();
1834 for (EnumDecl::enumerator_iterator
1835 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1836 Enum != EnumEnd; ++Enum) {
1837 Enumerators.push_back(
1838 DBuilder.createEnumerator(Enum->getName(),
David Blaikiece1ae382013-06-24 07:13:13 +00001839 Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001840 }
1841
1842 // Return a CompositeType for the enum itself.
1843 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1844
1845 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1846 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001847 llvm::DIDescriptor EnumContext =
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Adrian Prantlc60dc712013-04-19 19:56:39 +00001849 llvm::DIType ClassTy = ED->isFixed() ?
Guy Benyei11169dd2012-12-18 14:30:41 +00001850 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001851 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1853 Size, Align, EltArray,
1854 ClassTy);
1855 return DbgTy;
1856}
1857
David Blaikie05491062013-01-21 04:37:12 +00001858static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1859 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001860 do {
David Blaikie05491062013-01-21 04:37:12 +00001861 Quals += T.getLocalQualifiers();
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 QualType LastT = T;
1863 switch (T->getTypeClass()) {
1864 default:
David Blaikie05491062013-01-21 04:37:12 +00001865 return C.getQualifiedType(T.getTypePtr(), Quals);
Guy Benyei11169dd2012-12-18 14:30:41 +00001866 case Type::TemplateSpecialization:
1867 T = cast<TemplateSpecializationType>(T)->desugar();
1868 break;
1869 case Type::TypeOfExpr:
1870 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1871 break;
1872 case Type::TypeOf:
1873 T = cast<TypeOfType>(T)->getUnderlyingType();
1874 break;
1875 case Type::Decltype:
1876 T = cast<DecltypeType>(T)->getUnderlyingType();
1877 break;
1878 case Type::UnaryTransform:
1879 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1880 break;
1881 case Type::Attributed:
1882 T = cast<AttributedType>(T)->getEquivalentType();
1883 break;
1884 case Type::Elaborated:
1885 T = cast<ElaboratedType>(T)->getNamedType();
1886 break;
1887 case Type::Paren:
1888 T = cast<ParenType>(T)->getInnerType();
1889 break;
David Blaikie05491062013-01-21 04:37:12 +00001890 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00001892 break;
1893 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00001894 QualType DT = cast<AutoType>(T)->getDeducedType();
1895 if (DT.isNull())
1896 return T;
1897 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 break;
1899 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001900
Guy Benyei11169dd2012-12-18 14:30:41 +00001901 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00001902 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 } while (true);
1904}
1905
Eric Christopher0fdcb312013-05-16 00:52:20 +00001906/// getType - Get the type from the cache or return null type if it doesn't
1907/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00001908llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
1909
1910 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00001911 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001912
Guy Benyei11169dd2012-12-18 14:30:41 +00001913 // Check for existing entry.
Adrian Prantl73409ce2013-03-11 18:33:46 +00001914 if (Ty->getTypeClass() == Type::ObjCInterface) {
1915 llvm::Value *V = getCachedInterfaceTypeOrNull(Ty);
1916 if (V)
1917 return llvm::DIType(cast<llvm::MDNode>(V));
1918 else return llvm::DIType();
1919 }
1920
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1922 TypeCache.find(Ty.getAsOpaquePtr());
1923 if (it != TypeCache.end()) {
1924 // Verify that the debug info still exists.
1925 if (llvm::Value *V = it->second)
1926 return llvm::DIType(cast<llvm::MDNode>(V));
1927 }
1928
1929 return llvm::DIType();
1930}
1931
1932/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1933/// doesn't exist.
1934llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1935
1936 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00001937 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001938
1939 // Check for existing entry.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001940 llvm::Value *V = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1942 CompletedTypeCache.find(Ty.getAsOpaquePtr());
Adrian Prantla03a85a2013-03-06 22:03:30 +00001943 if (it != CompletedTypeCache.end())
1944 V = it->second;
1945 else {
Adrian Prantl73409ce2013-03-11 18:33:46 +00001946 V = getCachedInterfaceTypeOrNull(Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00001947 }
1948
Adrian Prantla03a85a2013-03-06 22:03:30 +00001949 // Verify that any cached debug info still exists.
David Blaikie80d28de2013-08-13 04:21:38 +00001950 return llvm::DIType(cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +00001951}
1952
Adrian Prantl73409ce2013-03-11 18:33:46 +00001953/// getCachedInterfaceTypeOrNull - Get the type from the interface
1954/// cache, unless it needs to regenerated. Otherwise return null.
1955llvm::Value *CGDebugInfo::getCachedInterfaceTypeOrNull(QualType Ty) {
1956 // Is there a cached interface that hasn't changed?
1957 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
1958 ::iterator it1 = ObjCInterfaceCache.find(Ty.getAsOpaquePtr());
1959
1960 if (it1 != ObjCInterfaceCache.end())
1961 if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty))
1962 if (Checksum(Decl) == it1->second.second)
1963 // Return cached forward declaration.
1964 return it1->second.first;
1965
1966 return 0;
1967}
Guy Benyei11169dd2012-12-18 14:30:41 +00001968
1969/// getOrCreateType - Get the type from the cache or create a new
1970/// one if necessary.
Eric Christopher1ecc5632013-06-07 22:54:39 +00001971llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit,
1972 bool Declaration) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001973 if (Ty.isNull())
1974 return llvm::DIType();
1975
1976 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00001977 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001978
David Blaikie80d28de2013-08-13 04:21:38 +00001979 if (llvm::DIType T = getCompletedTypeOrNull(Ty)) {
David Blaikie940ca4d2013-06-21 21:03:11 +00001980 // If we're looking for a definition, make sure we have definitions of any
1981 // underlying types.
1982 if (const TypedefType* TTy = dyn_cast<TypedefType>(Ty))
1983 getOrCreateType(TTy->getDecl()->getUnderlyingType(), Unit, Declaration);
1984 if (Ty.hasLocalQualifiers())
1985 getOrCreateType(QualType(Ty.getTypePtr(), 0), Unit, Declaration);
Guy Benyei11169dd2012-12-18 14:30:41 +00001986 return T;
David Blaikie940ca4d2013-06-21 21:03:11 +00001987 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001988
1989 // Otherwise create the type.
David Blaikiee36464c2013-06-05 05:32:23 +00001990 llvm::DIType Res = CreateTypeNode(Ty, Unit, Declaration);
Adrian Prantl73409ce2013-03-11 18:33:46 +00001991 void* TyPtr = Ty.getAsOpaquePtr();
1992
1993 // And update the type cache.
1994 TypeCache[TyPtr] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00001995
David Blaikie6a723442013-08-15 21:21:19 +00001996 // FIXME: this getTypeOrNull call seems silly when we just inserted the type
1997 // into the cache - but getTypeOrNull has a special case for cached interface
1998 // types. We should probably just pull that out as a special case for the
1999 // "else" block below & skip the otherwise needless lookup.
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 llvm::DIType TC = getTypeOrNull(Ty);
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002001 if (TC && TC.isForwardDecl())
Adrian Prantl73409ce2013-03-11 18:33:46 +00002002 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
2003 else if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty)) {
2004 // Interface types may have elements added to them by a
2005 // subsequent implementation or extension, so we keep them in
2006 // the ObjCInterfaceCache together with a checksum. Instead of
Adrian Prantlc20237d2013-05-08 23:37:22 +00002007 // the (possibly) incomplete interface type, we return a forward
Adrian Prantl73409ce2013-03-11 18:33:46 +00002008 // declaration that gets RAUW'd in CGDebugInfo::finalize().
David Blaikie8e5939b2013-05-21 18:29:40 +00002009 std::pair<llvm::WeakVH, unsigned> &V = ObjCInterfaceCache[TyPtr];
2010 if (V.first)
2011 return llvm::DIType(cast<llvm::MDNode>(V.first));
2012 TC = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2013 Decl->getName(), TheCU, Unit,
2014 getLineNumber(Decl->getLocation()),
2015 TheCU.getLanguage());
2016 // Store the forward declaration in the cache.
2017 V.first = TC;
2018 V.second = Checksum(Decl);
Adrian Prantl73409ce2013-03-11 18:33:46 +00002019
David Blaikie8e5939b2013-05-21 18:29:40 +00002020 // Register the type for replacement in finalize().
2021 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
2022
Adrian Prantl73409ce2013-03-11 18:33:46 +00002023 return TC;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002024 }
2025
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 if (!Res.isForwardDecl())
Adrian Prantl73409ce2013-03-11 18:33:46 +00002027 CompletedTypeCache[TyPtr] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002028
2029 return Res;
2030}
2031
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002032/// Currently the checksum of an interface includes the number of
2033/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002034unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002035 // The assumption is that the number of ivars can only increase
2036 // monotonically, so it is safe to just use their current number as
2037 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002038 unsigned Sum = 0;
2039 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
2040 Ivar != 0; Ivar = Ivar->getNextIvar())
2041 ++Sum;
2042
2043 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002044}
2045
2046ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2047 switch (Ty->getTypeClass()) {
2048 case Type::ObjCObjectPointer:
Eric Christopher0fdcb312013-05-16 00:52:20 +00002049 return getObjCInterfaceDecl(cast<ObjCObjectPointerType>(Ty)
2050 ->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002051 case Type::ObjCInterface:
2052 return cast<ObjCInterfaceType>(Ty)->getDecl();
2053 default:
2054 return 0;
2055 }
2056}
2057
Guy Benyei11169dd2012-12-18 14:30:41 +00002058/// CreateTypeNode - Create a new debug type node.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002059llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit,
2060 bool Declaration) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 // Handle qualifiers, which recursively handles what they refer to.
2062 if (Ty.hasLocalQualifiers())
David Blaikiee36464c2013-06-05 05:32:23 +00002063 return CreateQualifiedType(Ty, Unit, Declaration);
Guy Benyei11169dd2012-12-18 14:30:41 +00002064
2065 const char *Diag = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002066
Guy Benyei11169dd2012-12-18 14:30:41 +00002067 // Work out details of type.
2068 switch (Ty->getTypeClass()) {
2069#define TYPE(Class, Base)
2070#define ABSTRACT_TYPE(Class, Base)
2071#define NON_CANONICAL_TYPE(Class, Base)
2072#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2073#include "clang/AST/TypeNodes.def"
2074 llvm_unreachable("Dependent types cannot show up in debug information");
2075
2076 case Type::ExtVector:
2077 case Type::Vector:
2078 return CreateType(cast<VectorType>(Ty), Unit);
2079 case Type::ObjCObjectPointer:
2080 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2081 case Type::ObjCObject:
2082 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2083 case Type::ObjCInterface:
2084 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2085 case Type::Builtin:
2086 return CreateType(cast<BuiltinType>(Ty));
2087 case Type::Complex:
2088 return CreateType(cast<ComplexType>(Ty));
2089 case Type::Pointer:
2090 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner8a365022013-06-24 17:51:48 +00002091 case Type::Decayed:
2092 // Decayed types are just pointers in LLVM and DWARF.
2093 return CreateType(
2094 cast<PointerType>(cast<DecayedType>(Ty)->getDecayedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002095 case Type::BlockPointer:
2096 return CreateType(cast<BlockPointerType>(Ty), Unit);
2097 case Type::Typedef:
David Blaikiee36464c2013-06-05 05:32:23 +00002098 return CreateType(cast<TypedefType>(Ty), Unit, Declaration);
Guy Benyei11169dd2012-12-18 14:30:41 +00002099 case Type::Record:
David Blaikiee36464c2013-06-05 05:32:23 +00002100 return CreateType(cast<RecordType>(Ty), Declaration);
Guy Benyei11169dd2012-12-18 14:30:41 +00002101 case Type::Enum:
2102 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
2103 case Type::FunctionProto:
2104 case Type::FunctionNoProto:
2105 return CreateType(cast<FunctionType>(Ty), Unit);
2106 case Type::ConstantArray:
2107 case Type::VariableArray:
2108 case Type::IncompleteArray:
2109 return CreateType(cast<ArrayType>(Ty), Unit);
2110
2111 case Type::LValueReference:
2112 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2113 case Type::RValueReference:
2114 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2115
2116 case Type::MemberPointer:
2117 return CreateType(cast<MemberPointerType>(Ty), Unit);
2118
2119 case Type::Atomic:
2120 return CreateType(cast<AtomicType>(Ty), Unit);
2121
2122 case Type::Attributed:
2123 case Type::TemplateSpecialization:
2124 case Type::Elaborated:
2125 case Type::Paren:
2126 case Type::SubstTemplateTypeParm:
2127 case Type::TypeOfExpr:
2128 case Type::TypeOf:
2129 case Type::Decltype:
2130 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002131 case Type::PackExpansion:
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 llvm_unreachable("type should have been unwrapped!");
David Blaikie22c460a02013-05-24 21:24:35 +00002133 case Type::Auto:
2134 Diag = "auto";
2135 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002137
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 assert(Diag && "Fall through without a diagnostic?");
2139 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2140 "debug information for %0 is not yet supported");
2141 CGM.getDiags().Report(DiagID)
2142 << Diag;
2143 return llvm::DIType();
2144}
2145
2146/// getOrCreateLimitedType - Get the type from the cache or create a new
2147/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002148llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002149 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002150 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002151
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002152 llvm::DIType T = getTypeOrNull(QTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002153
2154 // We may have cached a forward decl when we could have created
2155 // a non-forward decl. Go ahead and create a non-forward decl
2156 // now.
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002157 if (T && !T.isForwardDecl()) return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002158
2159 // Otherwise create the type.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002160 llvm::DIType Res = CreateLimitedType(Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002161
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002162 if (T && T.isForwardDecl())
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002163 ReplaceMap.push_back(
2164 std::make_pair(QTy.getAsOpaquePtr(), static_cast<llvm::Value *>(T)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002165
2166 // And update the type cache.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002167 TypeCache[QTy.getAsOpaquePtr()] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 return Res;
2169}
2170
2171// TODO: Currently used for context chains when limiting debug info.
2172llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
2173 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002174
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 // Get overall information about the record type for the debug info.
2176 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2177 unsigned Line = getLineNumber(RD->getLocation());
2178 StringRef RDName = getClassName(RD);
2179
2180 llvm::DIDescriptor RDContext;
Eric Christopher75e17682013-05-16 00:45:23 +00002181 if (DebugKind == CodeGenOptions::LimitedDebugInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
2183 else
2184 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
2185
2186 // If this is just a forward declaration, construct an appropriately
2187 // marked node and just return it.
2188 if (!RD->getDefinition())
David Blaikied816e2d2013-08-15 18:59:40 +00002189 return getOrCreateRecordFwdDecl(RD, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002190
2191 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2192 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002193 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002194
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 if (RD->isUnion())
2196 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002197 Size, Align, 0, llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 else if (RD->isClass()) {
2199 // FIXME: This could be a struct type giving a default visibility different
2200 // than C++ class type, but needs llvm metadata changes first.
2201 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002202 Size, Align, 0, 0, llvm::DIType(),
2203 llvm::DIArray(), llvm::DIType(),
2204 llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 } else
2206 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
Eric Christopher0fdcb312013-05-16 00:52:20 +00002207 Size, Align, 0, llvm::DIType(),
2208 llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002209
2210 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie49ae6a72013-03-26 23:47:35 +00002211 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002212
David Blaikieadfbf992013-08-18 16:55:33 +00002213 if (const ClassTemplateSpecializationDecl *TSpecial =
2214 dyn_cast<ClassTemplateSpecializationDecl>(RD))
2215 RealDecl.setTypeArray(llvm::DIArray(),
2216 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002217 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002218}
2219
David Blaikieadfbf992013-08-18 16:55:33 +00002220void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2221 llvm::DICompositeType RealDecl) {
2222 // A class's primary base or the class itself contains the vtable.
2223 llvm::DICompositeType ContainingType;
2224 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2225 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
2226 // Seek non virtual primary base root.
2227 while (1) {
2228 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2229 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2230 if (PBT && !BRL.isPrimaryBaseVirtual())
2231 PBase = PBT;
2232 else
2233 break;
2234 }
2235 ContainingType = llvm::DICompositeType(
2236 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2237 getOrCreateFile(RD->getLocation())));
2238 } else if (RD->isDynamicClass())
2239 ContainingType = RealDecl;
2240
2241 RealDecl.setContainingType(ContainingType);
2242}
2243
Guy Benyei11169dd2012-12-18 14:30:41 +00002244/// CreateMemberType - Create new member and increase Offset by FType's size.
2245llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
2246 StringRef Name,
2247 uint64_t *Offset) {
2248 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2249 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2250 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
2251 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
2252 FieldSize, FieldAlign,
2253 *Offset, 0, FieldTy);
2254 *Offset += FieldSize;
2255 return Ty;
2256}
2257
David Blaikiebd483762013-05-20 04:58:53 +00002258llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
2259 // We only need a declaration (not a definition) of the type - so use whatever
2260 // we would otherwise do to get a type for a pointee. (forward declarations in
2261 // limited debug info, full definitions (if the type definition is available)
2262 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002263 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2264 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
2265 getOrCreateFile(TD->getLocation()), true);
David Blaikiebd483762013-05-20 04:58:53 +00002266 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2267 // This doesn't handle providing declarations (for functions or variables) for
2268 // entities without definitions in this TU, nor when the definition proceeds
2269 // the call to this function.
2270 // FIXME: This should be split out into more specific maps with support for
2271 // emitting forward declarations and merging definitions with declarations,
2272 // the same way as we do for types.
2273 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2274 DeclCache.find(D->getCanonicalDecl());
2275 if (I == DeclCache.end())
2276 return llvm::DIDescriptor();
2277 llvm::Value *V = I->second;
2278 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
2279}
2280
Guy Benyei11169dd2012-12-18 14:30:41 +00002281/// getFunctionDeclaration - Return debug info descriptor to describe method
2282/// declaration for the given method definition.
2283llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
David Blaikie18cfbc52013-06-22 00:09:36 +00002284 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2285 return llvm::DISubprogram();
2286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2288 if (!FD) return llvm::DISubprogram();
2289
2290 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002291 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002292
2293 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2294 MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002295 if (MI == SPCache.end()) {
David Blaikie7d5d7c72013-08-17 00:06:55 +00002296 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
David Blaikiefd07c602013-08-09 17:20:05 +00002297 llvm::DICompositeType T(S);
2298 llvm::DISubprogram SP = CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
2299 T.addMember(SP);
2300 return SP;
2301 }
2302 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 if (MI != SPCache.end()) {
2304 llvm::Value *V = MI->second;
2305 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002306 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 return SP;
2308 }
2309
2310 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
2311 E = FD->redecls_end(); I != E; ++I) {
2312 const FunctionDecl *NextFD = *I;
2313 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2314 MI = SPCache.find(NextFD->getCanonicalDecl());
2315 if (MI != SPCache.end()) {
2316 llvm::Value *V = MI->second;
2317 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002318 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002319 return SP;
2320 }
2321 }
2322 return llvm::DISubprogram();
2323}
2324
2325// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2326// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002327llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2328 QualType FnType,
2329 llvm::DIFile F) {
David Blaikie18cfbc52013-06-22 00:09:36 +00002330 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2331 // Create fake but valid subroutine type. Otherwise
2332 // llvm::DISubprogram::Verify() would return false, and
2333 // subprogram DIE will miss DW_AT_decl_file and
2334 // DW_AT_decl_line fields.
2335 return DBuilder.createSubroutineType(F, DBuilder.getOrCreateArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002336
2337 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2338 return getOrCreateMethodType(Method, F);
2339 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2340 // Add "self" and "_cmd"
2341 SmallVector<llvm::Value *, 16> Elts;
2342
2343 // First element is always return type. For 'void' functions it is NULL.
Adrian Prantl5f360102013-05-22 21:37:49 +00002344 QualType ResultTy = OMethod->getResultType();
2345
2346 // Replace the instancetype keyword with the actual type.
2347 if (ResultTy == CGM.getContext().getObjCInstanceType())
2348 ResultTy = CGM.getContext().getPointerType(
2349 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
2350
Adrian Prantl7bec9032013-05-10 21:08:31 +00002351 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002352 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002353 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2354 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2355 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002356 // "_cmd" pointer is always second argument.
2357 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2358 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2359 // Get rest of the arguments.
Eric Christopherb2a008c2013-05-16 00:45:12 +00002360 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 PE = OMethod->param_end(); PI != PE; ++PI)
2362 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2363
2364 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2365 return DBuilder.createSubroutineType(F, EltTypeArray);
2366 }
David Blaikie469f0792013-05-22 23:22:42 +00002367 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002368}
2369
2370/// EmitFunctionStart - Constructs the debug code for entering a function.
2371void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
2372 llvm::Function *Fn,
2373 CGBuilderTy &Builder) {
2374
2375 StringRef Name;
2376 StringRef LinkageName;
2377
2378 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2379
2380 const Decl *D = GD.getDecl();
2381 // Function may lack declaration in source code if it is created by Clang
2382 // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
2383 bool HasDecl = (D != 0);
2384 // Use the location of the declaration.
2385 SourceLocation Loc;
2386 if (HasDecl)
2387 Loc = D->getLocation();
2388
2389 unsigned Flags = 0;
2390 llvm::DIFile Unit = getOrCreateFile(Loc);
2391 llvm::DIDescriptor FDContext(Unit);
2392 llvm::DIArray TParamsArray;
2393 if (!HasDecl) {
2394 // Use llvm function name.
2395 Name = Fn->getName();
2396 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2397 // If there is a DISubprogram for this function available then use it.
2398 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2399 FI = SPCache.find(FD->getCanonicalDecl());
2400 if (FI != SPCache.end()) {
2401 llvm::Value *V = FI->second;
2402 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2403 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2404 llvm::MDNode *SPN = SP;
2405 LexicalBlockStack.push_back(SPN);
2406 RegionMap[D] = llvm::WeakVH(SP);
2407 return;
2408 }
2409 }
2410 Name = getFunctionName(FD);
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002411 // Use mangled name as linkage name for C/C++ functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00002412 if (FD->hasPrototype()) {
2413 LinkageName = CGM.getMangledName(GD);
2414 Flags |= llvm::DIDescriptor::FlagPrototyped;
2415 }
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002416 // No need to replicate the linkage name if it isn't different from the
2417 // subprogram name, no need to have it at all unless coverage is enabled or
2418 // debug is set to more than just line tables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 if (LinkageName == Name ||
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002420 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2421 !CGM.getCodeGenOpts().EmitGcovNotes &&
Eric Christopher75e17682013-05-16 00:45:23 +00002422 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
Guy Benyei11169dd2012-12-18 14:30:41 +00002423 LinkageName = StringRef();
2424
Eric Christopher75e17682013-05-16 00:45:23 +00002425 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 if (const NamespaceDecl *NSDecl =
2427 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2428 FDContext = getOrCreateNameSpace(NSDecl);
2429 else if (const RecordDecl *RDecl =
2430 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2431 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
2432
2433 // Collect template parameters.
2434 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2435 }
2436 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2437 Name = getObjCMethodName(OMD);
2438 Flags |= llvm::DIDescriptor::FlagPrototyped;
2439 } else {
2440 // Use llvm function name.
2441 Name = Fn->getName();
2442 Flags |= llvm::DIDescriptor::FlagPrototyped;
2443 }
2444 if (!Name.empty() && Name[0] == '\01')
2445 Name = Name.substr(1);
2446
2447 unsigned LineNo = getLineNumber(Loc);
2448 if (!HasDecl || D->isImplicit())
2449 Flags |= llvm::DIDescriptor::FlagArtificial;
2450
David Blaikie18cfbc52013-06-22 00:09:36 +00002451 llvm::DISubprogram SP = DBuilder.createFunction(
2452 FDContext, Name, LinkageName, Unit, LineNo,
2453 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2454 true /*definition*/, getLineNumber(CurLoc), Flags,
2455 CGM.getLangOpts().Optimize, Fn, TParamsArray, getFunctionDeclaration(D));
David Blaikiebd483762013-05-20 04:58:53 +00002456 if (HasDecl)
2457 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002458
2459 // Push function on region stack.
2460 llvm::MDNode *SPN = SP;
2461 LexicalBlockStack.push_back(SPN);
2462 if (HasDecl)
2463 RegionMap[D] = llvm::WeakVH(SP);
2464}
2465
2466/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002467/// information in the source file. If the location is invalid, the
2468/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002469void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
2470 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002471 // Update our current location
2472 setLocation(Loc);
2473
2474 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
2475
2476 // Don't bother if things are the same as last time.
2477 SourceManager &SM = CGM.getContext().getSourceManager();
2478 if (CurLoc == PrevLoc ||
2479 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2480 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002481 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2482 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
2483 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002485
Guy Benyei11169dd2012-12-18 14:30:41 +00002486 // Update last state.
2487 PrevLoc = CurLoc;
2488
2489 llvm::MDNode *Scope = LexicalBlockStack.back();
Adrian Prantlc7822422013-03-12 20:43:25 +00002490 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get
2491 (getLineNumber(CurLoc),
2492 getColumnNumber(CurLoc, ForceColumnInfo),
2493 Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002494}
2495
2496/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2497/// the stack.
2498void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
2499 llvm::DIDescriptor D =
2500 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
2501 llvm::DIDescriptor() :
2502 llvm::DIDescriptor(LexicalBlockStack.back()),
2503 getOrCreateFile(CurLoc),
2504 getLineNumber(CurLoc),
2505 getColumnNumber(CurLoc));
2506 llvm::MDNode *DN = D;
2507 LexicalBlockStack.push_back(DN);
2508}
2509
2510/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2511/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002512void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2513 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 // Set our current location.
2515 setLocation(Loc);
2516
2517 // Create a new lexical block and push it on the stack.
2518 CreateLexicalBlock(Loc);
2519
2520 // Emit a line table change for the current location inside the new scope.
2521 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
2522 getColumnNumber(Loc),
2523 LexicalBlockStack.back()));
2524}
2525
2526/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2527/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002528void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2529 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002530 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2531
2532 // Provide an entry in the line table for the end of the block.
2533 EmitLocation(Builder, Loc);
2534
2535 LexicalBlockStack.pop_back();
2536}
2537
2538/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2539void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2540 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2541 unsigned RCount = FnBeginRegionCount.back();
2542 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2543
2544 // Pop all regions for this function.
2545 while (LexicalBlockStack.size() != RCount)
2546 EmitLexicalBlockEnd(Builder, CurLoc);
2547 FnBeginRegionCount.pop_back();
2548}
2549
Eric Christopherb2a008c2013-05-16 00:45:12 +00002550// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002551// See BuildByRefType.
2552llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2553 uint64_t *XOffset) {
2554
2555 SmallVector<llvm::Value *, 5> EltTys;
2556 QualType FType;
2557 uint64_t FieldSize, FieldOffset;
2558 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002559
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002561 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
2563 FieldOffset = 0;
2564 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2565 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2566 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2567 FType = CGM.getContext().IntTy;
2568 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2569 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2570
2571 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2572 if (HasCopyAndDispose) {
2573 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2574 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2575 &FieldOffset));
2576 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2577 &FieldOffset));
2578 }
2579 bool HasByrefExtendedLayout;
2580 Qualifiers::ObjCLifetime Lifetime;
2581 if (CGM.getContext().getByrefLifetime(Type,
2582 Lifetime, HasByrefExtendedLayout)
Adrian Prantlead2ba42013-07-23 00:12:14 +00002583 && HasByrefExtendedLayout) {
2584 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 EltTys.push_back(CreateMemberType(Unit, FType,
2586 "__byref_variable_layout",
2587 &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002588 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002589
Guy Benyei11169dd2012-12-18 14:30:41 +00002590 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2591 if (Align > CGM.getContext().toCharUnitsFromBits(
John McCallc8e01702013-04-16 22:48:15 +00002592 CGM.getTarget().getPointerAlign(0))) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002593 CharUnits FieldOffsetInBytes
Guy Benyei11169dd2012-12-18 14:30:41 +00002594 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2595 CharUnits AlignedOffsetInBytes
2596 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2597 CharUnits NumPaddingBytes
2598 = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002599
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 if (NumPaddingBytes.isPositive()) {
2601 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2602 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2603 pad, ArrayType::Normal, 0);
2604 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2605 }
2606 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002607
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 FType = Type;
2609 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2610 FieldSize = CGM.getContext().getTypeSize(FType);
2611 FieldAlign = CGM.getContext().toBits(Align);
2612
Eric Christopherb2a008c2013-05-16 00:45:12 +00002613 *XOffset = FieldOffset;
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
2615 0, FieldSize, FieldAlign,
2616 FieldOffset, 0, FieldTy);
2617 EltTys.push_back(FieldTy);
2618 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002619
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002621
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002623
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002625 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002626}
2627
2628/// EmitDeclare - Emit local variable declaration debug info.
2629void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +00002630 llvm::Value *Storage,
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002632 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2634
2635 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2636 llvm::DIType Ty;
2637 uint64_t XOffset = 0;
2638 if (VD->hasAttr<BlocksAttr>())
2639 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002640 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 Ty = getOrCreateType(VD->getType(), Unit);
2642
2643 // If there is no debug info for this type then do not emit debug info
2644 // for this variable.
2645 if (!Ty)
2646 return;
2647
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 // Get location information.
2649 unsigned Line = getLineNumber(VD->getLocation());
2650 unsigned Column = getColumnNumber(VD->getLocation());
2651 unsigned Flags = 0;
2652 if (VD->isImplicit())
2653 Flags |= llvm::DIDescriptor::FlagArtificial;
2654 // If this is the first argument and it is implicit then
2655 // give it an object pointer flag.
2656 // FIXME: There has to be a better way to do this, but for static
2657 // functions there won't be an implicit param at arg1 and
2658 // otherwise it is 'self' or 'this'.
2659 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2660 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002661 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002662 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2663 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002664 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002665
2666 llvm::MDNode *Scope = LexicalBlockStack.back();
2667
2668 StringRef Name = VD->getName();
2669 if (!Name.empty()) {
2670 if (VD->hasAttr<BlocksAttr>()) {
2671 CharUnits offset = CharUnits::fromQuantity(32);
2672 SmallVector<llvm::Value *, 9> addr;
2673 llvm::Type *Int64Ty = CGM.Int64Ty;
2674 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2675 // offset of __forwarding field
2676 offset = CGM.getContext().toCharUnitsFromBits(
John McCallc8e01702013-04-16 22:48:15 +00002677 CGM.getTarget().getPointerWidth(0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002678 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2679 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2680 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2681 // offset of x field
2682 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2683 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2684
2685 // Create the descriptor for the variable.
2686 llvm::DIVariable D =
Eric Christopherb2a008c2013-05-16 00:45:12 +00002687 DBuilder.createComplexVariable(Tag,
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 llvm::DIDescriptor(Scope),
2689 VD->getName(), Unit, Line, Ty,
2690 addr, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002691
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 // Insert an llvm.dbg.declare into the current block.
2693 llvm::Instruction *Call =
2694 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2695 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2696 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002698 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2699 // If VD is an anonymous union then Storage represents value for
2700 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002702 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 for (RecordDecl::field_iterator I = RD->field_begin(),
2704 E = RD->field_end();
2705 I != E; ++I) {
2706 FieldDecl *Field = *I;
2707 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2708 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002709
Guy Benyei11169dd2012-12-18 14:30:41 +00002710 // Ignore unnamed fields. Do not ignore unnamed records.
2711 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2712 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002713
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 // Use VarDecl's Tag, Scope and Line number.
2715 llvm::DIVariable D =
2716 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Eric Christopherb2a008c2013-05-16 00:45:12 +00002717 FieldName, Unit, Line, FieldTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 CGM.getLangOpts().Optimize, Flags,
2719 ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002720
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 // Insert an llvm.dbg.declare into the current block.
2722 llvm::Instruction *Call =
2723 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2724 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2725 }
David Blaikie219c7d92013-01-05 20:03:07 +00002726 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002727 }
2728 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002729
2730 // Create the descriptor for the variable.
2731 llvm::DIVariable D =
2732 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2733 Name, Unit, Line, Ty,
2734 CGM.getLangOpts().Optimize, Flags, ArgNo);
2735
2736 // Insert an llvm.dbg.declare into the current block.
2737 llvm::Instruction *Call =
2738 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2739 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002740}
2741
2742void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2743 llvm::Value *Storage,
2744 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002745 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2747}
2748
Adrian Prantlde17db32013-03-29 19:20:29 +00002749/// Look up the completed type for a self pointer in the TypeCache and
2750/// create a copy of it with the ObjectPointer and Artificial flags
2751/// set. If the type is not cached, a new one is created. This should
2752/// never happen though, since creating a type for the implicit self
2753/// argument implies that we already parsed the interface definition
2754/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002755llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2756 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002757 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002758 if (CachedTy) Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002759 else DEBUG(llvm::dbgs() << "No cached type for self.");
2760 return DBuilder.createObjectPointerType(Ty);
2761}
2762
Guy Benyei11169dd2012-12-18 14:30:41 +00002763void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2764 llvm::Value *Storage,
2765 CGBuilderTy &Builder,
2766 const CGBlockInfo &blockInfo) {
Eric Christopher75e17682013-05-16 00:45:23 +00002767 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002769
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 if (Builder.GetInsertBlock() == 0)
2771 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002772
Guy Benyei11169dd2012-12-18 14:30:41 +00002773 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 uint64_t XOffset = 0;
2776 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2777 llvm::DIType Ty;
2778 if (isByRef)
2779 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002780 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 Ty = getOrCreateType(VD->getType(), Unit);
2782
2783 // Self is passed along as an implicit non-arg variable in a
2784 // block. Mark it as the object pointer.
2785 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002786 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002787
2788 // Get location information.
2789 unsigned Line = getLineNumber(VD->getLocation());
2790 unsigned Column = getColumnNumber(VD->getLocation());
2791
2792 const llvm::DataLayout &target = CGM.getDataLayout();
2793
2794 CharUnits offset = CharUnits::fromQuantity(
2795 target.getStructLayout(blockInfo.StructureType)
2796 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2797
2798 SmallVector<llvm::Value *, 9> addr;
2799 llvm::Type *Int64Ty = CGM.Int64Ty;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002800 if (isa<llvm::AllocaInst>(Storage))
2801 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
Guy Benyei11169dd2012-12-18 14:30:41 +00002802 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2803 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2804 if (isByRef) {
2805 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2806 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2807 // offset of __forwarding field
2808 offset = CGM.getContext()
2809 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
2810 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2811 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2812 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2813 // offset of x field
2814 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2815 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2816 }
2817
2818 // Create the descriptor for the variable.
2819 llvm::DIVariable D =
Eric Christopherb2a008c2013-05-16 00:45:12 +00002820 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 llvm::DIDescriptor(LexicalBlockStack.back()),
2822 VD->getName(), Unit, Line, Ty, addr);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002823
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 // Insert an llvm.dbg.declare into the current block.
2825 llvm::Instruction *Call =
2826 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
2827 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2828 LexicalBlockStack.back()));
2829}
2830
2831/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2832/// variable declaration.
2833void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2834 unsigned ArgNo,
2835 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002836 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2838}
2839
2840namespace {
2841 struct BlockLayoutChunk {
2842 uint64_t OffsetInBits;
2843 const BlockDecl::Capture *Capture;
2844 };
2845 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2846 return l.OffsetInBits < r.OffsetInBits;
2847 }
2848}
2849
2850void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002851 llvm::Value *Arg,
2852 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002854 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 ASTContext &C = CGM.getContext();
2856 const BlockDecl *blockDecl = block.getBlockDecl();
2857
2858 // Collect some general information about the block's location.
2859 SourceLocation loc = blockDecl->getCaretLocation();
2860 llvm::DIFile tunit = getOrCreateFile(loc);
2861 unsigned line = getLineNumber(loc);
2862 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002863
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 // Build the debug-info type for the block literal.
2865 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2866
2867 const llvm::StructLayout *blockLayout =
2868 CGM.getDataLayout().getStructLayout(block.StructureType);
2869
2870 SmallVector<llvm::Value*, 16> fields;
2871 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2872 blockLayout->getElementOffsetInBits(0),
2873 tunit, tunit));
2874 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2875 blockLayout->getElementOffsetInBits(1),
2876 tunit, tunit));
2877 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2878 blockLayout->getElementOffsetInBits(2),
2879 tunit, tunit));
2880 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2881 blockLayout->getElementOffsetInBits(3),
2882 tunit, tunit));
2883 fields.push_back(createFieldType("__descriptor",
2884 C.getPointerType(block.NeedsCopyDispose ?
2885 C.getBlockDescriptorExtendedType() :
2886 C.getBlockDescriptorType()),
2887 0, loc, AS_public,
2888 blockLayout->getElementOffsetInBits(4),
2889 tunit, tunit));
2890
2891 // We want to sort the captures by offset, not because DWARF
2892 // requires this, but because we're paranoid about debuggers.
2893 SmallVector<BlockLayoutChunk, 8> chunks;
2894
2895 // 'this' capture.
2896 if (blockDecl->capturesCXXThis()) {
2897 BlockLayoutChunk chunk;
2898 chunk.OffsetInBits =
2899 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2900 chunk.Capture = 0;
2901 chunks.push_back(chunk);
2902 }
2903
2904 // Variable captures.
2905 for (BlockDecl::capture_const_iterator
2906 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2907 i != e; ++i) {
2908 const BlockDecl::Capture &capture = *i;
2909 const VarDecl *variable = capture.getVariable();
2910 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2911
2912 // Ignore constant captures.
2913 if (captureInfo.isConstant())
2914 continue;
2915
2916 BlockLayoutChunk chunk;
2917 chunk.OffsetInBits =
2918 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2919 chunk.Capture = &capture;
2920 chunks.push_back(chunk);
2921 }
2922
2923 // Sort by offset.
2924 llvm::array_pod_sort(chunks.begin(), chunks.end());
2925
2926 for (SmallVectorImpl<BlockLayoutChunk>::iterator
2927 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2928 uint64_t offsetInBits = i->OffsetInBits;
2929 const BlockDecl::Capture *capture = i->Capture;
2930
2931 // If we have a null capture, this must be the C++ 'this' capture.
2932 if (!capture) {
2933 const CXXMethodDecl *method =
2934 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2935 QualType type = method->getThisType(C);
2936
2937 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
2938 offsetInBits, tunit, tunit));
2939 continue;
2940 }
2941
2942 const VarDecl *variable = capture->getVariable();
2943 StringRef name = variable->getName();
2944
2945 llvm::DIType fieldType;
2946 if (capture->isByRef()) {
2947 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2948
2949 // FIXME: this creates a second copy of this type!
2950 uint64_t xoffset;
2951 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2952 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
2953 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
2954 ptrInfo.first, ptrInfo.second,
2955 offsetInBits, 0, fieldType);
2956 } else {
2957 fieldType = createFieldType(name, variable->getType(), 0,
2958 loc, AS_public, offsetInBits, tunit, tunit);
2959 }
2960 fields.push_back(fieldType);
2961 }
2962
2963 SmallString<36> typeName;
2964 llvm::raw_svector_ostream(typeName)
2965 << "__block_literal_" << CGM.getUniqueBlockCount();
2966
2967 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
2968
2969 llvm::DIType type =
2970 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2971 CGM.getContext().toBits(block.BlockSize),
2972 CGM.getContext().toBits(block.BlockAlign),
David Blaikie6d4fe152013-02-25 01:07:08 +00002973 0, llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2975
2976 // Get overall information about the block.
2977 unsigned flags = llvm::DIDescriptor::FlagArtificial;
2978 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00002979
2980 // Create the descriptor for the parameter.
2981 llvm::DIVariable debugVar =
2982 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
Eric Christopherb2a008c2013-05-16 00:45:12 +00002983 llvm::DIDescriptor(scope),
Adrian Prantl51936dd2013-03-14 17:53:33 +00002984 Arg->getName(), tunit, line, type,
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 CGM.getLangOpts().Optimize, flags,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002986 cast<llvm::Argument>(Arg)->getArgNo() + 1);
2987
Adrian Prantl616bef42013-03-14 21:52:59 +00002988 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00002989 // Insert an llvm.dbg.value into the current block.
Adrian Prantl616bef42013-03-14 21:52:59 +00002990 llvm::Instruction *DbgVal =
2991 DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar,
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002992 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00002993 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2994 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00002995
Adrian Prantl616bef42013-03-14 21:52:59 +00002996 // Insert an llvm.dbg.declare into the current block.
2997 llvm::Instruction *DbgDecl =
2998 DBuilder.insertDeclare(Arg, debugVar, Builder.GetInsertBlock());
2999 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003000}
3001
David Blaikie7d5d7c72013-08-17 00:06:55 +00003002/// getStaticDataMemberDeclaration - If D is an out-of-class definition of
3003/// a static data member of a class, find its corresponding in-class
3004/// declaration.
3005llvm::DIDerivedType CGDebugInfo::getStaticDataMemberDeclaration(const VarDecl *D) {
3006 if (D->isStaticDataMember()) {
3007 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
3008 MI = StaticDataMemberCache.find(D->getCanonicalDecl());
3009 if (MI != StaticDataMemberCache.end())
3010 // Verify the info still exists.
3011 if (llvm::Value *V = MI->second)
3012 return llvm::DIDerivedType(cast<llvm::MDNode>(V));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003013 }
David Blaikie7d5d7c72013-08-17 00:06:55 +00003014 return llvm::DIDerivedType();
Eric Christopher91a31902013-01-16 01:22:32 +00003015}
3016
Guy Benyei11169dd2012-12-18 14:30:41 +00003017/// EmitGlobalVariable - Emit information about a global variable.
3018void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3019 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003020 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003021 // Create global variable debug descriptor.
3022 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
3023 unsigned LineNo = getLineNumber(D->getLocation());
3024
3025 setLocation(D->getLocation());
3026
3027 QualType T = D->getType();
3028 if (T->isIncompleteArrayType()) {
3029
3030 // CodeGen turns int[] into int[1] so we'll do the same here.
3031 llvm::APInt ConstVal(32, 1);
3032 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3033
3034 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3035 ArrayType::Normal, 0);
3036 }
3037 StringRef DeclName = D->getName();
3038 StringRef LinkageName;
3039 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
3040 && !isa<ObjCMethodDecl>(D->getDeclContext()))
3041 LinkageName = Var->getName();
3042 if (LinkageName == DeclName)
3043 LinkageName = StringRef();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003044 llvm::DIDescriptor DContext =
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
David Blaikie7d5d7c72013-08-17 00:06:55 +00003046 llvm::DIGlobalVariable GV =
3047 DBuilder.createStaticVariable(DContext, DeclName, LinkageName, Unit,
3048 LineNo, getOrCreateType(T, Unit),
3049 Var->hasInternalLinkage(), Var,
3050 getStaticDataMemberDeclaration(D));
David Blaikiebd483762013-05-20 04:58:53 +00003051 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003052}
3053
3054/// EmitGlobalVariable - Emit information about an objective-c interface.
3055void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3056 ObjCInterfaceDecl *ID) {
Eric Christopher75e17682013-05-16 00:45:23 +00003057 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 // Create global variable debug descriptor.
3059 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
3060 unsigned LineNo = getLineNumber(ID->getLocation());
3061
3062 StringRef Name = ID->getName();
3063
3064 QualType T = CGM.getContext().getObjCInterfaceType(ID);
3065 if (T->isIncompleteArrayType()) {
3066
3067 // CodeGen turns int[] into int[1] so we'll do the same here.
3068 llvm::APInt ConstVal(32, 1);
3069 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3070
3071 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3072 ArrayType::Normal, 0);
3073 }
3074
3075 DBuilder.createGlobalVariable(Name, Unit, LineNo,
3076 getOrCreateType(T, Unit),
3077 Var->hasInternalLinkage(), Var);
3078}
3079
3080/// EmitGlobalVariable - Emit global variable's debug info.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003081void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003083 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 // Create the descriptor for the variable.
3085 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3086 StringRef Name = VD->getName();
3087 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3088 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3089 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3090 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3091 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3092 }
3093 // Do not use DIGlobalVariable for enums.
3094 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3095 return;
David Blaikiea0ef9fa2013-08-15 21:42:43 +00003096 llvm::DIGlobalVariable GV = DBuilder.createStaticVariable(
3097 Unit, Name, Name, Unit, getLineNumber(VD->getLocation()), Ty, true, Init,
David Blaikie7d5d7c72013-08-17 00:06:55 +00003098 getStaticDataMemberDeclaration(cast<VarDecl>(VD)));
David Blaikiebd483762013-05-20 04:58:53 +00003099 DeclCache.insert(std::make_pair(VD->getCanonicalDecl(), llvm::WeakVH(GV)));
3100}
3101
3102llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3103 if (!LexicalBlockStack.empty())
3104 return llvm::DIScope(LexicalBlockStack.back());
3105 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003106}
3107
David Blaikie9f88fe82013-04-22 06:13:21 +00003108void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003109 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3110 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003111 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003112 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3113 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003114 getLineNumber(UD.getLocation()));
3115}
3116
David Blaikiebd483762013-05-20 04:58:53 +00003117void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3118 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3119 return;
3120 assert(UD.shadow_size() &&
3121 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3122 // Emitting one decl is sufficient - debuggers can detect that this is an
3123 // overloaded name & provide lookup for all the overloads.
3124 const UsingShadowDecl &USD = **UD.shadow_begin();
Eric Christopher1ecc5632013-06-07 22:54:39 +00003125 if (llvm::DIDescriptor Target =
3126 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003127 DBuilder.createImportedDeclaration(
3128 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3129 getLineNumber(USD.getLocation()));
3130}
3131
David Blaikief121b932013-05-20 22:50:41 +00003132llvm::DIImportedEntity
3133CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3134 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3135 return llvm::DIImportedEntity(0);
3136 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3137 if (VH)
3138 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
3139 llvm::DIImportedEntity R(0);
3140 if (const NamespaceAliasDecl *Underlying =
3141 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3142 // This could cache & dedup here rather than relying on metadata deduping.
3143 R = DBuilder.createImportedModule(
3144 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3145 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3146 NA.getName());
3147 else
3148 R = DBuilder.createImportedModule(
3149 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3150 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3151 getLineNumber(NA.getLocation()), NA.getName());
3152 VH = R;
3153 return R;
3154}
3155
Guy Benyei11169dd2012-12-18 14:30:41 +00003156/// getOrCreateNamesSpace - Return namespace descriptor for the given
3157/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003158llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003159CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003160 NSDecl = NSDecl->getCanonicalDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003161 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei11169dd2012-12-18 14:30:41 +00003162 NameSpaceCache.find(NSDecl);
3163 if (I != NameSpaceCache.end())
3164 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003165
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3167 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003168 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3170 llvm::DINameSpace NS =
3171 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3172 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3173 return NS;
3174}
3175
3176void CGDebugInfo::finalize() {
3177 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
3178 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
3179 llvm::DIType Ty, RepTy;
3180 // Verify that the debug info still exists.
3181 if (llvm::Value *V = VI->second)
3182 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003183
Guy Benyei11169dd2012-12-18 14:30:41 +00003184 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
3185 TypeCache.find(VI->first);
3186 if (it != TypeCache.end()) {
3187 // Verify that the debug info still exists.
3188 if (llvm::Value *V = it->second)
3189 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
3190 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003191
Eric Christopherf8bc4d82013-07-18 00:52:50 +00003192 if (Ty && Ty.isForwardDecl() && RepTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00003193 Ty.replaceAllUsesWith(RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003194 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003195
3196 // We keep our own list of retained types, because we need to look
3197 // up the final type in the type cache.
3198 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3199 RE = RetainedTypes.end(); RI != RE; ++RI)
3200 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
3201
Guy Benyei11169dd2012-12-18 14:30:41 +00003202 DBuilder.finalize();
3203}