blob: 8b18b296f518444db44b7035bc325a374d2326b2 [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
David Blaikie06b2c542014-12-17 18:02:04 +000055SaveAndRestoreLocation::SaveAndRestoreLocation(CodeGenFunction &CGF,
56 CGBuilderTy &B)
57 : DI(CGF.getDebugInfo()), Builder(B) {
58 if (DI) {
59 SavedLoc = DI->getLocation();
60 DI->CurLoc = SourceLocation();
61 }
62}
63
64SaveAndRestoreLocation::~SaveAndRestoreLocation() {
65 if (DI)
66 DI->EmitLocation(Builder, SavedLoc);
67}
68
69NoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B)
70 : SaveAndRestoreLocation(CGF, B) {
71 if (DI)
72 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
73}
74
75NoLocation::~NoLocation() {
76 if (DI)
77 assert(Builder.getCurrentDebugLocation().isUnknown());
78}
79
80ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
81 : SaveAndRestoreLocation(CGF, B) {
82 if (DI)
83 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
84}
85
86void ArtificialLocation::Emit() {
87 if (DI) {
88 // Sync the Builder.
89 DI->EmitLocation(Builder, SavedLoc);
90 DI->CurLoc = SourceLocation();
Adrian Prantl2e0637f2013-07-18 00:28:02 +000091 // 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());
David Blaikie06b2c542014-12-17 18:02:04 +000094 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000095 }
96}
97
David Blaikie06b2c542014-12-17 18:02:04 +000098ArtificialLocation::~ArtificialLocation() {
99 if (DI)
100 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();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000129 LexicalBlockStack.emplace_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();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000135 LexicalBlockStack.emplace_back(N);
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 }
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
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000144 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000145 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000146 llvm::Metadata *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000147 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000148 }
149
150 // Check namespace.
151 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000152 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153
David Blaikiebfa52742013-04-19 06:56:38 +0000154 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
155 if (!RDecl->isDependentType())
156 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000157 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 return TheCU;
159}
160
161/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000162/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000163/// is stored on the side.
164StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000165 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000167 FunctionTemplateSpecializationInfo *Info =
168 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000169 if (!Info && FII)
170 return FII->getName();
171
172 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000173 SmallString<128> NS;
174 llvm::raw_svector_ostream OS(NS);
175 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000176
177 // Add any template specialization args.
178 if (Info) {
179 const TemplateArgumentList *TArgs = Info->TemplateArguments;
180 const TemplateArgument *Args = TArgs->data();
181 unsigned NumArgs = TArgs->size();
182 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000183 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
184 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000185 }
186
187 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000188 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000189}
190
191StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
192 SmallString<256> MethodName;
193 llvm::raw_svector_ostream OS(MethodName);
194 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
195 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000196 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000197 dyn_cast<const ObjCImplementationDecl>(DC)) {
198 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000199 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000200 dyn_cast<const ObjCInterfaceDecl>(DC)) {
201 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000202 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000203 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
204 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
205 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000206 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000207 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000208 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000209 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000210 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000211 ClassTy.print(OS, PrintingPolicy(LangOptions()));
212 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 }
214 OS << ' ' << OMD->getSelector().getAsString() << ']';
215
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000216 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000217}
218
219/// getSelectorName - Return selector name. This is used for debugging
220/// info.
221StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000222 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000223}
224
225/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000226StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000227 // quick optimization to avoid having to intern strings that are already
228 // stored reliably elsewhere
229 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000230 return RD->getName();
231
David Blaikie65813a32014-04-02 18:21:09 +0000232 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000233 {
David Blaikie65813a32014-04-02 18:21:09 +0000234 llvm::raw_svector_ostream OS(Name);
235 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
236 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000237 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000238
239 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000240 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000241}
242
243/// getOrCreateFile - Get the file debug info descriptor for the input location.
244llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
245 if (!Loc.isValid())
246 // If Location is not valid then use main input file.
247 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
248
249 SourceManager &SM = CGM.getContext().getSourceManager();
250 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
251
252 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
253 // If the location is not valid then use main input file.
254 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
255
256 // Cache the results.
257 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000258 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
260 if (it != DIFileCache.end()) {
261 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000262 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +0000263 return llvm::DIFile(cast<llvm::MDNode>(V));
264 }
265
266 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
267
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000268 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000269 return F;
270}
271
272/// getOrCreateMainFile - Get the file info for main compile unit.
273llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
274 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
275}
276
277/// getLineNumber - Get line number for the location. If location is invalid
278/// then use current location.
279unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
280 if (Loc.isInvalid() && CurLoc.isInvalid())
281 return 0;
282 SourceManager &SM = CGM.getContext().getSourceManager();
283 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000284 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000285}
286
287/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000288unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000289 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000290 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000291 return 0;
292
293 // If the location is invalid then use the current column.
294 if (Loc.isInvalid() && CurLoc.isInvalid())
295 return 0;
296 SourceManager &SM = CGM.getContext().getSourceManager();
297 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000298 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000299}
300
301StringRef CGDebugInfo::getCurrentDirname() {
302 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
303 return CGM.getCodeGenOpts().DebugCompilationDir;
304
305 if (!CWDName.empty())
306 return CWDName;
307 SmallString<256> CWD;
308 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000309 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000310}
311
312/// CreateCompileUnit - Create new compile unit.
313void CGDebugInfo::CreateCompileUnit() {
314
David Blaikieaabde052014-05-14 00:29:00 +0000315 // Should we be asking the SourceManager for the main file name, instead of
316 // accepting it as an argument? This just causes the main file name to
317 // mismatch with source locations and create extra lexical scopes or
318 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
319 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
320 // because that's what the SourceManager says)
321
Guy Benyei11169dd2012-12-18 14:30:41 +0000322 // Get absolute path name.
323 SourceManager &SM = CGM.getContext().getSourceManager();
324 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
325 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000326 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000327
328 // The main file name provided via the "-main-file-name" option contains just
329 // the file name itself with no path information. This file name may have had
330 // a relative path, so we look into the actual file entry for the main
331 // file to determine the real absolute path for the file.
332 std::string MainFileDir;
333 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
334 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000335 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000336 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
337 llvm::sys::path::append(MainFileDirSS, MainFileName);
338 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000339 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000340 }
341
342 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000343 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000344
345 // Save split dwarf file string.
346 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000347 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000348
Ed Masteda706022014-05-07 12:49:30 +0000349 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000350 const LangOptions &LO = CGM.getLangOpts();
351 if (LO.CPlusPlus) {
352 if (LO.ObjC1)
353 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
354 else
355 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
356 } else if (LO.ObjC1) {
357 LangTag = llvm::dwarf::DW_LANG_ObjC;
358 } else if (LO.C99) {
359 LangTag = llvm::dwarf::DW_LANG_C99;
360 } else {
361 LangTag = llvm::dwarf::DW_LANG_C89;
362 }
363
364 std::string Producer = getClangFullVersion();
365
366 // Figure out which version of the ObjC runtime we have.
367 unsigned RuntimeVers = 0;
368 if (LO.ObjC1)
369 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
370
371 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000373 TheCU = DBuilder.createCompileUnit(
374 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
375 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000376 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000377 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000378 : llvm::DIBuilder::FullDebug,
379 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000380}
381
382/// CreateType - Get the Basic type from the cache or create a new
383/// one if necessary.
384llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000385 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000386 StringRef BTName;
387 switch (BT->getKind()) {
388#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000389#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000390#include "clang/AST/BuiltinTypes.def"
391 case BuiltinType::Dependent:
392 llvm_unreachable("Unexpected builtin type");
393 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000394 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000395 case BuiltinType::Void:
396 return llvm::DIType();
397 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000398 if (!ClassTy)
399 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
400 "objc_class", TheCU,
401 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000402 return ClassTy;
403 case BuiltinType::ObjCId: {
404 // typedef struct objc_class *Class;
405 // typedef struct objc_object {
406 // Class isa;
407 // } *id;
408
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000409 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 return ObjTy;
411
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000412 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
414 "objc_class", TheCU,
415 getOrCreateMainFile(), 0);
416
417 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000418
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
420
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000421 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000422 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
423 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000424
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000425 DBuilder.replaceArrays(
426 ObjTy,
427 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
428 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 return ObjTy;
430 }
431 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000432 if (!SelTy)
433 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
434 "objc_selector", TheCU,
435 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 return SelTy;
437 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000438
439 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000440 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000442 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000443 OCLImage1dArrayDITy);
444 case BuiltinType::OCLImage1dBuffer:
445 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
446 OCLImage1dBufferDITy);
447 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000448 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449 case BuiltinType::OCLImage2dArray:
450 return getOrCreateStructPtrType("opencl_image2d_array_t",
451 OCLImage2dArrayDITy);
452 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000453 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000454 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000455 return DBuilder.createBasicType(
456 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
457 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000458 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000460
Guy Benyei11169dd2012-12-18 14:30:41 +0000461 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000462 case BuiltinType::Char_U:
463 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
464 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000466 case BuiltinType::SChar:
467 Encoding = llvm::dwarf::DW_ATE_signed_char;
468 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000469 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000470 case BuiltinType::Char32:
471 Encoding = llvm::dwarf::DW_ATE_UTF;
472 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473 case BuiltinType::UShort:
474 case BuiltinType::UInt:
475 case BuiltinType::UInt128:
476 case BuiltinType::ULong:
477 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000478 case BuiltinType::ULongLong:
479 Encoding = llvm::dwarf::DW_ATE_unsigned;
480 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 case BuiltinType::Short:
482 case BuiltinType::Int:
483 case BuiltinType::Int128:
484 case BuiltinType::Long:
485 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000486 case BuiltinType::LongLong:
487 Encoding = llvm::dwarf::DW_ATE_signed;
488 break;
489 case BuiltinType::Bool:
490 Encoding = llvm::dwarf::DW_ATE_boolean;
491 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000492 case BuiltinType::Half:
493 case BuiltinType::Float:
494 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000495 case BuiltinType::Double:
496 Encoding = llvm::dwarf::DW_ATE_float;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 }
499
500 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000501 case BuiltinType::Long:
502 BTName = "long int";
503 break;
504 case BuiltinType::LongLong:
505 BTName = "long long int";
506 break;
507 case BuiltinType::ULong:
508 BTName = "long unsigned int";
509 break;
510 case BuiltinType::ULongLong:
511 BTName = "long long unsigned int";
512 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 default:
514 BTName = BT->getName(CGM.getLangOpts());
515 break;
516 }
517 // Bit size, align and offset of the type.
518 uint64_t Size = CGM.getContext().getTypeSize(BT);
519 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000520 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000521 return DbgTy;
522}
523
524llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
525 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000526 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 if (Ty->isComplexIntegerType())
528 Encoding = llvm::dwarf::DW_ATE_lo_user;
529
530 uint64_t Size = CGM.getContext().getTypeSize(Ty);
531 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000532 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000533 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000534
535 return DbgTy;
536}
537
538/// CreateCVRType - Get the qualified type from the cache or create
539/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000540llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000541 QualifierCollector Qc;
542 const Type *T = Qc.strip(Ty);
543
544 // Ignore these qualifiers for now.
545 Qc.removeObjCGCAttr();
546 Qc.removeAddressSpace();
547 Qc.removeObjCLifetime();
548
549 // We will create one Derived type for one qualifier and recurse to handle any
550 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000551 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000552 if (Qc.hasConst()) {
553 Tag = llvm::dwarf::DW_TAG_const_type;
554 Qc.removeConst();
555 } else if (Qc.hasVolatile()) {
556 Tag = llvm::dwarf::DW_TAG_volatile_type;
557 Qc.removeVolatile();
558 } else if (Qc.hasRestrict()) {
559 Tag = llvm::dwarf::DW_TAG_restrict_type;
560 Qc.removeRestrict();
561 } else {
562 assert(Qc.empty() && "Unknown type qualifier for debug info");
563 return getOrCreateType(QualType(T, 0), Unit);
564 }
565
David Blaikie99dab3b2013-09-04 22:03:57 +0000566 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000567
568 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
569 // CVR derived types.
570 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000571
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 return DbgTy;
573}
574
575llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
576 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000577
578 // The frontend treats 'id' as a typedef to an ObjCObjectType,
579 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
580 // debug info, we want to emit 'id' in both cases.
581 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000582 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000583
Eric Christophere7b87e52014-10-26 23:40:33 +0000584 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
585 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 return DbgTy;
587}
588
Eric Christophere7b87e52014-10-26 23:40:33 +0000589llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000590 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000591 Ty->getPointeeType(), Unit);
592}
593
Manman Rene0064d82013-08-29 23:19:58 +0000594/// In C++ mode, types have linkage, so we can rely on the ODR and
595/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000596static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
597 CodeGenModule &CGM,
598 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000599 SmallString<256> FullName;
600 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
601 // For now, only apply ODR with C++.
602 const TagDecl *TD = Ty->getDecl();
603 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
604 !TD->isExternallyVisible())
605 return FullName;
606 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
607 if (CGM.getTarget().getCXXABI().isMicrosoft())
608 return FullName;
609
610 // TODO: This is using the RTTI name. Is there a better way to get
611 // a unique string for a type?
612 llvm::raw_svector_ostream Out(FullName);
613 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
614 Out.flush();
615 return FullName;
616}
617
Guy Benyei11169dd2012-12-18 14:30:41 +0000618// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000619llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000620CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000621 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000622 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000623 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000624 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000625 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
626 unsigned Line = getLineNumber(RD->getLocation());
627 StringRef RDName = getClassName(RD);
628
Ed Masteda706022014-05-07 12:49:30 +0000629 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000630 if (RD->isStruct() || RD->isInterface())
631 Tag = llvm::dwarf::DW_TAG_structure_type;
632 else if (RD->isUnion())
633 Tag = llvm::dwarf::DW_TAG_union_type;
634 else {
635 assert(RD->isClass());
636 Tag = llvm::dwarf::DW_TAG_class_type;
637 }
638
639 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000640 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000641 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
642 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000643 ReplaceMap.emplace_back(
644 std::piecewise_construct, std::make_tuple(Ty),
645 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000646 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000647}
648
Ed Masteda706022014-05-07 12:49:30 +0000649llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000650 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 QualType PointeeTy,
652 llvm::DIFile Unit) {
653 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
654 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000655 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000656
Guy Benyei11169dd2012-12-18 14:30:41 +0000657 // Bit size, align and offset of the type.
658 // Size is always the size of a pointer. We can't use getTypeSize here
659 // because that does not return the correct value for references.
660 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000661 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000662 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
663
David Blaikie99dab3b2013-09-04 22:03:57 +0000664 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
665 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000666}
667
Eric Christopher0fdcb312013-05-16 00:52:20 +0000668llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
669 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000670 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000671 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000672 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
673 TheCU, getOrCreateMainFile(), 0);
674 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
675 Cache = DBuilder.createPointerType(Cache, Size);
676 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000677}
678
Guy Benyei11169dd2012-12-18 14:30:41 +0000679llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
680 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000681 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 return BlockLiteralGeneric;
683
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000684 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000685 llvm::DIType FieldTy;
686 QualType FType;
687 uint64_t FieldSize, FieldOffset;
688 unsigned FieldAlign;
689 llvm::DIArray Elements;
690 llvm::DIType EltTy, DescTy;
691
692 FieldOffset = 0;
693 FType = CGM.getContext().UnsignedLongTy;
694 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
695 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
696
697 Elements = DBuilder.getOrCreateArray(EltTys);
698 EltTys.clear();
699
700 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
701 unsigned LineNo = getLineNumber(CurLoc);
702
Eric Christophere7b87e52014-10-26 23:40:33 +0000703 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
704 FieldOffset, 0, Flags, llvm::DIType(),
705 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000706
707 // Bit size, align and offset of the type.
708 uint64_t Size = CGM.getContext().getTypeSize(Ty);
709
710 DescTy = DBuilder.createPointerType(EltTy, Size);
711
712 FieldOffset = 0;
713 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
714 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
715 FType = CGM.getContext().IntTy;
716 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
717 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000718 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000719 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
720
721 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
722 FieldTy = DescTy;
723 FieldSize = CGM.getContext().getTypeSize(Ty);
724 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000725 FieldTy =
726 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
727 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000728 EltTys.push_back(FieldTy);
729
730 FieldOffset += FieldSize;
731 Elements = DBuilder.getOrCreateArray(EltTys);
732
Eric Christophere7b87e52014-10-26 23:40:33 +0000733 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
734 LineNo, FieldOffset, 0, Flags,
735 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000736
Guy Benyei11169dd2012-12-18 14:30:41 +0000737 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
738 return BlockLiteralGeneric;
739}
740
Eric Christophere7b87e52014-10-26 23:40:33 +0000741llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
742 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000743 assert(Ty->isTypeAlias());
744 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000745
746 SmallString<128> NS;
747 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000748 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
749 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000750
751 TemplateSpecializationType::PrintTemplateArgumentList(
752 OS, Ty->getArgs(), Ty->getNumArgs(),
753 CGM.getContext().getPrintingPolicy());
754
Eric Christophere7b87e52014-10-26 23:40:33 +0000755 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
756 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000757
758 SourceLocation Loc = AliasDecl->getLocation();
759 llvm::DIFile File = getOrCreateFile(Loc);
760 unsigned Line = getLineNumber(Loc);
761
Eric Christophere7b87e52014-10-26 23:40:33 +0000762 llvm::DIDescriptor Ctxt =
763 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000764
765 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
766}
767
David Blaikie99dab3b2013-09-04 22:03:57 +0000768llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000769 // Typedefs are derived from some other type. If we have a typedef of a
770 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000771 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 // We don't set size information, but do specify where the typedef was
773 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000774 SourceLocation Loc = Ty->getDecl()->getLocation();
775 llvm::DIFile File = getOrCreateFile(Loc);
776 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000777 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000778
Guy Benyei11169dd2012-12-18 14:30:41 +0000779 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000780 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000781
Eric Christophere7b87e52014-10-26 23:40:33 +0000782 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
783 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000784}
785
786llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
787 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000788 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000789
790 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000791 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000792
793 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000794 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 if (isa<FunctionNoProtoType>(Ty))
796 EltTys.push_back(DBuilder.createUnspecifiedParameter());
797 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000798 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
799 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000800 if (FPT->isVariadic())
801 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000802 }
803
Manman Ren67f005e2014-07-28 22:24:34 +0000804 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 return DBuilder.createSubroutineType(Unit, EltTypeArray);
806}
807
Adrian Prantl21361fb2014-08-29 22:44:27 +0000808/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
809/// As an optimization, return 0 if the access specifier equals the
810/// default for the containing type.
811static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
812 AccessSpecifier Default = clang::AS_none;
813 if (RD && RD->isClass())
814 Default = clang::AS_private;
815 else if (RD && (RD->isStruct() || RD->isUnion()))
816 Default = clang::AS_public;
817
818 if (Access == Default)
819 return 0;
820
Eric Christophere7b87e52014-10-26 23:40:33 +0000821 switch (Access) {
822 case clang::AS_private:
823 return llvm::DIDescriptor::FlagPrivate;
824 case clang::AS_protected:
825 return llvm::DIDescriptor::FlagProtected;
826 case clang::AS_public:
827 return llvm::DIDescriptor::FlagPublic;
828 case clang::AS_none:
829 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000830 }
831 llvm_unreachable("unexpected access enumerator");
832}
Guy Benyei11169dd2012-12-18 14:30:41 +0000833
Eric Christophere7b87e52014-10-26 23:40:33 +0000834llvm::DIType CGDebugInfo::createFieldType(
835 StringRef name, QualType type, uint64_t sizeInBitsOverride,
836 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
837 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 llvm::DIType debugType = getOrCreateType(type, tunit);
839
840 // Get the location for the field.
841 llvm::DIFile file = getOrCreateFile(loc);
842 unsigned line = getLineNumber(loc);
843
David Majnemer34b57492014-07-30 01:30:47 +0000844 uint64_t SizeInBits = 0;
845 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000847 TypeInfo TI = CGM.getContext().getTypeInfo(type);
848 SizeInBits = TI.Width;
849 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000850
851 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000852 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000853 }
854
Adrian Prantl21361fb2014-08-29 22:44:27 +0000855 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000856 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
857 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000858}
859
Eric Christopher91a31902013-01-16 01:22:32 +0000860/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000861void CGDebugInfo::CollectRecordLambdaFields(
862 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &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);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000931 StaticDataMemberCache[Var->getCanonicalDecl()].reset(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,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000938 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000939 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.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000962void CGDebugInfo::CollectRecordFields(
963 const RecordDecl *record, llvm::DIFile tunit,
964 SmallVectorImpl<llvm::Metadata *> &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
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000981 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000982 if (MI != StaticDataMemberCache.end()) {
983 assert(MI->second &&
984 "Static data member declaration should still exist");
985 elements.push_back(
986 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000987 } else {
988 auto Field = CreateRecordStaticField(V, RecordTy, record);
989 elements.push_back(Field);
990 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000991 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000992 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
993 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000994
995 // Bump field number for next field.
996 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000997 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000998 }
999}
1000
1001/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1002/// function type is not updated to include implicit "this" pointer. Use this
1003/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001004llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001005CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1006 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001007 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001008 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001009 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001010 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1011 Func, Unit);
1012}
David Blaikie2aaf0652013-01-07 22:24:59 +00001013
David Blaikie469f0792013-05-22 23:22:42 +00001014llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001015 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001016 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001017 llvm::DITypeArray Args = llvm::DISubroutineType(
1018 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001019 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001020
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001021 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001022
1023 // First element is always return type. For 'void' functions it is NULL.
1024 Elts.push_back(Args.getElement(0));
1025
David Blaikie2aaf0652013-01-07 22:24:59 +00001026 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001027 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001028 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1029 // Create pointer type directly in this case.
1030 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1031 QualType PointeeTy = ThisPtrTy->getPointeeType();
1032 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001033 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001034 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1035 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001036 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001037 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001038 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001039 // TODO: This and the artificial type below are misleading, the
1040 // types aren't artificial the argument is, but the current
1041 // metadata doesn't represent that.
1042 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1043 Elts.push_back(ThisPtrType);
1044 } else {
1045 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001046 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001047 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1048 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001049 }
1050
1051 // Copy rest of the arguments.
1052 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1053 Elts.push_back(Args.getElement(i));
1054
Manman Ren67f005e2014-07-28 22:24:34 +00001055 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001056
Adrian Prantl0630eb72013-12-18 21:48:18 +00001057 unsigned Flags = 0;
1058 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1059 Flags |= llvm::DIDescriptor::FlagLValueReference;
1060 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1061 Flags |= llvm::DIDescriptor::FlagRValueReference;
1062
1063 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001064}
1065
Eric Christopherb2a008c2013-05-16 00:45:12 +00001066/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001067/// inside a function.
1068static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1069 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1070 return isFunctionLocalClass(NRD);
1071 if (isa<FunctionDecl>(RD->getDeclContext()))
1072 return true;
1073 return false;
1074}
1075
1076/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1077/// a single member function GlobalDecl.
1078llvm::DISubprogram
1079CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001080 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001081 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001082 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001083
Guy Benyei11169dd2012-12-18 14:30:41 +00001084 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001085 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001086
1087 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1088 // make sense to give a single ctor/dtor a linkage name.
1089 StringRef MethodLinkageName;
1090 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1091 MethodLinkageName = CGM.getMangledName(Method);
1092
1093 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001094 llvm::DIFile MethodDefUnit;
1095 unsigned MethodLine = 0;
1096 if (!Method->isImplicit()) {
1097 MethodDefUnit = getOrCreateFile(Method->getLocation());
1098 MethodLine = getLineNumber(Method->getLocation());
1099 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001100
1101 // Collect virtual method info.
1102 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001105
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 if (Method->isVirtual()) {
1107 if (Method->isPure())
1108 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1109 else
1110 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001111
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 // It doesn't make sense to give a virtual destructor a vtable index,
1113 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001114 // FIXME: Add proper support for debug info for virtual calls in
1115 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1116 // lookup if we have multiple or virtual inheritance.
1117 if (!isa<CXXDestructorDecl>(Method) &&
1118 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001119 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 ContainingType = RecordTy;
1121 }
1122
1123 unsigned Flags = 0;
1124 if (Method->isImplicit())
1125 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001126 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1128 if (CXXC->isExplicit())
1129 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001130 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001131 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 if (CXXC->isExplicit())
1133 Flags |= llvm::DIDescriptor::FlagExplicit;
1134 }
1135 if (Method->hasPrototype())
1136 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001137 if (Method->getRefQualifier() == RQ_LValue)
1138 Flags |= llvm::DIDescriptor::FlagLValueReference;
1139 if (Method->getRefQualifier() == RQ_RValue)
1140 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001141
1142 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001143 llvm::DISubprogram SP = DBuilder.createMethod(
1144 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1145 MethodTy, /*isLocalToUnit=*/false,
1146 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1147 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001149 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001150
1151 return SP;
1152}
1153
1154/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001155/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001156/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001157void CGDebugInfo::CollectCXXMemberFunctions(
1158 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001159 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001160
1161 // Since we want more than just the individual member decls if we
1162 // have templated functions iterate over every declaration to gather
1163 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001164 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001165 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1166 // If the member is implicit, don't add it to the member list. This avoids
1167 // the member being added to type units by LLVM, while still allowing it
1168 // to be emitted into the type declaration/reference inside the compile
1169 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001170 // FIXME: Handle Using(Shadow?)Decls here to create
1171 // DW_TAG_imported_declarations inside the class for base decls brought into
1172 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1173 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1174 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001175 if (!Method || Method->isImplicit())
1176 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001177
1178 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1179 continue;
1180
David Blaikiefd580722014-10-06 05:18:55 +00001181 // Reuse the existing member function declaration if it exists.
1182 // It may be associated with the declaration of the type & should be
1183 // reused as we're building the definition.
1184 //
1185 // This situation can arise in the vtable-based debug info reduction where
1186 // implicit members are emitted in a non-vtable TU.
1187 auto MI = SPCache.find(Method->getCanonicalDecl());
1188 EltTys.push_back(MI == SPCache.end()
1189 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001190 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001192}
Guy Benyei11169dd2012-12-18 14:30:41 +00001193
Guy Benyei11169dd2012-12-18 14:30:41 +00001194/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001195/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001196/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001197void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001198 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001199 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001200
1201 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001202 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001203 unsigned BFlags = 0;
1204 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001205
Guy Benyei11169dd2012-12-18 14:30:41 +00001206 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001207 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001208
Aaron Ballman574705e2014-03-13 15:41:46 +00001209 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001210 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1211 // virtual base offset offset is -ve. The code generator emits dwarf
1212 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001213 BaseOffset = 0 - CGM.getItaniumVTableContext()
1214 .getVirtualBaseOffsetOffset(RD, Base)
1215 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001216 } else {
1217 // In the MS ABI, store the vbtable offset, which is analogous to the
1218 // vbase offset offset in Itanium.
1219 BaseOffset =
1220 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1221 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001222 BFlags = llvm::DIDescriptor::FlagVirtual;
1223 } else
1224 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1225 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1226 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001227
Adrian Prantl21361fb2014-08-29 22:44:27 +00001228 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001229 llvm::DIType DTy = DBuilder.createInheritance(
1230 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001231 EltTys.push_back(DTy);
1232 }
1233}
1234
1235/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001236llvm::DIArray
1237CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1238 ArrayRef<TemplateArgument> TAList,
1239 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001240 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1242 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001243 StringRef Name;
1244 if (TPList)
1245 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001246 switch (TA.getKind()) {
1247 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1249 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001250 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001251 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001252 } break;
1253 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1255 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001256 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001257 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001258 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1259 TemplateParams.push_back(TVP);
1260 } break;
1261 case TemplateArgument::Declaration: {
1262 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001263 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001264 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001265 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001266 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001267 // Variable pointer template parameters have a value that is the address
1268 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001269 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001270 V = CGM.GetAddrOfGlobalVar(VD);
1271 // Member function pointers have special support for building them, though
1272 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001273 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001274 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001275 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001276 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001277 // Member data pointers have special handling too to compute the fixed
1278 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001279 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001280 // These five lines (& possibly the above member function pointer
1281 // handling) might be able to be refactored to use similar code in
1282 // CodeGenModule::getMemberPointerConstant
1283 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1284 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001285 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001286 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001287 }
1288 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001289 DBuilder.createTemplateValueParameter(
1290 TheCU, Name, TTy,
1291 cast_or_null<llvm::Constant>(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);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001297 llvm::Constant *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 =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001312 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1313 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001314 TemplateParams.push_back(TVP);
1315 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001316 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001317 llvm::DITemplateValueParameter
1318 TVP = DBuilder.createTemplateTemplateParameter(
1319 TheCU, Name, llvm::DIType(),
1320 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001321 TemplateParams.push_back(TVP);
1322 } break;
1323 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001324 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1325 TheCU, Name, llvm::DIType(),
1326 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001327 TemplateParams.push_back(TVP);
1328 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001329 case TemplateArgument::Expression: {
1330 const Expr *E = TA.getAsExpr();
1331 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001332 if (E->isGLValue())
1333 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001334 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001335 assert(V && "Expression in template argument isn't constant");
1336 llvm::DIType TTy = getOrCreateType(T, Unit);
1337 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001338 DBuilder.createTemplateValueParameter(
1339 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001340 TemplateParams.push_back(TVP);
1341 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001342 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001344 case TemplateArgument::Null:
1345 llvm_unreachable(
1346 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 }
1348 }
1349 return DBuilder.getOrCreateArray(TemplateParams);
1350}
1351
1352/// CollectFunctionTemplateParams - A helper function to collect debug
1353/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001354llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1355 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001356 if (FD->getTemplatedKind() ==
1357 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001358 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1359 ->getTemplate()
1360 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001361 return CollectTemplateParams(
1362 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 }
1364 return llvm::DIArray();
1365}
1366
1367/// CollectCXXTemplateParams - A helper function to collect debug info for
1368/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001369llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1370 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001371 // Always get the full list of parameters, not just the ones from
1372 // the specialization.
1373 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001374 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001375 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001376 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001377}
1378
1379/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1380llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1381 if (VTablePtrType.isValid())
1382 return VTablePtrType;
1383
1384 ASTContext &Context = CGM.getContext();
1385
1386 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001387 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001388 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001389 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1390 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001391 llvm::DIType vtbl_ptr_type =
1392 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001393 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1394 return VTablePtrType;
1395}
1396
1397/// getVTableName - Get vtable name for the given Class.
1398StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001399 // Copy the gdb compatible name on the side and use its reference.
1400 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001401}
1402
Guy Benyei11169dd2012-12-18 14:30:41 +00001403/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1404/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001405void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001406 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1408
1409 // If there is a primary base then it will hold vtable info.
1410 if (RL.getPrimaryBase())
1411 return;
1412
1413 // If this class is not dynamic then there is not any vtable info to collect.
1414 if (!RD->isDynamicClass())
1415 return;
1416
1417 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001418 llvm::DIType VPTR = DBuilder.createMemberType(
1419 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1420 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 EltTys.push_back(VPTR);
1422}
1423
Eric Christopherb2a008c2013-05-16 00:45:12 +00001424/// getOrCreateRecordType - Emit record type's standalone debug info.
1425llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001427 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001428 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1429 return T;
1430}
1431
1432/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1433/// debug info.
1434llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001435 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001436 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001438 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001439 return T;
1440}
1441
David Blaikie483a9da2014-05-06 18:35:21 +00001442void CGDebugInfo::completeType(const EnumDecl *ED) {
1443 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1444 return;
1445 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001446 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001447 auto I = TypeCache.find(TyPtr);
1448 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001449 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001450 return;
1451 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1452 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001453 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001454}
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() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001483 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001484 return;
1485 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1486 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001487 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001488}
1489
David Blaikie0e716b42014-03-03 23:48:23 +00001490static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1491 CXXRecordDecl::method_iterator End) {
1492 for (; I != End; ++I)
1493 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001494 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1495 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001496 return true;
1497 return false;
1498}
1499
1500static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1501 const RecordDecl *RD,
1502 const LangOptions &LangOpts) {
1503 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1504 return false;
1505
1506 if (!LangOpts.CPlusPlus)
1507 return false;
1508
1509 if (!RD->isCompleteDefinitionRequired())
1510 return true;
1511
1512 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1513
1514 if (!CXXDecl)
1515 return false;
1516
1517 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1518 return true;
1519
1520 TemplateSpecializationKind Spec = TSK_Undeclared;
1521 if (const ClassTemplateSpecializationDecl *SD =
1522 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1523 Spec = SD->getSpecializationKind();
1524
1525 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1526 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1527 CXXDecl->method_end()))
1528 return true;
1529
1530 return false;
1531}
1532
Guy Benyei11169dd2012-12-18 14:30:41 +00001533/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001534llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001536 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001537 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001538 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001539 T = getOrCreateRecordFwdDecl(
1540 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001541 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001542 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001543
David Blaikieb2e86eb2013-08-15 20:49:17 +00001544 return CreateTypeDefinition(Ty);
1545}
1546
1547llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1548 RecordDecl *RD = Ty->getDecl();
1549
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 // Get overall information about the record type for the debug info.
1551 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1552
1553 // Records and classes and unions can all be recursive. To handle them, we
1554 // first generate a debug descriptor for the struct as a forward declaration.
1555 // Then (if it is a definition) we go through and get debug info for all of
1556 // its members. Finally, we create a descriptor for the complete type (which
1557 // may refer to the forward decl if the struct is recursive) and replace all
1558 // uses of the forward declaration with the final definition.
1559
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001560 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001561 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001562 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001563
1564 if (FwdDecl.isForwardDecl())
1565 return FwdDecl;
1566
David Blaikieadfbf992013-08-18 16:55:33 +00001567 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1568 CollectContainingType(CXXDecl, FwdDecl);
1569
Guy Benyei11169dd2012-12-18 14:30:41 +00001570 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001571 LexicalBlockStack.emplace_back(&*FwdDecl);
1572 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001573
Guy Benyei11169dd2012-12-18 14:30:41 +00001574 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001575 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001576 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001577
1578 // Note: The split of CXXDecl information here is intentional, the
1579 // gdb tests will depend on a certain ordering at printout. The debug
1580 // information offsets are still correct if we merge them all together
1581 // though.
1582 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1583 if (CXXDecl) {
1584 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1585 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1586 }
1587
Eric Christopher91a31902013-01-16 01:22:32 +00001588 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001589 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001590 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001591 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001592
1593 LexicalBlockStack.pop_back();
1594 RegionMap.erase(Ty->getDecl());
1595
1596 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001597 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001599 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001600 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001601}
1602
1603/// CreateType - get objective-c object type.
1604llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1605 llvm::DIFile Unit) {
1606 // Ignore protocols.
1607 return getOrCreateType(Ty->getBaseType(), Unit);
1608}
1609
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001610/// \return true if Getter has the default name for the property PD.
1611static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1612 const ObjCMethodDecl *Getter) {
1613 assert(PD);
1614 if (!Getter)
1615 return true;
1616
1617 assert(Getter->getDeclName().isObjCZeroArgSelector());
1618 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001619 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001620}
1621
1622/// \return true if Setter has the default name for the property PD.
1623static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1624 const ObjCMethodDecl *Setter) {
1625 assert(PD);
1626 if (!Setter)
1627 return true;
1628
1629 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001630 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001631 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001632}
1633
Guy Benyei11169dd2012-12-18 14:30:41 +00001634/// CreateType - get objective-c interface type.
1635llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1636 llvm::DIFile Unit) {
1637 ObjCInterfaceDecl *ID = Ty->getDecl();
1638 if (!ID)
1639 return llvm::DIType();
1640
1641 // Get overall information about the record type for the debug info.
1642 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1643 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001644 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001645
1646 // If this is just a forward declaration return a special forward-declaration
1647 // debug type since we won't be able to lay out the entire type.
1648 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001649 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001650 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1651 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1652 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001653 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001654 return FwdDecl;
1655 }
1656
David Blaikieef8a9512014-05-05 23:23:53 +00001657 return CreateTypeDefinition(Ty, Unit);
1658}
1659
Eric Christophere7b87e52014-10-26 23:40:33 +00001660llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1661 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001662 ObjCInterfaceDecl *ID = Ty->getDecl();
1663 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1664 unsigned Line = getLineNumber(ID->getLocation());
1665 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001666
1667 // Bit size, align and offset of the type.
1668 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1669 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1670
1671 unsigned Flags = 0;
1672 if (ID->getImplementation())
1673 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1674
Eric Christophere7b87e52014-10-26 23:40:33 +00001675 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1676 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1677 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
David Blaikieef8a9512014-05-05 23:23:53 +00001679 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001680 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001682 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001683 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1684 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
1686 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001687 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001688
1689 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1690 if (SClass) {
1691 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001692 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001693 if (!SClassTy.isValid())
1694 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001695
Eric Christophere7b87e52014-10-26 23:40:33 +00001696 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001697 EltTys.push_back(InhTag);
1698 }
1699
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001700 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001701 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001702 SourceLocation Loc = PD->getLocation();
1703 llvm::DIFile PUnit = getOrCreateFile(Loc);
1704 unsigned PLine = getLineNumber(Loc);
1705 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1706 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001707 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1708 PD->getName(), PUnit, PLine,
1709 hasDefaultGetterName(PD, Getter) ? ""
1710 : getSelectorName(PD->getGetterName()),
1711 hasDefaultSetterName(PD, Setter) ? ""
1712 : getSelectorName(PD->getSetterName()),
1713 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001714 EltTys.push_back(PropertyNode);
1715 }
1716
1717 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1718 unsigned FieldNo = 0;
1719 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1720 Field = Field->getNextIvar(), ++FieldNo) {
1721 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1722 if (!FieldTy.isValid())
1723 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001724
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 StringRef FieldName = Field->getName();
1726
1727 // Ignore unnamed fields.
1728 if (FieldName.empty())
1729 continue;
1730
1731 // Get the location for the field.
1732 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1733 unsigned FieldLine = getLineNumber(Field->getLocation());
1734 QualType FType = Field->getType();
1735 uint64_t FieldSize = 0;
1736 unsigned FieldAlign = 0;
1737
1738 if (!FType->isIncompleteArrayType()) {
1739
1740 // Bit size, align and offset of the type.
1741 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001742 ? Field->getBitWidthValue(CGM.getContext())
1743 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001744 FieldAlign = CGM.getContext().getTypeAlign(FType);
1745 }
1746
1747 uint64_t FieldOffset;
1748 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1749 // We don't know the runtime offset of an ivar if we're using the
1750 // non-fragile ABI. For bitfields, use the bit offset into the first
1751 // byte of storage of the bitfield. For other fields, use zero.
1752 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001753 FieldOffset =
1754 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001755 FieldOffset %= CGM.getContext().getCharWidth();
1756 } else {
1757 FieldOffset = 0;
1758 }
1759 } else {
1760 FieldOffset = RL.getFieldOffset(FieldNo);
1761 }
1762
1763 unsigned Flags = 0;
1764 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1765 Flags = llvm::DIDescriptor::FlagProtected;
1766 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1767 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001768 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1769 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001770
Craig Topper8a13c412014-05-21 05:09:00 +00001771 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001773 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001774 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001776 SourceLocation Loc = PD->getLocation();
1777 llvm::DIFile PUnit = getOrCreateFile(Loc);
1778 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1780 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001781 PropertyNode = DBuilder.createObjCProperty(
1782 PD->getName(), PUnit, PLine,
1783 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1784 PD->getGetterName()),
1785 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1786 PD->getSetterName()),
1787 PD->getPropertyAttributes(),
1788 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 }
1790 }
1791 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001792 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1793 FieldSize, FieldAlign, FieldOffset, Flags,
1794 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 EltTys.push_back(FieldTy);
1796 }
1797
1798 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001799 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001800
Guy Benyei11169dd2012-12-18 14:30:41 +00001801 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001802 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001803}
1804
1805llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1806 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1807 int64_t Count = Ty->getNumElements();
1808 if (Count == 0)
1809 // If number of elements are not known then this is an unbounded array.
1810 // Use Count == -1 to express such arrays.
1811 Count = -1;
1812
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001813 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001814 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1815
1816 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1817 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1818
1819 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1820}
1821
Eric Christophere7b87e52014-10-26 23:40:33 +00001822llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001823 uint64_t Size;
1824 uint64_t Align;
1825
1826 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1827 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1828 Size = 0;
1829 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001830 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001831 } else if (Ty->isIncompleteArrayType()) {
1832 Size = 0;
1833 if (Ty->getElementType()->isIncompleteType())
1834 Align = 0;
1835 else
1836 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001837 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001838 Size = 0;
1839 Align = 0;
1840 } else {
1841 // Size and align of the whole array, not the element type.
1842 Size = CGM.getContext().getTypeSize(Ty);
1843 Align = CGM.getContext().getTypeAlign(Ty);
1844 }
1845
1846 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1847 // interior arrays, do we care? Why aren't nested arrays represented the
1848 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001849 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001850 QualType EltTy(Ty, 0);
1851 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1852 // If the number of elements is known, then count is that number. Otherwise,
1853 // it's -1. This allows us to represent a subrange with an array of 0
1854 // elements, like this:
1855 //
1856 // struct foo {
1857 // int x[0];
1858 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001859 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001860 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1861 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001862
Guy Benyei11169dd2012-12-18 14:30:41 +00001863 // FIXME: Verify this is right for VLAs.
1864 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1865 EltTy = Ty->getElementType();
1866 }
1867
1868 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1869
Eric Christophere7b87e52014-10-26 23:40:33 +00001870 llvm::DIType DbgTy = DBuilder.createArrayType(
1871 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872 return DbgTy;
1873}
1874
Eric Christopherb2a008c2013-05-16 00:45:12 +00001875llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001876 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001877 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1878 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001879}
1880
Eric Christopherb2a008c2013-05-16 00:45:12 +00001881llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001883 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1884 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001885}
1886
Eric Christopherb2a008c2013-05-16 00:45:12 +00001887llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001889 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1890 if (!Ty->getPointeeType()->isFunctionType())
1891 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001892 getOrCreateType(Ty->getPointeeType(), U), ClassType,
1893 CGM.PointerWidthInBits);
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),
Adrian Prantlee24e142014-12-23 19:11:54 +00001901 ClassType, CGM.PointerWidthInBits);
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);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001933 ReplaceMap.emplace_back(
1934 std::piecewise_construct, std::make_tuple(Ty),
1935 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001936 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 }
1938
David Blaikie483a9da2014-05-06 18:35:21 +00001939 return CreateTypeDefinition(Ty);
1940}
1941
1942llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1943 const EnumDecl *ED = Ty->getDecl();
1944 uint64_t Size = 0;
1945 uint64_t Align = 0;
1946 if (!ED->getTypeForDecl()->isIncompleteType()) {
1947 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1948 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1949 }
1950
1951 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1952
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001954 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001955 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001956 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001957 Enumerators.push_back(DBuilder.createEnumerator(
1958 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001959 }
1960
1961 // Return a CompositeType for the enum itself.
1962 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1963
1964 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1965 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001966 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001967 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1968 llvm::DIType ClassTy = ED->isFixed()
1969 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1970 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001971 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001972 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1973 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 return DbgTy;
1975}
1976
David Blaikie05491062013-01-21 04:37:12 +00001977static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1978 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001980 Qualifiers InnerQuals = T.getLocalQualifiers();
1981 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1982 // that is already there.
1983 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1984 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 QualType LastT = T;
1986 switch (T->getTypeClass()) {
1987 default:
David Blaikie05491062013-01-21 04:37:12 +00001988 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001989 case Type::TemplateSpecialization: {
1990 const auto *Spec = cast<TemplateSpecializationType>(T);
1991 if (Spec->isTypeAlias())
1992 return C.getQualifiedType(T.getTypePtr(), Quals);
1993 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001994 break;
1995 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 case Type::TypeOfExpr:
1997 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1998 break;
1999 case Type::TypeOf:
2000 T = cast<TypeOfType>(T)->getUnderlyingType();
2001 break;
2002 case Type::Decltype:
2003 T = cast<DecltypeType>(T)->getUnderlyingType();
2004 break;
2005 case Type::UnaryTransform:
2006 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2007 break;
2008 case Type::Attributed:
2009 T = cast<AttributedType>(T)->getEquivalentType();
2010 break;
2011 case Type::Elaborated:
2012 T = cast<ElaboratedType>(T)->getNamedType();
2013 break;
2014 case Type::Paren:
2015 T = cast<ParenType>(T)->getInnerType();
2016 break;
David Blaikie05491062013-01-21 04:37:12 +00002017 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 break;
2020 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002021 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002022 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002023 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 break;
2025 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002026
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002028 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 } while (true);
2030}
2031
Eric Christopher0fdcb312013-05-16 00:52:20 +00002032/// getType - Get the type from the cache or return null type if it doesn't
2033/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002034llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2035
2036 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002037 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002038
David Blaikief427b002014-05-06 03:42:01 +00002039 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 if (it != TypeCache.end()) {
2041 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002042 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 return llvm::DIType(cast<llvm::MDNode>(V));
2044 }
2045
2046 return llvm::DIType();
2047}
2048
David Blaikie0e716b42014-03-03 23:48:23 +00002049void CGDebugInfo::completeTemplateDefinition(
2050 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002051 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2052 return;
2053
David Blaikie0e716b42014-03-03 23:48:23 +00002054 completeClassData(&SD);
2055 // In case this type has no member function definitions being emitted, ensure
2056 // it is retained
2057 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2058}
2059
Guy Benyei11169dd2012-12-18 14:30:41 +00002060/// getOrCreateType - Get the type from the cache or create a new
2061/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002062llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002063 if (Ty.isNull())
2064 return llvm::DIType();
2065
2066 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002067 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002068
David Blaikieef8a9512014-05-05 23:23:53 +00002069 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 return T;
2071
2072 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002073 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002074 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002075
2076 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002077 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002078
Guy Benyei11169dd2012-12-18 14:30:41 +00002079 return Res;
2080}
2081
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002082/// Currently the checksum of an interface includes the number of
2083/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002084unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002085 // The assumption is that the number of ivars can only increase
2086 // monotonically, so it is safe to just use their current number as
2087 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002088 unsigned Sum = 0;
2089 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002090 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002091 ++Sum;
2092
2093 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002094}
2095
2096ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2097 switch (Ty->getTypeClass()) {
2098 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002099 return getObjCInterfaceDecl(
2100 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002101 case Type::ObjCInterface:
2102 return cast<ObjCInterfaceType>(Ty)->getDecl();
2103 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002104 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002105 }
2106}
2107
Guy Benyei11169dd2012-12-18 14:30:41 +00002108/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002109llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 // Handle qualifiers, which recursively handles what they refer to.
2111 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002112 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002113
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 // Work out details of type.
2115 switch (Ty->getTypeClass()) {
2116#define TYPE(Class, Base)
2117#define ABSTRACT_TYPE(Class, Base)
2118#define NON_CANONICAL_TYPE(Class, Base)
2119#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2120#include "clang/AST/TypeNodes.def"
2121 llvm_unreachable("Dependent types cannot show up in debug information");
2122
2123 case Type::ExtVector:
2124 case Type::Vector:
2125 return CreateType(cast<VectorType>(Ty), Unit);
2126 case Type::ObjCObjectPointer:
2127 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2128 case Type::ObjCObject:
2129 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2130 case Type::ObjCInterface:
2131 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2132 case Type::Builtin:
2133 return CreateType(cast<BuiltinType>(Ty));
2134 case Type::Complex:
2135 return CreateType(cast<ComplexType>(Ty));
2136 case Type::Pointer:
2137 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002138 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002139 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002140 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002141 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002142 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 case Type::BlockPointer:
2144 return CreateType(cast<BlockPointerType>(Ty), Unit);
2145 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002146 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002148 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002150 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 case Type::FunctionProto:
2152 case Type::FunctionNoProto:
2153 return CreateType(cast<FunctionType>(Ty), Unit);
2154 case Type::ConstantArray:
2155 case Type::VariableArray:
2156 case Type::IncompleteArray:
2157 return CreateType(cast<ArrayType>(Ty), Unit);
2158
2159 case Type::LValueReference:
2160 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2161 case Type::RValueReference:
2162 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2163
2164 case Type::MemberPointer:
2165 return CreateType(cast<MemberPointerType>(Ty), Unit);
2166
2167 case Type::Atomic:
2168 return CreateType(cast<AtomicType>(Ty), Unit);
2169
Guy Benyei11169dd2012-12-18 14:30:41 +00002170 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002171 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2172
David Blaikie42edade2014-11-11 20:44:45 +00002173 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002174 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 case Type::Elaborated:
2176 case Type::Paren:
2177 case Type::SubstTemplateTypeParm:
2178 case Type::TypeOfExpr:
2179 case Type::TypeOf:
2180 case Type::Decltype:
2181 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002182 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002183 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002185
David Blaikie42edade2014-11-11 20:44:45 +00002186 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002187}
2188
2189/// getOrCreateLimitedType - Get the type from the cache or create a new
2190/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002191llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002192 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002193 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194
David Blaikie8d5e1282013-08-20 21:03:29 +00002195 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002196
2197 // We may have cached a forward decl when we could have created
2198 // a non-forward decl. Go ahead and create a non-forward decl
2199 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002200 if (T && !T.isForwardDecl())
2201 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202
2203 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002204 llvm::DICompositeType Res = CreateLimitedType(Ty);
2205
2206 // Propagate members from the declaration to the definition
2207 // CreateType(const RecordType*) will overwrite this with the members in the
2208 // correct order if the full type is needed.
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002209 DBuilder.replaceArrays(Res, T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002210
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002212 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 return Res;
2214}
2215
2216// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002217llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002219
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 // Get overall information about the record type for the debug info.
2221 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2222 unsigned Line = getLineNumber(RD->getLocation());
2223 StringRef RDName = getClassName(RD);
2224
Eric Christopher07429ff2013-10-15 21:22:34 +00002225 llvm::DIDescriptor RDContext =
2226 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
David Blaikied2785892013-08-18 17:36:19 +00002228 // If we ended up creating the type during the context chain construction,
2229 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002230 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2231 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002232 return T;
David Blaikied2785892013-08-18 17:36:19 +00002233
Adrian Prantl381e7552014-02-04 21:29:50 +00002234 // If this is just a forward or incomplete declaration, construct an
2235 // appropriately marked node and just return it.
2236 const RecordDecl *D = RD->getDefinition();
2237 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002238 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002239
2240 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2241 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002242 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002243
Manman Rene0064d82013-08-29 23:19:58 +00002244 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2245
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002247 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2248 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002249 else if (RD->isClass()) {
2250 // FIXME: This could be a struct type giving a default visibility different
2251 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002252 RealDecl = DBuilder.createClassType(
2253 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2254 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002256 RealDecl = DBuilder.createStructType(
2257 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2258 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002259
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002260 RegionMap[Ty->getDecl()].reset(RealDecl);
2261 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002262
David Blaikieadfbf992013-08-18 16:55:33 +00002263 if (const ClassTemplateSpecializationDecl *TSpecial =
2264 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002265 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2266 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002267 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002268}
2269
David Blaikieadfbf992013-08-18 16:55:33 +00002270void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2271 llvm::DICompositeType RealDecl) {
2272 // A class's primary base or the class itself contains the vtable.
2273 llvm::DICompositeType ContainingType;
2274 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2275 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002276 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002277 while (1) {
2278 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2279 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2280 if (PBT && !BRL.isPrimaryBaseVirtual())
2281 PBase = PBT;
2282 else
2283 break;
2284 }
2285 ContainingType = llvm::DICompositeType(
2286 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2287 getOrCreateFile(RD->getLocation())));
2288 } else if (RD->isDynamicClass())
2289 ContainingType = RealDecl;
2290
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002291 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002292}
2293
Guy Benyei11169dd2012-12-18 14:30:41 +00002294/// CreateMemberType - Create new member and increase Offset by FType's size.
2295llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002296 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2298 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2299 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002300 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2301 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002302 *Offset += FieldSize;
2303 return Ty;
2304}
2305
Frederic Riss9db79f12014-11-18 03:40:46 +00002306void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2307 llvm::DIFile Unit,
2308 StringRef &Name, StringRef &LinkageName,
2309 llvm::DIDescriptor &FDContext,
2310 llvm::DIArray &TParamsArray,
2311 unsigned &Flags) {
2312 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2313 Name = getFunctionName(FD);
2314 // Use mangled name as linkage name for C/C++ functions.
2315 if (FD->hasPrototype()) {
2316 LinkageName = CGM.getMangledName(GD);
2317 Flags |= llvm::DIDescriptor::FlagPrototyped;
2318 }
2319 // No need to replicate the linkage name if it isn't different from the
2320 // subprogram name, no need to have it at all unless coverage is enabled or
2321 // debug is set to more than just line tables.
2322 if (LinkageName == Name ||
2323 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2324 !CGM.getCodeGenOpts().EmitGcovNotes &&
2325 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2326 LinkageName = StringRef();
2327
2328 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2329 if (const NamespaceDecl *NSDecl =
2330 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2331 FDContext = getOrCreateNameSpace(NSDecl);
2332 else if (const RecordDecl *RDecl =
2333 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2334 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2335 // Collect template parameters.
2336 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2337 }
2338}
2339
2340void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2341 unsigned &LineNo, QualType &T,
2342 StringRef &Name, StringRef &LinkageName,
2343 llvm::DIDescriptor &VDContext) {
2344 Unit = getOrCreateFile(VD->getLocation());
2345 LineNo = getLineNumber(VD->getLocation());
2346
2347 setLocation(VD->getLocation());
2348
2349 T = VD->getType();
2350 if (T->isIncompleteArrayType()) {
2351 // CodeGen turns int[] into int[1] so we'll do the same here.
2352 llvm::APInt ConstVal(32, 1);
2353 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2354
2355 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2356 ArrayType::Normal, 0);
2357 }
2358
2359 Name = VD->getName();
2360 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2361 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2362 LinkageName = CGM.getMangledName(VD);
2363 if (LinkageName == Name)
2364 LinkageName = StringRef();
2365
2366 // Since we emit declarations (DW_AT_members) for static members, place the
2367 // definition of those static members in the namespace they were declared in
2368 // in the source code (the lexical decl context).
2369 // FIXME: Generalize this for even non-member global variables where the
2370 // declaration and definition may have different lexical decl contexts, once
2371 // we have support for emitting declarations of (non-member) global variables.
2372 VDContext = getContextDescriptor(
2373 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2374 : VD->getDeclContext()));
2375}
2376
Frederic Rissd253ed62014-11-18 03:40:51 +00002377llvm::DISubprogram
2378CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2379 llvm::DIArray TParamsArray;
2380 StringRef Name, LinkageName;
2381 unsigned Flags = 0;
2382 SourceLocation Loc = FD->getLocation();
2383 llvm::DIFile Unit = getOrCreateFile(Loc);
2384 llvm::DIDescriptor DContext(Unit);
2385 unsigned Line = getLineNumber(Loc);
2386
2387 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2388 TParamsArray, Flags);
2389 // Build function type.
2390 SmallVector<QualType, 16> ArgTypes;
2391 for (const ParmVarDecl *Parm: FD->parameters())
2392 ArgTypes.push_back(Parm->getType());
2393 QualType FnType =
2394 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2395 FunctionProtoType::ExtProtoInfo());
2396 llvm::DISubprogram SP =
2397 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2398 getOrCreateFunctionType(FD, FnType, Unit),
2399 !FD->isExternallyVisible(),
2400 false /*declaration*/, 0, Flags,
2401 CGM.getLangOpts().Optimize, nullptr,
2402 TParamsArray, getFunctionDeclaration(FD));
2403 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002404 FwdDeclReplaceMap.emplace_back(
2405 std::piecewise_construct, std::make_tuple(CanonDecl),
2406 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002407 return SP;
2408}
2409
2410llvm::DIGlobalVariable
2411CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2412 QualType T;
2413 StringRef Name, LinkageName;
2414 SourceLocation Loc = VD->getLocation();
2415 llvm::DIFile Unit = getOrCreateFile(Loc);
2416 llvm::DIDescriptor DContext(Unit);
2417 unsigned Line = getLineNumber(Loc);
2418
2419 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2420 llvm::DIGlobalVariable GV =
2421 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2422 Line, getOrCreateType(T, Unit),
2423 !VD->isExternallyVisible(),
2424 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002425 FwdDeclReplaceMap.emplace_back(
2426 std::piecewise_construct,
2427 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2428 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002429 return GV;
2430}
2431
Frederic Riss442293e2014-11-06 21:12:06 +00002432llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002433 // We only need a declaration (not a definition) of the type - so use whatever
2434 // we would otherwise do to get a type for a pointee. (forward declarations in
2435 // limited debug info, full definitions (if the type definition is available)
2436 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002437 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2438 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002439 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002440 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002441
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002442 if (I != DeclCache.end())
2443 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002444
2445 // No definition for now. Emit a forward definition that might be
2446 // merged with a potential upcoming definition.
2447 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2448 return getFunctionForwardDeclaration(FD);
2449 else if (const auto *VD = dyn_cast<VarDecl>(D))
2450 return getGlobalVariableForwardDeclaration(VD);
2451
2452 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002453}
2454
Guy Benyei11169dd2012-12-18 14:30:41 +00002455/// getFunctionDeclaration - Return debug info descriptor to describe method
2456/// declaration for the given method definition.
2457llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002458 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002459 return llvm::DISubprogram();
2460
Guy Benyei11169dd2012-12-18 14:30:41 +00002461 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002462 if (!FD)
2463 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002464
2465 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002466 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002467
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002468 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002469 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002470 if (const CXXMethodDecl *MD =
2471 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002472 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002473 llvm::DISubprogram SP =
2474 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002475 return SP;
2476 }
2477 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002479 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002480 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 return SP;
2482 }
2483
Aaron Ballman86c93902014-03-06 23:45:36 +00002484 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002485 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002486 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002487 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002488 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 return SP;
2490 }
2491 }
2492 return llvm::DISubprogram();
2493}
2494
2495// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2496// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002497llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2498 QualType FnType,
2499 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002500 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002501 // Create fake but valid subroutine type. Otherwise
2502 // llvm::DISubprogram::Verify() would return false, and
2503 // subprogram DIE will miss DW_AT_decl_file and
2504 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002505 return DBuilder.createSubroutineType(F,
2506 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002507
2508 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2509 return getOrCreateMethodType(Method, F);
2510 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2511 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002512 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002513
2514 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002515 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002516
2517 // Replace the instancetype keyword with the actual type.
2518 if (ResultTy == CGM.getContext().getObjCInstanceType())
2519 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002520 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002521
Adrian Prantl7bec9032013-05-10 21:08:31 +00002522 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002524 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2525 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2526 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 // "_cmd" pointer is always second argument.
2528 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2529 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2530 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002531 for (const auto *PI : OMethod->params())
2532 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002533 // Variadic methods need a special marker at the end of the type list.
2534 if (OMethod->isVariadic())
2535 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002536
Manman Ren67f005e2014-07-28 22:24:34 +00002537 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002538 return DBuilder.createSubroutineType(F, EltTypeArray);
2539 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002540
Adrian Prantl800faef2014-02-25 23:42:18 +00002541 // Handle variadic function types; they need an additional
2542 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002543 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2544 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002545 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002546 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2547 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2548 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2549 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2550 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002551 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002552 return DBuilder.createSubroutineType(F, EltTypeArray);
2553 }
2554
David Blaikie469f0792013-05-22 23:22:42 +00002555 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002556}
2557
2558/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002559void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2560 SourceLocation ScopeLoc, QualType FnType,
2561 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
2563 StringRef Name;
2564 StringRef LinkageName;
2565
2566 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2567
2568 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002569 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002570
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 unsigned Flags = 0;
2572 llvm::DIFile Unit = getOrCreateFile(Loc);
2573 llvm::DIDescriptor FDContext(Unit);
2574 llvm::DIArray TParamsArray;
2575 if (!HasDecl) {
2576 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002577 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2579 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002580 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002582 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2584 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002585 LexicalBlockStack.emplace_back(SPN);
2586 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 return;
2588 }
2589 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002590 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2591 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2593 Name = getObjCMethodName(OMD);
2594 Flags |= llvm::DIDescriptor::FlagPrototyped;
2595 } else {
2596 // Use llvm function name.
2597 Name = Fn->getName();
2598 Flags |= llvm::DIDescriptor::FlagPrototyped;
2599 }
2600 if (!Name.empty() && Name[0] == '\01')
2601 Name = Name.substr(1);
2602
Adrian Prantl42d71b92014-04-10 23:21:53 +00002603 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002605 // Artificial functions without a location should not silently reuse CurLoc.
2606 if (Loc.isInvalid())
2607 CurLoc = SourceLocation();
2608 }
2609 unsigned LineNo = getLineNumber(Loc);
2610 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002611
Eric Christopher8018e412014-03-27 18:50:35 +00002612 // FIXME: The function declaration we're constructing here is mostly reusing
2613 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2614 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2615 // all subprograms instead of the actual context since subprogram definitions
2616 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002617 llvm::DISubprogram SP = DBuilder.createFunction(
2618 FDContext, Name, LinkageName, Unit, LineNo,
2619 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2620 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2621 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002622 // We might get here with a VarDecl in the case we're generating
2623 // code for the initialization of globals. Do not record these decls
2624 // as they will overwrite the actual VarDecl Decl in the cache.
2625 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002626 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002627
Adrian Prantlbebb8932014-03-21 21:01:58 +00002628 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002630 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002631
Guy Benyei11169dd2012-12-18 14:30:41 +00002632 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002633 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002634}
2635
2636/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002637/// information in the source file. If the location is invalid, the
2638/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002639void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002640 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 // Update our current location
2642 setLocation(Loc);
2643
Eric Christophere7b87e52014-10-26 23:40:33 +00002644 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2645 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002646
2647 // Don't bother if things are the same as last time.
2648 SourceManager &SM = CGM.getContext().getSourceManager();
2649 if (CurLoc == PrevLoc ||
2650 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2651 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002652 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2653 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002654 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002655 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002656
Guy Benyei11169dd2012-12-18 14:30:41 +00002657 // Update last state.
2658 PrevLoc = CurLoc;
2659
Adrian Prantle83b1302014-01-07 22:05:52 +00002660 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002661 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2662 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002663}
2664
2665/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2666/// the stack.
2667void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002668 llvm::MDNode *Back = nullptr;
2669 if (!LexicalBlockStack.empty())
2670 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002671 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002672 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2673 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002675 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002676}
2677
2678/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2679/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002680void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2681 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 // Set our current location.
2683 setLocation(Loc);
2684
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002686 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2687 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002688
2689 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2690 return;
2691
2692 // Create a new lexical block and push it on the stack.
2693 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002694}
2695
2696/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2697/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002698void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2699 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2701
2702 // Provide an entry in the line table for the end of the block.
2703 EmitLocation(Builder, Loc);
2704
David Blaikie60a877b2014-10-22 19:34:33 +00002705 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2706 return;
2707
Guy Benyei11169dd2012-12-18 14:30:41 +00002708 LexicalBlockStack.pop_back();
2709}
2710
2711/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2712void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2713 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2714 unsigned RCount = FnBeginRegionCount.back();
2715 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2716
2717 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002718 while (LexicalBlockStack.size() != RCount) {
2719 // Provide an entry in the line table for the end of the block.
2720 EmitLocation(Builder, CurLoc);
2721 LexicalBlockStack.pop_back();
2722 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 FnBeginRegionCount.pop_back();
2724}
2725
Eric Christopherb2a008c2013-05-16 00:45:12 +00002726// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002727// See BuildByRefType.
2728llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2729 uint64_t *XOffset) {
2730
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002731 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002732 QualType FType;
2733 uint64_t FieldSize, FieldOffset;
2734 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002735
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002737 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002738
2739 FieldOffset = 0;
2740 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2741 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2742 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2743 FType = CGM.getContext().IntTy;
2744 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2745 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2746
2747 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2748 if (HasCopyAndDispose) {
2749 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002750 EltTys.push_back(
2751 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2752 EltTys.push_back(
2753 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 }
2755 bool HasByrefExtendedLayout;
2756 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002757 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2758 HasByrefExtendedLayout) &&
2759 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002760 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002761 EltTys.push_back(
2762 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002763 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002764
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2766 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002767 CGM.getTarget().getPointerAlign(0))) {
2768 CharUnits FieldOffsetInBytes =
2769 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2770 CharUnits AlignedOffsetInBytes =
2771 FieldOffsetInBytes.RoundUpToAlignment(Align);
2772 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002773
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 if (NumPaddingBytes.isPositive()) {
2775 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2776 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2777 pad, ArrayType::Normal, 0);
2778 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2779 }
2780 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002781
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002783 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 FieldSize = CGM.getContext().getTypeSize(FType);
2785 FieldAlign = CGM.getContext().toBits(Align);
2786
Eric Christopherb2a008c2013-05-16 00:45:12 +00002787 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002788 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2789 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 EltTys.push_back(FieldTy);
2791 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002792
Guy Benyei11169dd2012-12-18 14:30:41 +00002793 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002794
Guy Benyei11169dd2012-12-18 14:30:41 +00002795 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002796
Guy Benyei11169dd2012-12-18 14:30:41 +00002797 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002798 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002799}
2800
2801/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002802void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002803 llvm::Value *Storage, unsigned ArgNo,
2804 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002805 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002806 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2807
David Blaikie7fceebf2013-08-19 03:37:48 +00002808 bool Unwritten =
2809 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2810 cast<Decl>(VD->getDeclContext())->isImplicit());
2811 llvm::DIFile Unit;
2812 if (!Unwritten)
2813 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 llvm::DIType Ty;
2815 uint64_t XOffset = 0;
2816 if (VD->hasAttr<BlocksAttr>())
2817 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002818 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 Ty = getOrCreateType(VD->getType(), Unit);
2820
2821 // If there is no debug info for this type then do not emit debug info
2822 // for this variable.
2823 if (!Ty)
2824 return;
2825
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002827 unsigned Line = 0;
2828 unsigned Column = 0;
2829 if (!Unwritten) {
2830 Line = getLineNumber(VD->getLocation());
2831 Column = getColumnNumber(VD->getLocation());
2832 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 unsigned Flags = 0;
2834 if (VD->isImplicit())
2835 Flags |= llvm::DIDescriptor::FlagArtificial;
2836 // If this is the first argument and it is implicit then
2837 // give it an object pointer flag.
2838 // FIXME: There has to be a better way to do this, but for static
2839 // functions there won't be an implicit param at arg1 and
2840 // otherwise it is 'self' or 'this'.
2841 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2842 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002843 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002844 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2845 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002846 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002847
2848 llvm::MDNode *Scope = LexicalBlockStack.back();
2849
2850 StringRef Name = VD->getName();
2851 if (!Name.empty()) {
2852 if (VD->hasAttr<BlocksAttr>()) {
2853 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002854 SmallVector<int64_t, 9> addr;
2855 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 // offset of __forwarding field
2857 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002858 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002859 addr.push_back(offset.getQuantity());
2860 addr.push_back(llvm::dwarf::DW_OP_deref);
2861 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002862 // offset of x field
2863 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002864 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002865
2866 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002867 llvm::DIVariable D = DBuilder.createLocalVariable(
2868 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002869
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 // Insert an llvm.dbg.declare into the current block.
2871 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002872 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2873 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002874 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2875 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002876 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002877 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002878 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2879 // If VD is an anonymous union then Storage represents value for
2880 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002882 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002883 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2885 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002886
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 // Ignore unnamed fields. Do not ignore unnamed records.
2888 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2889 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002890
Guy Benyei11169dd2012-12-18 14:30:41 +00002891 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002892 llvm::DIVariable D = DBuilder.createLocalVariable(
2893 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2894 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +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(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002899 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2900 }
David Blaikie219c7d92013-01-05 20:03:07 +00002901 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 }
2903 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002904
2905 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002906 llvm::DIVariable D = DBuilder.createLocalVariable(
2907 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2908 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002909
2910 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002911 llvm::Instruction *Call = DBuilder.insertDeclare(
2912 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002913 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002914}
2915
2916void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2917 llvm::Value *Storage,
2918 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002919 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002920 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2921}
2922
Adrian Prantlde17db32013-03-29 19:20:29 +00002923/// Look up the completed type for a self pointer in the TypeCache and
2924/// create a copy of it with the ObjectPointer and Artificial flags
2925/// set. If the type is not cached, a new one is created. This should
2926/// never happen though, since creating a type for the implicit self
2927/// argument implies that we already parsed the interface definition
2928/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002929llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2930 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002931 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002932 if (CachedTy)
2933 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002934 return DBuilder.createObjectPointerType(Ty);
2935}
2936
Eric Christophere7b87e52014-10-26 23:40:33 +00002937void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2938 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002939 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002940 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002942
Craig Topper8a13c412014-05-21 05:09:00 +00002943 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002944 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002945
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002947
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 uint64_t XOffset = 0;
2949 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2950 llvm::DIType Ty;
2951 if (isByRef)
2952 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002953 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002954 Ty = getOrCreateType(VD->getType(), Unit);
2955
2956 // Self is passed along as an implicit non-arg variable in a
2957 // block. Mark it as the object pointer.
2958 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002959 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002960
2961 // Get location information.
2962 unsigned Line = getLineNumber(VD->getLocation());
2963 unsigned Column = getColumnNumber(VD->getLocation());
2964
2965 const llvm::DataLayout &target = CGM.getDataLayout();
2966
2967 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002968 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2970
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002971 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002972 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002973 addr.push_back(llvm::dwarf::DW_OP_deref);
2974 addr.push_back(llvm::dwarf::DW_OP_plus);
2975 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002977 addr.push_back(llvm::dwarf::DW_OP_deref);
2978 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002980 offset =
2981 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002982 addr.push_back(offset.getQuantity());
2983 addr.push_back(llvm::dwarf::DW_OP_deref);
2984 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 // offset of x field
2986 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002987 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002988 }
2989
2990 // Create the descriptor for the variable.
2991 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002992 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2993 llvm::DIDescriptor(LexicalBlockStack.back()),
2994 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002995
Guy Benyei11169dd2012-12-18 14:30:41 +00002996 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002997 llvm::Instruction *Call = InsertPoint ?
2998 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2999 InsertPoint)
3000 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
3001 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00003002 Call->setDebugLoc(
3003 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00003004}
3005
3006/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3007/// variable declaration.
3008void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3009 unsigned ArgNo,
3010 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003011 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3013}
3014
3015namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003016struct BlockLayoutChunk {
3017 uint64_t OffsetInBits;
3018 const BlockDecl::Capture *Capture;
3019};
3020bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3021 return l.OffsetInBits < r.OffsetInBits;
3022}
Guy Benyei11169dd2012-12-18 14:30:41 +00003023}
3024
3025void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003026 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003027 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003028 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003030 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 ASTContext &C = CGM.getContext();
3032 const BlockDecl *blockDecl = block.getBlockDecl();
3033
3034 // Collect some general information about the block's location.
3035 SourceLocation loc = blockDecl->getCaretLocation();
3036 llvm::DIFile tunit = getOrCreateFile(loc);
3037 unsigned line = getLineNumber(loc);
3038 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003039
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 // Build the debug-info type for the block literal.
3041 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3042
3043 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003044 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003045
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003046 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003047 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3048 blockLayout->getElementOffsetInBits(0),
3049 tunit, tunit));
3050 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3051 blockLayout->getElementOffsetInBits(1),
3052 tunit, tunit));
3053 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3054 blockLayout->getElementOffsetInBits(2),
3055 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003056 auto *FnTy = block.getBlockExpr()->getFunctionType();
3057 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3058 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 blockLayout->getElementOffsetInBits(3),
3060 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003061 fields.push_back(createFieldType(
3062 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3063 ? C.getBlockDescriptorExtendedType()
3064 : C.getBlockDescriptorType()),
3065 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003066
3067 // We want to sort the captures by offset, not because DWARF
3068 // requires this, but because we're paranoid about debuggers.
3069 SmallVector<BlockLayoutChunk, 8> chunks;
3070
3071 // 'this' capture.
3072 if (blockDecl->capturesCXXThis()) {
3073 BlockLayoutChunk chunk;
3074 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003075 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003076 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003077 chunks.push_back(chunk);
3078 }
3079
3080 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003081 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 const VarDecl *variable = capture.getVariable();
3083 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3084
3085 // Ignore constant captures.
3086 if (captureInfo.isConstant())
3087 continue;
3088
3089 BlockLayoutChunk chunk;
3090 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003091 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003092 chunk.Capture = &capture;
3093 chunks.push_back(chunk);
3094 }
3095
3096 // Sort by offset.
3097 llvm::array_pod_sort(chunks.begin(), chunks.end());
3098
Eric Christophere7b87e52014-10-26 23:40:33 +00003099 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3100 e = chunks.end();
3101 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 uint64_t offsetInBits = i->OffsetInBits;
3103 const BlockDecl::Capture *capture = i->Capture;
3104
3105 // If we have a null capture, this must be the C++ 'this' capture.
3106 if (!capture) {
3107 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003108 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003109 QualType type = method->getThisType(C);
3110
3111 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3112 offsetInBits, tunit, tunit));
3113 continue;
3114 }
3115
3116 const VarDecl *variable = capture->getVariable();
3117 StringRef name = variable->getName();
3118
3119 llvm::DIType fieldType;
3120 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003121 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003122
3123 // FIXME: this creates a second copy of this type!
3124 uint64_t xoffset;
3125 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003126 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3127 fieldType =
3128 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3129 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003131 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3132 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 }
3134 fields.push_back(fieldType);
3135 }
3136
3137 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003138 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3139 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003140
3141 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3142
3143 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003144 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3145 CGM.getContext().toBits(block.BlockSize),
3146 CGM.getContext().toBits(block.BlockAlign), 0,
3147 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3149
3150 // Get overall information about the block.
3151 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3152 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003153
3154 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003155 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3156 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3157 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3158 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003159
Adrian Prantl616bef42013-03-14 21:52:59 +00003160 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003161 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3163 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3164 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003165 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3166 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003167
Adrian Prantl616bef42013-03-14 21:52:59 +00003168 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003169 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3170 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003171 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003172}
3173
David Blaikie6943dea2013-08-20 01:28:15 +00003174/// If D is an out-of-class definition of a static data member of a class, find
3175/// its corresponding in-class declaration.
3176llvm::DIDerivedType
3177CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3178 if (!D->isStaticDataMember())
3179 return llvm::DIDerivedType();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003180 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003181 if (MI != StaticDataMemberCache.end()) {
3182 assert(MI->second && "Static data member declaration should still exist");
3183 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003184 }
David Blaikiece763042013-08-20 21:49:21 +00003185
3186 // If the member wasn't found in the cache, lazily construct and add it to the
3187 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003188 auto DC = D->getDeclContext();
3189 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3190 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003191}
3192
Eric Christophercab9fae2014-04-10 05:20:00 +00003193/// Recursively collect all of the member fields of a global anonymous decl and
3194/// create static variables for them. The first time this is called it needs
3195/// to be on a union and then from there we can have additional unnamed fields.
3196llvm::DIGlobalVariable
3197CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3198 unsigned LineNo, StringRef LinkageName,
3199 llvm::GlobalVariable *Var,
3200 llvm::DIDescriptor DContext) {
3201 llvm::DIGlobalVariable GV;
3202
3203 for (const auto *Field : RD->fields()) {
3204 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3205 StringRef FieldName = Field->getName();
3206
3207 // Ignore unnamed fields, but recurse into anonymous records.
3208 if (FieldName.empty()) {
3209 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3210 if (RT)
3211 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3212 Var, DContext);
3213 continue;
3214 }
3215 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003216 GV = DBuilder.createGlobalVariable(
3217 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3218 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003219 }
3220 return GV;
3221}
3222
Guy Benyei11169dd2012-12-18 14:30:41 +00003223/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003224void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003226 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003227 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003228 llvm::DIFile Unit;
3229 llvm::DIDescriptor DContext;
3230 unsigned LineNo;
3231 StringRef DeclName, LinkageName;
3232 QualType T;
3233 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003234
3235 // Attempt to store one global variable for the declaration - even if we
3236 // emit a lot of fields.
3237 llvm::DIGlobalVariable GV;
3238
3239 // If this is an anonymous union then we'll want to emit a global
3240 // variable for each member of the anonymous union so that it's possible
3241 // to find the name of any field in the union.
3242 if (T->isUnionType() && DeclName.empty()) {
3243 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003244 assert(RD->isAnonymousStructOrUnion() &&
3245 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003246 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3247 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003248 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003249 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3250 Var->hasInternalLinkage(), Var,
3251 getOrCreateStaticDataMemberDeclarationOrNull(D));
3252 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003253 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003254}
3255
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003256/// EmitGlobalVariable - Emit global variable's debug info.
3257void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3258 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003259 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003260 // Create the descriptor for the variable.
3261 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3262 StringRef Name = VD->getName();
3263 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3264 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3265 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3266 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3267 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3268 }
3269 // Do not use DIGlobalVariable for enums.
3270 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3271 return;
David Blaikiea15565562014-04-04 20:56:17 +00003272 // Do not emit separate definitions for function local const/statics.
3273 if (isa<FunctionDecl>(VD->getDeclContext()))
3274 return;
David Blaikiebb113912014-04-05 07:23:17 +00003275 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003276 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003277 if (VarD->isStaticDataMember()) {
3278 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3279 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003280 // Ensure that the type is retained even though it's otherwise unreferenced.
3281 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003282 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003283 return;
3284 }
3285
David Blaikieaf080852014-11-21 00:20:58 +00003286 llvm::DIDescriptor DContext =
3287 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3288
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003289 auto &GV = DeclCache[VD];
3290 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003291 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003292 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003293 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003294 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003295}
3296
3297llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3298 if (!LexicalBlockStack.empty())
3299 return llvm::DIScope(LexicalBlockStack.back());
3300 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003301}
3302
David Blaikie9f88fe82013-04-22 06:13:21 +00003303void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003304 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3305 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003306 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003307 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3308 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003309 getLineNumber(UD.getLocation()));
3310}
3311
David Blaikiebd483762013-05-20 04:58:53 +00003312void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3313 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3314 return;
3315 assert(UD.shadow_size() &&
3316 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3317 // Emitting one decl is sufficient - debuggers can detect that this is an
3318 // overloaded name & provide lookup for all the overloads.
3319 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003320 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003321 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003322 DBuilder.createImportedDeclaration(
3323 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3324 getLineNumber(USD.getLocation()));
3325}
3326
David Blaikief121b932013-05-20 22:50:41 +00003327llvm::DIImportedEntity
3328CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3329 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003330 return llvm::DIImportedEntity(nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003331 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003332 if (VH)
3333 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003334 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003335 if (const NamespaceAliasDecl *Underlying =
3336 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3337 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003338 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003339 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3340 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3341 NA.getName());
3342 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003343 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003344 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3345 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3346 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003347 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003348 return R;
3349}
3350
Guy Benyei11169dd2012-12-18 14:30:41 +00003351/// getOrCreateNamesSpace - Return namespace descriptor for the given
3352/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003353llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003354CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003355 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003356 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003357 if (I != NameSpaceCache.end())
3358 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003359
Guy Benyei11169dd2012-12-18 14:30:41 +00003360 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3361 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003362 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3364 llvm::DINameSpace NS =
3365 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003366 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 return NS;
3368}
3369
3370void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003371 // Creating types might create further types - invalidating the current
3372 // element and the size(), so don't cache/reference them.
3373 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3374 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3375 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3376 E.Type->getDecl()->getDefinition()
3377 ? CreateTypeDefinition(E.Type, E.Unit)
3378 : E.Decl);
3379 }
3380
David Blaikief427b002014-05-06 03:42:01 +00003381 for (auto p : ReplaceMap) {
3382 assert(p.second);
3383 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003384 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003385
David Blaikief427b002014-05-06 03:42:01 +00003386 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003387 assert(it != TypeCache.end());
3388 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003389
David Blaikief427b002014-05-06 03:42:01 +00003390 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3391 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003392 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003393
Frederic Rissd253ed62014-11-18 03:40:51 +00003394 for (const auto &p : FwdDeclReplaceMap) {
3395 assert(p.second);
3396 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003397 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003398
3399 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003400 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003401 // with ourselves, that will destroy the temporary MDNode and
3402 // replace it with a standard one, avoiding leaking memory.
3403 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003404 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003405 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003406 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003407
Frederic Rissd253ed62014-11-18 03:40:51 +00003408 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003409 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003410 }
3411
Adrian Prantl73409ce2013-03-11 18:33:46 +00003412 // We keep our own list of retained types, because we need to look
3413 // up the final type in the type cache.
3414 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3415 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003416 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003417
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 DBuilder.finalize();
3419}
David Blaikie66088d52014-09-24 17:01:27 +00003420
3421void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3422 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3423 return;
3424 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3425 // Don't ignore in case of explicit cast where it is referenced indirectly.
3426 DBuilder.retainType(DieTy);
3427}