blob: 530797d15bf78e6025cf1081d09ef82128804dff [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
Guy Benyei11169dd2012-12-18 14:30:41 +0000624// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000625llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000626CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000627 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000628 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000629 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
David Blaikie8d5e1282013-08-20 21:03:29 +0000630 return llvm::DICompositeType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000631 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
632 unsigned Line = getLineNumber(RD->getLocation());
633 StringRef RDName = getClassName(RD);
634
Ed Masteda706022014-05-07 12:49:30 +0000635 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 if (RD->isStruct() || RD->isInterface())
637 Tag = llvm::dwarf::DW_TAG_structure_type;
638 else if (RD->isUnion())
639 Tag = llvm::dwarf::DW_TAG_union_type;
640 else {
641 assert(RD->isClass());
642 Tag = llvm::dwarf::DW_TAG_class_type;
643 }
644
645 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000646 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
David Blaikief427b002014-05-06 03:42:01 +0000647 llvm::DICompositeType RetTy = DBuilder.createReplaceableForwardDecl(
648 Tag, RDName, Ctx, DefUnit, Line, 0, 0, 0, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000649 ReplaceMap.emplace_back(
650 std::piecewise_construct, std::make_tuple(Ty),
651 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000652 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000653}
654
Ed Masteda706022014-05-07 12:49:30 +0000655llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000656 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000657 QualType PointeeTy,
658 llvm::DIFile Unit) {
659 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
660 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000661 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000662
Guy Benyei11169dd2012-12-18 14:30:41 +0000663 // Bit size, align and offset of the type.
664 // Size is always the size of a pointer. We can't use getTypeSize here
665 // because that does not return the correct value for references.
666 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000667 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000668 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
669
David Blaikie99dab3b2013-09-04 22:03:57 +0000670 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
671 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000672}
673
Eric Christopher0fdcb312013-05-16 00:52:20 +0000674llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
675 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000676 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000677 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000678 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
679 TheCU, getOrCreateMainFile(), 0);
680 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
681 Cache = DBuilder.createPointerType(Cache, Size);
682 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000683}
684
Guy Benyei11169dd2012-12-18 14:30:41 +0000685llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
686 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000687 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000688 return BlockLiteralGeneric;
689
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000690 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000691 llvm::DIType FieldTy;
692 QualType FType;
693 uint64_t FieldSize, FieldOffset;
694 unsigned FieldAlign;
695 llvm::DIArray Elements;
696 llvm::DIType EltTy, DescTy;
697
698 FieldOffset = 0;
699 FType = CGM.getContext().UnsignedLongTy;
700 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
701 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
702
703 Elements = DBuilder.getOrCreateArray(EltTys);
704 EltTys.clear();
705
706 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
707 unsigned LineNo = getLineNumber(CurLoc);
708
Eric Christophere7b87e52014-10-26 23:40:33 +0000709 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
710 FieldOffset, 0, Flags, llvm::DIType(),
711 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000712
713 // Bit size, align and offset of the type.
714 uint64_t Size = CGM.getContext().getTypeSize(Ty);
715
716 DescTy = DBuilder.createPointerType(EltTy, Size);
717
718 FieldOffset = 0;
719 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
720 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
721 FType = CGM.getContext().IntTy;
722 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
723 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000724 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000725 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
726
727 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
728 FieldTy = DescTy;
729 FieldSize = CGM.getContext().getTypeSize(Ty);
730 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000731 FieldTy =
732 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
733 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734 EltTys.push_back(FieldTy);
735
736 FieldOffset += FieldSize;
737 Elements = DBuilder.getOrCreateArray(EltTys);
738
Eric Christophere7b87e52014-10-26 23:40:33 +0000739 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
740 LineNo, FieldOffset, 0, Flags,
741 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000742
Guy Benyei11169dd2012-12-18 14:30:41 +0000743 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
744 return BlockLiteralGeneric;
745}
746
Eric Christophere7b87e52014-10-26 23:40:33 +0000747llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
748 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000749 assert(Ty->isTypeAlias());
750 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000751
752 SmallString<128> NS;
753 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000754 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
755 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000756
757 TemplateSpecializationType::PrintTemplateArgumentList(
758 OS, Ty->getArgs(), Ty->getNumArgs(),
759 CGM.getContext().getPrintingPolicy());
760
Eric Christophere7b87e52014-10-26 23:40:33 +0000761 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
762 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000763
764 SourceLocation Loc = AliasDecl->getLocation();
765 llvm::DIFile File = getOrCreateFile(Loc);
766 unsigned Line = getLineNumber(Loc);
767
Eric Christophere7b87e52014-10-26 23:40:33 +0000768 llvm::DIDescriptor Ctxt =
769 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000770
771 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
772}
773
David Blaikie99dab3b2013-09-04 22:03:57 +0000774llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000775 // Typedefs are derived from some other type. If we have a typedef of a
776 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000777 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 // We don't set size information, but do specify where the typedef was
779 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000780 SourceLocation Loc = Ty->getDecl()->getLocation();
781 llvm::DIFile File = getOrCreateFile(Loc);
782 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000783 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000784
Guy Benyei11169dd2012-12-18 14:30:41 +0000785 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000786 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000787
Eric Christophere7b87e52014-10-26 23:40:33 +0000788 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
789 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000790}
791
792llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
793 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000794 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000795
796 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000797 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000798
799 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000800 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 if (isa<FunctionNoProtoType>(Ty))
802 EltTys.push_back(DBuilder.createUnspecifiedParameter());
803 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000804 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
805 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000806 if (FPT->isVariadic())
807 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 }
809
Manman Ren67f005e2014-07-28 22:24:34 +0000810 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000811 return DBuilder.createSubroutineType(Unit, EltTypeArray);
812}
813
Adrian Prantl21361fb2014-08-29 22:44:27 +0000814/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
815/// As an optimization, return 0 if the access specifier equals the
816/// default for the containing type.
817static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
818 AccessSpecifier Default = clang::AS_none;
819 if (RD && RD->isClass())
820 Default = clang::AS_private;
821 else if (RD && (RD->isStruct() || RD->isUnion()))
822 Default = clang::AS_public;
823
824 if (Access == Default)
825 return 0;
826
Eric Christophere7b87e52014-10-26 23:40:33 +0000827 switch (Access) {
828 case clang::AS_private:
829 return llvm::DIDescriptor::FlagPrivate;
830 case clang::AS_protected:
831 return llvm::DIDescriptor::FlagProtected;
832 case clang::AS_public:
833 return llvm::DIDescriptor::FlagPublic;
834 case clang::AS_none:
835 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000836 }
837 llvm_unreachable("unexpected access enumerator");
838}
Guy Benyei11169dd2012-12-18 14:30:41 +0000839
Eric Christophere7b87e52014-10-26 23:40:33 +0000840llvm::DIType CGDebugInfo::createFieldType(
841 StringRef name, QualType type, uint64_t sizeInBitsOverride,
842 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
843 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000844 llvm::DIType debugType = getOrCreateType(type, tunit);
845
846 // Get the location for the field.
847 llvm::DIFile file = getOrCreateFile(loc);
848 unsigned line = getLineNumber(loc);
849
David Majnemer34b57492014-07-30 01:30:47 +0000850 uint64_t SizeInBits = 0;
851 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000853 TypeInfo TI = CGM.getContext().getTypeInfo(type);
854 SizeInBits = TI.Width;
855 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856
857 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000858 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 }
860
Adrian Prantl21361fb2014-08-29 22:44:27 +0000861 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000862 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
863 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000864}
865
Eric Christopher91a31902013-01-16 01:22:32 +0000866/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000867void CGDebugInfo::CollectRecordLambdaFields(
868 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
869 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000870 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
871 // has the name and the location of the variable so we should iterate over
872 // both concurrently.
873 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
874 RecordDecl::field_iterator Field = CXXDecl->field_begin();
875 unsigned fieldno = 0;
876 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000877 E = CXXDecl->captures_end();
878 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000879 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000880 if (C.capturesVariable()) {
881 VarDecl *V = C.getCapturedVar();
882 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
883 StringRef VName = V->getName();
884 uint64_t SizeInBitsOverride = 0;
885 if (Field->isBitField()) {
886 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
887 assert(SizeInBitsOverride && "found named 0-width bitfield");
888 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000889 llvm::DIType fieldType = createFieldType(
890 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
891 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
892 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000893 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000894 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000895 // TODO: Need to handle 'this' in some way by probably renaming the
896 // this of the lambda class and having a field member of 'this' or
897 // by using AT_object_pointer for the function and having that be
898 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000899 FieldDecl *f = *Field;
900 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
901 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000902 llvm::DIType fieldType = createFieldType(
903 "this", type, 0, f->getLocation(), f->getAccess(),
904 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000905
906 elements.push_back(fieldType);
907 }
908 }
909}
910
David Blaikie6943dea2013-08-20 01:28:15 +0000911/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000912llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
913 llvm::DIType RecordTy,
914 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000915 // Create the descriptor for the static variable, with or without
916 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000917 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000918 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
919 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
920
Eric Christopher91a31902013-01-16 01:22:32 +0000921 unsigned LineNumber = getLineNumber(Var->getLocation());
922 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000923 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000924 if (Var->getInit()) {
925 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000926 if (Value) {
927 if (Value->isInt())
928 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
929 if (Value->isFloat())
930 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
931 }
Eric Christopher91a31902013-01-16 01:22:32 +0000932 }
933
Adrian Prantl21361fb2014-08-29 22:44:27 +0000934 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000935 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
936 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000937 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000938 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000939}
940
941/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000942void CGDebugInfo::CollectRecordNormalField(
943 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000944 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000945 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000946 StringRef name = field->getName();
947 QualType type = field->getType();
948
949 // Ignore unnamed fields unless they're anonymous structs/unions.
950 if (name.empty() && !type->isRecordType())
951 return;
952
953 uint64_t SizeInBitsOverride = 0;
954 if (field->isBitField()) {
955 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
956 assert(SizeInBitsOverride && "found named 0-width bitfield");
957 }
958
Eric Christophere7b87e52014-10-26 23:40:33 +0000959 llvm::DIType fieldType =
960 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
961 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000962
963 elements.push_back(fieldType);
964}
965
Guy Benyei11169dd2012-12-18 14:30:41 +0000966/// CollectRecordFields - A helper function to collect debug info for
967/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000968void CGDebugInfo::CollectRecordFields(
969 const RecordDecl *record, llvm::DIFile tunit,
970 SmallVectorImpl<llvm::Metadata *> &elements,
971 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000972 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
973
Eric Christopher91a31902013-01-16 01:22:32 +0000974 if (CXXDecl && CXXDecl->isLambda())
975 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
976 else {
977 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000978
Eric Christopher91a31902013-01-16 01:22:32 +0000979 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000980 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000981
Eric Christopher91a31902013-01-16 01:22:32 +0000982 // Static and non-static members should appear in the same order as
983 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000984 for (const auto *I : record->decls())
985 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000986 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000987 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000988 if (MI != StaticDataMemberCache.end()) {
989 assert(MI->second &&
990 "Static data member declaration should still exist");
991 elements.push_back(
992 llvm::DIDerivedType(cast<llvm::MDNode>(MI->second)));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000993 } else {
994 auto Field = CreateRecordStaticField(V, RecordTy, record);
995 elements.push_back(Field);
996 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000997 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000998 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
999 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001000
1001 // Bump field number for next field.
1002 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001003 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001004 }
1005}
1006
1007/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1008/// function type is not updated to include implicit "this" pointer. Use this
1009/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001010llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001011CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1012 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001013 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001014 if (Method->isStatic())
David Blaikie469f0792013-05-22 23:22:42 +00001015 return llvm::DICompositeType(getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001016 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1017 Func, Unit);
1018}
David Blaikie2aaf0652013-01-07 22:24:59 +00001019
David Blaikie469f0792013-05-22 23:22:42 +00001020llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001021 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 // Add "this" pointer.
Manman Ren67f005e2014-07-28 22:24:34 +00001023 llvm::DITypeArray Args = llvm::DISubroutineType(
1024 getOrCreateType(QualType(Func, 0), Unit)).getTypeArray();
Eric Christophere7b87e52014-10-26 23:40:33 +00001025 assert(Args.getNumElements() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001026
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001027 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001028
1029 // First element is always return type. For 'void' functions it is NULL.
1030 Elts.push_back(Args.getElement(0));
1031
David Blaikie2aaf0652013-01-07 22:24:59 +00001032 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001033 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001034 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1035 // Create pointer type directly in this case.
1036 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1037 QualType PointeeTy = ThisPtrTy->getPointeeType();
1038 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001039 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001040 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1041 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001042 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001043 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001044 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001045 // TODO: This and the artificial type below are misleading, the
1046 // types aren't artificial the argument is, but the current
1047 // metadata doesn't represent that.
1048 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1049 Elts.push_back(ThisPtrType);
1050 } else {
1051 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001052 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001053 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1054 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 }
1056
1057 // Copy rest of the arguments.
1058 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
1059 Elts.push_back(Args.getElement(i));
1060
Manman Ren67f005e2014-07-28 22:24:34 +00001061 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062
Adrian Prantl0630eb72013-12-18 21:48:18 +00001063 unsigned Flags = 0;
1064 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1065 Flags |= llvm::DIDescriptor::FlagLValueReference;
1066 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1067 Flags |= llvm::DIDescriptor::FlagRValueReference;
1068
1069 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001070}
1071
Eric Christopherb2a008c2013-05-16 00:45:12 +00001072/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001073/// inside a function.
1074static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1075 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1076 return isFunctionLocalClass(NRD);
1077 if (isa<FunctionDecl>(RD->getDeclContext()))
1078 return true;
1079 return false;
1080}
1081
1082/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1083/// a single member function GlobalDecl.
1084llvm::DISubprogram
1085CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001086 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001087 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001088 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001089
Guy Benyei11169dd2012-12-18 14:30:41 +00001090 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001091 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001092
1093 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1094 // make sense to give a single ctor/dtor a linkage name.
1095 StringRef MethodLinkageName;
1096 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1097 MethodLinkageName = CGM.getMangledName(Method);
1098
1099 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001100 llvm::DIFile MethodDefUnit;
1101 unsigned MethodLine = 0;
1102 if (!Method->isImplicit()) {
1103 MethodDefUnit = getOrCreateFile(Method->getLocation());
1104 MethodLine = getLineNumber(Method->getLocation());
1105 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001106
1107 // Collect virtual method info.
1108 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001109 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001110 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001111
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 if (Method->isVirtual()) {
1113 if (Method->isPure())
1114 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1115 else
1116 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001117
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 // It doesn't make sense to give a virtual destructor a vtable index,
1119 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001120 // FIXME: Add proper support for debug info for virtual calls in
1121 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1122 // lookup if we have multiple or virtual inheritance.
1123 if (!isa<CXXDestructorDecl>(Method) &&
1124 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001125 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 ContainingType = RecordTy;
1127 }
1128
1129 unsigned Flags = 0;
1130 if (Method->isImplicit())
1131 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001132 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1134 if (CXXC->isExplicit())
1135 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001136 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001137 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001138 if (CXXC->isExplicit())
1139 Flags |= llvm::DIDescriptor::FlagExplicit;
1140 }
1141 if (Method->hasPrototype())
1142 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001143 if (Method->getRefQualifier() == RQ_LValue)
1144 Flags |= llvm::DIDescriptor::FlagLValueReference;
1145 if (Method->getRefQualifier() == RQ_RValue)
1146 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001147
1148 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001149 llvm::DISubprogram SP = DBuilder.createMethod(
1150 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1151 MethodTy, /*isLocalToUnit=*/false,
1152 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
1153 CGM.getLangOpts().Optimize, nullptr, TParamsArray);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001154
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001155 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001156
1157 return SP;
1158}
1159
1160/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001161/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001162/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001163void CGDebugInfo::CollectCXXMemberFunctions(
1164 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001165 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001166
1167 // Since we want more than just the individual member decls if we
1168 // have templated functions iterate over every declaration to gather
1169 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001170 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001171 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1172 // If the member is implicit, don't add it to the member list. This avoids
1173 // the member being added to type units by LLVM, while still allowing it
1174 // to be emitted into the type declaration/reference inside the compile
1175 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001176 // FIXME: Handle Using(Shadow?)Decls here to create
1177 // DW_TAG_imported_declarations inside the class for base decls brought into
1178 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1179 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1180 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001181 if (!Method || Method->isImplicit())
1182 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001183
1184 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1185 continue;
1186
David Blaikiefd580722014-10-06 05:18:55 +00001187 // Reuse the existing member function declaration if it exists.
1188 // It may be associated with the declaration of the type & should be
1189 // reused as we're building the definition.
1190 //
1191 // This situation can arise in the vtable-based debug info reduction where
1192 // implicit members are emitted in a non-vtable TU.
1193 auto MI = SPCache.find(Method->getCanonicalDecl());
1194 EltTys.push_back(MI == SPCache.end()
1195 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001196 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001197 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001198}
Guy Benyei11169dd2012-12-18 14:30:41 +00001199
Guy Benyei11169dd2012-12-18 14:30:41 +00001200/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001201/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001202/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001203void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001204 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001205 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001206
1207 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001208 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001209 unsigned BFlags = 0;
1210 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001211
Guy Benyei11169dd2012-12-18 14:30:41 +00001212 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001213 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001214
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001216 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1217 // virtual base offset offset is -ve. The code generator emits dwarf
1218 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001219 BaseOffset = 0 - CGM.getItaniumVTableContext()
1220 .getVirtualBaseOffsetOffset(RD, Base)
1221 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001222 } else {
1223 // In the MS ABI, store the vbtable offset, which is analogous to the
1224 // vbase offset offset in Itanium.
1225 BaseOffset =
1226 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1227 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001228 BFlags = llvm::DIDescriptor::FlagVirtual;
1229 } else
1230 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1231 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1232 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001233
Adrian Prantl21361fb2014-08-29 22:44:27 +00001234 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001235 llvm::DIType DTy = DBuilder.createInheritance(
1236 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 EltTys.push_back(DTy);
1238 }
1239}
1240
1241/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001242llvm::DIArray
1243CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1244 ArrayRef<TemplateArgument> TAList,
1245 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001246 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1248 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001249 StringRef Name;
1250 if (TPList)
1251 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001252 switch (TA.getKind()) {
1253 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001254 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1255 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001256 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001257 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001258 } break;
1259 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1261 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001262 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001263 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001264 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1265 TemplateParams.push_back(TVP);
1266 } break;
1267 case TemplateArgument::Declaration: {
1268 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001269 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001270 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001271 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001272 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001273 // Variable pointer template parameters have a value that is the address
1274 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001275 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001276 V = CGM.GetAddrOfGlobalVar(VD);
1277 // Member function pointers have special support for building them, though
1278 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001279 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001280 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001281 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001282 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001283 // Member data pointers have special handling too to compute the fixed
1284 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001285 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001286 // These five lines (& possibly the above member function pointer
1287 // handling) might be able to be refactored to use similar code in
1288 // CodeGenModule::getMemberPointerConstant
1289 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1290 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001291 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001292 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001293 }
1294 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001295 DBuilder.createTemplateValueParameter(
1296 TheCU, Name, TTy,
1297 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001298 TemplateParams.push_back(TVP);
1299 } break;
1300 case TemplateArgument::NullPtr: {
1301 QualType T = TA.getNullPtrType();
1302 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001303 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001304 // Special case member data pointer null values since they're actually -1
1305 // instead of zero.
1306 if (const MemberPointerType *MPT =
1307 dyn_cast<MemberPointerType>(T.getTypePtr()))
1308 // But treat member function pointers as simple zero integers because
1309 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1310 // CodeGen grows handling for values of non-null member function
1311 // pointers then perhaps we could remove this special case and rely on
1312 // EmitNullMemberPointer for member function pointers.
1313 if (MPT->isMemberDataPointer())
1314 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1315 if (!V)
1316 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1317 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001318 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1319 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001320 TemplateParams.push_back(TVP);
1321 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001322 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001323 llvm::DITemplateValueParameter
1324 TVP = DBuilder.createTemplateTemplateParameter(
1325 TheCU, Name, llvm::DIType(),
1326 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001327 TemplateParams.push_back(TVP);
1328 } break;
1329 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001330 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1331 TheCU, Name, llvm::DIType(),
1332 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001333 TemplateParams.push_back(TVP);
1334 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001335 case TemplateArgument::Expression: {
1336 const Expr *E = TA.getAsExpr();
1337 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001338 if (E->isGLValue())
1339 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001340 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001341 assert(V && "Expression in template argument isn't constant");
1342 llvm::DIType TTy = getOrCreateType(T, Unit);
1343 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001344 DBuilder.createTemplateValueParameter(
1345 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001346 TemplateParams.push_back(TVP);
1347 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001348 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001349 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001350 case TemplateArgument::Null:
1351 llvm_unreachable(
1352 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001353 }
1354 }
1355 return DBuilder.getOrCreateArray(TemplateParams);
1356}
1357
1358/// CollectFunctionTemplateParams - A helper function to collect debug
1359/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001360llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1361 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 if (FD->getTemplatedKind() ==
1363 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001364 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1365 ->getTemplate()
1366 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001367 return CollectTemplateParams(
1368 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 }
1370 return llvm::DIArray();
1371}
1372
1373/// CollectCXXTemplateParams - A helper function to collect debug info for
1374/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001375llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1376 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001377 // Always get the full list of parameters, not just the ones from
1378 // the specialization.
1379 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001380 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001381 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001382 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001383}
1384
1385/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1386llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
1387 if (VTablePtrType.isValid())
1388 return VTablePtrType;
1389
1390 ASTContext &Context = CGM.getContext();
1391
1392 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001393 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001394 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1396 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001397 llvm::DIType vtbl_ptr_type =
1398 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001399 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1400 return VTablePtrType;
1401}
1402
1403/// getVTableName - Get vtable name for the given Class.
1404StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001405 // Copy the gdb compatible name on the side and use its reference.
1406 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001407}
1408
Guy Benyei11169dd2012-12-18 14:30:41 +00001409/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1410/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001411void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001412 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001413 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1414
1415 // If there is a primary base then it will hold vtable info.
1416 if (RL.getPrimaryBase())
1417 return;
1418
1419 // If this class is not dynamic then there is not any vtable info to collect.
1420 if (!RD->isDynamicClass())
1421 return;
1422
1423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001424 llvm::DIType VPTR = DBuilder.createMemberType(
1425 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1426 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 EltTys.push_back(VPTR);
1428}
1429
Eric Christopherb2a008c2013-05-16 00:45:12 +00001430/// getOrCreateRecordType - Emit record type's standalone debug info.
1431llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001433 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1435 return T;
1436}
1437
1438/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1439/// debug info.
1440llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001441 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001442 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001444 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 return T;
1446}
1447
David Blaikie483a9da2014-05-06 18:35:21 +00001448void CGDebugInfo::completeType(const EnumDecl *ED) {
1449 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1450 return;
1451 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001452 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001453 auto I = TypeCache.find(TyPtr);
1454 if (I == TypeCache.end() ||
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001455 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001456 return;
1457 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
1458 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001459 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001460}
1461
David Blaikieb2e86eb2013-08-15 20:49:17 +00001462void CGDebugInfo::completeType(const RecordDecl *RD) {
1463 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1464 !CGM.getLangOpts().CPlusPlus)
1465 completeRequiredType(RD);
1466}
1467
1468void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001469 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1470 return;
1471
David Blaikie6943dea2013-08-20 01:28:15 +00001472 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1473 if (CXXDecl->isDynamicClass())
1474 return;
1475
David Blaikieb2e86eb2013-08-15 20:49:17 +00001476 QualType Ty = CGM.getContext().getRecordType(RD);
1477 llvm::DIType T = getTypeOrNull(Ty);
David Blaikie6943dea2013-08-20 01:28:15 +00001478 if (T && T.isForwardDecl())
1479 completeClassData(RD);
1480}
1481
1482void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1483 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001484 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001485 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001486 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001487 auto I = TypeCache.find(TyPtr);
1488 if (I != TypeCache.end() &&
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001489 !llvm::DIType(cast<llvm::MDNode>(I->second)).isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001490 return;
1491 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
1492 assert(!Res.isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001493 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001494}
1495
David Blaikie0e716b42014-03-03 23:48:23 +00001496static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1497 CXXRecordDecl::method_iterator End) {
1498 for (; I != End; ++I)
1499 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001500 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1501 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001502 return true;
1503 return false;
1504}
1505
1506static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1507 const RecordDecl *RD,
1508 const LangOptions &LangOpts) {
1509 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1510 return false;
1511
1512 if (!LangOpts.CPlusPlus)
1513 return false;
1514
1515 if (!RD->isCompleteDefinitionRequired())
1516 return true;
1517
1518 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1519
1520 if (!CXXDecl)
1521 return false;
1522
1523 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1524 return true;
1525
1526 TemplateSpecializationKind Spec = TSK_Undeclared;
1527 if (const ClassTemplateSpecializationDecl *SD =
1528 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1529 Spec = SD->getSpecializationKind();
1530
1531 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1532 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1533 CXXDecl->method_end()))
1534 return true;
1535
1536 return false;
1537}
1538
Guy Benyei11169dd2012-12-18 14:30:41 +00001539/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001540llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001541 RecordDecl *RD = Ty->getDecl();
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001542 llvm::DICompositeType T(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001543 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001544 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001545 T = getOrCreateRecordFwdDecl(
1546 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001547 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001548 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001549
David Blaikieb2e86eb2013-08-15 20:49:17 +00001550 return CreateTypeDefinition(Ty);
1551}
1552
1553llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1554 RecordDecl *RD = Ty->getDecl();
1555
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 // Get overall information about the record type for the debug info.
1557 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1558
1559 // Records and classes and unions can all be recursive. To handle them, we
1560 // first generate a debug descriptor for the struct as a forward declaration.
1561 // Then (if it is a definition) we go through and get debug info for all of
1562 // its members. Finally, we create a descriptor for the complete type (which
1563 // may refer to the forward decl if the struct is recursive) and replace all
1564 // uses of the forward declaration with the final definition.
1565
David Blaikie4a2b5ef2013-08-12 22:24:20 +00001566 llvm::DICompositeType FwdDecl(getOrCreateLimitedType(Ty, DefUnit));
Manman Ren0d441f12013-07-02 19:01:53 +00001567 assert(FwdDecl.isCompositeType() &&
David Blaikie469f0792013-05-22 23:22:42 +00001568 "The debug type of a RecordType should be a llvm::DICompositeType");
Guy Benyei11169dd2012-12-18 14:30:41 +00001569
1570 if (FwdDecl.isForwardDecl())
1571 return FwdDecl;
1572
David Blaikieadfbf992013-08-18 16:55:33 +00001573 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1574 CollectContainingType(CXXDecl, FwdDecl);
1575
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001577 LexicalBlockStack.emplace_back(&*FwdDecl);
1578 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001579
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001581 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001582 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
1584 // Note: The split of CXXDecl information here is intentional, the
1585 // gdb tests will depend on a certain ordering at printout. The debug
1586 // information offsets are still correct if we merge them all together
1587 // though.
1588 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1589 if (CXXDecl) {
1590 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1591 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1592 }
1593
Eric Christopher91a31902013-01-16 01:22:32 +00001594 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001596 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
1599 LexicalBlockStack.pop_back();
1600 RegionMap.erase(Ty->getDecl());
1601
1602 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001603 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001604
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001605 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001606 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001607}
1608
1609/// CreateType - get objective-c object type.
1610llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1611 llvm::DIFile Unit) {
1612 // Ignore protocols.
1613 return getOrCreateType(Ty->getBaseType(), Unit);
1614}
1615
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001616/// \return true if Getter has the default name for the property PD.
1617static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1618 const ObjCMethodDecl *Getter) {
1619 assert(PD);
1620 if (!Getter)
1621 return true;
1622
1623 assert(Getter->getDeclName().isObjCZeroArgSelector());
1624 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001625 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001626}
1627
1628/// \return true if Setter has the default name for the property PD.
1629static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1630 const ObjCMethodDecl *Setter) {
1631 assert(PD);
1632 if (!Setter)
1633 return true;
1634
1635 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001636 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001637 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001638}
1639
Guy Benyei11169dd2012-12-18 14:30:41 +00001640/// CreateType - get objective-c interface type.
1641llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1642 llvm::DIFile Unit) {
1643 ObjCInterfaceDecl *ID = Ty->getDecl();
1644 if (!ID)
1645 return llvm::DIType();
1646
1647 // Get overall information about the record type for the debug info.
1648 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1649 unsigned Line = getLineNumber(ID->getLocation());
Ed Masteda706022014-05-07 12:49:30 +00001650 llvm::dwarf::SourceLanguage RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001651
1652 // If this is just a forward declaration return a special forward-declaration
1653 // debug type since we won't be able to lay out the entire type.
1654 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001655 if (!Def || !Def->getImplementation()) {
David Blaikief427b002014-05-06 03:42:01 +00001656 llvm::DIType FwdDecl = DBuilder.createReplaceableForwardDecl(
1657 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1658 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001659 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 return FwdDecl;
1661 }
1662
David Blaikieef8a9512014-05-05 23:23:53 +00001663 return CreateTypeDefinition(Ty, Unit);
1664}
1665
Eric Christophere7b87e52014-10-26 23:40:33 +00001666llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1667 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001668 ObjCInterfaceDecl *ID = Ty->getDecl();
1669 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1670 unsigned Line = getLineNumber(ID->getLocation());
1671 unsigned RuntimeLang = TheCU.getLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001672
1673 // Bit size, align and offset of the type.
1674 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1675 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1676
1677 unsigned Flags = 0;
1678 if (ID->getImplementation())
1679 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1680
Eric Christophere7b87e52014-10-26 23:40:33 +00001681 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1682 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1683 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
David Blaikieef8a9512014-05-05 23:23:53 +00001685 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001686 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001687
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001688 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001689 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1690 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001691
1692 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001693 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001694
1695 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1696 if (SClass) {
1697 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001698 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001699 if (!SClassTy.isValid())
1700 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001701
Eric Christophere7b87e52014-10-26 23:40:33 +00001702 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001703 EltTys.push_back(InhTag);
1704 }
1705
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001706 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001707 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 SourceLocation Loc = PD->getLocation();
1709 llvm::DIFile PUnit = getOrCreateFile(Loc);
1710 unsigned PLine = getLineNumber(Loc);
1711 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1712 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001713 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1714 PD->getName(), PUnit, PLine,
1715 hasDefaultGetterName(PD, Getter) ? ""
1716 : getSelectorName(PD->getGetterName()),
1717 hasDefaultSetterName(PD, Setter) ? ""
1718 : getSelectorName(PD->getSetterName()),
1719 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001720 EltTys.push_back(PropertyNode);
1721 }
1722
1723 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1724 unsigned FieldNo = 0;
1725 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1726 Field = Field->getNextIvar(), ++FieldNo) {
1727 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1728 if (!FieldTy.isValid())
1729 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001730
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 StringRef FieldName = Field->getName();
1732
1733 // Ignore unnamed fields.
1734 if (FieldName.empty())
1735 continue;
1736
1737 // Get the location for the field.
1738 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1739 unsigned FieldLine = getLineNumber(Field->getLocation());
1740 QualType FType = Field->getType();
1741 uint64_t FieldSize = 0;
1742 unsigned FieldAlign = 0;
1743
1744 if (!FType->isIncompleteArrayType()) {
1745
1746 // Bit size, align and offset of the type.
1747 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001748 ? Field->getBitWidthValue(CGM.getContext())
1749 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001750 FieldAlign = CGM.getContext().getTypeAlign(FType);
1751 }
1752
1753 uint64_t FieldOffset;
1754 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1755 // We don't know the runtime offset of an ivar if we're using the
1756 // non-fragile ABI. For bitfields, use the bit offset into the first
1757 // byte of storage of the bitfield. For other fields, use zero.
1758 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001759 FieldOffset =
1760 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001761 FieldOffset %= CGM.getContext().getCharWidth();
1762 } else {
1763 FieldOffset = 0;
1764 }
1765 } else {
1766 FieldOffset = RL.getFieldOffset(FieldNo);
1767 }
1768
1769 unsigned Flags = 0;
1770 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1771 Flags = llvm::DIDescriptor::FlagProtected;
1772 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1773 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001774 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1775 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001776
Craig Topper8a13c412014-05-21 05:09:00 +00001777 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001779 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001780 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001782 SourceLocation Loc = PD->getLocation();
1783 llvm::DIFile PUnit = getOrCreateFile(Loc);
1784 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001785 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1786 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001787 PropertyNode = DBuilder.createObjCProperty(
1788 PD->getName(), PUnit, PLine,
1789 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1790 PD->getGetterName()),
1791 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1792 PD->getSetterName()),
1793 PD->getPropertyAttributes(),
1794 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 }
1796 }
1797 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001798 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1799 FieldSize, FieldAlign, FieldOffset, Flags,
1800 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001801 EltTys.push_back(FieldTy);
1802 }
1803
1804 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001805 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001806
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001808 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001809}
1810
1811llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1812 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1813 int64_t Count = Ty->getNumElements();
1814 if (Count == 0)
1815 // If number of elements are not known then this is an unbounded array.
1816 // Use Count == -1 to express such arrays.
1817 Count = -1;
1818
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001819 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001820 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1821
1822 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1823 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1824
1825 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1826}
1827
Eric Christophere7b87e52014-10-26 23:40:33 +00001828llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 uint64_t Size;
1830 uint64_t Align;
1831
1832 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1833 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1834 Size = 0;
1835 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001836 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001837 } else if (Ty->isIncompleteArrayType()) {
1838 Size = 0;
1839 if (Ty->getElementType()->isIncompleteType())
1840 Align = 0;
1841 else
1842 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001843 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001844 Size = 0;
1845 Align = 0;
1846 } else {
1847 // Size and align of the whole array, not the element type.
1848 Size = CGM.getContext().getTypeSize(Ty);
1849 Align = CGM.getContext().getTypeAlign(Ty);
1850 }
1851
1852 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1853 // interior arrays, do we care? Why aren't nested arrays represented the
1854 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001855 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 QualType EltTy(Ty, 0);
1857 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1858 // If the number of elements is known, then count is that number. Otherwise,
1859 // it's -1. This allows us to represent a subrange with an array of 0
1860 // elements, like this:
1861 //
1862 // struct foo {
1863 // int x[0];
1864 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001865 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001866 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1867 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001868
Guy Benyei11169dd2012-12-18 14:30:41 +00001869 // FIXME: Verify this is right for VLAs.
1870 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1871 EltTy = Ty->getElementType();
1872 }
1873
1874 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1875
Eric Christophere7b87e52014-10-26 23:40:33 +00001876 llvm::DIType DbgTy = DBuilder.createArrayType(
1877 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 return DbgTy;
1879}
1880
Eric Christopherb2a008c2013-05-16 00:45:12 +00001881llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001883 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1884 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001885}
1886
Eric Christopherb2a008c2013-05-16 00:45:12 +00001887llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001889 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1890 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001891}
1892
Eric Christopherb2a008c2013-05-16 00:45:12 +00001893llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001895 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1896 if (!Ty->getPointeeType()->isFunctionType())
1897 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001898 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001899 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001900
1901 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001902 Ty->getPointeeType()->getAs<FunctionProtoType>();
1903 return DBuilder.createMemberPointerType(
1904 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1905 Ty->getClass(), FPT->getTypeQuals())),
1906 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001907 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001908}
1909
Eric Christophere7b87e52014-10-26 23:40:33 +00001910llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 // Ignore the atomic wrapping
1912 // FIXME: What is the correct representation?
1913 return getOrCreateType(Ty->getValueType(), U);
1914}
1915
1916/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001917llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001918 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001919 uint64_t Size = 0;
1920 uint64_t Align = 0;
1921 if (!ED->getTypeForDecl()->isIncompleteType()) {
1922 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1923 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1924 }
1925
Manman Rene0064d82013-08-29 23:19:58 +00001926 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1927
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 // If this is just a forward declaration, construct an appropriately
1929 // marked node and just return it.
1930 if (!ED->getDefinition()) {
1931 llvm::DIDescriptor EDContext;
1932 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1933 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1934 unsigned Line = getLineNumber(ED->getLocation());
1935 StringRef EDName = ED->getName();
David Blaikief427b002014-05-06 03:42:01 +00001936 llvm::DIType RetTy = DBuilder.createReplaceableForwardDecl(
1937 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
1938 0, Size, Align, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001939 ReplaceMap.emplace_back(
1940 std::piecewise_construct, std::make_tuple(Ty),
1941 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001942 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001943 }
1944
David Blaikie483a9da2014-05-06 18:35:21 +00001945 return CreateTypeDefinition(Ty);
1946}
1947
1948llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1949 const EnumDecl *ED = Ty->getDecl();
1950 uint64_t Size = 0;
1951 uint64_t Align = 0;
1952 if (!ED->getTypeForDecl()->isIncompleteType()) {
1953 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1954 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1955 }
1956
1957 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1958
Guy Benyei11169dd2012-12-18 14:30:41 +00001959 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001960 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001962 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001963 Enumerators.push_back(DBuilder.createEnumerator(
1964 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001965 }
1966
1967 // Return a CompositeType for the enum itself.
1968 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1969
1970 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1971 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001972 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001973 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1974 llvm::DIType ClassTy = ED->isFixed()
1975 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1976 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001977 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001978 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1979 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001980 return DbgTy;
1981}
1982
David Blaikie05491062013-01-21 04:37:12 +00001983static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1984 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001986 Qualifiers InnerQuals = T.getLocalQualifiers();
1987 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1988 // that is already there.
1989 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1990 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 QualType LastT = T;
1992 switch (T->getTypeClass()) {
1993 default:
David Blaikie05491062013-01-21 04:37:12 +00001994 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001995 case Type::TemplateSpecialization: {
1996 const auto *Spec = cast<TemplateSpecializationType>(T);
1997 if (Spec->isTypeAlias())
1998 return C.getQualifiedType(T.getTypePtr(), Quals);
1999 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002000 break;
2001 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 case Type::TypeOfExpr:
2003 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2004 break;
2005 case Type::TypeOf:
2006 T = cast<TypeOfType>(T)->getUnderlyingType();
2007 break;
2008 case Type::Decltype:
2009 T = cast<DecltypeType>(T)->getUnderlyingType();
2010 break;
2011 case Type::UnaryTransform:
2012 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2013 break;
2014 case Type::Attributed:
2015 T = cast<AttributedType>(T)->getEquivalentType();
2016 break;
2017 case Type::Elaborated:
2018 T = cast<ElaboratedType>(T)->getNamedType();
2019 break;
2020 case Type::Paren:
2021 T = cast<ParenType>(T)->getInnerType();
2022 break;
David Blaikie05491062013-01-21 04:37:12 +00002023 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 break;
2026 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002027 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002028 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002029 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 break;
2031 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002032
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002034 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 } while (true);
2036}
2037
Eric Christopher0fdcb312013-05-16 00:52:20 +00002038/// getType - Get the type from the cache or return null type if it doesn't
2039/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002040llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2041
2042 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002043 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002044
David Blaikief427b002014-05-06 03:42:01 +00002045 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 if (it != TypeCache.end()) {
2047 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002048 if (llvm::Metadata *V = it->second)
Guy Benyei11169dd2012-12-18 14:30:41 +00002049 return llvm::DIType(cast<llvm::MDNode>(V));
2050 }
2051
2052 return llvm::DIType();
2053}
2054
David Blaikie0e716b42014-03-03 23:48:23 +00002055void CGDebugInfo::completeTemplateDefinition(
2056 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002057 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2058 return;
2059
David Blaikie0e716b42014-03-03 23:48:23 +00002060 completeClassData(&SD);
2061 // In case this type has no member function definitions being emitted, ensure
2062 // it is retained
2063 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2064}
2065
Guy Benyei11169dd2012-12-18 14:30:41 +00002066/// getOrCreateType - Get the type from the cache or create a new
2067/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002068llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 if (Ty.isNull())
2070 return llvm::DIType();
2071
2072 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002073 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002074
David Blaikieef8a9512014-05-05 23:23:53 +00002075 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 return T;
2077
2078 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002079 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002080 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002081
2082 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002083 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002084
Guy Benyei11169dd2012-12-18 14:30:41 +00002085 return Res;
2086}
2087
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002088/// Currently the checksum of an interface includes the number of
2089/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002090unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002091 // The assumption is that the number of ivars can only increase
2092 // monotonically, so it is safe to just use their current number as
2093 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002094 unsigned Sum = 0;
2095 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002096 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002097 ++Sum;
2098
2099 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002100}
2101
2102ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2103 switch (Ty->getTypeClass()) {
2104 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002105 return getObjCInterfaceDecl(
2106 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002107 case Type::ObjCInterface:
2108 return cast<ObjCInterfaceType>(Ty)->getDecl();
2109 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002110 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002111 }
2112}
2113
Guy Benyei11169dd2012-12-18 14:30:41 +00002114/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002115llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 // Handle qualifiers, which recursively handles what they refer to.
2117 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002118 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002119
Guy Benyei11169dd2012-12-18 14:30:41 +00002120 // Work out details of type.
2121 switch (Ty->getTypeClass()) {
2122#define TYPE(Class, Base)
2123#define ABSTRACT_TYPE(Class, Base)
2124#define NON_CANONICAL_TYPE(Class, Base)
2125#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2126#include "clang/AST/TypeNodes.def"
2127 llvm_unreachable("Dependent types cannot show up in debug information");
2128
2129 case Type::ExtVector:
2130 case Type::Vector:
2131 return CreateType(cast<VectorType>(Ty), Unit);
2132 case Type::ObjCObjectPointer:
2133 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2134 case Type::ObjCObject:
2135 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2136 case Type::ObjCInterface:
2137 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2138 case Type::Builtin:
2139 return CreateType(cast<BuiltinType>(Ty));
2140 case Type::Complex:
2141 return CreateType(cast<ComplexType>(Ty));
2142 case Type::Pointer:
2143 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002144 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002145 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002146 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002147 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002148 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 case Type::BlockPointer:
2150 return CreateType(cast<BlockPointerType>(Ty), Unit);
2151 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002152 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002154 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002155 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002156 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002157 case Type::FunctionProto:
2158 case Type::FunctionNoProto:
2159 return CreateType(cast<FunctionType>(Ty), Unit);
2160 case Type::ConstantArray:
2161 case Type::VariableArray:
2162 case Type::IncompleteArray:
2163 return CreateType(cast<ArrayType>(Ty), Unit);
2164
2165 case Type::LValueReference:
2166 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2167 case Type::RValueReference:
2168 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2169
2170 case Type::MemberPointer:
2171 return CreateType(cast<MemberPointerType>(Ty), Unit);
2172
2173 case Type::Atomic:
2174 return CreateType(cast<AtomicType>(Ty), Unit);
2175
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002177 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2178
David Blaikie42edade2014-11-11 20:44:45 +00002179 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002180 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 case Type::Elaborated:
2182 case Type::Paren:
2183 case Type::SubstTemplateTypeParm:
2184 case Type::TypeOfExpr:
2185 case Type::TypeOf:
2186 case Type::Decltype:
2187 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002188 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002189 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002190 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002191
David Blaikie42edade2014-11-11 20:44:45 +00002192 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002193}
2194
2195/// getOrCreateLimitedType - Get the type from the cache or create a new
2196/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002197llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002198 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002199 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002200
David Blaikie8d5e1282013-08-20 21:03:29 +00002201 llvm::DICompositeType T(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002202
2203 // We may have cached a forward decl when we could have created
2204 // a non-forward decl. Go ahead and create a non-forward decl
2205 // now.
Eric Christophere7b87e52014-10-26 23:40:33 +00002206 if (T && !T.isForwardDecl())
2207 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
2209 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002210 llvm::DICompositeType Res = CreateLimitedType(Ty);
2211
2212 // Propagate members from the declaration to the definition
2213 // CreateType(const RecordType*) will overwrite this with the members in the
2214 // correct order if the full type is needed.
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002215 DBuilder.replaceArrays(Res, T.getElements());
Guy Benyei11169dd2012-12-18 14:30:41 +00002216
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002218 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002219 return Res;
2220}
2221
2222// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002223llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002225
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 // Get overall information about the record type for the debug info.
2227 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2228 unsigned Line = getLineNumber(RD->getLocation());
2229 StringRef RDName = getClassName(RD);
2230
Eric Christopher07429ff2013-10-15 21:22:34 +00002231 llvm::DIDescriptor RDContext =
2232 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002233
David Blaikied2785892013-08-18 17:36:19 +00002234 // If we ended up creating the type during the context chain construction,
2235 // just return that.
David Blaikie8d5e1282013-08-20 21:03:29 +00002236 llvm::DICompositeType T(getTypeOrNull(CGM.getContext().getRecordType(RD)));
2237 if (T && (!T.isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002238 return T;
David Blaikied2785892013-08-18 17:36:19 +00002239
Adrian Prantl381e7552014-02-04 21:29:50 +00002240 // If this is just a forward or incomplete declaration, construct an
2241 // appropriately marked node and just return it.
2242 const RecordDecl *D = RD->getDefinition();
2243 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002244 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002245
2246 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2247 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002248 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002249
Manman Rene0064d82013-08-29 23:19:58 +00002250 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2251
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 if (RD->isUnion())
Eric Christophere7b87e52014-10-26 23:40:33 +00002253 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line, Size,
2254 Align, 0, llvm::DIArray(), 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 else if (RD->isClass()) {
2256 // FIXME: This could be a struct type giving a default visibility different
2257 // than C++ class type, but needs llvm metadata changes first.
Eric Christophere7b87e52014-10-26 23:40:33 +00002258 RealDecl = DBuilder.createClassType(
2259 RDContext, RDName, DefUnit, Line, Size, Align, 0, 0, llvm::DIType(),
2260 llvm::DIArray(), llvm::DIType(), llvm::DIArray(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002261 } else
Eric Christophere7b87e52014-10-26 23:40:33 +00002262 RealDecl = DBuilder.createStructType(
2263 RDContext, RDName, DefUnit, Line, Size, Align, 0, llvm::DIType(),
2264 llvm::DIArray(), 0, llvm::DIType(), FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002265
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002266 RegionMap[Ty->getDecl()].reset(RealDecl);
2267 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002268
David Blaikieadfbf992013-08-18 16:55:33 +00002269 if (const ClassTemplateSpecializationDecl *TSpecial =
2270 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002271 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2272 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002273 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002274}
2275
David Blaikieadfbf992013-08-18 16:55:33 +00002276void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2277 llvm::DICompositeType RealDecl) {
2278 // A class's primary base or the class itself contains the vtable.
2279 llvm::DICompositeType ContainingType;
2280 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2281 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002282 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002283 while (1) {
2284 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2285 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2286 if (PBT && !BRL.isPrimaryBaseVirtual())
2287 PBase = PBT;
2288 else
2289 break;
2290 }
2291 ContainingType = llvm::DICompositeType(
2292 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2293 getOrCreateFile(RD->getLocation())));
2294 } else if (RD->isDynamicClass())
2295 ContainingType = RealDecl;
2296
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002297 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002298}
2299
Guy Benyei11169dd2012-12-18 14:30:41 +00002300/// CreateMemberType - Create new member and increase Offset by FType's size.
2301llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002302 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2304 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2305 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002306 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2307 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 *Offset += FieldSize;
2309 return Ty;
2310}
2311
Frederic Riss9db79f12014-11-18 03:40:46 +00002312void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2313 llvm::DIFile Unit,
2314 StringRef &Name, StringRef &LinkageName,
2315 llvm::DIDescriptor &FDContext,
2316 llvm::DIArray &TParamsArray,
2317 unsigned &Flags) {
2318 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2319 Name = getFunctionName(FD);
2320 // Use mangled name as linkage name for C/C++ functions.
2321 if (FD->hasPrototype()) {
2322 LinkageName = CGM.getMangledName(GD);
2323 Flags |= llvm::DIDescriptor::FlagPrototyped;
2324 }
2325 // No need to replicate the linkage name if it isn't different from the
2326 // subprogram name, no need to have it at all unless coverage is enabled or
2327 // debug is set to more than just line tables.
2328 if (LinkageName == Name ||
2329 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2330 !CGM.getCodeGenOpts().EmitGcovNotes &&
2331 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2332 LinkageName = StringRef();
2333
2334 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2335 if (const NamespaceDecl *NSDecl =
2336 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2337 FDContext = getOrCreateNameSpace(NSDecl);
2338 else if (const RecordDecl *RDecl =
2339 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2340 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2341 // Collect template parameters.
2342 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2343 }
2344}
2345
2346void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2347 unsigned &LineNo, QualType &T,
2348 StringRef &Name, StringRef &LinkageName,
2349 llvm::DIDescriptor &VDContext) {
2350 Unit = getOrCreateFile(VD->getLocation());
2351 LineNo = getLineNumber(VD->getLocation());
2352
2353 setLocation(VD->getLocation());
2354
2355 T = VD->getType();
2356 if (T->isIncompleteArrayType()) {
2357 // CodeGen turns int[] into int[1] so we'll do the same here.
2358 llvm::APInt ConstVal(32, 1);
2359 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2360
2361 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2362 ArrayType::Normal, 0);
2363 }
2364
2365 Name = VD->getName();
2366 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2367 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2368 LinkageName = CGM.getMangledName(VD);
2369 if (LinkageName == Name)
2370 LinkageName = StringRef();
2371
2372 // Since we emit declarations (DW_AT_members) for static members, place the
2373 // definition of those static members in the namespace they were declared in
2374 // in the source code (the lexical decl context).
2375 // FIXME: Generalize this for even non-member global variables where the
2376 // declaration and definition may have different lexical decl contexts, once
2377 // we have support for emitting declarations of (non-member) global variables.
2378 VDContext = getContextDescriptor(
2379 dyn_cast<Decl>(VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2380 : VD->getDeclContext()));
2381}
2382
Frederic Rissd253ed62014-11-18 03:40:51 +00002383llvm::DISubprogram
2384CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2385 llvm::DIArray TParamsArray;
2386 StringRef Name, LinkageName;
2387 unsigned Flags = 0;
2388 SourceLocation Loc = FD->getLocation();
2389 llvm::DIFile Unit = getOrCreateFile(Loc);
2390 llvm::DIDescriptor DContext(Unit);
2391 unsigned Line = getLineNumber(Loc);
2392
2393 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2394 TParamsArray, Flags);
2395 // Build function type.
2396 SmallVector<QualType, 16> ArgTypes;
2397 for (const ParmVarDecl *Parm: FD->parameters())
2398 ArgTypes.push_back(Parm->getType());
2399 QualType FnType =
2400 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2401 FunctionProtoType::ExtProtoInfo());
2402 llvm::DISubprogram SP =
2403 DBuilder.createTempFunctionFwdDecl(DContext, Name, LinkageName, Unit, Line,
2404 getOrCreateFunctionType(FD, FnType, Unit),
2405 !FD->isExternallyVisible(),
2406 false /*declaration*/, 0, Flags,
2407 CGM.getLangOpts().Optimize, nullptr,
2408 TParamsArray, getFunctionDeclaration(FD));
2409 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002410 FwdDeclReplaceMap.emplace_back(
2411 std::piecewise_construct, std::make_tuple(CanonDecl),
2412 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002413 return SP;
2414}
2415
2416llvm::DIGlobalVariable
2417CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2418 QualType T;
2419 StringRef Name, LinkageName;
2420 SourceLocation Loc = VD->getLocation();
2421 llvm::DIFile Unit = getOrCreateFile(Loc);
2422 llvm::DIDescriptor DContext(Unit);
2423 unsigned Line = getLineNumber(Loc);
2424
2425 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2426 llvm::DIGlobalVariable GV =
2427 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2428 Line, getOrCreateType(T, Unit),
2429 !VD->isExternallyVisible(),
2430 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002431 FwdDeclReplaceMap.emplace_back(
2432 std::piecewise_construct,
2433 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2434 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002435 return GV;
2436}
2437
Frederic Riss442293e2014-11-06 21:12:06 +00002438llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002439 // We only need a declaration (not a definition) of the type - so use whatever
2440 // we would otherwise do to get a type for a pointee. (forward declarations in
2441 // limited debug info, full definitions (if the type definition is available)
2442 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002443 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2444 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002445 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002446 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002447
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002448 if (I != DeclCache.end())
2449 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002450
2451 // No definition for now. Emit a forward definition that might be
2452 // merged with a potential upcoming definition.
2453 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2454 return getFunctionForwardDeclaration(FD);
2455 else if (const auto *VD = dyn_cast<VarDecl>(D))
2456 return getGlobalVariableForwardDeclaration(VD);
2457
2458 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002459}
2460
Guy Benyei11169dd2012-12-18 14:30:41 +00002461/// getFunctionDeclaration - Return debug info descriptor to describe method
2462/// declaration for the given method definition.
2463llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002464 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002465 return llvm::DISubprogram();
2466
Guy Benyei11169dd2012-12-18 14:30:41 +00002467 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002468 if (!FD)
2469 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002470
2471 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002472 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002473
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002474 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002475 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002476 if (const CXXMethodDecl *MD =
2477 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
David Blaikiefd07c602013-08-09 17:20:05 +00002478 llvm::DICompositeType T(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002479 llvm::DISubprogram SP =
2480 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002481 return SP;
2482 }
2483 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002485 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002486 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 return SP;
2488 }
2489
Aaron Ballman86c93902014-03-06 23:45:36 +00002490 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002491 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002492 if (MI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002493 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(MI->second));
David Blaikie18cfbc52013-06-22 00:09:36 +00002494 if (SP.isSubprogram() && !SP.isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 return SP;
2496 }
2497 }
2498 return llvm::DISubprogram();
2499}
2500
2501// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2502// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002503llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2504 QualType FnType,
2505 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002506 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002507 // Create fake but valid subroutine type. Otherwise
2508 // llvm::DISubprogram::Verify() would return false, and
2509 // subprogram DIE will miss DW_AT_decl_file and
2510 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002511 return DBuilder.createSubroutineType(F,
2512 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002513
2514 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2515 return getOrCreateMethodType(Method, F);
2516 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2517 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002518 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002519
2520 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002521 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002522
2523 // Replace the instancetype keyword with the actual type.
2524 if (ResultTy == CGM.getContext().getObjCInstanceType())
2525 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002526 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002527
Adrian Prantl7bec9032013-05-10 21:08:31 +00002528 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002530 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2531 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2532 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002533 // "_cmd" pointer is always second argument.
2534 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2535 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2536 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002537 for (const auto *PI : OMethod->params())
2538 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002539 // Variadic methods need a special marker at the end of the type list.
2540 if (OMethod->isVariadic())
2541 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002542
Manman Ren67f005e2014-07-28 22:24:34 +00002543 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 return DBuilder.createSubroutineType(F, EltTypeArray);
2545 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002546
Adrian Prantl800faef2014-02-25 23:42:18 +00002547 // Handle variadic function types; they need an additional
2548 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002549 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2550 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002551 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002552 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2553 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2554 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2555 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2556 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002557 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002558 return DBuilder.createSubroutineType(F, EltTypeArray);
2559 }
2560
David Blaikie469f0792013-05-22 23:22:42 +00002561 return llvm::DICompositeType(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002562}
2563
2564/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002565void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2566 SourceLocation ScopeLoc, QualType FnType,
2567 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002568
2569 StringRef Name;
2570 StringRef LinkageName;
2571
2572 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2573
2574 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002575 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002576
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 unsigned Flags = 0;
2578 llvm::DIFile Unit = getOrCreateFile(Loc);
2579 llvm::DIDescriptor FDContext(Unit);
2580 llvm::DIArray TParamsArray;
2581 if (!HasDecl) {
2582 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002583 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2585 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002586 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 if (FI != SPCache.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002588 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2590 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002591 LexicalBlockStack.emplace_back(SPN);
2592 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 return;
2594 }
2595 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002596 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2597 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002598 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2599 Name = getObjCMethodName(OMD);
2600 Flags |= llvm::DIDescriptor::FlagPrototyped;
2601 } else {
2602 // Use llvm function name.
2603 Name = Fn->getName();
2604 Flags |= llvm::DIDescriptor::FlagPrototyped;
2605 }
2606 if (!Name.empty() && Name[0] == '\01')
2607 Name = Name.substr(1);
2608
Adrian Prantl42d71b92014-04-10 23:21:53 +00002609 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002610 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002611 // Artificial functions without a location should not silently reuse CurLoc.
2612 if (Loc.isInvalid())
2613 CurLoc = SourceLocation();
2614 }
2615 unsigned LineNo = getLineNumber(Loc);
2616 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002617
Eric Christopher8018e412014-03-27 18:50:35 +00002618 // FIXME: The function declaration we're constructing here is mostly reusing
2619 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2620 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2621 // all subprograms instead of the actual context since subprogram definitions
2622 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002623 llvm::DISubprogram SP = DBuilder.createFunction(
2624 FDContext, Name, LinkageName, Unit, LineNo,
2625 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2626 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
2627 TParamsArray, getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002628 // We might get here with a VarDecl in the case we're generating
2629 // code for the initialization of globals. Do not record these decls
2630 // as they will overwrite the actual VarDecl Decl in the cache.
2631 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002632 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002633
Adrian Prantlbebb8932014-03-21 21:01:58 +00002634 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002635 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002636 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002637
Guy Benyei11169dd2012-12-18 14:30:41 +00002638 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002639 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002640}
2641
2642/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002643/// information in the source file. If the location is invalid, the
2644/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002645void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002646 // Update our current location
2647 setLocation(Loc);
2648
Eric Christophere7b87e52014-10-26 23:40:33 +00002649 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2650 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002651
Adrian Prantle83b1302014-01-07 22:05:52 +00002652 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002653 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002654 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002655}
2656
2657/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2658/// the stack.
2659void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002660 llvm::MDNode *Back = nullptr;
2661 if (!LexicalBlockStack.empty())
2662 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002663 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002664 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2665 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002666 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002667 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002668}
2669
2670/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2671/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002672void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2673 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 // Set our current location.
2675 setLocation(Loc);
2676
Guy Benyei11169dd2012-12-18 14:30:41 +00002677 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002678 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2679 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002680
2681 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2682 return;
2683
2684 // Create a new lexical block and push it on the stack.
2685 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002686}
2687
2688/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2689/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002690void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2691 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2693
2694 // Provide an entry in the line table for the end of the block.
2695 EmitLocation(Builder, Loc);
2696
David Blaikie60a877b2014-10-22 19:34:33 +00002697 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2698 return;
2699
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 LexicalBlockStack.pop_back();
2701}
2702
2703/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2704void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2705 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2706 unsigned RCount = FnBeginRegionCount.back();
2707 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2708
2709 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002710 while (LexicalBlockStack.size() != RCount) {
2711 // Provide an entry in the line table for the end of the block.
2712 EmitLocation(Builder, CurLoc);
2713 LexicalBlockStack.pop_back();
2714 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002715 FnBeginRegionCount.pop_back();
2716}
2717
Eric Christopherb2a008c2013-05-16 00:45:12 +00002718// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002719// See BuildByRefType.
2720llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2721 uint64_t *XOffset) {
2722
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002723 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002724 QualType FType;
2725 uint64_t FieldSize, FieldOffset;
2726 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002727
Guy Benyei11169dd2012-12-18 14:30:41 +00002728 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002729 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002730
2731 FieldOffset = 0;
2732 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2733 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2734 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2735 FType = CGM.getContext().IntTy;
2736 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2737 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2738
2739 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2740 if (HasCopyAndDispose) {
2741 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002742 EltTys.push_back(
2743 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2744 EltTys.push_back(
2745 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 }
2747 bool HasByrefExtendedLayout;
2748 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002749 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2750 HasByrefExtendedLayout) &&
2751 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002752 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002753 EltTys.push_back(
2754 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002755 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002756
Guy Benyei11169dd2012-12-18 14:30:41 +00002757 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2758 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002759 CGM.getTarget().getPointerAlign(0))) {
2760 CharUnits FieldOffsetInBytes =
2761 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2762 CharUnits AlignedOffsetInBytes =
2763 FieldOffsetInBytes.RoundUpToAlignment(Align);
2764 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002765
Guy Benyei11169dd2012-12-18 14:30:41 +00002766 if (NumPaddingBytes.isPositive()) {
2767 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2768 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2769 pad, ArrayType::Normal, 0);
2770 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2771 }
2772 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002773
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002775 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 FieldSize = CGM.getContext().getTypeSize(FType);
2777 FieldAlign = CGM.getContext().toBits(Align);
2778
Eric Christopherb2a008c2013-05-16 00:45:12 +00002779 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002780 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2781 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 EltTys.push_back(FieldTy);
2783 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002784
Guy Benyei11169dd2012-12-18 14:30:41 +00002785 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002786
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002788
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002790 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002791}
2792
2793/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002794void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002795 llvm::Value *Storage, unsigned ArgNo,
2796 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002797 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002798 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2799
David Blaikie7fceebf2013-08-19 03:37:48 +00002800 bool Unwritten =
2801 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2802 cast<Decl>(VD->getDeclContext())->isImplicit());
2803 llvm::DIFile Unit;
2804 if (!Unwritten)
2805 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002806 llvm::DIType Ty;
2807 uint64_t XOffset = 0;
2808 if (VD->hasAttr<BlocksAttr>())
2809 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002810 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002811 Ty = getOrCreateType(VD->getType(), Unit);
2812
2813 // If there is no debug info for this type then do not emit debug info
2814 // for this variable.
2815 if (!Ty)
2816 return;
2817
Guy Benyei11169dd2012-12-18 14:30:41 +00002818 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002819 unsigned Line = 0;
2820 unsigned Column = 0;
2821 if (!Unwritten) {
2822 Line = getLineNumber(VD->getLocation());
2823 Column = getColumnNumber(VD->getLocation());
2824 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002825 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 unsigned Flags = 0;
2827 if (VD->isImplicit())
2828 Flags |= llvm::DIDescriptor::FlagArtificial;
2829 // If this is the first argument and it is implicit then
2830 // give it an object pointer flag.
2831 // FIXME: There has to be a better way to do this, but for static
2832 // functions there won't be an implicit param at arg1 and
2833 // otherwise it is 'self' or 'this'.
2834 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2835 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002836 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002837 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2838 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002839 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002840
2841 llvm::MDNode *Scope = LexicalBlockStack.back();
2842
2843 StringRef Name = VD->getName();
2844 if (!Name.empty()) {
2845 if (VD->hasAttr<BlocksAttr>()) {
2846 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002847 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 // offset of __forwarding field
2849 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002850 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002851 Expr.push_back(offset.getQuantity());
2852 Expr.push_back(llvm::dwarf::DW_OP_deref);
2853 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 // offset of x field
2855 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002856 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002857
2858 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002859 llvm::DIVariable D = DBuilder.createLocalVariable(
2860 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002861
Guy Benyei11169dd2012-12-18 14:30:41 +00002862 // Insert an llvm.dbg.declare into the current block.
2863 llvm::Instruction *Call =
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002864 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Eric Christophere7b87e52014-10-26 23:40:33 +00002865 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2867 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002868 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002869 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002870 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2871 // If VD is an anonymous union then Storage represents value for
2872 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002873 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002874 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002875 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2877 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002878
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 // Ignore unnamed fields. Do not ignore unnamed records.
2880 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2881 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002882
Guy Benyei11169dd2012-12-18 14:30:41 +00002883 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002884 llvm::DIVariable D = DBuilder.createLocalVariable(
2885 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2886 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002887
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002889 llvm::Instruction *Call = DBuilder.insertDeclare(
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002890 Storage, D, DBuilder.createExpression(Expr),
2891 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2893 }
David Blaikie219c7d92013-01-05 20:03:07 +00002894 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 }
2896 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002897
2898 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002899 llvm::DIVariable D = DBuilder.createLocalVariable(
2900 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2901 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002902
2903 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00002904 llvm::Instruction *Call = DBuilder.insertDeclare(
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002905 Storage, D, DBuilder.createExpression(Expr), Builder.GetInsertBlock());
David Blaikiea76a7c92013-01-05 05:58:35 +00002906 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002907}
2908
2909void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2910 llvm::Value *Storage,
2911 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002912 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002913 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2914}
2915
Adrian Prantlde17db32013-03-29 19:20:29 +00002916/// Look up the completed type for a self pointer in the TypeCache and
2917/// create a copy of it with the ObjectPointer and Artificial flags
2918/// set. If the type is not cached, a new one is created. This should
2919/// never happen though, since creating a type for the implicit self
2920/// argument implies that we already parsed the interface definition
2921/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002922llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2923 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002924 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002925 if (CachedTy)
2926 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002927 return DBuilder.createObjectPointerType(Ty);
2928}
2929
Eric Christophere7b87e52014-10-26 23:40:33 +00002930void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2931 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002932 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002933 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002935
Craig Topper8a13c412014-05-21 05:09:00 +00002936 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002938
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002940
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 uint64_t XOffset = 0;
2942 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2943 llvm::DIType Ty;
2944 if (isByRef)
2945 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002946 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 Ty = getOrCreateType(VD->getType(), Unit);
2948
2949 // Self is passed along as an implicit non-arg variable in a
2950 // block. Mark it as the object pointer.
2951 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002952 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002953
2954 // Get location information.
2955 unsigned Line = getLineNumber(VD->getLocation());
2956 unsigned Column = getColumnNumber(VD->getLocation());
2957
2958 const llvm::DataLayout &target = CGM.getDataLayout();
2959
2960 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002961 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002962 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2963
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002964 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002965 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002966 addr.push_back(llvm::dwarf::DW_OP_deref);
2967 addr.push_back(llvm::dwarf::DW_OP_plus);
2968 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002970 addr.push_back(llvm::dwarf::DW_OP_deref);
2971 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002973 offset =
2974 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002975 addr.push_back(offset.getQuantity());
2976 addr.push_back(llvm::dwarf::DW_OP_deref);
2977 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 // offset of x field
2979 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002980 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 }
2982
2983 // Create the descriptor for the variable.
2984 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002985 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2986 llvm::DIDescriptor(LexicalBlockStack.back()),
2987 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002988
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl88eec392014-11-21 00:35:25 +00002990 llvm::Instruction *Call = InsertPoint ?
2991 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2992 InsertPoint)
2993 : DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr),
2994 Builder.GetInsertBlock());
Eric Christophere7b87e52014-10-26 23:40:33 +00002995 Call->setDebugLoc(
2996 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002997}
2998
2999/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3000/// variable declaration.
3001void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3002 unsigned ArgNo,
3003 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003004 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3006}
3007
3008namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003009struct BlockLayoutChunk {
3010 uint64_t OffsetInBits;
3011 const BlockDecl::Capture *Capture;
3012};
3013bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3014 return l.OffsetInBits < r.OffsetInBits;
3015}
Guy Benyei11169dd2012-12-18 14:30:41 +00003016}
3017
3018void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003019 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003020 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003021 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003023 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 ASTContext &C = CGM.getContext();
3025 const BlockDecl *blockDecl = block.getBlockDecl();
3026
3027 // Collect some general information about the block's location.
3028 SourceLocation loc = blockDecl->getCaretLocation();
3029 llvm::DIFile tunit = getOrCreateFile(loc);
3030 unsigned line = getLineNumber(loc);
3031 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003032
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 // Build the debug-info type for the block literal.
3034 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3035
3036 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003037 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003038
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003039 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3041 blockLayout->getElementOffsetInBits(0),
3042 tunit, tunit));
3043 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3044 blockLayout->getElementOffsetInBits(1),
3045 tunit, tunit));
3046 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3047 blockLayout->getElementOffsetInBits(2),
3048 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003049 auto *FnTy = block.getBlockExpr()->getFunctionType();
3050 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3051 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 blockLayout->getElementOffsetInBits(3),
3053 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003054 fields.push_back(createFieldType(
3055 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3056 ? C.getBlockDescriptorExtendedType()
3057 : C.getBlockDescriptorType()),
3058 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003059
3060 // We want to sort the captures by offset, not because DWARF
3061 // requires this, but because we're paranoid about debuggers.
3062 SmallVector<BlockLayoutChunk, 8> chunks;
3063
3064 // 'this' capture.
3065 if (blockDecl->capturesCXXThis()) {
3066 BlockLayoutChunk chunk;
3067 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003068 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003069 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 chunks.push_back(chunk);
3071 }
3072
3073 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003074 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 const VarDecl *variable = capture.getVariable();
3076 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3077
3078 // Ignore constant captures.
3079 if (captureInfo.isConstant())
3080 continue;
3081
3082 BlockLayoutChunk chunk;
3083 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003084 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003085 chunk.Capture = &capture;
3086 chunks.push_back(chunk);
3087 }
3088
3089 // Sort by offset.
3090 llvm::array_pod_sort(chunks.begin(), chunks.end());
3091
Eric Christophere7b87e52014-10-26 23:40:33 +00003092 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3093 e = chunks.end();
3094 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 uint64_t offsetInBits = i->OffsetInBits;
3096 const BlockDecl::Capture *capture = i->Capture;
3097
3098 // If we have a null capture, this must be the C++ 'this' capture.
3099 if (!capture) {
3100 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003101 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 QualType type = method->getThisType(C);
3103
3104 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3105 offsetInBits, tunit, tunit));
3106 continue;
3107 }
3108
3109 const VarDecl *variable = capture->getVariable();
3110 StringRef name = variable->getName();
3111
3112 llvm::DIType fieldType;
3113 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003114 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003115
3116 // FIXME: this creates a second copy of this type!
3117 uint64_t xoffset;
3118 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003119 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3120 fieldType =
3121 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3122 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003123 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003124 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3125 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003126 }
3127 fields.push_back(fieldType);
3128 }
3129
3130 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003131 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3132 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003133
3134 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3135
3136 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003137 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3138 CGM.getContext().toBits(block.BlockSize),
3139 CGM.getContext().toBits(block.BlockAlign), 0,
3140 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3142
3143 // Get overall information about the block.
3144 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3145 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003146
3147 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003148 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3149 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3150 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3151 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003152
Adrian Prantl616bef42013-03-14 21:52:59 +00003153 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003154 // Insert an llvm.dbg.value into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003155 llvm::Instruction *DbgVal = DBuilder.insertDbgValueIntrinsic(
3156 LocalAddr, 0, debugVar, DBuilder.createExpression(),
3157 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003158 DbgVal->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
3159 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003160
Adrian Prantl616bef42013-03-14 21:52:59 +00003161 // Insert an llvm.dbg.declare into the current block.
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 llvm::Instruction *DbgDecl = DBuilder.insertDeclare(
3163 Arg, debugVar, DBuilder.createExpression(), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003164 DbgDecl->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003165}
3166
David Blaikie6943dea2013-08-20 01:28:15 +00003167/// If D is an out-of-class definition of a static data member of a class, find
3168/// its corresponding in-class declaration.
3169llvm::DIDerivedType
3170CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3171 if (!D->isStaticDataMember())
3172 return llvm::DIDerivedType();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003173 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003174 if (MI != StaticDataMemberCache.end()) {
3175 assert(MI->second && "Static data member declaration should still exist");
3176 return llvm::DIDerivedType(cast<llvm::MDNode>(MI->second));
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003177 }
David Blaikiece763042013-08-20 21:49:21 +00003178
3179 // If the member wasn't found in the cache, lazily construct and add it to the
3180 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003181 auto DC = D->getDeclContext();
3182 llvm::DICompositeType Ctxt(getContextDescriptor(cast<Decl>(DC)));
3183 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003184}
3185
Eric Christophercab9fae2014-04-10 05:20:00 +00003186/// Recursively collect all of the member fields of a global anonymous decl and
3187/// create static variables for them. The first time this is called it needs
3188/// to be on a union and then from there we can have additional unnamed fields.
3189llvm::DIGlobalVariable
3190CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3191 unsigned LineNo, StringRef LinkageName,
3192 llvm::GlobalVariable *Var,
3193 llvm::DIDescriptor DContext) {
3194 llvm::DIGlobalVariable GV;
3195
3196 for (const auto *Field : RD->fields()) {
3197 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3198 StringRef FieldName = Field->getName();
3199
3200 // Ignore unnamed fields, but recurse into anonymous records.
3201 if (FieldName.empty()) {
3202 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3203 if (RT)
3204 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3205 Var, DContext);
3206 continue;
3207 }
3208 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003209 GV = DBuilder.createGlobalVariable(
3210 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3211 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003212 }
3213 return GV;
3214}
3215
Guy Benyei11169dd2012-12-18 14:30:41 +00003216/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003217void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003219 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003221 llvm::DIFile Unit;
3222 llvm::DIDescriptor DContext;
3223 unsigned LineNo;
3224 StringRef DeclName, LinkageName;
3225 QualType T;
3226 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003227
3228 // Attempt to store one global variable for the declaration - even if we
3229 // emit a lot of fields.
3230 llvm::DIGlobalVariable GV;
3231
3232 // If this is an anonymous union then we'll want to emit a global
3233 // variable for each member of the anonymous union so that it's possible
3234 // to find the name of any field in the union.
3235 if (T->isUnionType() && DeclName.empty()) {
3236 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003237 assert(RD->isAnonymousStructOrUnion() &&
3238 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003239 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3240 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003241 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003242 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3243 Var->hasInternalLinkage(), Var,
3244 getOrCreateStaticDataMemberDeclarationOrNull(D));
3245 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003246 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003247}
3248
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003249/// EmitGlobalVariable - Emit global variable's debug info.
3250void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3251 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003252 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003253 // Create the descriptor for the variable.
3254 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3255 StringRef Name = VD->getName();
3256 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3257 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3258 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3259 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3260 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3261 }
3262 // Do not use DIGlobalVariable for enums.
3263 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
3264 return;
David Blaikiea15565562014-04-04 20:56:17 +00003265 // Do not emit separate definitions for function local const/statics.
3266 if (isa<FunctionDecl>(VD->getDeclContext()))
3267 return;
David Blaikiebb113912014-04-05 07:23:17 +00003268 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003269 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003270 if (VarD->isStaticDataMember()) {
3271 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3272 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003273 // Ensure that the type is retained even though it's otherwise unreferenced.
3274 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003275 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003276 return;
3277 }
3278
David Blaikieaf080852014-11-21 00:20:58 +00003279 llvm::DIDescriptor DContext =
3280 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3281
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003282 auto &GV = DeclCache[VD];
3283 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003284 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003285 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003286 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003287 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003288}
3289
3290llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3291 if (!LexicalBlockStack.empty())
3292 return llvm::DIScope(LexicalBlockStack.back());
3293 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003294}
3295
David Blaikie9f88fe82013-04-22 06:13:21 +00003296void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003297 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3298 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003299 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003300 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3301 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003302 getLineNumber(UD.getLocation()));
3303}
3304
David Blaikiebd483762013-05-20 04:58:53 +00003305void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3306 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3307 return;
3308 assert(UD.shadow_size() &&
3309 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3310 // Emitting one decl is sufficient - debuggers can detect that this is an
3311 // overloaded name & provide lookup for all the overloads.
3312 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003313 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003314 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003315 DBuilder.createImportedDeclaration(
3316 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3317 getLineNumber(USD.getLocation()));
3318}
3319
David Blaikief121b932013-05-20 22:50:41 +00003320llvm::DIImportedEntity
3321CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3322 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Craig Topper8a13c412014-05-21 05:09:00 +00003323 return llvm::DIImportedEntity(nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003324 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003325 if (VH)
3326 return llvm::DIImportedEntity(cast<llvm::MDNode>(VH));
Craig Topper8a13c412014-05-21 05:09:00 +00003327 llvm::DIImportedEntity R(nullptr);
David Blaikief121b932013-05-20 22:50:41 +00003328 if (const NamespaceAliasDecl *Underlying =
3329 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3330 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003331 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003332 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3333 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3334 NA.getName());
3335 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003336 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003337 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3338 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3339 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003340 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003341 return R;
3342}
3343
Guy Benyei11169dd2012-12-18 14:30:41 +00003344/// getOrCreateNamesSpace - Return namespace descriptor for the given
3345/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003346llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003347CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003348 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003349 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003350 if (I != NameSpaceCache.end())
3351 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
Eric Christopherb2a008c2013-05-16 00:45:12 +00003352
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3354 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003355 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3357 llvm::DINameSpace NS =
3358 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003359 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003360 return NS;
3361}
3362
3363void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003364 // Creating types might create further types - invalidating the current
3365 // element and the size(), so don't cache/reference them.
3366 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3367 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
3368 E.Decl.replaceAllUsesWith(CGM.getLLVMContext(),
3369 E.Type->getDecl()->getDefinition()
3370 ? CreateTypeDefinition(E.Type, E.Unit)
3371 : E.Decl);
3372 }
3373
David Blaikief427b002014-05-06 03:42:01 +00003374 for (auto p : ReplaceMap) {
3375 assert(p.second);
3376 llvm::DIType Ty(cast<llvm::MDNode>(p.second));
David Blaikieb8149042014-05-05 21:21:39 +00003377 assert(Ty.isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003378
David Blaikief427b002014-05-06 03:42:01 +00003379 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003380 assert(it != TypeCache.end());
3381 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003382
David Blaikief427b002014-05-06 03:42:01 +00003383 llvm::DIType RepTy(cast<llvm::MDNode>(it->second));
3384 Ty.replaceAllUsesWith(CGM.getLLVMContext(), RepTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003385 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003386
Frederic Rissd253ed62014-11-18 03:40:51 +00003387 for (const auto &p : FwdDeclReplaceMap) {
3388 assert(p.second);
3389 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003390 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003391
3392 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003393 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003394 // with ourselves, that will destroy the temporary MDNode and
3395 // replace it with a standard one, avoiding leaking memory.
3396 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003397 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003398 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003399 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003400
Frederic Rissd253ed62014-11-18 03:40:51 +00003401 FwdDecl.replaceAllUsesWith(CGM.getLLVMContext(),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003402 llvm::DIDescriptor(cast<llvm::MDNode>(Repl)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003403 }
3404
Adrian Prantl73409ce2013-03-11 18:33:46 +00003405 // We keep our own list of retained types, because we need to look
3406 // up the final type in the type cache.
3407 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3408 RE = RetainedTypes.end(); RI != RE; ++RI)
David Blaikie0856f662014-03-04 22:01:08 +00003409 DBuilder.retainType(llvm::DIType(cast<llvm::MDNode>(TypeCache[*RI])));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003410
Guy Benyei11169dd2012-12-18 14:30:41 +00003411 DBuilder.finalize();
3412}
David Blaikie66088d52014-09-24 17:01:27 +00003413
3414void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3415 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3416 return;
3417 llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile());
3418 // Don't ignore in case of explicit cast where it is referenced indirectly.
3419 DBuilder.retainType(DieTy);
3420}