blob: 0e3b98d90e451cf1ab5d80e6cc38b97e28d665ca [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie9dfd2432013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
40using namespace clang;
41using namespace clang::CodeGen;
42
43CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher688cf5b2013-07-14 21:12:44 +000044 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
45 DBuilder(CGM.getModule()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000046 CreateCompileUnit();
47}
48
49CGDebugInfo::~CGDebugInfo() {
50 assert(LexicalBlockStack.empty() &&
51 "Region stack mismatch, stack not empty!");
52}
53
Adrian Prantled6bbe42013-07-18 00:28:02 +000054
55NoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B)
56 : DI(CGF.getDebugInfo()), Builder(B) {
57 if (DI) {
58 SavedLoc = DI->getLocation();
59 DI->CurLoc = SourceLocation();
60 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
61 }
62}
63
64NoLocation::~NoLocation() {
65 if (DI) {
66 assert(Builder.getCurrentDebugLocation().isUnknown());
67 DI->CurLoc = SavedLoc;
68 }
69}
70
Adrian Prantlb061ce22013-07-18 01:36:04 +000071ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Adrian Prantled6bbe42013-07-18 00:28:02 +000072 : DI(CGF.getDebugInfo()), Builder(B) {
73 if (DI) {
74 SavedLoc = DI->getLocation();
Adrian Prantlb6cdc962013-07-24 20:34:39 +000075 DI->CurLoc = SourceLocation();
76 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
77 }
78}
79
80void ArtificialLocation::Emit() {
81 if (DI) {
Adrian Prantled6bbe42013-07-18 00:28:02 +000082 // Sync the Builder.
83 DI->EmitLocation(Builder, SavedLoc);
84 DI->CurLoc = SourceLocation();
85 // Construct a location that has a valid scope, but no line info.
Adrian Prantlb6cdc962013-07-24 20:34:39 +000086 assert(!DI->LexicalBlockStack.empty());
87 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
Adrian Prantled6bbe42013-07-18 00:28:02 +000088 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
89 }
90}
91
Adrian Prantlb061ce22013-07-18 01:36:04 +000092ArtificialLocation::~ArtificialLocation() {
Adrian Prantled6bbe42013-07-18 00:28:02 +000093 if (DI) {
94 assert(Builder.getCurrentDebugLocation().getLine() == 0);
95 DI->CurLoc = SavedLoc;
96 }
97}
98
Guy Benyei7f92f2d2012-12-18 14:30:41 +000099void CGDebugInfo::setLocation(SourceLocation Loc) {
100 // If the new location isn't valid return.
Adrian Prantl5f4554f2013-07-18 00:27:56 +0000101 if (Loc.isInvalid()) return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000102
103 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
104
105 // If we've changed files in the middle of a lexical scope go ahead
106 // and create a new lexical scope with file node if it's different
107 // from the one in the scope.
108 if (LexicalBlockStack.empty()) return;
109
110 SourceManager &SM = CGM.getContext().getSourceManager();
111 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
112 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
113
114 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
115 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
116 return;
117
118 llvm::MDNode *LB = LexicalBlockStack.back();
119 llvm::DIScope Scope = llvm::DIScope(LB);
120 if (Scope.isLexicalBlockFile()) {
121 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB);
122 llvm::DIDescriptor D
123 = DBuilder.createLexicalBlockFile(LBF.getScope(),
124 getOrCreateFile(CurLoc));
125 llvm::MDNode *N = D;
126 LexicalBlockStack.pop_back();
127 LexicalBlockStack.push_back(N);
David Blaikiea6504852013-01-26 22:16:26 +0000128 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000129 llvm::DIDescriptor D
130 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
131 llvm::MDNode *N = D;
132 LexicalBlockStack.pop_back();
133 LexicalBlockStack.push_back(N);
134 }
135}
136
137/// getContextDescriptor - Get context info for the decl.
David Blaikiebb000792013-04-19 06:56:38 +0000138llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000139 if (!Context)
140 return TheCU;
141
142 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
143 I = RegionMap.find(Context);
144 if (I != RegionMap.end()) {
145 llvm::Value *V = I->second;
David Blaikiebb000792013-04-19 06:56:38 +0000146 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000147 }
148
149 // Check namespace.
150 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebb000792013-04-19 06:56:38 +0000151 return getOrCreateNameSpace(NSDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000152
David Blaikiebb000792013-04-19 06:56:38 +0000153 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
154 if (!RDecl->isDependentType())
155 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000156 getOrCreateMainFile());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000157 return TheCU;
158}
159
160/// getFunctionName - Get function name for the given FunctionDecl. If the
161/// name is constructred on demand (e.g. C++ destructor) then the name
162/// is stored on the side.
163StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
164 assert (FD && "Invalid FunctionDecl!");
165 IdentifierInfo *FII = FD->getIdentifier();
166 FunctionTemplateSpecializationInfo *Info
167 = FD->getTemplateSpecializationInfo();
168 if (!Info && FII)
169 return FII->getName();
170
171 // Otherwise construct human readable name for debug info.
Benjamin Kramer5eada842013-02-22 15:46:01 +0000172 SmallString<128> NS;
173 llvm::raw_svector_ostream OS(NS);
174 FD->printName(OS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000175
176 // Add any template specialization args.
177 if (Info) {
178 const TemplateArgumentList *TArgs = Info->TemplateArguments;
179 const TemplateArgument *Args = TArgs->data();
180 unsigned NumArgs = TArgs->size();
181 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer5eada842013-02-22 15:46:01 +0000182 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
183 Policy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000184 }
185
186 // Copy this name on the side and use its reference.
Benjamin Kramer5eada842013-02-22 15:46:01 +0000187 OS.flush();
188 char *StrPtr = DebugInfoNames.Allocate<char>(NS.size());
189 memcpy(StrPtr, NS.data(), NS.size());
190 return StringRef(StrPtr, NS.size());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000191}
192
193StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
194 SmallString<256> MethodName;
195 llvm::raw_svector_ostream OS(MethodName);
196 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
197 const DeclContext *DC = OMD->getDeclContext();
Eric Christopher6537f082013-05-16 00:45:12 +0000198 if (const ObjCImplementationDecl *OID =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000199 dyn_cast<const ObjCImplementationDecl>(DC)) {
200 OS << OID->getName();
Eric Christopher6537f082013-05-16 00:45:12 +0000201 } else if (const ObjCInterfaceDecl *OID =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000202 dyn_cast<const ObjCInterfaceDecl>(DC)) {
203 OS << OID->getName();
Eric Christopher6537f082013-05-16 00:45:12 +0000204 } else if (const ObjCCategoryImplDecl *OCD =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000205 dyn_cast<const ObjCCategoryImplDecl>(DC)){
206 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
207 OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb5092242013-05-17 23:58:45 +0000208 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl687ecae2013-05-17 23:49:10 +0000209 // We can extract the type of the class from the self pointer.
210 if (ImplicitParamDecl* SelfDecl = OMD->getSelfDecl()) {
211 QualType ClassTy =
212 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
213 ClassTy.print(OS, PrintingPolicy(LangOptions()));
214 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000215 }
216 OS << ' ' << OMD->getSelector().getAsString() << ']';
217
218 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
219 memcpy(StrPtr, MethodName.begin(), OS.tell());
220 return StringRef(StrPtr, OS.tell());
221}
222
223/// getSelectorName - Return selector name. This is used for debugging
224/// info.
225StringRef CGDebugInfo::getSelectorName(Selector S) {
226 const std::string &SName = S.getAsString();
227 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
228 memcpy(StrPtr, SName.data(), SName.size());
229 return StringRef(StrPtr, SName.size());
230}
231
232/// getClassName - Get class name including template argument list.
Eric Christopher6537f082013-05-16 00:45:12 +0000233StringRef
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000234CGDebugInfo::getClassName(const RecordDecl *RD) {
235 const ClassTemplateSpecializationDecl *Spec
236 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
237 if (!Spec)
238 return RD->getName();
239
240 const TemplateArgument *Args;
241 unsigned NumArgs;
242 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
243 const TemplateSpecializationType *TST =
244 cast<TemplateSpecializationType>(TAW->getType());
245 Args = TST->getArgs();
246 NumArgs = TST->getNumArgs();
247 } else {
248 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
249 Args = TemplateArgs.data();
250 NumArgs = TemplateArgs.size();
251 }
252 StringRef Name = RD->getIdentifier()->getName();
253 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer5eada842013-02-22 15:46:01 +0000254 SmallString<128> TemplateArgList;
255 {
256 llvm::raw_svector_ostream OS(TemplateArgList);
257 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
258 Policy);
259 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000260
261 // Copy this name on the side and use its reference.
262 size_t Length = Name.size() + TemplateArgList.size();
263 char *StrPtr = DebugInfoNames.Allocate<char>(Length);
264 memcpy(StrPtr, Name.data(), Name.size());
265 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size());
266 return StringRef(StrPtr, Length);
267}
268
269/// getOrCreateFile - Get the file debug info descriptor for the input location.
270llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
271 if (!Loc.isValid())
272 // If Location is not valid then use main input file.
273 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
274
275 SourceManager &SM = CGM.getContext().getSourceManager();
276 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
277
278 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
279 // If the location is not valid then use main input file.
280 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
281
282 // Cache the results.
283 const char *fname = PLoc.getFilename();
284 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
285 DIFileCache.find(fname);
286
287 if (it != DIFileCache.end()) {
288 // Verify that the information still exists.
289 if (llvm::Value *V = it->second)
290 return llvm::DIFile(cast<llvm::MDNode>(V));
291 }
292
293 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
294
295 DIFileCache[fname] = F;
296 return F;
297}
298
299/// getOrCreateMainFile - Get the file info for main compile unit.
300llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
301 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
302}
303
304/// getLineNumber - Get line number for the location. If location is invalid
305/// then use current location.
306unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
307 if (Loc.isInvalid() && CurLoc.isInvalid())
308 return 0;
309 SourceManager &SM = CGM.getContext().getSourceManager();
310 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
311 return PLoc.isValid()? PLoc.getLine() : 0;
312}
313
314/// getColumnNumber - Get column number for the location.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000315unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000316 // We may not want column information at all.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000317 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000318 return 0;
319
320 // If the location is invalid then use the current column.
321 if (Loc.isInvalid() && CurLoc.isInvalid())
322 return 0;
323 SourceManager &SM = CGM.getContext().getSourceManager();
324 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
325 return PLoc.isValid()? PLoc.getColumn() : 0;
326}
327
328StringRef CGDebugInfo::getCurrentDirname() {
329 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
330 return CGM.getCodeGenOpts().DebugCompilationDir;
331
332 if (!CWDName.empty())
333 return CWDName;
334 SmallString<256> CWD;
335 llvm::sys::fs::current_path(CWD);
336 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
337 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
338 return CWDName = StringRef(CompDirnamePtr, CWD.size());
339}
340
341/// CreateCompileUnit - Create new compile unit.
342void CGDebugInfo::CreateCompileUnit() {
343
344 // Get absolute path name.
345 SourceManager &SM = CGM.getContext().getSourceManager();
346 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
347 if (MainFileName.empty())
348 MainFileName = "<unknown>";
349
350 // The main file name provided via the "-main-file-name" option contains just
351 // the file name itself with no path information. This file name may have had
352 // a relative path, so we look into the actual file entry for the main
353 // file to determine the real absolute path for the file.
354 std::string MainFileDir;
355 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
356 MainFileDir = MainFile->getDir()->getName();
357 if (MainFileDir != ".")
358 MainFileName = MainFileDir + "/" + MainFileName;
359 }
360
361 // Save filename string.
362 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
363 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
364 StringRef Filename(FilenamePtr, MainFileName.length());
Eric Christopherff971d72013-02-22 23:50:16 +0000365
366 // Save split dwarf file string.
367 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
368 char *SplitDwarfPtr = DebugInfoNames.Allocate<char>(SplitDwarfFile.length());
369 memcpy(SplitDwarfPtr, SplitDwarfFile.c_str(), SplitDwarfFile.length());
370 StringRef SplitDwarfFilename(SplitDwarfPtr, SplitDwarfFile.length());
Eric Christopher6537f082013-05-16 00:45:12 +0000371
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000372 unsigned LangTag;
373 const LangOptions &LO = CGM.getLangOpts();
374 if (LO.CPlusPlus) {
375 if (LO.ObjC1)
376 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
377 else
378 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
379 } else if (LO.ObjC1) {
380 LangTag = llvm::dwarf::DW_LANG_ObjC;
381 } else if (LO.C99) {
382 LangTag = llvm::dwarf::DW_LANG_C99;
383 } else {
384 LangTag = llvm::dwarf::DW_LANG_C89;
385 }
386
387 std::string Producer = getClangFullVersion();
388
389 // Figure out which version of the ObjC runtime we have.
390 unsigned RuntimeVers = 0;
391 if (LO.ObjC1)
392 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
393
394 // Create new compile unit.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000395 // FIXME - Eliminate TheCU.
Eric Christopher8fed3f42013-07-19 00:51:58 +0000396 TheCU = DBuilder.createCompileUnit(LangTag, Filename, getCurrentDirname(),
397 Producer, LO.Optimize,
398 CGM.getCodeGenOpts().DwarfDebugFlags,
399 RuntimeVers, SplitDwarfFilename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000400}
401
402/// CreateType - Get the Basic type from the cache or create a new
403/// one if necessary.
404llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
405 unsigned Encoding = 0;
406 StringRef BTName;
407 switch (BT->getKind()) {
408#define BUILTIN_TYPE(Id, SingletonId)
409#define PLACEHOLDER_TYPE(Id, SingletonId) \
410 case BuiltinType::Id:
411#include "clang/AST/BuiltinTypes.def"
412 case BuiltinType::Dependent:
413 llvm_unreachable("Unexpected builtin type");
414 case BuiltinType::NullPtr:
Peter Collingbourne24118f52013-06-27 22:51:01 +0000415 return DBuilder.createNullPtrType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000416 case BuiltinType::Void:
417 return llvm::DIType();
418 case BuiltinType::ObjCClass:
Eric Christopherb2d13922013-07-18 00:52:50 +0000419 if (ClassTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000420 return ClassTy;
421 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
422 "objc_class", TheCU,
423 getOrCreateMainFile(), 0);
424 return ClassTy;
425 case BuiltinType::ObjCId: {
426 // typedef struct objc_class *Class;
427 // typedef struct objc_object {
428 // Class isa;
429 // } *id;
430
Eric Christopherb2d13922013-07-18 00:52:50 +0000431 if (ObjTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000432 return ObjTy;
433
Eric Christopherb2d13922013-07-18 00:52:50 +0000434 if (!ClassTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000435 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
436 "objc_class", TheCU,
437 getOrCreateMainFile(), 0);
438
439 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopher6537f082013-05-16 00:45:12 +0000440
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000441 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
442
Eric Christopherf068c922013-04-02 22:59:11 +0000443 ObjTy =
David Blaikiec1d0af12013-02-25 01:07:08 +0000444 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
445 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000446
Eric Christopherf068c922013-04-02 22:59:11 +0000447 ObjTy.setTypeArray(DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
448 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000449 return ObjTy;
450 }
451 case BuiltinType::ObjCSel: {
Eric Christopherb2d13922013-07-18 00:52:50 +0000452 if (SelTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000453 return SelTy;
454 SelTy =
455 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
456 "objc_selector", TheCU, getOrCreateMainFile(),
457 0);
458 return SelTy;
459 }
Guy Benyeib13621d2012-12-18 14:38:23 +0000460
461 case BuiltinType::OCLImage1d:
462 return getOrCreateStructPtrType("opencl_image1d_t",
463 OCLImage1dDITy);
464 case BuiltinType::OCLImage1dArray:
Eric Christopher6537f082013-05-16 00:45:12 +0000465 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeib13621d2012-12-18 14:38:23 +0000466 OCLImage1dArrayDITy);
467 case BuiltinType::OCLImage1dBuffer:
468 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
469 OCLImage1dBufferDITy);
470 case BuiltinType::OCLImage2d:
471 return getOrCreateStructPtrType("opencl_image2d_t",
472 OCLImage2dDITy);
473 case BuiltinType::OCLImage2dArray:
474 return getOrCreateStructPtrType("opencl_image2d_array_t",
475 OCLImage2dArrayDITy);
476 case BuiltinType::OCLImage3d:
477 return getOrCreateStructPtrType("opencl_image3d_t",
478 OCLImage3dDITy);
Guy Benyei21f18c42013-02-07 10:55:47 +0000479 case BuiltinType::OCLSampler:
480 return DBuilder.createBasicType("opencl_sampler_t",
481 CGM.getContext().getTypeSize(BT),
482 CGM.getContext().getTypeAlign(BT),
483 llvm::dwarf::DW_ATE_unsigned);
Guy Benyeie6b9d802013-01-20 12:31:11 +0000484 case BuiltinType::OCLEvent:
485 return getOrCreateStructPtrType("opencl_event_t",
486 OCLEventDITy);
Guy Benyeib13621d2012-12-18 14:38:23 +0000487
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000488 case BuiltinType::UChar:
489 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
490 case BuiltinType::Char_S:
491 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
492 case BuiltinType::Char16:
493 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
494 case BuiltinType::UShort:
495 case BuiltinType::UInt:
496 case BuiltinType::UInt128:
497 case BuiltinType::ULong:
498 case BuiltinType::WChar_U:
499 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
500 case BuiltinType::Short:
501 case BuiltinType::Int:
502 case BuiltinType::Int128:
503 case BuiltinType::Long:
504 case BuiltinType::WChar_S:
505 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
506 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
507 case BuiltinType::Half:
508 case BuiltinType::Float:
509 case BuiltinType::LongDouble:
510 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
511 }
512
513 switch (BT->getKind()) {
514 case BuiltinType::Long: BTName = "long int"; break;
515 case BuiltinType::LongLong: BTName = "long long int"; break;
516 case BuiltinType::ULong: BTName = "long unsigned int"; break;
517 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
518 default:
519 BTName = BT->getName(CGM.getLangOpts());
520 break;
521 }
522 // Bit size, align and offset of the type.
523 uint64_t Size = CGM.getContext().getTypeSize(BT);
524 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christopher6537f082013-05-16 00:45:12 +0000525 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000526 DBuilder.createBasicType(BTName, Size, Align, Encoding);
527 return DbgTy;
528}
529
530llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
531 // Bit size, align and offset of the type.
532 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
533 if (Ty->isComplexIntegerType())
534 Encoding = llvm::dwarf::DW_ATE_lo_user;
535
536 uint64_t Size = CGM.getContext().getTypeSize(Ty);
537 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopher6537f082013-05-16 00:45:12 +0000538 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000539 DBuilder.createBasicType("complex", Size, Align, Encoding);
540
541 return DbgTy;
542}
543
544/// CreateCVRType - Get the qualified type from the cache or create
545/// a new one if necessary.
Eric Christopher56b108a2013-06-07 22:54:39 +0000546llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit,
547 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000548 QualifierCollector Qc;
549 const Type *T = Qc.strip(Ty);
550
551 // Ignore these qualifiers for now.
552 Qc.removeObjCGCAttr();
553 Qc.removeAddressSpace();
554 Qc.removeObjCLifetime();
555
556 // We will create one Derived type for one qualifier and recurse to handle any
557 // additional ones.
558 unsigned Tag;
559 if (Qc.hasConst()) {
560 Tag = llvm::dwarf::DW_TAG_const_type;
561 Qc.removeConst();
562 } else if (Qc.hasVolatile()) {
563 Tag = llvm::dwarf::DW_TAG_volatile_type;
564 Qc.removeVolatile();
565 } else if (Qc.hasRestrict()) {
566 Tag = llvm::dwarf::DW_TAG_restrict_type;
567 Qc.removeRestrict();
568 } else {
569 assert(Qc.empty() && "Unknown type qualifier for debug info");
570 return getOrCreateType(QualType(T, 0), Unit);
571 }
572
Eric Christopher56b108a2013-06-07 22:54:39 +0000573 llvm::DIType FromTy =
574 getOrCreateType(Qc.apply(CGM.getContext(), T), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000575
576 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
577 // CVR derived types.
578 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopher6537f082013-05-16 00:45:12 +0000579
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000580 return DbgTy;
581}
582
583llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
584 llvm::DIFile Unit) {
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000585
586 // The frontend treats 'id' as a typedef to an ObjCObjectType,
587 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
588 // debug info, we want to emit 'id' in both cases.
589 if (Ty->isObjCQualifiedIdType())
590 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
591
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000592 llvm::DIType DbgTy =
Eric Christopher6537f082013-05-16 00:45:12 +0000593 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000594 Ty->getPointeeType(), Unit);
595 return DbgTy;
596}
597
598llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
599 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +0000600 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000601 Ty->getPointeeType(), Unit);
602}
603
604// Creates a forward declaration for a RecordDecl in the given context.
605llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
606 llvm::DIDescriptor Ctx) {
607 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
608 unsigned Line = getLineNumber(RD->getLocation());
609 StringRef RDName = getClassName(RD);
610
611 unsigned Tag = 0;
612 if (RD->isStruct() || RD->isInterface())
613 Tag = llvm::dwarf::DW_TAG_structure_type;
614 else if (RD->isUnion())
615 Tag = llvm::dwarf::DW_TAG_union_type;
616 else {
617 assert(RD->isClass());
618 Tag = llvm::dwarf::DW_TAG_class_type;
619 }
620
621 // Create the type.
622 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
623}
624
625// Walk up the context chain and create forward decls for record decls,
626// and normal descriptors for namespaces.
627llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
628 if (!Context)
629 return TheCU;
630
631 // See if we already have the parent.
632 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
633 I = RegionMap.find(Context);
634 if (I != RegionMap.end()) {
635 llvm::Value *V = I->second;
636 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
637 }
Eric Christopher6537f082013-05-16 00:45:12 +0000638
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000639 // Check namespace.
640 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
641 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
642
643 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
644 if (!RD->isDependentType()) {
Eric Christopherf0890c42013-05-16 00:52:20 +0000645 llvm::DIType Ty =
646 getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
647 getOrCreateMainFile());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000648 return llvm::DIDescriptor(Ty);
649 }
650 }
651 return TheCU;
652}
653
David Blaikieb0f77b02013-05-24 21:33:22 +0000654/// getOrCreateTypeDeclaration - Create Pointee type. If Pointee is a record
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000655/// then emit record's fwd if debug info size reduction is enabled.
David Blaikieb0f77b02013-05-24 21:33:22 +0000656llvm::DIType CGDebugInfo::getOrCreateTypeDeclaration(QualType PointeeTy,
657 llvm::DIFile Unit) {
David Blaikie9faebd22013-05-20 04:58:53 +0000658 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000659 return getOrCreateType(PointeeTy, Unit);
David Blaikie5f6e2f42013-06-05 05:32:23 +0000660 return getOrCreateType(PointeeTy, Unit, true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000661}
662
663llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
Eric Christopher6537f082013-05-16 00:45:12 +0000664 const Type *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000665 QualType PointeeTy,
666 llvm::DIFile Unit) {
667 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
668 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikieb0f77b02013-05-24 21:33:22 +0000669 return DBuilder.createReferenceType(
670 Tag, getOrCreateTypeDeclaration(PointeeTy, Unit));
Fariborz Jahanian05f8ff12013-02-21 20:42:11 +0000671
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000672 // Bit size, align and offset of the type.
673 // Size is always the size of a pointer. We can't use getTypeSize here
674 // because that does not return the correct value for references.
675 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +0000676 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000677 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
678
David Blaikieb0f77b02013-05-24 21:33:22 +0000679 return DBuilder.createPointerType(getOrCreateTypeDeclaration(PointeeTy, Unit),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000680 Size, Align);
681}
682
Eric Christopherf0890c42013-05-16 00:52:20 +0000683llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
684 llvm::DIType &Cache) {
Eric Christopherb2d13922013-07-18 00:52:50 +0000685 if (Cache)
Guy Benyeib13621d2012-12-18 14:38:23 +0000686 return Cache;
David Blaikie1e97c1e2013-05-21 17:58:54 +0000687 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
688 TheCU, getOrCreateMainFile(), 0);
689 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
690 Cache = DBuilder.createPointerType(Cache, Size);
691 return Cache;
Guy Benyeib13621d2012-12-18 14:38:23 +0000692}
693
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000694llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
695 llvm::DIFile Unit) {
Eric Christopherb2d13922013-07-18 00:52:50 +0000696 if (BlockLiteralGeneric)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000697 return BlockLiteralGeneric;
698
699 SmallVector<llvm::Value *, 8> EltTys;
700 llvm::DIType FieldTy;
701 QualType FType;
702 uint64_t FieldSize, FieldOffset;
703 unsigned FieldAlign;
704 llvm::DIArray Elements;
705 llvm::DIType EltTy, DescTy;
706
707 FieldOffset = 0;
708 FType = CGM.getContext().UnsignedLongTy;
709 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
710 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
711
712 Elements = DBuilder.getOrCreateArray(EltTys);
713 EltTys.clear();
714
715 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
716 unsigned LineNo = getLineNumber(CurLoc);
717
718 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
719 Unit, LineNo, FieldOffset, 0,
David Blaikiec1d0af12013-02-25 01:07:08 +0000720 Flags, llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000721
722 // Bit size, align and offset of the type.
723 uint64_t Size = CGM.getContext().getTypeSize(Ty);
724
725 DescTy = DBuilder.createPointerType(EltTy, Size);
726
727 FieldOffset = 0;
728 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
729 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
730 FType = CGM.getContext().IntTy;
731 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
732 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
733 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
734 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
735
736 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
737 FieldTy = DescTy;
738 FieldSize = CGM.getContext().getTypeSize(Ty);
739 FieldAlign = CGM.getContext().getTypeAlign(Ty);
740 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
741 LineNo, FieldSize, FieldAlign,
742 FieldOffset, 0, FieldTy);
743 EltTys.push_back(FieldTy);
744
745 FieldOffset += FieldSize;
746 Elements = DBuilder.getOrCreateArray(EltTys);
747
748 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
749 Unit, LineNo, FieldOffset, 0,
David Blaikiec1d0af12013-02-25 01:07:08 +0000750 Flags, llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000751
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000752 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
753 return BlockLiteralGeneric;
754}
755
David Blaikie5f6e2f42013-06-05 05:32:23 +0000756llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit,
757 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000758 // Typedefs are derived from some other type. If we have a typedef of a
759 // typedef, make sure to emit the whole chain.
David Blaikieb0f77b02013-05-24 21:33:22 +0000760 llvm::DIType Src =
David Blaikie5f6e2f42013-06-05 05:32:23 +0000761 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit, Declaration);
Eric Christopherb2d13922013-07-18 00:52:50 +0000762 if (!Src)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000763 return llvm::DIType();
764 // We don't set size information, but do specify where the typedef was
765 // declared.
766 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
767 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher6537f082013-05-16 00:45:12 +0000768
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000769 llvm::DIDescriptor TypedefContext =
770 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher6537f082013-05-16 00:45:12 +0000771
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000772 return
773 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
774}
775
776llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
777 llvm::DIFile Unit) {
778 SmallVector<llvm::Value *, 16> EltTys;
779
780 // Add the result type at least.
781 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
782
783 // Set up remainder of arguments if there is a prototype.
784 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
785 if (isa<FunctionNoProtoType>(Ty))
786 EltTys.push_back(DBuilder.createUnspecifiedParameter());
787 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
788 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
789 EltTys.push_back(getOrCreateType(FPT->getArgType(i), Unit));
790 }
791
792 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
793 return DBuilder.createSubroutineType(Unit, EltTypeArray);
794}
795
796
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000797llvm::DIType CGDebugInfo::createFieldType(StringRef name,
798 QualType type,
799 uint64_t sizeInBitsOverride,
800 SourceLocation loc,
801 AccessSpecifier AS,
802 uint64_t offsetInBits,
803 llvm::DIFile tunit,
804 llvm::DIDescriptor scope) {
805 llvm::DIType debugType = getOrCreateType(type, tunit);
806
807 // Get the location for the field.
808 llvm::DIFile file = getOrCreateFile(loc);
809 unsigned line = getLineNumber(loc);
810
811 uint64_t sizeInBits = 0;
812 unsigned alignInBits = 0;
813 if (!type->isIncompleteArrayType()) {
814 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
815
816 if (sizeInBitsOverride)
817 sizeInBits = sizeInBitsOverride;
818 }
819
820 unsigned flags = 0;
821 if (AS == clang::AS_private)
822 flags |= llvm::DIDescriptor::FlagPrivate;
823 else if (AS == clang::AS_protected)
824 flags |= llvm::DIDescriptor::FlagProtected;
825
826 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
827 alignInBits, offsetInBits, flags, debugType);
828}
829
Eric Christopher0395de32013-01-16 01:22:32 +0000830/// CollectRecordLambdaFields - Helper for CollectRecordFields.
831void CGDebugInfo::
832CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
833 SmallVectorImpl<llvm::Value *> &elements,
834 llvm::DIType RecordTy) {
835 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
836 // has the name and the location of the variable so we should iterate over
837 // both concurrently.
838 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
839 RecordDecl::field_iterator Field = CXXDecl->field_begin();
840 unsigned fieldno = 0;
841 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
842 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
843 const LambdaExpr::Capture C = *I;
844 if (C.capturesVariable()) {
845 VarDecl *V = C.getCapturedVar();
846 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
847 StringRef VName = V->getName();
848 uint64_t SizeInBitsOverride = 0;
849 if (Field->isBitField()) {
850 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
851 assert(SizeInBitsOverride && "found named 0-width bitfield");
852 }
853 llvm::DIType fieldType
854 = createFieldType(VName, Field->getType(), SizeInBitsOverride,
855 C.getLocation(), Field->getAccess(),
856 layout.getFieldOffset(fieldno), VUnit, RecordTy);
857 elements.push_back(fieldType);
858 } else {
859 // TODO: Need to handle 'this' in some way by probably renaming the
860 // this of the lambda class and having a field member of 'this' or
861 // by using AT_object_pointer for the function and having that be
862 // used as 'this' for semantic references.
863 assert(C.capturesThis() && "Field that isn't captured and isn't this?");
864 FieldDecl *f = *Field;
865 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
866 QualType type = f->getType();
867 llvm::DIType fieldType
868 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
869 layout.getFieldOffset(fieldno), VUnit, RecordTy);
870
871 elements.push_back(fieldType);
872 }
873 }
874}
875
876/// CollectRecordStaticField - Helper for CollectRecordFields.
877void CGDebugInfo::
878CollectRecordStaticField(const VarDecl *Var,
879 SmallVectorImpl<llvm::Value *> &elements,
880 llvm::DIType RecordTy) {
881 // Create the descriptor for the static variable, with or without
882 // constant initializers.
883 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
884 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
885
886 // Do not describe enums as static members.
887 if (VTy.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
888 return;
889
890 unsigned LineNumber = getLineNumber(Var->getLocation());
891 StringRef VName = Var->getName();
David Blaikiea89701b2013-01-20 01:19:17 +0000892 llvm::Constant *C = NULL;
Eric Christopher0395de32013-01-16 01:22:32 +0000893 if (Var->getInit()) {
894 const APValue *Value = Var->evaluateValue();
David Blaikiea89701b2013-01-20 01:19:17 +0000895 if (Value) {
896 if (Value->isInt())
897 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
898 if (Value->isFloat())
899 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
900 }
Eric Christopher0395de32013-01-16 01:22:32 +0000901 }
902
903 unsigned Flags = 0;
904 AccessSpecifier Access = Var->getAccess();
905 if (Access == clang::AS_private)
906 Flags |= llvm::DIDescriptor::FlagPrivate;
907 else if (Access == clang::AS_protected)
908 Flags |= llvm::DIDescriptor::FlagProtected;
909
910 llvm::DIType GV = DBuilder.createStaticMemberType(RecordTy, VName, VUnit,
David Blaikiea89701b2013-01-20 01:19:17 +0000911 LineNumber, VTy, Flags, C);
Eric Christopher0395de32013-01-16 01:22:32 +0000912 elements.push_back(GV);
913 StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV);
914}
915
916/// CollectRecordNormalField - Helper for CollectRecordFields.
917void CGDebugInfo::
918CollectRecordNormalField(const FieldDecl *field, uint64_t OffsetInBits,
919 llvm::DIFile tunit,
920 SmallVectorImpl<llvm::Value *> &elements,
921 llvm::DIType RecordTy) {
922 StringRef name = field->getName();
923 QualType type = field->getType();
924
925 // Ignore unnamed fields unless they're anonymous structs/unions.
926 if (name.empty() && !type->isRecordType())
927 return;
928
929 uint64_t SizeInBitsOverride = 0;
930 if (field->isBitField()) {
931 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
932 assert(SizeInBitsOverride && "found named 0-width bitfield");
933 }
934
935 llvm::DIType fieldType
936 = createFieldType(name, type, SizeInBitsOverride,
937 field->getLocation(), field->getAccess(),
938 OffsetInBits, tunit, RecordTy);
939
940 elements.push_back(fieldType);
941}
942
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000943/// CollectRecordFields - A helper function to collect debug info for
944/// record fields. This is used while creating debug info entry for a Record.
945void CGDebugInfo::
946CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
947 SmallVectorImpl<llvm::Value *> &elements,
948 llvm::DIType RecordTy) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000949 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
950
Eric Christopher0395de32013-01-16 01:22:32 +0000951 if (CXXDecl && CXXDecl->isLambda())
952 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
953 else {
954 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000955
Eric Christopher0395de32013-01-16 01:22:32 +0000956 // Field number for non-static fields.
Eric Christopherfd5ac0d2013-01-04 17:59:07 +0000957 unsigned fieldNo = 0;
Eric Christopher0395de32013-01-16 01:22:32 +0000958
Eric Christopher0395de32013-01-16 01:22:32 +0000959 // Static and non-static members should appear in the same order as
960 // the corresponding declarations in the source program.
961 for (RecordDecl::decl_iterator I = record->decls_begin(),
962 E = record->decls_end(); I != E; ++I)
963 if (const VarDecl *V = dyn_cast<VarDecl>(*I))
964 CollectRecordStaticField(V, elements, RecordTy);
965 else if (FieldDecl *field = dyn_cast<FieldDecl>(*I)) {
Eric Christopher0395de32013-01-16 01:22:32 +0000966 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo),
967 tunit, elements, RecordTy);
968
969 // Bump field number for next field.
970 ++fieldNo;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000971 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000972 }
973}
974
975/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
976/// function type is not updated to include implicit "this" pointer. Use this
977/// routine to get a method type which includes "this" pointer.
David Blaikie9a845292013-05-22 23:22:42 +0000978llvm::DICompositeType
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000979CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
980 llvm::DIFile Unit) {
David Blaikie9c78f9b2013-01-07 23:06:35 +0000981 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie67f8b5e2013-01-07 22:24:59 +0000982 if (Method->isStatic())
David Blaikie9a845292013-05-22 23:22:42 +0000983 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie9c78f9b2013-01-07 23:06:35 +0000984 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
985 Func, Unit);
986}
David Blaikie67f8b5e2013-01-07 22:24:59 +0000987
David Blaikie9a845292013-05-22 23:22:42 +0000988llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie9c78f9b2013-01-07 23:06:35 +0000989 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000990 // Add "this" pointer.
David Blaikie9c78f9b2013-01-07 23:06:35 +0000991 llvm::DIArray Args = llvm::DICompositeType(
992 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000993 assert (Args.getNumElements() && "Invalid number of arguments!");
994
995 SmallVector<llvm::Value *, 16> Elts;
996
997 // First element is always return type. For 'void' functions it is NULL.
998 Elts.push_back(Args.getElement(0));
999
David Blaikie67f8b5e2013-01-07 22:24:59 +00001000 // "this" pointer is always first argument.
David Blaikie9c78f9b2013-01-07 23:06:35 +00001001 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie67f8b5e2013-01-07 22:24:59 +00001002 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1003 // Create pointer type directly in this case.
1004 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1005 QualType PointeeTy = ThisPtrTy->getPointeeType();
1006 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCall64aa4b32013-04-16 22:48:15 +00001007 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001008 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1009 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopherf0890c42013-05-16 00:52:20 +00001010 llvm::DIType ThisPtrType =
1011 DBuilder.createPointerType(PointeeType, Size, Align);
David Blaikie67f8b5e2013-01-07 22:24:59 +00001012 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1013 // TODO: This and the artificial type below are misleading, the
1014 // types aren't artificial the argument is, but the current
1015 // metadata doesn't represent that.
1016 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1017 Elts.push_back(ThisPtrType);
1018 } else {
1019 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
1020 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1021 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1022 Elts.push_back(ThisPtrType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001023 }
1024
1025 // Copy rest of the arguments.
1026 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1027 Elts.push_back(Args.getElement(i));
1028
1029 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1030
1031 return DBuilder.createSubroutineType(Unit, EltTypeArray);
1032}
1033
Eric Christopher6537f082013-05-16 00:45:12 +00001034/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001035/// inside a function.
1036static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1037 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1038 return isFunctionLocalClass(NRD);
1039 if (isa<FunctionDecl>(RD->getDeclContext()))
1040 return true;
1041 return false;
1042}
1043
1044/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1045/// a single member function GlobalDecl.
1046llvm::DISubprogram
1047CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
1048 llvm::DIFile Unit,
1049 llvm::DIType RecordTy) {
Eric Christopher6537f082013-05-16 00:45:12 +00001050 bool IsCtorOrDtor =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001051 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopher6537f082013-05-16 00:45:12 +00001052
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001053 StringRef MethodName = getFunctionName(Method);
David Blaikie9a845292013-05-22 23:22:42 +00001054 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001055
1056 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1057 // make sense to give a single ctor/dtor a linkage name.
1058 StringRef MethodLinkageName;
1059 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1060 MethodLinkageName = CGM.getMangledName(Method);
1061
1062 // Get the location for the method.
1063 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
1064 unsigned MethodLine = getLineNumber(Method->getLocation());
1065
1066 // Collect virtual method info.
1067 llvm::DIType ContainingType;
Eric Christopher6537f082013-05-16 00:45:12 +00001068 unsigned Virtuality = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001069 unsigned VIndex = 0;
Eric Christopher6537f082013-05-16 00:45:12 +00001070
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001071 if (Method->isVirtual()) {
1072 if (Method->isPure())
1073 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1074 else
1075 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopher6537f082013-05-16 00:45:12 +00001076
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001077 // It doesn't make sense to give a virtual destructor a vtable index,
1078 // since a single destructor has two entries in the vtable.
1079 if (!isa<CXXDestructorDecl>(Method))
1080 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
1081 ContainingType = RecordTy;
1082 }
1083
1084 unsigned Flags = 0;
1085 if (Method->isImplicit())
1086 Flags |= llvm::DIDescriptor::FlagArtificial;
1087 AccessSpecifier Access = Method->getAccess();
1088 if (Access == clang::AS_private)
1089 Flags |= llvm::DIDescriptor::FlagPrivate;
1090 else if (Access == clang::AS_protected)
1091 Flags |= llvm::DIDescriptor::FlagProtected;
1092 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1093 if (CXXC->isExplicit())
1094 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopher6537f082013-05-16 00:45:12 +00001095 } else if (const CXXConversionDecl *CXXC =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001096 dyn_cast<CXXConversionDecl>(Method)) {
1097 if (CXXC->isExplicit())
1098 Flags |= llvm::DIDescriptor::FlagExplicit;
1099 }
1100 if (Method->hasPrototype())
1101 Flags |= llvm::DIDescriptor::FlagPrototyped;
1102
1103 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1104 llvm::DISubprogram SP =
Eric Christopher6537f082013-05-16 00:45:12 +00001105 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001106 MethodDefUnit, MethodLine,
Eric Christopher6537f082013-05-16 00:45:12 +00001107 MethodTy, /*isLocalToUnit=*/false,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001108 /* isDefinition=*/ false,
1109 Virtuality, VIndex, ContainingType,
1110 Flags, CGM.getLangOpts().Optimize, NULL,
1111 TParamsArray);
Eric Christopher6537f082013-05-16 00:45:12 +00001112
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001113 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
1114
1115 return SP;
1116}
1117
1118/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopher6537f082013-05-16 00:45:12 +00001119/// C++ member functions. This is used while creating debug info entry for
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001120/// a Record.
1121void CGDebugInfo::
1122CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
1123 SmallVectorImpl<llvm::Value *> &EltTys,
1124 llvm::DIType RecordTy) {
1125
1126 // Since we want more than just the individual member decls if we
1127 // have templated functions iterate over every declaration to gather
1128 // the functions.
1129 for(DeclContext::decl_iterator I = RD->decls_begin(),
1130 E = RD->decls_end(); I != E; ++I) {
1131 Decl *D = *I;
1132 if (D->isImplicit() && !D->isUsed())
1133 continue;
1134
1135 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1136 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
1137 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
1138 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(),
1139 SE = FTD->spec_end(); SI != SE; ++SI)
1140 EltTys.push_back(CreateCXXMemberFunction(cast<CXXMethodDecl>(*SI), Unit,
1141 RecordTy));
1142 }
Eric Christopher6537f082013-05-16 00:45:12 +00001143}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144
1145/// CollectCXXFriends - A helper function to collect debug info for
1146/// C++ base classes. This is used while creating debug info entry for
1147/// a Record.
1148void CGDebugInfo::
1149CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
1150 SmallVectorImpl<llvm::Value *> &EltTys,
1151 llvm::DIType RecordTy) {
1152 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
1153 BE = RD->friend_end(); BI != BE; ++BI) {
1154 if ((*BI)->isUnsupportedFriend())
1155 continue;
1156 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Eric Christopher6537f082013-05-16 00:45:12 +00001157 EltTys.push_back(DBuilder.createFriend(RecordTy,
1158 getOrCreateType(TInfo->getType(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001159 Unit)));
1160 }
1161}
1162
1163/// CollectCXXBases - A helper function to collect debug info for
Eric Christopher6537f082013-05-16 00:45:12 +00001164/// C++ base classes. This is used while creating debug info entry for
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001165/// a Record.
1166void CGDebugInfo::
1167CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
1168 SmallVectorImpl<llvm::Value *> &EltTys,
1169 llvm::DIType RecordTy) {
1170
1171 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1172 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1173 BE = RD->bases_end(); BI != BE; ++BI) {
1174 unsigned BFlags = 0;
1175 uint64_t BaseOffset;
Eric Christopher6537f082013-05-16 00:45:12 +00001176
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001177 const CXXRecordDecl *Base =
1178 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
Eric Christopher6537f082013-05-16 00:45:12 +00001179
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001180 if (BI->isVirtual()) {
1181 // virtual base offset offset is -ve. The code generator emits dwarf
1182 // expression where it expects +ve number.
Eric Christopher6537f082013-05-16 00:45:12 +00001183 BaseOffset =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001184 0 - CGM.getVTableContext()
1185 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
1186 BFlags = llvm::DIDescriptor::FlagVirtual;
1187 } else
1188 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1189 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1190 // BI->isVirtual() and bits when not.
Eric Christopher6537f082013-05-16 00:45:12 +00001191
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001192 AccessSpecifier Access = BI->getAccessSpecifier();
1193 if (Access == clang::AS_private)
1194 BFlags |= llvm::DIDescriptor::FlagPrivate;
1195 else if (Access == clang::AS_protected)
1196 BFlags |= llvm::DIDescriptor::FlagProtected;
Eric Christopher6537f082013-05-16 00:45:12 +00001197
1198 llvm::DIType DTy =
1199 DBuilder.createInheritance(RecordTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001200 getOrCreateType(BI->getType(), Unit),
1201 BaseOffset, BFlags);
1202 EltTys.push_back(DTy);
1203 }
1204}
1205
1206/// CollectTemplateParams - A helper function to collect template parameters.
1207llvm::DIArray CGDebugInfo::
1208CollectTemplateParams(const TemplateParameterList *TPList,
David Blaikie35178dc2013-06-22 18:59:18 +00001209 ArrayRef<TemplateArgument> TAList,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001210 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001211 SmallVector<llvm::Value *, 16> TemplateParams;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001212 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1213 const TemplateArgument &TA = TAList[i];
David Blaikie35178dc2013-06-22 18:59:18 +00001214 StringRef Name;
1215 if (TPList)
1216 Name = TPList->getParam(i)->getName();
David Blaikie9dfd2432013-05-10 21:53:14 +00001217 switch (TA.getKind()) {
1218 case TemplateArgument::Type: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001219 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1220 llvm::DITemplateTypeParameter TTP =
David Blaikie35178dc2013-06-22 18:59:18 +00001221 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001222 TemplateParams.push_back(TTP);
David Blaikie9dfd2432013-05-10 21:53:14 +00001223 } break;
1224 case TemplateArgument::Integral: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001225 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1226 llvm::DITemplateValueParameter TVP =
David Blaikie9dfd2432013-05-10 21:53:14 +00001227 DBuilder.createTemplateValueParameter(
David Blaikie35178dc2013-06-22 18:59:18 +00001228 TheCU, Name, TTy,
David Blaikie9dfd2432013-05-10 21:53:14 +00001229 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1230 TemplateParams.push_back(TVP);
1231 } break;
1232 case TemplateArgument::Declaration: {
1233 const ValueDecl *D = TA.getAsDecl();
1234 bool InstanceMember = D->isCXXInstanceMember();
1235 QualType T = InstanceMember
1236 ? CGM.getContext().getMemberPointerType(
1237 D->getType(), cast<RecordDecl>(D->getDeclContext())
1238 ->getTypeForDecl())
1239 : CGM.getContext().getPointerType(D->getType());
1240 llvm::DIType TTy = getOrCreateType(T, Unit);
1241 llvm::Value *V = 0;
1242 // Variable pointer template parameters have a value that is the address
1243 // of the variable.
1244 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
1245 V = CGM.GetAddrOfGlobalVar(VD);
1246 // Member function pointers have special support for building them, though
1247 // this is currently unsupported in LLVM CodeGen.
David Blaikief8aa1552013-05-13 06:57:50 +00001248 if (InstanceMember) {
David Blaikie9dfd2432013-05-10 21:53:14 +00001249 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(D))
1250 V = CGM.getCXXABI().EmitMemberPointer(method);
David Blaikief8aa1552013-05-13 06:57:50 +00001251 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1252 V = CGM.GetAddrOfFunction(FD);
David Blaikie9dfd2432013-05-10 21:53:14 +00001253 // Member data pointers have special handling too to compute the fixed
1254 // offset within the object.
1255 if (isa<FieldDecl>(D)) {
1256 // These five lines (& possibly the above member function pointer
1257 // handling) might be able to be refactored to use similar code in
1258 // CodeGenModule::getMemberPointerConstant
1259 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1260 CharUnits chars =
1261 CGM.getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1262 V = CGM.getCXXABI().EmitMemberDataPointer(
1263 cast<MemberPointerType>(T.getTypePtr()), chars);
1264 }
1265 llvm::DITemplateValueParameter TVP =
David Blaikie35178dc2013-06-22 18:59:18 +00001266 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie9dfd2432013-05-10 21:53:14 +00001267 TemplateParams.push_back(TVP);
1268 } break;
1269 case TemplateArgument::NullPtr: {
1270 QualType T = TA.getNullPtrType();
1271 llvm::DIType TTy = getOrCreateType(T, Unit);
1272 llvm::Value *V = 0;
1273 // Special case member data pointer null values since they're actually -1
1274 // instead of zero.
1275 if (const MemberPointerType *MPT =
1276 dyn_cast<MemberPointerType>(T.getTypePtr()))
1277 // But treat member function pointers as simple zero integers because
1278 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1279 // CodeGen grows handling for values of non-null member function
1280 // pointers then perhaps we could remove this special case and rely on
1281 // EmitNullMemberPointer for member function pointers.
1282 if (MPT->isMemberDataPointer())
1283 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1284 if (!V)
1285 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1286 llvm::DITemplateValueParameter TVP =
David Blaikie35178dc2013-06-22 18:59:18 +00001287 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie9dfd2432013-05-10 21:53:14 +00001288 TemplateParams.push_back(TVP);
1289 } break;
David Blaikie35178dc2013-06-22 18:59:18 +00001290 case TemplateArgument::Template: {
1291 llvm::DITemplateValueParameter TVP =
1292 DBuilder.createTemplateTemplateParameter(
1293 TheCU, Name, llvm::DIType(),
1294 TA.getAsTemplate().getAsTemplateDecl()
1295 ->getQualifiedNameAsString());
1296 TemplateParams.push_back(TVP);
1297 } break;
1298 case TemplateArgument::Pack: {
1299 llvm::DITemplateValueParameter TVP =
1300 DBuilder.createTemplateParameterPack(
1301 TheCU, Name, llvm::DIType(),
1302 CollectTemplateParams(NULL, TA.getPackAsArray(), Unit));
1303 TemplateParams.push_back(TVP);
1304 } break;
David Blaikiee8065122013-05-10 23:36:06 +00001305 // And the following should never occur:
David Blaikie9dfd2432013-05-10 21:53:14 +00001306 case TemplateArgument::Expression:
1307 case TemplateArgument::TemplateExpansion:
David Blaikie9dfd2432013-05-10 21:53:14 +00001308 case TemplateArgument::Null:
1309 llvm_unreachable(
1310 "These argument types shouldn't exist in concrete types");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001311 }
1312 }
1313 return DBuilder.getOrCreateArray(TemplateParams);
1314}
1315
1316/// CollectFunctionTemplateParams - A helper function to collect debug
1317/// info for function template parameters.
1318llvm::DIArray CGDebugInfo::
1319CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
1320 if (FD->getTemplatedKind() ==
1321 FunctionDecl::TK_FunctionTemplateSpecialization) {
1322 const TemplateParameterList *TList =
1323 FD->getTemplateSpecializationInfo()->getTemplate()
1324 ->getTemplateParameters();
David Blaikie35178dc2013-06-22 18:59:18 +00001325 return CollectTemplateParams(
1326 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001327 }
1328 return llvm::DIArray();
1329}
1330
1331/// CollectCXXTemplateParams - A helper function to collect debug info for
1332/// template parameters.
1333llvm::DIArray CGDebugInfo::
1334CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1335 llvm::DIFile Unit) {
1336 llvm::PointerUnion<ClassTemplateDecl *,
1337 ClassTemplatePartialSpecializationDecl *>
1338 PU = TSpecial->getSpecializedTemplateOrPartial();
Eric Christopher6537f082013-05-16 00:45:12 +00001339
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001340 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1341 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1342 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1343 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
David Blaikie35178dc2013-06-22 18:59:18 +00001344 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001345}
1346
1347/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1348llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1349 if (VTablePtrType.isValid())
1350 return VTablePtrType;
1351
1352 ASTContext &Context = CGM.getContext();
1353
1354 /* Function type */
1355 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
1356 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
1357 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1358 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
1359 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
1360 "__vtbl_ptr_type");
1361 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1362 return VTablePtrType;
1363}
1364
1365/// getVTableName - Get vtable name for the given Class.
1366StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
1367 // Construct gdb compatible name name.
1368 std::string Name = "_vptr$" + RD->getNameAsString();
1369
1370 // Copy this name on the side and use its reference.
1371 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
1372 memcpy(StrPtr, Name.data(), Name.length());
1373 return StringRef(StrPtr, Name.length());
1374}
1375
1376
1377/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1378/// debug info entry in EltTys vector.
1379void CGDebugInfo::
1380CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1381 SmallVectorImpl<llvm::Value *> &EltTys) {
1382 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1383
1384 // If there is a primary base then it will hold vtable info.
1385 if (RL.getPrimaryBase())
1386 return;
1387
1388 // If this class is not dynamic then there is not any vtable info to collect.
1389 if (!RD->isDynamicClass())
1390 return;
1391
1392 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1393 llvm::DIType VPTR
1394 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Eric Christopherf0890c42013-05-16 00:52:20 +00001395 0, Size, 0, 0,
1396 llvm::DIDescriptor::FlagArtificial,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397 getOrCreateVTablePtrType(Unit));
1398 EltTys.push_back(VPTR);
1399}
1400
Eric Christopher6537f082013-05-16 00:45:12 +00001401/// getOrCreateRecordType - Emit record type's standalone debug info.
1402llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001403 SourceLocation Loc) {
Eric Christopher13c97672013-05-16 00:45:23 +00001404 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1406 return T;
1407}
1408
1409/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1410/// debug info.
1411llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001412 SourceLocation Loc) {
Eric Christopher13c97672013-05-16 00:45:23 +00001413 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001414 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001415 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001416 return T;
1417}
1418
1419/// CreateType - get structure or union type.
David Blaikie5f6e2f42013-06-05 05:32:23 +00001420llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001421 RecordDecl *RD = Ty->getDecl();
Adrian Prantl776bfa12013-06-18 23:32:21 +00001422 // Limited debug info should only remove struct definitions that can
1423 // safely be replaced by a forward declaration in the source code.
David Blaikie658cd2c2013-07-13 21:08:14 +00001424 if (DebugKind <= CodeGenOptions::LimitedDebugInfo && Declaration &&
1425 !RD->isCompleteDefinitionRequired()) {
Adrian Prantl776bfa12013-06-18 23:32:21 +00001426 // FIXME: This implementation is problematic; there are some test
1427 // cases where we violate the above principle, such as
1428 // test/CodeGen/debug-info-records.c .
David Blaikie5f6e2f42013-06-05 05:32:23 +00001429 llvm::DIDescriptor FDContext =
1430 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1431 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
1432 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RetTy;
1433 return RetTy;
1434 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001435
1436 // Get overall information about the record type for the debug info.
1437 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1438
1439 // Records and classes and unions can all be recursive. To handle them, we
1440 // first generate a debug descriptor for the struct as a forward declaration.
1441 // Then (if it is a definition) we go through and get debug info for all of
1442 // its members. Finally, we create a descriptor for the complete type (which
1443 // may refer to the forward decl if the struct is recursive) and replace all
1444 // uses of the forward declaration with the final definition.
1445
Eric Christopherf068c922013-04-02 22:59:11 +00001446 llvm::DICompositeType FwdDecl(
1447 getOrCreateLimitedType(QualType(Ty, 0), DefUnit));
Manman Renb6b0a712013-07-02 19:01:53 +00001448 assert(FwdDecl.isCompositeType() &&
David Blaikie9a845292013-05-22 23:22:42 +00001449 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001450
1451 if (FwdDecl.isForwardDecl())
1452 return FwdDecl;
1453
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001454 // Push the struct on region stack.
Eric Christopherf068c922013-04-02 22:59:11 +00001455 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001456 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1457
Adrian Prantl4919de62013-03-06 22:03:30 +00001458 // Add this to the completed-type cache while we're completing it recursively.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001459 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1460
1461 // Convert all the elements.
1462 SmallVector<llvm::Value *, 16> EltTys;
1463
1464 // Note: The split of CXXDecl information here is intentional, the
1465 // gdb tests will depend on a certain ordering at printout. The debug
1466 // information offsets are still correct if we merge them all together
1467 // though.
1468 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1469 if (CXXDecl) {
1470 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1471 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1472 }
1473
Eric Christopher0395de32013-01-16 01:22:32 +00001474 // Collect data fields (including static variables and any initializers).
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001475 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001476 if (CXXDecl) {
1477 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1478 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001479 }
1480
1481 LexicalBlockStack.pop_back();
1482 RegionMap.erase(Ty->getDecl());
1483
1484 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
David Blaikie80588332013-08-01 20:31:40 +00001485 FwdDecl.setTypeArray(Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001486
Eric Christopherf068c922013-04-02 22:59:11 +00001487 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1488 return FwdDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001489}
1490
1491/// CreateType - get objective-c object type.
1492llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1493 llvm::DIFile Unit) {
1494 // Ignore protocols.
1495 return getOrCreateType(Ty->getBaseType(), Unit);
1496}
1497
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001498
1499/// \return true if Getter has the default name for the property PD.
1500static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1501 const ObjCMethodDecl *Getter) {
1502 assert(PD);
1503 if (!Getter)
1504 return true;
1505
1506 assert(Getter->getDeclName().isObjCZeroArgSelector());
1507 return PD->getName() ==
1508 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
1509}
1510
1511/// \return true if Setter has the default name for the property PD.
1512static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1513 const ObjCMethodDecl *Setter) {
1514 assert(PD);
1515 if (!Setter)
1516 return true;
1517
1518 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantl80e8ea92013-06-07 22:29:12 +00001519 return SelectorTable::constructSetterName(PD->getName()) ==
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001520 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
1521}
1522
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001523/// CreateType - get objective-c interface type.
1524llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1525 llvm::DIFile Unit) {
1526 ObjCInterfaceDecl *ID = Ty->getDecl();
1527 if (!ID)
1528 return llvm::DIType();
1529
1530 // Get overall information about the record type for the debug info.
1531 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1532 unsigned Line = getLineNumber(ID->getLocation());
1533 unsigned RuntimeLang = TheCU.getLanguage();
1534
1535 // If this is just a forward declaration return a special forward-declaration
1536 // debug type since we won't be able to lay out the entire type.
1537 ObjCInterfaceDecl *Def = ID->getDefinition();
1538 if (!Def) {
1539 llvm::DIType FwdDecl =
1540 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001541 ID->getName(), TheCU, DefUnit, Line,
1542 RuntimeLang);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001543 return FwdDecl;
1544 }
1545
1546 ID = Def;
1547
1548 // Bit size, align and offset of the type.
1549 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1550 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1551
1552 unsigned Flags = 0;
1553 if (ID->getImplementation())
1554 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1555
Eric Christopherf068c922013-04-02 22:59:11 +00001556 llvm::DICompositeType RealDecl =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001557 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1558 Line, Size, Align, Flags,
David Blaikiec1d0af12013-02-25 01:07:08 +00001559 llvm::DIType(), llvm::DIArray(), RuntimeLang);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001560
1561 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1562 // will find it and we're emitting the complete type.
Adrian Prantl4919de62013-03-06 22:03:30 +00001563 QualType QualTy = QualType(Ty, 0);
1564 CompletedTypeCache[QualTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001565
Eric Christopherd3003dc2013-07-14 21:00:07 +00001566 // Push the struct on region stack.
Eric Christopherf068c922013-04-02 22:59:11 +00001567 LexicalBlockStack.push_back(static_cast<llvm::MDNode*>(RealDecl));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001568 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1569
1570 // Convert all the elements.
1571 SmallVector<llvm::Value *, 16> EltTys;
1572
1573 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1574 if (SClass) {
1575 llvm::DIType SClassTy =
1576 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
1577 if (!SClassTy.isValid())
1578 return llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001579
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001580 llvm::DIType InhTag =
1581 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
1582 EltTys.push_back(InhTag);
1583 }
1584
Eric Christopherd3003dc2013-07-14 21:00:07 +00001585 // Create entries for all of the properties.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001586 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1587 E = ID->prop_end(); I != E; ++I) {
1588 const ObjCPropertyDecl *PD = *I;
1589 SourceLocation Loc = PD->getLocation();
1590 llvm::DIFile PUnit = getOrCreateFile(Loc);
1591 unsigned PLine = getLineNumber(Loc);
1592 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1593 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1594 llvm::MDNode *PropertyNode =
1595 DBuilder.createObjCProperty(PD->getName(),
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001596 PUnit, PLine,
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001597 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001598 getSelectorName(PD->getGetterName()),
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001599 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001600 getSelectorName(PD->getSetterName()),
1601 PD->getPropertyAttributes(),
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001602 getOrCreateType(PD->getType(), PUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001603 EltTys.push_back(PropertyNode);
1604 }
1605
1606 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1607 unsigned FieldNo = 0;
1608 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1609 Field = Field->getNextIvar(), ++FieldNo) {
1610 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1611 if (!FieldTy.isValid())
1612 return llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001613
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001614 StringRef FieldName = Field->getName();
1615
1616 // Ignore unnamed fields.
1617 if (FieldName.empty())
1618 continue;
1619
1620 // Get the location for the field.
1621 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1622 unsigned FieldLine = getLineNumber(Field->getLocation());
1623 QualType FType = Field->getType();
1624 uint64_t FieldSize = 0;
1625 unsigned FieldAlign = 0;
1626
1627 if (!FType->isIncompleteArrayType()) {
1628
1629 // Bit size, align and offset of the type.
1630 FieldSize = Field->isBitField()
Eric Christopherd3003dc2013-07-14 21:00:07 +00001631 ? Field->getBitWidthValue(CGM.getContext())
1632 : CGM.getContext().getTypeSize(FType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001633 FieldAlign = CGM.getContext().getTypeAlign(FType);
1634 }
1635
1636 uint64_t FieldOffset;
1637 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1638 // We don't know the runtime offset of an ivar if we're using the
1639 // non-fragile ABI. For bitfields, use the bit offset into the first
1640 // byte of storage of the bitfield. For other fields, use zero.
1641 if (Field->isBitField()) {
1642 FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset(
1643 CGM, ID, Field);
1644 FieldOffset %= CGM.getContext().getCharWidth();
1645 } else {
1646 FieldOffset = 0;
1647 }
1648 } else {
1649 FieldOffset = RL.getFieldOffset(FieldNo);
1650 }
1651
1652 unsigned Flags = 0;
1653 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1654 Flags = llvm::DIDescriptor::FlagProtected;
1655 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1656 Flags = llvm::DIDescriptor::FlagPrivate;
1657
1658 llvm::MDNode *PropertyNode = NULL;
1659 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopher6537f082013-05-16 00:45:12 +00001660 if (ObjCPropertyImplDecl *PImpD =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001661 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1662 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherbe5f1be2013-02-21 22:35:08 +00001663 SourceLocation Loc = PD->getLocation();
1664 llvm::DIFile PUnit = getOrCreateFile(Loc);
1665 unsigned PLine = getLineNumber(Loc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001666 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1667 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1668 PropertyNode =
1669 DBuilder.createObjCProperty(PD->getName(),
1670 PUnit, PLine,
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001671 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001672 getSelectorName(PD->getGetterName()),
Adrian Prantl5ae17a12013-06-07 01:10:45 +00001673 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001674 getSelectorName(PD->getSetterName()),
1675 PD->getPropertyAttributes(),
1676 getOrCreateType(PD->getType(), PUnit));
1677 }
1678 }
1679 }
1680 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1681 FieldLine, FieldSize, FieldAlign,
1682 FieldOffset, Flags, FieldTy,
1683 PropertyNode);
1684 EltTys.push_back(FieldTy);
1685 }
1686
1687 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherf068c922013-04-02 22:59:11 +00001688 RealDecl.setTypeArray(Elements);
Adrian Prantl4919de62013-03-06 22:03:30 +00001689
1690 // If the implementation is not yet set, we do not want to mark it
1691 // as complete. An implementation may declare additional
1692 // private ivars that we would miss otherwise.
1693 if (ID->getImplementation() == 0)
1694 CompletedTypeCache.erase(QualTy.getAsOpaquePtr());
Eric Christopher6537f082013-05-16 00:45:12 +00001695
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001696 LexicalBlockStack.pop_back();
Eric Christopherf068c922013-04-02 22:59:11 +00001697 return RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001698}
1699
1700llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1701 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1702 int64_t Count = Ty->getNumElements();
1703 if (Count == 0)
1704 // If number of elements are not known then this is an unbounded array.
1705 // Use Count == -1 to express such arrays.
1706 Count = -1;
1707
1708 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1709 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1710
1711 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1712 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1713
1714 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1715}
1716
1717llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1718 llvm::DIFile Unit) {
1719 uint64_t Size;
1720 uint64_t Align;
1721
1722 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1723 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1724 Size = 0;
1725 Align =
1726 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
1727 } else if (Ty->isIncompleteArrayType()) {
1728 Size = 0;
1729 if (Ty->getElementType()->isIncompleteType())
1730 Align = 0;
1731 else
1732 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikie089db2e2013-05-09 20:48:12 +00001733 } else if (Ty->isIncompleteType()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001734 Size = 0;
1735 Align = 0;
1736 } else {
1737 // Size and align of the whole array, not the element type.
1738 Size = CGM.getContext().getTypeSize(Ty);
1739 Align = CGM.getContext().getTypeAlign(Ty);
1740 }
1741
1742 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1743 // interior arrays, do we care? Why aren't nested arrays represented the
1744 // obvious/recursive way?
1745 SmallVector<llvm::Value *, 8> Subscripts;
1746 QualType EltTy(Ty, 0);
1747 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1748 // If the number of elements is known, then count is that number. Otherwise,
1749 // it's -1. This allows us to represent a subrange with an array of 0
1750 // elements, like this:
1751 //
1752 // struct foo {
1753 // int x[0];
1754 // };
1755 int64_t Count = -1; // Count == -1 is an unbounded array.
1756 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1757 Count = CAT->getSize().getZExtValue();
Eric Christopher6537f082013-05-16 00:45:12 +00001758
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001759 // FIXME: Verify this is right for VLAs.
1760 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1761 EltTy = Ty->getElementType();
1762 }
1763
1764 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1765
Eric Christopher6537f082013-05-16 00:45:12 +00001766 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001767 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1768 SubscriptArray);
1769 return DbgTy;
1770}
1771
Eric Christopher6537f082013-05-16 00:45:12 +00001772llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001773 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001774 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001775 Ty, Ty->getPointeeType(), Unit);
1776}
1777
Eric Christopher6537f082013-05-16 00:45:12 +00001778llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001779 llvm::DIFile Unit) {
Eric Christopher6537f082013-05-16 00:45:12 +00001780 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001781 Ty, Ty->getPointeeType(), Unit);
1782}
1783
Eric Christopher6537f082013-05-16 00:45:12 +00001784llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001785 llvm::DIFile U) {
David Blaikiee8d75142013-01-19 19:20:56 +00001786 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1787 if (!Ty->getPointeeType()->isFunctionType())
1788 return DBuilder.createMemberPointerType(
David Blaikieb0f77b02013-05-24 21:33:22 +00001789 getOrCreateTypeDeclaration(Ty->getPointeeType(), U), ClassType);
David Blaikiee8d75142013-01-19 19:20:56 +00001790 return DBuilder.createMemberPointerType(getOrCreateInstanceMethodType(
1791 CGM.getContext().getPointerType(
1792 QualType(Ty->getClass(), Ty->getPointeeType().getCVRQualifiers())),
1793 Ty->getPointeeType()->getAs<FunctionProtoType>(), U),
1794 ClassType);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001795}
1796
Eric Christopher6537f082013-05-16 00:45:12 +00001797llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001798 llvm::DIFile U) {
1799 // Ignore the atomic wrapping
1800 // FIXME: What is the correct representation?
1801 return getOrCreateType(Ty->getValueType(), U);
1802}
1803
1804/// CreateEnumType - get enumeration type.
1805llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1806 uint64_t Size = 0;
1807 uint64_t Align = 0;
1808 if (!ED->getTypeForDecl()->isIncompleteType()) {
1809 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1810 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1811 }
1812
1813 // If this is just a forward declaration, construct an appropriately
1814 // marked node and just return it.
1815 if (!ED->getDefinition()) {
1816 llvm::DIDescriptor EDContext;
1817 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1818 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1819 unsigned Line = getLineNumber(ED->getLocation());
1820 StringRef EDName = ED->getName();
1821 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_enumeration_type,
1822 EDName, EDContext, DefUnit, Line, 0,
1823 Size, Align);
1824 }
1825
1826 // Create DIEnumerator elements for each enumerator.
1827 SmallVector<llvm::Value *, 16> Enumerators;
1828 ED = ED->getDefinition();
1829 for (EnumDecl::enumerator_iterator
1830 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1831 Enum != EnumEnd; ++Enum) {
1832 Enumerators.push_back(
1833 DBuilder.createEnumerator(Enum->getName(),
David Blaikieac8f43c2013-06-24 07:13:13 +00001834 Enum->getInitVal().getSExtValue()));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001835 }
1836
1837 // Return a CompositeType for the enum itself.
1838 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1839
1840 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1841 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00001842 llvm::DIDescriptor EnumContext =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001843 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Adrian Prantl59d6a712013-04-19 19:56:39 +00001844 llvm::DIType ClassTy = ED->isFixed() ?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001845 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Eric Christopher6537f082013-05-16 00:45:12 +00001846 llvm::DIType DbgTy =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001847 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1848 Size, Align, EltArray,
1849 ClassTy);
1850 return DbgTy;
1851}
1852
David Blaikie4b12be62013-01-21 04:37:12 +00001853static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1854 Qualifiers Quals;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001855 do {
David Blaikie4b12be62013-01-21 04:37:12 +00001856 Quals += T.getLocalQualifiers();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001857 QualType LastT = T;
1858 switch (T->getTypeClass()) {
1859 default:
David Blaikie4b12be62013-01-21 04:37:12 +00001860 return C.getQualifiedType(T.getTypePtr(), Quals);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001861 case Type::TemplateSpecialization:
1862 T = cast<TemplateSpecializationType>(T)->desugar();
1863 break;
1864 case Type::TypeOfExpr:
1865 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1866 break;
1867 case Type::TypeOf:
1868 T = cast<TypeOfType>(T)->getUnderlyingType();
1869 break;
1870 case Type::Decltype:
1871 T = cast<DecltypeType>(T)->getUnderlyingType();
1872 break;
1873 case Type::UnaryTransform:
1874 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1875 break;
1876 case Type::Attributed:
1877 T = cast<AttributedType>(T)->getEquivalentType();
1878 break;
1879 case Type::Elaborated:
1880 T = cast<ElaboratedType>(T)->getNamedType();
1881 break;
1882 case Type::Paren:
1883 T = cast<ParenType>(T)->getInnerType();
1884 break;
David Blaikie4b12be62013-01-21 04:37:12 +00001885 case Type::SubstTemplateTypeParm:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001886 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001887 break;
1888 case Type::Auto:
David Blaikie91296482013-05-24 21:24:35 +00001889 QualType DT = cast<AutoType>(T)->getDeducedType();
1890 if (DT.isNull())
1891 return T;
1892 T = DT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001893 break;
1894 }
Eric Christopher6537f082013-05-16 00:45:12 +00001895
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001896 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumid24c9ab2013-01-21 10:51:28 +00001897 (void)LastT;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001898 } while (true);
1899}
1900
Eric Christopherf0890c42013-05-16 00:52:20 +00001901/// getType - Get the type from the cache or return null type if it doesn't
1902/// exist.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001903llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
1904
1905 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001906 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopher6537f082013-05-16 00:45:12 +00001907
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001908 // Check for existing entry.
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001909 if (Ty->getTypeClass() == Type::ObjCInterface) {
1910 llvm::Value *V = getCachedInterfaceTypeOrNull(Ty);
1911 if (V)
1912 return llvm::DIType(cast<llvm::MDNode>(V));
1913 else return llvm::DIType();
1914 }
1915
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001916 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1917 TypeCache.find(Ty.getAsOpaquePtr());
1918 if (it != TypeCache.end()) {
1919 // Verify that the debug info still exists.
1920 if (llvm::Value *V = it->second)
1921 return llvm::DIType(cast<llvm::MDNode>(V));
1922 }
1923
1924 return llvm::DIType();
1925}
1926
1927/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1928/// doesn't exist.
1929llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1930
1931 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001932 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001933
1934 // Check for existing entry.
Adrian Prantl4919de62013-03-06 22:03:30 +00001935 llvm::Value *V = 0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001936 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1937 CompletedTypeCache.find(Ty.getAsOpaquePtr());
Adrian Prantl4919de62013-03-06 22:03:30 +00001938 if (it != CompletedTypeCache.end())
1939 V = it->second;
1940 else {
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001941 V = getCachedInterfaceTypeOrNull(Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001942 }
1943
Adrian Prantl4919de62013-03-06 22:03:30 +00001944 // Verify that any cached debug info still exists.
David Blaikieeab6a362013-06-21 00:40:50 +00001945 if (V != 0)
1946 return llvm::DIType(cast<llvm::MDNode>(V));
Adrian Prantl4919de62013-03-06 22:03:30 +00001947
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001948 return llvm::DIType();
1949}
1950
David Blaikieeab6a362013-06-21 00:40:50 +00001951void CGDebugInfo::completeFwdDecl(const RecordDecl &RD) {
1952 // In limited debug info we only want to do this if the complete type was
1953 // required.
1954 if (DebugKind <= CodeGenOptions::LimitedDebugInfo)
1955 return;
1956
David Blaikie076f51f2013-06-21 00:59:44 +00001957 QualType QTy = CGM.getContext().getRecordType(&RD);
1958 llvm::DIType T = getTypeOrNull(QTy);
David Blaikieeab6a362013-06-21 00:40:50 +00001959
Eric Christopherb2d13922013-07-18 00:52:50 +00001960 if (T && T.isForwardDecl())
David Blaikie076f51f2013-06-21 00:59:44 +00001961 getOrCreateType(QTy, getOrCreateFile(RD.getLocation()));
David Blaikieeab6a362013-06-21 00:40:50 +00001962}
1963
Adrian Prantlebbd7e02013-03-11 18:33:46 +00001964/// getCachedInterfaceTypeOrNull - Get the type from the interface
1965/// cache, unless it needs to regenerated. Otherwise return null.
1966llvm::Value *CGDebugInfo::getCachedInterfaceTypeOrNull(QualType Ty) {
1967 // Is there a cached interface that hasn't changed?
1968 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
1969 ::iterator it1 = ObjCInterfaceCache.find(Ty.getAsOpaquePtr());
1970
1971 if (it1 != ObjCInterfaceCache.end())
1972 if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty))
1973 if (Checksum(Decl) == it1->second.second)
1974 // Return cached forward declaration.
1975 return it1->second.first;
1976
1977 return 0;
1978}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001979
1980/// getOrCreateType - Get the type from the cache or create a new
1981/// one if necessary.
Eric Christopher56b108a2013-06-07 22:54:39 +00001982llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit,
1983 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001984 if (Ty.isNull())
1985 return llvm::DIType();
1986
1987 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00001988 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001989
1990 llvm::DIType T = getCompletedTypeOrNull(Ty);
1991
Eric Christopherb2d13922013-07-18 00:52:50 +00001992 if (T) {
David Blaikief0c31d92013-06-21 21:03:11 +00001993 // If we're looking for a definition, make sure we have definitions of any
1994 // underlying types.
1995 if (const TypedefType* TTy = dyn_cast<TypedefType>(Ty))
1996 getOrCreateType(TTy->getDecl()->getUnderlyingType(), Unit, Declaration);
1997 if (Ty.hasLocalQualifiers())
1998 getOrCreateType(QualType(Ty.getTypePtr(), 0), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001999 return T;
David Blaikief0c31d92013-06-21 21:03:11 +00002000 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002001
2002 // Otherwise create the type.
David Blaikie5f6e2f42013-06-05 05:32:23 +00002003 llvm::DIType Res = CreateTypeNode(Ty, Unit, Declaration);
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002004 void* TyPtr = Ty.getAsOpaquePtr();
2005
2006 // And update the type cache.
2007 TypeCache[TyPtr] = Res;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002008
2009 llvm::DIType TC = getTypeOrNull(Ty);
Eric Christopherb2d13922013-07-18 00:52:50 +00002010 if (TC && TC.isForwardDecl())
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002011 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
2012 else if (ObjCInterfaceDecl* Decl = getObjCInterfaceDecl(Ty)) {
2013 // Interface types may have elements added to them by a
2014 // subsequent implementation or extension, so we keep them in
2015 // the ObjCInterfaceCache together with a checksum. Instead of
Adrian Prantlf06989b2013-05-08 23:37:22 +00002016 // the (possibly) incomplete interface type, we return a forward
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002017 // declaration that gets RAUW'd in CGDebugInfo::finalize().
David Blaikiee2eb89a2013-05-21 18:29:40 +00002018 std::pair<llvm::WeakVH, unsigned> &V = ObjCInterfaceCache[TyPtr];
2019 if (V.first)
2020 return llvm::DIType(cast<llvm::MDNode>(V.first));
2021 TC = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2022 Decl->getName(), TheCU, Unit,
2023 getLineNumber(Decl->getLocation()),
2024 TheCU.getLanguage());
2025 // Store the forward declaration in the cache.
2026 V.first = TC;
2027 V.second = Checksum(Decl);
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002028
David Blaikiee2eb89a2013-05-21 18:29:40 +00002029 // Register the type for replacement in finalize().
2030 ReplaceMap.push_back(std::make_pair(TyPtr, static_cast<llvm::Value*>(TC)));
2031
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002032 return TC;
Adrian Prantl4919de62013-03-06 22:03:30 +00002033 }
2034
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002035 if (!Res.isForwardDecl())
Adrian Prantlebbd7e02013-03-11 18:33:46 +00002036 CompletedTypeCache[TyPtr] = Res;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002037
2038 return Res;
2039}
2040
Adrian Prantlb5a50072013-06-07 01:10:41 +00002041/// Currently the checksum of an interface includes the number of
2042/// ivars and property accessors.
Eric Christopher56b108a2013-06-07 22:54:39 +00002043unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl4f97f852013-06-07 01:10:48 +00002044 // The assumption is that the number of ivars can only increase
2045 // monotonically, so it is safe to just use their current number as
2046 // a checksum.
Adrian Prantlb5a50072013-06-07 01:10:41 +00002047 unsigned Sum = 0;
2048 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
2049 Ivar != 0; Ivar = Ivar->getNextIvar())
2050 ++Sum;
2051
2052 return Sum;
Adrian Prantl4919de62013-03-06 22:03:30 +00002053}
2054
2055ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2056 switch (Ty->getTypeClass()) {
2057 case Type::ObjCObjectPointer:
Eric Christopherf0890c42013-05-16 00:52:20 +00002058 return getObjCInterfaceDecl(cast<ObjCObjectPointerType>(Ty)
2059 ->getPointeeType());
Adrian Prantl4919de62013-03-06 22:03:30 +00002060 case Type::ObjCInterface:
2061 return cast<ObjCInterfaceType>(Ty)->getDecl();
2062 default:
2063 return 0;
2064 }
2065}
2066
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002067/// CreateTypeNode - Create a new debug type node.
Eric Christopher56b108a2013-06-07 22:54:39 +00002068llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit,
2069 bool Declaration) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002070 // Handle qualifiers, which recursively handles what they refer to.
2071 if (Ty.hasLocalQualifiers())
David Blaikie5f6e2f42013-06-05 05:32:23 +00002072 return CreateQualifiedType(Ty, Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002073
2074 const char *Diag = 0;
Eric Christopher6537f082013-05-16 00:45:12 +00002075
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002076 // Work out details of type.
2077 switch (Ty->getTypeClass()) {
2078#define TYPE(Class, Base)
2079#define ABSTRACT_TYPE(Class, Base)
2080#define NON_CANONICAL_TYPE(Class, Base)
2081#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2082#include "clang/AST/TypeNodes.def"
2083 llvm_unreachable("Dependent types cannot show up in debug information");
2084
2085 case Type::ExtVector:
2086 case Type::Vector:
2087 return CreateType(cast<VectorType>(Ty), Unit);
2088 case Type::ObjCObjectPointer:
2089 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2090 case Type::ObjCObject:
2091 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2092 case Type::ObjCInterface:
2093 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2094 case Type::Builtin:
2095 return CreateType(cast<BuiltinType>(Ty));
2096 case Type::Complex:
2097 return CreateType(cast<ComplexType>(Ty));
2098 case Type::Pointer:
2099 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner12df2462013-06-24 17:51:48 +00002100 case Type::Decayed:
2101 // Decayed types are just pointers in LLVM and DWARF.
2102 return CreateType(
2103 cast<PointerType>(cast<DecayedType>(Ty)->getDecayedType()), Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002104 case Type::BlockPointer:
2105 return CreateType(cast<BlockPointerType>(Ty), Unit);
2106 case Type::Typedef:
David Blaikie5f6e2f42013-06-05 05:32:23 +00002107 return CreateType(cast<TypedefType>(Ty), Unit, Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002108 case Type::Record:
David Blaikie5f6e2f42013-06-05 05:32:23 +00002109 return CreateType(cast<RecordType>(Ty), Declaration);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002110 case Type::Enum:
2111 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
2112 case Type::FunctionProto:
2113 case Type::FunctionNoProto:
2114 return CreateType(cast<FunctionType>(Ty), Unit);
2115 case Type::ConstantArray:
2116 case Type::VariableArray:
2117 case Type::IncompleteArray:
2118 return CreateType(cast<ArrayType>(Ty), Unit);
2119
2120 case Type::LValueReference:
2121 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2122 case Type::RValueReference:
2123 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2124
2125 case Type::MemberPointer:
2126 return CreateType(cast<MemberPointerType>(Ty), Unit);
2127
2128 case Type::Atomic:
2129 return CreateType(cast<AtomicType>(Ty), Unit);
2130
2131 case Type::Attributed:
2132 case Type::TemplateSpecialization:
2133 case Type::Elaborated:
2134 case Type::Paren:
2135 case Type::SubstTemplateTypeParm:
2136 case Type::TypeOfExpr:
2137 case Type::TypeOf:
2138 case Type::Decltype:
2139 case Type::UnaryTransform:
David Blaikie226399c2013-07-13 21:08:08 +00002140 case Type::PackExpansion:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002141 llvm_unreachable("type should have been unwrapped!");
David Blaikie91296482013-05-24 21:24:35 +00002142 case Type::Auto:
2143 Diag = "auto";
2144 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002145 }
Eric Christopher6537f082013-05-16 00:45:12 +00002146
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002147 assert(Diag && "Fall through without a diagnostic?");
2148 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2149 "debug information for %0 is not yet supported");
2150 CGM.getDiags().Report(DiagID)
2151 << Diag;
2152 return llvm::DIType();
2153}
2154
2155/// getOrCreateLimitedType - Get the type from the cache or create a new
2156/// limited type if necessary.
2157llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002158 llvm::DIFile Unit) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002159 if (Ty.isNull())
2160 return llvm::DIType();
2161
2162 // Unwrap the type as needed for debug information.
David Blaikie4b12be62013-01-21 04:37:12 +00002163 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002164
2165 llvm::DIType T = getTypeOrNull(Ty);
2166
2167 // We may have cached a forward decl when we could have created
2168 // a non-forward decl. Go ahead and create a non-forward decl
2169 // now.
Eric Christopherb2d13922013-07-18 00:52:50 +00002170 if (T && !T.isForwardDecl()) return T;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002171
2172 // Otherwise create the type.
2173 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
2174
Eric Christopherb2d13922013-07-18 00:52:50 +00002175 if (T && T.isForwardDecl())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002176 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(),
2177 static_cast<llvm::Value*>(T)));
2178
2179 // And update the type cache.
2180 TypeCache[Ty.getAsOpaquePtr()] = Res;
2181 return Res;
2182}
2183
2184// TODO: Currently used for context chains when limiting debug info.
2185llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
2186 RecordDecl *RD = Ty->getDecl();
Eric Christopher6537f082013-05-16 00:45:12 +00002187
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002188 // Get overall information about the record type for the debug info.
2189 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2190 unsigned Line = getLineNumber(RD->getLocation());
2191 StringRef RDName = getClassName(RD);
2192
2193 llvm::DIDescriptor RDContext;
Eric Christopher13c97672013-05-16 00:45:23 +00002194 if (DebugKind == CodeGenOptions::LimitedDebugInfo)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002195 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
2196 else
2197 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
2198
2199 // If this is just a forward declaration, construct an appropriately
2200 // marked node and just return it.
2201 if (!RD->getDefinition())
2202 return createRecordFwdDecl(RD, RDContext);
2203
2204 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2205 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie2fcadbe2013-03-26 23:47:35 +00002206 llvm::DICompositeType RealDecl;
Eric Christopher6537f082013-05-16 00:45:12 +00002207
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002208 if (RD->isUnion())
2209 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002210 Size, Align, 0, llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002211 else if (RD->isClass()) {
2212 // FIXME: This could be a struct type giving a default visibility different
2213 // than C++ class type, but needs llvm metadata changes first.
2214 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002215 Size, Align, 0, 0, llvm::DIType(),
2216 llvm::DIArray(), llvm::DIType(),
2217 llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002218 } else
2219 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
Eric Christopherf0890c42013-05-16 00:52:20 +00002220 Size, Align, 0, llvm::DIType(),
2221 llvm::DIArray());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002222
2223 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie2fcadbe2013-03-26 23:47:35 +00002224 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002225
David Blaikie841b37c2013-08-01 18:23:24 +00002226 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002227 // A class's primary base or the class itself contains the vtable.
David Blaikie2fcadbe2013-03-26 23:47:35 +00002228 llvm::DICompositeType ContainingType;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002229 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2230 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
2231 // Seek non virtual primary base root.
2232 while (1) {
Eric Christopherbe5f1be2013-02-21 22:35:08 +00002233 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2234 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2235 if (PBT && !BRL.isPrimaryBaseVirtual())
2236 PBase = PBT;
2237 else
2238 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002239 }
David Blaikie2fcadbe2013-03-26 23:47:35 +00002240 ContainingType = llvm::DICompositeType(
2241 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit));
2242 } else if (CXXDecl->isDynamicClass())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002243 ContainingType = RealDecl;
2244
David Blaikie2fcadbe2013-03-26 23:47:35 +00002245 RealDecl.setContainingType(ContainingType);
David Blaikie80588332013-08-01 20:31:40 +00002246 if (const ClassTemplateSpecializationDecl *TSpecial =
2247 dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl))
2248 RealDecl.setTypeArray(llvm::DIArray(),
2249 CollectCXXTemplateParams(TSpecial, DefUnit));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002250 }
2251 return llvm::DIType(RealDecl);
2252}
2253
2254/// CreateLimitedTypeNode - Create a new debug type node, but only forward
2255/// declare composite types that haven't been processed yet.
2256llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
2257
2258 // Work out details of type.
2259 switch (Ty->getTypeClass()) {
2260#define TYPE(Class, Base)
2261#define ABSTRACT_TYPE(Class, Base)
2262#define NON_CANONICAL_TYPE(Class, Base)
2263#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2264 #include "clang/AST/TypeNodes.def"
2265 llvm_unreachable("Dependent types cannot show up in debug information");
2266
2267 case Type::Record:
2268 return CreateLimitedType(cast<RecordType>(Ty));
2269 default:
David Blaikie5f6e2f42013-06-05 05:32:23 +00002270 return CreateTypeNode(Ty, Unit, false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002271 }
2272}
2273
2274/// CreateMemberType - Create new member and increase Offset by FType's size.
2275llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
2276 StringRef Name,
2277 uint64_t *Offset) {
2278 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2279 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2280 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
2281 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
2282 FieldSize, FieldAlign,
2283 *Offset, 0, FieldTy);
2284 *Offset += FieldSize;
2285 return Ty;
2286}
2287
David Blaikie9faebd22013-05-20 04:58:53 +00002288llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
2289 // We only need a declaration (not a definition) of the type - so use whatever
2290 // we would otherwise do to get a type for a pointee. (forward declarations in
2291 // limited debug info, full definitions (if the type definition is available)
2292 // in unlimited debug info)
2293 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
2294 llvm::DIFile DefUnit = getOrCreateFile(TD->getLocation());
David Blaikieb0f77b02013-05-24 21:33:22 +00002295 return getOrCreateTypeDeclaration(CGM.getContext().getTypeDeclType(TD),
2296 DefUnit);
David Blaikie9faebd22013-05-20 04:58:53 +00002297 }
2298 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2299 // This doesn't handle providing declarations (for functions or variables) for
2300 // entities without definitions in this TU, nor when the definition proceeds
2301 // the call to this function.
2302 // FIXME: This should be split out into more specific maps with support for
2303 // emitting forward declarations and merging definitions with declarations,
2304 // the same way as we do for types.
2305 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2306 DeclCache.find(D->getCanonicalDecl());
2307 if (I == DeclCache.end())
2308 return llvm::DIDescriptor();
2309 llvm::Value *V = I->second;
2310 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
2311}
2312
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002313/// getFunctionDeclaration - Return debug info descriptor to describe method
2314/// declaration for the given method definition.
2315llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
David Blaikie23e66db2013-06-22 00:09:36 +00002316 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2317 return llvm::DISubprogram();
2318
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002319 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2320 if (!FD) return llvm::DISubprogram();
2321
2322 // Setup context.
2323 getContextDescriptor(cast<Decl>(D->getDeclContext()));
2324
2325 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2326 MI = SPCache.find(FD->getCanonicalDecl());
2327 if (MI != SPCache.end()) {
2328 llvm::Value *V = MI->second;
2329 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie23e66db2013-06-22 00:09:36 +00002330 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002331 return SP;
2332 }
2333
2334 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
2335 E = FD->redecls_end(); I != E; ++I) {
2336 const FunctionDecl *NextFD = *I;
2337 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2338 MI = SPCache.find(NextFD->getCanonicalDecl());
2339 if (MI != SPCache.end()) {
2340 llvm::Value *V = MI->second;
2341 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie23e66db2013-06-22 00:09:36 +00002342 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002343 return SP;
2344 }
2345 }
2346 return llvm::DISubprogram();
2347}
2348
2349// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2350// implicit parameter "this".
David Blaikie9a845292013-05-22 23:22:42 +00002351llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2352 QualType FnType,
2353 llvm::DIFile F) {
David Blaikie23e66db2013-06-22 00:09:36 +00002354 if (!D || DebugKind == CodeGenOptions::DebugLineTablesOnly)
2355 // Create fake but valid subroutine type. Otherwise
2356 // llvm::DISubprogram::Verify() would return false, and
2357 // subprogram DIE will miss DW_AT_decl_file and
2358 // DW_AT_decl_line fields.
2359 return DBuilder.createSubroutineType(F, DBuilder.getOrCreateArray(None));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002360
2361 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2362 return getOrCreateMethodType(Method, F);
2363 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2364 // Add "self" and "_cmd"
2365 SmallVector<llvm::Value *, 16> Elts;
2366
2367 // First element is always return type. For 'void' functions it is NULL.
Adrian Prantl0cb00022013-05-22 21:37:49 +00002368 QualType ResultTy = OMethod->getResultType();
2369
2370 // Replace the instancetype keyword with the actual type.
2371 if (ResultTy == CGM.getContext().getObjCInstanceType())
2372 ResultTy = CGM.getContext().getPointerType(
2373 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
2374
Adrian Prantl566a9c32013-05-10 21:08:31 +00002375 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002376 // "self" pointer is always first argument.
Adrian Prantle86fcc42013-03-29 19:20:29 +00002377 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2378 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2379 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002380 // "_cmd" pointer is always second argument.
2381 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2382 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2383 // Get rest of the arguments.
Eric Christopher6537f082013-05-16 00:45:12 +00002384 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002385 PE = OMethod->param_end(); PI != PE; ++PI)
2386 Elts.push_back(getOrCreateType((*PI)->getType(), F));
2387
2388 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2389 return DBuilder.createSubroutineType(F, EltTypeArray);
2390 }
David Blaikie9a845292013-05-22 23:22:42 +00002391 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002392}
2393
2394/// EmitFunctionStart - Constructs the debug code for entering a function.
2395void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
2396 llvm::Function *Fn,
2397 CGBuilderTy &Builder) {
2398
2399 StringRef Name;
2400 StringRef LinkageName;
2401
2402 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2403
2404 const Decl *D = GD.getDecl();
2405 // Function may lack declaration in source code if it is created by Clang
2406 // CodeGen (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
2407 bool HasDecl = (D != 0);
2408 // Use the location of the declaration.
2409 SourceLocation Loc;
2410 if (HasDecl)
2411 Loc = D->getLocation();
2412
2413 unsigned Flags = 0;
2414 llvm::DIFile Unit = getOrCreateFile(Loc);
2415 llvm::DIDescriptor FDContext(Unit);
2416 llvm::DIArray TParamsArray;
2417 if (!HasDecl) {
2418 // Use llvm function name.
2419 Name = Fn->getName();
2420 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2421 // If there is a DISubprogram for this function available then use it.
2422 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2423 FI = SPCache.find(FD->getCanonicalDecl());
2424 if (FI != SPCache.end()) {
2425 llvm::Value *V = FI->second;
2426 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2427 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2428 llvm::MDNode *SPN = SP;
2429 LexicalBlockStack.push_back(SPN);
2430 RegionMap[D] = llvm::WeakVH(SP);
2431 return;
2432 }
2433 }
2434 Name = getFunctionName(FD);
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002435 // Use mangled name as linkage name for C/C++ functions.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002436 if (FD->hasPrototype()) {
2437 LinkageName = CGM.getMangledName(GD);
2438 Flags |= llvm::DIDescriptor::FlagPrototyped;
2439 }
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002440 // No need to replicate the linkage name if it isn't different from the
2441 // subprogram name, no need to have it at all unless coverage is enabled or
2442 // debug is set to more than just line tables.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002443 if (LinkageName == Name ||
Nick Lewyckyf2b5e072013-03-20 01:38:16 +00002444 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2445 !CGM.getCodeGenOpts().EmitGcovNotes &&
Eric Christopher13c97672013-05-16 00:45:23 +00002446 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002447 LinkageName = StringRef();
2448
Eric Christopher13c97672013-05-16 00:45:23 +00002449 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002450 if (const NamespaceDecl *NSDecl =
2451 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2452 FDContext = getOrCreateNameSpace(NSDecl);
2453 else if (const RecordDecl *RDecl =
2454 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2455 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
2456
2457 // Collect template parameters.
2458 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2459 }
2460 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2461 Name = getObjCMethodName(OMD);
2462 Flags |= llvm::DIDescriptor::FlagPrototyped;
2463 } else {
2464 // Use llvm function name.
2465 Name = Fn->getName();
2466 Flags |= llvm::DIDescriptor::FlagPrototyped;
2467 }
2468 if (!Name.empty() && Name[0] == '\01')
2469 Name = Name.substr(1);
2470
2471 unsigned LineNo = getLineNumber(Loc);
2472 if (!HasDecl || D->isImplicit())
2473 Flags |= llvm::DIDescriptor::FlagArtificial;
2474
David Blaikie23e66db2013-06-22 00:09:36 +00002475 llvm::DISubprogram SP = DBuilder.createFunction(
2476 FDContext, Name, LinkageName, Unit, LineNo,
2477 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2478 true /*definition*/, getLineNumber(CurLoc), Flags,
2479 CGM.getLangOpts().Optimize, Fn, TParamsArray, getFunctionDeclaration(D));
David Blaikie9faebd22013-05-20 04:58:53 +00002480 if (HasDecl)
2481 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002482
2483 // Push function on region stack.
2484 llvm::MDNode *SPN = SP;
2485 LexicalBlockStack.push_back(SPN);
2486 if (HasDecl)
2487 RegionMap[D] = llvm::WeakVH(SP);
2488}
2489
2490/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl18a0cd52013-07-18 00:27:59 +00002491/// information in the source file. If the location is invalid, the
2492/// previous location will be reused.
Adrian Prantl00df5ea2013-03-12 20:43:25 +00002493void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
2494 bool ForceColumnInfo) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002495 // Update our current location
2496 setLocation(Loc);
2497
2498 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
2499
2500 // Don't bother if things are the same as last time.
2501 SourceManager &SM = CGM.getContext().getSourceManager();
2502 if (CurLoc == PrevLoc ||
2503 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2504 // New Builder may not be in sync with CGDebugInfo.
David Blaikie0a0f93c2013-02-01 19:09:49 +00002505 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2506 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
2507 LexicalBlockStack.back())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002508 return;
Eric Christopher6537f082013-05-16 00:45:12 +00002509
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002510 // Update last state.
2511 PrevLoc = CurLoc;
2512
2513 llvm::MDNode *Scope = LexicalBlockStack.back();
Adrian Prantl00df5ea2013-03-12 20:43:25 +00002514 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get
2515 (getLineNumber(CurLoc),
2516 getColumnNumber(CurLoc, ForceColumnInfo),
2517 Scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002518}
2519
2520/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2521/// the stack.
2522void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
2523 llvm::DIDescriptor D =
2524 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
2525 llvm::DIDescriptor() :
2526 llvm::DIDescriptor(LexicalBlockStack.back()),
2527 getOrCreateFile(CurLoc),
2528 getLineNumber(CurLoc),
2529 getColumnNumber(CurLoc));
2530 llvm::MDNode *DN = D;
2531 LexicalBlockStack.push_back(DN);
2532}
2533
2534/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2535/// region - beginning of a DW_TAG_lexical_block.
Eric Christopherf0890c42013-05-16 00:52:20 +00002536void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2537 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002538 // Set our current location.
2539 setLocation(Loc);
2540
2541 // Create a new lexical block and push it on the stack.
2542 CreateLexicalBlock(Loc);
2543
2544 // Emit a line table change for the current location inside the new scope.
2545 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
2546 getColumnNumber(Loc),
2547 LexicalBlockStack.back()));
2548}
2549
2550/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2551/// region - end of a DW_TAG_lexical_block.
Eric Christopherf0890c42013-05-16 00:52:20 +00002552void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2553 SourceLocation Loc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002554 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2555
2556 // Provide an entry in the line table for the end of the block.
2557 EmitLocation(Builder, Loc);
2558
2559 LexicalBlockStack.pop_back();
2560}
2561
2562/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2563void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2564 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2565 unsigned RCount = FnBeginRegionCount.back();
2566 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2567
2568 // Pop all regions for this function.
2569 while (LexicalBlockStack.size() != RCount)
2570 EmitLexicalBlockEnd(Builder, CurLoc);
2571 FnBeginRegionCount.pop_back();
2572}
2573
Eric Christopher6537f082013-05-16 00:45:12 +00002574// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002575// See BuildByRefType.
2576llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2577 uint64_t *XOffset) {
2578
2579 SmallVector<llvm::Value *, 5> EltTys;
2580 QualType FType;
2581 uint64_t FieldSize, FieldOffset;
2582 unsigned FieldAlign;
Eric Christopher6537f082013-05-16 00:45:12 +00002583
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002584 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00002585 QualType Type = VD->getType();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002586
2587 FieldOffset = 0;
2588 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2589 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2590 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2591 FType = CGM.getContext().IntTy;
2592 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2593 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2594
2595 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2596 if (HasCopyAndDispose) {
2597 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2598 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2599 &FieldOffset));
2600 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2601 &FieldOffset));
2602 }
2603 bool HasByrefExtendedLayout;
2604 Qualifiers::ObjCLifetime Lifetime;
2605 if (CGM.getContext().getByrefLifetime(Type,
2606 Lifetime, HasByrefExtendedLayout)
Adrian Prantl1f437912013-07-23 00:12:14 +00002607 && HasByrefExtendedLayout) {
2608 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002609 EltTys.push_back(CreateMemberType(Unit, FType,
2610 "__byref_variable_layout",
2611 &FieldOffset));
Adrian Prantl1f437912013-07-23 00:12:14 +00002612 }
Eric Christopher6537f082013-05-16 00:45:12 +00002613
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002614 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2615 if (Align > CGM.getContext().toCharUnitsFromBits(
John McCall64aa4b32013-04-16 22:48:15 +00002616 CGM.getTarget().getPointerAlign(0))) {
Eric Christopher6537f082013-05-16 00:45:12 +00002617 CharUnits FieldOffsetInBytes
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002618 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2619 CharUnits AlignedOffsetInBytes
2620 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2621 CharUnits NumPaddingBytes
2622 = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopher6537f082013-05-16 00:45:12 +00002623
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002624 if (NumPaddingBytes.isPositive()) {
2625 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2626 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2627 pad, ArrayType::Normal, 0);
2628 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2629 }
2630 }
Eric Christopher6537f082013-05-16 00:45:12 +00002631
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002632 FType = Type;
2633 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2634 FieldSize = CGM.getContext().getTypeSize(FType);
2635 FieldAlign = CGM.getContext().toBits(Align);
2636
Eric Christopher6537f082013-05-16 00:45:12 +00002637 *XOffset = FieldOffset;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002638 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
2639 0, FieldSize, FieldAlign,
2640 FieldOffset, 0, FieldTy);
2641 EltTys.push_back(FieldTy);
2642 FieldOffset += FieldSize;
Eric Christopher6537f082013-05-16 00:45:12 +00002643
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002644 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher6537f082013-05-16 00:45:12 +00002645
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002646 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopher6537f082013-05-16 00:45:12 +00002647
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002648 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikiec1d0af12013-02-25 01:07:08 +00002649 llvm::DIType(), Elements);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002650}
2651
2652/// EmitDeclare - Emit local variable declaration debug info.
2653void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Eric Christopher6537f082013-05-16 00:45:12 +00002654 llvm::Value *Storage,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002655 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002656 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002657 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2658
2659 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2660 llvm::DIType Ty;
2661 uint64_t XOffset = 0;
2662 if (VD->hasAttr<BlocksAttr>())
2663 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00002664 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002665 Ty = getOrCreateType(VD->getType(), Unit);
2666
2667 // If there is no debug info for this type then do not emit debug info
2668 // for this variable.
2669 if (!Ty)
2670 return;
2671
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002672 // Get location information.
2673 unsigned Line = getLineNumber(VD->getLocation());
2674 unsigned Column = getColumnNumber(VD->getLocation());
2675 unsigned Flags = 0;
2676 if (VD->isImplicit())
2677 Flags |= llvm::DIDescriptor::FlagArtificial;
2678 // If this is the first argument and it is implicit then
2679 // give it an object pointer flag.
2680 // FIXME: There has to be a better way to do this, but for static
2681 // functions there won't be an implicit param at arg1 and
2682 // otherwise it is 'self' or 'this'.
2683 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2684 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikie41c9bae2013-06-19 21:53:53 +00002685 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopher7dab97b2013-07-17 22:52:53 +00002686 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2687 !VD->getType()->isPointerType())
David Blaikie41c9bae2013-06-19 21:53:53 +00002688 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002689
2690 llvm::MDNode *Scope = LexicalBlockStack.back();
2691
2692 StringRef Name = VD->getName();
2693 if (!Name.empty()) {
2694 if (VD->hasAttr<BlocksAttr>()) {
2695 CharUnits offset = CharUnits::fromQuantity(32);
2696 SmallVector<llvm::Value *, 9> addr;
2697 llvm::Type *Int64Ty = CGM.Int64Ty;
2698 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2699 // offset of __forwarding field
2700 offset = CGM.getContext().toCharUnitsFromBits(
John McCall64aa4b32013-04-16 22:48:15 +00002701 CGM.getTarget().getPointerWidth(0));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002702 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2703 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2704 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2705 // offset of x field
2706 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2707 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2708
2709 // Create the descriptor for the variable.
2710 llvm::DIVariable D =
Eric Christopher6537f082013-05-16 00:45:12 +00002711 DBuilder.createComplexVariable(Tag,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002712 llvm::DIDescriptor(Scope),
2713 VD->getName(), Unit, Line, Ty,
2714 addr, ArgNo);
Eric Christopher6537f082013-05-16 00:45:12 +00002715
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002716 // Insert an llvm.dbg.declare into the current block.
2717 llvm::Instruction *Call =
2718 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2719 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2720 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002721 }
David Blaikie436653b2013-01-05 05:58:35 +00002722 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2723 // If VD is an anonymous union then Storage represents value for
2724 // all union fields.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002725 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikied8180cf2013-01-05 20:03:07 +00002726 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002727 for (RecordDecl::field_iterator I = RD->field_begin(),
2728 E = RD->field_end();
2729 I != E; ++I) {
2730 FieldDecl *Field = *I;
2731 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2732 StringRef FieldName = Field->getName();
Eric Christopher6537f082013-05-16 00:45:12 +00002733
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002734 // Ignore unnamed fields. Do not ignore unnamed records.
2735 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2736 continue;
Eric Christopher6537f082013-05-16 00:45:12 +00002737
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002738 // Use VarDecl's Tag, Scope and Line number.
2739 llvm::DIVariable D =
2740 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Eric Christopher6537f082013-05-16 00:45:12 +00002741 FieldName, Unit, Line, FieldTy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002742 CGM.getLangOpts().Optimize, Flags,
2743 ArgNo);
Eric Christopher6537f082013-05-16 00:45:12 +00002744
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002745 // Insert an llvm.dbg.declare into the current block.
2746 llvm::Instruction *Call =
2747 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2748 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2749 }
David Blaikied8180cf2013-01-05 20:03:07 +00002750 return;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002751 }
2752 }
David Blaikie436653b2013-01-05 05:58:35 +00002753
2754 // Create the descriptor for the variable.
2755 llvm::DIVariable D =
2756 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2757 Name, Unit, Line, Ty,
2758 CGM.getLangOpts().Optimize, Flags, ArgNo);
2759
2760 // Insert an llvm.dbg.declare into the current block.
2761 llvm::Instruction *Call =
2762 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2763 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002764}
2765
2766void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2767 llvm::Value *Storage,
2768 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002769 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002770 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2771}
2772
Adrian Prantle86fcc42013-03-29 19:20:29 +00002773/// Look up the completed type for a self pointer in the TypeCache and
2774/// create a copy of it with the ObjectPointer and Artificial flags
2775/// set. If the type is not cached, a new one is created. This should
2776/// never happen though, since creating a type for the implicit self
2777/// argument implies that we already parsed the interface definition
2778/// and the ivar declarations in the implementation.
Eric Christopherf0890c42013-05-16 00:52:20 +00002779llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2780 llvm::DIType Ty) {
Adrian Prantle86fcc42013-03-29 19:20:29 +00002781 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christopherb2d13922013-07-18 00:52:50 +00002782 if (CachedTy) Ty = CachedTy;
Adrian Prantle86fcc42013-03-29 19:20:29 +00002783 else DEBUG(llvm::dbgs() << "No cached type for self.");
2784 return DBuilder.createObjectPointerType(Ty);
2785}
2786
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002787void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2788 llvm::Value *Storage,
2789 CGBuilderTy &Builder,
2790 const CGBlockInfo &blockInfo) {
Eric Christopher13c97672013-05-16 00:45:23 +00002791 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002792 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopher6537f082013-05-16 00:45:12 +00002793
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002794 if (Builder.GetInsertBlock() == 0)
2795 return;
Eric Christopher6537f082013-05-16 00:45:12 +00002796
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002797 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopher6537f082013-05-16 00:45:12 +00002798
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002799 uint64_t XOffset = 0;
2800 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2801 llvm::DIType Ty;
2802 if (isByRef)
2803 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopher6537f082013-05-16 00:45:12 +00002804 else
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002805 Ty = getOrCreateType(VD->getType(), Unit);
2806
2807 // Self is passed along as an implicit non-arg variable in a
2808 // block. Mark it as the object pointer.
2809 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantle86fcc42013-03-29 19:20:29 +00002810 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002811
2812 // Get location information.
2813 unsigned Line = getLineNumber(VD->getLocation());
2814 unsigned Column = getColumnNumber(VD->getLocation());
2815
2816 const llvm::DataLayout &target = CGM.getDataLayout();
2817
2818 CharUnits offset = CharUnits::fromQuantity(
2819 target.getStructLayout(blockInfo.StructureType)
2820 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2821
2822 SmallVector<llvm::Value *, 9> addr;
2823 llvm::Type *Int64Ty = CGM.Int64Ty;
Adrian Prantl9b97adf2013-03-29 19:20:35 +00002824 if (isa<llvm::AllocaInst>(Storage))
2825 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002826 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2827 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2828 if (isByRef) {
2829 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2830 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2831 // offset of __forwarding field
2832 offset = CGM.getContext()
2833 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
2834 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2835 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2836 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
2837 // offset of x field
2838 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
2839 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2840 }
2841
2842 // Create the descriptor for the variable.
2843 llvm::DIVariable D =
Eric Christopher6537f082013-05-16 00:45:12 +00002844 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002845 llvm::DIDescriptor(LexicalBlockStack.back()),
2846 VD->getName(), Unit, Line, Ty, addr);
Adrian Prantl9b97adf2013-03-29 19:20:35 +00002847
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002848 // Insert an llvm.dbg.declare into the current block.
2849 llvm::Instruction *Call =
2850 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
2851 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2852 LexicalBlockStack.back()));
2853}
2854
2855/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2856/// variable declaration.
2857void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2858 unsigned ArgNo,
2859 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002860 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002861 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2862}
2863
2864namespace {
2865 struct BlockLayoutChunk {
2866 uint64_t OffsetInBits;
2867 const BlockDecl::Capture *Capture;
2868 };
2869 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2870 return l.OffsetInBits < r.OffsetInBits;
2871 }
2872}
2873
2874void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl836e7c92013-03-14 17:53:33 +00002875 llvm::Value *Arg,
2876 llvm::Value *LocalAddr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002877 CGBuilderTy &Builder) {
Eric Christopher13c97672013-05-16 00:45:23 +00002878 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002879 ASTContext &C = CGM.getContext();
2880 const BlockDecl *blockDecl = block.getBlockDecl();
2881
2882 // Collect some general information about the block's location.
2883 SourceLocation loc = blockDecl->getCaretLocation();
2884 llvm::DIFile tunit = getOrCreateFile(loc);
2885 unsigned line = getLineNumber(loc);
2886 unsigned column = getColumnNumber(loc);
Eric Christopher6537f082013-05-16 00:45:12 +00002887
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002888 // Build the debug-info type for the block literal.
2889 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2890
2891 const llvm::StructLayout *blockLayout =
2892 CGM.getDataLayout().getStructLayout(block.StructureType);
2893
2894 SmallVector<llvm::Value*, 16> fields;
2895 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2896 blockLayout->getElementOffsetInBits(0),
2897 tunit, tunit));
2898 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2899 blockLayout->getElementOffsetInBits(1),
2900 tunit, tunit));
2901 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2902 blockLayout->getElementOffsetInBits(2),
2903 tunit, tunit));
2904 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2905 blockLayout->getElementOffsetInBits(3),
2906 tunit, tunit));
2907 fields.push_back(createFieldType("__descriptor",
2908 C.getPointerType(block.NeedsCopyDispose ?
2909 C.getBlockDescriptorExtendedType() :
2910 C.getBlockDescriptorType()),
2911 0, loc, AS_public,
2912 blockLayout->getElementOffsetInBits(4),
2913 tunit, tunit));
2914
2915 // We want to sort the captures by offset, not because DWARF
2916 // requires this, but because we're paranoid about debuggers.
2917 SmallVector<BlockLayoutChunk, 8> chunks;
2918
2919 // 'this' capture.
2920 if (blockDecl->capturesCXXThis()) {
2921 BlockLayoutChunk chunk;
2922 chunk.OffsetInBits =
2923 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2924 chunk.Capture = 0;
2925 chunks.push_back(chunk);
2926 }
2927
2928 // Variable captures.
2929 for (BlockDecl::capture_const_iterator
2930 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2931 i != e; ++i) {
2932 const BlockDecl::Capture &capture = *i;
2933 const VarDecl *variable = capture.getVariable();
2934 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2935
2936 // Ignore constant captures.
2937 if (captureInfo.isConstant())
2938 continue;
2939
2940 BlockLayoutChunk chunk;
2941 chunk.OffsetInBits =
2942 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2943 chunk.Capture = &capture;
2944 chunks.push_back(chunk);
2945 }
2946
2947 // Sort by offset.
2948 llvm::array_pod_sort(chunks.begin(), chunks.end());
2949
2950 for (SmallVectorImpl<BlockLayoutChunk>::iterator
2951 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2952 uint64_t offsetInBits = i->OffsetInBits;
2953 const BlockDecl::Capture *capture = i->Capture;
2954
2955 // If we have a null capture, this must be the C++ 'this' capture.
2956 if (!capture) {
2957 const CXXMethodDecl *method =
2958 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2959 QualType type = method->getThisType(C);
2960
2961 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
2962 offsetInBits, tunit, tunit));
2963 continue;
2964 }
2965
2966 const VarDecl *variable = capture->getVariable();
2967 StringRef name = variable->getName();
2968
2969 llvm::DIType fieldType;
2970 if (capture->isByRef()) {
2971 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2972
2973 // FIXME: this creates a second copy of this type!
2974 uint64_t xoffset;
2975 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2976 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
2977 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
2978 ptrInfo.first, ptrInfo.second,
2979 offsetInBits, 0, fieldType);
2980 } else {
2981 fieldType = createFieldType(name, variable->getType(), 0,
2982 loc, AS_public, offsetInBits, tunit, tunit);
2983 }
2984 fields.push_back(fieldType);
2985 }
2986
2987 SmallString<36> typeName;
2988 llvm::raw_svector_ostream(typeName)
2989 << "__block_literal_" << CGM.getUniqueBlockCount();
2990
2991 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
2992
2993 llvm::DIType type =
2994 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2995 CGM.getContext().toBits(block.BlockSize),
2996 CGM.getContext().toBits(block.BlockAlign),
David Blaikiec1d0af12013-02-25 01:07:08 +00002997 0, llvm::DIType(), fieldsArray);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002998 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2999
3000 // Get overall information about the block.
3001 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3002 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003003
3004 // Create the descriptor for the parameter.
3005 llvm::DIVariable debugVar =
3006 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
Eric Christopher6537f082013-05-16 00:45:12 +00003007 llvm::DIDescriptor(scope),
Adrian Prantl836e7c92013-03-14 17:53:33 +00003008 Arg->getName(), tunit, line, type,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003009 CGM.getLangOpts().Optimize, flags,
Adrian Prantl836e7c92013-03-14 17:53:33 +00003010 cast<llvm::Argument>(Arg)->getArgNo() + 1);
3011
Adrian Prantlbea407c2013-03-14 21:52:59 +00003012 if (LocalAddr) {
Adrian Prantl836e7c92013-03-14 17:53:33 +00003013 // Insert an llvm.dbg.value into the current block.
Adrian Prantlbea407c2013-03-14 21:52:59 +00003014 llvm::Instruction *DbgVal =
3015 DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar,
Eric Christopherf068c922013-04-02 22:59:11 +00003016 Builder.GetInsertBlock());
Adrian Prantlbea407c2013-03-14 21:52:59 +00003017 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3018 }
Adrian Prantl836e7c92013-03-14 17:53:33 +00003019
Adrian Prantlbea407c2013-03-14 21:52:59 +00003020 // Insert an llvm.dbg.declare into the current block.
3021 llvm::Instruction *DbgDecl =
3022 DBuilder.insertDeclare(Arg, debugVar, Builder.GetInsertBlock());
3023 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003024}
3025
Eric Christopher0395de32013-01-16 01:22:32 +00003026/// getStaticDataMemberDeclaration - If D is an out-of-class definition of
3027/// a static data member of a class, find its corresponding in-class
3028/// declaration.
3029llvm::DIDerivedType CGDebugInfo::getStaticDataMemberDeclaration(const Decl *D) {
3030 if (cast<VarDecl>(D)->isStaticDataMember()) {
3031 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
3032 MI = StaticDataMemberCache.find(D->getCanonicalDecl());
3033 if (MI != StaticDataMemberCache.end())
3034 // Verify the info still exists.
3035 if (llvm::Value *V = MI->second)
3036 return llvm::DIDerivedType(cast<llvm::MDNode>(V));
3037 }
3038 return llvm::DIDerivedType();
3039}
3040
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003041/// EmitGlobalVariable - Emit information about a global variable.
3042void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3043 const VarDecl *D) {
Eric Christopher13c97672013-05-16 00:45:23 +00003044 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003045 // Create global variable debug descriptor.
3046 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
3047 unsigned LineNo = getLineNumber(D->getLocation());
3048
3049 setLocation(D->getLocation());
3050
3051 QualType T = D->getType();
3052 if (T->isIncompleteArrayType()) {
3053
3054 // CodeGen turns int[] into int[1] so we'll do the same here.
3055 llvm::APInt ConstVal(32, 1);
3056 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3057
3058 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3059 ArrayType::Normal, 0);
3060 }
3061 StringRef DeclName = D->getName();
3062 StringRef LinkageName;
3063 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
3064 && !isa<ObjCMethodDecl>(D->getDeclContext()))
3065 LinkageName = Var->getName();
3066 if (LinkageName == DeclName)
3067 LinkageName = StringRef();
Eric Christopher6537f082013-05-16 00:45:12 +00003068 llvm::DIDescriptor DContext =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003069 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Eric Christopher56b108a2013-06-07 22:54:39 +00003070 llvm::DIGlobalVariable GV =
3071 DBuilder.createStaticVariable(DContext, DeclName, LinkageName, Unit,
3072 LineNo, getOrCreateType(T, Unit),
3073 Var->hasInternalLinkage(), Var,
3074 getStaticDataMemberDeclaration(D));
David Blaikie9faebd22013-05-20 04:58:53 +00003075 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003076}
3077
3078/// EmitGlobalVariable - Emit information about an objective-c interface.
3079void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
3080 ObjCInterfaceDecl *ID) {
Eric Christopher13c97672013-05-16 00:45:23 +00003081 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003082 // Create global variable debug descriptor.
3083 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
3084 unsigned LineNo = getLineNumber(ID->getLocation());
3085
3086 StringRef Name = ID->getName();
3087
3088 QualType T = CGM.getContext().getObjCInterfaceType(ID);
3089 if (T->isIncompleteArrayType()) {
3090
3091 // CodeGen turns int[] into int[1] so we'll do the same here.
3092 llvm::APInt ConstVal(32, 1);
3093 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3094
3095 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3096 ArrayType::Normal, 0);
3097 }
3098
3099 DBuilder.createGlobalVariable(Name, Unit, LineNo,
3100 getOrCreateType(T, Unit),
3101 Var->hasInternalLinkage(), Var);
3102}
3103
3104/// EmitGlobalVariable - Emit global variable's debug info.
Eric Christopher6537f082013-05-16 00:45:12 +00003105void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003106 llvm::Constant *Init) {
Eric Christopher13c97672013-05-16 00:45:23 +00003107 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003108 // Create the descriptor for the variable.
3109 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3110 StringRef Name = VD->getName();
3111 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3112 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3113 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3114 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3115 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3116 }
3117 // Do not use DIGlobalVariable for enums.
3118 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3119 return;
Eric Christopher56b108a2013-06-07 22:54:39 +00003120 llvm::DIGlobalVariable GV =
3121 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
3122 getLineNumber(VD->getLocation()), Ty, true,
3123 Init, getStaticDataMemberDeclaration(VD));
David Blaikie9faebd22013-05-20 04:58:53 +00003124 DeclCache.insert(std::make_pair(VD->getCanonicalDecl(), llvm::WeakVH(GV)));
3125}
3126
3127llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3128 if (!LexicalBlockStack.empty())
3129 return llvm::DIScope(LexicalBlockStack.back());
3130 return getContextDescriptor(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003131}
3132
David Blaikie957dac52013-04-22 06:13:21 +00003133void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikie9faebd22013-05-20 04:58:53 +00003134 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3135 return;
David Blaikie957dac52013-04-22 06:13:21 +00003136 DBuilder.createImportedModule(
David Blaikie9faebd22013-05-20 04:58:53 +00003137 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3138 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie957dac52013-04-22 06:13:21 +00003139 getLineNumber(UD.getLocation()));
3140}
3141
David Blaikie9faebd22013-05-20 04:58:53 +00003142void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3143 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3144 return;
3145 assert(UD.shadow_size() &&
3146 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3147 // Emitting one decl is sufficient - debuggers can detect that this is an
3148 // overloaded name & provide lookup for all the overloads.
3149 const UsingShadowDecl &USD = **UD.shadow_begin();
Eric Christopher56b108a2013-06-07 22:54:39 +00003150 if (llvm::DIDescriptor Target =
3151 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikie9faebd22013-05-20 04:58:53 +00003152 DBuilder.createImportedDeclaration(
3153 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3154 getLineNumber(USD.getLocation()));
3155}
3156
David Blaikiefc46ebc2013-05-20 22:50:41 +00003157llvm::DIImportedEntity
3158CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3159 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3160 return llvm::DIImportedEntity(0);
3161 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3162 if (VH)
3163 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
3164 llvm::DIImportedEntity R(0);
3165 if (const NamespaceAliasDecl *Underlying =
3166 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3167 // This could cache & dedup here rather than relying on metadata deduping.
3168 R = DBuilder.createImportedModule(
3169 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3170 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3171 NA.getName());
3172 else
3173 R = DBuilder.createImportedModule(
3174 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3175 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3176 getLineNumber(NA.getLocation()), NA.getName());
3177 VH = R;
3178 return R;
3179}
3180
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003181/// getOrCreateNamesSpace - Return namespace descriptor for the given
3182/// namespace decl.
Eric Christopher6537f082013-05-16 00:45:12 +00003183llvm::DINameSpace
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003184CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Eric Christopher6537f082013-05-16 00:45:12 +00003185 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003186 NameSpaceCache.find(NSDecl);
3187 if (I != NameSpaceCache.end())
3188 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopher6537f082013-05-16 00:45:12 +00003189
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003190 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3191 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopher6537f082013-05-16 00:45:12 +00003192 llvm::DIDescriptor Context =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003193 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3194 llvm::DINameSpace NS =
3195 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3196 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3197 return NS;
3198}
3199
3200void CGDebugInfo::finalize() {
3201 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
3202 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
3203 llvm::DIType Ty, RepTy;
3204 // Verify that the debug info still exists.
3205 if (llvm::Value *V = VI->second)
3206 Ty = llvm::DIType(cast<llvm::MDNode>(V));
Eric Christopher6537f082013-05-16 00:45:12 +00003207
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003208 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
3209 TypeCache.find(VI->first);
3210 if (it != TypeCache.end()) {
3211 // Verify that the debug info still exists.
3212 if (llvm::Value *V = it->second)
3213 RepTy = llvm::DIType(cast<llvm::MDNode>(V));
3214 }
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003215
Eric Christopherb2d13922013-07-18 00:52:50 +00003216 if (Ty && Ty.isForwardDecl() && RepTy)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003217 Ty.replaceAllUsesWith(RepTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003218 }
Adrian Prantlebbd7e02013-03-11 18:33:46 +00003219
3220 // We keep our own list of retained types, because we need to look
3221 // up the final type in the type cache.
3222 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3223 RE = RetainedTypes.end(); RI != RE; ++RI)
3224 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
3225
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003226 DBuilder.finalize();
3227}