blob: 20603f44afb4d64e472228ae4166ba5d956375f0 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000040#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041using namespace clang;
42using namespace clang::CodeGen;
43
44CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000045 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
46 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000047 CreateCompileUnit();
48}
49
50CGDebugInfo::~CGDebugInfo() {
51 assert(LexicalBlockStack.empty() &&
52 "Region stack mismatch, stack not empty!");
53}
54
Eric Christopher0a1301f2014-02-26 02:49:36 +000055SaveAndRestoreLocation::SaveAndRestoreLocation(CodeGenFunction &CGF,
56 CGBuilderTy &B)
57 : DI(CGF.getDebugInfo()), Builder(B) {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000058 if (DI) {
59 SavedLoc = DI->getLocation();
60 DI->CurLoc = SourceLocation();
Adrian Prantl2e0637f2013-07-18 00:28:02 +000061 }
62}
63
Adrian Prantld1b151e2014-01-17 00:15:10 +000064SaveAndRestoreLocation::~SaveAndRestoreLocation() {
65 if (DI)
66 DI->EmitLocation(Builder, SavedLoc);
67}
68
69NoLocation::NoLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Eric Christophere7b87e52014-10-26 23:40:33 +000070 : SaveAndRestoreLocation(CGF, B) {
Adrian Prantld1b151e2014-01-17 00:15:10 +000071 if (DI)
72 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
73}
74
Adrian Prantl2e0637f2013-07-18 00:28:02 +000075NoLocation::~NoLocation() {
Adrian Prantld1b151e2014-01-17 00:15:10 +000076 if (DI)
Adrian Prantl2e0637f2013-07-18 00:28:02 +000077 assert(Builder.getCurrentDebugLocation().isUnknown());
Adrian Prantl2e0637f2013-07-18 00:28:02 +000078}
79
Adrian Prantlb75016d2013-07-18 01:36:04 +000080ArtificialLocation::ArtificialLocation(CodeGenFunction &CGF, CGBuilderTy &B)
Eric Christophere7b87e52014-10-26 23:40:33 +000081 : SaveAndRestoreLocation(CGF, B) {
Adrian Prantld1b151e2014-01-17 00:15:10 +000082 if (DI)
Adrian Prantl49a78562013-07-24 20:34:39 +000083 Builder.SetCurrentDebugLocation(llvm::DebugLoc());
Adrian Prantl49a78562013-07-24 20:34:39 +000084}
85
86void ArtificialLocation::Emit() {
87 if (DI) {
Adrian Prantl2e0637f2013-07-18 00:28:02 +000088 // Sync the Builder.
89 DI->EmitLocation(Builder, SavedLoc);
90 DI->CurLoc = SourceLocation();
91 // Construct a location that has a valid scope, but no line info.
Adrian Prantl49a78562013-07-24 20:34:39 +000092 assert(!DI->LexicalBlockStack.empty());
93 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
Adrian Prantl2e0637f2013-07-18 00:28:02 +000094 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
95 }
96}
97
Adrian Prantlb75016d2013-07-18 01:36:04 +000098ArtificialLocation::~ArtificialLocation() {
Adrian Prantld1b151e2014-01-17 00:15:10 +000099 if (DI)
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000100 assert(Builder.getCurrentDebugLocation().getLine() == 0);
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000101}
102
Guy Benyei11169dd2012-12-18 14:30:41 +0000103void CGDebugInfo::setLocation(SourceLocation Loc) {
104 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000105 if (Loc.isInvalid())
106 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000107
108 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
109
110 // If we've changed files in the middle of a lexical scope go ahead
111 // and create a new lexical scope with file node if it's different
112 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000113 if (LexicalBlockStack.empty())
114 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000115
116 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +0000117 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000118 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000119
David Blaikieaabde052014-05-14 00:29:00 +0000120 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000121 return;
122
Guy Benyei11169dd2012-12-18 14:30:41 +0000123 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000124 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Eric Christophere7b87e52014-10-26 23:40:33 +0000125 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
126 LBF.getScope(), getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 llvm::MDNode *N = D;
128 LexicalBlockStack.pop_back();
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
Eric Christophere7b87e52014-10-26 23:40:33 +0000425 ObjTy.setArrays(DBuilder.getOrCreateArray(
426 &*DBuilder.createMemberType(ObjTy, "isa", getOrCreateMainFile(), 0,
427 Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 return ObjTy;
429 }
430 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000431 if (!SelTy)
432 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
433 "objc_selector", TheCU,
434 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return SelTy;
436 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000437
438 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000439 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000440 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000441 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442 OCLImage1dArrayDITy);
443 case BuiltinType::OCLImage1dBuffer:
444 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
445 OCLImage1dBufferDITy);
446 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000447 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000448 case BuiltinType::OCLImage2dArray:
449 return getOrCreateStructPtrType("opencl_image2d_array_t",
450 OCLImage2dArrayDITy);
451 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000452 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000453 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return DBuilder.createBasicType(
455 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
456 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000457 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000459
Guy Benyei11169dd2012-12-18 14:30:41 +0000460 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 case BuiltinType::Char_U:
462 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
463 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 case BuiltinType::SChar:
466 Encoding = llvm::dwarf::DW_ATE_signed_char;
467 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000469 case BuiltinType::Char32:
470 Encoding = llvm::dwarf::DW_ATE_UTF;
471 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472 case BuiltinType::UShort:
473 case BuiltinType::UInt:
474 case BuiltinType::UInt128:
475 case BuiltinType::ULong:
476 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000477 case BuiltinType::ULongLong:
478 Encoding = llvm::dwarf::DW_ATE_unsigned;
479 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000480 case BuiltinType::Short:
481 case BuiltinType::Int:
482 case BuiltinType::Int128:
483 case BuiltinType::Long:
484 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000485 case BuiltinType::LongLong:
486 Encoding = llvm::dwarf::DW_ATE_signed;
487 break;
488 case BuiltinType::Bool:
489 Encoding = llvm::dwarf::DW_ATE_boolean;
490 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000491 case BuiltinType::Half:
492 case BuiltinType::Float:
493 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 case BuiltinType::Double:
495 Encoding = llvm::dwarf::DW_ATE_float;
496 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 }
498
499 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000500 case BuiltinType::Long:
501 BTName = "long int";
502 break;
503 case BuiltinType::LongLong:
504 BTName = "long long int";
505 break;
506 case BuiltinType::ULong:
507 BTName = "long unsigned int";
508 break;
509 case BuiltinType::ULongLong:
510 BTName = "long long unsigned int";
511 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000512 default:
513 BTName = BT->getName(CGM.getLangOpts());
514 break;
515 }
516 // Bit size, align and offset of the type.
517 uint64_t Size = CGM.getContext().getTypeSize(BT);
518 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000519 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 return DbgTy;
521}
522
523llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
524 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000525 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 if (Ty->isComplexIntegerType())
527 Encoding = llvm::dwarf::DW_ATE_lo_user;
528
529 uint64_t Size = CGM.getContext().getTypeSize(Ty);
530 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000531 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000532 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000533
534 return DbgTy;
535}
536
537/// CreateCVRType - Get the qualified type from the cache or create
538/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000539llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 QualifierCollector Qc;
541 const Type *T = Qc.strip(Ty);
542
543 // Ignore these qualifiers for now.
544 Qc.removeObjCGCAttr();
545 Qc.removeAddressSpace();
546 Qc.removeObjCLifetime();
547
548 // We will create one Derived type for one qualifier and recurse to handle any
549 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000550 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 if (Qc.hasConst()) {
552 Tag = llvm::dwarf::DW_TAG_const_type;
553 Qc.removeConst();
554 } else if (Qc.hasVolatile()) {
555 Tag = llvm::dwarf::DW_TAG_volatile_type;
556 Qc.removeVolatile();
557 } else if (Qc.hasRestrict()) {
558 Tag = llvm::dwarf::DW_TAG_restrict_type;
559 Qc.removeRestrict();
560 } else {
561 assert(Qc.empty() && "Unknown type qualifier for debug info");
562 return getOrCreateType(QualType(T, 0), Unit);
563 }
564
David Blaikie99dab3b2013-09-04 22:03:57 +0000565 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000566
567 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
568 // CVR derived types.
569 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000570
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 return DbgTy;
572}
573
574llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
575 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000576
577 // The frontend treats 'id' as a typedef to an ObjCObjectType,
578 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
579 // debug info, we want to emit 'id' in both cases.
580 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000581 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000582
Eric Christophere7b87e52014-10-26 23:40:33 +0000583 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
584 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 return DbgTy;
586}
587
Eric Christophere7b87e52014-10-26 23:40:33 +0000588llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000589 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000590 Ty->getPointeeType(), Unit);
591}
592
Manman Rene0064d82013-08-29 23:19:58 +0000593/// In C++ mode, types have linkage, so we can rely on the ODR and
594/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000595static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
596 CodeGenModule &CGM,
597 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000598 SmallString<256> FullName;
599 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
600 // For now, only apply ODR with C++.
601 const TagDecl *TD = Ty->getDecl();
602 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
603 !TD->isExternallyVisible())
604 return FullName;
605 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
606 if (CGM.getTarget().getCXXABI().isMicrosoft())
607 return FullName;
608
609 // TODO: This is using the RTTI name. Is there a better way to get
610 // a unique string for a type?
611 llvm::raw_svector_ostream Out(FullName);
612 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
613 Out.flush();
614 return FullName;
615}
616
Guy Benyei11169dd2012-12-18 14:30:41 +0000617// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000618llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000619CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000620 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000621 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000622 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000623 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000624 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
625 unsigned Line = getLineNumber(RD->getLocation());
626 StringRef RDName = getClassName(RD);
627
Ed Masteda706022014-05-07 12:49:30 +0000628 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000629 if (RD->isStruct() || RD->isInterface())
630 Tag = llvm::dwarf::DW_TAG_structure_type;
631 else if (RD->isUnion())
632 Tag = llvm::dwarf::DW_TAG_union_type;
633 else {
634 assert(RD->isClass());
635 Tag = llvm::dwarf::DW_TAG_class_type;
636 }
637
638 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000639 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000640 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
641 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000642 ReplaceMap.emplace_back(
643 std::piecewise_construct, std::make_tuple(Ty),
644 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000645 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000646}
647
Ed Masteda706022014-05-07 12:49:30 +0000648llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000649 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000650 QualType PointeeTy,
651 llvm::DIFile Unit) {
652 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
653 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000654 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000655
Guy Benyei11169dd2012-12-18 14:30:41 +0000656 // Bit size, align and offset of the type.
657 // Size is always the size of a pointer. We can't use getTypeSize here
658 // because that does not return the correct value for references.
659 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000660 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000661 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
662
David Blaikie99dab3b2013-09-04 22:03:57 +0000663 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
664 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000665}
666
Eric Christopher0fdcb312013-05-16 00:52:20 +0000667llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
668 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000669 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000670 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000671 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
672 TheCU, getOrCreateMainFile(), 0);
673 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
674 Cache = DBuilder.createPointerType(Cache, Size);
675 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000676}
677
Guy Benyei11169dd2012-12-18 14:30:41 +0000678llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
679 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000680 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 return BlockLiteralGeneric;
682
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000683 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000684 llvm::DIType FieldTy;
685 QualType FType;
686 uint64_t FieldSize, FieldOffset;
687 unsigned FieldAlign;
688 llvm::DIArray Elements;
689 llvm::DIType EltTy, DescTy;
690
691 FieldOffset = 0;
692 FType = CGM.getContext().UnsignedLongTy;
693 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
694 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
695
696 Elements = DBuilder.getOrCreateArray(EltTys);
697 EltTys.clear();
698
699 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
700 unsigned LineNo = getLineNumber(CurLoc);
701
Eric Christophere7b87e52014-10-26 23:40:33 +0000702 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
703 FieldOffset, 0, Flags, llvm::DIType(),
704 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000705
706 // Bit size, align and offset of the type.
707 uint64_t Size = CGM.getContext().getTypeSize(Ty);
708
709 DescTy = DBuilder.createPointerType(EltTy, Size);
710
711 FieldOffset = 0;
712 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
713 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
714 FType = CGM.getContext().IntTy;
715 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
716 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000717 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
719
720 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
721 FieldTy = DescTy;
722 FieldSize = CGM.getContext().getTypeSize(Ty);
723 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000724 FieldTy =
725 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
726 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727 EltTys.push_back(FieldTy);
728
729 FieldOffset += FieldSize;
730 Elements = DBuilder.getOrCreateArray(EltTys);
731
Eric Christophere7b87e52014-10-26 23:40:33 +0000732 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
733 LineNo, FieldOffset, 0, Flags,
734 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000735
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
737 return BlockLiteralGeneric;
738}
739
Eric Christophere7b87e52014-10-26 23:40:33 +0000740llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
741 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000742 assert(Ty->isTypeAlias());
743 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000744
745 SmallString<128> NS;
746 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000747 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
748 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000749
750 TemplateSpecializationType::PrintTemplateArgumentList(
751 OS, Ty->getArgs(), Ty->getNumArgs(),
752 CGM.getContext().getPrintingPolicy());
753
Eric Christophere7b87e52014-10-26 23:40:33 +0000754 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
755 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000756
757 SourceLocation Loc = AliasDecl->getLocation();
758 llvm::DIFile File = getOrCreateFile(Loc);
759 unsigned Line = getLineNumber(Loc);
760
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 llvm::DIDescriptor Ctxt =
762 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000763
764 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
765}
766
David Blaikie99dab3b2013-09-04 22:03:57 +0000767llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 // Typedefs are derived from some other type. If we have a typedef of a
769 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000770 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 // We don't set size information, but do specify where the typedef was
772 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000773 SourceLocation Loc = Ty->getDecl()->getLocation();
774 llvm::DIFile File = getOrCreateFile(Loc);
775 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000776 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000777
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000779 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000780
Eric Christophere7b87e52014-10-26 23:40:33 +0000781 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
782 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000783}
784
785llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
786 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000787 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000788
789 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000790 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000791
792 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000793 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000794 if (isa<FunctionNoProtoType>(Ty))
795 EltTys.push_back(DBuilder.createUnspecifiedParameter());
796 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000797 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
798 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000799 if (FPT->isVariadic())
800 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 }
802
Manman Ren67f005e2014-07-28 22:24:34 +0000803 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000804 return DBuilder.createSubroutineType(Unit, EltTypeArray);
805}
806
Adrian Prantl21361fb2014-08-29 22:44:27 +0000807/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
808/// As an optimization, return 0 if the access specifier equals the
809/// default for the containing type.
810static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
811 AccessSpecifier Default = clang::AS_none;
812 if (RD && RD->isClass())
813 Default = clang::AS_private;
814 else if (RD && (RD->isStruct() || RD->isUnion()))
815 Default = clang::AS_public;
816
817 if (Access == Default)
818 return 0;
819
Eric Christophere7b87e52014-10-26 23:40:33 +0000820 switch (Access) {
821 case clang::AS_private:
822 return llvm::DIDescriptor::FlagPrivate;
823 case clang::AS_protected:
824 return llvm::DIDescriptor::FlagProtected;
825 case clang::AS_public:
826 return llvm::DIDescriptor::FlagPublic;
827 case clang::AS_none:
828 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000829 }
830 llvm_unreachable("unexpected access enumerator");
831}
Guy Benyei11169dd2012-12-18 14:30:41 +0000832
Eric Christophere7b87e52014-10-26 23:40:33 +0000833llvm::DIType CGDebugInfo::createFieldType(
834 StringRef name, QualType type, uint64_t sizeInBitsOverride,
835 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
836 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 llvm::DIType debugType = getOrCreateType(type, tunit);
838
839 // Get the location for the field.
840 llvm::DIFile file = getOrCreateFile(loc);
841 unsigned line = getLineNumber(loc);
842
David Majnemer34b57492014-07-30 01:30:47 +0000843 uint64_t SizeInBits = 0;
844 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000846 TypeInfo TI = CGM.getContext().getTypeInfo(type);
847 SizeInBits = TI.Width;
848 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000849
850 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000851 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 }
853
Adrian Prantl21361fb2014-08-29 22:44:27 +0000854 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000855 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
856 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000857}
858
Eric Christopher91a31902013-01-16 01:22:32 +0000859/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000860void CGDebugInfo::CollectRecordLambdaFields(
861 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
862 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000863 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
864 // has the name and the location of the variable so we should iterate over
865 // both concurrently.
866 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
867 RecordDecl::field_iterator Field = CXXDecl->field_begin();
868 unsigned fieldno = 0;
869 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000870 E = CXXDecl->captures_end();
871 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000872 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000873 if (C.capturesVariable()) {
874 VarDecl *V = C.getCapturedVar();
875 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
876 StringRef VName = V->getName();
877 uint64_t SizeInBitsOverride = 0;
878 if (Field->isBitField()) {
879 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
880 assert(SizeInBitsOverride && "found named 0-width bitfield");
881 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000882 llvm::DIType fieldType = createFieldType(
883 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
884 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
885 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000886 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000887 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000888 // TODO: Need to handle 'this' in some way by probably renaming the
889 // this of the lambda class and having a field member of 'this' or
890 // by using AT_object_pointer for the function and having that be
891 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000892 FieldDecl *f = *Field;
893 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
894 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000895 llvm::DIType fieldType = createFieldType(
896 "this", type, 0, f->getLocation(), f->getAccess(),
897 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000898
899 elements.push_back(fieldType);
900 }
901 }
902}
903
David Blaikie6943dea2013-08-20 01:28:15 +0000904/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000905llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
906 llvm::DIType RecordTy,
907 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000908 // Create the descriptor for the static variable, with or without
909 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000910 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000911 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
912 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
913
Eric Christopher91a31902013-01-16 01:22:32 +0000914 unsigned LineNumber = getLineNumber(Var->getLocation());
915 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000916 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000917 if (Var->getInit()) {
918 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000919 if (Value) {
920 if (Value->isInt())
921 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
922 if (Value->isFloat())
923 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
924 }
Eric Christopher91a31902013-01-16 01:22:32 +0000925 }
926
Adrian Prantl21361fb2014-08-29 22:44:27 +0000927 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000928 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
929 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000930 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000931 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000932}
933
934/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000935void CGDebugInfo::CollectRecordNormalField(
936 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000937 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000938 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000939 StringRef name = field->getName();
940 QualType type = field->getType();
941
942 // Ignore unnamed fields unless they're anonymous structs/unions.
943 if (name.empty() && !type->isRecordType())
944 return;
945
946 uint64_t SizeInBitsOverride = 0;
947 if (field->isBitField()) {
948 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
949 assert(SizeInBitsOverride && "found named 0-width bitfield");
950 }
951
Eric Christophere7b87e52014-10-26 23:40:33 +0000952 llvm::DIType fieldType =
953 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
954 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000955
956 elements.push_back(fieldType);
957}
958
Guy Benyei11169dd2012-12-18 14:30:41 +0000959/// CollectRecordFields - A helper function to collect debug info for
960/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000961void CGDebugInfo::CollectRecordFields(
962 const RecordDecl *record, llvm::DIFile tunit,
963 SmallVectorImpl<llvm::Metadata *> &elements,
964 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
966
Eric Christopher91a31902013-01-16 01:22:32 +0000967 if (CXXDecl && CXXDecl->isLambda())
968 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
969 else {
970 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000971
Eric Christopher91a31902013-01-16 01:22:32 +0000972 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000973 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000974
Eric Christopher91a31902013-01-16 01:22:32 +0000975 // Static and non-static members should appear in the same order as
976 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000977 for (const auto *I : record->decls())
978 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000979 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000980 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000981 if (MI != StaticDataMemberCache.end()) {
982 assert(MI->second &&
983 "Static data member declaration should still exist");
984 elements.push_back(
985 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000986 } else {
987 auto Field = CreateRecordStaticField(V, RecordTy, record);
988 elements.push_back(Field);
989 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000990 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000991 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
992 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000993
994 // Bump field number for next field.
995 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000997 }
998}
999
1000/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1001/// function type is not updated to include implicit "this" pointer. Use this
1002/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001003llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001004CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1005 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001006 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001007 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001008 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001009 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1010 Func, Unit);
1011}
David Blaikie2aaf0652013-01-07 22:24:59 +00001012
David Blaikie469f0792013-05-22 23:22:42 +00001013llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001014 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001015 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001016 llvm::DITypeArray Args = llvm::DISubroutineType(
1017 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001018 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001019
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001020 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
1022 // First element is always return type. For 'void' functions it is NULL.
1023 Elts.push_back(Args.getElement(0));
1024
David Blaikie2aaf0652013-01-07 22:24:59 +00001025 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001026 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1028 // Create pointer type directly in this case.
1029 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1030 QualType PointeeTy = ThisPtrTy->getPointeeType();
1031 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001032 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001033 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1034 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001035 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001036 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001037 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001038 // TODO: This and the artificial type below are misleading, the
1039 // types aren't artificial the argument is, but the current
1040 // metadata doesn't represent that.
1041 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1042 Elts.push_back(ThisPtrType);
1043 } else {
1044 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001045 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001046 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1047 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001048 }
1049
1050 // Copy rest of the arguments.
1051 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1052 Elts.push_back(Args.getElement(i));
1053
Manman Ren67f005e2014-07-28 22:24:34 +00001054 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055
Adrian Prantl0630eb72013-12-18 21:48:18 +00001056 unsigned Flags = 0;
1057 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1058 Flags |= llvm::DIDescriptor::FlagLValueReference;
1059 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1060 Flags |= llvm::DIDescriptor::FlagRValueReference;
1061
1062 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001063}
1064
Eric Christopherb2a008c2013-05-16 00:45:12 +00001065/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001066/// inside a function.
1067static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1068 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1069 return isFunctionLocalClass(NRD);
1070 if (isa<FunctionDecl>(RD->getDeclContext()))
1071 return true;
1072 return false;
1073}
1074
1075/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1076/// a single member function GlobalDecl.
1077llvm::DISubprogram
1078CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001079 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001080 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001081 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001082
Guy Benyei11169dd2012-12-18 14:30:41 +00001083 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001084 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001085
1086 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1087 // make sense to give a single ctor/dtor a linkage name.
1088 StringRef MethodLinkageName;
1089 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1090 MethodLinkageName = CGM.getMangledName(Method);
1091
1092 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001093 llvm::DIFile MethodDefUnit;
1094 unsigned MethodLine = 0;
1095 if (!Method->isImplicit()) {
1096 MethodDefUnit = getOrCreateFile(Method->getLocation());
1097 MethodLine = getLineNumber(Method->getLocation());
1098 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001099
1100 // Collect virtual method info.
1101 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001102 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001103 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 if (Method->isVirtual()) {
1106 if (Method->isPure())
1107 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1108 else
1109 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001110
Guy Benyei11169dd2012-12-18 14:30:41 +00001111 // It doesn't make sense to give a virtual destructor a vtable index,
1112 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001113 // FIXME: Add proper support for debug info for virtual calls in
1114 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1115 // lookup if we have multiple or virtual inheritance.
1116 if (!isa<CXXDestructorDecl>(Method) &&
1117 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001118 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001119 ContainingType = RecordTy;
1120 }
1121
1122 unsigned Flags = 0;
1123 if (Method->isImplicit())
1124 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001125 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1127 if (CXXC->isExplicit())
1128 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001129 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001130 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001131 if (CXXC->isExplicit())
1132 Flags |= llvm::DIDescriptor::FlagExplicit;
1133 }
1134 if (Method->hasPrototype())
1135 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001136 if (Method->getRefQualifier() == RQ_LValue)
1137 Flags |= llvm::DIDescriptor::FlagLValueReference;
1138 if (Method->getRefQualifier() == RQ_RValue)
1139 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001140
1141 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001142 llvm::DISubprogram SP = DBuilder.createMethod(
1143 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1144 MethodTy, /*isLocalToUnit=*/false,
1145 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1146 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001147
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001148 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001149
1150 return SP;
1151}
1152
1153/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001154/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001155/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001156void CGDebugInfo::CollectCXXMemberFunctions(
1157 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001158 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001159
1160 // Since we want more than just the individual member decls if we
1161 // have templated functions iterate over every declaration to gather
1162 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001163 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001164 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1165 // If the member is implicit, don't add it to the member list. This avoids
1166 // the member being added to type units by LLVM, while still allowing it
1167 // to be emitted into the type declaration/reference inside the compile
1168 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001169 // FIXME: Handle Using(Shadow?)Decls here to create
1170 // DW_TAG_imported_declarations inside the class for base decls brought into
1171 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1172 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1173 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001174 if (!Method || Method->isImplicit())
1175 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001176
1177 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1178 continue;
1179
David Blaikiefd580722014-10-06 05:18:55 +00001180 // Reuse the existing member function declaration if it exists.
1181 // It may be associated with the declaration of the type & should be
1182 // reused as we're building the definition.
1183 //
1184 // This situation can arise in the vtable-based debug info reduction where
1185 // implicit members are emitted in a non-vtable TU.
1186 auto MI = SPCache.find(Method->getCanonicalDecl());
1187 EltTys.push_back(MI == SPCache.end()
1188 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001189 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001190 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001191}
Guy Benyei11169dd2012-12-18 14:30:41 +00001192
Guy Benyei11169dd2012-12-18 14:30:41 +00001193/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001194/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001195/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001196void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001197 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001198 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001199
1200 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001201 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001202 unsigned BFlags = 0;
1203 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001204
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001206 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001207
Aaron Ballman574705e2014-03-13 15:41:46 +00001208 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001209 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1210 // virtual base offset offset is -ve. The code generator emits dwarf
1211 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001212 BaseOffset = 0 - CGM.getItaniumVTableContext()
1213 .getVirtualBaseOffsetOffset(RD, Base)
1214 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001215 } else {
1216 // In the MS ABI, store the vbtable offset, which is analogous to the
1217 // vbase offset offset in Itanium.
1218 BaseOffset =
1219 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1220 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001221 BFlags = llvm::DIDescriptor::FlagVirtual;
1222 } else
1223 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1224 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1225 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001226
Adrian Prantl21361fb2014-08-29 22:44:27 +00001227 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001228 llvm::DIType DTy = DBuilder.createInheritance(
1229 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001230 EltTys.push_back(DTy);
1231 }
1232}
1233
1234/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001235llvm::DIArray
1236CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1237 ArrayRef<TemplateArgument> TAList,
1238 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001239 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1241 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001242 StringRef Name;
1243 if (TPList)
1244 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001245 switch (TA.getKind()) {
1246 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1248 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001249 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001250 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001251 } break;
1252 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1254 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001255 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001256 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001257 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1258 TemplateParams.push_back(TVP);
1259 } break;
1260 case TemplateArgument::Declaration: {
1261 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001262 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001263 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001264 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001265 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001266 // Variable pointer template parameters have a value that is the address
1267 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001268 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001269 V = CGM.GetAddrOfGlobalVar(VD);
1270 // Member function pointers have special support for building them, though
1271 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001272 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001273 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001274 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001275 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001276 // Member data pointers have special handling too to compute the fixed
1277 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001278 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001279 // These five lines (& possibly the above member function pointer
1280 // handling) might be able to be refactored to use similar code in
1281 // CodeGenModule::getMemberPointerConstant
1282 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1283 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001284 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001285 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001286 }
1287 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001288 DBuilder.createTemplateValueParameter(
1289 TheCU, Name, TTy,
1290 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001291 TemplateParams.push_back(TVP);
1292 } break;
1293 case TemplateArgument::NullPtr: {
1294 QualType T = TA.getNullPtrType();
1295 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001296 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001297 // Special case member data pointer null values since they're actually -1
1298 // instead of zero.
1299 if (const MemberPointerType *MPT =
1300 dyn_cast<MemberPointerType>(T.getTypePtr()))
1301 // But treat member function pointers as simple zero integers because
1302 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1303 // CodeGen grows handling for values of non-null member function
1304 // pointers then perhaps we could remove this special case and rely on
1305 // EmitNullMemberPointer for member function pointers.
1306 if (MPT->isMemberDataPointer())
1307 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1308 if (!V)
1309 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1310 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001311 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1312 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001313 TemplateParams.push_back(TVP);
1314 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001315 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001316 llvm::DITemplateValueParameter
1317 TVP = DBuilder.createTemplateTemplateParameter(
1318 TheCU, Name, llvm::DIType(),
1319 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001320 TemplateParams.push_back(TVP);
1321 } break;
1322 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001323 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1324 TheCU, Name, llvm::DIType(),
1325 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001326 TemplateParams.push_back(TVP);
1327 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001328 case TemplateArgument::Expression: {
1329 const Expr *E = TA.getAsExpr();
1330 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001331 if (E->isGLValue())
1332 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001333 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001334 assert(V && "Expression in template argument isn't constant");
1335 llvm::DIType TTy = getOrCreateType(T, Unit);
1336 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001337 DBuilder.createTemplateValueParameter(
1338 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001339 TemplateParams.push_back(TVP);
1340 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001341 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001342 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::Null:
1344 llvm_unreachable(
1345 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001346 }
1347 }
1348 return DBuilder.getOrCreateArray(TemplateParams);
1349}
1350
1351/// CollectFunctionTemplateParams - A helper function to collect debug
1352/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001353llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1354 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 if (FD->getTemplatedKind() ==
1356 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001357 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1358 ->getTemplate()
1359 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001360 return CollectTemplateParams(
1361 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
1363 return llvm::DIArray();
1364}
1365
1366/// CollectCXXTemplateParams - A helper function to collect debug info for
1367/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001368llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1369 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001370 // Always get the full list of parameters, not just the ones from
1371 // the specialization.
1372 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001373 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001374 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001375 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001376}
1377
1378/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1379llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1380 if (VTablePtrType.isValid())
1381 return VTablePtrType;
1382
1383 ASTContext &Context = CGM.getContext();
1384
1385 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001386 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001387 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1389 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001390 llvm::DIType vtbl_ptr_type =
1391 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1393 return VTablePtrType;
1394}
1395
1396/// getVTableName - Get vtable name for the given Class.
1397StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001398 // Copy the gdb compatible name on the side and use its reference.
1399 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001400}
1401
Guy Benyei11169dd2012-12-18 14:30:41 +00001402/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1403/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001404void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001405 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1407
1408 // If there is a primary base then it will hold vtable info.
1409 if (RL.getPrimaryBase())
1410 return;
1411
1412 // If this class is not dynamic then there is not any vtable info to collect.
1413 if (!RD->isDynamicClass())
1414 return;
1415
1416 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001417 llvm::DIType VPTR = DBuilder.createMemberType(
1418 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1419 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 EltTys.push_back(VPTR);
1421}
1422
Eric Christopherb2a008c2013-05-16 00:45:12 +00001423/// getOrCreateRecordType - Emit record type's standalone debug info.
1424llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001425 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001426 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1428 return T;
1429}
1430
1431/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1432/// debug info.
1433llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001434 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001435 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001437 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 return T;
1439}
1440
David Blaikie483a9da2014-05-06 18:35:21 +00001441void CGDebugInfo::completeType(const EnumDecl *ED) {
1442 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1443 return;
1444 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001445 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001446 auto I = TypeCache.find(TyPtr);
1447 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001448 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001449 return;
1450 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1451 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001452 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001453}
1454
David Blaikieb2e86eb2013-08-15 20:49:17 +00001455void CGDebugInfo::completeType(const RecordDecl *RD) {
1456 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1457 !CGM.getLangOpts().CPlusPlus)
1458 completeRequiredType(RD);
1459}
1460
1461void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001462 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1463 return;
1464
David Blaikie6943dea2013-08-20 01:28:15 +00001465 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1466 if (CXXDecl->isDynamicClass())
1467 return;
1468
David Blaikieb2e86eb2013-08-15 20:49:17 +00001469 QualType Ty = CGM.getContext().getRecordType(RD);
1470 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001471 if (T && T.isForwardDecl())
1472 completeClassData(RD);
1473}
1474
1475void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1476 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001477 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001478 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001479 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001480 auto I = TypeCache.find(TyPtr);
1481 if (I != TypeCache.end() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001482 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001483 return;
1484 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1485 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001486 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001487}
1488
David Blaikie0e716b42014-03-03 23:48:23 +00001489static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1490 CXXRecordDecl::method_iterator End) {
1491 for (; I != End; ++I)
1492 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001493 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1494 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001495 return true;
1496 return false;
1497}
1498
1499static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1500 const RecordDecl *RD,
1501 const LangOptions &LangOpts) {
1502 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1503 return false;
1504
1505 if (!LangOpts.CPlusPlus)
1506 return false;
1507
1508 if (!RD->isCompleteDefinitionRequired())
1509 return true;
1510
1511 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1512
1513 if (!CXXDecl)
1514 return false;
1515
1516 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1517 return true;
1518
1519 TemplateSpecializationKind Spec = TSK_Undeclared;
1520 if (const ClassTemplateSpecializationDecl *SD =
1521 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1522 Spec = SD->getSpecializationKind();
1523
1524 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1525 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1526 CXXDecl->method_end()))
1527 return true;
1528
1529 return false;
1530}
1531
Guy Benyei11169dd2012-12-18 14:30:41 +00001532/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001533llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001535 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001536 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001537 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001538 T = getOrCreateRecordFwdDecl(
1539 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001540 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001541 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001542
David Blaikieb2e86eb2013-08-15 20:49:17 +00001543 return CreateTypeDefinition(Ty);
1544}
1545
1546llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1547 RecordDecl *RD = Ty->getDecl();
1548
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 // Get overall information about the record type for the debug info.
1550 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1551
1552 // Records and classes and unions can all be recursive. To handle them, we
1553 // first generate a debug descriptor for the struct as a forward declaration.
1554 // Then (if it is a definition) we go through and get debug info for all of
1555 // its members. Finally, we create a descriptor for the complete type (which
1556 // may refer to the forward decl if the struct is recursive) and replace all
1557 // uses of the forward declaration with the final definition.
1558
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001559 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001560 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001561 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001562
1563 if (FwdDecl.isForwardDecl())
1564 return FwdDecl;
1565
David Blaikieadfbf992013-08-18 16:55:33 +00001566 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1567 CollectContainingType(CXXDecl, FwdDecl);
1568
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001570 LexicalBlockStack.emplace_back(&*FwdDecl);
1571 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001574 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001575 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001576
1577 // Note: The split of CXXDecl information here is intentional, the
1578 // gdb tests will depend on a certain ordering at printout. The debug
1579 // information offsets are still correct if we merge them all together
1580 // though.
1581 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1582 if (CXXDecl) {
1583 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1584 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1585 }
1586
Eric Christopher91a31902013-01-16 01:22:32 +00001587 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001588 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001589 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001591
1592 LexicalBlockStack.pop_back();
1593 RegionMap.erase(Ty->getDecl());
1594
1595 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001596 FwdDecl.setArrays(Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001597
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001598 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001599 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001600}
1601
1602/// CreateType - get objective-c object type.
1603llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1604 llvm::DIFile Unit) {
1605 // Ignore protocols.
1606 return getOrCreateType(Ty->getBaseType(), Unit);
1607}
1608
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001609/// \return true if Getter has the default name for the property PD.
1610static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1611 const ObjCMethodDecl *Getter) {
1612 assert(PD);
1613 if (!Getter)
1614 return true;
1615
1616 assert(Getter->getDeclName().isObjCZeroArgSelector());
1617 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001618 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001619}
1620
1621/// \return true if Setter has the default name for the property PD.
1622static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1623 const ObjCMethodDecl *Setter) {
1624 assert(PD);
1625 if (!Setter)
1626 return true;
1627
1628 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001629 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001630 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001631}
1632
Guy Benyei11169dd2012-12-18 14:30:41 +00001633/// CreateType - get objective-c interface type.
1634llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1635 llvm::DIFile Unit) {
1636 ObjCInterfaceDecl *ID = Ty->getDecl();
1637 if (!ID)
1638 return llvm::DIType();
1639
1640 // Get overall information about the record type for the debug info.
1641 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1642 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001643 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001644
1645 // If this is just a forward declaration return a special forward-declaration
1646 // debug type since we won't be able to lay out the entire type.
1647 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001648 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001649 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1650 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1651 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001652 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001653 return FwdDecl;
1654 }
1655
David Blaikieef8a9512014-05-05 23:23:53 +00001656 return CreateTypeDefinition(Ty, Unit);
1657}
1658
Eric Christophere7b87e52014-10-26 23:40:33 +00001659llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1660 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001661 ObjCInterfaceDecl *ID = Ty->getDecl();
1662 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1663 unsigned Line = getLineNumber(ID->getLocation());
1664 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001665
1666 // Bit size, align and offset of the type.
1667 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1668 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1669
1670 unsigned Flags = 0;
1671 if (ID->getImplementation())
1672 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1673
Eric Christophere7b87e52014-10-26 23:40:33 +00001674 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1675 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1676 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001677
David Blaikieef8a9512014-05-05 23:23:53 +00001678 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001679 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001680
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001681 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001682 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1683 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
1685 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001686 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001687
1688 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1689 if (SClass) {
1690 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001691 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 if (!SClassTy.isValid())
1693 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001694
Eric Christophere7b87e52014-10-26 23:40:33 +00001695 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 EltTys.push_back(InhTag);
1697 }
1698
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001699 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001700 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 SourceLocation Loc = PD->getLocation();
1702 llvm::DIFile PUnit = getOrCreateFile(Loc);
1703 unsigned PLine = getLineNumber(Loc);
1704 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1705 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001706 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1707 PD->getName(), PUnit, PLine,
1708 hasDefaultGetterName(PD, Getter) ? ""
1709 : getSelectorName(PD->getGetterName()),
1710 hasDefaultSetterName(PD, Setter) ? ""
1711 : getSelectorName(PD->getSetterName()),
1712 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001713 EltTys.push_back(PropertyNode);
1714 }
1715
1716 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1717 unsigned FieldNo = 0;
1718 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1719 Field = Field->getNextIvar(), ++FieldNo) {
1720 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1721 if (!FieldTy.isValid())
1722 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001723
Guy Benyei11169dd2012-12-18 14:30:41 +00001724 StringRef FieldName = Field->getName();
1725
1726 // Ignore unnamed fields.
1727 if (FieldName.empty())
1728 continue;
1729
1730 // Get the location for the field.
1731 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1732 unsigned FieldLine = getLineNumber(Field->getLocation());
1733 QualType FType = Field->getType();
1734 uint64_t FieldSize = 0;
1735 unsigned FieldAlign = 0;
1736
1737 if (!FType->isIncompleteArrayType()) {
1738
1739 // Bit size, align and offset of the type.
1740 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001741 ? Field->getBitWidthValue(CGM.getContext())
1742 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 FieldAlign = CGM.getContext().getTypeAlign(FType);
1744 }
1745
1746 uint64_t FieldOffset;
1747 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1748 // We don't know the runtime offset of an ivar if we're using the
1749 // non-fragile ABI. For bitfields, use the bit offset into the first
1750 // byte of storage of the bitfield. For other fields, use zero.
1751 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001752 FieldOffset =
1753 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001754 FieldOffset %= CGM.getContext().getCharWidth();
1755 } else {
1756 FieldOffset = 0;
1757 }
1758 } else {
1759 FieldOffset = RL.getFieldOffset(FieldNo);
1760 }
1761
1762 unsigned Flags = 0;
1763 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1764 Flags = llvm::DIDescriptor::FlagProtected;
1765 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1766 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001767 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1768 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001769
Craig Topper8a13c412014-05-21 05:09:00 +00001770 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001771 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001772 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001773 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001775 SourceLocation Loc = PD->getLocation();
1776 llvm::DIFile PUnit = getOrCreateFile(Loc);
1777 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1779 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001780 PropertyNode = DBuilder.createObjCProperty(
1781 PD->getName(), PUnit, PLine,
1782 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1783 PD->getGetterName()),
1784 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1785 PD->getSetterName()),
1786 PD->getPropertyAttributes(),
1787 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001788 }
1789 }
1790 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001791 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1792 FieldSize, FieldAlign, FieldOffset, Flags,
1793 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001794 EltTys.push_back(FieldTy);
1795 }
1796
1797 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Manman Ren51289892014-07-28 19:14:41 +00001798 RealDecl.setArrays(Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001799
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001801 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001802}
1803
1804llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1805 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1806 int64_t Count = Ty->getNumElements();
1807 if (Count == 0)
1808 // If number of elements are not known then this is an unbounded array.
1809 // Use Count == -1 to express such arrays.
1810 Count = -1;
1811
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001812 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001813 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1814
1815 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1816 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1817
1818 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1819}
1820
Eric Christophere7b87e52014-10-26 23:40:33 +00001821llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 uint64_t Size;
1823 uint64_t Align;
1824
1825 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1826 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1827 Size = 0;
1828 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001829 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001830 } else if (Ty->isIncompleteArrayType()) {
1831 Size = 0;
1832 if (Ty->getElementType()->isIncompleteType())
1833 Align = 0;
1834 else
1835 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001836 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001837 Size = 0;
1838 Align = 0;
1839 } else {
1840 // Size and align of the whole array, not the element type.
1841 Size = CGM.getContext().getTypeSize(Ty);
1842 Align = CGM.getContext().getTypeAlign(Ty);
1843 }
1844
1845 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1846 // interior arrays, do we care? Why aren't nested arrays represented the
1847 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001848 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001849 QualType EltTy(Ty, 0);
1850 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1851 // If the number of elements is known, then count is that number. Otherwise,
1852 // it's -1. This allows us to represent a subrange with an array of 0
1853 // elements, like this:
1854 //
1855 // struct foo {
1856 // int x[0];
1857 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001858 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001859 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1860 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001861
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 // FIXME: Verify this is right for VLAs.
1863 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1864 EltTy = Ty->getElementType();
1865 }
1866
1867 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1868
Eric Christophere7b87e52014-10-26 23:40:33 +00001869 llvm::DIType DbgTy = DBuilder.createArrayType(
1870 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 return DbgTy;
1872}
1873
Eric Christopherb2a008c2013-05-16 00:45:12 +00001874llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001876 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1877 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001878}
1879
Eric Christopherb2a008c2013-05-16 00:45:12 +00001880llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001882 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1883 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001884}
1885
Eric Christopherb2a008c2013-05-16 00:45:12 +00001886llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001888 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1889 if (!Ty->getPointeeType()->isFunctionType())
1890 return DBuilder.createMemberPointerType(
David Blaikie99dab3b2013-09-04 22:03:57 +00001891 getOrCreateType(Ty->getPointeeType(), U), ClassType);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001892
1893 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001894 Ty->getPointeeType()->getAs<FunctionProtoType>();
1895 return DBuilder.createMemberPointerType(
1896 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1897 Ty->getClass(), FPT->getTypeQuals())),
1898 FPT, U),
1899 ClassType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001900}
1901
Eric Christophere7b87e52014-10-26 23:40:33 +00001902llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 // Ignore the atomic wrapping
1904 // FIXME: What is the correct representation?
1905 return getOrCreateType(Ty->getValueType(), U);
1906}
1907
1908/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001909llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001910 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 uint64_t Size = 0;
1912 uint64_t Align = 0;
1913 if (!ED->getTypeForDecl()->isIncompleteType()) {
1914 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1915 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1916 }
1917
Manman Rene0064d82013-08-29 23:19:58 +00001918 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // If this is just a forward declaration, construct an appropriately
1921 // marked node and just return it.
1922 if (!ED->getDefinition()) {
1923 llvm::DIDescriptor EDContext;
1924 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1925 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1926 unsigned Line = getLineNumber(ED->getLocation());
1927 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001928 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1929 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1930 0, Size, Align, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001931 ReplaceMap.emplace_back(
1932 std::piecewise_construct, std::make_tuple(Ty),
1933 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001934 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 }
1936
David Blaikie483a9da2014-05-06 18:35:21 +00001937 return CreateTypeDefinition(Ty);
1938}
1939
1940llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1941 const EnumDecl *ED = Ty->getDecl();
1942 uint64_t Size = 0;
1943 uint64_t Align = 0;
1944 if (!ED->getTypeForDecl()->isIncompleteType()) {
1945 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1946 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1947 }
1948
1949 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1950
Guy Benyei11169dd2012-12-18 14:30:41 +00001951 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001952 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001954 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001955 Enumerators.push_back(DBuilder.createEnumerator(
1956 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001957 }
1958
1959 // Return a CompositeType for the enum itself.
1960 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1961
1962 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1963 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001964 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001965 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1966 llvm::DIType ClassTy = ED->isFixed()
1967 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1968 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001969 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001970 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1971 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001972 return DbgTy;
1973}
1974
David Blaikie05491062013-01-21 04:37:12 +00001975static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1976 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001978 Qualifiers InnerQuals = T.getLocalQualifiers();
1979 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1980 // that is already there.
1981 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1982 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001983 QualType LastT = T;
1984 switch (T->getTypeClass()) {
1985 default:
David Blaikie05491062013-01-21 04:37:12 +00001986 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001987 case Type::TemplateSpecialization: {
1988 const auto *Spec = cast<TemplateSpecializationType>(T);
1989 if (Spec->isTypeAlias())
1990 return C.getQualifiedType(T.getTypePtr(), Quals);
1991 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001992 break;
1993 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001994 case Type::TypeOfExpr:
1995 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1996 break;
1997 case Type::TypeOf:
1998 T = cast<TypeOfType>(T)->getUnderlyingType();
1999 break;
2000 case Type::Decltype:
2001 T = cast<DecltypeType>(T)->getUnderlyingType();
2002 break;
2003 case Type::UnaryTransform:
2004 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2005 break;
2006 case Type::Attributed:
2007 T = cast<AttributedType>(T)->getEquivalentType();
2008 break;
2009 case Type::Elaborated:
2010 T = cast<ElaboratedType>(T)->getNamedType();
2011 break;
2012 case Type::Paren:
2013 T = cast<ParenType>(T)->getInnerType();
2014 break;
David Blaikie05491062013-01-21 04:37:12 +00002015 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 break;
2018 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002019 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002020 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002021 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 break;
2023 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002024
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002026 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 } while (true);
2028}
2029
Eric Christopher0fdcb312013-05-16 00:52:20 +00002030/// getType - Get the type from the cache or return null type if it doesn't
2031/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002032llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2033
2034 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002035 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002036
David Blaikief427b002014-05-06 03:42:01 +00002037 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 if (it != TypeCache.end()) {
2039 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002040 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 return llvm::DIType(cast<llvm::MDNode>(V));
2042 }
2043
2044 return llvm::DIType();
2045}
2046
David Blaikie0e716b42014-03-03 23:48:23 +00002047void CGDebugInfo::completeTemplateDefinition(
2048 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002049 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2050 return;
2051
David Blaikie0e716b42014-03-03 23:48:23 +00002052 completeClassData(&SD);
2053 // In case this type has no member function definitions being emitted, ensure
2054 // it is retained
2055 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2056}
2057
Guy Benyei11169dd2012-12-18 14:30:41 +00002058/// getOrCreateType - Get the type from the cache or create a new
2059/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002060llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 if (Ty.isNull())
2062 return llvm::DIType();
2063
2064 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002065 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002066
David Blaikieef8a9512014-05-05 23:23:53 +00002067 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 return T;
2069
2070 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002071 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002072 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002073
2074 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002075 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002076
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 return Res;
2078}
2079
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002080/// Currently the checksum of an interface includes the number of
2081/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002082unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002083 // The assumption is that the number of ivars can only increase
2084 // monotonically, so it is safe to just use their current number as
2085 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002086 unsigned Sum = 0;
2087 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002088 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002089 ++Sum;
2090
2091 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002092}
2093
2094ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2095 switch (Ty->getTypeClass()) {
2096 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002097 return getObjCInterfaceDecl(
2098 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002099 case Type::ObjCInterface:
2100 return cast<ObjCInterfaceType>(Ty)->getDecl();
2101 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002102 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002103 }
2104}
2105
Guy Benyei11169dd2012-12-18 14:30:41 +00002106/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002107llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002108 // Handle qualifiers, which recursively handles what they refer to.
2109 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002110 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002111
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 // Work out details of type.
2113 switch (Ty->getTypeClass()) {
2114#define TYPE(Class, Base)
2115#define ABSTRACT_TYPE(Class, Base)
2116#define NON_CANONICAL_TYPE(Class, Base)
2117#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2118#include "clang/AST/TypeNodes.def"
2119 llvm_unreachable("Dependent types cannot show up in debug information");
2120
2121 case Type::ExtVector:
2122 case Type::Vector:
2123 return CreateType(cast<VectorType>(Ty), Unit);
2124 case Type::ObjCObjectPointer:
2125 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2126 case Type::ObjCObject:
2127 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2128 case Type::ObjCInterface:
2129 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2130 case Type::Builtin:
2131 return CreateType(cast<BuiltinType>(Ty));
2132 case Type::Complex:
2133 return CreateType(cast<ComplexType>(Ty));
2134 case Type::Pointer:
2135 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002136 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002137 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002138 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002139 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002140 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002141 case Type::BlockPointer:
2142 return CreateType(cast<BlockPointerType>(Ty), Unit);
2143 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002144 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002146 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002148 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 case Type::FunctionProto:
2150 case Type::FunctionNoProto:
2151 return CreateType(cast<FunctionType>(Ty), Unit);
2152 case Type::ConstantArray:
2153 case Type::VariableArray:
2154 case Type::IncompleteArray:
2155 return CreateType(cast<ArrayType>(Ty), Unit);
2156
2157 case Type::LValueReference:
2158 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2159 case Type::RValueReference:
2160 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2161
2162 case Type::MemberPointer:
2163 return CreateType(cast<MemberPointerType>(Ty), Unit);
2164
2165 case Type::Atomic:
2166 return CreateType(cast<AtomicType>(Ty), Unit);
2167
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002169 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2170
David Blaikie42edade2014-11-11 20:44:45 +00002171 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002172 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 case Type::Elaborated:
2174 case Type::Paren:
2175 case Type::SubstTemplateTypeParm:
2176 case Type::TypeOfExpr:
2177 case Type::TypeOf:
2178 case Type::Decltype:
2179 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002180 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002181 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002183
David Blaikie42edade2014-11-11 20:44:45 +00002184 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002185}
2186
2187/// getOrCreateLimitedType - Get the type from the cache or create a new
2188/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002189llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002190 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002191 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002192
David Blaikie8d5e1282013-08-20 21:03:29 +00002193 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002194
2195 // We may have cached a forward decl when we could have created
2196 // a non-forward decl. Go ahead and create a non-forward decl
2197 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002198 if (T && !T.isForwardDecl())
2199 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002200
2201 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002202 llvm::DICompositeType Res = CreateLimitedType(Ty);
2203
2204 // Propagate members from the declaration to the definition
2205 // CreateType(const RecordType*) will overwrite this with the members in the
2206 // correct order if the full type is needed.
Manman Ren51289892014-07-28 19:14:41 +00002207 Res.setArrays(T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002210 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 return Res;
2212}
2213
2214// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002215llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002217
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 // Get overall information about the record type for the debug info.
2219 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2220 unsigned Line = getLineNumber(RD->getLocation());
2221 StringRef RDName = getClassName(RD);
2222
Eric Christopher07429ff2013-10-15 21:22:34 +00002223 llvm::DIDescriptor RDContext =
2224 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002225
David Blaikied2785892013-08-18 17:36:19 +00002226 // If we ended up creating the type during the context chain construction,
2227 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002228 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2229 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002230 return T;
David Blaikied2785892013-08-18 17:36:19 +00002231
Adrian Prantl381e7552014-02-04 21:29:50 +00002232 // If this is just a forward or incomplete declaration, construct an
2233 // appropriately marked node and just return it.
2234 const RecordDecl *D = RD->getDefinition();
2235 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002236 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002237
2238 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2239 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002240 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002241
Manman Rene0064d82013-08-29 23:19:58 +00002242 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2243
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002245 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2246 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 else if (RD->isClass()) {
2248 // FIXME: This could be a struct type giving a default visibility different
2249 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002250 RealDecl = DBuilder.createClassType(
2251 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2252 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002253 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002254 RealDecl = DBuilder.createStructType(
2255 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2256 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002257
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002258 RegionMap[Ty->getDecl()].reset(RealDecl);
2259 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
David Blaikieadfbf992013-08-18 16:55:33 +00002261 if (const ClassTemplateSpecializationDecl *TSpecial =
2262 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Manman Ren51289892014-07-28 19:14:41 +00002263 RealDecl.setArrays(llvm::DIArray(),
Eric Christophere7b87e52014-10-26 23:40:33 +00002264 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002265 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002266}
2267
David Blaikieadfbf992013-08-18 16:55:33 +00002268void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2269 llvm::DICompositeType RealDecl) {
2270 // A class's primary base or the class itself contains the vtable.
2271 llvm::DICompositeType ContainingType;
2272 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2273 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002274 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002275 while (1) {
2276 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2277 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2278 if (PBT && !BRL.isPrimaryBaseVirtual())
2279 PBase = PBT;
2280 else
2281 break;
2282 }
2283 ContainingType = llvm::DICompositeType(
2284 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2285 getOrCreateFile(RD->getLocation())));
2286 } else if (RD->isDynamicClass())
2287 ContainingType = RealDecl;
2288
2289 RealDecl.setContainingType(ContainingType);
2290}
2291
Guy Benyei11169dd2012-12-18 14:30:41 +00002292/// CreateMemberType - Create new member and increase Offset by FType's size.
2293llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002294 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2296 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2297 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002298 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2299 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002300 *Offset += FieldSize;
2301 return Ty;
2302}
2303
Frederic Riss9db79f12014-11-18 03:40:46 +00002304void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2305 llvm::DIFile Unit,
2306 StringRef &Name, StringRef &LinkageName,
2307 llvm::DIDescriptor &FDContext,
2308 llvm::DIArray &TParamsArray,
2309 unsigned &Flags) {
2310 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2311 Name = getFunctionName(FD);
2312 // Use mangled name as linkage name for C/C++ functions.
2313 if (FD->hasPrototype()) {
2314 LinkageName = CGM.getMangledName(GD);
2315 Flags |= llvm::DIDescriptor::FlagPrototyped;
2316 }
2317 // No need to replicate the linkage name if it isn't different from the
2318 // subprogram name, no need to have it at all unless coverage is enabled or
2319 // debug is set to more than just line tables.
2320 if (LinkageName == Name ||
2321 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2322 !CGM.getCodeGenOpts().EmitGcovNotes &&
2323 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2324 LinkageName = StringRef();
2325
2326 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2327 if (const NamespaceDecl *NSDecl =
2328 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2329 FDContext = getOrCreateNameSpace(NSDecl);
2330 else if (const RecordDecl *RDecl =
2331 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2332 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2333 // Collect template parameters.
2334 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2335 }
2336}
2337
2338void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2339 unsigned &LineNo, QualType &T,
2340 StringRef &Name, StringRef &LinkageName,
2341 llvm::DIDescriptor &VDContext) {
2342 Unit = getOrCreateFile(VD->getLocation());
2343 LineNo = getLineNumber(VD->getLocation());
2344
2345 setLocation(VD->getLocation());
2346
2347 T = VD->getType();
2348 if (T->isIncompleteArrayType()) {
2349 // CodeGen turns int[] into int[1] so we'll do the same here.
2350 llvm::APInt ConstVal(32, 1);
2351 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2352
2353 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2354 ArrayType::Normal, 0);
2355 }
2356
2357 Name = VD->getName();
2358 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2359 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2360 LinkageName = CGM.getMangledName(VD);
2361 if (LinkageName == Name)
2362 LinkageName = StringRef();
2363
2364 // Since we emit declarations (DW_AT_members) for static members, place the
2365 // definition of those static members in the namespace they were declared in
2366 // in the source code (the lexical decl context).
2367 // FIXME: Generalize this for even non-member global variables where the
2368 // declaration and definition may have different lexical decl contexts, once
2369 // we have support for emitting declarations of (non-member) global variables.
2370 VDContext = getContextDescriptor(
2371 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2372 : VD->getDeclContext()));
2373}
2374
Frederic Rissd253ed62014-11-18 03:40:51 +00002375llvm::DISubprogram
2376CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2377 llvm::DIArray TParamsArray;
2378 StringRef Name, LinkageName;
2379 unsigned Flags = 0;
2380 SourceLocation Loc = FD->getLocation();
2381 llvm::DIFile Unit = getOrCreateFile(Loc);
2382 llvm::DIDescriptor DContext(Unit);
2383 unsigned Line = getLineNumber(Loc);
2384
2385 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2386 TParamsArray, Flags);
2387 // Build function type.
2388 SmallVector<QualType, 16> ArgTypes;
2389 for (const ParmVarDecl *Parm: FD->parameters())
2390 ArgTypes.push_back(Parm->getType());
2391 QualType FnType =
2392 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2393 FunctionProtoType::ExtProtoInfo());
2394 llvm::DISubprogram SP =
2395 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2396 getOrCreateFunctionType(FD, FnType, Unit),
2397 !FD->isExternallyVisible(),
2398 false /*declaration*/, 0, Flags,
2399 CGM.getLangOpts().Optimize, nullptr,
2400 TParamsArray, getFunctionDeclaration(FD));
2401 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002402 FwdDeclReplaceMap.emplace_back(
2403 std::piecewise_construct, std::make_tuple(CanonDecl),
2404 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002405 return SP;
2406}
2407
2408llvm::DIGlobalVariable
2409CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2410 QualType T;
2411 StringRef Name, LinkageName;
2412 SourceLocation Loc = VD->getLocation();
2413 llvm::DIFile Unit = getOrCreateFile(Loc);
2414 llvm::DIDescriptor DContext(Unit);
2415 unsigned Line = getLineNumber(Loc);
2416
2417 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2418 llvm::DIGlobalVariable GV =
2419 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2420 Line, getOrCreateType(T, Unit),
2421 !VD->isExternallyVisible(),
2422 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002423 FwdDeclReplaceMap.emplace_back(
2424 std::piecewise_construct,
2425 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2426 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002427 return GV;
2428}
2429
Frederic Riss442293e2014-11-06 21:12:06 +00002430llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002431 // We only need a declaration (not a definition) of the type - so use whatever
2432 // we would otherwise do to get a type for a pointee. (forward declarations in
2433 // limited debug info, full definitions (if the type definition is available)
2434 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002435 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2436 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002437 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002438 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002439
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002440 if (I != DeclCache.end())
2441 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002442
2443 // No definition for now. Emit a forward definition that might be
2444 // merged with a potential upcoming definition.
2445 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2446 return getFunctionForwardDeclaration(FD);
2447 else if (const auto *VD = dyn_cast<VarDecl>(D))
2448 return getGlobalVariableForwardDeclaration(VD);
2449
2450 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002451}
2452
Guy Benyei11169dd2012-12-18 14:30:41 +00002453/// getFunctionDeclaration - Return debug info descriptor to describe method
2454/// declaration for the given method definition.
2455llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002456 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002457 return llvm::DISubprogram();
2458
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002460 if (!FD)
2461 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002462
2463 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002464 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002465
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002466 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002467 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002468 if (const CXXMethodDecl *MD =
2469 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002470 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002471 llvm::DISubprogram SP =
2472 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002473 return SP;
2474 }
2475 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002477 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002478 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 return SP;
2480 }
2481
Aaron Ballman86c93902014-03-06 23:45:36 +00002482 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002483 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002485 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002486 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 return SP;
2488 }
2489 }
2490 return llvm::DISubprogram();
2491}
2492
2493// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2494// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002495llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2496 QualType FnType,
2497 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002498 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002499 // Create fake but valid subroutine type. Otherwise
2500 // llvm::DISubprogram::Verify() would return false, and
2501 // subprogram DIE will miss DW_AT_decl_file and
2502 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002503 return DBuilder.createSubroutineType(F,
2504 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002505
2506 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2507 return getOrCreateMethodType(Method, F);
2508 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2509 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002510 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002511
2512 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002513 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002514
2515 // Replace the instancetype keyword with the actual type.
2516 if (ResultTy == CGM.getContext().getObjCInstanceType())
2517 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002518 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002519
Adrian Prantl7bec9032013-05-10 21:08:31 +00002520 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002522 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2523 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2524 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 // "_cmd" pointer is always second argument.
2526 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2527 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2528 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002529 for (const auto *PI : OMethod->params())
2530 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002531 // Variadic methods need a special marker at the end of the type list.
2532 if (OMethod->isVariadic())
2533 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002534
Manman Ren67f005e2014-07-28 22:24:34 +00002535 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002536 return DBuilder.createSubroutineType(F, EltTypeArray);
2537 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002538
Adrian Prantl800faef2014-02-25 23:42:18 +00002539 // Handle variadic function types; they need an additional
2540 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002541 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2542 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002543 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002544 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2545 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2546 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2547 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2548 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002549 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002550 return DBuilder.createSubroutineType(F, EltTypeArray);
2551 }
2552
David Blaikie469f0792013-05-22 23:22:42 +00002553 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002554}
2555
2556/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002557void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2558 SourceLocation ScopeLoc, QualType FnType,
2559 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002560
2561 StringRef Name;
2562 StringRef LinkageName;
2563
2564 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2565
2566 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002567 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 unsigned Flags = 0;
2570 llvm::DIFile Unit = getOrCreateFile(Loc);
2571 llvm::DIDescriptor FDContext(Unit);
2572 llvm::DIArray TParamsArray;
2573 if (!HasDecl) {
2574 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002575 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2577 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002578 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002580 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2582 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002583 LexicalBlockStack.emplace_back(SPN);
2584 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 return;
2586 }
2587 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002588 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2589 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002590 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2591 Name = getObjCMethodName(OMD);
2592 Flags |= llvm::DIDescriptor::FlagPrototyped;
2593 } else {
2594 // Use llvm function name.
2595 Name = Fn->getName();
2596 Flags |= llvm::DIDescriptor::FlagPrototyped;
2597 }
2598 if (!Name.empty() && Name[0] == '\01')
2599 Name = Name.substr(1);
2600
Adrian Prantl42d71b92014-04-10 23:21:53 +00002601 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002603 // Artificial functions without a location should not silently reuse CurLoc.
2604 if (Loc.isInvalid())
2605 CurLoc = SourceLocation();
2606 }
2607 unsigned LineNo = getLineNumber(Loc);
2608 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002609
Eric Christopher8018e412014-03-27 18:50:35 +00002610 // FIXME: The function declaration we're constructing here is mostly reusing
2611 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2612 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2613 // all subprograms instead of the actual context since subprogram definitions
2614 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002615 llvm::DISubprogram SP = DBuilder.createFunction(
2616 FDContext, Name, LinkageName, Unit, LineNo,
2617 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2618 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2619 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002620 // We might get here with a VarDecl in the case we're generating
2621 // code for the initialization of globals. Do not record these decls
2622 // as they will overwrite the actual VarDecl Decl in the cache.
2623 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002624 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002625
Adrian Prantlbebb8932014-03-21 21:01:58 +00002626 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002628 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002629
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002631 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002632}
2633
2634/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002635/// information in the source file. If the location is invalid, the
2636/// previous location will be reused.
Adrian Prantlc7822422013-03-12 20:43:25 +00002637void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
Adrian Prantle83b1302014-01-07 22:05:52 +00002638 bool ForceColumnInfo) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 // Update our current location
2640 setLocation(Loc);
2641
Eric Christophere7b87e52014-10-26 23:40:33 +00002642 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2643 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002644
2645 // Don't bother if things are the same as last time.
2646 SourceManager &SM = CGM.getContext().getSourceManager();
2647 if (CurLoc == PrevLoc ||
2648 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
2649 // New Builder may not be in sync with CGDebugInfo.
David Blaikie357aafb2013-02-01 19:09:49 +00002650 if (!Builder.getCurrentDebugLocation().isUnknown() &&
2651 Builder.getCurrentDebugLocation().getScope(CGM.getLLVMContext()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002652 LexicalBlockStack.back())
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002654
Guy Benyei11169dd2012-12-18 14:30:41 +00002655 // Update last state.
2656 PrevLoc = CurLoc;
2657
Adrian Prantle83b1302014-01-07 22:05:52 +00002658 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002659 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2660 getLineNumber(CurLoc), getColumnNumber(CurLoc, ForceColumnInfo), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002661}
2662
2663/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2664/// the stack.
2665void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
David Blaikief9ea2422014-06-02 16:32:05 +00002666 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
2667 llvm::DIDescriptor(LexicalBlockStack.empty() ? nullptr
2668 : LexicalBlockStack.back()),
David Blaikieb8449842014-08-21 22:46:45 +00002669 getOrCreateFile(CurLoc), getLineNumber(CurLoc), getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002671 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002672}
2673
2674/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2675/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002676void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2677 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002678 // Set our current location.
2679 setLocation(Loc);
2680
Guy Benyei11169dd2012-12-18 14:30:41 +00002681 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002682 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2683 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002684
2685 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2686 return;
2687
2688 // Create a new lexical block and push it on the stack.
2689 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002690}
2691
2692/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2693/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002694void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2695 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002696 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2697
2698 // Provide an entry in the line table for the end of the block.
2699 EmitLocation(Builder, Loc);
2700
David Blaikie60a877b2014-10-22 19:34:33 +00002701 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2702 return;
2703
Guy Benyei11169dd2012-12-18 14:30:41 +00002704 LexicalBlockStack.pop_back();
2705}
2706
2707/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2708void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2709 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2710 unsigned RCount = FnBeginRegionCount.back();
2711 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2712
2713 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002714 while (LexicalBlockStack.size() != RCount) {
2715 // Provide an entry in the line table for the end of the block.
2716 EmitLocation(Builder, CurLoc);
2717 LexicalBlockStack.pop_back();
2718 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002719 FnBeginRegionCount.pop_back();
2720}
2721
Eric Christopherb2a008c2013-05-16 00:45:12 +00002722// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002723// See BuildByRefType.
2724llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2725 uint64_t *XOffset) {
2726
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002727 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002728 QualType FType;
2729 uint64_t FieldSize, FieldOffset;
2730 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002731
Guy Benyei11169dd2012-12-18 14:30:41 +00002732 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002733 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002734
2735 FieldOffset = 0;
2736 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2737 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2738 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2739 FType = CGM.getContext().IntTy;
2740 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2741 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2742
2743 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2744 if (HasCopyAndDispose) {
2745 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002746 EltTys.push_back(
2747 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2748 EltTys.push_back(
2749 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002750 }
2751 bool HasByrefExtendedLayout;
2752 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002753 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2754 HasByrefExtendedLayout) &&
2755 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002756 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002757 EltTys.push_back(
2758 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002759 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002760
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2762 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002763 CGM.getTarget().getPointerAlign(0))) {
2764 CharUnits FieldOffsetInBytes =
2765 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2766 CharUnits AlignedOffsetInBytes =
2767 FieldOffsetInBytes.RoundUpToAlignment(Align);
2768 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002769
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 if (NumPaddingBytes.isPositive()) {
2771 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2772 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2773 pad, ArrayType::Normal, 0);
2774 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2775 }
2776 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002777
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002779 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 FieldSize = CGM.getContext().getTypeSize(FType);
2781 FieldAlign = CGM.getContext().toBits(Align);
2782
Eric Christopherb2a008c2013-05-16 00:45:12 +00002783 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002784 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2785 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002786 EltTys.push_back(FieldTy);
2787 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002788
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002790
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002792
Guy Benyei11169dd2012-12-18 14:30:41 +00002793 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002794 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002795}
2796
2797/// EmitDeclare - Emit local variable declaration debug info.
Ed Masteda706022014-05-07 12:49:30 +00002798void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::LLVMConstants Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002799 llvm::Value *Storage, unsigned ArgNo,
2800 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002801 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002802 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2803
David Blaikie7fceebf2013-08-19 03:37:48 +00002804 bool Unwritten =
2805 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2806 cast<Decl>(VD->getDeclContext())->isImplicit());
2807 llvm::DIFile Unit;
2808 if (!Unwritten)
2809 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002810 llvm::DIType Ty;
2811 uint64_t XOffset = 0;
2812 if (VD->hasAttr<BlocksAttr>())
2813 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002814 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 Ty = getOrCreateType(VD->getType(), Unit);
2816
2817 // If there is no debug info for this type then do not emit debug info
2818 // for this variable.
2819 if (!Ty)
2820 return;
2821
Guy Benyei11169dd2012-12-18 14:30:41 +00002822 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002823 unsigned Line = 0;
2824 unsigned Column = 0;
2825 if (!Unwritten) {
2826 Line = getLineNumber(VD->getLocation());
2827 Column = getColumnNumber(VD->getLocation());
2828 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002829 unsigned Flags = 0;
2830 if (VD->isImplicit())
2831 Flags |= llvm::DIDescriptor::FlagArtificial;
2832 // If this is the first argument and it is implicit then
2833 // give it an object pointer flag.
2834 // FIXME: There has to be a better way to do this, but for static
2835 // functions there won't be an implicit param at arg1 and
2836 // otherwise it is 'self' or 'this'.
2837 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2838 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002839 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002840 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2841 !VD->getType()->isPointerType())
David Blaikieb9c667d2013-06-19 21:53:53 +00002842 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002843
2844 llvm::MDNode *Scope = LexicalBlockStack.back();
2845
2846 StringRef Name = VD->getName();
2847 if (!Name.empty()) {
2848 if (VD->hasAttr<BlocksAttr>()) {
2849 CharUnits offset = CharUnits::fromQuantity(32);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002850 SmallVector<int64_t, 9> addr;
2851 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002852 // offset of __forwarding field
2853 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002854 CGM.getTarget().getPointerWidth(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002855 addr.push_back(offset.getQuantity());
2856 addr.push_back(llvm::dwarf::DW_OP_deref);
2857 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 // offset of x field
2859 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002860 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002861
2862 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002863 llvm::DIVariable D = DBuilder.createLocalVariable(
2864 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002865
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 // Insert an llvm.dbg.declare into the current block.
2867 llvm::Instruction *Call =
Eric Christophere7b87e52014-10-26 23:40:33 +00002868 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2869 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2871 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002872 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl0315f382013-09-18 22:08:57 +00002873 Flags |= llvm::DIDescriptor::FlagIndirectVariable;
David Blaikiea76a7c92013-01-05 05:58:35 +00002874 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2875 // If VD is an anonymous union then Storage represents value for
2876 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002877 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002878 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002879 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2881 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002882
Guy Benyei11169dd2012-12-18 14:30:41 +00002883 // Ignore unnamed fields. Do not ignore unnamed records.
2884 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2885 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002886
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002888 llvm::DIVariable D = DBuilder.createLocalVariable(
2889 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2890 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002891
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002893 llvm::Instruction *Call = DBuilder.insertDeclare(
2894 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2896 }
David Blaikie219c7d92013-01-05 20:03:07 +00002897 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002898 }
2899 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002900
2901 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002902 llvm::DIVariable D = DBuilder.createLocalVariable(
2903 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2904 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002905
2906 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002907 llvm::Instruction *Call = DBuilder.insertDeclare(
2908 Storage, D, DBuilder.createExpression(), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002909 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002910}
2911
2912void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2913 llvm::Value *Storage,
2914 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002915 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002916 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2917}
2918
Adrian Prantlde17db32013-03-29 19:20:29 +00002919/// Look up the completed type for a self pointer in the TypeCache and
2920/// create a copy of it with the ObjectPointer and Artificial flags
2921/// set. If the type is not cached, a new one is created. This should
2922/// never happen though, since creating a type for the implicit self
2923/// argument implies that we already parsed the interface definition
2924/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002925llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2926 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002927 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002928 if (CachedTy)
2929 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002930 return DBuilder.createObjectPointerType(Ty);
2931}
2932
Eric Christophere7b87e52014-10-26 23:40:33 +00002933void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2934 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002935 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002936 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002938
Craig Topper8a13c412014-05-21 05:09:00 +00002939 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002940 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002941
Guy Benyei11169dd2012-12-18 14:30:41 +00002942 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002943
Guy Benyei11169dd2012-12-18 14:30:41 +00002944 uint64_t XOffset = 0;
2945 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2946 llvm::DIType Ty;
2947 if (isByRef)
2948 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002949 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 Ty = getOrCreateType(VD->getType(), Unit);
2951
2952 // Self is passed along as an implicit non-arg variable in a
2953 // block. Mark it as the object pointer.
2954 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002955 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002956
2957 // Get location information.
2958 unsigned Line = getLineNumber(VD->getLocation());
2959 unsigned Column = getColumnNumber(VD->getLocation());
2960
2961 const llvm::DataLayout &target = CGM.getDataLayout();
2962
2963 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002964 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2966
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002967 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002968 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002969 addr.push_back(llvm::dwarf::DW_OP_deref);
2970 addr.push_back(llvm::dwarf::DW_OP_plus);
2971 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 if (isByRef) {
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);
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002976 offset =
2977 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002978 addr.push_back(offset.getQuantity());
2979 addr.push_back(llvm::dwarf::DW_OP_deref);
2980 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 // offset of x field
2982 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002983 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002984 }
2985
2986 // Create the descriptor for the variable.
2987 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002988 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2989 llvm::DIDescriptor(LexicalBlockStack.back()),
2990 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002991
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002993 llvm::Instruction *Call = InsertPoint ?
2994 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2995 InsertPoint)
2996 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2997 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00002998 Call->setDebugLoc(
2999 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00003000}
3001
3002/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3003/// variable declaration.
3004void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3005 unsigned ArgNo,
3006 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003007 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003008 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3009}
3010
3011namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003012struct BlockLayoutChunk {
3013 uint64_t OffsetInBits;
3014 const BlockDecl::Capture *Capture;
3015};
3016bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3017 return l.OffsetInBits < r.OffsetInBits;
3018}
Guy Benyei11169dd2012-12-18 14:30:41 +00003019}
3020
3021void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003022 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003023 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003024 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003026 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 ASTContext &C = CGM.getContext();
3028 const BlockDecl *blockDecl = block.getBlockDecl();
3029
3030 // Collect some general information about the block's location.
3031 SourceLocation loc = blockDecl->getCaretLocation();
3032 llvm::DIFile tunit = getOrCreateFile(loc);
3033 unsigned line = getLineNumber(loc);
3034 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003035
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 // Build the debug-info type for the block literal.
3037 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3038
3039 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003040 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003041
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003042 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003043 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3044 blockLayout->getElementOffsetInBits(0),
3045 tunit, tunit));
3046 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3047 blockLayout->getElementOffsetInBits(1),
3048 tunit, tunit));
3049 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3050 blockLayout->getElementOffsetInBits(2),
3051 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003052 auto *FnTy = block.getBlockExpr()->getFunctionType();
3053 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3054 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 blockLayout->getElementOffsetInBits(3),
3056 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003057 fields.push_back(createFieldType(
3058 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3059 ? C.getBlockDescriptorExtendedType()
3060 : C.getBlockDescriptorType()),
3061 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003062
3063 // We want to sort the captures by offset, not because DWARF
3064 // requires this, but because we're paranoid about debuggers.
3065 SmallVector<BlockLayoutChunk, 8> chunks;
3066
3067 // 'this' capture.
3068 if (blockDecl->capturesCXXThis()) {
3069 BlockLayoutChunk chunk;
3070 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003071 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003072 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 chunks.push_back(chunk);
3074 }
3075
3076 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003077 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 const VarDecl *variable = capture.getVariable();
3079 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3080
3081 // Ignore constant captures.
3082 if (captureInfo.isConstant())
3083 continue;
3084
3085 BlockLayoutChunk chunk;
3086 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003087 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003088 chunk.Capture = &capture;
3089 chunks.push_back(chunk);
3090 }
3091
3092 // Sort by offset.
3093 llvm::array_pod_sort(chunks.begin(), chunks.end());
3094
Eric Christophere7b87e52014-10-26 23:40:33 +00003095 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3096 e = chunks.end();
3097 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 uint64_t offsetInBits = i->OffsetInBits;
3099 const BlockDecl::Capture *capture = i->Capture;
3100
3101 // If we have a null capture, this must be the C++ 'this' capture.
3102 if (!capture) {
3103 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003104 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003105 QualType type = method->getThisType(C);
3106
3107 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3108 offsetInBits, tunit, tunit));
3109 continue;
3110 }
3111
3112 const VarDecl *variable = capture->getVariable();
3113 StringRef name = variable->getName();
3114
3115 llvm::DIType fieldType;
3116 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003117 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003118
3119 // FIXME: this creates a second copy of this type!
3120 uint64_t xoffset;
3121 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003122 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3123 fieldType =
3124 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3125 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003126 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003127 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3128 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003129 }
3130 fields.push_back(fieldType);
3131 }
3132
3133 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003134 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3135 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003136
3137 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3138
3139 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003140 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3141 CGM.getContext().toBits(block.BlockSize),
3142 CGM.getContext().toBits(block.BlockAlign), 0,
3143 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3145
3146 // Get overall information about the block.
3147 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3148 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003149
3150 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003151 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3152 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3153 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3154 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003155
Adrian Prantl616bef42013-03-14 21:52:59 +00003156 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003157 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003158 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3159 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3160 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003161 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3162 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003163
Adrian Prantl616bef42013-03-14 21:52:59 +00003164 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003165 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3166 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003167 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003168}
3169
David Blaikie6943dea2013-08-20 01:28:15 +00003170/// If D is an out-of-class definition of a static data member of a class, find
3171/// its corresponding in-class declaration.
3172llvm::DIDerivedType
3173CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3174 if (!D->isStaticDataMember())
3175 return llvm::DIDerivedType();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003176 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003177 if (MI != StaticDataMemberCache.end()) {
3178 assert(MI->second && "Static data member declaration should still exist");
3179 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003180 }
David Blaikiece763042013-08-20 21:49:21 +00003181
3182 // If the member wasn't found in the cache, lazily construct and add it to the
3183 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003184 auto DC = D->getDeclContext();
3185 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3186 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003187}
3188
Eric Christophercab9fae2014-04-10 05:20:00 +00003189/// Recursively collect all of the member fields of a global anonymous decl and
3190/// create static variables for them. The first time this is called it needs
3191/// to be on a union and then from there we can have additional unnamed fields.
3192llvm::DIGlobalVariable
3193CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3194 unsigned LineNo, StringRef LinkageName,
3195 llvm::GlobalVariable *Var,
3196 llvm::DIDescriptor DContext) {
3197 llvm::DIGlobalVariable GV;
3198
3199 for (const auto *Field : RD->fields()) {
3200 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3201 StringRef FieldName = Field->getName();
3202
3203 // Ignore unnamed fields, but recurse into anonymous records.
3204 if (FieldName.empty()) {
3205 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3206 if (RT)
3207 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3208 Var, DContext);
3209 continue;
3210 }
3211 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003212 GV = DBuilder.createGlobalVariable(
3213 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3214 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003215 }
3216 return GV;
3217}
3218
Guy Benyei11169dd2012-12-18 14:30:41 +00003219/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003220void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003221 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003222 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003223 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003224 llvm::DIFile Unit;
3225 llvm::DIDescriptor DContext;
3226 unsigned LineNo;
3227 StringRef DeclName, LinkageName;
3228 QualType T;
3229 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003230
3231 // Attempt to store one global variable for the declaration - even if we
3232 // emit a lot of fields.
3233 llvm::DIGlobalVariable GV;
3234
3235 // If this is an anonymous union then we'll want to emit a global
3236 // variable for each member of the anonymous union so that it's possible
3237 // to find the name of any field in the union.
3238 if (T->isUnionType() && DeclName.empty()) {
3239 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003240 assert(RD->isAnonymousStructOrUnion() &&
3241 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003242 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3243 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003244 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003245 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3246 Var->hasInternalLinkage(), Var,
3247 getOrCreateStaticDataMemberDeclarationOrNull(D));
3248 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003249 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003250}
3251
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003252/// EmitGlobalVariable - Emit global variable's debug info.
3253void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3254 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003255 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003256 // Create the descriptor for the variable.
3257 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3258 StringRef Name = VD->getName();
3259 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3260 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3261 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3262 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3263 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3264 }
3265 // Do not use DIGlobalVariable for enums.
3266 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3267 return;
David Blaikiea15565562014-04-04 20:56:17 +00003268 // Do not emit separate definitions for function local const/statics.
3269 if (isa<FunctionDecl>(VD->getDeclContext()))
3270 return;
David Blaikiebb113912014-04-05 07:23:17 +00003271 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003272 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003273 if (VarD->isStaticDataMember()) {
3274 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3275 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003276 // Ensure that the type is retained even though it's otherwise unreferenced.
3277 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003278 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003279 return;
3280 }
3281
David Blaikieaf080852014-11-21 00:20:58 +00003282 llvm::DIDescriptor DContext =
3283 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3284
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003285 auto &GV = DeclCache[VD];
3286 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003287 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003288 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003289 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003290 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003291}
3292
3293llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3294 if (!LexicalBlockStack.empty())
3295 return llvm::DIScope(LexicalBlockStack.back());
3296 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003297}
3298
David Blaikie9f88fe82013-04-22 06:13:21 +00003299void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003300 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3301 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003302 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003303 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3304 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003305 getLineNumber(UD.getLocation()));
3306}
3307
David Blaikiebd483762013-05-20 04:58:53 +00003308void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3309 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3310 return;
3311 assert(UD.shadow_size() &&
3312 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3313 // Emitting one decl is sufficient - debuggers can detect that this is an
3314 // overloaded name & provide lookup for all the overloads.
3315 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003316 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003317 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003318 DBuilder.createImportedDeclaration(
3319 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3320 getLineNumber(USD.getLocation()));
3321}
3322
David Blaikief121b932013-05-20 22:50:41 +00003323llvm::DIImportedEntity
3324CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3325 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003326 return llvm::DIImportedEntity(nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003327 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003328 if (VH)
3329 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003330 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003331 if (const NamespaceAliasDecl *Underlying =
3332 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3333 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003334 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003335 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3336 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3337 NA.getName());
3338 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003339 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003340 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3341 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3342 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003343 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003344 return R;
3345}
3346
Guy Benyei11169dd2012-12-18 14:30:41 +00003347/// getOrCreateNamesSpace - Return namespace descriptor for the given
3348/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003349llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003350CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003351 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003352 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 if (I != NameSpaceCache.end())
3354 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003355
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3357 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003358 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003359 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3360 llvm::DINameSpace NS =
3361 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003362 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 return NS;
3364}
3365
3366void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003367 // Creating types might create further types - invalidating the current
3368 // element and the size(), so don't cache/reference them.
3369 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3370 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3371 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3372 E.Type->getDecl()->getDefinition()
3373 ? CreateTypeDefinition(E.Type, E.Unit)
3374 : E.Decl);
3375 }
3376
David Blaikief427b002014-05-06 03:42:01 +00003377 for (auto p : ReplaceMap) {
3378 assert(p.second);
3379 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003380 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003381
David Blaikief427b002014-05-06 03:42:01 +00003382 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003383 assert(it != TypeCache.end());
3384 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003385
David Blaikief427b002014-05-06 03:42:01 +00003386 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3387 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003389
Frederic Rissd253ed62014-11-18 03:40:51 +00003390 for (const auto &p : FwdDeclReplaceMap) {
3391 assert(p.second);
3392 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003393 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003394
3395 auto it = DeclCache.find(p.first);
3396 // If there has been no definition for the declaration, call RAUV
3397 // with ourselves, that will destroy the temporary MDNode and
3398 // replace it with a standard one, avoiding leaking memory.
3399 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003400 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003401 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003402 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003403
Frederic Rissd253ed62014-11-18 03:40:51 +00003404 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003405 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003406 }
3407
Adrian Prantl73409ce2013-03-11 18:33:46 +00003408 // We keep our own list of retained types, because we need to look
3409 // up the final type in the type cache.
3410 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3411 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003412 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003413
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 DBuilder.finalize();
3415}
David Blaikie66088d52014-09-24 17:01:27 +00003416
3417void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3418 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3419 return;
3420 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3421 // Don't ignore in case of explicit cast where it is referenced indirectly.
3422 DBuilder.retainType(DieTy);
3423}