blob: 512db76a8416945c605717895e43b741dce7e5b7 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000040#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041using namespace clang;
42using namespace clang::CodeGen;
43
44CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000045 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
46 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000047 CreateCompileUnit();
48}
49
50CGDebugInfo::~CGDebugInfo() {
51 assert(LexicalBlockStack.empty() &&
52 "Region stack mismatch, stack not empty!");
53}
54
Eric Christopher0a1301f2014-02-26 02:49:36 +000055SaveAndRestoreLocation::SaveAndRestoreLocation(CodeGenFunction &CGF,
56 CGBuilderTy &B)
57 : DI(CGF.getDebugInfo()), Builder(B) {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000058 if (DI) {
59 SavedLoc = DI->getLocation();
60 DI->CurLoc = SourceLocation();
Adrian Prantl2e0637f2013-07-18 00:28:02 +000061 }
62}
63
Adrian Prantld1b151e2014-01-17 00:15:10 +000064SaveAndRestoreLocation::~SaveAndRestoreLocation() {
65 if (DI)
66 DI->EmitLocation(Builder, SavedLoc);
67}
68
69NoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B)
70 : SaveAndRestoreLocation(CGF, B) {
71 if (DI)
72 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
73}
74
Adrian Prantl2e0637f2013-07-18 00:28:02 +000075NoLocation::~NoLocation() {
Adrian Prantld1b151e2014-01-17 00:15:10 +000076 if (DI)
Adrian Prantl2e0637f2013-07-18 00:28:02 +000077 assert(Builder.getCurrentDebugLocation().isUnknown());
Adrian Prantl2e0637f2013-07-18 00:28:02 +000078}
79
Adrian Prantlb75016d2013-07-18 01:36:04 +000080ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Adrian Prantld1b151e2014-01-17 00:15:10 +000081 : SaveAndRestoreLocation(CGF, B) {
82 if (DI)
Adrian Prantl49a78562013-07-24 20:34:39 +000083 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
Adrian Prantl49a78562013-07-24 20:34:39 +000084}
85
86void ArtificialLocation::Emit() {
87 if (DI) {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000088 // Sync the Builder.
89 DI->EmitLocation(Builder, SavedLoc);
90 DI->CurLoc = SourceLocation();
91 // Construct a location that has a valid scope, but no line info.
Adrian Prantl49a78562013-07-24 20:34:39 +000092 assert(!DI->LexicalBlockStack.empty());
93 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
Adrian Prantl2e0637f2013-07-18 00:28:02 +000094 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
95 }
96}
97
Adrian Prantlb75016d2013-07-18 01:36:04 +000098ArtificialLocation::~ArtificialLocation() {
Adrian Prantld1b151e2014-01-17 00:15:10 +000099 if (DI)
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000100 assert(Builder.getCurrentDebugLocation().getLine() == 0);
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000101}
102
Guy Benyei11169dd2012-12-18 14:30:41 +0000103void CGDebugInfo::setLocation(SourceLocation Loc) {
104 // If the new location isn't valid return.
Adrian Prantlb1b3bfc2013-07-18 00:27:56 +0000105 if (Loc.isInvalid()) return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000106
107 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
108
109 // If we've changed files in the middle of a lexical scope go ahead
110 // and create a new lexical scope with file node if it's different
111 // from the one in the scope.
112 if (LexicalBlockStack.empty()) return;
113
114 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +0000115 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000116 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000117
David Blaikieaabde052014-05-14 00:29:00 +0000118 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000119 return;
120
Guy Benyei11169dd2012-12-18 14:30:41 +0000121 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000122 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Guy Benyei11169dd2012-12-18 14:30:41 +0000123 llvm::DIDescriptor D
124 = DBuilder.createLexicalBlockFile(LBF.getScope(),
125 getOrCreateFile(CurLoc));
126 llvm::MDNode *N = D;
127 LexicalBlockStack.pop_back();
128 LexicalBlockStack.push_back(N);
David Blaikie0a21d0d2013-01-26 22:16:26 +0000129 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000130 llvm::DIDescriptor D
131 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
132 llvm::MDNode *N = D;
133 LexicalBlockStack.pop_back();
134 LexicalBlockStack.push_back(N);
135 }
136}
137
138/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000139llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000140 if (!Context)
141 return TheCU;
142
143 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
144 I = RegionMap.find(Context);
145 if (I != RegionMap.end()) {
146 llvm::Value *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000147 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000148 }
149
150 // Check namespace.
151 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000152 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153
David Blaikiebfa52742013-04-19 06:56:38 +0000154 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
155 if (!RDecl->isDependentType())
156 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Guy Benyei11169dd2012-12-18 14:30:41 +0000157 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 return TheCU;
159}
160
161/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000162/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000163/// is stored on the side.
164StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
165 assert (FD && "Invalid FunctionDecl!");
166 IdentifierInfo *FII = FD->getIdentifier();
167 FunctionTemplateSpecializationInfo *Info
168 = FD->getTemplateSpecializationInfo();
169 if (!Info && FII)
170 return FII->getName();
171
172 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000173 SmallString<128> NS;
174 llvm::raw_svector_ostream OS(NS);
175 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000176
177 // Add any template specialization args.
178 if (Info) {
179 const TemplateArgumentList *TArgs = Info->TemplateArguments;
180 const TemplateArgument *Args = TArgs->data();
181 unsigned NumArgs = TArgs->size();
182 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000183 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
184 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000185 }
186
187 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000188 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000189}
190
191StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
192 SmallString<256> MethodName;
193 llvm::raw_svector_ostream OS(MethodName);
194 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
195 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000196 if (const ObjCImplementationDecl *OID =
Guy Benyei11169dd2012-12-18 14:30:41 +0000197 dyn_cast<const ObjCImplementationDecl>(DC)) {
198 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000199 } else if (const ObjCInterfaceDecl *OID =
Guy Benyei11169dd2012-12-18 14:30:41 +0000200 dyn_cast<const ObjCInterfaceDecl>(DC)) {
201 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000202 } else if (const ObjCCategoryImplDecl *OCD =
Guy Benyei11169dd2012-12-18 14:30:41 +0000203 dyn_cast<const ObjCCategoryImplDecl>(DC)){
204 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
205 OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000206 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000207 // We can extract the type of the class from the self pointer.
208 if (ImplicitParamDecl* SelfDecl = OMD->getSelfDecl()) {
209 QualType ClassTy =
210 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
211 ClassTy.print(OS, PrintingPolicy(LangOptions()));
212 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 }
214 OS << ' ' << OMD->getSelector().getAsString() << ']';
215
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000216 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000217}
218
219/// getSelectorName - Return selector name. This is used for debugging
220/// info.
221StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000222 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000223}
224
225/// getClassName - Get class name including template argument list.
Eric Christopherb2a008c2013-05-16 00:45:12 +0000226StringRef
Guy Benyei11169dd2012-12-18 14:30:41 +0000227CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000228 // quick optimization to avoid having to intern strings that are already
229 // stored reliably elsewhere
230 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000231 return RD->getName();
232
David Blaikie65813a32014-04-02 18:21:09 +0000233 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000234 {
David Blaikie65813a32014-04-02 18:21:09 +0000235 llvm::raw_svector_ostream OS(Name);
236 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
237 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000238 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000239
240 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000241 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000242}
243
244/// getOrCreateFile - Get the file debug info descriptor for the input location.
245llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
246 if (!Loc.isValid())
247 // If Location is not valid then use main input file.
248 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
249
250 SourceManager &SM = CGM.getContext().getSourceManager();
251 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
252
253 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
254 // If the location is not valid then use main input file.
255 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
256
257 // Cache the results.
258 const char *fname = PLoc.getFilename();
259 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
260 DIFileCache.find(fname);
261
262 if (it != DIFileCache.end()) {
263 // Verify that the information still exists.
264 if (llvm::Value *V = it->second)
265 return llvm::DIFile(cast<llvm::MDNode>(V));
266 }
267
268 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
269
270 DIFileCache[fname] = F;
271 return F;
272}
273
274/// getOrCreateMainFile - Get the file info for main compile unit.
275llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
276 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
277}
278
279/// getLineNumber - Get line number for the location. If location is invalid
280/// then use current location.
281unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
282 if (Loc.isInvalid() && CurLoc.isInvalid())
283 return 0;
284 SourceManager &SM = CGM.getContext().getSourceManager();
285 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
286 return PLoc.isValid()? PLoc.getLine() : 0;
287}
288
289/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000290unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000291 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000292 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000293 return 0;
294
295 // If the location is invalid then use the current column.
296 if (Loc.isInvalid() && CurLoc.isInvalid())
297 return 0;
298 SourceManager &SM = CGM.getContext().getSourceManager();
299 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
300 return PLoc.isValid()? PLoc.getColumn() : 0;
301}
302
303StringRef CGDebugInfo::getCurrentDirname() {
304 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
305 return CGM.getCodeGenOpts().DebugCompilationDir;
306
307 if (!CWDName.empty())
308 return CWDName;
309 SmallString<256> CWD;
310 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000311 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000312}
313
314/// CreateCompileUnit - Create new compile unit.
315void CGDebugInfo::CreateCompileUnit() {
316
David Blaikieaabde052014-05-14 00:29:00 +0000317 // Should we be asking the SourceManager for the main file name, instead of
318 // accepting it as an argument? This just causes the main file name to
319 // mismatch with source locations and create extra lexical scopes or
320 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
321 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
322 // because that's what the SourceManager says)
323
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 // Get absolute path name.
325 SourceManager &SM = CGM.getContext().getSourceManager();
326 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
327 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000328 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000329
330 // The main file name provided via the "-main-file-name" option contains just
331 // the file name itself with no path information. This file name may have had
332 // a relative path, so we look into the actual file entry for the main
333 // file to determine the real absolute path for the file.
334 std::string MainFileDir;
335 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
336 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000337 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000338 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
339 llvm::sys::path::append(MainFileDirSS, MainFileName);
340 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000341 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000342 }
343
344 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000345 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000346
347 // Save split dwarf file string.
348 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000349 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000350
Ed Masteda706022014-05-07 12:49:30 +0000351 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000352 const LangOptions &LO = CGM.getLangOpts();
353 if (LO.CPlusPlus) {
354 if (LO.ObjC1)
355 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
356 else
357 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
358 } else if (LO.ObjC1) {
359 LangTag = llvm::dwarf::DW_LANG_ObjC;
360 } else if (LO.C99) {
361 LangTag = llvm::dwarf::DW_LANG_C99;
362 } else {
363 LangTag = llvm::dwarf::DW_LANG_C89;
364 }
365
366 std::string Producer = getClangFullVersion();
367
368 // Figure out which version of the ObjC runtime we have.
369 unsigned RuntimeVers = 0;
370 if (LO.ObjC1)
371 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
372
373 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000374 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000375 TheCU = DBuilder.createCompileUnit(
376 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
377 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000378 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000379 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000380 : llvm::DIBuilder::FullDebug,
381 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000382}
383
384/// CreateType - Get the Basic type from the cache or create a new
385/// one if necessary.
386llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000387 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000388 StringRef BTName;
389 switch (BT->getKind()) {
390#define BUILTIN_TYPE(Id, SingletonId)
391#define PLACEHOLDER_TYPE(Id, SingletonId) \
392 case BuiltinType::Id:
393#include "clang/AST/BuiltinTypes.def"
394 case BuiltinType::Dependent:
395 llvm_unreachable("Unexpected builtin type");
396 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000397 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000398 case BuiltinType::Void:
399 return llvm::DIType();
400 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000401 if (!ClassTy)
402 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
403 "objc_class", TheCU,
404 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000405 return ClassTy;
406 case BuiltinType::ObjCId: {
407 // typedef struct objc_class *Class;
408 // typedef struct objc_object {
409 // Class isa;
410 // } *id;
411
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000412 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 return ObjTy;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
417 "objc_class", TheCU,
418 getOrCreateMainFile(), 0);
419
420 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000421
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
423
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000424 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000425 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
426 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000427
Manman Ren51289892014-07-28 19:14:41 +0000428 ObjTy.setArrays(DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000429 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000430 return ObjTy;
431 }
432 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000433 if (!SelTy)
434 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
435 "objc_selector", TheCU,
436 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000437 return SelTy;
438 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000439
440 case BuiltinType::OCLImage1d:
441 return getOrCreateStructPtrType("opencl_image1d_t",
442 OCLImage1dDITy);
443 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000444 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000445 OCLImage1dArrayDITy);
446 case BuiltinType::OCLImage1dBuffer:
447 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
448 OCLImage1dBufferDITy);
449 case BuiltinType::OCLImage2d:
450 return getOrCreateStructPtrType("opencl_image2d_t",
451 OCLImage2dDITy);
452 case BuiltinType::OCLImage2dArray:
453 return getOrCreateStructPtrType("opencl_image2d_array_t",
454 OCLImage2dArrayDITy);
455 case BuiltinType::OCLImage3d:
456 return getOrCreateStructPtrType("opencl_image3d_t",
457 OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000458 case BuiltinType::OCLSampler:
459 return DBuilder.createBasicType("opencl_sampler_t",
460 CGM.getContext().getTypeSize(BT),
461 CGM.getContext().getTypeAlign(BT),
462 llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000463 case BuiltinType::OCLEvent:
464 return getOrCreateStructPtrType("opencl_event_t",
465 OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000466
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::UChar:
468 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
469 case BuiltinType::Char_S:
470 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
471 case BuiltinType::Char16:
472 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
473 case BuiltinType::UShort:
474 case BuiltinType::UInt:
475 case BuiltinType::UInt128:
476 case BuiltinType::ULong:
477 case BuiltinType::WChar_U:
478 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
479 case BuiltinType::Short:
480 case BuiltinType::Int:
481 case BuiltinType::Int128:
482 case BuiltinType::Long:
483 case BuiltinType::WChar_S:
484 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
485 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
486 case BuiltinType::Half:
487 case BuiltinType::Float:
488 case BuiltinType::LongDouble:
489 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
490 }
491
492 switch (BT->getKind()) {
493 case BuiltinType::Long: BTName = "long int"; break;
494 case BuiltinType::LongLong: BTName = "long long int"; break;
495 case BuiltinType::ULong: BTName = "long unsigned int"; break;
496 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
497 default:
498 BTName = BT->getName(CGM.getLangOpts());
499 break;
500 }
501 // Bit size, align and offset of the type.
502 uint64_t Size = CGM.getContext().getTypeSize(BT);
503 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000504 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 DBuilder.createBasicType(BTName, Size, Align, Encoding);
506 return DbgTy;
507}
508
509llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
510 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000511 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000512 if (Ty->isComplexIntegerType())
513 Encoding = llvm::dwarf::DW_ATE_lo_user;
514
515 uint64_t Size = CGM.getContext().getTypeSize(Ty);
516 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000517 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 DBuilder.createBasicType("complex", Size, Align, Encoding);
519
520 return DbgTy;
521}
522
523/// CreateCVRType - Get the qualified type from the cache or create
524/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000525llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 QualifierCollector Qc;
527 const Type *T = Qc.strip(Ty);
528
529 // Ignore these qualifiers for now.
530 Qc.removeObjCGCAttr();
531 Qc.removeAddressSpace();
532 Qc.removeObjCLifetime();
533
534 // We will create one Derived type for one qualifier and recurse to handle any
535 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000536 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 if (Qc.hasConst()) {
538 Tag = llvm::dwarf::DW_TAG_const_type;
539 Qc.removeConst();
540 } else if (Qc.hasVolatile()) {
541 Tag = llvm::dwarf::DW_TAG_volatile_type;
542 Qc.removeVolatile();
543 } else if (Qc.hasRestrict()) {
544 Tag = llvm::dwarf::DW_TAG_restrict_type;
545 Qc.removeRestrict();
546 } else {
547 assert(Qc.empty() && "Unknown type qualifier for debug info");
548 return getOrCreateType(QualType(T, 0), Unit);
549 }
550
David Blaikie99dab3b2013-09-04 22:03:57 +0000551 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000552
553 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
554 // CVR derived types.
555 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000556
Guy Benyei11169dd2012-12-18 14:30:41 +0000557 return DbgTy;
558}
559
560llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
561 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000562
563 // The frontend treats 'id' as a typedef to an ObjCObjectType,
564 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
565 // debug info, we want to emit 'id' in both cases.
566 if (Ty->isObjCQualifiedIdType())
567 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
568
Guy Benyei11169dd2012-12-18 14:30:41 +0000569 llvm::DIType DbgTy =
Eric Christopherb2a008c2013-05-16 00:45:12 +0000570 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 Ty->getPointeeType(), Unit);
572 return DbgTy;
573}
574
575llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
576 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000577 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000578 Ty->getPointeeType(), Unit);
579}
580
Manman Rene0064d82013-08-29 23:19:58 +0000581/// In C++ mode, types have linkage, so we can rely on the ODR and
582/// on their mangled names, if they're external.
583static SmallString<256>
584getUniqueTagTypeName(const TagType *Ty, CodeGenModule &CGM,
585 llvm::DICompileUnit TheCU) {
586 SmallString<256> FullName;
587 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
588 // For now, only apply ODR with C++.
589 const TagDecl *TD = Ty->getDecl();
590 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
591 !TD->isExternallyVisible())
592 return FullName;
593 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
594 if (CGM.getTarget().getCXXABI().isMicrosoft())
595 return FullName;
596
597 // TODO: This is using the RTTI name. Is there a better way to get
598 // a unique string for a type?
599 llvm::raw_svector_ostream Out(FullName);
600 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
601 Out.flush();
602 return FullName;
603}
604
Guy Benyei11169dd2012-12-18 14:30:41 +0000605// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000606llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000607CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000608 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000609 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000610 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000611 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
613 unsigned Line = getLineNumber(RD->getLocation());
614 StringRef RDName = getClassName(RD);
615
Ed Masteda706022014-05-07 12:49:30 +0000616 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 if (RD->isStruct() || RD->isInterface())
618 Tag = llvm::dwarf::DW_TAG_structure_type;
619 else if (RD->isUnion())
620 Tag = llvm::dwarf::DW_TAG_union_type;
621 else {
622 assert(RD->isClass());
623 Tag = llvm::dwarf::DW_TAG_class_type;
624 }
625
626 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000627 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000628 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
629 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
630 ReplaceMap.push_back(std::make_pair(Ty, static_cast<llvm::Value *>(RetTy)));
631 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000632}
633
Ed Masteda706022014-05-07 12:49:30 +0000634llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000635 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 QualType PointeeTy,
637 llvm::DIFile Unit) {
638 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
639 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000640 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000641
Guy Benyei11169dd2012-12-18 14:30:41 +0000642 // Bit size, align and offset of the type.
643 // Size is always the size of a pointer. We can't use getTypeSize here
644 // because that does not return the correct value for references.
645 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000646 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000647 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
648
David Blaikie99dab3b2013-09-04 22:03:57 +0000649 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
650 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000651}
652
Eric Christopher0fdcb312013-05-16 00:52:20 +0000653llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
654 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000655 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000656 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000657 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
658 TheCU, getOrCreateMainFile(), 0);
659 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
660 Cache = DBuilder.createPointerType(Cache, Size);
661 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000662}
663
Guy Benyei11169dd2012-12-18 14:30:41 +0000664llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
665 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000666 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000667 return BlockLiteralGeneric;
668
669 SmallVector<llvm::Value *, 8> EltTys;
670 llvm::DIType FieldTy;
671 QualType FType;
672 uint64_t FieldSize, FieldOffset;
673 unsigned FieldAlign;
674 llvm::DIArray Elements;
675 llvm::DIType EltTy, DescTy;
676
677 FieldOffset = 0;
678 FType = CGM.getContext().UnsignedLongTy;
679 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
680 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
681
682 Elements = DBuilder.getOrCreateArray(EltTys);
683 EltTys.clear();
684
685 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
686 unsigned LineNo = getLineNumber(CurLoc);
687
688 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
689 Unit, LineNo, FieldOffset, 0,
David Blaikie6d4fe152013-02-25 01:07:08 +0000690 Flags, llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000691
692 // Bit size, align and offset of the type.
693 uint64_t Size = CGM.getContext().getTypeSize(Ty);
694
695 DescTy = DBuilder.createPointerType(EltTy, Size);
696
697 FieldOffset = 0;
698 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
699 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
700 FType = CGM.getContext().IntTy;
701 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
702 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
703 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
704 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
705
706 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
707 FieldTy = DescTy;
708 FieldSize = CGM.getContext().getTypeSize(Ty);
709 FieldAlign = CGM.getContext().getTypeAlign(Ty);
710 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
711 LineNo, FieldSize, FieldAlign,
712 FieldOffset, 0, FieldTy);
713 EltTys.push_back(FieldTy);
714
715 FieldOffset += FieldSize;
716 Elements = DBuilder.getOrCreateArray(EltTys);
717
718 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
719 Unit, LineNo, FieldOffset, 0,
David Blaikie6d4fe152013-02-25 01:07:08 +0000720 Flags, llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
723 return BlockLiteralGeneric;
724}
725
David Blaikief1b382e2014-04-06 17:14:06 +0000726llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, llvm::DIFile Unit) {
727 assert(Ty->isTypeAlias());
728 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000729
730 SmallString<128> NS;
731 llvm::raw_svector_ostream OS(NS);
732 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(), /*qualified*/ false);
733
734 TemplateSpecializationType::PrintTemplateArgumentList(
735 OS, Ty->getArgs(), Ty->getNumArgs(),
736 CGM.getContext().getPrintingPolicy());
737
738 TypeAliasDecl *AliasDecl =
739 cast<TypeAliasTemplateDecl>(Ty->getTemplateName().getAsTemplateDecl())
740 ->getTemplatedDecl();
741
742 SourceLocation Loc = AliasDecl->getLocation();
743 llvm::DIFile File = getOrCreateFile(Loc);
744 unsigned Line = getLineNumber(Loc);
745
746 llvm::DIDescriptor Ctxt = getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
747
748 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
749}
750
David Blaikie99dab3b2013-09-04 22:03:57 +0000751llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000752 // Typedefs are derived from some other type. If we have a typedef of a
753 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000754 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000755 // We don't set size information, but do specify where the typedef was
756 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000757 SourceLocation Loc = Ty->getDecl()->getLocation();
758 llvm::DIFile File = getOrCreateFile(Loc);
759 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000761
Guy Benyei11169dd2012-12-18 14:30:41 +0000762 llvm::DIDescriptor TypedefContext =
763 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000764
Guy Benyei11169dd2012-12-18 14:30:41 +0000765 return
Adrian Prantl3eff2252014-01-21 18:42:27 +0000766 DBuilder.createTypedef(Src, TyDecl->getName(), File, Line, TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000767}
768
769llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
770 llvm::DIFile Unit) {
771 SmallVector<llvm::Value *, 16> EltTys;
772
773 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000774 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000775
776 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000777 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 if (isa<FunctionNoProtoType>(Ty))
779 EltTys.push_back(DBuilder.createUnspecifiedParameter());
780 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000781 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
782 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000783 if (FPT->isVariadic())
784 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000785 }
786
Manman Ren67f005e2014-07-28 22:24:34 +0000787 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000788 return DBuilder.createSubroutineType(Unit, EltTypeArray);
789}
790
Adrian Prantl21361fb2014-08-29 22:44:27 +0000791/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
792/// As an optimization, return 0 if the access specifier equals the
793/// default for the containing type.
794static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
795 AccessSpecifier Default = clang::AS_none;
796 if (RD && RD->isClass())
797 Default = clang::AS_private;
798 else if (RD && (RD->isStruct() || RD->isUnion()))
799 Default = clang::AS_public;
800
801 if (Access == Default)
802 return 0;
803
804 switch(Access) {
805 case clang::AS_private: return llvm::DIDescriptor::FlagPrivate;
806 case clang::AS_protected: return llvm::DIDescriptor::FlagProtected;
807 case clang::AS_public: return llvm::DIDescriptor::FlagPublic;
808 case clang::AS_none: return 0;
809 }
810 llvm_unreachable("unexpected access enumerator");
811}
Guy Benyei11169dd2012-12-18 14:30:41 +0000812
Guy Benyei11169dd2012-12-18 14:30:41 +0000813llvm::DIType CGDebugInfo::createFieldType(StringRef name,
814 QualType type,
815 uint64_t sizeInBitsOverride,
816 SourceLocation loc,
817 AccessSpecifier AS,
818 uint64_t offsetInBits,
819 llvm::DIFile tunit,
Adrian Prantl21361fb2014-08-29 22:44:27 +0000820 llvm::DIScope scope,
821 const RecordDecl* RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000822 llvm::DIType debugType = getOrCreateType(type, tunit);
823
824 // Get the location for the field.
825 llvm::DIFile file = getOrCreateFile(loc);
826 unsigned line = getLineNumber(loc);
827
David Majnemer34b57492014-07-30 01:30:47 +0000828 uint64_t SizeInBits = 0;
829 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000831 TypeInfo TI = CGM.getContext().getTypeInfo(type);
832 SizeInBits = TI.Width;
833 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000834
835 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000836 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 }
838
Adrian Prantl21361fb2014-08-29 22:44:27 +0000839 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000840 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
841 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000842}
843
Eric Christopher91a31902013-01-16 01:22:32 +0000844/// CollectRecordLambdaFields - Helper for CollectRecordFields.
845void CGDebugInfo::
846CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
847 SmallVectorImpl<llvm::Value *> &elements,
848 llvm::DIType RecordTy) {
849 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
850 // has the name and the location of the variable so we should iterate over
851 // both concurrently.
852 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
853 RecordDecl::field_iterator Field = CXXDecl->field_begin();
854 unsigned fieldno = 0;
855 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
856 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000857 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000858 if (C.capturesVariable()) {
859 VarDecl *V = C.getCapturedVar();
860 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
861 StringRef VName = V->getName();
862 uint64_t SizeInBitsOverride = 0;
863 if (Field->isBitField()) {
864 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
865 assert(SizeInBitsOverride && "found named 0-width bitfield");
866 }
867 llvm::DIType fieldType
868 = createFieldType(VName, Field->getType(), SizeInBitsOverride,
869 C.getLocation(), Field->getAccess(),
Adrian Prantl21361fb2014-08-29 22:44:27 +0000870 layout.getFieldOffset(fieldno), VUnit, RecordTy,
871 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000872 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000873 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000874 // TODO: Need to handle 'this' in some way by probably renaming the
875 // this of the lambda class and having a field member of 'this' or
876 // by using AT_object_pointer for the function and having that be
877 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000878 FieldDecl *f = *Field;
879 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
880 QualType type = f->getType();
881 llvm::DIType fieldType
882 = createFieldType("this", type, 0, f->getLocation(), f->getAccess(),
Adrian Prantl21361fb2014-08-29 22:44:27 +0000883 layout.getFieldOffset(fieldno), VUnit, RecordTy,
884 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000885
886 elements.push_back(fieldType);
887 }
888 }
889}
890
David Blaikie6943dea2013-08-20 01:28:15 +0000891/// Helper for CollectRecordFields.
David Blaikieae019462013-08-15 22:50:29 +0000892llvm::DIDerivedType
893CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
Adrian Prantl21361fb2014-08-29 22:44:27 +0000894 llvm::DIType RecordTy,
895 const RecordDecl* RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000896 // Create the descriptor for the static variable, with or without
897 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000898 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000899 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
900 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
901
Eric Christopher91a31902013-01-16 01:22:32 +0000902 unsigned LineNumber = getLineNumber(Var->getLocation());
903 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000904 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000905 if (Var->getInit()) {
906 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000907 if (Value) {
908 if (Value->isInt())
909 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
910 if (Value->isFloat())
911 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
912 }
Eric Christopher91a31902013-01-16 01:22:32 +0000913 }
914
Adrian Prantl21361fb2014-08-29 22:44:27 +0000915 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000916 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
917 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Eric Christopher91a31902013-01-16 01:22:32 +0000918 StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV);
David Blaikieae019462013-08-15 22:50:29 +0000919 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000920}
921
922/// CollectRecordNormalField - Helper for CollectRecordFields.
923void CGDebugInfo::
924CollectRecordNormalField(const FieldDecl *field, uint64_t OffsetInBits,
925 llvm::DIFile tunit,
926 SmallVectorImpl<llvm::Value *> &elements,
Adrian Prantl21361fb2014-08-29 22:44:27 +0000927 llvm::DIType RecordTy,
928 const RecordDecl* RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000929 StringRef name = field->getName();
930 QualType type = field->getType();
931
932 // Ignore unnamed fields unless they're anonymous structs/unions.
933 if (name.empty() && !type->isRecordType())
934 return;
935
936 uint64_t SizeInBitsOverride = 0;
937 if (field->isBitField()) {
938 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
939 assert(SizeInBitsOverride && "found named 0-width bitfield");
940 }
941
942 llvm::DIType fieldType
943 = createFieldType(name, type, SizeInBitsOverride,
944 field->getLocation(), field->getAccess(),
Adrian Prantl21361fb2014-08-29 22:44:27 +0000945 OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000946
947 elements.push_back(fieldType);
948}
949
Guy Benyei11169dd2012-12-18 14:30:41 +0000950/// CollectRecordFields - A helper function to collect debug info for
951/// record fields. This is used while creating debug info entry for a Record.
David Blaikieab255bb2013-08-16 20:40:25 +0000952void CGDebugInfo::CollectRecordFields(const RecordDecl *record,
953 llvm::DIFile tunit,
954 SmallVectorImpl<llvm::Value *> &elements,
955 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
957
Eric Christopher91a31902013-01-16 01:22:32 +0000958 if (CXXDecl && CXXDecl->isLambda())
959 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
960 else {
961 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000962
Eric Christopher91a31902013-01-16 01:22:32 +0000963 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000964 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000965
Eric Christopher91a31902013-01-16 01:22:32 +0000966 // Static and non-static members should appear in the same order as
967 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000968 for (const auto *I : record->decls())
969 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000970 // Reuse the existing static member declaration if one exists
971 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI =
972 StaticDataMemberCache.find(V->getCanonicalDecl());
973 if (MI != StaticDataMemberCache.end()) {
974 assert(MI->second &&
975 "Static data member declaration should still exist");
976 elements.push_back(
977 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000978 } else {
979 auto Field = CreateRecordStaticField(V, RecordTy, record);
980 elements.push_back(Field);
981 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000982 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christopher91a31902013-01-16 01:22:32 +0000983 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo),
Adrian Prantl21361fb2014-08-29 22:44:27 +0000984 tunit, elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000985
986 // Bump field number for next field.
987 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000988 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000989 }
990}
991
992/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
993/// function type is not updated to include implicit "this" pointer. Use this
994/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +0000995llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +0000996CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
997 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +0000998 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +0000999 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001000 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001001 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1002 Func, Unit);
1003}
David Blaikie2aaf0652013-01-07 22:24:59 +00001004
David Blaikie469f0792013-05-22 23:22:42 +00001005llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001006 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001007 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001008 llvm::DITypeArray Args = llvm::DISubroutineType(
1009 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 assert (Args.getNumElements() && "Invalid number of arguments!");
1011
1012 SmallVector<llvm::Value *, 16> Elts;
1013
1014 // First element is always return type. For 'void' functions it is NULL.
1015 Elts.push_back(Args.getElement(0));
1016
David Blaikie2aaf0652013-01-07 22:24:59 +00001017 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001018 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001019 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1020 // Create pointer type directly in this case.
1021 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1022 QualType PointeeTy = ThisPtrTy->getPointeeType();
1023 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001024 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001025 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1026 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001027 llvm::DIType ThisPtrType =
1028 DBuilder.createPointerType(PointeeType, Size, Align);
David Blaikie2aaf0652013-01-07 22:24:59 +00001029 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1030 // TODO: This and the artificial type below are misleading, the
1031 // types aren't artificial the argument is, but the current
1032 // metadata doesn't represent that.
1033 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1034 Elts.push_back(ThisPtrType);
1035 } else {
1036 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
1037 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1038 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1039 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001040 }
1041
1042 // Copy rest of the arguments.
1043 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1044 Elts.push_back(Args.getElement(i));
1045
Manman Ren67f005e2014-07-28 22:24:34 +00001046 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001047
Adrian Prantl0630eb72013-12-18 21:48:18 +00001048 unsigned Flags = 0;
1049 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1050 Flags |= llvm::DIDescriptor::FlagLValueReference;
1051 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1052 Flags |= llvm::DIDescriptor::FlagRValueReference;
1053
1054 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055}
1056
Eric Christopherb2a008c2013-05-16 00:45:12 +00001057/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001058/// inside a function.
1059static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1060 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1061 return isFunctionLocalClass(NRD);
1062 if (isa<FunctionDecl>(RD->getDeclContext()))
1063 return true;
1064 return false;
1065}
1066
1067/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1068/// a single member function GlobalDecl.
1069llvm::DISubprogram
1070CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
1071 llvm::DIFile Unit,
1072 llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001073 bool IsCtorOrDtor =
Guy Benyei11169dd2012-12-18 14:30:41 +00001074 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001075
Guy Benyei11169dd2012-12-18 14:30:41 +00001076 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001077 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001078
1079 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1080 // make sense to give a single ctor/dtor a linkage name.
1081 StringRef MethodLinkageName;
1082 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1083 MethodLinkageName = CGM.getMangledName(Method);
1084
1085 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001086 llvm::DIFile MethodDefUnit;
1087 unsigned MethodLine = 0;
1088 if (!Method->isImplicit()) {
1089 MethodDefUnit = getOrCreateFile(Method->getLocation());
1090 MethodLine = getLineNumber(Method->getLocation());
1091 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001092
1093 // Collect virtual method info.
1094 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001095 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001097
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 if (Method->isVirtual()) {
1099 if (Method->isPure())
1100 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1101 else
1102 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 // It doesn't make sense to give a virtual destructor a vtable index,
1105 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001106 // FIXME: Add proper support for debug info for virtual calls in
1107 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1108 // lookup if we have multiple or virtual inheritance.
1109 if (!isa<CXXDestructorDecl>(Method) &&
1110 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001111 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 ContainingType = RecordTy;
1113 }
1114
1115 unsigned Flags = 0;
1116 if (Method->isImplicit())
1117 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001118 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001119 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1120 if (CXXC->isExplicit())
1121 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001122 } else if (const CXXConversionDecl *CXXC =
Guy Benyei11169dd2012-12-18 14:30:41 +00001123 dyn_cast<CXXConversionDecl>(Method)) {
1124 if (CXXC->isExplicit())
1125 Flags |= llvm::DIDescriptor::FlagExplicit;
1126 }
1127 if (Method->hasPrototype())
1128 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001129 if (Method->getRefQualifier() == RQ_LValue)
1130 Flags |= llvm::DIDescriptor::FlagLValueReference;
1131 if (Method->getRefQualifier() == RQ_RValue)
1132 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001133
1134 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1135 llvm::DISubprogram SP =
Eric Christopherb2a008c2013-05-16 00:45:12 +00001136 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Guy Benyei11169dd2012-12-18 14:30:41 +00001137 MethodDefUnit, MethodLine,
Eric Christopherb2a008c2013-05-16 00:45:12 +00001138 MethodTy, /*isLocalToUnit=*/false,
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 /* isDefinition=*/ false,
1140 Virtuality, VIndex, ContainingType,
Craig Topper8a13c412014-05-21 05:09:00 +00001141 Flags, CGM.getLangOpts().Optimize, nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +00001142 TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001143
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
1145
1146 return SP;
1147}
1148
1149/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001150/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001151/// a Record.
1152void CGDebugInfo::
1153CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
1154 SmallVectorImpl<llvm::Value *> &EltTys,
1155 llvm::DIType RecordTy) {
1156
1157 // Since we want more than just the individual member decls if we
1158 // have templated functions iterate over every declaration to gather
1159 // the functions.
Aaron Ballman629afae2014-03-07 19:56:05 +00001160 for(const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001161 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1162 // If the member is implicit, don't add it to the member list. This avoids
1163 // the member being added to type units by LLVM, while still allowing it
1164 // to be emitted into the type declaration/reference inside the compile
1165 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001166 // FIXME: Handle Using(Shadow?)Decls here to create
1167 // DW_TAG_imported_declarations inside the class for base decls brought into
1168 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1169 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1170 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001171 if (!Method || Method->isImplicit())
1172 continue;
1173 // Reuse the existing member function declaration if it exists.
1174 // It may be associated with the declaration of the type & should be
1175 // reused as we're building the definition.
1176 //
1177 // This situation can arise in the vtable-based debug info reduction where
1178 // implicit members are emitted in a non-vtable TU.
1179 auto MI = SPCache.find(Method->getCanonicalDecl());
1180 EltTys.push_back(MI == SPCache.end()
1181 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Aaron Ballmand2a3f472014-10-06 12:42:31 +00001182 : static_cast<llvm::Value *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001183 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001184}
Guy Benyei11169dd2012-12-18 14:30:41 +00001185
Guy Benyei11169dd2012-12-18 14:30:41 +00001186/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001187/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001188/// a Record.
1189void CGDebugInfo::
1190CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
1191 SmallVectorImpl<llvm::Value *> &EltTys,
1192 llvm::DIType RecordTy) {
1193
1194 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001195 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001196 unsigned BFlags = 0;
1197 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001198
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00001200 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001201
Aaron Ballman574705e2014-03-13 15:41:46 +00001202 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001203 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1204 // virtual base offset offset is -ve. The code generator emits dwarf
1205 // expression where it expects +ve number.
1206 BaseOffset =
1207 0 - CGM.getItaniumVTableContext()
1208 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
1209 } else {
1210 // In the MS ABI, store the vbtable offset, which is analogous to the
1211 // vbase offset offset in Itanium.
1212 BaseOffset =
1213 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1214 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 BFlags = llvm::DIDescriptor::FlagVirtual;
1216 } else
1217 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1218 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1219 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001220
Adrian Prantl21361fb2014-08-29 22:44:27 +00001221 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001222 llvm::DIType DTy =
1223 DBuilder.createInheritance(RecordTy,
Aaron Ballman574705e2014-03-13 15:41:46 +00001224 getOrCreateType(BI.getType(), Unit),
Guy Benyei11169dd2012-12-18 14:30:41 +00001225 BaseOffset, BFlags);
1226 EltTys.push_back(DTy);
1227 }
1228}
1229
1230/// CollectTemplateParams - A helper function to collect template parameters.
1231llvm::DIArray CGDebugInfo::
1232CollectTemplateParams(const TemplateParameterList *TPList,
David Blaikie47c11502013-06-22 18:59:18 +00001233 ArrayRef<TemplateArgument> TAList,
Guy Benyei11169dd2012-12-18 14:30:41 +00001234 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001235 SmallVector<llvm::Value *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001236 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1237 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001238 StringRef Name;
1239 if (TPList)
1240 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001241 switch (TA.getKind()) {
1242 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1244 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001245 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001247 } break;
1248 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1250 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001251 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001252 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001253 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1254 TemplateParams.push_back(TVP);
1255 } break;
1256 case TemplateArgument::Declaration: {
1257 const ValueDecl *D = TA.getAsDecl();
1258 bool InstanceMember = D->isCXXInstanceMember();
1259 QualType T = InstanceMember
1260 ? CGM.getContext().getMemberPointerType(
1261 D->getType(), cast<RecordDecl>(D->getDeclContext())
1262 ->getTypeForDecl())
1263 : CGM.getContext().getPointerType(D->getType());
1264 llvm::DIType TTy = getOrCreateType(T, Unit);
Craig Topper8a13c412014-05-21 05:09:00 +00001265 llvm::Value *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001266 // Variable pointer template parameters have a value that is the address
1267 // of the variable.
1268 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
1269 V = CGM.GetAddrOfGlobalVar(VD);
1270 // Member function pointers have special support for building them, though
1271 // this is currently unsupported in LLVM CodeGen.
David Blaikied900f982013-05-13 06:57:50 +00001272 if (InstanceMember) {
David Blaikie38079fd2013-05-10 21:53:14 +00001273 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(D))
1274 V = CGM.getCXXABI().EmitMemberPointer(method);
David Blaikied900f982013-05-13 06:57:50 +00001275 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1276 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001277 // Member data pointers have special handling too to compute the fixed
1278 // offset within the object.
Adrian Prantlefb88052014-04-01 17:52:06 +00001279 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) {
David Blaikie38079fd2013-05-10 21:53:14 +00001280 // These five lines (& possibly the above member function pointer
1281 // handling) might be able to be refactored to use similar code in
1282 // CodeGenModule::getMemberPointerConstant
1283 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1284 CharUnits chars =
1285 CGM.getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1286 V = CGM.getCXXABI().EmitMemberDataPointer(
1287 cast<MemberPointerType>(T.getTypePtr()), chars);
1288 }
1289 llvm::DITemplateValueParameter TVP =
David Majnemera3644d62013-08-25 22:13:27 +00001290 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1291 V->stripPointerCasts());
David Blaikie38079fd2013-05-10 21:53:14 +00001292 TemplateParams.push_back(TVP);
1293 } break;
1294 case TemplateArgument::NullPtr: {
1295 QualType T = TA.getNullPtrType();
1296 llvm::DIType TTy = getOrCreateType(T, Unit);
Craig Topper8a13c412014-05-21 05:09:00 +00001297 llvm::Value *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001298 // Special case member data pointer null values since they're actually -1
1299 // instead of zero.
1300 if (const MemberPointerType *MPT =
1301 dyn_cast<MemberPointerType>(T.getTypePtr()))
1302 // But treat member function pointers as simple zero integers because
1303 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1304 // CodeGen grows handling for values of non-null member function
1305 // pointers then perhaps we could remove this special case and rely on
1306 // EmitNullMemberPointer for member function pointers.
1307 if (MPT->isMemberDataPointer())
1308 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1309 if (!V)
1310 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1311 llvm::DITemplateValueParameter TVP =
David Blaikie47c11502013-06-22 18:59:18 +00001312 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie38079fd2013-05-10 21:53:14 +00001313 TemplateParams.push_back(TVP);
1314 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001315 case TemplateArgument::Template: {
1316 llvm::DITemplateValueParameter TVP =
1317 DBuilder.createTemplateTemplateParameter(
1318 TheCU, Name, llvm::DIType(),
1319 TA.getAsTemplate().getAsTemplateDecl()
1320 ->getQualifiedNameAsString());
1321 TemplateParams.push_back(TVP);
1322 } break;
1323 case TemplateArgument::Pack: {
1324 llvm::DITemplateValueParameter TVP =
1325 DBuilder.createTemplateParameterPack(
1326 TheCU, Name, llvm::DIType(),
Craig Topper8a13c412014-05-21 05:09:00 +00001327 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001328 TemplateParams.push_back(TVP);
1329 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001330 case TemplateArgument::Expression: {
1331 const Expr *E = TA.getAsExpr();
1332 QualType T = E->getType();
1333 llvm::Value *V = CGM.EmitConstantExpr(E, T);
1334 assert(V && "Expression in template argument isn't constant");
1335 llvm::DIType TTy = getOrCreateType(T, Unit);
1336 llvm::DITemplateValueParameter TVP =
1337 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1338 V->stripPointerCasts());
1339 TemplateParams.push_back(TVP);
1340 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001341 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001342 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::Null:
1344 llvm_unreachable(
1345 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001346 }
1347 }
1348 return DBuilder.getOrCreateArray(TemplateParams);
1349}
1350
1351/// CollectFunctionTemplateParams - A helper function to collect debug
1352/// info for function template parameters.
1353llvm::DIArray CGDebugInfo::
1354CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
1355 if (FD->getTemplatedKind() ==
1356 FunctionDecl::TK_FunctionTemplateSpecialization) {
1357 const TemplateParameterList *TList =
1358 FD->getTemplateSpecializationInfo()->getTemplate()
1359 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001360 return CollectTemplateParams(
1361 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
1363 return llvm::DIArray();
1364}
1365
1366/// CollectCXXTemplateParams - A helper function to collect debug info for
1367/// template parameters.
1368llvm::DIArray CGDebugInfo::
1369CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1370 llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001371 // Always get the full list of parameters, not just the ones from
1372 // the specialization.
1373 TemplateParameterList *TPList =
1374 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001375 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001376 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001377}
1378
1379/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1380llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1381 if (VTablePtrType.isValid())
1382 return VTablePtrType;
1383
1384 ASTContext &Context = CGM.getContext();
1385
1386 /* Function type */
1387 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001388 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001389 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1390 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
1391 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
1392 "__vtbl_ptr_type");
1393 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1394 return VTablePtrType;
1395}
1396
1397/// getVTableName - Get vtable name for the given Class.
1398StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001399 // Copy the gdb compatible name on the side and use its reference.
1400 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001401}
1402
1403
1404/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1405/// debug info entry in EltTys vector.
1406void CGDebugInfo::
1407CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1408 SmallVectorImpl<llvm::Value *> &EltTys) {
1409 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1410
1411 // If there is a primary base then it will hold vtable info.
1412 if (RL.getPrimaryBase())
1413 return;
1414
1415 // If this class is not dynamic then there is not any vtable info to collect.
1416 if (!RD->isDynamicClass())
1417 return;
1418
1419 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1420 llvm::DIType VPTR
1421 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Eric Christopher0fdcb312013-05-16 00:52:20 +00001422 0, Size, 0, 0,
1423 llvm::DIDescriptor::FlagArtificial,
Guy Benyei11169dd2012-12-18 14:30:41 +00001424 getOrCreateVTablePtrType(Unit));
1425 EltTys.push_back(VPTR);
1426}
1427
Eric Christopherb2a008c2013-05-16 00:45:12 +00001428/// getOrCreateRecordType - Emit record type's standalone debug info.
1429llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001430 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001431 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1433 return T;
1434}
1435
1436/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1437/// debug info.
1438llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001439 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001440 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001441 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001442 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 return T;
1444}
1445
David Blaikie483a9da2014-05-06 18:35:21 +00001446void CGDebugInfo::completeType(const EnumDecl *ED) {
1447 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1448 return;
1449 QualType Ty = CGM.getContext().getEnumType(ED);
1450 void* TyPtr = Ty.getAsOpaquePtr();
1451 auto I = TypeCache.find(TyPtr);
1452 if (I == TypeCache.end() ||
1453 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1454 .isForwardDecl())
1455 return;
1456 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1457 assert(!Res.isForwardDecl());
1458 TypeCache[TyPtr] = Res;
1459}
1460
David Blaikieb2e86eb2013-08-15 20:49:17 +00001461void CGDebugInfo::completeType(const RecordDecl *RD) {
1462 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1463 !CGM.getLangOpts().CPlusPlus)
1464 completeRequiredType(RD);
1465}
1466
1467void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001468 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1469 return;
1470
David Blaikie6943dea2013-08-20 01:28:15 +00001471 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1472 if (CXXDecl->isDynamicClass())
1473 return;
1474
David Blaikieb2e86eb2013-08-15 20:49:17 +00001475 QualType Ty = CGM.getContext().getRecordType(RD);
1476 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001477 if (T && T.isForwardDecl())
1478 completeClassData(RD);
1479}
1480
1481void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1482 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001483 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001484 QualType Ty = CGM.getContext().getRecordType(RD);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001485 void* TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001486 auto I = TypeCache.find(TyPtr);
1487 if (I != TypeCache.end() &&
1488 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1489 .isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001490 return;
1491 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1492 assert(!Res.isForwardDecl());
David Blaikieb2e86eb2013-08-15 20:49:17 +00001493 TypeCache[TyPtr] = Res;
1494}
1495
David Blaikie0e716b42014-03-03 23:48:23 +00001496static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1497 CXXRecordDecl::method_iterator End) {
1498 for (; I != End; ++I)
1499 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001500 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1501 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001502 return true;
1503 return false;
1504}
1505
1506static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1507 const RecordDecl *RD,
1508 const LangOptions &LangOpts) {
1509 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1510 return false;
1511
1512 if (!LangOpts.CPlusPlus)
1513 return false;
1514
1515 if (!RD->isCompleteDefinitionRequired())
1516 return true;
1517
1518 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1519
1520 if (!CXXDecl)
1521 return false;
1522
1523 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1524 return true;
1525
1526 TemplateSpecializationKind Spec = TSK_Undeclared;
1527 if (const ClassTemplateSpecializationDecl *SD =
1528 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1529 Spec = SD->getSpecializationKind();
1530
1531 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1532 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1533 CXXDecl->method_end()))
1534 return true;
1535
1536 return false;
1537}
1538
Guy Benyei11169dd2012-12-18 14:30:41 +00001539/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001540llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001541 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001542 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001543 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001544 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001545 T = getOrCreateRecordFwdDecl(
1546 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001547 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001548 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001549
David Blaikieb2e86eb2013-08-15 20:49:17 +00001550 return CreateTypeDefinition(Ty);
1551}
1552
1553llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1554 RecordDecl *RD = Ty->getDecl();
1555
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 // Get overall information about the record type for the debug info.
1557 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1558
1559 // Records and classes and unions can all be recursive. To handle them, we
1560 // first generate a debug descriptor for the struct as a forward declaration.
1561 // Then (if it is a definition) we go through and get debug info for all of
1562 // its members. Finally, we create a descriptor for the complete type (which
1563 // may refer to the forward decl if the struct is recursive) and replace all
1564 // uses of the forward declaration with the final definition.
1565
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001566 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001567 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001568 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001569
1570 if (FwdDecl.isForwardDecl())
1571 return FwdDecl;
1572
David Blaikieadfbf992013-08-18 16:55:33 +00001573 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1574 CollectContainingType(CXXDecl, FwdDecl);
1575
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001577 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001578 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1579
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 // Convert all the elements.
1581 SmallVector<llvm::Value *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001582 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
1584 // Note: The split of CXXDecl information here is intentional, the
1585 // gdb tests will depend on a certain ordering at printout. The debug
1586 // information offsets are still correct if we merge them all together
1587 // though.
1588 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1589 if (CXXDecl) {
1590 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1591 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1592 }
1593
Eric Christopher91a31902013-01-16 01:22:32 +00001594 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001596 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
1599 LexicalBlockStack.pop_back();
1600 RegionMap.erase(Ty->getDecl());
1601
1602 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001603 FwdDecl.setArrays(Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001604
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001605 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1606 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001607}
1608
1609/// CreateType - get objective-c object type.
1610llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1611 llvm::DIFile Unit) {
1612 // Ignore protocols.
1613 return getOrCreateType(Ty->getBaseType(), Unit);
1614}
1615
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001616
1617/// \return true if Getter has the default name for the property PD.
1618static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1619 const ObjCMethodDecl *Getter) {
1620 assert(PD);
1621 if (!Getter)
1622 return true;
1623
1624 assert(Getter->getDeclName().isObjCZeroArgSelector());
1625 return PD->getName() ==
1626 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
1627}
1628
1629/// \return true if Setter has the default name for the property PD.
1630static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1631 const ObjCMethodDecl *Setter) {
1632 assert(PD);
1633 if (!Setter)
1634 return true;
1635
1636 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001637 return SelectorTable::constructSetterName(PD->getName()) ==
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001638 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
1639}
1640
Guy Benyei11169dd2012-12-18 14:30:41 +00001641/// CreateType - get objective-c interface type.
1642llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1643 llvm::DIFile Unit) {
1644 ObjCInterfaceDecl *ID = Ty->getDecl();
1645 if (!ID)
1646 return llvm::DIType();
1647
1648 // Get overall information about the record type for the debug info.
1649 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1650 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001651 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001652
1653 // If this is just a forward declaration return a special forward-declaration
1654 // debug type since we won't be able to lay out the entire type.
1655 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001656 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001657 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1658 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1659 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001660 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001661 return FwdDecl;
1662 }
1663
David Blaikieef8a9512014-05-05 23:23:53 +00001664
1665 return CreateTypeDefinition(Ty, Unit);
1666}
1667
1668llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, llvm::DIFile Unit) {
1669 ObjCInterfaceDecl *ID = Ty->getDecl();
1670 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1671 unsigned Line = getLineNumber(ID->getLocation());
1672 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001673
1674 // Bit size, align and offset of the type.
1675 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1676 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1677
1678 unsigned Flags = 0;
1679 if (ID->getImplementation())
1680 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1681
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001682 llvm::DICompositeType RealDecl =
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1684 Line, Size, Align, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00001685 llvm::DIType(), llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001686
David Blaikieef8a9512014-05-05 23:23:53 +00001687 QualType QTy(Ty, 0);
1688 TypeCache[QTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001689
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001690 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001691 LexicalBlockStack.push_back(static_cast<llvm::MDNode*>(RealDecl));
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1693
1694 // Convert all the elements.
1695 SmallVector<llvm::Value *, 16> EltTys;
1696
1697 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1698 if (SClass) {
1699 llvm::DIType SClassTy =
1700 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
1701 if (!SClassTy.isValid())
1702 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001703
Guy Benyei11169dd2012-12-18 14:30:41 +00001704 llvm::DIType InhTag =
1705 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
1706 EltTys.push_back(InhTag);
1707 }
1708
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001709 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001710 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 SourceLocation Loc = PD->getLocation();
1712 llvm::DIFile PUnit = getOrCreateFile(Loc);
1713 unsigned PLine = getLineNumber(Loc);
1714 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1715 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1716 llvm::MDNode *PropertyNode =
1717 DBuilder.createObjCProperty(PD->getName(),
Eric Christopherc0c5d462013-02-21 22:35:08 +00001718 PUnit, PLine,
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001719 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001720 getSelectorName(PD->getGetterName()),
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001721 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 getSelectorName(PD->getSetterName()),
1723 PD->getPropertyAttributes(),
Eric Christopherc0c5d462013-02-21 22:35:08 +00001724 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 EltTys.push_back(PropertyNode);
1726 }
1727
1728 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1729 unsigned FieldNo = 0;
1730 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1731 Field = Field->getNextIvar(), ++FieldNo) {
1732 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1733 if (!FieldTy.isValid())
1734 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001735
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 StringRef FieldName = Field->getName();
1737
1738 // Ignore unnamed fields.
1739 if (FieldName.empty())
1740 continue;
1741
1742 // Get the location for the field.
1743 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1744 unsigned FieldLine = getLineNumber(Field->getLocation());
1745 QualType FType = Field->getType();
1746 uint64_t FieldSize = 0;
1747 unsigned FieldAlign = 0;
1748
1749 if (!FType->isIncompleteArrayType()) {
1750
1751 // Bit size, align and offset of the type.
1752 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001753 ? Field->getBitWidthValue(CGM.getContext())
1754 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001755 FieldAlign = CGM.getContext().getTypeAlign(FType);
1756 }
1757
1758 uint64_t FieldOffset;
1759 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1760 // We don't know the runtime offset of an ivar if we're using the
1761 // non-fragile ABI. For bitfields, use the bit offset into the first
1762 // byte of storage of the bitfield. For other fields, use zero.
1763 if (Field->isBitField()) {
1764 FieldOffset = CGM.getObjCRuntime().ComputeBitfieldBitOffset(
1765 CGM, ID, Field);
1766 FieldOffset %= CGM.getContext().getCharWidth();
1767 } else {
1768 FieldOffset = 0;
1769 }
1770 } else {
1771 FieldOffset = RL.getFieldOffset(FieldNo);
1772 }
1773
1774 unsigned Flags = 0;
1775 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1776 Flags = llvm::DIDescriptor::FlagProtected;
1777 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1778 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001779 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1780 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001781
Craig Topper8a13c412014-05-21 05:09:00 +00001782 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001784 if (ObjCPropertyImplDecl *PImpD =
Guy Benyei11169dd2012-12-18 14:30:41 +00001785 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1786 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001787 SourceLocation Loc = PD->getLocation();
1788 llvm::DIFile PUnit = getOrCreateFile(Loc);
1789 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1791 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1792 PropertyNode =
1793 DBuilder.createObjCProperty(PD->getName(),
1794 PUnit, PLine,
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001795 hasDefaultGetterName(PD, Getter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001796 getSelectorName(PD->getGetterName()),
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001797 hasDefaultSetterName(PD, Setter) ? "" :
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 getSelectorName(PD->getSetterName()),
1799 PD->getPropertyAttributes(),
1800 getOrCreateType(PD->getType(), PUnit));
1801 }
1802 }
1803 }
1804 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1805 FieldLine, FieldSize, FieldAlign,
1806 FieldOffset, Flags, FieldTy,
1807 PropertyNode);
1808 EltTys.push_back(FieldTy);
1809 }
1810
1811 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001812 RealDecl.setArrays(Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001813
Guy Benyei11169dd2012-12-18 14:30:41 +00001814 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001815 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001816}
1817
1818llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1819 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1820 int64_t Count = Ty->getNumElements();
1821 if (Count == 0)
1822 // If number of elements are not known then this is an unbounded array.
1823 // Use Count == -1 to express such arrays.
1824 Count = -1;
1825
1826 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1827 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1828
1829 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1830 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1831
1832 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1833}
1834
1835llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1836 llvm::DIFile Unit) {
1837 uint64_t Size;
1838 uint64_t Align;
1839
1840 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1841 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1842 Size = 0;
1843 Align =
1844 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
1845 } else if (Ty->isIncompleteArrayType()) {
1846 Size = 0;
1847 if (Ty->getElementType()->isIncompleteType())
1848 Align = 0;
1849 else
1850 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001851 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 Size = 0;
1853 Align = 0;
1854 } else {
1855 // Size and align of the whole array, not the element type.
1856 Size = CGM.getContext().getTypeSize(Ty);
1857 Align = CGM.getContext().getTypeAlign(Ty);
1858 }
1859
1860 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1861 // interior arrays, do we care? Why aren't nested arrays represented the
1862 // obvious/recursive way?
1863 SmallVector<llvm::Value *, 8> Subscripts;
1864 QualType EltTy(Ty, 0);
1865 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1866 // If the number of elements is known, then count is that number. Otherwise,
1867 // it's -1. This allows us to represent a subrange with an array of 0
1868 // elements, like this:
1869 //
1870 // struct foo {
1871 // int x[0];
1872 // };
1873 int64_t Count = -1; // Count == -1 is an unbounded array.
1874 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1875 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001876
Guy Benyei11169dd2012-12-18 14:30:41 +00001877 // FIXME: Verify this is right for VLAs.
1878 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1879 EltTy = Ty->getElementType();
1880 }
1881
1882 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1883
Eric Christopherb2a008c2013-05-16 00:45:12 +00001884 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +00001885 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1886 SubscriptArray);
1887 return DbgTy;
1888}
1889
Eric Christopherb2a008c2013-05-16 00:45:12 +00001890llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001892 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 Ty, Ty->getPointeeType(), Unit);
1894}
1895
Eric Christopherb2a008c2013-05-16 00:45:12 +00001896llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001897 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001898 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 Ty, Ty->getPointeeType(), Unit);
1900}
1901
Eric Christopherb2a008c2013-05-16 00:45:12 +00001902llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001904 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1905 if (!Ty->getPointeeType()->isFunctionType())
1906 return DBuilder.createMemberPointerType(
David Blaikie99dab3b2013-09-04 22:03:57 +00001907 getOrCreateType(Ty->getPointeeType(), U), ClassType);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001908
1909 const FunctionProtoType *FPT =
1910 Ty->getPointeeType()->getAs<FunctionProtoType>();
David Blaikie2c705ca2013-01-19 19:20:56 +00001911 return DBuilder.createMemberPointerType(getOrCreateInstanceMethodType(
Adrian Prantl0866acd2013-12-19 01:38:47 +00001912 CGM.getContext().getPointerType(QualType(Ty->getClass(),
1913 FPT->getTypeQuals())),
1914 FPT, U), ClassType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001915}
1916
Eric Christopherb2a008c2013-05-16 00:45:12 +00001917llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 llvm::DIFile U) {
1919 // Ignore the atomic wrapping
1920 // FIXME: What is the correct representation?
1921 return getOrCreateType(Ty->getValueType(), U);
1922}
1923
1924/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001925llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001926 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 uint64_t Size = 0;
1928 uint64_t Align = 0;
1929 if (!ED->getTypeForDecl()->isIncompleteType()) {
1930 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1931 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1932 }
1933
Manman Rene0064d82013-08-29 23:19:58 +00001934 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1935
Guy Benyei11169dd2012-12-18 14:30:41 +00001936 // If this is just a forward declaration, construct an appropriately
1937 // marked node and just return it.
1938 if (!ED->getDefinition()) {
1939 llvm::DIDescriptor EDContext;
1940 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1941 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1942 unsigned Line = getLineNumber(ED->getLocation());
1943 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001944 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1945 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1946 0, Size, Align, FullName);
1947 ReplaceMap.push_back(std::make_pair(Ty, static_cast<llvm::Value *>(RetTy)));
1948 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 }
1950
David Blaikie483a9da2014-05-06 18:35:21 +00001951 return CreateTypeDefinition(Ty);
1952}
1953
1954llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1955 const EnumDecl *ED = Ty->getDecl();
1956 uint64_t Size = 0;
1957 uint64_t Align = 0;
1958 if (!ED->getTypeForDecl()->isIncompleteType()) {
1959 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1960 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1961 }
1962
1963 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1964
Guy Benyei11169dd2012-12-18 14:30:41 +00001965 // Create DIEnumerator elements for each enumerator.
1966 SmallVector<llvm::Value *, 16> Enumerators;
1967 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001968 for (const auto *Enum : ED->enumerators()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001969 Enumerators.push_back(
1970 DBuilder.createEnumerator(Enum->getName(),
David Blaikiece1ae382013-06-24 07:13:13 +00001971 Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001972 }
1973
1974 // Return a CompositeType for the enum itself.
1975 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1976
1977 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1978 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001979 llvm::DIDescriptor EnumContext =
Guy Benyei11169dd2012-12-18 14:30:41 +00001980 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Adrian Prantlc60dc712013-04-19 19:56:39 +00001981 llvm::DIType ClassTy = ED->isFixed() ?
Guy Benyei11169dd2012-12-18 14:30:41 +00001982 getOrCreateType(ED->getIntegerType(), DefUnit) : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001983 llvm::DIType DbgTy =
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1985 Size, Align, EltArray,
Manman Rene0064d82013-08-29 23:19:58 +00001986 ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 return DbgTy;
1988}
1989
David Blaikie05491062013-01-21 04:37:12 +00001990static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1991 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001993 Qualifiers InnerQuals = T.getLocalQualifiers();
1994 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1995 // that is already there.
1996 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1997 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 QualType LastT = T;
1999 switch (T->getTypeClass()) {
2000 default:
David Blaikie05491062013-01-21 04:37:12 +00002001 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002002 case Type::TemplateSpecialization: {
2003 const auto *Spec = cast<TemplateSpecializationType>(T);
2004 if (Spec->isTypeAlias())
2005 return C.getQualifiedType(T.getTypePtr(), Quals);
2006 T = Spec->desugar();
2007 break; }
Guy Benyei11169dd2012-12-18 14:30:41 +00002008 case Type::TypeOfExpr:
2009 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2010 break;
2011 case Type::TypeOf:
2012 T = cast<TypeOfType>(T)->getUnderlyingType();
2013 break;
2014 case Type::Decltype:
2015 T = cast<DecltypeType>(T)->getUnderlyingType();
2016 break;
2017 case Type::UnaryTransform:
2018 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2019 break;
2020 case Type::Attributed:
2021 T = cast<AttributedType>(T)->getEquivalentType();
2022 break;
2023 case Type::Elaborated:
2024 T = cast<ElaboratedType>(T)->getNamedType();
2025 break;
2026 case Type::Paren:
2027 T = cast<ParenType>(T)->getInnerType();
2028 break;
David Blaikie05491062013-01-21 04:37:12 +00002029 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 break;
2032 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002033 QualType DT = cast<AutoType>(T)->getDeducedType();
2034 if (DT.isNull())
2035 return T;
2036 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002037 break;
2038 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002039
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002041 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 } while (true);
2043}
2044
Eric Christopher0fdcb312013-05-16 00:52:20 +00002045/// getType - Get the type from the cache or return null type if it doesn't
2046/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002047llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2048
2049 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002050 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002051
David Blaikief427b002014-05-06 03:42:01 +00002052 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002053 if (it != TypeCache.end()) {
2054 // Verify that the debug info still exists.
2055 if (llvm::Value *V = it->second)
2056 return llvm::DIType(cast<llvm::MDNode>(V));
2057 }
2058
2059 return llvm::DIType();
2060}
2061
David Blaikie0e716b42014-03-03 23:48:23 +00002062void CGDebugInfo::completeTemplateDefinition(
2063 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002064 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2065 return;
2066
David Blaikie0e716b42014-03-03 23:48:23 +00002067 completeClassData(&SD);
2068 // In case this type has no member function definitions being emitted, ensure
2069 // it is retained
2070 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2071}
2072
Guy Benyei11169dd2012-12-18 14:30:41 +00002073/// getOrCreateType - Get the type from the cache or create a new
2074/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002075llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 if (Ty.isNull())
2077 return llvm::DIType();
2078
2079 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002080 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002081
David Blaikieef8a9512014-05-05 23:23:53 +00002082 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 return T;
2084
2085 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002086 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Adrian Prantl73409ce2013-03-11 18:33:46 +00002087 void* TyPtr = Ty.getAsOpaquePtr();
2088
2089 // And update the type cache.
2090 TypeCache[TyPtr] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002091
Guy Benyei11169dd2012-12-18 14:30:41 +00002092 return Res;
2093}
2094
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002095/// Currently the checksum of an interface includes the number of
2096/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002097unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002098 // The assumption is that the number of ivars can only increase
2099 // monotonically, so it is safe to just use their current number as
2100 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002101 unsigned Sum = 0;
2102 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002103 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002104 ++Sum;
2105
2106 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002107}
2108
2109ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2110 switch (Ty->getTypeClass()) {
2111 case Type::ObjCObjectPointer:
Eric Christopher0fdcb312013-05-16 00:52:20 +00002112 return getObjCInterfaceDecl(cast<ObjCObjectPointerType>(Ty)
2113 ->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002114 case Type::ObjCInterface:
2115 return cast<ObjCInterfaceType>(Ty)->getDecl();
2116 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002117 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002118 }
2119}
2120
Guy Benyei11169dd2012-12-18 14:30:41 +00002121/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002122llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 // Handle qualifiers, which recursively handles what they refer to.
2124 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002125 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002126
Craig Topper8a13c412014-05-21 05:09:00 +00002127 const char *Diag = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002128
Guy Benyei11169dd2012-12-18 14:30:41 +00002129 // Work out details of type.
2130 switch (Ty->getTypeClass()) {
2131#define TYPE(Class, Base)
2132#define ABSTRACT_TYPE(Class, Base)
2133#define NON_CANONICAL_TYPE(Class, Base)
2134#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2135#include "clang/AST/TypeNodes.def"
2136 llvm_unreachable("Dependent types cannot show up in debug information");
2137
2138 case Type::ExtVector:
2139 case Type::Vector:
2140 return CreateType(cast<VectorType>(Ty), Unit);
2141 case Type::ObjCObjectPointer:
2142 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2143 case Type::ObjCObject:
2144 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2145 case Type::ObjCInterface:
2146 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2147 case Type::Builtin:
2148 return CreateType(cast<BuiltinType>(Ty));
2149 case Type::Complex:
2150 return CreateType(cast<ComplexType>(Ty));
2151 case Type::Pointer:
2152 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002153 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002154 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002155 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002156 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002157 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 case Type::BlockPointer:
2159 return CreateType(cast<BlockPointerType>(Ty), Unit);
2160 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002161 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002163 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002165 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002166 case Type::FunctionProto:
2167 case Type::FunctionNoProto:
2168 return CreateType(cast<FunctionType>(Ty), Unit);
2169 case Type::ConstantArray:
2170 case Type::VariableArray:
2171 case Type::IncompleteArray:
2172 return CreateType(cast<ArrayType>(Ty), Unit);
2173
2174 case Type::LValueReference:
2175 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2176 case Type::RValueReference:
2177 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2178
2179 case Type::MemberPointer:
2180 return CreateType(cast<MemberPointerType>(Ty), Unit);
2181
2182 case Type::Atomic:
2183 return CreateType(cast<AtomicType>(Ty), Unit);
2184
Guy Benyei11169dd2012-12-18 14:30:41 +00002185 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002186 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2187
2188 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002189 case Type::Elaborated:
2190 case Type::Paren:
2191 case Type::SubstTemplateTypeParm:
2192 case Type::TypeOfExpr:
2193 case Type::TypeOf:
2194 case Type::Decltype:
2195 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002196 case Type::PackExpansion:
Guy Benyei11169dd2012-12-18 14:30:41 +00002197 llvm_unreachable("type should have been unwrapped!");
David Blaikie22c460a02013-05-24 21:24:35 +00002198 case Type::Auto:
2199 Diag = "auto";
2200 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002202
Guy Benyei11169dd2012-12-18 14:30:41 +00002203 assert(Diag && "Fall through without a diagnostic?");
2204 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
2205 "debug information for %0 is not yet supported");
2206 CGM.getDiags().Report(DiagID)
2207 << Diag;
2208 return llvm::DIType();
2209}
2210
2211/// getOrCreateLimitedType - Get the type from the cache or create a new
2212/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002213llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002214 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002215 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002216
David Blaikie8d5e1282013-08-20 21:03:29 +00002217 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002218
2219 // We may have cached a forward decl when we could have created
2220 // a non-forward decl. Go ahead and create a non-forward decl
2221 // now.
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002222 if (T && !T.isForwardDecl()) return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002223
2224 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002225 llvm::DICompositeType Res = CreateLimitedType(Ty);
2226
2227 // Propagate members from the declaration to the definition
2228 // CreateType(const RecordType*) will overwrite this with the members in the
2229 // correct order if the full type is needed.
Manman Ren51289892014-07-28 19:14:41 +00002230 Res.setArrays(T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002231
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 // And update the type cache.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002233 TypeCache[QTy.getAsOpaquePtr()] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002234 return Res;
2235}
2236
2237// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002238llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002239 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002240
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 // Get overall information about the record type for the debug info.
2242 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2243 unsigned Line = getLineNumber(RD->getLocation());
2244 StringRef RDName = getClassName(RD);
2245
Eric Christopher07429ff2013-10-15 21:22:34 +00002246 llvm::DIDescriptor RDContext =
2247 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002248
David Blaikied2785892013-08-18 17:36:19 +00002249 // If we ended up creating the type during the context chain construction,
2250 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002251 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2252 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
David Blaikied2785892013-08-18 17:36:19 +00002253 return T;
2254
Adrian Prantl381e7552014-02-04 21:29:50 +00002255 // If this is just a forward or incomplete declaration, construct an
2256 // appropriately marked node and just return it.
2257 const RecordDecl *D = RD->getDefinition();
2258 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002259 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
2261 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2262 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002263 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002264
Manman Rene0064d82013-08-29 23:19:58 +00002265 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2266
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 if (RD->isUnion())
2268 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Manman Rene0064d82013-08-29 23:19:58 +00002269 Size, Align, 0, llvm::DIArray(), 0,
2270 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002271 else if (RD->isClass()) {
2272 // FIXME: This could be a struct type giving a default visibility different
2273 // than C++ class type, but needs llvm metadata changes first.
2274 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002275 Size, Align, 0, 0, llvm::DIType(),
2276 llvm::DIArray(), llvm::DIType(),
Manman Rene0064d82013-08-29 23:19:58 +00002277 llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 } else
2279 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
Eric Christopher0fdcb312013-05-16 00:52:20 +00002280 Size, Align, 0, llvm::DIType(),
David Blaikieba477362013-11-18 23:38:26 +00002281 llvm::DIArray(), 0, llvm::DIType(),
2282 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002283
2284 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie49ae6a72013-03-26 23:47:35 +00002285 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002286
David Blaikieadfbf992013-08-18 16:55:33 +00002287 if (const ClassTemplateSpecializationDecl *TSpecial =
2288 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Manman Ren51289892014-07-28 19:14:41 +00002289 RealDecl.setArrays(llvm::DIArray(),
David Blaikieadfbf992013-08-18 16:55:33 +00002290 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002291 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002292}
2293
David Blaikieadfbf992013-08-18 16:55:33 +00002294void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2295 llvm::DICompositeType RealDecl) {
2296 // A class's primary base or the class itself contains the vtable.
2297 llvm::DICompositeType ContainingType;
2298 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2299 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002300 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002301 while (1) {
2302 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2303 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2304 if (PBT && !BRL.isPrimaryBaseVirtual())
2305 PBase = PBT;
2306 else
2307 break;
2308 }
2309 ContainingType = llvm::DICompositeType(
2310 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2311 getOrCreateFile(RD->getLocation())));
2312 } else if (RD->isDynamicClass())
2313 ContainingType = RealDecl;
2314
2315 RealDecl.setContainingType(ContainingType);
2316}
2317
Guy Benyei11169dd2012-12-18 14:30:41 +00002318/// CreateMemberType - Create new member and increase Offset by FType's size.
2319llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
2320 StringRef Name,
2321 uint64_t *Offset) {
2322 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2323 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2324 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
2325 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
2326 FieldSize, FieldAlign,
2327 *Offset, 0, FieldTy);
2328 *Offset += FieldSize;
2329 return Ty;
2330}
2331
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00002332llvm::DIScope CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002333 // We only need a declaration (not a definition) of the type - so use whatever
2334 // we would otherwise do to get a type for a pointee. (forward declarations in
2335 // limited debug info, full definitions (if the type definition is available)
2336 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002337 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2338 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002339 getOrCreateFile(TD->getLocation()));
David Blaikiebd483762013-05-20 04:58:53 +00002340 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2341 // This doesn't handle providing declarations (for functions or variables) for
2342 // entities without definitions in this TU, nor when the definition proceeds
2343 // the call to this function.
2344 // FIXME: This should be split out into more specific maps with support for
2345 // emitting forward declarations and merging definitions with declarations,
2346 // the same way as we do for types.
2347 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2348 DeclCache.find(D->getCanonicalDecl());
2349 if (I == DeclCache.end())
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00002350 return llvm::DIScope();
David Blaikiebd483762013-05-20 04:58:53 +00002351 llvm::Value *V = I->second;
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00002352 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikiebd483762013-05-20 04:58:53 +00002353}
2354
Guy Benyei11169dd2012-12-18 14:30:41 +00002355/// getFunctionDeclaration - Return debug info descriptor to describe method
2356/// declaration for the given method definition.
2357llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002358 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002359 return llvm::DISubprogram();
2360
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2362 if (!FD) return llvm::DISubprogram();
2363
2364 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002365 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002366
2367 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2368 MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002369 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002370 if (const CXXMethodDecl *MD =
2371 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002372 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002373 llvm::DISubprogram SP =
2374 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002375 return SP;
2376 }
2377 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002378 if (MI != SPCache.end()) {
2379 llvm::Value *V = MI->second;
2380 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002381 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002382 return SP;
2383 }
2384
Aaron Ballman86c93902014-03-06 23:45:36 +00002385 for (auto NextFD : FD->redecls()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2387 MI = SPCache.find(NextFD->getCanonicalDecl());
2388 if (MI != SPCache.end()) {
2389 llvm::Value *V = MI->second;
2390 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002391 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002392 return SP;
2393 }
2394 }
2395 return llvm::DISubprogram();
2396}
2397
2398// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2399// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002400llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2401 QualType FnType,
2402 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002403 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002404 // Create fake but valid subroutine type. Otherwise
2405 // llvm::DISubprogram::Verify() would return false, and
2406 // subprogram DIE will miss DW_AT_decl_file and
2407 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002408 return DBuilder.createSubroutineType(F,
2409 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002410
2411 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2412 return getOrCreateMethodType(Method, F);
2413 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2414 // Add "self" and "_cmd"
2415 SmallVector<llvm::Value *, 16> Elts;
2416
2417 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002418 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002419
2420 // Replace the instancetype keyword with the actual type.
2421 if (ResultTy == CGM.getContext().getObjCInstanceType())
2422 ResultTy = CGM.getContext().getPointerType(
2423 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
2424
Adrian Prantl7bec9032013-05-10 21:08:31 +00002425 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002427 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2428 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2429 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 // "_cmd" pointer is always second argument.
2431 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2432 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2433 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002434 for (const auto *PI : OMethod->params())
2435 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002436 // Variadic methods need a special marker at the end of the type list.
2437 if (OMethod->isVariadic())
2438 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002439
Manman Ren67f005e2014-07-28 22:24:34 +00002440 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002441 return DBuilder.createSubroutineType(F, EltTypeArray);
2442 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002443
Adrian Prantl800faef2014-02-25 23:42:18 +00002444 // Handle variadic function types; they need an additional
2445 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002446 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2447 if (FD->isVariadic()) {
2448 SmallVector<llvm::Value *, 16> EltTys;
2449 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2450 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2451 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2452 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2453 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002454 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002455 return DBuilder.createSubroutineType(F, EltTypeArray);
2456 }
2457
David Blaikie469f0792013-05-22 23:22:42 +00002458 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002459}
2460
2461/// EmitFunctionStart - Constructs the debug code for entering a function.
Adrian Prantl42d71b92014-04-10 23:21:53 +00002462void CGDebugInfo::EmitFunctionStart(GlobalDecl GD,
2463 SourceLocation Loc,
2464 SourceLocation ScopeLoc,
2465 QualType FnType,
Guy Benyei11169dd2012-12-18 14:30:41 +00002466 llvm::Function *Fn,
2467 CGBuilderTy &Builder) {
2468
2469 StringRef Name;
2470 StringRef LinkageName;
2471
2472 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2473
2474 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002475 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002476
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 unsigned Flags = 0;
2478 llvm::DIFile Unit = getOrCreateFile(Loc);
2479 llvm::DIDescriptor FDContext(Unit);
2480 llvm::DIArray TParamsArray;
2481 if (!HasDecl) {
2482 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002483 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2485 // If there is a DISubprogram for this function available then use it.
2486 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
2487 FI = SPCache.find(FD->getCanonicalDecl());
2488 if (FI != SPCache.end()) {
2489 llvm::Value *V = FI->second;
2490 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2491 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2492 llvm::MDNode *SPN = SP;
2493 LexicalBlockStack.push_back(SPN);
2494 RegionMap[D] = llvm::WeakVH(SP);
2495 return;
2496 }
2497 }
2498 Name = getFunctionName(FD);
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002499 // Use mangled name as linkage name for C/C++ functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00002500 if (FD->hasPrototype()) {
2501 LinkageName = CGM.getMangledName(GD);
2502 Flags |= llvm::DIDescriptor::FlagPrototyped;
2503 }
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002504 // No need to replicate the linkage name if it isn't different from the
2505 // subprogram name, no need to have it at all unless coverage is enabled or
2506 // debug is set to more than just line tables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 if (LinkageName == Name ||
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002508 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2509 !CGM.getCodeGenOpts().EmitGcovNotes &&
Eric Christopher75e17682013-05-16 00:45:23 +00002510 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
Guy Benyei11169dd2012-12-18 14:30:41 +00002511 LinkageName = StringRef();
2512
Eric Christopher75e17682013-05-16 00:45:23 +00002513 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
Eric Christopher4cbd0d9d2014-03-27 05:29:34 +00002514 if (const NamespaceDecl *NSDecl =
2515 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2516 FDContext = getOrCreateNameSpace(NSDecl);
2517 else if (const RecordDecl *RDecl =
2518 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2519 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2520
2521 // Collect template parameters.
Guy Benyei11169dd2012-12-18 14:30:41 +00002522 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2523 }
2524 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2525 Name = getObjCMethodName(OMD);
2526 Flags |= llvm::DIDescriptor::FlagPrototyped;
2527 } else {
2528 // Use llvm function name.
2529 Name = Fn->getName();
2530 Flags |= llvm::DIDescriptor::FlagPrototyped;
2531 }
2532 if (!Name.empty() && Name[0] == '\01')
2533 Name = Name.substr(1);
2534
Adrian Prantl42d71b92014-04-10 23:21:53 +00002535 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002536 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002537 // Artificial functions without a location should not silently reuse CurLoc.
2538 if (Loc.isInvalid())
2539 CurLoc = SourceLocation();
2540 }
2541 unsigned LineNo = getLineNumber(Loc);
2542 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002543
Eric Christopher8018e412014-03-27 18:50:35 +00002544 // FIXME: The function declaration we're constructing here is mostly reusing
2545 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2546 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2547 // all subprograms instead of the actual context since subprogram definitions
2548 // are emitted as CU level entities by the backend.
Eric Christopher9e6f5f92013-10-17 01:31:21 +00002549 llvm::DISubprogram SP =
2550 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2551 getOrCreateFunctionType(D, FnType, Unit),
2552 Fn->hasInternalLinkage(), true /*definition*/,
Adrian Prantl42d71b92014-04-10 23:21:53 +00002553 ScopeLine, Flags,
Eric Christopher9e6f5f92013-10-17 01:31:21 +00002554 CGM.getLangOpts().Optimize, Fn, TParamsArray,
2555 getFunctionDeclaration(D));
David Blaikiebd483762013-05-20 04:58:53 +00002556 if (HasDecl)
2557 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002558
Adrian Prantlbebb8932014-03-21 21:01:58 +00002559 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 llvm::MDNode *SPN = SP;
2561 LexicalBlockStack.push_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002562
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 if (HasDecl)
2564 RegionMap[D] = llvm::WeakVH(SP);
2565}
2566
2567/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002568/// information in the source file. If the location is invalid, the
2569/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002570void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002571 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 // Update our current location
2573 setLocation(Loc);
2574
2575 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
2576
2577 // Don't bother if things are the same as last time.
2578 SourceManager &SM = CGM.getContext().getSourceManager();
2579 if (CurLoc == PrevLoc ||
2580 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2581 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002582 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2583 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
2584 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002586
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 // Update last state.
2588 PrevLoc = CurLoc;
2589
Adrian Prantle83b1302014-01-07 22:05:52 +00002590 llvm::MDNode *Scope = LexicalBlockStack.back();
Adrian Prantlc7822422013-03-12 20:43:25 +00002591 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get
2592 (getLineNumber(CurLoc),
2593 getColumnNumber(CurLoc, ForceColumnInfo),
2594 Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002595}
2596
2597/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2598/// the stack.
2599void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
David Blaikief9ea2422014-06-02 16:32:05 +00002600 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
2601 llvm::DIDescriptor(LexicalBlockStack.empty() ? nullptr
2602 : LexicalBlockStack.back()),
David Blaikieb8449842014-08-21 22:46:45 +00002603 getOrCreateFile(CurLoc), getLineNumber(CurLoc), getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 llvm::MDNode *DN = D;
2605 LexicalBlockStack.push_back(DN);
2606}
2607
2608/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2609/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002610void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2611 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 // Set our current location.
2613 setLocation(Loc);
2614
2615 // Create a new lexical block and push it on the stack.
2616 CreateLexicalBlock(Loc);
2617
2618 // Emit a line table change for the current location inside the new scope.
2619 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
2620 getColumnNumber(Loc),
2621 LexicalBlockStack.back()));
2622}
2623
2624/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2625/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002626void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2627 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2629
2630 // Provide an entry in the line table for the end of the block.
2631 EmitLocation(Builder, Loc);
2632
2633 LexicalBlockStack.pop_back();
2634}
2635
2636/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2637void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2638 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2639 unsigned RCount = FnBeginRegionCount.back();
2640 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2641
2642 // Pop all regions for this function.
2643 while (LexicalBlockStack.size() != RCount)
2644 EmitLexicalBlockEnd(Builder, CurLoc);
2645 FnBeginRegionCount.pop_back();
2646}
2647
Eric Christopherb2a008c2013-05-16 00:45:12 +00002648// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002649// See BuildByRefType.
2650llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2651 uint64_t *XOffset) {
2652
2653 SmallVector<llvm::Value *, 5> EltTys;
2654 QualType FType;
2655 uint64_t FieldSize, FieldOffset;
2656 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002657
Guy Benyei11169dd2012-12-18 14:30:41 +00002658 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002659 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002660
2661 FieldOffset = 0;
2662 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2663 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2664 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2665 FType = CGM.getContext().IntTy;
2666 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2667 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2668
2669 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2670 if (HasCopyAndDispose) {
2671 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2672 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2673 &FieldOffset));
2674 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2675 &FieldOffset));
2676 }
2677 bool HasByrefExtendedLayout;
2678 Qualifiers::ObjCLifetime Lifetime;
2679 if (CGM.getContext().getByrefLifetime(Type,
2680 Lifetime, HasByrefExtendedLayout)
Adrian Prantlead2ba42013-07-23 00:12:14 +00002681 && HasByrefExtendedLayout) {
2682 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 EltTys.push_back(CreateMemberType(Unit, FType,
2684 "__byref_variable_layout",
2685 &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002686 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002687
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2689 if (Align > CGM.getContext().toCharUnitsFromBits(
John McCallc8e01702013-04-16 22:48:15 +00002690 CGM.getTarget().getPointerAlign(0))) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002691 CharUnits FieldOffsetInBytes
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2693 CharUnits AlignedOffsetInBytes
2694 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2695 CharUnits NumPaddingBytes
2696 = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002697
Guy Benyei11169dd2012-12-18 14:30:41 +00002698 if (NumPaddingBytes.isPositive()) {
2699 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2700 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2701 pad, ArrayType::Normal, 0);
2702 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2703 }
2704 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002705
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002707 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002708 FieldSize = CGM.getContext().getTypeSize(FType);
2709 FieldAlign = CGM.getContext().toBits(Align);
2710
Eric Christopherb2a008c2013-05-16 00:45:12 +00002711 *XOffset = FieldOffset;
Guy Benyei11169dd2012-12-18 14:30:41 +00002712 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
2713 0, FieldSize, FieldAlign,
2714 FieldOffset, 0, FieldTy);
2715 EltTys.push_back(FieldTy);
2716 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002717
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002719
Guy Benyei11169dd2012-12-18 14:30:41 +00002720 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002721
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002723 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002724}
2725
2726/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002727void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +00002728 llvm::Value *Storage,
Guy Benyei11169dd2012-12-18 14:30:41 +00002729 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002730 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2732
David Blaikie7fceebf2013-08-19 03:37:48 +00002733 bool Unwritten =
2734 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2735 cast<Decl>(VD->getDeclContext())->isImplicit());
2736 llvm::DIFile Unit;
2737 if (!Unwritten)
2738 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 llvm::DIType Ty;
2740 uint64_t XOffset = 0;
2741 if (VD->hasAttr<BlocksAttr>())
2742 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002743 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002744 Ty = getOrCreateType(VD->getType(), Unit);
2745
2746 // If there is no debug info for this type then do not emit debug info
2747 // for this variable.
2748 if (!Ty)
2749 return;
2750
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002752 unsigned Line = 0;
2753 unsigned Column = 0;
2754 if (!Unwritten) {
2755 Line = getLineNumber(VD->getLocation());
2756 Column = getColumnNumber(VD->getLocation());
2757 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002758 unsigned Flags = 0;
2759 if (VD->isImplicit())
2760 Flags |= llvm::DIDescriptor::FlagArtificial;
2761 // If this is the first argument and it is implicit then
2762 // give it an object pointer flag.
2763 // FIXME: There has to be a better way to do this, but for static
2764 // functions there won't be an implicit param at arg1 and
2765 // otherwise it is 'self' or 'this'.
2766 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2767 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002768 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002769 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2770 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002771 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002772
2773 llvm::MDNode *Scope = LexicalBlockStack.back();
2774
2775 StringRef Name = VD->getName();
2776 if (!Name.empty()) {
2777 if (VD->hasAttr<BlocksAttr>()) {
2778 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002779 SmallVector<int64_t, 9> addr;
2780 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 // offset of __forwarding field
2782 offset = CGM.getContext().toCharUnitsFromBits(
John McCallc8e01702013-04-16 22:48:15 +00002783 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002784 addr.push_back(offset.getQuantity());
2785 addr.push_back(llvm::dwarf::DW_OP_deref);
2786 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 // offset of x field
2788 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002789 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002790
2791 // Create the descriptor for the variable.
2792 llvm::DIVariable D =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002793 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2794 VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002795
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 // Insert an llvm.dbg.declare into the current block.
2797 llvm::Instruction *Call =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002798 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2799 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2801 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002802 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002803 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002804 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2805 // If VD is an anonymous union then Storage represents value for
2806 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002808 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002809 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002810 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2811 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002812
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 // Ignore unnamed fields. Do not ignore unnamed records.
2814 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2815 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002816
Guy Benyei11169dd2012-12-18 14:30:41 +00002817 // Use VarDecl's Tag, Scope and Line number.
2818 llvm::DIVariable D =
2819 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Eric Christopherb2a008c2013-05-16 00:45:12 +00002820 FieldName, Unit, Line, FieldTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 CGM.getLangOpts().Optimize, Flags,
2822 ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002823
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 // Insert an llvm.dbg.declare into the current block.
2825 llvm::Instruction *Call =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002826 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(),
2827 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2829 }
David Blaikie219c7d92013-01-05 20:03:07 +00002830 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 }
2832 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002833
2834 // Create the descriptor for the variable.
2835 llvm::DIVariable D =
2836 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2837 Name, Unit, Line, Ty,
2838 CGM.getLangOpts().Optimize, Flags, ArgNo);
2839
2840 // Insert an llvm.dbg.declare into the current block.
2841 llvm::Instruction *Call =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002842 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(),
2843 Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002844 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002845}
2846
2847void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2848 llvm::Value *Storage,
2849 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002850 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2852}
2853
Adrian Prantlde17db32013-03-29 19:20:29 +00002854/// Look up the completed type for a self pointer in the TypeCache and
2855/// create a copy of it with the ObjectPointer and Artificial flags
2856/// set. If the type is not cached, a new one is created. This should
2857/// never happen though, since creating a type for the implicit self
2858/// argument implies that we already parsed the interface definition
2859/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002860llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2861 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002862 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christopherf8bc4d82013-07-18 00:52:50 +00002863 if (CachedTy) Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002864 return DBuilder.createObjectPointerType(Ty);
2865}
2866
Guy Benyei11169dd2012-12-18 14:30:41 +00002867void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(const VarDecl *VD,
2868 llvm::Value *Storage,
2869 CGBuilderTy &Builder,
2870 const CGBlockInfo &blockInfo) {
Eric Christopher75e17682013-05-16 00:45:23 +00002871 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002872 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002873
Craig Topper8a13c412014-05-21 05:09:00 +00002874 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002875 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002876
Guy Benyei11169dd2012-12-18 14:30:41 +00002877 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002878
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 uint64_t XOffset = 0;
2880 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2881 llvm::DIType Ty;
2882 if (isByRef)
2883 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002884 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 Ty = getOrCreateType(VD->getType(), Unit);
2886
2887 // Self is passed along as an implicit non-arg variable in a
2888 // block. Mark it as the object pointer.
2889 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002890 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002891
2892 // Get location information.
2893 unsigned Line = getLineNumber(VD->getLocation());
2894 unsigned Column = getColumnNumber(VD->getLocation());
2895
2896 const llvm::DataLayout &target = CGM.getDataLayout();
2897
2898 CharUnits offset = CharUnits::fromQuantity(
2899 target.getStructLayout(blockInfo.StructureType)
2900 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2901
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002902 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002903 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002904 addr.push_back(llvm::dwarf::DW_OP_deref);
2905 addr.push_back(llvm::dwarf::DW_OP_plus);
2906 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002908 addr.push_back(llvm::dwarf::DW_OP_deref);
2909 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 // offset of __forwarding field
2911 offset = CGM.getContext()
2912 .toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002913 addr.push_back(offset.getQuantity());
2914 addr.push_back(llvm::dwarf::DW_OP_deref);
2915 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002916 // offset of x field
2917 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002918 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 }
2920
2921 // Create the descriptor for the variable.
2922 llvm::DIVariable D =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002923 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2924 llvm::DIDescriptor(LexicalBlockStack.back()),
2925 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002926
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 // Insert an llvm.dbg.declare into the current block.
2928 llvm::Instruction *Call =
Adrian Prantl2706eb02014-10-01 18:55:34 +00002929 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2930 Builder.GetInsertPoint());
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2932 LexicalBlockStack.back()));
2933}
2934
2935/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2936/// variable declaration.
2937void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2938 unsigned ArgNo,
2939 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002940 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2942}
2943
2944namespace {
2945 struct BlockLayoutChunk {
2946 uint64_t OffsetInBits;
2947 const BlockDecl::Capture *Capture;
2948 };
2949 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2950 return l.OffsetInBits < r.OffsetInBits;
2951 }
2952}
2953
2954void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002955 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002956 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002957 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002959 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 ASTContext &C = CGM.getContext();
2961 const BlockDecl *blockDecl = block.getBlockDecl();
2962
2963 // Collect some general information about the block's location.
2964 SourceLocation loc = blockDecl->getCaretLocation();
2965 llvm::DIFile tunit = getOrCreateFile(loc);
2966 unsigned line = getLineNumber(loc);
2967 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002968
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 // Build the debug-info type for the block literal.
2970 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2971
2972 const llvm::StructLayout *blockLayout =
2973 CGM.getDataLayout().getStructLayout(block.StructureType);
2974
2975 SmallVector<llvm::Value*, 16> fields;
2976 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2977 blockLayout->getElementOffsetInBits(0),
2978 tunit, tunit));
2979 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2980 blockLayout->getElementOffsetInBits(1),
2981 tunit, tunit));
2982 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2983 blockLayout->getElementOffsetInBits(2),
2984 tunit, tunit));
2985 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2986 blockLayout->getElementOffsetInBits(3),
2987 tunit, tunit));
2988 fields.push_back(createFieldType("__descriptor",
2989 C.getPointerType(block.NeedsCopyDispose ?
2990 C.getBlockDescriptorExtendedType() :
2991 C.getBlockDescriptorType()),
2992 0, loc, AS_public,
2993 blockLayout->getElementOffsetInBits(4),
2994 tunit, tunit));
2995
2996 // We want to sort the captures by offset, not because DWARF
2997 // requires this, but because we're paranoid about debuggers.
2998 SmallVector<BlockLayoutChunk, 8> chunks;
2999
3000 // 'this' capture.
3001 if (blockDecl->capturesCXXThis()) {
3002 BlockLayoutChunk chunk;
3003 chunk.OffsetInBits =
3004 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003005 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 chunks.push_back(chunk);
3007 }
3008
3009 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003010 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 const VarDecl *variable = capture.getVariable();
3012 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3013
3014 // Ignore constant captures.
3015 if (captureInfo.isConstant())
3016 continue;
3017
3018 BlockLayoutChunk chunk;
3019 chunk.OffsetInBits =
3020 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
3021 chunk.Capture = &capture;
3022 chunks.push_back(chunk);
3023 }
3024
3025 // Sort by offset.
3026 llvm::array_pod_sort(chunks.begin(), chunks.end());
3027
3028 for (SmallVectorImpl<BlockLayoutChunk>::iterator
3029 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
3030 uint64_t offsetInBits = i->OffsetInBits;
3031 const BlockDecl::Capture *capture = i->Capture;
3032
3033 // If we have a null capture, this must be the C++ 'this' capture.
3034 if (!capture) {
3035 const CXXMethodDecl *method =
3036 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
3037 QualType type = method->getThisType(C);
3038
3039 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3040 offsetInBits, tunit, tunit));
3041 continue;
3042 }
3043
3044 const VarDecl *variable = capture->getVariable();
3045 StringRef name = variable->getName();
3046
3047 llvm::DIType fieldType;
3048 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003049 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003050
3051 // FIXME: this creates a second copy of this type!
3052 uint64_t xoffset;
3053 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003054 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3055 fieldType =
3056 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3057 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 } else {
3059 fieldType = createFieldType(name, variable->getType(), 0,
3060 loc, AS_public, offsetInBits, tunit, tunit);
3061 }
3062 fields.push_back(fieldType);
3063 }
3064
3065 SmallString<36> typeName;
3066 llvm::raw_svector_ostream(typeName)
3067 << "__block_literal_" << CGM.getUniqueBlockCount();
3068
3069 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3070
3071 llvm::DIType type =
3072 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3073 CGM.getContext().toBits(block.BlockSize),
3074 CGM.getContext().toBits(block.BlockAlign),
David Blaikie6d4fe152013-02-25 01:07:08 +00003075 0, llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003076 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3077
3078 // Get overall information about the block.
3079 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3080 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003081
3082 // Create the descriptor for the parameter.
3083 llvm::DIVariable debugVar =
3084 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
Eric Christopherb2a008c2013-05-16 00:45:12 +00003085 llvm::DIDescriptor(scope),
Adrian Prantl51936dd2013-03-14 17:53:33 +00003086 Arg->getName(), tunit, line, type,
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 CGM.getLangOpts().Optimize, flags,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003088 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003089
Adrian Prantl616bef42013-03-14 21:52:59 +00003090 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003091 // Insert an llvm.dbg.value into the current block.
Adrian Prantl616bef42013-03-14 21:52:59 +00003092 llvm::Instruction *DbgVal =
3093 DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar,
Adrian Prantl2706eb02014-10-01 18:55:34 +00003094 DBuilder.createExpression(),
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00003095 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003096 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3097 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003098
Adrian Prantl616bef42013-03-14 21:52:59 +00003099 // Insert an llvm.dbg.declare into the current block.
3100 llvm::Instruction *DbgDecl =
Adrian Prantl2706eb02014-10-01 18:55:34 +00003101 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3102 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003103 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003104}
3105
David Blaikie6943dea2013-08-20 01:28:15 +00003106/// If D is an out-of-class definition of a static data member of a class, find
3107/// its corresponding in-class declaration.
3108llvm::DIDerivedType
3109CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3110 if (!D->isStaticDataMember())
3111 return llvm::DIDerivedType();
3112 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI =
3113 StaticDataMemberCache.find(D->getCanonicalDecl());
3114 if (MI != StaticDataMemberCache.end()) {
3115 assert(MI->second && "Static data member declaration should still exist");
3116 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003117 }
David Blaikiece763042013-08-20 21:49:21 +00003118
3119 // If the member wasn't found in the cache, lazily construct and add it to the
3120 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003121 auto DC = D->getDeclContext();
3122 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3123 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003124}
3125
Eric Christophercab9fae2014-04-10 05:20:00 +00003126/// Recursively collect all of the member fields of a global anonymous decl and
3127/// create static variables for them. The first time this is called it needs
3128/// to be on a union and then from there we can have additional unnamed fields.
3129llvm::DIGlobalVariable
3130CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3131 unsigned LineNo, StringRef LinkageName,
3132 llvm::GlobalVariable *Var,
3133 llvm::DIDescriptor DContext) {
3134 llvm::DIGlobalVariable GV;
3135
3136 for (const auto *Field : RD->fields()) {
3137 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3138 StringRef FieldName = Field->getName();
3139
3140 // Ignore unnamed fields, but recurse into anonymous records.
3141 if (FieldName.empty()) {
3142 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3143 if (RT)
3144 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3145 Var, DContext);
3146 continue;
3147 }
3148 // Use VarDecl's Tag, Scope and Line number.
Jyoti Allurb76b57f2014-09-29 06:32:54 +00003149 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
Eric Christophercab9fae2014-04-10 05:20:00 +00003150 LineNo, FieldTy,
3151 Var->hasInternalLinkage(), Var,
3152 llvm::DIDerivedType());
3153 }
3154 return GV;
3155}
3156
Guy Benyei11169dd2012-12-18 14:30:41 +00003157/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003158void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003160 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003161 // Create global variable debug descriptor.
3162 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
3163 unsigned LineNo = getLineNumber(D->getLocation());
3164
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003165 setLocation(D->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00003166
3167 QualType T = D->getType();
3168 if (T->isIncompleteArrayType()) {
3169
3170 // CodeGen turns int[] into int[1] so we'll do the same here.
3171 llvm::APInt ConstVal(32, 1);
3172 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3173
3174 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3175 ArrayType::Normal, 0);
3176 }
Eric Christophercab9fae2014-04-10 05:20:00 +00003177
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003178 StringRef DeclName = D->getName();
3179 StringRef LinkageName;
Eric Christophercab9fae2014-04-10 05:20:00 +00003180 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) &&
3181 !isa<ObjCMethodDecl>(D->getDeclContext()))
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003182 LinkageName = Var->getName();
3183 if (LinkageName == DeclName)
3184 LinkageName = StringRef();
Eric Christophercab9fae2014-04-10 05:20:00 +00003185
Eric Christopherb2a008c2013-05-16 00:45:12 +00003186 llvm::DIDescriptor DContext =
Guy Benyei11169dd2012-12-18 14:30:41 +00003187 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Eric Christophercab9fae2014-04-10 05:20:00 +00003188
3189 // Attempt to store one global variable for the declaration - even if we
3190 // emit a lot of fields.
3191 llvm::DIGlobalVariable GV;
3192
3193 // If this is an anonymous union then we'll want to emit a global
3194 // variable for each member of the anonymous union so that it's possible
3195 // to find the name of any field in the union.
3196 if (T->isUnionType() && DeclName.empty()) {
3197 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
3198 assert(RD->isAnonymousStructOrUnion() && "unnamed non-anonymous struct or union?");
3199 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3200 } else {
Jyoti Allurb76b57f2014-09-29 06:32:54 +00003201 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003202 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3203 Var->hasInternalLinkage(), Var,
3204 getOrCreateStaticDataMemberDeclarationOrNull(D));
3205 }
David Blaikiebd483762013-05-20 04:58:53 +00003206 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003207}
3208
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003209/// EmitGlobalVariable - Emit global variable's debug info.
3210void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3211 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003212 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003213 // Create the descriptor for the variable.
3214 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3215 StringRef Name = VD->getName();
3216 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3217 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3218 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3219 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3220 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3221 }
3222 // Do not use DIGlobalVariable for enums.
3223 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3224 return;
David Blaikiea15565562014-04-04 20:56:17 +00003225 // Do not emit separate definitions for function local const/statics.
3226 if (isa<FunctionDecl>(VD->getDeclContext()))
3227 return;
David Blaikiebb113912014-04-05 07:23:17 +00003228 VD = cast<ValueDecl>(VD->getCanonicalDecl());
3229 auto pair = DeclCache.insert(std::make_pair(VD, llvm::WeakVH()));
3230 if (!pair.second)
3231 return;
David Blaikie506a7452014-04-05 07:46:57 +00003232 llvm::DIDescriptor DContext =
3233 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
Jyoti Allurb76b57f2014-09-29 06:32:54 +00003234 llvm::DIGlobalVariable GV = DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003235 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
3236 true, Init,
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003237 getOrCreateStaticDataMemberDeclarationOrNull(cast<VarDecl>(VD)));
David Blaikiebb113912014-04-05 07:23:17 +00003238 pair.first->second = llvm::WeakVH(GV);
David Blaikiebd483762013-05-20 04:58:53 +00003239}
3240
3241llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3242 if (!LexicalBlockStack.empty())
3243 return llvm::DIScope(LexicalBlockStack.back());
3244 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003245}
3246
David Blaikie9f88fe82013-04-22 06:13:21 +00003247void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003248 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3249 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003250 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003251 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3252 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003253 getLineNumber(UD.getLocation()));
3254}
3255
David Blaikiebd483762013-05-20 04:58:53 +00003256void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3257 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3258 return;
3259 assert(UD.shadow_size() &&
3260 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3261 // Emitting one decl is sufficient - debuggers can detect that this is an
3262 // overloaded name & provide lookup for all the overloads.
3263 const UsingShadowDecl &USD = **UD.shadow_begin();
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00003264 if (llvm::DIScope Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003265 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003266 DBuilder.createImportedDeclaration(
3267 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3268 getLineNumber(USD.getLocation()));
3269}
3270
David Blaikief121b932013-05-20 22:50:41 +00003271llvm::DIImportedEntity
3272CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3273 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003274 return llvm::DIImportedEntity(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003275 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3276 if (VH)
3277 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003278 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003279 if (const NamespaceAliasDecl *Underlying =
3280 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3281 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003282 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003283 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3284 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3285 NA.getName());
3286 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003287 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003288 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3289 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3290 getLineNumber(NA.getLocation()), NA.getName());
3291 VH = R;
3292 return R;
3293}
3294
Guy Benyei11169dd2012-12-18 14:30:41 +00003295/// getOrCreateNamesSpace - Return namespace descriptor for the given
3296/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003297llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003298CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003299 NSDecl = NSDecl->getCanonicalDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003300 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei11169dd2012-12-18 14:30:41 +00003301 NameSpaceCache.find(NSDecl);
3302 if (I != NameSpaceCache.end())
3303 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003304
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3306 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003307 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003308 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3309 llvm::DINameSpace NS =
3310 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3311 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3312 return NS;
3313}
3314
3315void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003316 // Creating types might create further types - invalidating the current
3317 // element and the size(), so don't cache/reference them.
3318 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3319 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3320 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3321 E.Type->getDecl()->getDefinition()
3322 ? CreateTypeDefinition(E.Type, E.Unit)
3323 : E.Decl);
3324 }
3325
David Blaikief427b002014-05-06 03:42:01 +00003326 for (auto p : ReplaceMap) {
3327 assert(p.second);
3328 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003329 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003330
David Blaikief427b002014-05-06 03:42:01 +00003331 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003332 assert(it != TypeCache.end());
3333 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003334
David Blaikief427b002014-05-06 03:42:01 +00003335 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3336 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003337 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003338
3339 // We keep our own list of retained types, because we need to look
3340 // up the final type in the type cache.
3341 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3342 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003343 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003344
Guy Benyei11169dd2012-12-18 14:30:41 +00003345 DBuilder.finalize();
3346}
David Blaikie66088d52014-09-24 17:01:27 +00003347
3348void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3349 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3350 return;
3351 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3352 // Don't ignore in case of explicit cast where it is referenced indirectly.
3353 DBuilder.retainType(DieTy);
3354}