blob: 8ba69eaa5c5a2249a6a48aaee6480a3e861e5d6a [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)
Eric Christophere7b87e52014-10-26 23:40:33 +000070 : SaveAndRestoreLocation(CGF, B) {
Adrian Prantld1b151e2014-01-17 00:15:10 +000071 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)
Eric Christophere7b87e52014-10-26 23:40:33 +000081 : SaveAndRestoreLocation(CGF, B) {
Adrian Prantld1b151e2014-01-17 00:15:10 +000082 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.
Eric Christophere7b87e52014-10-26 23:40:33 +0000105 if (Loc.isInvalid())
106 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000107
108 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
109
110 // If we've changed files in the middle of a lexical scope go ahead
111 // and create a new lexical scope with file node if it's different
112 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000113 if (LexicalBlockStack.empty())
114 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000115
116 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +0000117 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000118 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000119
David Blaikieaabde052014-05-14 00:29:00 +0000120 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000121 return;
122
Guy Benyei11169dd2012-12-18 14:30:41 +0000123 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000124 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Eric Christophere7b87e52014-10-26 23:40:33 +0000125 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
126 LBF.getScope(), getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 llvm::MDNode *N = D;
128 LexicalBlockStack.pop_back();
129 LexicalBlockStack.push_back(N);
David Blaikie0a21d0d2013-01-26 22:16:26 +0000130 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000131 llvm::DIDescriptor D =
132 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000133 llvm::MDNode *N = D;
134 LexicalBlockStack.pop_back();
135 LexicalBlockStack.push_back(N);
136 }
137}
138
139/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000140llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000141 if (!Context)
142 return TheCU;
143
Eric Christophere7b87e52014-10-26 23:40:33 +0000144 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
145 RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000146 if (I != RegionMap.end()) {
147 llvm::Value *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000148 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000149 }
150
151 // Check namespace.
152 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000153 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000154
David Blaikiebfa52742013-04-19 06:56:38 +0000155 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
156 if (!RDecl->isDependentType())
157 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000158 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 return TheCU;
160}
161
162/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000163/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000164/// is stored on the side.
165StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000166 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000167 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000168 FunctionTemplateSpecializationInfo *Info =
169 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000170 if (!Info && FII)
171 return FII->getName();
172
173 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000174 SmallString<128> NS;
175 llvm::raw_svector_ostream OS(NS);
176 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000177
178 // Add any template specialization args.
179 if (Info) {
180 const TemplateArgumentList *TArgs = Info->TemplateArguments;
181 const TemplateArgument *Args = TArgs->data();
182 unsigned NumArgs = TArgs->size();
183 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000184 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
185 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 }
187
188 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000189 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000190}
191
192StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
193 SmallString<256> MethodName;
194 llvm::raw_svector_ostream OS(MethodName);
195 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
196 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000197 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000198 dyn_cast<const ObjCImplementationDecl>(DC)) {
199 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000200 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000201 dyn_cast<const ObjCInterfaceDecl>(DC)) {
202 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000203 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000204 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
205 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
206 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000207 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000208 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000209 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000210 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000211 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000212 ClassTy.print(OS, PrintingPolicy(LangOptions()));
213 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000214 }
215 OS << ' ' << OMD->getSelector().getAsString() << ']';
216
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000217 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000218}
219
220/// getSelectorName - Return selector name. This is used for debugging
221/// info.
222StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000223 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000224}
225
226/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000227StringRef CGDebugInfo::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 =
Eric Christophere7b87e52014-10-26 23:40:33 +0000260 DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
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);
Eric Christophere7b87e52014-10-26 23:40:33 +0000286 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000287}
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);
Eric Christophere7b87e52014-10-26 23:40:33 +0000300 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000301}
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)
Eric Christophere7b87e52014-10-26 23:40:33 +0000391#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000392#include "clang/AST/BuiltinTypes.def"
393 case BuiltinType::Dependent:
394 llvm_unreachable("Unexpected builtin type");
395 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000396 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000397 case BuiltinType::Void:
398 return llvm::DIType();
399 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000400 if (!ClassTy)
401 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
402 "objc_class", TheCU,
403 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000404 return ClassTy;
405 case BuiltinType::ObjCId: {
406 // typedef struct objc_class *Class;
407 // typedef struct objc_object {
408 // Class isa;
409 // } *id;
410
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000411 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 return ObjTy;
413
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000414 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000415 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
416 "objc_class", TheCU,
417 getOrCreateMainFile(), 0);
418
419 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000420
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
422
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000423 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000424 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
425 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000426
Eric Christophere7b87e52014-10-26 23:40:33 +0000427 ObjTy.setArrays(DBuilder.getOrCreateArray(
428 &*DBuilder.createMemberType(ObjTy, "isa", getOrCreateMainFile(), 0,
429 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:
Eric Christophere7b87e52014-10-26 23:40:33 +0000441 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000443 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444 OCLImage1dArrayDITy);
445 case BuiltinType::OCLImage1dBuffer:
446 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
447 OCLImage1dBufferDITy);
448 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000449 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000450 case BuiltinType::OCLImage2dArray:
451 return getOrCreateStructPtrType("opencl_image2d_array_t",
452 OCLImage2dArrayDITy);
453 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000455 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000456 return DBuilder.createBasicType(
457 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
458 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000459 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000460 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000461
Guy Benyei11169dd2012-12-18 14:30:41 +0000462 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000463 case BuiltinType::Char_U:
464 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
465 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000467 case BuiltinType::SChar:
468 Encoding = llvm::dwarf::DW_ATE_signed_char;
469 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000470 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000471 case BuiltinType::Char32:
472 Encoding = llvm::dwarf::DW_ATE_UTF;
473 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000474 case BuiltinType::UShort:
475 case BuiltinType::UInt:
476 case BuiltinType::UInt128:
477 case BuiltinType::ULong:
478 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000479 case BuiltinType::ULongLong:
480 Encoding = llvm::dwarf::DW_ATE_unsigned;
481 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000482 case BuiltinType::Short:
483 case BuiltinType::Int:
484 case BuiltinType::Int128:
485 case BuiltinType::Long:
486 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000487 case BuiltinType::LongLong:
488 Encoding = llvm::dwarf::DW_ATE_signed;
489 break;
490 case BuiltinType::Bool:
491 Encoding = llvm::dwarf::DW_ATE_boolean;
492 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000493 case BuiltinType::Half:
494 case BuiltinType::Float:
495 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000496 case BuiltinType::Double:
497 Encoding = llvm::dwarf::DW_ATE_float;
498 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000499 }
500
501 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000502 case BuiltinType::Long:
503 BTName = "long int";
504 break;
505 case BuiltinType::LongLong:
506 BTName = "long long int";
507 break;
508 case BuiltinType::ULong:
509 BTName = "long unsigned int";
510 break;
511 case BuiltinType::ULongLong:
512 BTName = "long long unsigned int";
513 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000514 default:
515 BTName = BT->getName(CGM.getLangOpts());
516 break;
517 }
518 // Bit size, align and offset of the type.
519 uint64_t Size = CGM.getContext().getTypeSize(BT);
520 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000521 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 return DbgTy;
523}
524
525llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
526 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000527 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 if (Ty->isComplexIntegerType())
529 Encoding = llvm::dwarf::DW_ATE_lo_user;
530
531 uint64_t Size = CGM.getContext().getTypeSize(Ty);
532 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000533 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000534 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000535
536 return DbgTy;
537}
538
539/// CreateCVRType - Get the qualified type from the cache or create
540/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000541llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 QualifierCollector Qc;
543 const Type *T = Qc.strip(Ty);
544
545 // Ignore these qualifiers for now.
546 Qc.removeObjCGCAttr();
547 Qc.removeAddressSpace();
548 Qc.removeObjCLifetime();
549
550 // We will create one Derived type for one qualifier and recurse to handle any
551 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000552 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 if (Qc.hasConst()) {
554 Tag = llvm::dwarf::DW_TAG_const_type;
555 Qc.removeConst();
556 } else if (Qc.hasVolatile()) {
557 Tag = llvm::dwarf::DW_TAG_volatile_type;
558 Qc.removeVolatile();
559 } else if (Qc.hasRestrict()) {
560 Tag = llvm::dwarf::DW_TAG_restrict_type;
561 Qc.removeRestrict();
562 } else {
563 assert(Qc.empty() && "Unknown type qualifier for debug info");
564 return getOrCreateType(QualType(T, 0), Unit);
565 }
566
David Blaikie99dab3b2013-09-04 22:03:57 +0000567 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568
569 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
570 // CVR derived types.
571 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000572
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 return DbgTy;
574}
575
576llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
577 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000578
579 // The frontend treats 'id' as a typedef to an ObjCObjectType,
580 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
581 // debug info, we want to emit 'id' in both cases.
582 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000583 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000584
Eric Christophere7b87e52014-10-26 23:40:33 +0000585 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
586 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000587 return DbgTy;
588}
589
Eric Christophere7b87e52014-10-26 23:40:33 +0000590llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000591 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 Ty->getPointeeType(), Unit);
593}
594
Manman Rene0064d82013-08-29 23:19:58 +0000595/// In C++ mode, types have linkage, so we can rely on the ODR and
596/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000597static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
598 CodeGenModule &CGM,
599 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000600 SmallString<256> FullName;
601 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
602 // For now, only apply ODR with C++.
603 const TagDecl *TD = Ty->getDecl();
604 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
605 !TD->isExternallyVisible())
606 return FullName;
607 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
608 if (CGM.getTarget().getCXXABI().isMicrosoft())
609 return FullName;
610
611 // TODO: This is using the RTTI name. Is there a better way to get
612 // a unique string for a type?
613 llvm::raw_svector_ostream Out(FullName);
614 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
615 Out.flush();
616 return FullName;
617}
618
Guy Benyei11169dd2012-12-18 14:30:41 +0000619// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000620llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000621CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000622 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000623 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000624 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000625 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000626 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
627 unsigned Line = getLineNumber(RD->getLocation());
628 StringRef RDName = getClassName(RD);
629
Ed Masteda706022014-05-07 12:49:30 +0000630 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000631 if (RD->isStruct() || RD->isInterface())
632 Tag = llvm::dwarf::DW_TAG_structure_type;
633 else if (RD->isUnion())
634 Tag = llvm::dwarf::DW_TAG_union_type;
635 else {
636 assert(RD->isClass());
637 Tag = llvm::dwarf::DW_TAG_class_type;
638 }
639
640 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000641 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000642 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
643 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
644 ReplaceMap.push_back(std::make_pair(Ty, static_cast<llvm::Value *>(RetTy)));
645 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000646}
647
Ed Masteda706022014-05-07 12:49:30 +0000648llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000649 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000650 QualType PointeeTy,
651 llvm::DIFile Unit) {
652 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
653 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000654 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000655
Guy Benyei11169dd2012-12-18 14:30:41 +0000656 // Bit size, align and offset of the type.
657 // Size is always the size of a pointer. We can't use getTypeSize here
658 // because that does not return the correct value for references.
659 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000660 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000661 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
662
David Blaikie99dab3b2013-09-04 22:03:57 +0000663 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
664 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000665}
666
Eric Christopher0fdcb312013-05-16 00:52:20 +0000667llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
668 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000669 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000670 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000671 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
672 TheCU, getOrCreateMainFile(), 0);
673 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
674 Cache = DBuilder.createPointerType(Cache, Size);
675 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000676}
677
Guy Benyei11169dd2012-12-18 14:30:41 +0000678llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
679 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000680 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 return BlockLiteralGeneric;
682
683 SmallVector<llvm::Value *, 8> EltTys;
684 llvm::DIType FieldTy;
685 QualType FType;
686 uint64_t FieldSize, FieldOffset;
687 unsigned FieldAlign;
688 llvm::DIArray Elements;
689 llvm::DIType EltTy, DescTy;
690
691 FieldOffset = 0;
692 FType = CGM.getContext().UnsignedLongTy;
693 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
694 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
695
696 Elements = DBuilder.getOrCreateArray(EltTys);
697 EltTys.clear();
698
699 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
700 unsigned LineNo = getLineNumber(CurLoc);
701
Eric Christophere7b87e52014-10-26 23:40:33 +0000702 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
703 FieldOffset, 0, Flags, llvm::DIType(),
704 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000705
706 // Bit size, align and offset of the type.
707 uint64_t Size = CGM.getContext().getTypeSize(Ty);
708
709 DescTy = DBuilder.createPointerType(EltTy, Size);
710
711 FieldOffset = 0;
712 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
713 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
714 FType = CGM.getContext().IntTy;
715 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
716 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000717 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
719
720 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
721 FieldTy = DescTy;
722 FieldSize = CGM.getContext().getTypeSize(Ty);
723 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000724 FieldTy =
725 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
726 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727 EltTys.push_back(FieldTy);
728
729 FieldOffset += FieldSize;
730 Elements = DBuilder.getOrCreateArray(EltTys);
731
Eric Christophere7b87e52014-10-26 23:40:33 +0000732 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
733 LineNo, FieldOffset, 0, Flags,
734 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000735
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
737 return BlockLiteralGeneric;
738}
739
Eric Christophere7b87e52014-10-26 23:40:33 +0000740llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
741 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000742 assert(Ty->isTypeAlias());
743 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000744
745 SmallString<128> NS;
746 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000747 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
748 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000749
750 TemplateSpecializationType::PrintTemplateArgumentList(
751 OS, Ty->getArgs(), Ty->getNumArgs(),
752 CGM.getContext().getPrintingPolicy());
753
Eric Christophere7b87e52014-10-26 23:40:33 +0000754 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
755 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000756
757 SourceLocation Loc = AliasDecl->getLocation();
758 llvm::DIFile File = getOrCreateFile(Loc);
759 unsigned Line = getLineNumber(Loc);
760
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 llvm::DIDescriptor Ctxt =
762 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000763
764 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
765}
766
David Blaikie99dab3b2013-09-04 22:03:57 +0000767llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 // Typedefs are derived from some other type. If we have a typedef of a
769 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000770 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 // We don't set size information, but do specify where the typedef was
772 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000773 SourceLocation Loc = Ty->getDecl()->getLocation();
774 llvm::DIFile File = getOrCreateFile(Loc);
775 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000776 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000777
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000779 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000780
Eric Christophere7b87e52014-10-26 23:40:33 +0000781 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
782 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000783}
784
785llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
786 llvm::DIFile Unit) {
787 SmallVector<llvm::Value *, 16> EltTys;
788
789 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000790 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000791
792 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000793 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000794 if (isa<FunctionNoProtoType>(Ty))
795 EltTys.push_back(DBuilder.createUnspecifiedParameter());
796 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000797 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
798 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000799 if (FPT->isVariadic())
800 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 }
802
Manman Ren67f005e2014-07-28 22:24:34 +0000803 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000804 return DBuilder.createSubroutineType(Unit, EltTypeArray);
805}
806
Adrian Prantl21361fb2014-08-29 22:44:27 +0000807/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
808/// As an optimization, return 0 if the access specifier equals the
809/// default for the containing type.
810static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
811 AccessSpecifier Default = clang::AS_none;
812 if (RD && RD->isClass())
813 Default = clang::AS_private;
814 else if (RD && (RD->isStruct() || RD->isUnion()))
815 Default = clang::AS_public;
816
817 if (Access == Default)
818 return 0;
819
Eric Christophere7b87e52014-10-26 23:40:33 +0000820 switch (Access) {
821 case clang::AS_private:
822 return llvm::DIDescriptor::FlagPrivate;
823 case clang::AS_protected:
824 return llvm::DIDescriptor::FlagProtected;
825 case clang::AS_public:
826 return llvm::DIDescriptor::FlagPublic;
827 case clang::AS_none:
828 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000829 }
830 llvm_unreachable("unexpected access enumerator");
831}
Guy Benyei11169dd2012-12-18 14:30:41 +0000832
Eric Christophere7b87e52014-10-26 23:40:33 +0000833llvm::DIType CGDebugInfo::createFieldType(
834 StringRef name, QualType type, uint64_t sizeInBitsOverride,
835 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
836 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 llvm::DIType debugType = getOrCreateType(type, tunit);
838
839 // Get the location for the field.
840 llvm::DIFile file = getOrCreateFile(loc);
841 unsigned line = getLineNumber(loc);
842
David Majnemer34b57492014-07-30 01:30:47 +0000843 uint64_t SizeInBits = 0;
844 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000846 TypeInfo TI = CGM.getContext().getTypeInfo(type);
847 SizeInBits = TI.Width;
848 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000849
850 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000851 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 }
853
Adrian Prantl21361fb2014-08-29 22:44:27 +0000854 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000855 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
856 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000857}
858
Eric Christopher91a31902013-01-16 01:22:32 +0000859/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000860void
861CGDebugInfo::CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
862 SmallVectorImpl<llvm::Value *> &elements,
863 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000864 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
865 // has the name and the location of the variable so we should iterate over
866 // both concurrently.
867 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
868 RecordDecl::field_iterator Field = CXXDecl->field_begin();
869 unsigned fieldno = 0;
870 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000871 E = CXXDecl->captures_end();
872 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000873 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000874 if (C.capturesVariable()) {
875 VarDecl *V = C.getCapturedVar();
876 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
877 StringRef VName = V->getName();
878 uint64_t SizeInBitsOverride = 0;
879 if (Field->isBitField()) {
880 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
881 assert(SizeInBitsOverride && "found named 0-width bitfield");
882 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000883 llvm::DIType fieldType = createFieldType(
884 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
885 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
886 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000887 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000888 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000889 // TODO: Need to handle 'this' in some way by probably renaming the
890 // this of the lambda class and having a field member of 'this' or
891 // by using AT_object_pointer for the function and having that be
892 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000893 FieldDecl *f = *Field;
894 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
895 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000896 llvm::DIType fieldType = createFieldType(
897 "this", type, 0, f->getLocation(), f->getAccess(),
898 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000899
900 elements.push_back(fieldType);
901 }
902 }
903}
904
David Blaikie6943dea2013-08-20 01:28:15 +0000905/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000906llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
907 llvm::DIType RecordTy,
908 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000909 // Create the descriptor for the static variable, with or without
910 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000911 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000912 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
913 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
914
Eric Christopher91a31902013-01-16 01:22:32 +0000915 unsigned LineNumber = getLineNumber(Var->getLocation());
916 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000917 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000918 if (Var->getInit()) {
919 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000920 if (Value) {
921 if (Value->isInt())
922 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
923 if (Value->isFloat())
924 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
925 }
Eric Christopher91a31902013-01-16 01:22:32 +0000926 }
927
Adrian Prantl21361fb2014-08-29 22:44:27 +0000928 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000929 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
930 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Eric Christopher91a31902013-01-16 01:22:32 +0000931 StaticDataMemberCache[Var->getCanonicalDecl()] = llvm::WeakVH(GV);
David Blaikieae019462013-08-15 22:50:29 +0000932 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000933}
934
935/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000936void CGDebugInfo::CollectRecordNormalField(
937 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
938 SmallVectorImpl<llvm::Value *> &elements, llvm::DIType RecordTy,
939 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000940 StringRef name = field->getName();
941 QualType type = field->getType();
942
943 // Ignore unnamed fields unless they're anonymous structs/unions.
944 if (name.empty() && !type->isRecordType())
945 return;
946
947 uint64_t SizeInBitsOverride = 0;
948 if (field->isBitField()) {
949 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
950 assert(SizeInBitsOverride && "found named 0-width bitfield");
951 }
952
Eric Christophere7b87e52014-10-26 23:40:33 +0000953 llvm::DIType fieldType =
954 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
955 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000956
957 elements.push_back(fieldType);
958}
959
Guy Benyei11169dd2012-12-18 14:30:41 +0000960/// CollectRecordFields - A helper function to collect debug info for
961/// record fields. This is used while creating debug info entry for a Record.
David Blaikieab255bb2013-08-16 20:40:25 +0000962void CGDebugInfo::CollectRecordFields(const RecordDecl *record,
963 llvm::DIFile tunit,
964 SmallVectorImpl<llvm::Value *> &elements,
965 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
967
Eric Christopher91a31902013-01-16 01:22:32 +0000968 if (CXXDecl && CXXDecl->isLambda())
969 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
970 else {
971 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000972
Eric Christopher91a31902013-01-16 01:22:32 +0000973 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000974 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000975
Eric Christopher91a31902013-01-16 01:22:32 +0000976 // Static and non-static members should appear in the same order as
977 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000978 for (const auto *I : record->decls())
979 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000980 // Reuse the existing static member declaration if one exists
981 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI =
982 StaticDataMemberCache.find(V->getCanonicalDecl());
983 if (MI != StaticDataMemberCache.end()) {
984 assert(MI->second &&
985 "Static data member declaration should still exist");
986 elements.push_back(
987 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000988 } else {
989 auto Field = CreateRecordStaticField(V, RecordTy, record);
990 elements.push_back(Field);
991 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000992 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000993 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
994 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000995
996 // Bump field number for next field.
997 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000998 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000999 }
1000}
1001
1002/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1003/// function type is not updated to include implicit "this" pointer. Use this
1004/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001005llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001006CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1007 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001008 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001009 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001010 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001011 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1012 Func, Unit);
1013}
David Blaikie2aaf0652013-01-07 22:24:59 +00001014
David Blaikie469f0792013-05-22 23:22:42 +00001015llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001016 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001017 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001018 llvm::DITypeArray Args = llvm::DISubroutineType(
1019 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001020 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
1022 SmallVector<llvm::Value *, 16> Elts;
1023
1024 // First element is always return type. For 'void' functions it is NULL.
1025 Elts.push_back(Args.getElement(0));
1026
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001028 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001029 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1030 // Create pointer type directly in this case.
1031 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1032 QualType PointeeTy = ThisPtrTy->getPointeeType();
1033 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001034 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1036 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001037 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001038 DBuilder.createPointerType(PointeeType, Size, Align);
David Blaikie2aaf0652013-01-07 22:24:59 +00001039 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1040 // TODO: This and the artificial type below are misleading, the
1041 // types aren't artificial the argument is, but the current
1042 // metadata doesn't represent that.
1043 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1044 Elts.push_back(ThisPtrType);
1045 } else {
1046 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
1047 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
1048 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1049 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 }
1051
1052 // Copy rest of the arguments.
1053 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1054 Elts.push_back(Args.getElement(i));
1055
Manman Ren67f005e2014-07-28 22:24:34 +00001056 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001057
Adrian Prantl0630eb72013-12-18 21:48:18 +00001058 unsigned Flags = 0;
1059 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1060 Flags |= llvm::DIDescriptor::FlagLValueReference;
1061 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1062 Flags |= llvm::DIDescriptor::FlagRValueReference;
1063
1064 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001065}
1066
Eric Christopherb2a008c2013-05-16 00:45:12 +00001067/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001068/// inside a function.
1069static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1070 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1071 return isFunctionLocalClass(NRD);
1072 if (isa<FunctionDecl>(RD->getDeclContext()))
1073 return true;
1074 return false;
1075}
1076
1077/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1078/// a single member function GlobalDecl.
1079llvm::DISubprogram
1080CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001081 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001082 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001083 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001084
Guy Benyei11169dd2012-12-18 14:30:41 +00001085 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001086 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001087
1088 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1089 // make sense to give a single ctor/dtor a linkage name.
1090 StringRef MethodLinkageName;
1091 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1092 MethodLinkageName = CGM.getMangledName(Method);
1093
1094 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001095 llvm::DIFile MethodDefUnit;
1096 unsigned MethodLine = 0;
1097 if (!Method->isImplicit()) {
1098 MethodDefUnit = getOrCreateFile(Method->getLocation());
1099 MethodLine = getLineNumber(Method->getLocation());
1100 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001101
1102 // Collect virtual method info.
1103 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001106
Guy Benyei11169dd2012-12-18 14:30:41 +00001107 if (Method->isVirtual()) {
1108 if (Method->isPure())
1109 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1110 else
1111 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001112
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 // It doesn't make sense to give a virtual destructor a vtable index,
1114 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001115 // FIXME: Add proper support for debug info for virtual calls in
1116 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1117 // lookup if we have multiple or virtual inheritance.
1118 if (!isa<CXXDestructorDecl>(Method) &&
1119 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001120 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001121 ContainingType = RecordTy;
1122 }
1123
1124 unsigned Flags = 0;
1125 if (Method->isImplicit())
1126 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001127 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001128 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1129 if (CXXC->isExplicit())
1130 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001131 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001132 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 if (CXXC->isExplicit())
1134 Flags |= llvm::DIDescriptor::FlagExplicit;
1135 }
1136 if (Method->hasPrototype())
1137 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001138 if (Method->getRefQualifier() == RQ_LValue)
1139 Flags |= llvm::DIDescriptor::FlagLValueReference;
1140 if (Method->getRefQualifier() == RQ_RValue)
1141 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001142
1143 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001144 llvm::DISubprogram SP = DBuilder.createMethod(
1145 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1146 MethodTy, /*isLocalToUnit=*/false,
1147 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1148 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001149
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
1151
1152 return SP;
1153}
1154
1155/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001156/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001157/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001158void CGDebugInfo::CollectCXXMemberFunctions(
1159 const CXXRecordDecl *RD, llvm::DIFile Unit,
1160 SmallVectorImpl<llvm::Value *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001161
1162 // Since we want more than just the individual member decls if we
1163 // have templated functions iterate over every declaration to gather
1164 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001165 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001166 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1167 // If the member is implicit, don't add it to the member list. This avoids
1168 // the member being added to type units by LLVM, while still allowing it
1169 // to be emitted into the type declaration/reference inside the compile
1170 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001171 // FIXME: Handle Using(Shadow?)Decls here to create
1172 // DW_TAG_imported_declarations inside the class for base decls brought into
1173 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1174 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1175 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001176 if (!Method || Method->isImplicit())
1177 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001178
1179 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1180 continue;
1181
David Blaikiefd580722014-10-06 05:18:55 +00001182 // Reuse the existing member function declaration if it exists.
1183 // It may be associated with the declaration of the type & should be
1184 // reused as we're building the definition.
1185 //
1186 // This situation can arise in the vtable-based debug info reduction where
1187 // implicit members are emitted in a non-vtable TU.
1188 auto MI = SPCache.find(Method->getCanonicalDecl());
1189 EltTys.push_back(MI == SPCache.end()
1190 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Aaron Ballmand2a3f472014-10-06 12:42:31 +00001191 : static_cast<llvm::Value *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001192 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001193}
Guy Benyei11169dd2012-12-18 14:30:41 +00001194
Guy Benyei11169dd2012-12-18 14:30:41 +00001195/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001196/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001197/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001198void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
1199 SmallVectorImpl<llvm::Value *> &EltTys,
1200 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001201
1202 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001203 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001204 unsigned BFlags = 0;
1205 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001206
Guy Benyei11169dd2012-12-18 14:30:41 +00001207 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001208 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001209
Aaron Ballman574705e2014-03-13 15:41:46 +00001210 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001211 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1212 // virtual base offset offset is -ve. The code generator emits dwarf
1213 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001214 BaseOffset = 0 - CGM.getItaniumVTableContext()
1215 .getVirtualBaseOffsetOffset(RD, Base)
1216 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001217 } else {
1218 // In the MS ABI, store the vbtable offset, which is analogous to the
1219 // vbase offset offset in Itanium.
1220 BaseOffset =
1221 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1222 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 BFlags = llvm::DIDescriptor::FlagVirtual;
1224 } else
1225 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1226 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1227 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001228
Adrian Prantl21361fb2014-08-29 22:44:27 +00001229 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001230 llvm::DIType DTy = DBuilder.createInheritance(
1231 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001232 EltTys.push_back(DTy);
1233 }
1234}
1235
1236/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001237llvm::DIArray
1238CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1239 ArrayRef<TemplateArgument> TAList,
1240 llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001241 SmallVector<llvm::Value *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1243 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001244 StringRef Name;
1245 if (TPList)
1246 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001247 switch (TA.getKind()) {
1248 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1250 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001251 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001252 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001253 } break;
1254 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001255 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1256 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001257 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001258 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001259 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1260 TemplateParams.push_back(TVP);
1261 } break;
1262 case TemplateArgument::Declaration: {
1263 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001264 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001265 llvm::DIType TTy = getOrCreateType(T, Unit);
Craig Topper8a13c412014-05-21 05:09:00 +00001266 llvm::Value *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001267 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001268 // Variable pointer template parameters have a value that is the address
1269 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001270 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001271 V = CGM.GetAddrOfGlobalVar(VD);
1272 // Member function pointers have special support for building them, though
1273 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001274 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001275 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001276 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001277 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001278 // Member data pointers have special handling too to compute the fixed
1279 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001280 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001281 // These five lines (& possibly the above member function pointer
1282 // handling) might be able to be refactored to use similar code in
1283 // CodeGenModule::getMemberPointerConstant
1284 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1285 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001286 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001287 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001288 }
1289 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001290 DBuilder.createTemplateValueParameter(
1291 TheCU, Name, TTy,
1292 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001293 TemplateParams.push_back(TVP);
1294 } break;
1295 case TemplateArgument::NullPtr: {
1296 QualType T = TA.getNullPtrType();
1297 llvm::DIType TTy = getOrCreateType(T, Unit);
Craig Topper8a13c412014-05-21 05:09:00 +00001298 llvm::Value *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001299 // Special case member data pointer null values since they're actually -1
1300 // instead of zero.
1301 if (const MemberPointerType *MPT =
1302 dyn_cast<MemberPointerType>(T.getTypePtr()))
1303 // But treat member function pointers as simple zero integers because
1304 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1305 // CodeGen grows handling for values of non-null member function
1306 // pointers then perhaps we could remove this special case and rely on
1307 // EmitNullMemberPointer for member function pointers.
1308 if (MPT->isMemberDataPointer())
1309 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1310 if (!V)
1311 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1312 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001313 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1314 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001315 TemplateParams.push_back(TVP);
1316 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001317 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001318 llvm::DITemplateValueParameter
1319 TVP = DBuilder.createTemplateTemplateParameter(
1320 TheCU, Name, llvm::DIType(),
1321 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001322 TemplateParams.push_back(TVP);
1323 } break;
1324 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001325 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1326 TheCU, Name, llvm::DIType(),
1327 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();
David Majnemer922ad9f2014-10-24 19:49:04 +00001333 if (E->isGLValue())
1334 T = CGM.getContext().getLValueReferenceType(T);
David Majnemer5559d472013-08-24 08:21:10 +00001335 llvm::Value *V = CGM.EmitConstantExpr(E, T);
1336 assert(V && "Expression in template argument isn't constant");
1337 llvm::DIType TTy = getOrCreateType(T, Unit);
1338 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001339 DBuilder.createTemplateValueParameter(
1340 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001341 TemplateParams.push_back(TVP);
1342 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001343 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001344 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001345 case TemplateArgument::Null:
1346 llvm_unreachable(
1347 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 }
1349 }
1350 return DBuilder.getOrCreateArray(TemplateParams);
1351}
1352
1353/// CollectFunctionTemplateParams - A helper function to collect debug
1354/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001355llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1356 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 if (FD->getTemplatedKind() ==
1358 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001359 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1360 ->getTemplate()
1361 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001362 return CollectTemplateParams(
1363 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 }
1365 return llvm::DIArray();
1366}
1367
1368/// CollectCXXTemplateParams - A helper function to collect debug info for
1369/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001370llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1371 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001372 // Always get the full list of parameters, not just the ones from
1373 // the specialization.
1374 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001375 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001376 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001377 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001378}
1379
1380/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1381llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1382 if (VTablePtrType.isValid())
1383 return VTablePtrType;
1384
1385 ASTContext &Context = CGM.getContext();
1386
1387 /* Function type */
1388 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001389 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001390 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1391 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001392 llvm::DIType vtbl_ptr_type =
1393 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1395 return VTablePtrType;
1396}
1397
1398/// getVTableName - Get vtable name for the given Class.
1399StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001400 // Copy the gdb compatible name on the side and use its reference.
1401 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001402}
1403
Guy Benyei11169dd2012-12-18 14:30:41 +00001404/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1405/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001406void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1407 SmallVectorImpl<llvm::Value *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1409
1410 // If there is a primary base then it will hold vtable info.
1411 if (RL.getPrimaryBase())
1412 return;
1413
1414 // If this class is not dynamic then there is not any vtable info to collect.
1415 if (!RD->isDynamicClass())
1416 return;
1417
1418 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001419 llvm::DIType VPTR = DBuilder.createMemberType(
1420 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1421 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 EltTys.push_back(VPTR);
1423}
1424
Eric Christopherb2a008c2013-05-16 00:45:12 +00001425/// getOrCreateRecordType - Emit record type's standalone debug info.
1426llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001428 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001429 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1430 return T;
1431}
1432
1433/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1434/// debug info.
1435llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001436 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001437 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001439 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 return T;
1441}
1442
David Blaikie483a9da2014-05-06 18:35:21 +00001443void CGDebugInfo::completeType(const EnumDecl *ED) {
1444 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1445 return;
1446 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001447 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001448 auto I = TypeCache.find(TyPtr);
1449 if (I == TypeCache.end() ||
1450 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1451 .isForwardDecl())
1452 return;
1453 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1454 assert(!Res.isForwardDecl());
1455 TypeCache[TyPtr] = Res;
1456}
1457
David Blaikieb2e86eb2013-08-15 20:49:17 +00001458void CGDebugInfo::completeType(const RecordDecl *RD) {
1459 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1460 !CGM.getLangOpts().CPlusPlus)
1461 completeRequiredType(RD);
1462}
1463
1464void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001465 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1466 return;
1467
David Blaikie6943dea2013-08-20 01:28:15 +00001468 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1469 if (CXXDecl->isDynamicClass())
1470 return;
1471
David Blaikieb2e86eb2013-08-15 20:49:17 +00001472 QualType Ty = CGM.getContext().getRecordType(RD);
1473 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001474 if (T && T.isForwardDecl())
1475 completeClassData(RD);
1476}
1477
1478void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1479 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001480 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001481 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001482 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001483 auto I = TypeCache.find(TyPtr);
1484 if (I != TypeCache.end() &&
1485 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1486 .isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001487 return;
1488 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1489 assert(!Res.isForwardDecl());
David Blaikieb2e86eb2013-08-15 20:49:17 +00001490 TypeCache[TyPtr] = Res;
1491}
1492
David Blaikie0e716b42014-03-03 23:48:23 +00001493static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1494 CXXRecordDecl::method_iterator End) {
1495 for (; I != End; ++I)
1496 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001497 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1498 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001499 return true;
1500 return false;
1501}
1502
1503static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1504 const RecordDecl *RD,
1505 const LangOptions &LangOpts) {
1506 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1507 return false;
1508
1509 if (!LangOpts.CPlusPlus)
1510 return false;
1511
1512 if (!RD->isCompleteDefinitionRequired())
1513 return true;
1514
1515 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1516
1517 if (!CXXDecl)
1518 return false;
1519
1520 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1521 return true;
1522
1523 TemplateSpecializationKind Spec = TSK_Undeclared;
1524 if (const ClassTemplateSpecializationDecl *SD =
1525 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1526 Spec = SD->getSpecializationKind();
1527
1528 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1529 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1530 CXXDecl->method_end()))
1531 return true;
1532
1533 return false;
1534}
1535
Guy Benyei11169dd2012-12-18 14:30:41 +00001536/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001537llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001538 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001539 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001540 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001541 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001542 T = getOrCreateRecordFwdDecl(
1543 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001544 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001545 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001546
David Blaikieb2e86eb2013-08-15 20:49:17 +00001547 return CreateTypeDefinition(Ty);
1548}
1549
1550llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1551 RecordDecl *RD = Ty->getDecl();
1552
Guy Benyei11169dd2012-12-18 14:30:41 +00001553 // Get overall information about the record type for the debug info.
1554 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1555
1556 // Records and classes and unions can all be recursive. To handle them, we
1557 // first generate a debug descriptor for the struct as a forward declaration.
1558 // Then (if it is a definition) we go through and get debug info for all of
1559 // its members. Finally, we create a descriptor for the complete type (which
1560 // may refer to the forward decl if the struct is recursive) and replace all
1561 // uses of the forward declaration with the final definition.
1562
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001563 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001564 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001565 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001566
1567 if (FwdDecl.isForwardDecl())
1568 return FwdDecl;
1569
David Blaikieadfbf992013-08-18 16:55:33 +00001570 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1571 CollectContainingType(CXXDecl, FwdDecl);
1572
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001574 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1576
Guy Benyei11169dd2012-12-18 14:30:41 +00001577 // Convert all the elements.
1578 SmallVector<llvm::Value *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001579 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001580
1581 // Note: The split of CXXDecl information here is intentional, the
1582 // gdb tests will depend on a certain ordering at printout. The debug
1583 // information offsets are still correct if we merge them all together
1584 // though.
1585 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1586 if (CXXDecl) {
1587 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1588 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1589 }
1590
Eric Christopher91a31902013-01-16 01:22:32 +00001591 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001593 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001595
1596 LexicalBlockStack.pop_back();
1597 RegionMap.erase(Ty->getDecl());
1598
1599 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001600 FwdDecl.setArrays(Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001601
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001602 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1603 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001604}
1605
1606/// CreateType - get objective-c object type.
1607llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1608 llvm::DIFile Unit) {
1609 // Ignore protocols.
1610 return getOrCreateType(Ty->getBaseType(), Unit);
1611}
1612
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001613/// \return true if Getter has the default name for the property PD.
1614static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1615 const ObjCMethodDecl *Getter) {
1616 assert(PD);
1617 if (!Getter)
1618 return true;
1619
1620 assert(Getter->getDeclName().isObjCZeroArgSelector());
1621 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001622 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001623}
1624
1625/// \return true if Setter has the default name for the property PD.
1626static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1627 const ObjCMethodDecl *Setter) {
1628 assert(PD);
1629 if (!Setter)
1630 return true;
1631
1632 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001633 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001634 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001635}
1636
Guy Benyei11169dd2012-12-18 14:30:41 +00001637/// CreateType - get objective-c interface type.
1638llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1639 llvm::DIFile Unit) {
1640 ObjCInterfaceDecl *ID = Ty->getDecl();
1641 if (!ID)
1642 return llvm::DIType();
1643
1644 // Get overall information about the record type for the debug info.
1645 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1646 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001647 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001648
1649 // If this is just a forward declaration return a special forward-declaration
1650 // debug type since we won't be able to lay out the entire type.
1651 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001652 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001653 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1654 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1655 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001656 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001657 return FwdDecl;
1658 }
1659
David Blaikieef8a9512014-05-05 23:23:53 +00001660 return CreateTypeDefinition(Ty, Unit);
1661}
1662
Eric Christophere7b87e52014-10-26 23:40:33 +00001663llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1664 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001665 ObjCInterfaceDecl *ID = Ty->getDecl();
1666 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1667 unsigned Line = getLineNumber(ID->getLocation());
1668 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001669
1670 // Bit size, align and offset of the type.
1671 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1672 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1673
1674 unsigned Flags = 0;
1675 if (ID->getImplementation())
1676 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1677
Eric Christophere7b87e52014-10-26 23:40:33 +00001678 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1679 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1680 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
David Blaikieef8a9512014-05-05 23:23:53 +00001682 QualType QTy(Ty, 0);
1683 TypeCache[QTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001685 // Push the struct on region stack.
Eric Christophere7b87e52014-10-26 23:40:33 +00001686 LexicalBlockStack.push_back(static_cast<llvm::MDNode *>(RealDecl));
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1688
1689 // Convert all the elements.
1690 SmallVector<llvm::Value *, 16> EltTys;
1691
1692 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1693 if (SClass) {
1694 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001695 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 if (!SClassTy.isValid())
1697 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001698
Eric Christophere7b87e52014-10-26 23:40:33 +00001699 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 EltTys.push_back(InhTag);
1701 }
1702
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001703 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001704 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 SourceLocation Loc = PD->getLocation();
1706 llvm::DIFile PUnit = getOrCreateFile(Loc);
1707 unsigned PLine = getLineNumber(Loc);
1708 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1709 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001710 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1711 PD->getName(), PUnit, PLine,
1712 hasDefaultGetterName(PD, Getter) ? ""
1713 : getSelectorName(PD->getGetterName()),
1714 hasDefaultSetterName(PD, Setter) ? ""
1715 : getSelectorName(PD->getSetterName()),
1716 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 EltTys.push_back(PropertyNode);
1718 }
1719
1720 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1721 unsigned FieldNo = 0;
1722 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1723 Field = Field->getNextIvar(), ++FieldNo) {
1724 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1725 if (!FieldTy.isValid())
1726 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001727
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 StringRef FieldName = Field->getName();
1729
1730 // Ignore unnamed fields.
1731 if (FieldName.empty())
1732 continue;
1733
1734 // Get the location for the field.
1735 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1736 unsigned FieldLine = getLineNumber(Field->getLocation());
1737 QualType FType = Field->getType();
1738 uint64_t FieldSize = 0;
1739 unsigned FieldAlign = 0;
1740
1741 if (!FType->isIncompleteArrayType()) {
1742
1743 // Bit size, align and offset of the type.
1744 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001745 ? Field->getBitWidthValue(CGM.getContext())
1746 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001747 FieldAlign = CGM.getContext().getTypeAlign(FType);
1748 }
1749
1750 uint64_t FieldOffset;
1751 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1752 // We don't know the runtime offset of an ivar if we're using the
1753 // non-fragile ABI. For bitfields, use the bit offset into the first
1754 // byte of storage of the bitfield. For other fields, use zero.
1755 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001756 FieldOffset =
1757 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001758 FieldOffset %= CGM.getContext().getCharWidth();
1759 } else {
1760 FieldOffset = 0;
1761 }
1762 } else {
1763 FieldOffset = RL.getFieldOffset(FieldNo);
1764 }
1765
1766 unsigned Flags = 0;
1767 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1768 Flags = llvm::DIDescriptor::FlagProtected;
1769 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1770 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001771 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1772 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001773
Craig Topper8a13c412014-05-21 05:09:00 +00001774 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001776 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001777 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001779 SourceLocation Loc = PD->getLocation();
1780 llvm::DIFile PUnit = getOrCreateFile(Loc);
1781 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1783 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001784 PropertyNode = DBuilder.createObjCProperty(
1785 PD->getName(), PUnit, PLine,
1786 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1787 PD->getGetterName()),
1788 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1789 PD->getSetterName()),
1790 PD->getPropertyAttributes(),
1791 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 }
1793 }
1794 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001795 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1796 FieldSize, FieldAlign, FieldOffset, Flags,
1797 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 EltTys.push_back(FieldTy);
1799 }
1800
1801 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001802 RealDecl.setArrays(Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001803
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001805 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001806}
1807
1808llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1809 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1810 int64_t Count = Ty->getNumElements();
1811 if (Count == 0)
1812 // If number of elements are not known then this is an unbounded array.
1813 // Use Count == -1 to express such arrays.
1814 Count = -1;
1815
1816 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1817 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1818
1819 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1820 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1821
1822 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1823}
1824
Eric Christophere7b87e52014-10-26 23:40:33 +00001825llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 uint64_t Size;
1827 uint64_t Align;
1828
1829 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1830 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1831 Size = 0;
1832 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001833 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001834 } else if (Ty->isIncompleteArrayType()) {
1835 Size = 0;
1836 if (Ty->getElementType()->isIncompleteType())
1837 Align = 0;
1838 else
1839 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001840 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 Size = 0;
1842 Align = 0;
1843 } else {
1844 // Size and align of the whole array, not the element type.
1845 Size = CGM.getContext().getTypeSize(Ty);
1846 Align = CGM.getContext().getTypeAlign(Ty);
1847 }
1848
1849 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1850 // interior arrays, do we care? Why aren't nested arrays represented the
1851 // obvious/recursive way?
1852 SmallVector<llvm::Value *, 8> Subscripts;
1853 QualType EltTy(Ty, 0);
1854 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1855 // If the number of elements is known, then count is that number. Otherwise,
1856 // it's -1. This allows us to represent a subrange with an array of 0
1857 // elements, like this:
1858 //
1859 // struct foo {
1860 // int x[0];
1861 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001862 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001863 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1864 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001865
Guy Benyei11169dd2012-12-18 14:30:41 +00001866 // FIXME: Verify this is right for VLAs.
1867 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1868 EltTy = Ty->getElementType();
1869 }
1870
1871 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1872
Eric Christophere7b87e52014-10-26 23:40:33 +00001873 llvm::DIType DbgTy = DBuilder.createArrayType(
1874 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 return DbgTy;
1876}
1877
Eric Christopherb2a008c2013-05-16 00:45:12 +00001878llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001879 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001880 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1881 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001882}
1883
Eric Christopherb2a008c2013-05-16 00:45:12 +00001884llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001885 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001886 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1887 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001888}
1889
Eric Christopherb2a008c2013-05-16 00:45:12 +00001890llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001892 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1893 if (!Ty->getPointeeType()->isFunctionType())
1894 return DBuilder.createMemberPointerType(
David Blaikie99dab3b2013-09-04 22:03:57 +00001895 getOrCreateType(Ty->getPointeeType(), U), ClassType);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001896
1897 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 Ty->getPointeeType()->getAs<FunctionProtoType>();
1899 return DBuilder.createMemberPointerType(
1900 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1901 Ty->getClass(), FPT->getTypeQuals())),
1902 FPT, U),
1903 ClassType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001904}
1905
Eric Christophere7b87e52014-10-26 23:40:33 +00001906llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 // Ignore the atomic wrapping
1908 // FIXME: What is the correct representation?
1909 return getOrCreateType(Ty->getValueType(), U);
1910}
1911
1912/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001913llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001914 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 uint64_t Size = 0;
1916 uint64_t Align = 0;
1917 if (!ED->getTypeForDecl()->isIncompleteType()) {
1918 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1919 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1920 }
1921
Manman Rene0064d82013-08-29 23:19:58 +00001922 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1923
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 // If this is just a forward declaration, construct an appropriately
1925 // marked node and just return it.
1926 if (!ED->getDefinition()) {
1927 llvm::DIDescriptor EDContext;
1928 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1929 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1930 unsigned Line = getLineNumber(ED->getLocation());
1931 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001932 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1933 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1934 0, Size, Align, FullName);
1935 ReplaceMap.push_back(std::make_pair(Ty, static_cast<llvm::Value *>(RetTy)));
1936 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 }
1938
David Blaikie483a9da2014-05-06 18:35:21 +00001939 return CreateTypeDefinition(Ty);
1940}
1941
1942llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1943 const EnumDecl *ED = Ty->getDecl();
1944 uint64_t Size = 0;
1945 uint64_t Align = 0;
1946 if (!ED->getTypeForDecl()->isIncompleteType()) {
1947 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1948 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1949 }
1950
1951 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1952
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 // Create DIEnumerator elements for each enumerator.
1954 SmallVector<llvm::Value *, 16> Enumerators;
1955 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001956 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001957 Enumerators.push_back(DBuilder.createEnumerator(
1958 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001959 }
1960
1961 // Return a CompositeType for the enum itself.
1962 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1963
1964 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1965 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001966 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001967 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1968 llvm::DIType ClassTy = ED->isFixed()
1969 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1970 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001971 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001972 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1973 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 return DbgTy;
1975}
1976
David Blaikie05491062013-01-21 04:37:12 +00001977static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1978 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001980 Qualifiers InnerQuals = T.getLocalQualifiers();
1981 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1982 // that is already there.
1983 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1984 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 QualType LastT = T;
1986 switch (T->getTypeClass()) {
1987 default:
David Blaikie05491062013-01-21 04:37:12 +00001988 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001989 case Type::TemplateSpecialization: {
1990 const auto *Spec = cast<TemplateSpecializationType>(T);
1991 if (Spec->isTypeAlias())
1992 return C.getQualifiedType(T.getTypePtr(), Quals);
1993 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001994 break;
1995 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 case Type::TypeOfExpr:
1997 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1998 break;
1999 case Type::TypeOf:
2000 T = cast<TypeOfType>(T)->getUnderlyingType();
2001 break;
2002 case Type::Decltype:
2003 T = cast<DecltypeType>(T)->getUnderlyingType();
2004 break;
2005 case Type::UnaryTransform:
2006 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2007 break;
2008 case Type::Attributed:
2009 T = cast<AttributedType>(T)->getEquivalentType();
2010 break;
2011 case Type::Elaborated:
2012 T = cast<ElaboratedType>(T)->getNamedType();
2013 break;
2014 case Type::Paren:
2015 T = cast<ParenType>(T)->getInnerType();
2016 break;
David Blaikie05491062013-01-21 04:37:12 +00002017 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 break;
2020 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002021 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002022 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002023 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 break;
2025 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002026
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002028 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 } while (true);
2030}
2031
Eric Christopher0fdcb312013-05-16 00:52:20 +00002032/// getType - Get the type from the cache or return null type if it doesn't
2033/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002034llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2035
2036 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002037 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002038
David Blaikief427b002014-05-06 03:42:01 +00002039 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 if (it != TypeCache.end()) {
2041 // Verify that the debug info still exists.
2042 if (llvm::Value *V = it->second)
2043 return llvm::DIType(cast<llvm::MDNode>(V));
2044 }
2045
2046 return llvm::DIType();
2047}
2048
David Blaikie0e716b42014-03-03 23:48:23 +00002049void CGDebugInfo::completeTemplateDefinition(
2050 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002051 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2052 return;
2053
David Blaikie0e716b42014-03-03 23:48:23 +00002054 completeClassData(&SD);
2055 // In case this type has no member function definitions being emitted, ensure
2056 // it is retained
2057 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2058}
2059
Guy Benyei11169dd2012-12-18 14:30:41 +00002060/// getOrCreateType - Get the type from the cache or create a new
2061/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002062llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002063 if (Ty.isNull())
2064 return llvm::DIType();
2065
2066 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002067 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002068
David Blaikieef8a9512014-05-05 23:23:53 +00002069 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 return T;
2071
2072 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002073 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002074 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002075
2076 // And update the type cache.
2077 TypeCache[TyPtr] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002078
Guy Benyei11169dd2012-12-18 14:30:41 +00002079 return Res;
2080}
2081
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002082/// Currently the checksum of an interface includes the number of
2083/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002084unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002085 // The assumption is that the number of ivars can only increase
2086 // monotonically, so it is safe to just use their current number as
2087 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002088 unsigned Sum = 0;
2089 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002090 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002091 ++Sum;
2092
2093 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002094}
2095
2096ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2097 switch (Ty->getTypeClass()) {
2098 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002099 return getObjCInterfaceDecl(
2100 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002101 case Type::ObjCInterface:
2102 return cast<ObjCInterfaceType>(Ty)->getDecl();
2103 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002104 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002105 }
2106}
2107
Guy Benyei11169dd2012-12-18 14:30:41 +00002108/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002109llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 // Handle qualifiers, which recursively handles what they refer to.
2111 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002112 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002113
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 // Work out details of type.
2115 switch (Ty->getTypeClass()) {
2116#define TYPE(Class, Base)
2117#define ABSTRACT_TYPE(Class, Base)
2118#define NON_CANONICAL_TYPE(Class, Base)
2119#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2120#include "clang/AST/TypeNodes.def"
2121 llvm_unreachable("Dependent types cannot show up in debug information");
2122
2123 case Type::ExtVector:
2124 case Type::Vector:
2125 return CreateType(cast<VectorType>(Ty), Unit);
2126 case Type::ObjCObjectPointer:
2127 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2128 case Type::ObjCObject:
2129 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2130 case Type::ObjCInterface:
2131 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2132 case Type::Builtin:
2133 return CreateType(cast<BuiltinType>(Ty));
2134 case Type::Complex:
2135 return CreateType(cast<ComplexType>(Ty));
2136 case Type::Pointer:
2137 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002138 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002139 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002140 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002141 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002142 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 case Type::BlockPointer:
2144 return CreateType(cast<BlockPointerType>(Ty), Unit);
2145 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002146 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002148 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002150 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 case Type::FunctionProto:
2152 case Type::FunctionNoProto:
2153 return CreateType(cast<FunctionType>(Ty), Unit);
2154 case Type::ConstantArray:
2155 case Type::VariableArray:
2156 case Type::IncompleteArray:
2157 return CreateType(cast<ArrayType>(Ty), Unit);
2158
2159 case Type::LValueReference:
2160 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2161 case Type::RValueReference:
2162 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2163
2164 case Type::MemberPointer:
2165 return CreateType(cast<MemberPointerType>(Ty), Unit);
2166
2167 case Type::Atomic:
2168 return CreateType(cast<AtomicType>(Ty), Unit);
2169
Guy Benyei11169dd2012-12-18 14:30:41 +00002170 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002171 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2172
David Blaikie42edade2014-11-11 20:44:45 +00002173 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002174 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 case Type::Elaborated:
2176 case Type::Paren:
2177 case Type::SubstTemplateTypeParm:
2178 case Type::TypeOfExpr:
2179 case Type::TypeOf:
2180 case Type::Decltype:
2181 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002182 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002183 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002185
David Blaikie42edade2014-11-11 20:44:45 +00002186 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002187}
2188
2189/// getOrCreateLimitedType - Get the type from the cache or create a new
2190/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002191llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002192 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002193 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194
David Blaikie8d5e1282013-08-20 21:03:29 +00002195 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002196
2197 // We may have cached a forward decl when we could have created
2198 // a non-forward decl. Go ahead and create a non-forward decl
2199 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002200 if (T && !T.isForwardDecl())
2201 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202
2203 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002204 llvm::DICompositeType Res = CreateLimitedType(Ty);
2205
2206 // Propagate members from the declaration to the definition
2207 // CreateType(const RecordType*) will overwrite this with the members in the
2208 // correct order if the full type is needed.
Manman Ren51289892014-07-28 19:14:41 +00002209 Res.setArrays(T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002210
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 // And update the type cache.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002212 TypeCache[QTy.getAsOpaquePtr()] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 return Res;
2214}
2215
2216// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002217llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002219
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 // Get overall information about the record type for the debug info.
2221 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2222 unsigned Line = getLineNumber(RD->getLocation());
2223 StringRef RDName = getClassName(RD);
2224
Eric Christopher07429ff2013-10-15 21:22:34 +00002225 llvm::DIDescriptor RDContext =
2226 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
David Blaikied2785892013-08-18 17:36:19 +00002228 // If we ended up creating the type during the context chain construction,
2229 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002230 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2231 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002232 return T;
David Blaikied2785892013-08-18 17:36:19 +00002233
Adrian Prantl381e7552014-02-04 21:29:50 +00002234 // If this is just a forward or incomplete declaration, construct an
2235 // appropriately marked node and just return it.
2236 const RecordDecl *D = RD->getDefinition();
2237 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002238 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002239
2240 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2241 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002242 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002243
Manman Rene0064d82013-08-29 23:19:58 +00002244 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2245
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002247 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2248 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002249 else if (RD->isClass()) {
2250 // FIXME: This could be a struct type giving a default visibility different
2251 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002252 RealDecl = DBuilder.createClassType(
2253 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2254 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002256 RealDecl = DBuilder.createStructType(
2257 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2258 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002259
2260 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie49ae6a72013-03-26 23:47:35 +00002261 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002262
David Blaikieadfbf992013-08-18 16:55:33 +00002263 if (const ClassTemplateSpecializationDecl *TSpecial =
2264 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Manman Ren51289892014-07-28 19:14:41 +00002265 RealDecl.setArrays(llvm::DIArray(),
Eric Christophere7b87e52014-10-26 23:40:33 +00002266 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002267 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002268}
2269
David Blaikieadfbf992013-08-18 16:55:33 +00002270void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2271 llvm::DICompositeType RealDecl) {
2272 // A class's primary base or the class itself contains the vtable.
2273 llvm::DICompositeType ContainingType;
2274 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2275 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002276 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002277 while (1) {
2278 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2279 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2280 if (PBT && !BRL.isPrimaryBaseVirtual())
2281 PBase = PBT;
2282 else
2283 break;
2284 }
2285 ContainingType = llvm::DICompositeType(
2286 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2287 getOrCreateFile(RD->getLocation())));
2288 } else if (RD->isDynamicClass())
2289 ContainingType = RealDecl;
2290
2291 RealDecl.setContainingType(ContainingType);
2292}
2293
Guy Benyei11169dd2012-12-18 14:30:41 +00002294/// CreateMemberType - Create new member and increase Offset by FType's size.
2295llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002296 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2298 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2299 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002300 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2301 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002302 *Offset += FieldSize;
2303 return Ty;
2304}
2305
Frederic Riss9db79f12014-11-18 03:40:46 +00002306void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2307 llvm::DIFile Unit,
2308 StringRef &Name, StringRef &LinkageName,
2309 llvm::DIDescriptor &FDContext,
2310 llvm::DIArray &TParamsArray,
2311 unsigned &Flags) {
2312 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2313 Name = getFunctionName(FD);
2314 // Use mangled name as linkage name for C/C++ functions.
2315 if (FD->hasPrototype()) {
2316 LinkageName = CGM.getMangledName(GD);
2317 Flags |= llvm::DIDescriptor::FlagPrototyped;
2318 }
2319 // No need to replicate the linkage name if it isn't different from the
2320 // subprogram name, no need to have it at all unless coverage is enabled or
2321 // debug is set to more than just line tables.
2322 if (LinkageName == Name ||
2323 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2324 !CGM.getCodeGenOpts().EmitGcovNotes &&
2325 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2326 LinkageName = StringRef();
2327
2328 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2329 if (const NamespaceDecl *NSDecl =
2330 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2331 FDContext = getOrCreateNameSpace(NSDecl);
2332 else if (const RecordDecl *RDecl =
2333 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2334 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2335 // Collect template parameters.
2336 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2337 }
2338}
2339
2340void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2341 unsigned &LineNo, QualType &T,
2342 StringRef &Name, StringRef &LinkageName,
2343 llvm::DIDescriptor &VDContext) {
2344 Unit = getOrCreateFile(VD->getLocation());
2345 LineNo = getLineNumber(VD->getLocation());
2346
2347 setLocation(VD->getLocation());
2348
2349 T = VD->getType();
2350 if (T->isIncompleteArrayType()) {
2351 // CodeGen turns int[] into int[1] so we'll do the same here.
2352 llvm::APInt ConstVal(32, 1);
2353 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2354
2355 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2356 ArrayType::Normal, 0);
2357 }
2358
2359 Name = VD->getName();
2360 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2361 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2362 LinkageName = CGM.getMangledName(VD);
2363 if (LinkageName == Name)
2364 LinkageName = StringRef();
2365
2366 // Since we emit declarations (DW_AT_members) for static members, place the
2367 // definition of those static members in the namespace they were declared in
2368 // in the source code (the lexical decl context).
2369 // FIXME: Generalize this for even non-member global variables where the
2370 // declaration and definition may have different lexical decl contexts, once
2371 // we have support for emitting declarations of (non-member) global variables.
2372 VDContext = getContextDescriptor(
2373 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2374 : VD->getDeclContext()));
2375}
2376
Frederic Riss442293e2014-11-06 21:12:06 +00002377llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002378 // We only need a declaration (not a definition) of the type - so use whatever
2379 // we would otherwise do to get a type for a pointee. (forward declarations in
2380 // limited debug info, full definitions (if the type definition is available)
2381 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002382 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2383 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002384 getOrCreateFile(TD->getLocation()));
David Blaikiebd483762013-05-20 04:58:53 +00002385 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2386 // This doesn't handle providing declarations (for functions or variables) for
2387 // entities without definitions in this TU, nor when the definition proceeds
2388 // the call to this function.
2389 // FIXME: This should be split out into more specific maps with support for
2390 // emitting forward declarations and merging definitions with declarations,
2391 // the same way as we do for types.
2392 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2393 DeclCache.find(D->getCanonicalDecl());
2394 if (I == DeclCache.end())
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00002395 return llvm::DIScope();
David Blaikiebd483762013-05-20 04:58:53 +00002396 llvm::Value *V = I->second;
Frederic Riss442293e2014-11-06 21:12:06 +00002397 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikiebd483762013-05-20 04:58:53 +00002398}
2399
Guy Benyei11169dd2012-12-18 14:30:41 +00002400/// getFunctionDeclaration - Return debug info descriptor to describe method
2401/// declaration for the given method definition.
2402llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002403 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002404 return llvm::DISubprogram();
2405
Guy Benyei11169dd2012-12-18 14:30:41 +00002406 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002407 if (!FD)
2408 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002409
2410 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002411 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002412
Eric Christophere7b87e52014-10-26 23:40:33 +00002413 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI =
2414 SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002415 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002416 if (const CXXMethodDecl *MD =
2417 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002418 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002419 llvm::DISubprogram SP =
2420 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002421 return SP;
2422 }
2423 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 if (MI != SPCache.end()) {
2425 llvm::Value *V = MI->second;
2426 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002427 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002428 return SP;
2429 }
2430
Aaron Ballman86c93902014-03-06 23:45:36 +00002431 for (auto NextFD : FD->redecls()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002432 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI =
2433 SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002434 if (MI != SPCache.end()) {
2435 llvm::Value *V = MI->second;
2436 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002437 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002438 return SP;
2439 }
2440 }
2441 return llvm::DISubprogram();
2442}
2443
2444// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2445// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002446llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2447 QualType FnType,
2448 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002449 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002450 // Create fake but valid subroutine type. Otherwise
2451 // llvm::DISubprogram::Verify() would return false, and
2452 // subprogram DIE will miss DW_AT_decl_file and
2453 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002454 return DBuilder.createSubroutineType(F,
2455 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002456
2457 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2458 return getOrCreateMethodType(Method, F);
2459 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2460 // Add "self" and "_cmd"
2461 SmallVector<llvm::Value *, 16> Elts;
2462
2463 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002464 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002465
2466 // Replace the instancetype keyword with the actual type.
2467 if (ResultTy == CGM.getContext().getObjCInstanceType())
2468 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002469 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002470
Adrian Prantl7bec9032013-05-10 21:08:31 +00002471 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002473 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2474 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2475 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 // "_cmd" pointer is always second argument.
2477 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2478 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2479 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002480 for (const auto *PI : OMethod->params())
2481 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002482 // Variadic methods need a special marker at the end of the type list.
2483 if (OMethod->isVariadic())
2484 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002485
Manman Ren67f005e2014-07-28 22:24:34 +00002486 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 return DBuilder.createSubroutineType(F, EltTypeArray);
2488 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002489
Adrian Prantl800faef2014-02-25 23:42:18 +00002490 // Handle variadic function types; they need an additional
2491 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002492 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2493 if (FD->isVariadic()) {
2494 SmallVector<llvm::Value *, 16> EltTys;
2495 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2496 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2497 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2498 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2499 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002500 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002501 return DBuilder.createSubroutineType(F, EltTypeArray);
2502 }
2503
David Blaikie469f0792013-05-22 23:22:42 +00002504 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002505}
2506
2507/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002508void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2509 SourceLocation ScopeLoc, QualType FnType,
2510 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002511
2512 StringRef Name;
2513 StringRef LinkageName;
2514
2515 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2516
2517 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002518 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002519
Guy Benyei11169dd2012-12-18 14:30:41 +00002520 unsigned Flags = 0;
2521 llvm::DIFile Unit = getOrCreateFile(Loc);
2522 llvm::DIDescriptor FDContext(Unit);
2523 llvm::DIArray TParamsArray;
2524 if (!HasDecl) {
2525 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002526 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2528 // If there is a DISubprogram for this function available then use it.
Eric Christophere7b87e52014-10-26 23:40:33 +00002529 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator FI =
2530 SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 if (FI != SPCache.end()) {
2532 llvm::Value *V = FI->second;
2533 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2534 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2535 llvm::MDNode *SPN = SP;
2536 LexicalBlockStack.push_back(SPN);
2537 RegionMap[D] = llvm::WeakVH(SP);
2538 return;
2539 }
2540 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002541 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2542 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2544 Name = getObjCMethodName(OMD);
2545 Flags |= llvm::DIDescriptor::FlagPrototyped;
2546 } else {
2547 // Use llvm function name.
2548 Name = Fn->getName();
2549 Flags |= llvm::DIDescriptor::FlagPrototyped;
2550 }
2551 if (!Name.empty() && Name[0] == '\01')
2552 Name = Name.substr(1);
2553
Adrian Prantl42d71b92014-04-10 23:21:53 +00002554 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002556 // Artificial functions without a location should not silently reuse CurLoc.
2557 if (Loc.isInvalid())
2558 CurLoc = SourceLocation();
2559 }
2560 unsigned LineNo = getLineNumber(Loc);
2561 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
Eric Christopher8018e412014-03-27 18:50:35 +00002563 // FIXME: The function declaration we're constructing here is mostly reusing
2564 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2565 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2566 // all subprograms instead of the actual context since subprogram definitions
2567 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002568 llvm::DISubprogram SP = DBuilder.createFunction(
2569 FDContext, Name, LinkageName, Unit, LineNo,
2570 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2571 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2572 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002573 // We might get here with a VarDecl in the case we're generating
2574 // code for the initialization of globals. Do not record these decls
2575 // as they will overwrite the actual VarDecl Decl in the cache.
2576 if (HasDecl && isa<FunctionDecl>(D))
David Blaikiebd483762013-05-20 04:58:53 +00002577 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002578
Adrian Prantlbebb8932014-03-21 21:01:58 +00002579 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 llvm::MDNode *SPN = SP;
2581 LexicalBlockStack.push_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002582
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 if (HasDecl)
2584 RegionMap[D] = llvm::WeakVH(SP);
2585}
2586
2587/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002588/// information in the source file. If the location is invalid, the
2589/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002590void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002591 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 // Update our current location
2593 setLocation(Loc);
2594
Eric Christophere7b87e52014-10-26 23:40:33 +00002595 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2596 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002597
2598 // Don't bother if things are the same as last time.
2599 SourceManager &SM = CGM.getContext().getSourceManager();
2600 if (CurLoc == PrevLoc ||
2601 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2602 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002603 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2604 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002605 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002606 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002607
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 // Update last state.
2609 PrevLoc = CurLoc;
2610
Adrian Prantle83b1302014-01-07 22:05:52 +00002611 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002612 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2613 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002614}
2615
2616/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2617/// the stack.
2618void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
David Blaikief9ea2422014-06-02 16:32:05 +00002619 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
2620 llvm::DIDescriptor(LexicalBlockStack.empty() ? nullptr
2621 : LexicalBlockStack.back()),
David Blaikieb8449842014-08-21 22:46:45 +00002622 getOrCreateFile(CurLoc), getLineNumber(CurLoc), getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 llvm::MDNode *DN = D;
2624 LexicalBlockStack.push_back(DN);
2625}
2626
2627/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2628/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002629void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2630 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 // Set our current location.
2632 setLocation(Loc);
2633
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002635 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2636 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002637
2638 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2639 return;
2640
2641 // Create a new lexical block and push it on the stack.
2642 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002643}
2644
2645/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2646/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002647void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2648 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002649 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2650
2651 // Provide an entry in the line table for the end of the block.
2652 EmitLocation(Builder, Loc);
2653
David Blaikie60a877b2014-10-22 19:34:33 +00002654 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2655 return;
2656
Guy Benyei11169dd2012-12-18 14:30:41 +00002657 LexicalBlockStack.pop_back();
2658}
2659
2660/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2661void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2662 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2663 unsigned RCount = FnBeginRegionCount.back();
2664 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2665
2666 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002667 while (LexicalBlockStack.size() != RCount) {
2668 // Provide an entry in the line table for the end of the block.
2669 EmitLocation(Builder, CurLoc);
2670 LexicalBlockStack.pop_back();
2671 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002672 FnBeginRegionCount.pop_back();
2673}
2674
Eric Christopherb2a008c2013-05-16 00:45:12 +00002675// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002676// See BuildByRefType.
2677llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2678 uint64_t *XOffset) {
2679
2680 SmallVector<llvm::Value *, 5> EltTys;
2681 QualType FType;
2682 uint64_t FieldSize, FieldOffset;
2683 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002684
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002686 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002687
2688 FieldOffset = 0;
2689 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2690 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2691 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2692 FType = CGM.getContext().IntTy;
2693 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2694 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2695
2696 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2697 if (HasCopyAndDispose) {
2698 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002699 EltTys.push_back(
2700 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2701 EltTys.push_back(
2702 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 }
2704 bool HasByrefExtendedLayout;
2705 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002706 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2707 HasByrefExtendedLayout) &&
2708 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002709 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002710 EltTys.push_back(
2711 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002712 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002713
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2715 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002716 CGM.getTarget().getPointerAlign(0))) {
2717 CharUnits FieldOffsetInBytes =
2718 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2719 CharUnits AlignedOffsetInBytes =
2720 FieldOffsetInBytes.RoundUpToAlignment(Align);
2721 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002722
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 if (NumPaddingBytes.isPositive()) {
2724 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2725 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2726 pad, ArrayType::Normal, 0);
2727 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2728 }
2729 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002730
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002732 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002733 FieldSize = CGM.getContext().getTypeSize(FType);
2734 FieldAlign = CGM.getContext().toBits(Align);
2735
Eric Christopherb2a008c2013-05-16 00:45:12 +00002736 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002737 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2738 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 EltTys.push_back(FieldTy);
2740 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002741
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002743
Guy Benyei11169dd2012-12-18 14:30:41 +00002744 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002745
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002747 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002748}
2749
2750/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002751void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002752 llvm::Value *Storage, unsigned ArgNo,
2753 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002754 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002755 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2756
David Blaikie7fceebf2013-08-19 03:37:48 +00002757 bool Unwritten =
2758 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2759 cast<Decl>(VD->getDeclContext())->isImplicit());
2760 llvm::DIFile Unit;
2761 if (!Unwritten)
2762 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002763 llvm::DIType Ty;
2764 uint64_t XOffset = 0;
2765 if (VD->hasAttr<BlocksAttr>())
2766 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002767 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 Ty = getOrCreateType(VD->getType(), Unit);
2769
2770 // If there is no debug info for this type then do not emit debug info
2771 // for this variable.
2772 if (!Ty)
2773 return;
2774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002776 unsigned Line = 0;
2777 unsigned Column = 0;
2778 if (!Unwritten) {
2779 Line = getLineNumber(VD->getLocation());
2780 Column = getColumnNumber(VD->getLocation());
2781 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 unsigned Flags = 0;
2783 if (VD->isImplicit())
2784 Flags |= llvm::DIDescriptor::FlagArtificial;
2785 // If this is the first argument and it is implicit then
2786 // give it an object pointer flag.
2787 // FIXME: There has to be a better way to do this, but for static
2788 // functions there won't be an implicit param at arg1 and
2789 // otherwise it is 'self' or 'this'.
2790 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2791 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002792 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002793 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2794 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002795 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002796
2797 llvm::MDNode *Scope = LexicalBlockStack.back();
2798
2799 StringRef Name = VD->getName();
2800 if (!Name.empty()) {
2801 if (VD->hasAttr<BlocksAttr>()) {
2802 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002803 SmallVector<int64_t, 9> addr;
2804 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002805 // offset of __forwarding field
2806 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002807 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002808 addr.push_back(offset.getQuantity());
2809 addr.push_back(llvm::dwarf::DW_OP_deref);
2810 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002811 // offset of x field
2812 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002813 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002814
2815 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002816 llvm::DIVariable D = DBuilder.createLocalVariable(
2817 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002818
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 // Insert an llvm.dbg.declare into the current block.
2820 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002821 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2822 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002823 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2824 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002825 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002826 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002827 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2828 // If VD is an anonymous union then Storage represents value for
2829 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002830 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002831 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002832 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2834 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002835
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 // Ignore unnamed fields. Do not ignore unnamed records.
2837 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2838 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002839
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002841 llvm::DIVariable D = DBuilder.createLocalVariable(
2842 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2843 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002844
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002846 llvm::Instruction *Call = DBuilder.insertDeclare(
2847 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2849 }
David Blaikie219c7d92013-01-05 20:03:07 +00002850 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 }
2852 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002853
2854 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002855 llvm::DIVariable D = DBuilder.createLocalVariable(
2856 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2857 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002858
2859 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002860 llvm::Instruction *Call = DBuilder.insertDeclare(
2861 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002862 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002863}
2864
2865void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2866 llvm::Value *Storage,
2867 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002868 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2870}
2871
Adrian Prantlde17db32013-03-29 19:20:29 +00002872/// Look up the completed type for a self pointer in the TypeCache and
2873/// create a copy of it with the ObjectPointer and Artificial flags
2874/// set. If the type is not cached, a new one is created. This should
2875/// never happen though, since creating a type for the implicit self
2876/// argument implies that we already parsed the interface definition
2877/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002878llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2879 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002880 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002881 if (CachedTy)
2882 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002883 return DBuilder.createObjectPointerType(Ty);
2884}
2885
Eric Christophere7b87e52014-10-26 23:40:33 +00002886void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2887 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2888 const CGBlockInfo &blockInfo) {
Eric Christopher75e17682013-05-16 00:45:23 +00002889 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002890 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002891
Craig Topper8a13c412014-05-21 05:09:00 +00002892 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002893 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002894
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002896
Guy Benyei11169dd2012-12-18 14:30:41 +00002897 uint64_t XOffset = 0;
2898 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2899 llvm::DIType Ty;
2900 if (isByRef)
2901 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002902 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 Ty = getOrCreateType(VD->getType(), Unit);
2904
2905 // Self is passed along as an implicit non-arg variable in a
2906 // block. Mark it as the object pointer.
2907 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002908 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002909
2910 // Get location information.
2911 unsigned Line = getLineNumber(VD->getLocation());
2912 unsigned Column = getColumnNumber(VD->getLocation());
2913
2914 const llvm::DataLayout &target = CGM.getDataLayout();
2915
2916 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002917 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2919
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002920 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002921 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002922 addr.push_back(llvm::dwarf::DW_OP_deref);
2923 addr.push_back(llvm::dwarf::DW_OP_plus);
2924 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002925 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002926 addr.push_back(llvm::dwarf::DW_OP_deref);
2927 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002929 offset =
2930 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002931 addr.push_back(offset.getQuantity());
2932 addr.push_back(llvm::dwarf::DW_OP_deref);
2933 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 // offset of x field
2935 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002936 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 }
2938
2939 // Create the descriptor for the variable.
2940 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002941 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2942 llvm::DIDescriptor(LexicalBlockStack.back()),
2943 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002944
Guy Benyei11169dd2012-12-18 14:30:41 +00002945 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002946 llvm::Instruction *Call = DBuilder.insertDeclare(
2947 Storage, D, DBuilder.createExpression(addr), Builder.GetInsertPoint());
2948 Call->setDebugLoc(
2949 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002950}
2951
2952/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2953/// variable declaration.
2954void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2955 unsigned ArgNo,
2956 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002957 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2959}
2960
2961namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002962struct BlockLayoutChunk {
2963 uint64_t OffsetInBits;
2964 const BlockDecl::Capture *Capture;
2965};
2966bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2967 return l.OffsetInBits < r.OffsetInBits;
2968}
Guy Benyei11169dd2012-12-18 14:30:41 +00002969}
2970
2971void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002972 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002973 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002974 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002976 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002977 ASTContext &C = CGM.getContext();
2978 const BlockDecl *blockDecl = block.getBlockDecl();
2979
2980 // Collect some general information about the block's location.
2981 SourceLocation loc = blockDecl->getCaretLocation();
2982 llvm::DIFile tunit = getOrCreateFile(loc);
2983 unsigned line = getLineNumber(loc);
2984 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002985
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 // Build the debug-info type for the block literal.
2987 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2988
2989 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002990 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00002991
Eric Christophere7b87e52014-10-26 23:40:33 +00002992 SmallVector<llvm::Value *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00002993 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2994 blockLayout->getElementOffsetInBits(0),
2995 tunit, tunit));
2996 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2997 blockLayout->getElementOffsetInBits(1),
2998 tunit, tunit));
2999 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3000 blockLayout->getElementOffsetInBits(2),
3001 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003002 auto *FnTy = block.getBlockExpr()->getFunctionType();
3003 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3004 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 blockLayout->getElementOffsetInBits(3),
3006 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003007 fields.push_back(createFieldType(
3008 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3009 ? C.getBlockDescriptorExtendedType()
3010 : C.getBlockDescriptorType()),
3011 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003012
3013 // We want to sort the captures by offset, not because DWARF
3014 // requires this, but because we're paranoid about debuggers.
3015 SmallVector<BlockLayoutChunk, 8> chunks;
3016
3017 // 'this' capture.
3018 if (blockDecl->capturesCXXThis()) {
3019 BlockLayoutChunk chunk;
3020 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003021 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003022 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003023 chunks.push_back(chunk);
3024 }
3025
3026 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003027 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 const VarDecl *variable = capture.getVariable();
3029 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3030
3031 // Ignore constant captures.
3032 if (captureInfo.isConstant())
3033 continue;
3034
3035 BlockLayoutChunk chunk;
3036 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003037 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 chunk.Capture = &capture;
3039 chunks.push_back(chunk);
3040 }
3041
3042 // Sort by offset.
3043 llvm::array_pod_sort(chunks.begin(), chunks.end());
3044
Eric Christophere7b87e52014-10-26 23:40:33 +00003045 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3046 e = chunks.end();
3047 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 uint64_t offsetInBits = i->OffsetInBits;
3049 const BlockDecl::Capture *capture = i->Capture;
3050
3051 // If we have a null capture, this must be the C++ 'this' capture.
3052 if (!capture) {
3053 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003054 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 QualType type = method->getThisType(C);
3056
3057 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3058 offsetInBits, tunit, tunit));
3059 continue;
3060 }
3061
3062 const VarDecl *variable = capture->getVariable();
3063 StringRef name = variable->getName();
3064
3065 llvm::DIType fieldType;
3066 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003067 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003068
3069 // FIXME: this creates a second copy of this type!
3070 uint64_t xoffset;
3071 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003072 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3073 fieldType =
3074 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3075 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003076 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003077 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3078 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 }
3080 fields.push_back(fieldType);
3081 }
3082
3083 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003084 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3085 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003086
3087 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3088
3089 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003090 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3091 CGM.getContext().toBits(block.BlockSize),
3092 CGM.getContext().toBits(block.BlockAlign), 0,
3093 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3095
3096 // Get overall information about the block.
3097 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3098 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003099
3100 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003101 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3102 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3103 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3104 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003105
Adrian Prantl616bef42013-03-14 21:52:59 +00003106 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003107 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003108 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3109 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3110 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003111 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3112 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003113
Adrian Prantl616bef42013-03-14 21:52:59 +00003114 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003115 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3116 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003117 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003118}
3119
David Blaikie6943dea2013-08-20 01:28:15 +00003120/// If D is an out-of-class definition of a static data member of a class, find
3121/// its corresponding in-class declaration.
3122llvm::DIDerivedType
3123CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3124 if (!D->isStaticDataMember())
3125 return llvm::DIDerivedType();
3126 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI =
3127 StaticDataMemberCache.find(D->getCanonicalDecl());
3128 if (MI != StaticDataMemberCache.end()) {
3129 assert(MI->second && "Static data member declaration should still exist");
3130 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003131 }
David Blaikiece763042013-08-20 21:49:21 +00003132
3133 // If the member wasn't found in the cache, lazily construct and add it to the
3134 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003135 auto DC = D->getDeclContext();
3136 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3137 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003138}
3139
Eric Christophercab9fae2014-04-10 05:20:00 +00003140/// Recursively collect all of the member fields of a global anonymous decl and
3141/// create static variables for them. The first time this is called it needs
3142/// to be on a union and then from there we can have additional unnamed fields.
3143llvm::DIGlobalVariable
3144CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3145 unsigned LineNo, StringRef LinkageName,
3146 llvm::GlobalVariable *Var,
3147 llvm::DIDescriptor DContext) {
3148 llvm::DIGlobalVariable GV;
3149
3150 for (const auto *Field : RD->fields()) {
3151 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3152 StringRef FieldName = Field->getName();
3153
3154 // Ignore unnamed fields, but recurse into anonymous records.
3155 if (FieldName.empty()) {
3156 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3157 if (RT)
3158 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3159 Var, DContext);
3160 continue;
3161 }
3162 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003163 GV = DBuilder.createGlobalVariable(
3164 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3165 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003166 }
3167 return GV;
3168}
3169
Guy Benyei11169dd2012-12-18 14:30:41 +00003170/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003171void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003173 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003174 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003175 llvm::DIFile Unit;
3176 llvm::DIDescriptor DContext;
3177 unsigned LineNo;
3178 StringRef DeclName, LinkageName;
3179 QualType T;
3180 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003181
3182 // Attempt to store one global variable for the declaration - even if we
3183 // emit a lot of fields.
3184 llvm::DIGlobalVariable GV;
3185
3186 // If this is an anonymous union then we'll want to emit a global
3187 // variable for each member of the anonymous union so that it's possible
3188 // to find the name of any field in the union.
3189 if (T->isUnionType() && DeclName.empty()) {
3190 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003191 assert(RD->isAnonymousStructOrUnion() &&
3192 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003193 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3194 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003195 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003196 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3197 Var->hasInternalLinkage(), Var,
3198 getOrCreateStaticDataMemberDeclarationOrNull(D));
3199 }
David Blaikiebd483762013-05-20 04:58:53 +00003200 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003201}
3202
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003203/// EmitGlobalVariable - Emit global variable's debug info.
3204void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3205 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003206 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003207 // Create the descriptor for the variable.
3208 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3209 StringRef Name = VD->getName();
3210 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3211 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3212 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3213 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3214 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3215 }
3216 // Do not use DIGlobalVariable for enums.
3217 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3218 return;
David Blaikiea15565562014-04-04 20:56:17 +00003219 // Do not emit separate definitions for function local const/statics.
3220 if (isa<FunctionDecl>(VD->getDeclContext()))
3221 return;
David Blaikiebb113912014-04-05 07:23:17 +00003222 VD = cast<ValueDecl>(VD->getCanonicalDecl());
3223 auto pair = DeclCache.insert(std::make_pair(VD, llvm::WeakVH()));
3224 if (!pair.second)
3225 return;
David Blaikie506a7452014-04-05 07:46:57 +00003226 llvm::DIDescriptor DContext =
3227 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
Jyoti Allurb76b57f2014-09-29 06:32:54 +00003228 llvm::DIGlobalVariable GV = DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003229 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
3230 true, Init,
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003231 getOrCreateStaticDataMemberDeclarationOrNull(cast<VarDecl>(VD)));
David Blaikiebb113912014-04-05 07:23:17 +00003232 pair.first->second = llvm::WeakVH(GV);
David Blaikiebd483762013-05-20 04:58:53 +00003233}
3234
3235llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3236 if (!LexicalBlockStack.empty())
3237 return llvm::DIScope(LexicalBlockStack.back());
3238 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003239}
3240
David Blaikie9f88fe82013-04-22 06:13:21 +00003241void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003242 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3243 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003244 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003245 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3246 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003247 getLineNumber(UD.getLocation()));
3248}
3249
David Blaikiebd483762013-05-20 04:58:53 +00003250void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3251 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3252 return;
3253 assert(UD.shadow_size() &&
3254 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3255 // Emitting one decl is sufficient - debuggers can detect that this is an
3256 // overloaded name & provide lookup for all the overloads.
3257 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003258 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003259 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003260 DBuilder.createImportedDeclaration(
3261 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3262 getLineNumber(USD.getLocation()));
3263}
3264
David Blaikief121b932013-05-20 22:50:41 +00003265llvm::DIImportedEntity
3266CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3267 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003268 return llvm::DIImportedEntity(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003269 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3270 if (VH)
3271 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003272 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003273 if (const NamespaceAliasDecl *Underlying =
3274 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3275 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003276 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003277 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3278 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3279 NA.getName());
3280 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003281 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003282 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3283 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3284 getLineNumber(NA.getLocation()), NA.getName());
3285 VH = R;
3286 return R;
3287}
3288
Guy Benyei11169dd2012-12-18 14:30:41 +00003289/// getOrCreateNamesSpace - Return namespace descriptor for the given
3290/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003291llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003292CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003293 NSDecl = NSDecl->getCanonicalDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003294 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei11169dd2012-12-18 14:30:41 +00003295 NameSpaceCache.find(NSDecl);
3296 if (I != NameSpaceCache.end())
3297 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003298
Guy Benyei11169dd2012-12-18 14:30:41 +00003299 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3300 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003301 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3303 llvm::DINameSpace NS =
3304 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3305 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3306 return NS;
3307}
3308
3309void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003310 // Creating types might create further types - invalidating the current
3311 // element and the size(), so don't cache/reference them.
3312 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3313 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3314 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3315 E.Type->getDecl()->getDefinition()
3316 ? CreateTypeDefinition(E.Type, E.Unit)
3317 : E.Decl);
3318 }
3319
David Blaikief427b002014-05-06 03:42:01 +00003320 for (auto p : ReplaceMap) {
3321 assert(p.second);
3322 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003323 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003324
David Blaikief427b002014-05-06 03:42:01 +00003325 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003326 assert(it != TypeCache.end());
3327 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003328
David Blaikief427b002014-05-06 03:42:01 +00003329 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3330 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003331 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003332
3333 // We keep our own list of retained types, because we need to look
3334 // up the final type in the type cache.
3335 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3336 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003337 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003338
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 DBuilder.finalize();
3340}
David Blaikie66088d52014-09-24 17:01:27 +00003341
3342void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3343 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3344 return;
3345 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3346 // Don't ignore in case of explicit cast where it is referenced indirectly.
3347 DBuilder.retainType(DieTy);
3348}