blob: f2a5d2723faf794a59913a3add48bbbe4a5da37d [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 =
David Majnemera3644d62013-08-25 22:13:27 +00001290 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1291 V->stripPointerCasts());
David Blaikie38079fd2013-05-10 21:53:14 +00001292 TemplateParams.push_back(TVP);
1293 } break;
1294 case TemplateArgument::NullPtr: {
1295 QualType T = TA.getNullPtrType();
1296 llvm::DIType TTy = getOrCreateType(T, Unit);
Craig Topper8a13c412014-05-21 05:09:00 +00001297 llvm::Value *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001298 // Special case member data pointer null values since they're actually -1
1299 // instead of zero.
1300 if (const MemberPointerType *MPT =
1301 dyn_cast<MemberPointerType>(T.getTypePtr()))
1302 // But treat member function pointers as simple zero integers because
1303 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1304 // CodeGen grows handling for values of non-null member function
1305 // pointers then perhaps we could remove this special case and rely on
1306 // EmitNullMemberPointer for member function pointers.
1307 if (MPT->isMemberDataPointer())
1308 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1309 if (!V)
1310 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1311 llvm::DITemplateValueParameter TVP =
David Blaikie47c11502013-06-22 18:59:18 +00001312 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V);
David Blaikie38079fd2013-05-10 21:53:14 +00001313 TemplateParams.push_back(TVP);
1314 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001315 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001316 llvm::DITemplateValueParameter
1317 TVP = DBuilder.createTemplateTemplateParameter(
1318 TheCU, Name, llvm::DIType(),
1319 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001320 TemplateParams.push_back(TVP);
1321 } break;
1322 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001323 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1324 TheCU, Name, llvm::DIType(),
1325 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001326 TemplateParams.push_back(TVP);
1327 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001328 case TemplateArgument::Expression: {
1329 const Expr *E = TA.getAsExpr();
1330 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001331 if (E->isGLValue())
1332 T = CGM.getContext().getLValueReferenceType(T);
David Majnemer5559d472013-08-24 08:21:10 +00001333 llvm::Value *V = CGM.EmitConstantExpr(E, T);
1334 assert(V && "Expression in template argument isn't constant");
1335 llvm::DIType TTy = getOrCreateType(T, Unit);
1336 llvm::DITemplateValueParameter TVP =
1337 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1338 V->stripPointerCasts());
1339 TemplateParams.push_back(TVP);
1340 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001341 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001342 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::Null:
1344 llvm_unreachable(
1345 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001346 }
1347 }
1348 return DBuilder.getOrCreateArray(TemplateParams);
1349}
1350
1351/// CollectFunctionTemplateParams - A helper function to collect debug
1352/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001353llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1354 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 if (FD->getTemplatedKind() ==
1356 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001357 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1358 ->getTemplate()
1359 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001360 return CollectTemplateParams(
1361 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
1363 return llvm::DIArray();
1364}
1365
1366/// CollectCXXTemplateParams - A helper function to collect debug info for
1367/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001368llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1369 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001370 // Always get the full list of parameters, not just the ones from
1371 // the specialization.
1372 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001373 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001374 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001375 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001376}
1377
1378/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1379llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1380 if (VTablePtrType.isValid())
1381 return VTablePtrType;
1382
1383 ASTContext &Context = CGM.getContext();
1384
1385 /* Function type */
1386 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001387 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1389 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001390 llvm::DIType vtbl_ptr_type =
1391 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1393 return VTablePtrType;
1394}
1395
1396/// getVTableName - Get vtable name for the given Class.
1397StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001398 // Copy the gdb compatible name on the side and use its reference.
1399 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001400}
1401
Guy Benyei11169dd2012-12-18 14:30:41 +00001402/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1403/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001404void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
1405 SmallVectorImpl<llvm::Value *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1407
1408 // If there is a primary base then it will hold vtable info.
1409 if (RL.getPrimaryBase())
1410 return;
1411
1412 // If this class is not dynamic then there is not any vtable info to collect.
1413 if (!RD->isDynamicClass())
1414 return;
1415
1416 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001417 llvm::DIType VPTR = DBuilder.createMemberType(
1418 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1419 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 EltTys.push_back(VPTR);
1421}
1422
Eric Christopherb2a008c2013-05-16 00:45:12 +00001423/// getOrCreateRecordType - Emit record type's standalone debug info.
1424llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001425 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001426 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1428 return T;
1429}
1430
1431/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1432/// debug info.
1433llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001434 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001435 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001437 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 return T;
1439}
1440
David Blaikie483a9da2014-05-06 18:35:21 +00001441void CGDebugInfo::completeType(const EnumDecl *ED) {
1442 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1443 return;
1444 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001445 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001446 auto I = TypeCache.find(TyPtr);
1447 if (I == TypeCache.end() ||
1448 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1449 .isForwardDecl())
1450 return;
1451 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1452 assert(!Res.isForwardDecl());
1453 TypeCache[TyPtr] = Res;
1454}
1455
David Blaikieb2e86eb2013-08-15 20:49:17 +00001456void CGDebugInfo::completeType(const RecordDecl *RD) {
1457 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1458 !CGM.getLangOpts().CPlusPlus)
1459 completeRequiredType(RD);
1460}
1461
1462void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001463 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1464 return;
1465
David Blaikie6943dea2013-08-20 01:28:15 +00001466 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1467 if (CXXDecl->isDynamicClass())
1468 return;
1469
David Blaikieb2e86eb2013-08-15 20:49:17 +00001470 QualType Ty = CGM.getContext().getRecordType(RD);
1471 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001472 if (T && T.isForwardDecl())
1473 completeClassData(RD);
1474}
1475
1476void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1477 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001478 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001479 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001480 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001481 auto I = TypeCache.find(TyPtr);
1482 if (I != TypeCache.end() &&
1483 !llvm::DIType(cast<llvm::MDNode>(static_cast<llvm::Value *>(I->second)))
1484 .isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001485 return;
1486 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1487 assert(!Res.isForwardDecl());
David Blaikieb2e86eb2013-08-15 20:49:17 +00001488 TypeCache[TyPtr] = Res;
1489}
1490
David Blaikie0e716b42014-03-03 23:48:23 +00001491static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1492 CXXRecordDecl::method_iterator End) {
1493 for (; I != End; ++I)
1494 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001495 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1496 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001497 return true;
1498 return false;
1499}
1500
1501static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1502 const RecordDecl *RD,
1503 const LangOptions &LangOpts) {
1504 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1505 return false;
1506
1507 if (!LangOpts.CPlusPlus)
1508 return false;
1509
1510 if (!RD->isCompleteDefinitionRequired())
1511 return true;
1512
1513 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1514
1515 if (!CXXDecl)
1516 return false;
1517
1518 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1519 return true;
1520
1521 TemplateSpecializationKind Spec = TSK_Undeclared;
1522 if (const ClassTemplateSpecializationDecl *SD =
1523 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1524 Spec = SD->getSpecializationKind();
1525
1526 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1527 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1528 CXXDecl->method_end()))
1529 return true;
1530
1531 return false;
1532}
1533
Guy Benyei11169dd2012-12-18 14:30:41 +00001534/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001535llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001536 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001537 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001538 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001539 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001540 T = getOrCreateRecordFwdDecl(
1541 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001542 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001543 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001544
David Blaikieb2e86eb2013-08-15 20:49:17 +00001545 return CreateTypeDefinition(Ty);
1546}
1547
1548llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1549 RecordDecl *RD = Ty->getDecl();
1550
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 // Get overall information about the record type for the debug info.
1552 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1553
1554 // Records and classes and unions can all be recursive. To handle them, we
1555 // first generate a debug descriptor for the struct as a forward declaration.
1556 // Then (if it is a definition) we go through and get debug info for all of
1557 // its members. Finally, we create a descriptor for the complete type (which
1558 // may refer to the forward decl if the struct is recursive) and replace all
1559 // uses of the forward declaration with the final definition.
1560
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001561 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001562 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001563 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001564
1565 if (FwdDecl.isForwardDecl())
1566 return FwdDecl;
1567
David Blaikieadfbf992013-08-18 16:55:33 +00001568 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1569 CollectContainingType(CXXDecl, FwdDecl);
1570
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 // Push the struct on region stack.
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001572 LexicalBlockStack.push_back(&*FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1574
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 // Convert all the elements.
1576 SmallVector<llvm::Value *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001577 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001578
1579 // Note: The split of CXXDecl information here is intentional, the
1580 // gdb tests will depend on a certain ordering at printout. The debug
1581 // information offsets are still correct if we merge them all together
1582 // though.
1583 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1584 if (CXXDecl) {
1585 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1586 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1587 }
1588
Eric Christopher91a31902013-01-16 01:22:32 +00001589 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001591 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001593
1594 LexicalBlockStack.pop_back();
1595 RegionMap.erase(Ty->getDecl());
1596
1597 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001598 FwdDecl.setArrays(Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001599
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001600 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
1601 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001602}
1603
1604/// CreateType - get objective-c object type.
1605llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1606 llvm::DIFile Unit) {
1607 // Ignore protocols.
1608 return getOrCreateType(Ty->getBaseType(), Unit);
1609}
1610
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001611/// \return true if Getter has the default name for the property PD.
1612static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1613 const ObjCMethodDecl *Getter) {
1614 assert(PD);
1615 if (!Getter)
1616 return true;
1617
1618 assert(Getter->getDeclName().isObjCZeroArgSelector());
1619 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001620 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001621}
1622
1623/// \return true if Setter has the default name for the property PD.
1624static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1625 const ObjCMethodDecl *Setter) {
1626 assert(PD);
1627 if (!Setter)
1628 return true;
1629
1630 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001631 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001632 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001633}
1634
Guy Benyei11169dd2012-12-18 14:30:41 +00001635/// CreateType - get objective-c interface type.
1636llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1637 llvm::DIFile Unit) {
1638 ObjCInterfaceDecl *ID = Ty->getDecl();
1639 if (!ID)
1640 return llvm::DIType();
1641
1642 // Get overall information about the record type for the debug info.
1643 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1644 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001645 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001646
1647 // If this is just a forward declaration return a special forward-declaration
1648 // debug type since we won't be able to lay out the entire type.
1649 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001650 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001651 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1652 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1653 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001654 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 return FwdDecl;
1656 }
1657
David Blaikieef8a9512014-05-05 23:23:53 +00001658 return CreateTypeDefinition(Ty, Unit);
1659}
1660
Eric Christophere7b87e52014-10-26 23:40:33 +00001661llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1662 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001663 ObjCInterfaceDecl *ID = Ty->getDecl();
1664 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1665 unsigned Line = getLineNumber(ID->getLocation());
1666 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001667
1668 // Bit size, align and offset of the type.
1669 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1670 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1671
1672 unsigned Flags = 0;
1673 if (ID->getImplementation())
1674 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1675
Eric Christophere7b87e52014-10-26 23:40:33 +00001676 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1677 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1678 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001679
David Blaikieef8a9512014-05-05 23:23:53 +00001680 QualType QTy(Ty, 0);
1681 TypeCache[QTy.getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001682
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001683 // Push the struct on region stack.
Eric Christophere7b87e52014-10-26 23:40:33 +00001684 LexicalBlockStack.push_back(static_cast<llvm::MDNode *>(RealDecl));
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1686
1687 // Convert all the elements.
1688 SmallVector<llvm::Value *, 16> EltTys;
1689
1690 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1691 if (SClass) {
1692 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001693 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 if (!SClassTy.isValid())
1695 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001696
Eric Christophere7b87e52014-10-26 23:40:33 +00001697 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001698 EltTys.push_back(InhTag);
1699 }
1700
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001701 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001702 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001703 SourceLocation Loc = PD->getLocation();
1704 llvm::DIFile PUnit = getOrCreateFile(Loc);
1705 unsigned PLine = getLineNumber(Loc);
1706 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1707 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001708 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1709 PD->getName(), PUnit, PLine,
1710 hasDefaultGetterName(PD, Getter) ? ""
1711 : getSelectorName(PD->getGetterName()),
1712 hasDefaultSetterName(PD, Setter) ? ""
1713 : getSelectorName(PD->getSetterName()),
1714 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 EltTys.push_back(PropertyNode);
1716 }
1717
1718 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1719 unsigned FieldNo = 0;
1720 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1721 Field = Field->getNextIvar(), ++FieldNo) {
1722 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1723 if (!FieldTy.isValid())
1724 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001725
Guy Benyei11169dd2012-12-18 14:30:41 +00001726 StringRef FieldName = Field->getName();
1727
1728 // Ignore unnamed fields.
1729 if (FieldName.empty())
1730 continue;
1731
1732 // Get the location for the field.
1733 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1734 unsigned FieldLine = getLineNumber(Field->getLocation());
1735 QualType FType = Field->getType();
1736 uint64_t FieldSize = 0;
1737 unsigned FieldAlign = 0;
1738
1739 if (!FType->isIncompleteArrayType()) {
1740
1741 // Bit size, align and offset of the type.
1742 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001743 ? Field->getBitWidthValue(CGM.getContext())
1744 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 FieldAlign = CGM.getContext().getTypeAlign(FType);
1746 }
1747
1748 uint64_t FieldOffset;
1749 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1750 // We don't know the runtime offset of an ivar if we're using the
1751 // non-fragile ABI. For bitfields, use the bit offset into the first
1752 // byte of storage of the bitfield. For other fields, use zero.
1753 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001754 FieldOffset =
1755 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001756 FieldOffset %= CGM.getContext().getCharWidth();
1757 } else {
1758 FieldOffset = 0;
1759 }
1760 } else {
1761 FieldOffset = RL.getFieldOffset(FieldNo);
1762 }
1763
1764 unsigned Flags = 0;
1765 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1766 Flags = llvm::DIDescriptor::FlagProtected;
1767 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1768 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001769 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1770 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001771
Craig Topper8a13c412014-05-21 05:09:00 +00001772 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001773 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001774 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001775 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001777 SourceLocation Loc = PD->getLocation();
1778 llvm::DIFile PUnit = getOrCreateFile(Loc);
1779 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001780 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1781 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001782 PropertyNode = DBuilder.createObjCProperty(
1783 PD->getName(), PUnit, PLine,
1784 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1785 PD->getGetterName()),
1786 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1787 PD->getSetterName()),
1788 PD->getPropertyAttributes(),
1789 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 }
1791 }
1792 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001793 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1794 FieldSize, FieldAlign, FieldOffset, Flags,
1795 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001796 EltTys.push_back(FieldTy);
1797 }
1798
1799 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001800 RealDecl.setArrays(Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001801
Guy Benyei11169dd2012-12-18 14:30:41 +00001802 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001803 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001804}
1805
1806llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1807 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1808 int64_t Count = Ty->getNumElements();
1809 if (Count == 0)
1810 // If number of elements are not known then this is an unbounded array.
1811 // Use Count == -1 to express such arrays.
1812 Count = -1;
1813
1814 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, Count);
1815 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1816
1817 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1818 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1819
1820 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1821}
1822
Eric Christophere7b87e52014-10-26 23:40:33 +00001823llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001824 uint64_t Size;
1825 uint64_t Align;
1826
1827 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1828 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1829 Size = 0;
1830 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001831 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 } else if (Ty->isIncompleteArrayType()) {
1833 Size = 0;
1834 if (Ty->getElementType()->isIncompleteType())
1835 Align = 0;
1836 else
1837 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001838 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 Size = 0;
1840 Align = 0;
1841 } else {
1842 // Size and align of the whole array, not the element type.
1843 Size = CGM.getContext().getTypeSize(Ty);
1844 Align = CGM.getContext().getTypeAlign(Ty);
1845 }
1846
1847 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1848 // interior arrays, do we care? Why aren't nested arrays represented the
1849 // obvious/recursive way?
1850 SmallVector<llvm::Value *, 8> Subscripts;
1851 QualType EltTy(Ty, 0);
1852 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1853 // If the number of elements is known, then count is that number. Otherwise,
1854 // it's -1. This allows us to represent a subrange with an array of 0
1855 // elements, like this:
1856 //
1857 // struct foo {
1858 // int x[0];
1859 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001860 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1862 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001863
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 // FIXME: Verify this is right for VLAs.
1865 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1866 EltTy = Ty->getElementType();
1867 }
1868
1869 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1870
Eric Christophere7b87e52014-10-26 23:40:33 +00001871 llvm::DIType DbgTy = DBuilder.createArrayType(
1872 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001873 return DbgTy;
1874}
1875
Eric Christopherb2a008c2013-05-16 00:45:12 +00001876llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001877 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001878 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1879 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001880}
1881
Eric Christopherb2a008c2013-05-16 00:45:12 +00001882llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001884 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1885 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001886}
1887
Eric Christopherb2a008c2013-05-16 00:45:12 +00001888llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001890 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1891 if (!Ty->getPointeeType()->isFunctionType())
1892 return DBuilder.createMemberPointerType(
David Blaikie99dab3b2013-09-04 22:03:57 +00001893 getOrCreateType(Ty->getPointeeType(), U), ClassType);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001894
1895 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001896 Ty->getPointeeType()->getAs<FunctionProtoType>();
1897 return DBuilder.createMemberPointerType(
1898 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1899 Ty->getClass(), FPT->getTypeQuals())),
1900 FPT, U),
1901 ClassType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001902}
1903
Eric Christophere7b87e52014-10-26 23:40:33 +00001904llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001905 // Ignore the atomic wrapping
1906 // FIXME: What is the correct representation?
1907 return getOrCreateType(Ty->getValueType(), U);
1908}
1909
1910/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001911llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001912 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001913 uint64_t Size = 0;
1914 uint64_t Align = 0;
1915 if (!ED->getTypeForDecl()->isIncompleteType()) {
1916 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1917 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1918 }
1919
Manman Rene0064d82013-08-29 23:19:58 +00001920 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1921
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 // If this is just a forward declaration, construct an appropriately
1923 // marked node and just return it.
1924 if (!ED->getDefinition()) {
1925 llvm::DIDescriptor EDContext;
1926 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1927 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1928 unsigned Line = getLineNumber(ED->getLocation());
1929 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001930 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1931 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1932 0, Size, Align, FullName);
1933 ReplaceMap.push_back(std::make_pair(Ty, static_cast<llvm::Value *>(RetTy)));
1934 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 }
1936
David Blaikie483a9da2014-05-06 18:35:21 +00001937 return CreateTypeDefinition(Ty);
1938}
1939
1940llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1941 const EnumDecl *ED = Ty->getDecl();
1942 uint64_t Size = 0;
1943 uint64_t Align = 0;
1944 if (!ED->getTypeForDecl()->isIncompleteType()) {
1945 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1946 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1947 }
1948
1949 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1950
Guy Benyei11169dd2012-12-18 14:30:41 +00001951 // Create DIEnumerator elements for each enumerator.
1952 SmallVector<llvm::Value *, 16> Enumerators;
1953 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001954 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001955 Enumerators.push_back(DBuilder.createEnumerator(
1956 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001957 }
1958
1959 // Return a CompositeType for the enum itself.
1960 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1961
1962 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1963 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001964 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001965 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1966 llvm::DIType ClassTy = ED->isFixed()
1967 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1968 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001969 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001970 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1971 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001972 return DbgTy;
1973}
1974
David Blaikie05491062013-01-21 04:37:12 +00001975static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1976 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001978 Qualifiers InnerQuals = T.getLocalQualifiers();
1979 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1980 // that is already there.
1981 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1982 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001983 QualType LastT = T;
1984 switch (T->getTypeClass()) {
1985 default:
David Blaikie05491062013-01-21 04:37:12 +00001986 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001987 case Type::TemplateSpecialization: {
1988 const auto *Spec = cast<TemplateSpecializationType>(T);
1989 if (Spec->isTypeAlias())
1990 return C.getQualifiedType(T.getTypePtr(), Quals);
1991 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001992 break;
1993 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001994 case Type::TypeOfExpr:
1995 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1996 break;
1997 case Type::TypeOf:
1998 T = cast<TypeOfType>(T)->getUnderlyingType();
1999 break;
2000 case Type::Decltype:
2001 T = cast<DecltypeType>(T)->getUnderlyingType();
2002 break;
2003 case Type::UnaryTransform:
2004 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2005 break;
2006 case Type::Attributed:
2007 T = cast<AttributedType>(T)->getEquivalentType();
2008 break;
2009 case Type::Elaborated:
2010 T = cast<ElaboratedType>(T)->getNamedType();
2011 break;
2012 case Type::Paren:
2013 T = cast<ParenType>(T)->getInnerType();
2014 break;
David Blaikie05491062013-01-21 04:37:12 +00002015 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 break;
2018 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002019 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002020 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002021 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 break;
2023 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002024
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002026 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 } while (true);
2028}
2029
Eric Christopher0fdcb312013-05-16 00:52:20 +00002030/// getType - Get the type from the cache or return null type if it doesn't
2031/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002032llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2033
2034 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002035 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002036
David Blaikief427b002014-05-06 03:42:01 +00002037 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 if (it != TypeCache.end()) {
2039 // Verify that the debug info still exists.
2040 if (llvm::Value *V = it->second)
2041 return llvm::DIType(cast<llvm::MDNode>(V));
2042 }
2043
2044 return llvm::DIType();
2045}
2046
David Blaikie0e716b42014-03-03 23:48:23 +00002047void CGDebugInfo::completeTemplateDefinition(
2048 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002049 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2050 return;
2051
David Blaikie0e716b42014-03-03 23:48:23 +00002052 completeClassData(&SD);
2053 // In case this type has no member function definitions being emitted, ensure
2054 // it is retained
2055 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2056}
2057
Guy Benyei11169dd2012-12-18 14:30:41 +00002058/// getOrCreateType - Get the type from the cache or create a new
2059/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002060llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 if (Ty.isNull())
2062 return llvm::DIType();
2063
2064 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002065 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002066
David Blaikieef8a9512014-05-05 23:23:53 +00002067 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 return T;
2069
2070 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002071 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002072 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002073
2074 // And update the type cache.
2075 TypeCache[TyPtr] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002076
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 return Res;
2078}
2079
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002080/// Currently the checksum of an interface includes the number of
2081/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002082unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002083 // The assumption is that the number of ivars can only increase
2084 // monotonically, so it is safe to just use their current number as
2085 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002086 unsigned Sum = 0;
2087 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002088 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002089 ++Sum;
2090
2091 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002092}
2093
2094ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2095 switch (Ty->getTypeClass()) {
2096 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002097 return getObjCInterfaceDecl(
2098 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002099 case Type::ObjCInterface:
2100 return cast<ObjCInterfaceType>(Ty)->getDecl();
2101 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002102 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002103 }
2104}
2105
Guy Benyei11169dd2012-12-18 14:30:41 +00002106/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002107llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002108 // Handle qualifiers, which recursively handles what they refer to.
2109 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002110 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002111
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 // Work out details of type.
2113 switch (Ty->getTypeClass()) {
2114#define TYPE(Class, Base)
2115#define ABSTRACT_TYPE(Class, Base)
2116#define NON_CANONICAL_TYPE(Class, Base)
2117#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2118#include "clang/AST/TypeNodes.def"
2119 llvm_unreachable("Dependent types cannot show up in debug information");
2120
2121 case Type::ExtVector:
2122 case Type::Vector:
2123 return CreateType(cast<VectorType>(Ty), Unit);
2124 case Type::ObjCObjectPointer:
2125 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2126 case Type::ObjCObject:
2127 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2128 case Type::ObjCInterface:
2129 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2130 case Type::Builtin:
2131 return CreateType(cast<BuiltinType>(Ty));
2132 case Type::Complex:
2133 return CreateType(cast<ComplexType>(Ty));
2134 case Type::Pointer:
2135 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002136 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002137 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002138 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002139 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002140 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002141 case Type::BlockPointer:
2142 return CreateType(cast<BlockPointerType>(Ty), Unit);
2143 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002144 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002146 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002148 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 case Type::FunctionProto:
2150 case Type::FunctionNoProto:
2151 return CreateType(cast<FunctionType>(Ty), Unit);
2152 case Type::ConstantArray:
2153 case Type::VariableArray:
2154 case Type::IncompleteArray:
2155 return CreateType(cast<ArrayType>(Ty), Unit);
2156
2157 case Type::LValueReference:
2158 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2159 case Type::RValueReference:
2160 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2161
2162 case Type::MemberPointer:
2163 return CreateType(cast<MemberPointerType>(Ty), Unit);
2164
2165 case Type::Atomic:
2166 return CreateType(cast<AtomicType>(Ty), Unit);
2167
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002169 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2170
David Blaikie42edade2014-11-11 20:44:45 +00002171 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002172 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 case Type::Elaborated:
2174 case Type::Paren:
2175 case Type::SubstTemplateTypeParm:
2176 case Type::TypeOfExpr:
2177 case Type::TypeOf:
2178 case Type::Decltype:
2179 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002180 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002181 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002183
David Blaikie42edade2014-11-11 20:44:45 +00002184 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002185}
2186
2187/// getOrCreateLimitedType - Get the type from the cache or create a new
2188/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002189llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002190 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002191 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002192
David Blaikie8d5e1282013-08-20 21:03:29 +00002193 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002194
2195 // We may have cached a forward decl when we could have created
2196 // a non-forward decl. Go ahead and create a non-forward decl
2197 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002198 if (T && !T.isForwardDecl())
2199 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002200
2201 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002202 llvm::DICompositeType Res = CreateLimitedType(Ty);
2203
2204 // Propagate members from the declaration to the definition
2205 // CreateType(const RecordType*) will overwrite this with the members in the
2206 // correct order if the full type is needed.
Manman Ren51289892014-07-28 19:14:41 +00002207 Res.setArrays(T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 // And update the type cache.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002210 TypeCache[QTy.getAsOpaquePtr()] = Res;
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 return Res;
2212}
2213
2214// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002215llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002217
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 // Get overall information about the record type for the debug info.
2219 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2220 unsigned Line = getLineNumber(RD->getLocation());
2221 StringRef RDName = getClassName(RD);
2222
Eric Christopher07429ff2013-10-15 21:22:34 +00002223 llvm::DIDescriptor RDContext =
2224 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002225
David Blaikied2785892013-08-18 17:36:19 +00002226 // If we ended up creating the type during the context chain construction,
2227 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002228 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2229 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002230 return T;
David Blaikied2785892013-08-18 17:36:19 +00002231
Adrian Prantl381e7552014-02-04 21:29:50 +00002232 // If this is just a forward or incomplete declaration, construct an
2233 // appropriately marked node and just return it.
2234 const RecordDecl *D = RD->getDefinition();
2235 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002236 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002237
2238 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2239 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002240 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002241
Manman Rene0064d82013-08-29 23:19:58 +00002242 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2243
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002245 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2246 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 else if (RD->isClass()) {
2248 // FIXME: This could be a struct type giving a default visibility different
2249 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002250 RealDecl = DBuilder.createClassType(
2251 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2252 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002253 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002254 RealDecl = DBuilder.createStructType(
2255 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2256 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002257
2258 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
David Blaikie49ae6a72013-03-26 23:47:35 +00002259 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
David Blaikieadfbf992013-08-18 16:55:33 +00002261 if (const ClassTemplateSpecializationDecl *TSpecial =
2262 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Manman Ren51289892014-07-28 19:14:41 +00002263 RealDecl.setArrays(llvm::DIArray(),
Eric Christophere7b87e52014-10-26 23:40:33 +00002264 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002265 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002266}
2267
David Blaikieadfbf992013-08-18 16:55:33 +00002268void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2269 llvm::DICompositeType RealDecl) {
2270 // A class's primary base or the class itself contains the vtable.
2271 llvm::DICompositeType ContainingType;
2272 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2273 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002274 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002275 while (1) {
2276 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2277 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2278 if (PBT && !BRL.isPrimaryBaseVirtual())
2279 PBase = PBT;
2280 else
2281 break;
2282 }
2283 ContainingType = llvm::DICompositeType(
2284 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2285 getOrCreateFile(RD->getLocation())));
2286 } else if (RD->isDynamicClass())
2287 ContainingType = RealDecl;
2288
2289 RealDecl.setContainingType(ContainingType);
2290}
2291
Guy Benyei11169dd2012-12-18 14:30:41 +00002292/// CreateMemberType - Create new member and increase Offset by FType's size.
2293llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002294 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2296 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2297 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002298 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2299 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002300 *Offset += FieldSize;
2301 return Ty;
2302}
2303
Frederic Riss442293e2014-11-06 21:12:06 +00002304llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002305 // We only need a declaration (not a definition) of the type - so use whatever
2306 // we would otherwise do to get a type for a pointee. (forward declarations in
2307 // limited debug info, full definitions (if the type definition is available)
2308 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002309 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2310 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002311 getOrCreateFile(TD->getLocation()));
David Blaikiebd483762013-05-20 04:58:53 +00002312 // Otherwise fall back to a fairly rudimentary cache of existing declarations.
2313 // This doesn't handle providing declarations (for functions or variables) for
2314 // entities without definitions in this TU, nor when the definition proceeds
2315 // the call to this function.
2316 // FIXME: This should be split out into more specific maps with support for
2317 // emitting forward declarations and merging definitions with declarations,
2318 // the same way as we do for types.
2319 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator I =
2320 DeclCache.find(D->getCanonicalDecl());
2321 if (I == DeclCache.end())
Adrian Prantl6cdce9e2014-04-01 03:41:01 +00002322 return llvm::DIScope();
David Blaikiebd483762013-05-20 04:58:53 +00002323 llvm::Value *V = I->second;
Frederic Riss442293e2014-11-06 21:12:06 +00002324 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikiebd483762013-05-20 04:58:53 +00002325}
2326
Guy Benyei11169dd2012-12-18 14:30:41 +00002327/// getFunctionDeclaration - Return debug info descriptor to describe method
2328/// declaration for the given method definition.
2329llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002330 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002331 return llvm::DISubprogram();
2332
Guy Benyei11169dd2012-12-18 14:30:41 +00002333 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002334 if (!FD)
2335 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002336
2337 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002338 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002339
Eric Christophere7b87e52014-10-26 23:40:33 +00002340 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI =
2341 SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002342 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002343 if (const CXXMethodDecl *MD =
2344 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002345 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002346 llvm::DISubprogram SP =
2347 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002348 return SP;
2349 }
2350 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 if (MI != SPCache.end()) {
2352 llvm::Value *V = MI->second;
2353 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002354 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002355 return SP;
2356 }
2357
Aaron Ballman86c93902014-03-06 23:45:36 +00002358 for (auto NextFD : FD->redecls()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002359 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator MI =
2360 SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 if (MI != SPCache.end()) {
2362 llvm::Value *V = MI->second;
2363 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(V));
David Blaikie18cfbc52013-06-22 00:09:36 +00002364 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002365 return SP;
2366 }
2367 }
2368 return llvm::DISubprogram();
2369}
2370
2371// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2372// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002373llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2374 QualType FnType,
2375 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002376 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002377 // Create fake but valid subroutine type. Otherwise
2378 // llvm::DISubprogram::Verify() would return false, and
2379 // subprogram DIE will miss DW_AT_decl_file and
2380 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002381 return DBuilder.createSubroutineType(F,
2382 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002383
2384 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2385 return getOrCreateMethodType(Method, F);
2386 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2387 // Add "self" and "_cmd"
2388 SmallVector<llvm::Value *, 16> Elts;
2389
2390 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002391 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002392
2393 // Replace the instancetype keyword with the actual type.
2394 if (ResultTy == CGM.getContext().getObjCInstanceType())
2395 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002396 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002397
Adrian Prantl7bec9032013-05-10 21:08:31 +00002398 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002399 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002400 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2401 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2402 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002403 // "_cmd" pointer is always second argument.
2404 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2405 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2406 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002407 for (const auto *PI : OMethod->params())
2408 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002409 // Variadic methods need a special marker at the end of the type list.
2410 if (OMethod->isVariadic())
2411 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002412
Manman Ren67f005e2014-07-28 22:24:34 +00002413 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002414 return DBuilder.createSubroutineType(F, EltTypeArray);
2415 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002416
Adrian Prantl800faef2014-02-25 23:42:18 +00002417 // Handle variadic function types; they need an additional
2418 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002419 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2420 if (FD->isVariadic()) {
2421 SmallVector<llvm::Value *, 16> EltTys;
2422 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2423 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2424 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2425 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2426 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002427 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002428 return DBuilder.createSubroutineType(F, EltTypeArray);
2429 }
2430
David Blaikie469f0792013-05-22 23:22:42 +00002431 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002432}
2433
2434/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002435void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2436 SourceLocation ScopeLoc, QualType FnType,
2437 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002438
2439 StringRef Name;
2440 StringRef LinkageName;
2441
2442 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2443
2444 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002445 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002446
Guy Benyei11169dd2012-12-18 14:30:41 +00002447 unsigned Flags = 0;
2448 llvm::DIFile Unit = getOrCreateFile(Loc);
2449 llvm::DIDescriptor FDContext(Unit);
2450 llvm::DIArray TParamsArray;
2451 if (!HasDecl) {
2452 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002453 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2455 // If there is a DISubprogram for this function available then use it.
Eric Christophere7b87e52014-10-26 23:40:33 +00002456 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator FI =
2457 SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 if (FI != SPCache.end()) {
2459 llvm::Value *V = FI->second;
2460 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(V));
2461 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2462 llvm::MDNode *SPN = SP;
2463 LexicalBlockStack.push_back(SPN);
2464 RegionMap[D] = llvm::WeakVH(SP);
2465 return;
2466 }
2467 }
2468 Name = getFunctionName(FD);
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002469 // Use mangled name as linkage name for C/C++ functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 if (FD->hasPrototype()) {
2471 LinkageName = CGM.getMangledName(GD);
2472 Flags |= llvm::DIDescriptor::FlagPrototyped;
2473 }
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002474 // No need to replicate the linkage name if it isn't different from the
2475 // subprogram name, no need to have it at all unless coverage is enabled or
2476 // debug is set to more than just line tables.
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 if (LinkageName == Name ||
Nick Lewyckyc02bbb62013-03-20 01:38:16 +00002478 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2479 !CGM.getCodeGenOpts().EmitGcovNotes &&
Eric Christopher75e17682013-05-16 00:45:23 +00002480 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 LinkageName = StringRef();
2482
Eric Christopher75e17682013-05-16 00:45:23 +00002483 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
Eric Christopher4cbd0d9d2014-03-27 05:29:34 +00002484 if (const NamespaceDecl *NSDecl =
2485 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2486 FDContext = getOrCreateNameSpace(NSDecl);
2487 else if (const RecordDecl *RDecl =
2488 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2489 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2490
2491 // Collect template parameters.
Guy Benyei11169dd2012-12-18 14:30:41 +00002492 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2493 }
2494 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2495 Name = getObjCMethodName(OMD);
2496 Flags |= llvm::DIDescriptor::FlagPrototyped;
2497 } else {
2498 // Use llvm function name.
2499 Name = Fn->getName();
2500 Flags |= llvm::DIDescriptor::FlagPrototyped;
2501 }
2502 if (!Name.empty() && Name[0] == '\01')
2503 Name = Name.substr(1);
2504
Adrian Prantl42d71b92014-04-10 23:21:53 +00002505 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002507 // Artificial functions without a location should not silently reuse CurLoc.
2508 if (Loc.isInvalid())
2509 CurLoc = SourceLocation();
2510 }
2511 unsigned LineNo = getLineNumber(Loc);
2512 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002513
Eric Christopher8018e412014-03-27 18:50:35 +00002514 // FIXME: The function declaration we're constructing here is mostly reusing
2515 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2516 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2517 // all subprograms instead of the actual context since subprogram definitions
2518 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002519 llvm::DISubprogram SP = DBuilder.createFunction(
2520 FDContext, Name, LinkageName, Unit, LineNo,
2521 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2522 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2523 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002524 // We might get here with a VarDecl in the case we're generating
2525 // code for the initialization of globals. Do not record these decls
2526 // as they will overwrite the actual VarDecl Decl in the cache.
2527 if (HasDecl && isa<FunctionDecl>(D))
David Blaikiebd483762013-05-20 04:58:53 +00002528 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(SP)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002529
Adrian Prantlbebb8932014-03-21 21:01:58 +00002530 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 llvm::MDNode *SPN = SP;
2532 LexicalBlockStack.push_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002533
Guy Benyei11169dd2012-12-18 14:30:41 +00002534 if (HasDecl)
2535 RegionMap[D] = llvm::WeakVH(SP);
2536}
2537
2538/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002539/// information in the source file. If the location is invalid, the
2540/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002541void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002542 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 // Update our current location
2544 setLocation(Loc);
2545
Eric Christophere7b87e52014-10-26 23:40:33 +00002546 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2547 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002548
2549 // Don't bother if things are the same as last time.
2550 SourceManager &SM = CGM.getContext().getSourceManager();
2551 if (CurLoc == PrevLoc ||
2552 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2553 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002554 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2555 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002556 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002557 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002558
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 // Update last state.
2560 PrevLoc = CurLoc;
2561
Adrian Prantle83b1302014-01-07 22:05:52 +00002562 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002563 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2564 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002565}
2566
2567/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2568/// the stack.
2569void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
David Blaikief9ea2422014-06-02 16:32:05 +00002570 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
2571 llvm::DIDescriptor(LexicalBlockStack.empty() ? nullptr
2572 : LexicalBlockStack.back()),
David Blaikieb8449842014-08-21 22:46:45 +00002573 getOrCreateFile(CurLoc), getLineNumber(CurLoc), getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 llvm::MDNode *DN = D;
2575 LexicalBlockStack.push_back(DN);
2576}
2577
2578/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2579/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002580void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2581 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002582 // Set our current location.
2583 setLocation(Loc);
2584
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002586 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2587 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002588
2589 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2590 return;
2591
2592 // Create a new lexical block and push it on the stack.
2593 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002594}
2595
2596/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2597/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002598void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2599 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2601
2602 // Provide an entry in the line table for the end of the block.
2603 EmitLocation(Builder, Loc);
2604
David Blaikie60a877b2014-10-22 19:34:33 +00002605 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2606 return;
2607
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 LexicalBlockStack.pop_back();
2609}
2610
2611/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2612void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2613 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2614 unsigned RCount = FnBeginRegionCount.back();
2615 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2616
2617 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002618 while (LexicalBlockStack.size() != RCount) {
2619 // Provide an entry in the line table for the end of the block.
2620 EmitLocation(Builder, CurLoc);
2621 LexicalBlockStack.pop_back();
2622 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 FnBeginRegionCount.pop_back();
2624}
2625
Eric Christopherb2a008c2013-05-16 00:45:12 +00002626// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002627// See BuildByRefType.
2628llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2629 uint64_t *XOffset) {
2630
2631 SmallVector<llvm::Value *, 5> EltTys;
2632 QualType FType;
2633 uint64_t FieldSize, FieldOffset;
2634 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002635
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002637 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002638
2639 FieldOffset = 0;
2640 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2641 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2642 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2643 FType = CGM.getContext().IntTy;
2644 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2645 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2646
2647 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2648 if (HasCopyAndDispose) {
2649 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002650 EltTys.push_back(
2651 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2652 EltTys.push_back(
2653 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002654 }
2655 bool HasByrefExtendedLayout;
2656 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002657 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2658 HasByrefExtendedLayout) &&
2659 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002660 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002661 EltTys.push_back(
2662 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002663 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002664
Guy Benyei11169dd2012-12-18 14:30:41 +00002665 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2666 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002667 CGM.getTarget().getPointerAlign(0))) {
2668 CharUnits FieldOffsetInBytes =
2669 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2670 CharUnits AlignedOffsetInBytes =
2671 FieldOffsetInBytes.RoundUpToAlignment(Align);
2672 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002673
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 if (NumPaddingBytes.isPositive()) {
2675 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2676 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2677 pad, ArrayType::Normal, 0);
2678 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2679 }
2680 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002681
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002683 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002684 FieldSize = CGM.getContext().getTypeSize(FType);
2685 FieldAlign = CGM.getContext().toBits(Align);
2686
Eric Christopherb2a008c2013-05-16 00:45:12 +00002687 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002688 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2689 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 EltTys.push_back(FieldTy);
2691 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002692
Guy Benyei11169dd2012-12-18 14:30:41 +00002693 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002694
Guy Benyei11169dd2012-12-18 14:30:41 +00002695 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002696
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002698 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002699}
2700
2701/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002702void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002703 llvm::Value *Storage, unsigned ArgNo,
2704 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002705 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2707
David Blaikie7fceebf2013-08-19 03:37:48 +00002708 bool Unwritten =
2709 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2710 cast<Decl>(VD->getDeclContext())->isImplicit());
2711 llvm::DIFile Unit;
2712 if (!Unwritten)
2713 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 llvm::DIType Ty;
2715 uint64_t XOffset = 0;
2716 if (VD->hasAttr<BlocksAttr>())
2717 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002718 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002719 Ty = getOrCreateType(VD->getType(), Unit);
2720
2721 // If there is no debug info for this type then do not emit debug info
2722 // for this variable.
2723 if (!Ty)
2724 return;
2725
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002727 unsigned Line = 0;
2728 unsigned Column = 0;
2729 if (!Unwritten) {
2730 Line = getLineNumber(VD->getLocation());
2731 Column = getColumnNumber(VD->getLocation());
2732 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002733 unsigned Flags = 0;
2734 if (VD->isImplicit())
2735 Flags |= llvm::DIDescriptor::FlagArtificial;
2736 // If this is the first argument and it is implicit then
2737 // give it an object pointer flag.
2738 // FIXME: There has to be a better way to do this, but for static
2739 // functions there won't be an implicit param at arg1 and
2740 // otherwise it is 'self' or 'this'.
2741 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2742 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002743 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002744 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2745 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002746 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002747
2748 llvm::MDNode *Scope = LexicalBlockStack.back();
2749
2750 StringRef Name = VD->getName();
2751 if (!Name.empty()) {
2752 if (VD->hasAttr<BlocksAttr>()) {
2753 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002754 SmallVector<int64_t, 9> addr;
2755 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 // offset of __forwarding field
2757 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002758 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002759 addr.push_back(offset.getQuantity());
2760 addr.push_back(llvm::dwarf::DW_OP_deref);
2761 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 // offset of x field
2763 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002764 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002765
2766 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002767 llvm::DIVariable D = DBuilder.createLocalVariable(
2768 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002769
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 // Insert an llvm.dbg.declare into the current block.
2771 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002772 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2773 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2775 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002776 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002777 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002778 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2779 // If VD is an anonymous union then Storage represents value for
2780 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002782 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002783 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2785 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002786
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 // Ignore unnamed fields. Do not ignore unnamed records.
2788 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2789 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002790
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002792 llvm::DIVariable D = DBuilder.createLocalVariable(
2793 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2794 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002795
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002797 llvm::Instruction *Call = DBuilder.insertDeclare(
2798 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002799 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2800 }
David Blaikie219c7d92013-01-05 20:03:07 +00002801 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002802 }
2803 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002804
2805 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002806 llvm::DIVariable D = DBuilder.createLocalVariable(
2807 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2808 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002809
2810 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002811 llvm::Instruction *Call = DBuilder.insertDeclare(
2812 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002813 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002814}
2815
2816void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2817 llvm::Value *Storage,
2818 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002819 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2821}
2822
Adrian Prantlde17db32013-03-29 19:20:29 +00002823/// Look up the completed type for a self pointer in the TypeCache and
2824/// create a copy of it with the ObjectPointer and Artificial flags
2825/// set. If the type is not cached, a new one is created. This should
2826/// never happen though, since creating a type for the implicit self
2827/// argument implies that we already parsed the interface definition
2828/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002829llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2830 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002831 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002832 if (CachedTy)
2833 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002834 return DBuilder.createObjectPointerType(Ty);
2835}
2836
Eric Christophere7b87e52014-10-26 23:40:33 +00002837void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2838 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2839 const CGBlockInfo &blockInfo) {
Eric Christopher75e17682013-05-16 00:45:23 +00002840 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002841 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002842
Craig Topper8a13c412014-05-21 05:09:00 +00002843 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002845
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002847
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 uint64_t XOffset = 0;
2849 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2850 llvm::DIType Ty;
2851 if (isByRef)
2852 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002853 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 Ty = getOrCreateType(VD->getType(), Unit);
2855
2856 // Self is passed along as an implicit non-arg variable in a
2857 // block. Mark it as the object pointer.
2858 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002859 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002860
2861 // Get location information.
2862 unsigned Line = getLineNumber(VD->getLocation());
2863 unsigned Column = getColumnNumber(VD->getLocation());
2864
2865 const llvm::DataLayout &target = CGM.getDataLayout();
2866
2867 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002868 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2870
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002871 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002872 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002873 addr.push_back(llvm::dwarf::DW_OP_deref);
2874 addr.push_back(llvm::dwarf::DW_OP_plus);
2875 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002877 addr.push_back(llvm::dwarf::DW_OP_deref);
2878 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002880 offset =
2881 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002882 addr.push_back(offset.getQuantity());
2883 addr.push_back(llvm::dwarf::DW_OP_deref);
2884 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 // offset of x field
2886 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002887 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 }
2889
2890 // Create the descriptor for the variable.
2891 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002892 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2893 llvm::DIDescriptor(LexicalBlockStack.back()),
2894 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002895
Guy Benyei11169dd2012-12-18 14:30:41 +00002896 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002897 llvm::Instruction *Call = DBuilder.insertDeclare(
2898 Storage, D, DBuilder.createExpression(addr), Builder.GetInsertPoint());
2899 Call->setDebugLoc(
2900 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002901}
2902
2903/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2904/// variable declaration.
2905void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2906 unsigned ArgNo,
2907 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002908 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002909 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2910}
2911
2912namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002913struct BlockLayoutChunk {
2914 uint64_t OffsetInBits;
2915 const BlockDecl::Capture *Capture;
2916};
2917bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2918 return l.OffsetInBits < r.OffsetInBits;
2919}
Guy Benyei11169dd2012-12-18 14:30:41 +00002920}
2921
2922void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002923 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002924 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002925 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002927 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 ASTContext &C = CGM.getContext();
2929 const BlockDecl *blockDecl = block.getBlockDecl();
2930
2931 // Collect some general information about the block's location.
2932 SourceLocation loc = blockDecl->getCaretLocation();
2933 llvm::DIFile tunit = getOrCreateFile(loc);
2934 unsigned line = getLineNumber(loc);
2935 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002936
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 // Build the debug-info type for the block literal.
2938 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2939
2940 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002941 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00002942
Eric Christophere7b87e52014-10-26 23:40:33 +00002943 SmallVector<llvm::Value *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00002944 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2945 blockLayout->getElementOffsetInBits(0),
2946 tunit, tunit));
2947 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2948 blockLayout->getElementOffsetInBits(1),
2949 tunit, tunit));
2950 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2951 blockLayout->getElementOffsetInBits(2),
2952 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00002953 auto *FnTy = block.getBlockExpr()->getFunctionType();
2954 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
2955 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 blockLayout->getElementOffsetInBits(3),
2957 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00002958 fields.push_back(createFieldType(
2959 "__descriptor", C.getPointerType(block.NeedsCopyDispose
2960 ? C.getBlockDescriptorExtendedType()
2961 : C.getBlockDescriptorType()),
2962 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002963
2964 // We want to sort the captures by offset, not because DWARF
2965 // requires this, but because we're paranoid about debuggers.
2966 SmallVector<BlockLayoutChunk, 8> chunks;
2967
2968 // 'this' capture.
2969 if (blockDecl->capturesCXXThis()) {
2970 BlockLayoutChunk chunk;
2971 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00002972 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00002973 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 chunks.push_back(chunk);
2975 }
2976
2977 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00002978 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 const VarDecl *variable = capture.getVariable();
2980 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2981
2982 // Ignore constant captures.
2983 if (captureInfo.isConstant())
2984 continue;
2985
2986 BlockLayoutChunk chunk;
2987 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00002988 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 chunk.Capture = &capture;
2990 chunks.push_back(chunk);
2991 }
2992
2993 // Sort by offset.
2994 llvm::array_pod_sort(chunks.begin(), chunks.end());
2995
Eric Christophere7b87e52014-10-26 23:40:33 +00002996 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
2997 e = chunks.end();
2998 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 uint64_t offsetInBits = i->OffsetInBits;
3000 const BlockDecl::Capture *capture = i->Capture;
3001
3002 // If we have a null capture, this must be the C++ 'this' capture.
3003 if (!capture) {
3004 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003005 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 QualType type = method->getThisType(C);
3007
3008 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3009 offsetInBits, tunit, tunit));
3010 continue;
3011 }
3012
3013 const VarDecl *variable = capture->getVariable();
3014 StringRef name = variable->getName();
3015
3016 llvm::DIType fieldType;
3017 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003018 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003019
3020 // FIXME: this creates a second copy of this type!
3021 uint64_t xoffset;
3022 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003023 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3024 fieldType =
3025 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3026 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003028 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3029 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 }
3031 fields.push_back(fieldType);
3032 }
3033
3034 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003035 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3036 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003037
3038 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3039
3040 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003041 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3042 CGM.getContext().toBits(block.BlockSize),
3043 CGM.getContext().toBits(block.BlockAlign), 0,
3044 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3046
3047 // Get overall information about the block.
3048 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3049 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003050
3051 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003052 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3053 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3054 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3055 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003056
Adrian Prantl616bef42013-03-14 21:52:59 +00003057 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003058 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003059 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3060 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3061 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003062 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3063 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003064
Adrian Prantl616bef42013-03-14 21:52:59 +00003065 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003066 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3067 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003068 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003069}
3070
David Blaikie6943dea2013-08-20 01:28:15 +00003071/// If D is an out-of-class definition of a static data member of a class, find
3072/// its corresponding in-class declaration.
3073llvm::DIDerivedType
3074CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3075 if (!D->isStaticDataMember())
3076 return llvm::DIDerivedType();
3077 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator MI =
3078 StaticDataMemberCache.find(D->getCanonicalDecl());
3079 if (MI != StaticDataMemberCache.end()) {
3080 assert(MI->second && "Static data member declaration should still exist");
3081 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003082 }
David Blaikiece763042013-08-20 21:49:21 +00003083
3084 // If the member wasn't found in the cache, lazily construct and add it to the
3085 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003086 auto DC = D->getDeclContext();
3087 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3088 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003089}
3090
Eric Christophercab9fae2014-04-10 05:20:00 +00003091/// Recursively collect all of the member fields of a global anonymous decl and
3092/// create static variables for them. The first time this is called it needs
3093/// to be on a union and then from there we can have additional unnamed fields.
3094llvm::DIGlobalVariable
3095CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3096 unsigned LineNo, StringRef LinkageName,
3097 llvm::GlobalVariable *Var,
3098 llvm::DIDescriptor DContext) {
3099 llvm::DIGlobalVariable GV;
3100
3101 for (const auto *Field : RD->fields()) {
3102 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3103 StringRef FieldName = Field->getName();
3104
3105 // Ignore unnamed fields, but recurse into anonymous records.
3106 if (FieldName.empty()) {
3107 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3108 if (RT)
3109 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3110 Var, DContext);
3111 continue;
3112 }
3113 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003114 GV = DBuilder.createGlobalVariable(
3115 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3116 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003117 }
3118 return GV;
3119}
3120
Guy Benyei11169dd2012-12-18 14:30:41 +00003121/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003122void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003123 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003124 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 // Create global variable debug descriptor.
3126 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
3127 unsigned LineNo = getLineNumber(D->getLocation());
3128
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003129 setLocation(D->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00003130
3131 QualType T = D->getType();
3132 if (T->isIncompleteArrayType()) {
3133
3134 // CodeGen turns int[] into int[1] so we'll do the same here.
3135 llvm::APInt ConstVal(32, 1);
3136 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3137
Eric Christophere7b87e52014-10-26 23:40:33 +00003138 T = CGM.getContext().getConstantArrayType(ET, ConstVal, ArrayType::Normal,
3139 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00003140 }
Eric Christophercab9fae2014-04-10 05:20:00 +00003141
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003142 StringRef DeclName = D->getName();
3143 StringRef LinkageName;
Eric Christophercab9fae2014-04-10 05:20:00 +00003144 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()) &&
3145 !isa<ObjCMethodDecl>(D->getDeclContext()))
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003146 LinkageName = Var->getName();
3147 if (LinkageName == DeclName)
3148 LinkageName = StringRef();
Eric Christophercab9fae2014-04-10 05:20:00 +00003149
David Blaikie3813b262014-10-23 16:39:49 +00003150 // Since we emit declarations (DW_AT_members) for static members, place the
3151 // definition of those static members in the namespace they were declared in
3152 // in the source code (the lexical decl context).
3153 // FIXME: Generalize this for even non-member global variables where the
3154 // declaration and definition may have different lexical decl contexts, once
3155 // we have support for emitting declarations of (non-member) global variables.
3156 llvm::DIDescriptor DContext = getContextDescriptor(
3157 dyn_cast<Decl>(D->isStaticDataMember() ? D->getLexicalDeclContext()
3158 : D->getDeclContext()));
Eric Christophercab9fae2014-04-10 05:20:00 +00003159
3160 // Attempt to store one global variable for the declaration - even if we
3161 // emit a lot of fields.
3162 llvm::DIGlobalVariable GV;
3163
3164 // If this is an anonymous union then we'll want to emit a global
3165 // variable for each member of the anonymous union so that it's possible
3166 // to find the name of any field in the union.
3167 if (T->isUnionType() && DeclName.empty()) {
3168 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003169 assert(RD->isAnonymousStructOrUnion() &&
3170 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003171 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3172 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003173 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003174 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3175 Var->hasInternalLinkage(), Var,
3176 getOrCreateStaticDataMemberDeclarationOrNull(D));
3177 }
David Blaikiebd483762013-05-20 04:58:53 +00003178 DeclCache.insert(std::make_pair(D->getCanonicalDecl(), llvm::WeakVH(GV)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003179}
3180
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003181/// EmitGlobalVariable - Emit global variable's debug info.
3182void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3183 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003184 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003185 // Create the descriptor for the variable.
3186 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3187 StringRef Name = VD->getName();
3188 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3189 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3190 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3191 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3192 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3193 }
3194 // Do not use DIGlobalVariable for enums.
3195 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3196 return;
David Blaikiea15565562014-04-04 20:56:17 +00003197 // Do not emit separate definitions for function local const/statics.
3198 if (isa<FunctionDecl>(VD->getDeclContext()))
3199 return;
David Blaikiebb113912014-04-05 07:23:17 +00003200 VD = cast<ValueDecl>(VD->getCanonicalDecl());
3201 auto pair = DeclCache.insert(std::make_pair(VD, llvm::WeakVH()));
3202 if (!pair.second)
3203 return;
David Blaikie506a7452014-04-05 07:46:57 +00003204 llvm::DIDescriptor DContext =
3205 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
Jyoti Allurb76b57f2014-09-29 06:32:54 +00003206 llvm::DIGlobalVariable GV = DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003207 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
3208 true, Init,
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003209 getOrCreateStaticDataMemberDeclarationOrNull(cast<VarDecl>(VD)));
David Blaikiebb113912014-04-05 07:23:17 +00003210 pair.first->second = llvm::WeakVH(GV);
David Blaikiebd483762013-05-20 04:58:53 +00003211}
3212
3213llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3214 if (!LexicalBlockStack.empty())
3215 return llvm::DIScope(LexicalBlockStack.back());
3216 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003217}
3218
David Blaikie9f88fe82013-04-22 06:13:21 +00003219void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003220 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3221 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003222 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003223 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3224 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003225 getLineNumber(UD.getLocation()));
3226}
3227
David Blaikiebd483762013-05-20 04:58:53 +00003228void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3229 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3230 return;
3231 assert(UD.shadow_size() &&
3232 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3233 // Emitting one decl is sufficient - debuggers can detect that this is an
3234 // overloaded name & provide lookup for all the overloads.
3235 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003236 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003237 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003238 DBuilder.createImportedDeclaration(
3239 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3240 getLineNumber(USD.getLocation()));
3241}
3242
David Blaikief121b932013-05-20 22:50:41 +00003243llvm::DIImportedEntity
3244CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3245 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003246 return llvm::DIImportedEntity(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003247 llvm::WeakVH &VH = NamespaceAliasCache[&NA];
3248 if (VH)
3249 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003250 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003251 if (const NamespaceAliasDecl *Underlying =
3252 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3253 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003254 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003255 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3256 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3257 NA.getName());
3258 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003259 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003260 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3261 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3262 getLineNumber(NA.getLocation()), NA.getName());
3263 VH = R;
3264 return R;
3265}
3266
Guy Benyei11169dd2012-12-18 14:30:41 +00003267/// getOrCreateNamesSpace - Return namespace descriptor for the given
3268/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003269llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003270CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003271 NSDecl = NSDecl->getCanonicalDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003272 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
Guy Benyei11169dd2012-12-18 14:30:41 +00003273 NameSpaceCache.find(NSDecl);
3274 if (I != NameSpaceCache.end())
3275 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003276
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3278 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003279 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3281 llvm::DINameSpace NS =
3282 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
3283 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
3284 return NS;
3285}
3286
3287void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003288 // Creating types might create further types - invalidating the current
3289 // element and the size(), so don't cache/reference them.
3290 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3291 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3292 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3293 E.Type->getDecl()->getDefinition()
3294 ? CreateTypeDefinition(E.Type, E.Unit)
3295 : E.Decl);
3296 }
3297
David Blaikief427b002014-05-06 03:42:01 +00003298 for (auto p : ReplaceMap) {
3299 assert(p.second);
3300 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003301 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003302
David Blaikief427b002014-05-06 03:42:01 +00003303 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003304 assert(it != TypeCache.end());
3305 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003306
David Blaikief427b002014-05-06 03:42:01 +00003307 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3308 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003310
3311 // We keep our own list of retained types, because we need to look
3312 // up the final type in the type cache.
3313 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3314 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003315 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003316
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 DBuilder.finalize();
3318}
David Blaikie66088d52014-09-24 17:01:27 +00003319
3320void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3321 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3322 return;
3323 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3324 // Don't ignore in case of explicit cast where it is referenced indirectly.
3325 DBuilder.retainType(DieTy);
3326}