blob: 42d5c3613be59ca47d06fe02324ef3cb0cc53f00 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000040#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041using namespace clang;
42using namespace clang::CodeGen;
43
44CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000045 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
46 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000047 CreateCompileUnit();
48}
49
50CGDebugInfo::~CGDebugInfo() {
51 assert(LexicalBlockStack.empty() &&
52 "Region stack mismatch, stack not empty!");
53}
54
David Blaikie66e41972015-01-14 07:38:27 +000055ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000056 SourceLocation TemporaryLocation)
David Blaikie66e41972015-01-14 07:38:27 +000057 : CGF(CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000058 init(TemporaryLocation);
59}
60
Adrian Prantl39428e72015-02-03 18:40:42 +000061ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000062 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000063 SourceLocation TemporaryLocation)
64 : CGF(CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000065 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000066}
67
68void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000069 bool DefaultToEmpty) {
David Blaikie66e41972015-01-14 07:38:27 +000070 if (auto *DI = CGF.getDebugInfo()) {
71 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Adrian Prantl39428e72015-02-03 18:40:42 +000072 if (TemporaryLocation.isInvalid()) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000073 if (DefaultToEmpty)
Adrian Prantl39428e72015-02-03 18:40:42 +000074 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc());
75 else {
76 // Construct a location that has a valid scope, but no line info.
77 assert(!DI->LexicalBlockStack.empty());
78 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
79 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
80 }
81 } else
David Blaikie835afb22015-01-21 23:08:17 +000082 DI->EmitLocation(CGF.Builder, TemporaryLocation);
David Blaikie66e41972015-01-14 07:38:27 +000083 }
Adrian Prantl2e0637f2013-07-18 00:28:02 +000084}
85
David Blaikie9b479662015-01-25 01:19:10 +000086ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
87 : CGF(CGF) {
88 init(E->getExprLoc());
89}
90
David Blaikie66e41972015-01-14 07:38:27 +000091ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
92 : CGF(CGF) {
93 if (CGF.getDebugInfo()) {
94 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
95 if (!Loc.isUnknown())
Benjamin Kramer03278662015-02-07 13:15:54 +000096 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +000097 }
98}
99
100ApplyDebugLocation::~ApplyDebugLocation() {
101 // Query CGF so the location isn't overwritten when location updates are
102 // temporarily disabled (for C++ default function arguments)
103 if (CGF.getDebugInfo())
Benjamin Kramer03278662015-02-07 13:15:54 +0000104 CGF.Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000105}
106
107/// ArtificialLocation - An RAII object that temporarily switches to
108/// an artificial debug location that has a valid scope, but no line
Guy Benyei11169dd2012-12-18 14:30:41 +0000109void CGDebugInfo::setLocation(SourceLocation Loc) {
110 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000111 if (Loc.isInvalid())
112 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000113
114 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
115
116 // If we've changed files in the middle of a lexical scope go ahead
117 // and create a new lexical scope with file node if it's different
118 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000119 if (LexicalBlockStack.empty())
120 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000121
122 SourceManager &SM = CGM.getContext().getSourceManager();
David Blaikieaabde052014-05-14 00:29:00 +0000123 llvm::DIScope Scope(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000124 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
David Blaikieaabde052014-05-14 00:29:00 +0000126 if (PCLoc.isInvalid() || Scope.getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 return;
128
Guy Benyei11169dd2012-12-18 14:30:41 +0000129 if (Scope.isLexicalBlockFile()) {
David Blaikieaabde052014-05-14 00:29:00 +0000130 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(Scope);
Eric Christophere7b87e52014-10-26 23:40:33 +0000131 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
132 LBF.getScope(), 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);
David Blaikie0a21d0d2013-01-26 22:16:26 +0000136 } else if (Scope.isLexicalBlock() || Scope.isSubprogram()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000137 llvm::DIDescriptor D =
138 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 llvm::MDNode *N = D;
140 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000141 LexicalBlockStack.emplace_back(N);
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 }
143}
144
145/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000146llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 if (!Context)
148 return TheCU;
149
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000150 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000152 llvm::Metadata *V = I->second;
David Blaikiebfa52742013-04-19 06:56:38 +0000153 return llvm::DIScope(dyn_cast_or_null<llvm::MDNode>(V));
Guy Benyei11169dd2012-12-18 14:30:41 +0000154 }
155
156 // Check namespace.
157 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000158 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000159
David Blaikiebfa52742013-04-19 06:56:38 +0000160 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
161 if (!RDecl->isDependentType())
162 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000163 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000164 return TheCU;
165}
166
167/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000168/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000169/// is stored on the side.
170StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000171 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000172 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000173 FunctionTemplateSpecializationInfo *Info =
174 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000175 if (!Info && FII)
176 return FII->getName();
177
178 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000179 SmallString<128> NS;
180 llvm::raw_svector_ostream OS(NS);
181 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000182
183 // Add any template specialization args.
184 if (Info) {
185 const TemplateArgumentList *TArgs = Info->TemplateArguments;
186 const TemplateArgument *Args = TArgs->data();
187 unsigned NumArgs = TArgs->size();
188 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000189 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
190 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000191 }
192
193 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000194 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000195}
196
197StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
198 SmallString<256> MethodName;
199 llvm::raw_svector_ostream OS(MethodName);
200 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
201 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000202 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000203 dyn_cast<const ObjCImplementationDecl>(DC)) {
204 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000205 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000206 dyn_cast<const ObjCInterfaceDecl>(DC)) {
207 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000208 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000209 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
210 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
211 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000212 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000213 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000215 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000216 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000217 ClassTy.print(OS, PrintingPolicy(LangOptions()));
218 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000219 }
220 OS << ' ' << OMD->getSelector().getAsString() << ']';
221
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000222 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000223}
224
225/// getSelectorName - Return selector name. This is used for debugging
226/// info.
227StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000228 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000229}
230
231/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000232StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000233 // quick optimization to avoid having to intern strings that are already
234 // stored reliably elsewhere
235 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000236 return RD->getName();
237
David Blaikie65813a32014-04-02 18:21:09 +0000238 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 {
David Blaikie65813a32014-04-02 18:21:09 +0000240 llvm::raw_svector_ostream OS(Name);
241 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
242 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000243 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000244
245 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000246 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000247}
248
249/// getOrCreateFile - Get the file debug info descriptor for the input location.
250llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
251 if (!Loc.isValid())
252 // If Location is not valid then use main input file.
253 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
254
255 SourceManager &SM = CGM.getContext().getSourceManager();
256 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
257
258 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
259 // If the location is not valid then use main input file.
260 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
261
262 // Cache the results.
263 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000264 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000265
266 if (it != DIFileCache.end()) {
267 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000268 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +0000269 return llvm::DIFile(cast<llvm::MDNode>(V));
270 }
271
272 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
273
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000274 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000275 return F;
276}
277
278/// getOrCreateMainFile - Get the file info for main compile unit.
279llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
280 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
281}
282
283/// getLineNumber - Get line number for the location. If location is invalid
284/// then use current location.
285unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
286 if (Loc.isInvalid() && CurLoc.isInvalid())
287 return 0;
288 SourceManager &SM = CGM.getContext().getSourceManager();
289 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000290 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000291}
292
293/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000294unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000295 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000296 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000297 return 0;
298
299 // If the location is invalid then use the current column.
300 if (Loc.isInvalid() && CurLoc.isInvalid())
301 return 0;
302 SourceManager &SM = CGM.getContext().getSourceManager();
303 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000304 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
307StringRef CGDebugInfo::getCurrentDirname() {
308 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
309 return CGM.getCodeGenOpts().DebugCompilationDir;
310
311 if (!CWDName.empty())
312 return CWDName;
313 SmallString<256> CWD;
314 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000315 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000316}
317
318/// CreateCompileUnit - Create new compile unit.
319void CGDebugInfo::CreateCompileUnit() {
320
David Blaikieaabde052014-05-14 00:29:00 +0000321 // Should we be asking the SourceManager for the main file name, instead of
322 // accepting it as an argument? This just causes the main file name to
323 // mismatch with source locations and create extra lexical scopes or
324 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
325 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
326 // because that's what the SourceManager says)
327
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 // Get absolute path name.
329 SourceManager &SM = CGM.getContext().getSourceManager();
330 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
331 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000332 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000333
334 // The main file name provided via the "-main-file-name" option contains just
335 // the file name itself with no path information. This file name may have had
336 // a relative path, so we look into the actual file entry for the main
337 // file to determine the real absolute path for the file.
338 std::string MainFileDir;
339 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
340 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000341 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000342 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
343 llvm::sys::path::append(MainFileDirSS, MainFileName);
344 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000345 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000346 }
347
348 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000349 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000350
351 // Save split dwarf file string.
352 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000353 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000354
Ed Masteda706022014-05-07 12:49:30 +0000355 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000356 const LangOptions &LO = CGM.getLangOpts();
357 if (LO.CPlusPlus) {
358 if (LO.ObjC1)
359 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
360 else
361 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
362 } else if (LO.ObjC1) {
363 LangTag = llvm::dwarf::DW_LANG_ObjC;
364 } else if (LO.C99) {
365 LangTag = llvm::dwarf::DW_LANG_C99;
366 } else {
367 LangTag = llvm::dwarf::DW_LANG_C89;
368 }
369
370 std::string Producer = getClangFullVersion();
371
372 // Figure out which version of the ObjC runtime we have.
373 unsigned RuntimeVers = 0;
374 if (LO.ObjC1)
375 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
376
377 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000379 TheCU = DBuilder.createCompileUnit(
380 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
381 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000382 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000383 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000384 : llvm::DIBuilder::FullDebug,
385 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000386}
387
388/// CreateType - Get the Basic type from the cache or create a new
389/// one if necessary.
390llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000391 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000392 StringRef BTName;
393 switch (BT->getKind()) {
394#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000395#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000396#include "clang/AST/BuiltinTypes.def"
397 case BuiltinType::Dependent:
398 llvm_unreachable("Unexpected builtin type");
399 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000400 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 case BuiltinType::Void:
402 return llvm::DIType();
403 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000404 if (!ClassTy)
405 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
406 "objc_class", TheCU,
407 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return ClassTy;
409 case BuiltinType::ObjCId: {
410 // typedef struct objc_class *Class;
411 // typedef struct objc_object {
412 // Class isa;
413 // } *id;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 return ObjTy;
417
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000418 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
420 "objc_class", TheCU,
421 getOrCreateMainFile(), 0);
422
423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000424
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
426
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000427 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000428 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
429 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000430
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000431 DBuilder.replaceArrays(
432 ObjTy,
433 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
434 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return ObjTy;
436 }
437 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000438 if (!SelTy)
439 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
440 "objc_selector", TheCU,
441 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 return SelTy;
443 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444
445 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000448 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449 OCLImage1dArrayDITy);
450 case BuiltinType::OCLImage1dBuffer:
451 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
452 OCLImage1dBufferDITy);
453 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000455 case BuiltinType::OCLImage2dArray:
456 return getOrCreateStructPtrType("opencl_image2d_array_t",
457 OCLImage2dArrayDITy);
458 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000460 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 return DBuilder.createBasicType(
462 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
463 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000464 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000466
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::Char_U:
469 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 case BuiltinType::SChar:
473 Encoding = llvm::dwarf::DW_ATE_signed_char;
474 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000475 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::Char32:
477 Encoding = llvm::dwarf::DW_ATE_UTF;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 case BuiltinType::UShort:
480 case BuiltinType::UInt:
481 case BuiltinType::UInt128:
482 case BuiltinType::ULong:
483 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::ULongLong:
485 Encoding = llvm::dwarf::DW_ATE_unsigned;
486 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 case BuiltinType::Short:
488 case BuiltinType::Int:
489 case BuiltinType::Int128:
490 case BuiltinType::Long:
491 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000492 case BuiltinType::LongLong:
493 Encoding = llvm::dwarf::DW_ATE_signed;
494 break;
495 case BuiltinType::Bool:
496 Encoding = llvm::dwarf::DW_ATE_boolean;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::Half:
499 case BuiltinType::Float:
500 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000501 case BuiltinType::Double:
502 Encoding = llvm::dwarf::DW_ATE_float;
503 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 }
505
506 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000507 case BuiltinType::Long:
508 BTName = "long int";
509 break;
510 case BuiltinType::LongLong:
511 BTName = "long long int";
512 break;
513 case BuiltinType::ULong:
514 BTName = "long unsigned int";
515 break;
516 case BuiltinType::ULongLong:
517 BTName = "long long unsigned int";
518 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000519 default:
520 BTName = BT->getName(CGM.getLangOpts());
521 break;
522 }
523 // Bit size, align and offset of the type.
524 uint64_t Size = CGM.getContext().getTypeSize(BT);
525 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 return DbgTy;
528}
529
530llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
531 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000532 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000533 if (Ty->isComplexIntegerType())
534 Encoding = llvm::dwarf::DW_ATE_lo_user;
535
536 uint64_t Size = CGM.getContext().getTypeSize(Ty);
537 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000538 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000540
541 return DbgTy;
542}
543
544/// CreateCVRType - Get the qualified type from the cache or create
545/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000546llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000547 QualifierCollector Qc;
548 const Type *T = Qc.strip(Ty);
549
550 // Ignore these qualifiers for now.
551 Qc.removeObjCGCAttr();
552 Qc.removeAddressSpace();
553 Qc.removeObjCLifetime();
554
555 // We will create one Derived type for one qualifier and recurse to handle any
556 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000557 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000558 if (Qc.hasConst()) {
559 Tag = llvm::dwarf::DW_TAG_const_type;
560 Qc.removeConst();
561 } else if (Qc.hasVolatile()) {
562 Tag = llvm::dwarf::DW_TAG_volatile_type;
563 Qc.removeVolatile();
564 } else if (Qc.hasRestrict()) {
565 Tag = llvm::dwarf::DW_TAG_restrict_type;
566 Qc.removeRestrict();
567 } else {
568 assert(Qc.empty() && "Unknown type qualifier for debug info");
569 return getOrCreateType(QualType(T, 0), Unit);
570 }
571
David Blaikie99dab3b2013-09-04 22:03:57 +0000572 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000573
574 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
575 // CVR derived types.
576 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000577
Guy Benyei11169dd2012-12-18 14:30:41 +0000578 return DbgTy;
579}
580
581llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
582 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000583
584 // The frontend treats 'id' as a typedef to an ObjCObjectType,
585 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
586 // debug info, we want to emit 'id' in both cases.
587 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000588 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000589
Eric Christophere7b87e52014-10-26 23:40:33 +0000590 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
591 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 return DbgTy;
593}
594
Eric Christophere7b87e52014-10-26 23:40:33 +0000595llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000596 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000597 Ty->getPointeeType(), Unit);
598}
599
Manman Rene0064d82013-08-29 23:19:58 +0000600/// In C++ mode, types have linkage, so we can rely on the ODR and
601/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000602static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
603 CodeGenModule &CGM,
604 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000605 SmallString<256> FullName;
606 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
607 // For now, only apply ODR with C++.
608 const TagDecl *TD = Ty->getDecl();
609 if (TheCU.getLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
610 !TD->isExternallyVisible())
611 return FullName;
612 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
613 if (CGM.getTarget().getCXXABI().isMicrosoft())
614 return FullName;
615
616 // TODO: This is using the RTTI name. Is there a better way to get
617 // a unique string for a type?
618 llvm::raw_svector_ostream Out(FullName);
619 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
620 Out.flush();
621 return FullName;
622}
623
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000624static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
625 llvm::dwarf::Tag Tag;
626 if (RD->isStruct() || RD->isInterface())
627 Tag = llvm::dwarf::DW_TAG_structure_type;
628 else if (RD->isUnion())
629 Tag = llvm::dwarf::DW_TAG_union_type;
630 else {
631 // FIXME: This could be a struct type giving a default visibility different
632 // than C++ class type, but needs llvm metadata changes first.
633 assert(RD->isClass());
634 Tag = llvm::dwarf::DW_TAG_class_type;
635 }
636 return Tag;
637}
638
Guy Benyei11169dd2012-12-18 14:30:41 +0000639// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000640llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000641CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000642 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000643 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000644 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000645 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000646 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
647 unsigned Line = getLineNumber(RD->getLocation());
648 StringRef RDName = getClassName(RD);
649
Guy Benyei11169dd2012-12-18 14:30:41 +0000650
651 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000652 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000653 llvm::DICompositeType RetTy = DBuilder.createReplaceableCompositeType(
654 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, 0, 0,
655 llvm::DIDescriptor::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000656 ReplaceMap.emplace_back(
657 std::piecewise_construct, std::make_tuple(Ty),
658 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000659 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000660}
661
Ed Masteda706022014-05-07 12:49:30 +0000662llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000663 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000664 QualType PointeeTy,
665 llvm::DIFile Unit) {
666 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
667 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000668 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000669
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 // Bit size, align and offset of the type.
671 // Size is always the size of a pointer. We can't use getTypeSize here
672 // because that does not return the correct value for references.
673 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000674 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
676
David Blaikie99dab3b2013-09-04 22:03:57 +0000677 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
678 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000679}
680
Eric Christopher0fdcb312013-05-16 00:52:20 +0000681llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
682 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000683 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000684 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000685 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
686 TheCU, getOrCreateMainFile(), 0);
687 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
688 Cache = DBuilder.createPointerType(Cache, Size);
689 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000690}
691
Guy Benyei11169dd2012-12-18 14:30:41 +0000692llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
693 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000694 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 return BlockLiteralGeneric;
696
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000697 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000698 llvm::DIType FieldTy;
699 QualType FType;
700 uint64_t FieldSize, FieldOffset;
701 unsigned FieldAlign;
702 llvm::DIArray Elements;
703 llvm::DIType EltTy, DescTy;
704
705 FieldOffset = 0;
706 FType = CGM.getContext().UnsignedLongTy;
707 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
708 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
709
710 Elements = DBuilder.getOrCreateArray(EltTys);
711 EltTys.clear();
712
713 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
714 unsigned LineNo = getLineNumber(CurLoc);
715
Eric Christophere7b87e52014-10-26 23:40:33 +0000716 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
717 FieldOffset, 0, Flags, llvm::DIType(),
718 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000719
720 // Bit size, align and offset of the type.
721 uint64_t Size = CGM.getContext().getTypeSize(Ty);
722
723 DescTy = DBuilder.createPointerType(EltTy, Size);
724
725 FieldOffset = 0;
726 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
727 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
728 FType = CGM.getContext().IntTy;
729 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
730 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000731 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
733
734 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
735 FieldTy = DescTy;
736 FieldSize = CGM.getContext().getTypeSize(Ty);
737 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000738 FieldTy =
739 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
740 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 EltTys.push_back(FieldTy);
742
743 FieldOffset += FieldSize;
744 Elements = DBuilder.getOrCreateArray(EltTys);
745
Eric Christophere7b87e52014-10-26 23:40:33 +0000746 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
747 LineNo, FieldOffset, 0, Flags,
748 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000749
Guy Benyei11169dd2012-12-18 14:30:41 +0000750 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
751 return BlockLiteralGeneric;
752}
753
Eric Christophere7b87e52014-10-26 23:40:33 +0000754llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
755 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000756 assert(Ty->isTypeAlias());
757 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000758
759 SmallString<128> NS;
760 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
762 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000763
764 TemplateSpecializationType::PrintTemplateArgumentList(
765 OS, Ty->getArgs(), Ty->getNumArgs(),
766 CGM.getContext().getPrintingPolicy());
767
Eric Christophere7b87e52014-10-26 23:40:33 +0000768 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
769 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000770
771 SourceLocation Loc = AliasDecl->getLocation();
772 llvm::DIFile File = getOrCreateFile(Loc);
773 unsigned Line = getLineNumber(Loc);
774
Eric Christophere7b87e52014-10-26 23:40:33 +0000775 llvm::DIDescriptor Ctxt =
776 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000777
778 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
779}
780
David Blaikie99dab3b2013-09-04 22:03:57 +0000781llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000782 // Typedefs are derived from some other type. If we have a typedef of a
783 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000784 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000785 // We don't set size information, but do specify where the typedef was
786 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000787 SourceLocation Loc = Ty->getDecl()->getLocation();
788 llvm::DIFile File = getOrCreateFile(Loc);
789 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000790 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000791
Guy Benyei11169dd2012-12-18 14:30:41 +0000792 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000793 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000794
Eric Christophere7b87e52014-10-26 23:40:33 +0000795 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
796 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000797}
798
799llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
800 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000801 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000802
803 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000804 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000805
806 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000807 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 if (isa<FunctionNoProtoType>(Ty))
809 EltTys.push_back(DBuilder.createUnspecifiedParameter());
810 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000811 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
812 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000813 if (FPT->isVariadic())
814 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000815 }
816
Manman Ren67f005e2014-07-28 22:24:34 +0000817 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000818 return DBuilder.createSubroutineType(Unit, EltTypeArray);
819}
820
Adrian Prantl21361fb2014-08-29 22:44:27 +0000821/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
822/// As an optimization, return 0 if the access specifier equals the
823/// default for the containing type.
824static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
825 AccessSpecifier Default = clang::AS_none;
826 if (RD && RD->isClass())
827 Default = clang::AS_private;
828 else if (RD && (RD->isStruct() || RD->isUnion()))
829 Default = clang::AS_public;
830
831 if (Access == Default)
832 return 0;
833
Eric Christophere7b87e52014-10-26 23:40:33 +0000834 switch (Access) {
835 case clang::AS_private:
836 return llvm::DIDescriptor::FlagPrivate;
837 case clang::AS_protected:
838 return llvm::DIDescriptor::FlagProtected;
839 case clang::AS_public:
840 return llvm::DIDescriptor::FlagPublic;
841 case clang::AS_none:
842 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000843 }
844 llvm_unreachable("unexpected access enumerator");
845}
Guy Benyei11169dd2012-12-18 14:30:41 +0000846
Eric Christophere7b87e52014-10-26 23:40:33 +0000847llvm::DIType CGDebugInfo::createFieldType(
848 StringRef name, QualType type, uint64_t sizeInBitsOverride,
849 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
850 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000851 llvm::DIType debugType = getOrCreateType(type, tunit);
852
853 // Get the location for the field.
854 llvm::DIFile file = getOrCreateFile(loc);
855 unsigned line = getLineNumber(loc);
856
David Majnemer34b57492014-07-30 01:30:47 +0000857 uint64_t SizeInBits = 0;
858 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000860 TypeInfo TI = CGM.getContext().getTypeInfo(type);
861 SizeInBits = TI.Width;
862 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000863
864 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000865 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000866 }
867
Adrian Prantl21361fb2014-08-29 22:44:27 +0000868 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000869 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
870 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000871}
872
Eric Christopher91a31902013-01-16 01:22:32 +0000873/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000874void CGDebugInfo::CollectRecordLambdaFields(
875 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
876 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000877 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
878 // has the name and the location of the variable so we should iterate over
879 // both concurrently.
880 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
881 RecordDecl::field_iterator Field = CXXDecl->field_begin();
882 unsigned fieldno = 0;
883 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000884 E = CXXDecl->captures_end();
885 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000886 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000887 if (C.capturesVariable()) {
888 VarDecl *V = C.getCapturedVar();
889 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
890 StringRef VName = V->getName();
891 uint64_t SizeInBitsOverride = 0;
892 if (Field->isBitField()) {
893 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
894 assert(SizeInBitsOverride && "found named 0-width bitfield");
895 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000896 llvm::DIType fieldType = createFieldType(
897 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
898 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
899 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000900 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000901 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000902 // TODO: Need to handle 'this' in some way by probably renaming the
903 // this of the lambda class and having a field member of 'this' or
904 // by using AT_object_pointer for the function and having that be
905 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000906 FieldDecl *f = *Field;
907 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
908 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000909 llvm::DIType fieldType = createFieldType(
910 "this", type, 0, f->getLocation(), f->getAccess(),
911 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000912
913 elements.push_back(fieldType);
914 }
915 }
916}
917
David Blaikie6943dea2013-08-20 01:28:15 +0000918/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000919llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
920 llvm::DIType RecordTy,
921 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000922 // Create the descriptor for the static variable, with or without
923 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000924 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000925 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
926 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
927
Eric Christopher91a31902013-01-16 01:22:32 +0000928 unsigned LineNumber = getLineNumber(Var->getLocation());
929 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000930 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000931 if (Var->getInit()) {
932 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000933 if (Value) {
934 if (Value->isInt())
935 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
936 if (Value->isFloat())
937 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
938 }
Eric Christopher91a31902013-01-16 01:22:32 +0000939 }
940
Adrian Prantl21361fb2014-08-29 22:44:27 +0000941 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000942 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
943 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000944 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000945 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000946}
947
948/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000949void CGDebugInfo::CollectRecordNormalField(
950 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000951 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000952 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000953 StringRef name = field->getName();
954 QualType type = field->getType();
955
956 // Ignore unnamed fields unless they're anonymous structs/unions.
957 if (name.empty() && !type->isRecordType())
958 return;
959
960 uint64_t SizeInBitsOverride = 0;
961 if (field->isBitField()) {
962 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
963 assert(SizeInBitsOverride && "found named 0-width bitfield");
964 }
965
Eric Christophere7b87e52014-10-26 23:40:33 +0000966 llvm::DIType fieldType =
967 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
968 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000969
970 elements.push_back(fieldType);
971}
972
Guy Benyei11169dd2012-12-18 14:30:41 +0000973/// CollectRecordFields - A helper function to collect debug info for
974/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000975void CGDebugInfo::CollectRecordFields(
976 const RecordDecl *record, llvm::DIFile tunit,
977 SmallVectorImpl<llvm::Metadata *> &elements,
978 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000979 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
980
Eric Christopher91a31902013-01-16 01:22:32 +0000981 if (CXXDecl && CXXDecl->isLambda())
982 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
983 else {
984 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000985
Eric Christopher91a31902013-01-16 01:22:32 +0000986 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000987 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000988
Eric Christopher91a31902013-01-16 01:22:32 +0000989 // Static and non-static members should appear in the same order as
990 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000991 for (const auto *I : record->decls())
992 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000993 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000994 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000995 if (MI != StaticDataMemberCache.end()) {
996 assert(MI->second &&
997 "Static data member declaration should still exist");
998 elements.push_back(
999 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00001000 } else {
1001 auto Field = CreateRecordStaticField(V, RecordTy, record);
1002 elements.push_back(Field);
1003 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001004 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001005 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1006 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001007
1008 // Bump field number for next field.
1009 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 }
1012}
1013
1014/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1015/// function type is not updated to include implicit "this" pointer. Use this
1016/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001017llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001018CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1019 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001020 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001021 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001022 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001023 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1024 Func, Unit);
1025}
David Blaikie2aaf0652013-01-07 22:24:59 +00001026
David Blaikie469f0792013-05-22 23:22:42 +00001027llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001028 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001029 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001030 llvm::DITypeArray Args = llvm::DISubroutineType(
1031 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001032 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001033
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001034 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001035
1036 // First element is always return type. For 'void' functions it is NULL.
1037 Elts.push_back(Args.getElement(0));
1038
David Blaikie2aaf0652013-01-07 22:24:59 +00001039 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001040 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001041 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1042 // Create pointer type directly in this case.
1043 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1044 QualType PointeeTy = ThisPtrTy->getPointeeType();
1045 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001046 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001047 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1048 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001049 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001050 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001051 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001052 // TODO: This and the artificial type below are misleading, the
1053 // types aren't artificial the argument is, but the current
1054 // metadata doesn't represent that.
1055 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1056 Elts.push_back(ThisPtrType);
1057 } else {
1058 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001059 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001060 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1061 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 }
1063
1064 // Copy rest of the arguments.
1065 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1066 Elts.push_back(Args.getElement(i));
1067
Manman Ren67f005e2014-07-28 22:24:34 +00001068 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001069
Adrian Prantl0630eb72013-12-18 21:48:18 +00001070 unsigned Flags = 0;
1071 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1072 Flags |= llvm::DIDescriptor::FlagLValueReference;
1073 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1074 Flags |= llvm::DIDescriptor::FlagRValueReference;
1075
1076 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001077}
1078
Eric Christopherb2a008c2013-05-16 00:45:12 +00001079/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001080/// inside a function.
1081static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1082 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1083 return isFunctionLocalClass(NRD);
1084 if (isa<FunctionDecl>(RD->getDeclContext()))
1085 return true;
1086 return false;
1087}
1088
1089/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1090/// a single member function GlobalDecl.
1091llvm::DISubprogram
1092CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001093 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001094 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001095 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001096
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001098 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001099
1100 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1101 // make sense to give a single ctor/dtor a linkage name.
1102 StringRef MethodLinkageName;
1103 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1104 MethodLinkageName = CGM.getMangledName(Method);
1105
1106 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001107 llvm::DIFile MethodDefUnit;
1108 unsigned MethodLine = 0;
1109 if (!Method->isImplicit()) {
1110 MethodDefUnit = getOrCreateFile(Method->getLocation());
1111 MethodLine = getLineNumber(Method->getLocation());
1112 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001113
1114 // Collect virtual method info.
1115 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001116 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001117 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001118
Guy Benyei11169dd2012-12-18 14:30:41 +00001119 if (Method->isVirtual()) {
1120 if (Method->isPure())
1121 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1122 else
1123 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001124
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 // It doesn't make sense to give a virtual destructor a vtable index,
1126 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001127 // FIXME: Add proper support for debug info for virtual calls in
1128 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1129 // lookup if we have multiple or virtual inheritance.
1130 if (!isa<CXXDestructorDecl>(Method) &&
1131 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001132 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 ContainingType = RecordTy;
1134 }
1135
1136 unsigned Flags = 0;
1137 if (Method->isImplicit())
1138 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001139 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001140 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1141 if (CXXC->isExplicit())
1142 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001143 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001144 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001145 if (CXXC->isExplicit())
1146 Flags |= llvm::DIDescriptor::FlagExplicit;
1147 }
1148 if (Method->hasPrototype())
1149 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001150 if (Method->getRefQualifier() == RQ_LValue)
1151 Flags |= llvm::DIDescriptor::FlagLValueReference;
1152 if (Method->getRefQualifier() == RQ_RValue)
1153 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001154
1155 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001156 llvm::DISubprogram SP = DBuilder.createMethod(
1157 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1158 MethodTy, /*isLocalToUnit=*/false,
1159 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1160 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001161
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001162 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001163
1164 return SP;
1165}
1166
1167/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001168/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001169/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001170void CGDebugInfo::CollectCXXMemberFunctions(
1171 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001172 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001173
1174 // Since we want more than just the individual member decls if we
1175 // have templated functions iterate over every declaration to gather
1176 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001177 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001178 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1179 // If the member is implicit, don't add it to the member list. This avoids
1180 // the member being added to type units by LLVM, while still allowing it
1181 // to be emitted into the type declaration/reference inside the compile
1182 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001183 // FIXME: Handle Using(Shadow?)Decls here to create
1184 // DW_TAG_imported_declarations inside the class for base decls brought into
1185 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1186 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1187 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001188 if (!Method || Method->isImplicit())
1189 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001190
1191 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1192 continue;
1193
David Blaikiefd580722014-10-06 05:18:55 +00001194 // Reuse the existing member function declaration if it exists.
1195 // It may be associated with the declaration of the type & should be
1196 // reused as we're building the definition.
1197 //
1198 // This situation can arise in the vtable-based debug info reduction where
1199 // implicit members are emitted in a non-vtable TU.
1200 auto MI = SPCache.find(Method->getCanonicalDecl());
1201 EltTys.push_back(MI == SPCache.end()
1202 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001203 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001204 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001205}
Guy Benyei11169dd2012-12-18 14:30:41 +00001206
Guy Benyei11169dd2012-12-18 14:30:41 +00001207/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001208/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001209/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001210void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001211 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001212 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001213
1214 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 unsigned BFlags = 0;
1217 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001218
Guy Benyei11169dd2012-12-18 14:30:41 +00001219 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001220 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001221
Aaron Ballman574705e2014-03-13 15:41:46 +00001222 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001223 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1224 // virtual base offset offset is -ve. The code generator emits dwarf
1225 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001226 BaseOffset = 0 - CGM.getItaniumVTableContext()
1227 .getVirtualBaseOffsetOffset(RD, Base)
1228 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001229 } else {
1230 // In the MS ABI, store the vbtable offset, which is analogous to the
1231 // vbase offset offset in Itanium.
1232 BaseOffset =
1233 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1234 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 BFlags = llvm::DIDescriptor::FlagVirtual;
1236 } else
1237 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1238 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1239 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001240
Adrian Prantl21361fb2014-08-29 22:44:27 +00001241 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001242 llvm::DIType DTy = DBuilder.createInheritance(
1243 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 EltTys.push_back(DTy);
1245 }
1246}
1247
1248/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001249llvm::DIArray
1250CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1251 ArrayRef<TemplateArgument> TAList,
1252 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001253 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1255 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001256 StringRef Name;
1257 if (TPList)
1258 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001259 switch (TA.getKind()) {
1260 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001261 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1262 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001263 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001264 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001265 } break;
1266 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001267 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1268 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001269 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001270 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001271 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1272 TemplateParams.push_back(TVP);
1273 } break;
1274 case TemplateArgument::Declaration: {
1275 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001276 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001277 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001278 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001279 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001280 // Variable pointer template parameters have a value that is the address
1281 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001282 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001283 V = CGM.GetAddrOfGlobalVar(VD);
1284 // Member function pointers have special support for building them, though
1285 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001286 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001287 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001288 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001289 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001290 // Member data pointers have special handling too to compute the fixed
1291 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001292 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001293 // These five lines (& possibly the above member function pointer
1294 // handling) might be able to be refactored to use similar code in
1295 // CodeGenModule::getMemberPointerConstant
1296 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1297 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001298 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001299 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001300 }
1301 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001302 DBuilder.createTemplateValueParameter(
1303 TheCU, Name, TTy,
1304 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001305 TemplateParams.push_back(TVP);
1306 } break;
1307 case TemplateArgument::NullPtr: {
1308 QualType T = TA.getNullPtrType();
1309 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001310 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001311 // Special case member data pointer null values since they're actually -1
1312 // instead of zero.
1313 if (const MemberPointerType *MPT =
1314 dyn_cast<MemberPointerType>(T.getTypePtr()))
1315 // But treat member function pointers as simple zero integers because
1316 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1317 // CodeGen grows handling for values of non-null member function
1318 // pointers then perhaps we could remove this special case and rely on
1319 // EmitNullMemberPointer for member function pointers.
1320 if (MPT->isMemberDataPointer())
1321 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1322 if (!V)
1323 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1324 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001325 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1326 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001327 TemplateParams.push_back(TVP);
1328 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001329 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001330 llvm::DITemplateValueParameter
1331 TVP = DBuilder.createTemplateTemplateParameter(
1332 TheCU, Name, llvm::DIType(),
1333 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001334 TemplateParams.push_back(TVP);
1335 } break;
1336 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001337 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1338 TheCU, Name, llvm::DIType(),
1339 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001340 TemplateParams.push_back(TVP);
1341 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001342 case TemplateArgument::Expression: {
1343 const Expr *E = TA.getAsExpr();
1344 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001345 if (E->isGLValue())
1346 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001347 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001348 assert(V && "Expression in template argument isn't constant");
1349 llvm::DIType TTy = getOrCreateType(T, Unit);
1350 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001351 DBuilder.createTemplateValueParameter(
1352 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001353 TemplateParams.push_back(TVP);
1354 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001355 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001356 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001357 case TemplateArgument::Null:
1358 llvm_unreachable(
1359 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 }
1361 }
1362 return DBuilder.getOrCreateArray(TemplateParams);
1363}
1364
1365/// CollectFunctionTemplateParams - A helper function to collect debug
1366/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001367llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1368 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 if (FD->getTemplatedKind() ==
1370 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001371 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1372 ->getTemplate()
1373 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001374 return CollectTemplateParams(
1375 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001376 }
1377 return llvm::DIArray();
1378}
1379
1380/// CollectCXXTemplateParams - A helper function to collect debug info for
1381/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001382llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1383 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001384 // Always get the full list of parameters, not just the ones from
1385 // the specialization.
1386 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001387 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001388 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001389 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001390}
1391
1392/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1393llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1394 if (VTablePtrType.isValid())
1395 return VTablePtrType;
1396
1397 ASTContext &Context = CGM.getContext();
1398
1399 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001400 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001401 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1403 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001404 llvm::DIType vtbl_ptr_type =
1405 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1407 return VTablePtrType;
1408}
1409
1410/// getVTableName - Get vtable name for the given Class.
1411StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001412 // Copy the gdb compatible name on the side and use its reference.
1413 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001414}
1415
Guy Benyei11169dd2012-12-18 14:30:41 +00001416/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1417/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001418void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001419 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1421
1422 // If there is a primary base then it will hold vtable info.
1423 if (RL.getPrimaryBase())
1424 return;
1425
1426 // If this class is not dynamic then there is not any vtable info to collect.
1427 if (!RD->isDynamicClass())
1428 return;
1429
1430 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001431 llvm::DIType VPTR = DBuilder.createMemberType(
1432 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1433 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 EltTys.push_back(VPTR);
1435}
1436
Eric Christopherb2a008c2013-05-16 00:45:12 +00001437/// getOrCreateRecordType - Emit record type's standalone debug info.
1438llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001439 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001440 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001441 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1442 return T;
1443}
1444
1445/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1446/// debug info.
1447llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001448 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001449 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001451 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001452 return T;
1453}
1454
David Blaikie483a9da2014-05-06 18:35:21 +00001455void CGDebugInfo::completeType(const EnumDecl *ED) {
1456 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1457 return;
1458 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001459 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001460 auto I = TypeCache.find(TyPtr);
1461 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001462 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001463 return;
1464 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1465 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001466 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001467}
1468
David Blaikieb2e86eb2013-08-15 20:49:17 +00001469void CGDebugInfo::completeType(const RecordDecl *RD) {
1470 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1471 !CGM.getLangOpts().CPlusPlus)
1472 completeRequiredType(RD);
1473}
1474
1475void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001476 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1477 return;
1478
David Blaikie6943dea2013-08-20 01:28:15 +00001479 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1480 if (CXXDecl->isDynamicClass())
1481 return;
1482
David Blaikieb2e86eb2013-08-15 20:49:17 +00001483 QualType Ty = CGM.getContext().getRecordType(RD);
1484 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001485 if (T && T.isForwardDecl())
1486 completeClassData(RD);
1487}
1488
1489void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1490 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001491 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001492 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001493 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001494 auto I = TypeCache.find(TyPtr);
1495 if (I != TypeCache.end() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001496 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001497 return;
1498 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1499 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001500 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001501}
1502
David Blaikie0e716b42014-03-03 23:48:23 +00001503static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1504 CXXRecordDecl::method_iterator End) {
1505 for (; I != End; ++I)
1506 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001507 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1508 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001509 return true;
1510 return false;
1511}
1512
1513static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1514 const RecordDecl *RD,
1515 const LangOptions &LangOpts) {
1516 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1517 return false;
1518
1519 if (!LangOpts.CPlusPlus)
1520 return false;
1521
1522 if (!RD->isCompleteDefinitionRequired())
1523 return true;
1524
1525 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1526
1527 if (!CXXDecl)
1528 return false;
1529
1530 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1531 return true;
1532
1533 TemplateSpecializationKind Spec = TSK_Undeclared;
1534 if (const ClassTemplateSpecializationDecl *SD =
1535 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1536 Spec = SD->getSpecializationKind();
1537
1538 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1539 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1540 CXXDecl->method_end()))
1541 return true;
1542
1543 return false;
1544}
1545
Guy Benyei11169dd2012-12-18 14:30:41 +00001546/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001547llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001549 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001550 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001551 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001552 T = getOrCreateRecordFwdDecl(
1553 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001554 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001555 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001556
David Blaikieb2e86eb2013-08-15 20:49:17 +00001557 return CreateTypeDefinition(Ty);
1558}
1559
1560llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1561 RecordDecl *RD = Ty->getDecl();
1562
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 // Get overall information about the record type for the debug info.
1564 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1565
1566 // Records and classes and unions can all be recursive. To handle them, we
1567 // first generate a debug descriptor for the struct as a forward declaration.
1568 // Then (if it is a definition) we go through and get debug info for all of
1569 // its members. Finally, we create a descriptor for the complete type (which
1570 // may refer to the forward decl if the struct is recursive) and replace all
1571 // uses of the forward declaration with the final definition.
1572
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001573 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001574 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001575 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001576
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001577 const RecordDecl *D = RD->getDefinition();
1578 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 return FwdDecl;
1580
David Blaikieadfbf992013-08-18 16:55:33 +00001581 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1582 CollectContainingType(CXXDecl, FwdDecl);
1583
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001585 LexicalBlockStack.emplace_back(&*FwdDecl);
1586 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001587
Guy Benyei11169dd2012-12-18 14:30:41 +00001588 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001589 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001590 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001591
1592 // Note: The split of CXXDecl information here is intentional, the
1593 // gdb tests will depend on a certain ordering at printout. The debug
1594 // information offsets are still correct if we merge them all together
1595 // though.
1596 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1597 if (CXXDecl) {
1598 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1599 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1600 }
1601
Eric Christopher91a31902013-01-16 01:22:32 +00001602 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001604 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001605 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001606
1607 LexicalBlockStack.pop_back();
1608 RegionMap.erase(Ty->getDecl());
1609
1610 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001611 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001612
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001613 if (FwdDecl->isTemporary())
1614 FwdDecl = llvm::DICompositeType(llvm::MDNode::replaceWithPermanent(
1615 llvm::TempMDNode(FwdDecl.get())));
1616
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001617 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001618 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001619}
1620
1621/// CreateType - get objective-c object type.
1622llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1623 llvm::DIFile Unit) {
1624 // Ignore protocols.
1625 return getOrCreateType(Ty->getBaseType(), Unit);
1626}
1627
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001628/// \return true if Getter has the default name for the property PD.
1629static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1630 const ObjCMethodDecl *Getter) {
1631 assert(PD);
1632 if (!Getter)
1633 return true;
1634
1635 assert(Getter->getDeclName().isObjCZeroArgSelector());
1636 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001637 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001638}
1639
1640/// \return true if Setter has the default name for the property PD.
1641static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1642 const ObjCMethodDecl *Setter) {
1643 assert(PD);
1644 if (!Setter)
1645 return true;
1646
1647 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001648 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001649 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001650}
1651
Guy Benyei11169dd2012-12-18 14:30:41 +00001652/// CreateType - get objective-c interface type.
1653llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1654 llvm::DIFile Unit) {
1655 ObjCInterfaceDecl *ID = Ty->getDecl();
1656 if (!ID)
1657 return llvm::DIType();
1658
1659 // Get overall information about the record type for the debug info.
1660 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1661 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001662 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001663
1664 // If this is just a forward declaration return a special forward-declaration
1665 // debug type since we won't be able to lay out the entire type.
1666 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001667 if (!Def || !Def->getImplementation()) {
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001668 llvm::DIType FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001669 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1670 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001671 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001672 return FwdDecl;
1673 }
1674
David Blaikieef8a9512014-05-05 23:23:53 +00001675 return CreateTypeDefinition(Ty, Unit);
1676}
1677
Eric Christophere7b87e52014-10-26 23:40:33 +00001678llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1679 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001680 ObjCInterfaceDecl *ID = Ty->getDecl();
1681 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1682 unsigned Line = getLineNumber(ID->getLocation());
1683 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
1685 // Bit size, align and offset of the type.
1686 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1687 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1688
1689 unsigned Flags = 0;
1690 if (ID->getImplementation())
1691 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1692
Eric Christophere7b87e52014-10-26 23:40:33 +00001693 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1694 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1695 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696
David Blaikieef8a9512014-05-05 23:23:53 +00001697 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001698 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001699
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001700 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001701 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1702 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001703
1704 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001705 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001706
1707 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1708 if (SClass) {
1709 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001710 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 if (!SClassTy.isValid())
1712 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001713
Eric Christophere7b87e52014-10-26 23:40:33 +00001714 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 EltTys.push_back(InhTag);
1716 }
1717
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001718 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001719 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001720 SourceLocation Loc = PD->getLocation();
1721 llvm::DIFile PUnit = getOrCreateFile(Loc);
1722 unsigned PLine = getLineNumber(Loc);
1723 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1724 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001725 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1726 PD->getName(), PUnit, PLine,
1727 hasDefaultGetterName(PD, Getter) ? ""
1728 : getSelectorName(PD->getGetterName()),
1729 hasDefaultSetterName(PD, Setter) ? ""
1730 : getSelectorName(PD->getSetterName()),
1731 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001732 EltTys.push_back(PropertyNode);
1733 }
1734
1735 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1736 unsigned FieldNo = 0;
1737 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1738 Field = Field->getNextIvar(), ++FieldNo) {
1739 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1740 if (!FieldTy.isValid())
1741 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001742
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 StringRef FieldName = Field->getName();
1744
1745 // Ignore unnamed fields.
1746 if (FieldName.empty())
1747 continue;
1748
1749 // Get the location for the field.
1750 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1751 unsigned FieldLine = getLineNumber(Field->getLocation());
1752 QualType FType = Field->getType();
1753 uint64_t FieldSize = 0;
1754 unsigned FieldAlign = 0;
1755
1756 if (!FType->isIncompleteArrayType()) {
1757
1758 // Bit size, align and offset of the type.
1759 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001760 ? Field->getBitWidthValue(CGM.getContext())
1761 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001762 FieldAlign = CGM.getContext().getTypeAlign(FType);
1763 }
1764
1765 uint64_t FieldOffset;
1766 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1767 // We don't know the runtime offset of an ivar if we're using the
1768 // non-fragile ABI. For bitfields, use the bit offset into the first
1769 // byte of storage of the bitfield. For other fields, use zero.
1770 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001771 FieldOffset =
1772 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001773 FieldOffset %= CGM.getContext().getCharWidth();
1774 } else {
1775 FieldOffset = 0;
1776 }
1777 } else {
1778 FieldOffset = RL.getFieldOffset(FieldNo);
1779 }
1780
1781 unsigned Flags = 0;
1782 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1783 Flags = llvm::DIDescriptor::FlagProtected;
1784 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1785 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001786 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1787 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001788
Craig Topper8a13c412014-05-21 05:09:00 +00001789 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001791 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001792 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001794 SourceLocation Loc = PD->getLocation();
1795 llvm::DIFile PUnit = getOrCreateFile(Loc);
1796 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001797 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1798 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001799 PropertyNode = DBuilder.createObjCProperty(
1800 PD->getName(), PUnit, PLine,
1801 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1802 PD->getGetterName()),
1803 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1804 PD->getSetterName()),
1805 PD->getPropertyAttributes(),
1806 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 }
1808 }
1809 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001810 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1811 FieldSize, FieldAlign, FieldOffset, Flags,
1812 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001813 EltTys.push_back(FieldTy);
1814 }
1815
1816 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001817 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001818
Guy Benyei11169dd2012-12-18 14:30:41 +00001819 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001820 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001821}
1822
1823llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1824 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1825 int64_t Count = Ty->getNumElements();
1826 if (Count == 0)
1827 // If number of elements are not known then this is an unbounded array.
1828 // Use Count == -1 to express such arrays.
1829 Count = -1;
1830
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001831 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1833
1834 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1835 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1836
1837 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1838}
1839
Eric Christophere7b87e52014-10-26 23:40:33 +00001840llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 uint64_t Size;
1842 uint64_t Align;
1843
1844 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1845 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1846 Size = 0;
1847 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001848 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001849 } else if (Ty->isIncompleteArrayType()) {
1850 Size = 0;
1851 if (Ty->getElementType()->isIncompleteType())
1852 Align = 0;
1853 else
1854 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001855 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 Size = 0;
1857 Align = 0;
1858 } else {
1859 // Size and align of the whole array, not the element type.
1860 Size = CGM.getContext().getTypeSize(Ty);
1861 Align = CGM.getContext().getTypeAlign(Ty);
1862 }
1863
1864 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1865 // interior arrays, do we care? Why aren't nested arrays represented the
1866 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001867 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 QualType EltTy(Ty, 0);
1869 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1870 // If the number of elements is known, then count is that number. Otherwise,
1871 // it's -1. This allows us to represent a subrange with an array of 0
1872 // elements, like this:
1873 //
1874 // struct foo {
1875 // int x[0];
1876 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001877 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1879 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001880
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 // FIXME: Verify this is right for VLAs.
1882 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1883 EltTy = Ty->getElementType();
1884 }
1885
1886 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1887
Eric Christophere7b87e52014-10-26 23:40:33 +00001888 llvm::DIType DbgTy = DBuilder.createArrayType(
1889 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001890 return DbgTy;
1891}
1892
Eric Christopherb2a008c2013-05-16 00:45:12 +00001893llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001895 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1896 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001897}
1898
Eric Christopherb2a008c2013-05-16 00:45:12 +00001899llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001900 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001901 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1902 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001903}
1904
Eric Christopherb2a008c2013-05-16 00:45:12 +00001905llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001907 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1908 if (!Ty->getPointeeType()->isFunctionType())
1909 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001910 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001911 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001912
1913 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001914 Ty->getPointeeType()->getAs<FunctionProtoType>();
1915 return DBuilder.createMemberPointerType(
1916 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1917 Ty->getClass(), FPT->getTypeQuals())),
1918 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001919 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001920}
1921
Eric Christophere7b87e52014-10-26 23:40:33 +00001922llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001923 // Ignore the atomic wrapping
1924 // FIXME: What is the correct representation?
1925 return getOrCreateType(Ty->getValueType(), U);
1926}
1927
1928/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001929llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001930 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 uint64_t Size = 0;
1932 uint64_t Align = 0;
1933 if (!ED->getTypeForDecl()->isIncompleteType()) {
1934 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1935 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1936 }
1937
Manman Rene0064d82013-08-29 23:19:58 +00001938 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1939
Guy Benyei11169dd2012-12-18 14:30:41 +00001940 // If this is just a forward declaration, construct an appropriately
1941 // marked node and just return it.
1942 if (!ED->getDefinition()) {
1943 llvm::DIDescriptor EDContext;
1944 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1945 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1946 unsigned Line = getLineNumber(ED->getLocation());
1947 StringRef EDName = ED->getName();
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001948 llvm::DIType RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001949 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001950 0, Size, Align, llvm::DIDescriptor::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001951 ReplaceMap.emplace_back(
1952 std::piecewise_construct, std::make_tuple(Ty),
1953 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001954 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001955 }
1956
David Blaikie483a9da2014-05-06 18:35:21 +00001957 return CreateTypeDefinition(Ty);
1958}
1959
1960llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1961 const EnumDecl *ED = Ty->getDecl();
1962 uint64_t Size = 0;
1963 uint64_t Align = 0;
1964 if (!ED->getTypeForDecl()->isIncompleteType()) {
1965 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1966 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1967 }
1968
1969 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1970
Guy Benyei11169dd2012-12-18 14:30:41 +00001971 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001972 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001973 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001974 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001975 Enumerators.push_back(DBuilder.createEnumerator(
1976 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 }
1978
1979 // Return a CompositeType for the enum itself.
1980 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1981
1982 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1983 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001984 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001985 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1986 llvm::DIType ClassTy = ED->isFixed()
1987 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1988 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001989 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001990 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1991 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 return DbgTy;
1993}
1994
David Blaikie05491062013-01-21 04:37:12 +00001995static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1996 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001998 Qualifiers InnerQuals = T.getLocalQualifiers();
1999 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2000 // that is already there.
2001 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2002 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002003 QualType LastT = T;
2004 switch (T->getTypeClass()) {
2005 default:
David Blaikie05491062013-01-21 04:37:12 +00002006 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002007 case Type::TemplateSpecialization: {
2008 const auto *Spec = cast<TemplateSpecializationType>(T);
2009 if (Spec->isTypeAlias())
2010 return C.getQualifiedType(T.getTypePtr(), Quals);
2011 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002012 break;
2013 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 case Type::TypeOfExpr:
2015 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2016 break;
2017 case Type::TypeOf:
2018 T = cast<TypeOfType>(T)->getUnderlyingType();
2019 break;
2020 case Type::Decltype:
2021 T = cast<DecltypeType>(T)->getUnderlyingType();
2022 break;
2023 case Type::UnaryTransform:
2024 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2025 break;
2026 case Type::Attributed:
2027 T = cast<AttributedType>(T)->getEquivalentType();
2028 break;
2029 case Type::Elaborated:
2030 T = cast<ElaboratedType>(T)->getNamedType();
2031 break;
2032 case Type::Paren:
2033 T = cast<ParenType>(T)->getInnerType();
2034 break;
David Blaikie05491062013-01-21 04:37:12 +00002035 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002037 break;
2038 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002039 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002040 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002041 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 break;
2043 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002044
Guy Benyei11169dd2012-12-18 14:30:41 +00002045 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002046 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002047 } while (true);
2048}
2049
Eric Christopher0fdcb312013-05-16 00:52:20 +00002050/// getType - Get the type from the cache or return null type if it doesn't
2051/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002052llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2053
2054 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002055 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002056
David Blaikief427b002014-05-06 03:42:01 +00002057 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002058 if (it != TypeCache.end()) {
2059 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002060 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 return llvm::DIType(cast<llvm::MDNode>(V));
2062 }
2063
2064 return llvm::DIType();
2065}
2066
David Blaikie0e716b42014-03-03 23:48:23 +00002067void CGDebugInfo::completeTemplateDefinition(
2068 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002069 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2070 return;
2071
David Blaikie0e716b42014-03-03 23:48:23 +00002072 completeClassData(&SD);
2073 // In case this type has no member function definitions being emitted, ensure
2074 // it is retained
2075 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2076}
2077
Guy Benyei11169dd2012-12-18 14:30:41 +00002078/// getOrCreateType - Get the type from the cache or create a new
2079/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002080llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002081 if (Ty.isNull())
2082 return llvm::DIType();
2083
2084 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002085 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002086
David Blaikieef8a9512014-05-05 23:23:53 +00002087 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 return T;
2089
2090 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002091 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002092 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002093
2094 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002095 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002096
Guy Benyei11169dd2012-12-18 14:30:41 +00002097 return Res;
2098}
2099
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002100/// Currently the checksum of an interface includes the number of
2101/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002102unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002103 // The assumption is that the number of ivars can only increase
2104 // monotonically, so it is safe to just use their current number as
2105 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002106 unsigned Sum = 0;
2107 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002108 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002109 ++Sum;
2110
2111 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002112}
2113
2114ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2115 switch (Ty->getTypeClass()) {
2116 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002117 return getObjCInterfaceDecl(
2118 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002119 case Type::ObjCInterface:
2120 return cast<ObjCInterfaceType>(Ty)->getDecl();
2121 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002122 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002123 }
2124}
2125
Guy Benyei11169dd2012-12-18 14:30:41 +00002126/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002127llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002128 // Handle qualifiers, which recursively handles what they refer to.
2129 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002130 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002131
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 // Work out details of type.
2133 switch (Ty->getTypeClass()) {
2134#define TYPE(Class, Base)
2135#define ABSTRACT_TYPE(Class, Base)
2136#define NON_CANONICAL_TYPE(Class, Base)
2137#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2138#include "clang/AST/TypeNodes.def"
2139 llvm_unreachable("Dependent types cannot show up in debug information");
2140
2141 case Type::ExtVector:
2142 case Type::Vector:
2143 return CreateType(cast<VectorType>(Ty), Unit);
2144 case Type::ObjCObjectPointer:
2145 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2146 case Type::ObjCObject:
2147 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2148 case Type::ObjCInterface:
2149 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2150 case Type::Builtin:
2151 return CreateType(cast<BuiltinType>(Ty));
2152 case Type::Complex:
2153 return CreateType(cast<ComplexType>(Ty));
2154 case Type::Pointer:
2155 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002156 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002157 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002158 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002159 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002160 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002161 case Type::BlockPointer:
2162 return CreateType(cast<BlockPointerType>(Ty), Unit);
2163 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002164 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002166 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002168 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002169 case Type::FunctionProto:
2170 case Type::FunctionNoProto:
2171 return CreateType(cast<FunctionType>(Ty), Unit);
2172 case Type::ConstantArray:
2173 case Type::VariableArray:
2174 case Type::IncompleteArray:
2175 return CreateType(cast<ArrayType>(Ty), Unit);
2176
2177 case Type::LValueReference:
2178 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2179 case Type::RValueReference:
2180 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2181
2182 case Type::MemberPointer:
2183 return CreateType(cast<MemberPointerType>(Ty), Unit);
2184
2185 case Type::Atomic:
2186 return CreateType(cast<AtomicType>(Ty), Unit);
2187
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002189 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2190
David Blaikie42edade2014-11-11 20:44:45 +00002191 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002192 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002193 case Type::Elaborated:
2194 case Type::Paren:
2195 case Type::SubstTemplateTypeParm:
2196 case Type::TypeOfExpr:
2197 case Type::TypeOf:
2198 case Type::Decltype:
2199 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002200 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002201 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002203
David Blaikie42edade2014-11-11 20:44:45 +00002204 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002205}
2206
2207/// getOrCreateLimitedType - Get the type from the cache or create a new
2208/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002209llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002210 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002211 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002212
David Blaikie8d5e1282013-08-20 21:03:29 +00002213 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002214
2215 // We may have cached a forward decl when we could have created
2216 // a non-forward decl. Go ahead and create a non-forward decl
2217 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002218 if (T && !T.isForwardDecl())
2219 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
2221 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002222 llvm::DICompositeType Res = CreateLimitedType(Ty);
2223
2224 // Propagate members from the declaration to the definition
2225 // CreateType(const RecordType*) will overwrite this with the members in the
2226 // correct order if the full type is needed.
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002227 DBuilder.replaceArrays(Res, T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002228
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002230 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 return Res;
2232}
2233
2234// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002235llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002237
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 // Get overall information about the record type for the debug info.
2239 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2240 unsigned Line = getLineNumber(RD->getLocation());
2241 StringRef RDName = getClassName(RD);
2242
Eric Christopher07429ff2013-10-15 21:22:34 +00002243 llvm::DIDescriptor RDContext =
2244 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002245
David Blaikied2785892013-08-18 17:36:19 +00002246 // If we ended up creating the type during the context chain construction,
2247 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002248 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2249 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002250 return T;
David Blaikied2785892013-08-18 17:36:19 +00002251
Adrian Prantl381e7552014-02-04 21:29:50 +00002252 // If this is just a forward or incomplete declaration, construct an
2253 // appropriately marked node and just return it.
2254 const RecordDecl *D = RD->getDefinition();
2255 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002256 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002257
2258 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2259 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002260 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002261
Manman Rene0064d82013-08-29 23:19:58 +00002262 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2263
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002264 RealDecl = DBuilder.createReplaceableCompositeType(getTagForRecord(RD),
2265 RDName, RDContext, DefUnit, Line, 0, Size, Align, 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002266
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002267 RegionMap[Ty->getDecl()].reset(RealDecl);
2268 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002269
David Blaikieadfbf992013-08-18 16:55:33 +00002270 if (const ClassTemplateSpecializationDecl *TSpecial =
2271 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002272 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2273 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002274 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002275}
2276
David Blaikieadfbf992013-08-18 16:55:33 +00002277void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2278 llvm::DICompositeType RealDecl) {
2279 // A class's primary base or the class itself contains the vtable.
2280 llvm::DICompositeType ContainingType;
2281 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2282 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002283 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002284 while (1) {
2285 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2286 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2287 if (PBT && !BRL.isPrimaryBaseVirtual())
2288 PBase = PBT;
2289 else
2290 break;
2291 }
2292 ContainingType = llvm::DICompositeType(
2293 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2294 getOrCreateFile(RD->getLocation())));
2295 } else if (RD->isDynamicClass())
2296 ContainingType = RealDecl;
2297
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002298 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002299}
2300
Guy Benyei11169dd2012-12-18 14:30:41 +00002301/// CreateMemberType - Create new member and increase Offset by FType's size.
2302llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002303 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002304 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2305 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2306 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002307 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2308 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 *Offset += FieldSize;
2310 return Ty;
2311}
2312
Frederic Riss9db79f12014-11-18 03:40:46 +00002313void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2314 llvm::DIFile Unit,
2315 StringRef &Name, StringRef &LinkageName,
2316 llvm::DIDescriptor &FDContext,
2317 llvm::DIArray &TParamsArray,
2318 unsigned &Flags) {
2319 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2320 Name = getFunctionName(FD);
2321 // Use mangled name as linkage name for C/C++ functions.
2322 if (FD->hasPrototype()) {
2323 LinkageName = CGM.getMangledName(GD);
2324 Flags |= llvm::DIDescriptor::FlagPrototyped;
2325 }
2326 // No need to replicate the linkage name if it isn't different from the
2327 // subprogram name, no need to have it at all unless coverage is enabled or
2328 // debug is set to more than just line tables.
2329 if (LinkageName == Name ||
2330 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2331 !CGM.getCodeGenOpts().EmitGcovNotes &&
2332 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2333 LinkageName = StringRef();
2334
2335 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2336 if (const NamespaceDecl *NSDecl =
2337 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2338 FDContext = getOrCreateNameSpace(NSDecl);
2339 else if (const RecordDecl *RDecl =
2340 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2341 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2342 // Collect template parameters.
2343 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2344 }
2345}
2346
2347void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2348 unsigned &LineNo, QualType &T,
2349 StringRef &Name, StringRef &LinkageName,
2350 llvm::DIDescriptor &VDContext) {
2351 Unit = getOrCreateFile(VD->getLocation());
2352 LineNo = getLineNumber(VD->getLocation());
2353
2354 setLocation(VD->getLocation());
2355
2356 T = VD->getType();
2357 if (T->isIncompleteArrayType()) {
2358 // CodeGen turns int[] into int[1] so we'll do the same here.
2359 llvm::APInt ConstVal(32, 1);
2360 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2361
2362 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2363 ArrayType::Normal, 0);
2364 }
2365
2366 Name = VD->getName();
2367 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2368 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2369 LinkageName = CGM.getMangledName(VD);
2370 if (LinkageName == Name)
2371 LinkageName = StringRef();
2372
2373 // Since we emit declarations (DW_AT_members) for static members, place the
2374 // definition of those static members in the namespace they were declared in
2375 // in the source code (the lexical decl context).
2376 // FIXME: Generalize this for even non-member global variables where the
2377 // declaration and definition may have different lexical decl contexts, once
2378 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002379 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2380 : VD->getDeclContext();
2381 // When a record type contains an in-line initialization of a static data
2382 // member, and the record type is marked as __declspec(dllexport), an implicit
2383 // definition of the member will be created in the record context. DWARF
2384 // doesn't seem to have a nice way to describe this in a form that consumers
2385 // are likely to understand, so fake the "normal" situation of a definition
2386 // outside the class by putting it in the global scope.
2387 if (DC->isRecord())
2388 DC = CGM.getContext().getTranslationUnitDecl();
2389 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002390}
2391
Frederic Rissd253ed62014-11-18 03:40:51 +00002392llvm::DISubprogram
2393CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2394 llvm::DIArray TParamsArray;
2395 StringRef Name, LinkageName;
2396 unsigned Flags = 0;
2397 SourceLocation Loc = FD->getLocation();
2398 llvm::DIFile Unit = getOrCreateFile(Loc);
2399 llvm::DIDescriptor DContext(Unit);
2400 unsigned Line = getLineNumber(Loc);
2401
2402 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2403 TParamsArray, Flags);
2404 // Build function type.
2405 SmallVector<QualType, 16> ArgTypes;
2406 for (const ParmVarDecl *Parm: FD->parameters())
2407 ArgTypes.push_back(Parm->getType());
2408 QualType FnType =
2409 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2410 FunctionProtoType::ExtProtoInfo());
2411 llvm::DISubprogram SP =
2412 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2413 getOrCreateFunctionType(FD, FnType, Unit),
2414 !FD->isExternallyVisible(),
2415 false /*declaration*/, 0, Flags,
2416 CGM.getLangOpts().Optimize, nullptr,
2417 TParamsArray, getFunctionDeclaration(FD));
2418 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002419 FwdDeclReplaceMap.emplace_back(
2420 std::piecewise_construct, std::make_tuple(CanonDecl),
2421 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002422 return SP;
2423}
2424
2425llvm::DIGlobalVariable
2426CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2427 QualType T;
2428 StringRef Name, LinkageName;
2429 SourceLocation Loc = VD->getLocation();
2430 llvm::DIFile Unit = getOrCreateFile(Loc);
2431 llvm::DIDescriptor DContext(Unit);
2432 unsigned Line = getLineNumber(Loc);
2433
2434 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2435 llvm::DIGlobalVariable GV =
2436 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2437 Line, getOrCreateType(T, Unit),
2438 !VD->isExternallyVisible(),
2439 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002440 FwdDeclReplaceMap.emplace_back(
2441 std::piecewise_construct,
2442 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2443 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002444 return GV;
2445}
2446
Frederic Riss442293e2014-11-06 21:12:06 +00002447llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002448 // We only need a declaration (not a definition) of the type - so use whatever
2449 // we would otherwise do to get a type for a pointee. (forward declarations in
2450 // limited debug info, full definitions (if the type definition is available)
2451 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002452 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2453 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002454 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002455 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002456
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002457 if (I != DeclCache.end())
2458 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002459
2460 // No definition for now. Emit a forward definition that might be
2461 // merged with a potential upcoming definition.
2462 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2463 return getFunctionForwardDeclaration(FD);
2464 else if (const auto *VD = dyn_cast<VarDecl>(D))
2465 return getGlobalVariableForwardDeclaration(VD);
2466
2467 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002468}
2469
Guy Benyei11169dd2012-12-18 14:30:41 +00002470/// getFunctionDeclaration - Return debug info descriptor to describe method
2471/// declaration for the given method definition.
2472llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002473 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002474 return llvm::DISubprogram();
2475
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002477 if (!FD)
2478 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002479
2480 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002481 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002482
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002483 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002484 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002485 if (const CXXMethodDecl *MD =
2486 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002487 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002488 llvm::DISubprogram SP =
2489 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002490 return SP;
2491 }
2492 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002494 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002495 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002496 return SP;
2497 }
2498
Aaron Ballman86c93902014-03-06 23:45:36 +00002499 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002500 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002502 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002503 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002504 return SP;
2505 }
2506 }
2507 return llvm::DISubprogram();
2508}
2509
2510// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2511// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002512llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2513 QualType FnType,
2514 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002515 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002516 // Create fake but valid subroutine type. Otherwise
2517 // llvm::DISubprogram::Verify() would return false, and
2518 // subprogram DIE will miss DW_AT_decl_file and
2519 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002520 return DBuilder.createSubroutineType(F,
2521 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002522
2523 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2524 return getOrCreateMethodType(Method, F);
2525 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2526 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002527 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002528
2529 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002530 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002531
2532 // Replace the instancetype keyword with the actual type.
2533 if (ResultTy == CGM.getContext().getObjCInstanceType())
2534 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002535 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002536
Adrian Prantl7bec9032013-05-10 21:08:31 +00002537 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002538 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002539 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2540 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2541 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002542 // "_cmd" pointer is always second argument.
2543 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2544 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2545 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002546 for (const auto *PI : OMethod->params())
2547 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002548 // Variadic methods need a special marker at the end of the type list.
2549 if (OMethod->isVariadic())
2550 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002551
Manman Ren67f005e2014-07-28 22:24:34 +00002552 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 return DBuilder.createSubroutineType(F, EltTypeArray);
2554 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002555
Adrian Prantl800faef2014-02-25 23:42:18 +00002556 // Handle variadic function types; they need an additional
2557 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002558 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2559 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002560 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002561 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2562 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2563 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2564 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2565 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002566 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002567 return DBuilder.createSubroutineType(F, EltTypeArray);
2568 }
2569
David Blaikie469f0792013-05-22 23:22:42 +00002570 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002571}
2572
2573/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002574void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2575 SourceLocation ScopeLoc, QualType FnType,
2576 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002577
2578 StringRef Name;
2579 StringRef LinkageName;
2580
2581 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2582
2583 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002584 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002585
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 unsigned Flags = 0;
2587 llvm::DIFile Unit = getOrCreateFile(Loc);
2588 llvm::DIDescriptor FDContext(Unit);
2589 llvm::DIArray TParamsArray;
2590 if (!HasDecl) {
2591 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002592 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2594 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002595 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002596 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002597 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002598 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2599 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002600 LexicalBlockStack.emplace_back(SPN);
2601 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 return;
2603 }
2604 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002605 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2606 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2608 Name = getObjCMethodName(OMD);
2609 Flags |= llvm::DIDescriptor::FlagPrototyped;
2610 } else {
2611 // Use llvm function name.
2612 Name = Fn->getName();
2613 Flags |= llvm::DIDescriptor::FlagPrototyped;
2614 }
2615 if (!Name.empty() && Name[0] == '\01')
2616 Name = Name.substr(1);
2617
Adrian Prantl42d71b92014-04-10 23:21:53 +00002618 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002619 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002620 // Artificial functions without a location should not silently reuse CurLoc.
2621 if (Loc.isInvalid())
2622 CurLoc = SourceLocation();
2623 }
2624 unsigned LineNo = getLineNumber(Loc);
2625 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002626
Eric Christopher8018e412014-03-27 18:50:35 +00002627 // FIXME: The function declaration we're constructing here is mostly reusing
2628 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2629 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2630 // all subprograms instead of the actual context since subprogram definitions
2631 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002632 llvm::DISubprogram SP = DBuilder.createFunction(
2633 FDContext, Name, LinkageName, Unit, LineNo,
2634 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2635 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2636 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002637 // We might get here with a VarDecl in the case we're generating
2638 // code for the initialization of globals. Do not record these decls
2639 // as they will overwrite the actual VarDecl Decl in the cache.
2640 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002641 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002642
Adrian Prantlbebb8932014-03-21 21:01:58 +00002643 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002645 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002646
Guy Benyei11169dd2012-12-18 14:30:41 +00002647 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002648 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002649}
2650
2651/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002652/// information in the source file. If the location is invalid, the
2653/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002654void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002655 // Update our current location
2656 setLocation(Loc);
2657
Eric Christophere7b87e52014-10-26 23:40:33 +00002658 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2659 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002660
Adrian Prantle83b1302014-01-07 22:05:52 +00002661 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002662 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002663 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002664}
2665
2666/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2667/// the stack.
2668void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002669 llvm::MDNode *Back = nullptr;
2670 if (!LexicalBlockStack.empty())
2671 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002672 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002673 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2674 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002676 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002677}
2678
2679/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2680/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002681void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2682 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 // Set our current location.
2684 setLocation(Loc);
2685
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002687 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2688 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002689
2690 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2691 return;
2692
2693 // Create a new lexical block and push it on the stack.
2694 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002695}
2696
2697/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2698/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002699void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2700 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2702
2703 // Provide an entry in the line table for the end of the block.
2704 EmitLocation(Builder, Loc);
2705
David Blaikie60a877b2014-10-22 19:34:33 +00002706 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2707 return;
2708
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 LexicalBlockStack.pop_back();
2710}
2711
2712/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2713void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2714 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2715 unsigned RCount = FnBeginRegionCount.back();
2716 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2717
2718 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002719 while (LexicalBlockStack.size() != RCount) {
2720 // Provide an entry in the line table for the end of the block.
2721 EmitLocation(Builder, CurLoc);
2722 LexicalBlockStack.pop_back();
2723 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002724 FnBeginRegionCount.pop_back();
2725}
2726
Eric Christopherb2a008c2013-05-16 00:45:12 +00002727// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002728// See BuildByRefType.
2729llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2730 uint64_t *XOffset) {
2731
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002732 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002733 QualType FType;
2734 uint64_t FieldSize, FieldOffset;
2735 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002736
Guy Benyei11169dd2012-12-18 14:30:41 +00002737 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002738 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002739
2740 FieldOffset = 0;
2741 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2742 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2743 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2744 FType = CGM.getContext().IntTy;
2745 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2746 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2747
2748 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2749 if (HasCopyAndDispose) {
2750 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002751 EltTys.push_back(
2752 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2753 EltTys.push_back(
2754 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002755 }
2756 bool HasByrefExtendedLayout;
2757 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002758 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2759 HasByrefExtendedLayout) &&
2760 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002761 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002762 EltTys.push_back(
2763 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002764 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002765
Guy Benyei11169dd2012-12-18 14:30:41 +00002766 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2767 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002768 CGM.getTarget().getPointerAlign(0))) {
2769 CharUnits FieldOffsetInBytes =
2770 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2771 CharUnits AlignedOffsetInBytes =
2772 FieldOffsetInBytes.RoundUpToAlignment(Align);
2773 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 if (NumPaddingBytes.isPositive()) {
2776 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2777 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2778 pad, ArrayType::Normal, 0);
2779 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2780 }
2781 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002782
Guy Benyei11169dd2012-12-18 14:30:41 +00002783 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002784 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002785 FieldSize = CGM.getContext().getTypeSize(FType);
2786 FieldAlign = CGM.getContext().toBits(Align);
2787
Eric Christopherb2a008c2013-05-16 00:45:12 +00002788 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002789 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2790 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 EltTys.push_back(FieldTy);
2792 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002793
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002795
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002797
Guy Benyei11169dd2012-12-18 14:30:41 +00002798 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002799 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002800}
2801
2802/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002803void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002804 llvm::Value *Storage, unsigned ArgNo,
2805 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002806 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2808
David Blaikie7fceebf2013-08-19 03:37:48 +00002809 bool Unwritten =
2810 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2811 cast<Decl>(VD->getDeclContext())->isImplicit());
2812 llvm::DIFile Unit;
2813 if (!Unwritten)
2814 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 llvm::DIType Ty;
2816 uint64_t XOffset = 0;
2817 if (VD->hasAttr<BlocksAttr>())
2818 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002819 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 Ty = getOrCreateType(VD->getType(), Unit);
2821
2822 // If there is no debug info for this type then do not emit debug info
2823 // for this variable.
2824 if (!Ty)
2825 return;
2826
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002828 unsigned Line = 0;
2829 unsigned Column = 0;
2830 if (!Unwritten) {
2831 Line = getLineNumber(VD->getLocation());
2832 Column = getColumnNumber(VD->getLocation());
2833 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002834 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002835 unsigned Flags = 0;
2836 if (VD->isImplicit())
2837 Flags |= llvm::DIDescriptor::FlagArtificial;
2838 // If this is the first argument and it is implicit then
2839 // give it an object pointer flag.
2840 // FIXME: There has to be a better way to do this, but for static
2841 // functions there won't be an implicit param at arg1 and
2842 // otherwise it is 'self' or 'this'.
2843 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2844 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002845 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002846 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2847 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002848 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002849
2850 llvm::MDNode *Scope = LexicalBlockStack.back();
2851
2852 StringRef Name = VD->getName();
2853 if (!Name.empty()) {
2854 if (VD->hasAttr<BlocksAttr>()) {
2855 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002856 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002857 // offset of __forwarding field
2858 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002859 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002860 Expr.push_back(offset.getQuantity());
2861 Expr.push_back(llvm::dwarf::DW_OP_deref);
2862 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 // offset of x field
2864 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002865 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002866
2867 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002868 llvm::DIVariable D = DBuilder.createLocalVariable(
2869 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002870
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 // Insert an llvm.dbg.declare into the current block.
2872 llvm::Instruction *Call =
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002873 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Eric Christophere7b87e52014-10-26 23:40:33 +00002874 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002875 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2876 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002877 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002878 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002879 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2880 // If VD is an anonymous union then Storage represents value for
2881 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002883 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002884 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2886 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002887
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 // Ignore unnamed fields. Do not ignore unnamed records.
2889 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2890 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002891
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002893 llvm::DIVariable D = DBuilder.createLocalVariable(
2894 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2895 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002896
Guy Benyei11169dd2012-12-18 14:30:41 +00002897 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002898 llvm::Instruction *Call = DBuilder.insertDeclare(
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002899 Storage, D, DBuilder.createExpression(Expr),
2900 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2902 }
David Blaikie219c7d92013-01-05 20:03:07 +00002903 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 }
2905 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002906
2907 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002908 llvm::DIVariable D = DBuilder.createLocalVariable(
2909 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2910 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002911
2912 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002913 llvm::Instruction *Call = DBuilder.insertDeclare(
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002914 Storage, D, DBuilder.createExpression(Expr), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002915 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002916}
2917
2918void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2919 llvm::Value *Storage,
2920 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002921 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2923}
2924
Adrian Prantlde17db32013-03-29 19:20:29 +00002925/// Look up the completed type for a self pointer in the TypeCache and
2926/// create a copy of it with the ObjectPointer and Artificial flags
2927/// set. If the type is not cached, a new one is created. This should
2928/// never happen though, since creating a type for the implicit self
2929/// argument implies that we already parsed the interface definition
2930/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002931llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2932 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002933 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002934 if (CachedTy)
2935 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002936 return DBuilder.createObjectPointerType(Ty);
2937}
2938
Eric Christophere7b87e52014-10-26 23:40:33 +00002939void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2940 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002941 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002942 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002943 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002944
Craig Topper8a13c412014-05-21 05:09:00 +00002945 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002947
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002949
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 uint64_t XOffset = 0;
2951 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2952 llvm::DIType Ty;
2953 if (isByRef)
2954 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002955 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 Ty = getOrCreateType(VD->getType(), Unit);
2957
2958 // Self is passed along as an implicit non-arg variable in a
2959 // block. Mark it as the object pointer.
2960 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002961 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002962
2963 // Get location information.
2964 unsigned Line = getLineNumber(VD->getLocation());
2965 unsigned Column = getColumnNumber(VD->getLocation());
2966
2967 const llvm::DataLayout &target = CGM.getDataLayout();
2968
2969 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002970 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002971 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2972
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002973 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002974 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002975 addr.push_back(llvm::dwarf::DW_OP_deref);
2976 addr.push_back(llvm::dwarf::DW_OP_plus);
2977 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002979 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 __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002982 offset =
2983 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002984 addr.push_back(offset.getQuantity());
2985 addr.push_back(llvm::dwarf::DW_OP_deref);
2986 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002987 // offset of x field
2988 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002989 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002990 }
2991
2992 // Create the descriptor for the variable.
2993 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002994 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2995 llvm::DIDescriptor(LexicalBlockStack.back()),
2996 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002997
Guy Benyei11169dd2012-12-18 14:30:41 +00002998 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002999 llvm::Instruction *Call = InsertPoint ?
3000 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
3001 InsertPoint)
3002 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
3003 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00003004 Call->setDebugLoc(
3005 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00003006}
3007
3008/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3009/// variable declaration.
3010void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3011 unsigned ArgNo,
3012 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003013 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3015}
3016
3017namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003018struct BlockLayoutChunk {
3019 uint64_t OffsetInBits;
3020 const BlockDecl::Capture *Capture;
3021};
3022bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3023 return l.OffsetInBits < r.OffsetInBits;
3024}
Guy Benyei11169dd2012-12-18 14:30:41 +00003025}
3026
3027void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003028 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003029 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003030 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003032 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 ASTContext &C = CGM.getContext();
3034 const BlockDecl *blockDecl = block.getBlockDecl();
3035
3036 // Collect some general information about the block's location.
3037 SourceLocation loc = blockDecl->getCaretLocation();
3038 llvm::DIFile tunit = getOrCreateFile(loc);
3039 unsigned line = getLineNumber(loc);
3040 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003041
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 // Build the debug-info type for the block literal.
3043 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3044
3045 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003046 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003047
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003048 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003049 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3050 blockLayout->getElementOffsetInBits(0),
3051 tunit, tunit));
3052 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3053 blockLayout->getElementOffsetInBits(1),
3054 tunit, tunit));
3055 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3056 blockLayout->getElementOffsetInBits(2),
3057 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003058 auto *FnTy = block.getBlockExpr()->getFunctionType();
3059 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3060 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 blockLayout->getElementOffsetInBits(3),
3062 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003063 fields.push_back(createFieldType(
3064 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3065 ? C.getBlockDescriptorExtendedType()
3066 : C.getBlockDescriptorType()),
3067 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003068
3069 // We want to sort the captures by offset, not because DWARF
3070 // requires this, but because we're paranoid about debuggers.
3071 SmallVector<BlockLayoutChunk, 8> chunks;
3072
3073 // 'this' capture.
3074 if (blockDecl->capturesCXXThis()) {
3075 BlockLayoutChunk chunk;
3076 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003077 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003078 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 chunks.push_back(chunk);
3080 }
3081
3082 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003083 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 const VarDecl *variable = capture.getVariable();
3085 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3086
3087 // Ignore constant captures.
3088 if (captureInfo.isConstant())
3089 continue;
3090
3091 BlockLayoutChunk chunk;
3092 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003093 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 chunk.Capture = &capture;
3095 chunks.push_back(chunk);
3096 }
3097
3098 // Sort by offset.
3099 llvm::array_pod_sort(chunks.begin(), chunks.end());
3100
Eric Christophere7b87e52014-10-26 23:40:33 +00003101 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3102 e = chunks.end();
3103 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 uint64_t offsetInBits = i->OffsetInBits;
3105 const BlockDecl::Capture *capture = i->Capture;
3106
3107 // If we have a null capture, this must be the C++ 'this' capture.
3108 if (!capture) {
3109 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003110 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 QualType type = method->getThisType(C);
3112
3113 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3114 offsetInBits, tunit, tunit));
3115 continue;
3116 }
3117
3118 const VarDecl *variable = capture->getVariable();
3119 StringRef name = variable->getName();
3120
3121 llvm::DIType fieldType;
3122 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003123 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003124
3125 // FIXME: this creates a second copy of this type!
3126 uint64_t xoffset;
3127 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003128 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3129 fieldType =
3130 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3131 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003133 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3134 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 }
3136 fields.push_back(fieldType);
3137 }
3138
3139 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003140 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3141 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003142
3143 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3144
3145 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003146 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3147 CGM.getContext().toBits(block.BlockSize),
3148 CGM.getContext().toBits(block.BlockAlign), 0,
3149 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3151
3152 // Get overall information about the block.
3153 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3154 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003155
3156 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003157 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3158 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3159 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3160 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003161
Adrian Prantl616bef42013-03-14 21:52:59 +00003162 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003163 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003164 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3165 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3166 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003167 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3168 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003169
Adrian Prantl616bef42013-03-14 21:52:59 +00003170 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003171 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3172 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003173 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003174}
3175
David Blaikie6943dea2013-08-20 01:28:15 +00003176/// If D is an out-of-class definition of a static data member of a class, find
3177/// its corresponding in-class declaration.
3178llvm::DIDerivedType
3179CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3180 if (!D->isStaticDataMember())
3181 return llvm::DIDerivedType();
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003182
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003183 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003184 if (MI != StaticDataMemberCache.end()) {
3185 assert(MI->second && "Static data member declaration should still exist");
3186 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003187 }
David Blaikiece763042013-08-20 21:49:21 +00003188
3189 // If the member wasn't found in the cache, lazily construct and add it to the
3190 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003191 auto DC = D->getDeclContext();
3192 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3193 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003194}
3195
Eric Christophercab9fae2014-04-10 05:20:00 +00003196/// Recursively collect all of the member fields of a global anonymous decl and
3197/// create static variables for them. The first time this is called it needs
3198/// to be on a union and then from there we can have additional unnamed fields.
3199llvm::DIGlobalVariable
3200CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3201 unsigned LineNo, StringRef LinkageName,
3202 llvm::GlobalVariable *Var,
3203 llvm::DIDescriptor DContext) {
3204 llvm::DIGlobalVariable GV;
3205
3206 for (const auto *Field : RD->fields()) {
3207 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3208 StringRef FieldName = Field->getName();
3209
3210 // Ignore unnamed fields, but recurse into anonymous records.
3211 if (FieldName.empty()) {
3212 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3213 if (RT)
3214 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3215 Var, DContext);
3216 continue;
3217 }
3218 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003219 GV = DBuilder.createGlobalVariable(
3220 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3221 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003222 }
3223 return GV;
3224}
3225
Guy Benyei11169dd2012-12-18 14:30:41 +00003226/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003227void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003228 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003229 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003230 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003231 llvm::DIFile Unit;
3232 llvm::DIDescriptor DContext;
3233 unsigned LineNo;
3234 StringRef DeclName, LinkageName;
3235 QualType T;
3236 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003237
3238 // Attempt to store one global variable for the declaration - even if we
3239 // emit a lot of fields.
3240 llvm::DIGlobalVariable GV;
3241
3242 // If this is an anonymous union then we'll want to emit a global
3243 // variable for each member of the anonymous union so that it's possible
3244 // to find the name of any field in the union.
3245 if (T->isUnionType() && DeclName.empty()) {
3246 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003247 assert(RD->isAnonymousStructOrUnion() &&
3248 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003249 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3250 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003251 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003252 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3253 Var->hasInternalLinkage(), Var,
3254 getOrCreateStaticDataMemberDeclarationOrNull(D));
3255 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003256 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003257}
3258
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003259/// EmitGlobalVariable - Emit global variable's debug info.
3260void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3261 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003262 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003263 // Create the descriptor for the variable.
3264 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3265 StringRef Name = VD->getName();
3266 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3267 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3268 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3269 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3270 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3271 }
3272 // Do not use DIGlobalVariable for enums.
3273 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3274 return;
David Blaikiea15565562014-04-04 20:56:17 +00003275 // Do not emit separate definitions for function local const/statics.
3276 if (isa<FunctionDecl>(VD->getDeclContext()))
3277 return;
David Blaikiebb113912014-04-05 07:23:17 +00003278 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003279 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003280 if (VarD->isStaticDataMember()) {
3281 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3282 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003283 // Ensure that the type is retained even though it's otherwise unreferenced.
3284 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003285 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003286 return;
3287 }
3288
David Blaikieaf080852014-11-21 00:20:58 +00003289 llvm::DIDescriptor DContext =
3290 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3291
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003292 auto &GV = DeclCache[VD];
3293 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003294 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003295 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003296 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003297 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003298}
3299
3300llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3301 if (!LexicalBlockStack.empty())
3302 return llvm::DIScope(LexicalBlockStack.back());
3303 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003304}
3305
David Blaikie9f88fe82013-04-22 06:13:21 +00003306void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003307 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3308 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003309 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003310 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3311 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003312 getLineNumber(UD.getLocation()));
3313}
3314
David Blaikiebd483762013-05-20 04:58:53 +00003315void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3316 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3317 return;
3318 assert(UD.shadow_size() &&
3319 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3320 // Emitting one decl is sufficient - debuggers can detect that this is an
3321 // overloaded name & provide lookup for all the overloads.
3322 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003323 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003324 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003325 DBuilder.createImportedDeclaration(
3326 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3327 getLineNumber(USD.getLocation()));
3328}
3329
David Blaikief121b932013-05-20 22:50:41 +00003330llvm::DIImportedEntity
3331CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3332 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003333 return llvm::DIImportedEntity();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003334 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003335 if (VH)
3336 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003337 llvm::DIImportedEntity R;
David Blaikief121b932013-05-20 22:50:41 +00003338 if (const NamespaceAliasDecl *Underlying =
3339 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3340 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003341 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003342 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3343 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3344 NA.getName());
3345 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003346 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003347 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3348 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3349 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003350 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003351 return R;
3352}
3353
Guy Benyei11169dd2012-12-18 14:30:41 +00003354/// getOrCreateNamesSpace - Return namespace descriptor for the given
3355/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003356llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003357CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003358 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003359 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003360 if (I != NameSpaceCache.end())
3361 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003362
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3364 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003365 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3367 llvm::DINameSpace NS =
3368 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003369 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 return NS;
3371}
3372
3373void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003374 // Creating types might create further types - invalidating the current
3375 // element and the size(), so don't cache/reference them.
3376 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3377 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3378 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3379 E.Type->getDecl()->getDefinition()
3380 ? CreateTypeDefinition(E.Type, E.Unit)
3381 : E.Decl);
3382 }
3383
David Blaikief427b002014-05-06 03:42:01 +00003384 for (auto p : ReplaceMap) {
3385 assert(p.second);
3386 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003387 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003388
David Blaikief427b002014-05-06 03:42:01 +00003389 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003390 assert(it != TypeCache.end());
3391 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003392
David Blaikief427b002014-05-06 03:42:01 +00003393 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3394 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003396
Frederic Rissd253ed62014-11-18 03:40:51 +00003397 for (const auto &p : FwdDeclReplaceMap) {
3398 assert(p.second);
3399 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003400 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003401
3402 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003403 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003404 // with ourselves, that will destroy the temporary MDNode and
3405 // replace it with a standard one, avoiding leaking memory.
3406 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003407 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003408 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003409 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003410
Frederic Rissd253ed62014-11-18 03:40:51 +00003411 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003412 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003413 }
3414
Adrian Prantl73409ce2013-03-11 18:33:46 +00003415 // We keep our own list of retained types, because we need to look
3416 // up the final type in the type cache.
3417 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3418 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003419 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003420
Guy Benyei11169dd2012-12-18 14:30:41 +00003421 DBuilder.finalize();
3422}
David Blaikie66088d52014-09-24 17:01:27 +00003423
3424void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3425 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3426 return;
3427 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3428 // Don't ignore in case of explicit cast where it is referenced indirectly.
3429 DBuilder.retainType(DieTy);
3430}