blob: 0ff0b09ee36f03068a448da9601ad5b7df8c9e6e [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());
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +000078 CGF.Builder.SetCurrentDebugLocation(
79 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl39428e72015-02-03 18:40:42 +000080 }
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();
Duncan P. N. Exon Smith2809cc72015-03-30 20:01:41 +000095 if (Loc)
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();
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000123 auto *Scope = cast<llvm::MDScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000124 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000126 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 return;
128
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +0000129 if (auto *LBF = dyn_cast<llvm::MDLexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000130 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000131 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
132 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000133 } else if (isa<llvm::MDLexicalBlock>(Scope) ||
134 isa<llvm::MDSubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000135 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000136 LexicalBlockStack.emplace_back(
137 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000138 }
139}
140
141/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000142llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000143 if (!Context)
144 return TheCU;
145
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000146 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000148 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000149 return dyn_cast_or_null<llvm::MDScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 }
151
152 // Check namespace.
153 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000154 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000155
David Blaikiebfa52742013-04-19 06:56:38 +0000156 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
157 if (!RDecl->isDependentType())
158 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000159 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000160 return TheCU;
161}
162
163/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000164/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000165/// is stored on the side.
166StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000167 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000169 FunctionTemplateSpecializationInfo *Info =
170 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000171 if (!Info && FII)
172 return FII->getName();
173
174 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000175 SmallString<128> NS;
176 llvm::raw_svector_ostream OS(NS);
177 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000178
179 // Add any template specialization args.
180 if (Info) {
181 const TemplateArgumentList *TArgs = Info->TemplateArguments;
182 const TemplateArgument *Args = TArgs->data();
183 unsigned NumArgs = TArgs->size();
184 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000185 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
186 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000187 }
188
189 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000190 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000191}
192
193StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
194 SmallString<256> MethodName;
195 llvm::raw_svector_ostream OS(MethodName);
196 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
197 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000198 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000199 dyn_cast<const ObjCImplementationDecl>(DC)) {
200 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000201 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000202 dyn_cast<const ObjCInterfaceDecl>(DC)) {
203 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000204 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000205 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
206 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
207 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000208 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000209 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000210 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000211 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000212 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000213 ClassTy.print(OS, PrintingPolicy(LangOptions()));
214 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000215 }
216 OS << ' ' << OMD->getSelector().getAsString() << ']';
217
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000218 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000219}
220
221/// getSelectorName - Return selector name. This is used for debugging
222/// info.
223StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000224 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000225}
226
227/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000228StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000229 // quick optimization to avoid having to intern strings that are already
230 // stored reliably elsewhere
231 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000232 return RD->getName();
233
David Blaikie65813a32014-04-02 18:21:09 +0000234 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000235 {
David Blaikie65813a32014-04-02 18:21:09 +0000236 llvm::raw_svector_ostream OS(Name);
237 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
238 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000240
241 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000242 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000243}
244
245/// getOrCreateFile - Get the file debug info descriptor for the input location.
246llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
247 if (!Loc.isValid())
248 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000249 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000250
251 SourceManager &SM = CGM.getContext().getSourceManager();
252 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
253
254 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
255 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000256 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000257
258 // Cache the results.
259 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000260 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
262 if (it != DIFileCache.end()) {
263 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000264 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000265 return cast<llvm::MDFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000266 }
267
268 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
269
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000270 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000271 return F;
272}
273
274/// getOrCreateMainFile - Get the file info for main compile unit.
275llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000276 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000277}
278
279/// getLineNumber - Get line number for the location. If location is invalid
280/// then use current location.
281unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
282 if (Loc.isInvalid() && CurLoc.isInvalid())
283 return 0;
284 SourceManager &SM = CGM.getContext().getSourceManager();
285 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000286 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000287}
288
289/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000290unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000291 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000292 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000293 return 0;
294
295 // If the location is invalid then use the current column.
296 if (Loc.isInvalid() && CurLoc.isInvalid())
297 return 0;
298 SourceManager &SM = CGM.getContext().getSourceManager();
299 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000300 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000301}
302
303StringRef CGDebugInfo::getCurrentDirname() {
304 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
305 return CGM.getCodeGenOpts().DebugCompilationDir;
306
307 if (!CWDName.empty())
308 return CWDName;
309 SmallString<256> CWD;
310 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000311 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000312}
313
314/// CreateCompileUnit - Create new compile unit.
315void CGDebugInfo::CreateCompileUnit() {
316
David Blaikieaabde052014-05-14 00:29:00 +0000317 // Should we be asking the SourceManager for the main file name, instead of
318 // accepting it as an argument? This just causes the main file name to
319 // mismatch with source locations and create extra lexical scopes or
320 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
321 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
322 // because that's what the SourceManager says)
323
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 // Get absolute path name.
325 SourceManager &SM = CGM.getContext().getSourceManager();
326 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
327 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000328 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000329
330 // The main file name provided via the "-main-file-name" option contains just
331 // the file name itself with no path information. This file name may have had
332 // a relative path, so we look into the actual file entry for the main
333 // file to determine the real absolute path for the file.
334 std::string MainFileDir;
335 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
336 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000337 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000338 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
339 llvm::sys::path::append(MainFileDirSS, MainFileName);
340 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000341 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000342 }
343
344 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000345 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000346
347 // Save split dwarf file string.
348 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000349 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000350
Ed Masteda706022014-05-07 12:49:30 +0000351 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000352 const LangOptions &LO = CGM.getLangOpts();
353 if (LO.CPlusPlus) {
354 if (LO.ObjC1)
355 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
356 else
357 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
358 } else if (LO.ObjC1) {
359 LangTag = llvm::dwarf::DW_LANG_ObjC;
360 } else if (LO.C99) {
361 LangTag = llvm::dwarf::DW_LANG_C99;
362 } else {
363 LangTag = llvm::dwarf::DW_LANG_C89;
364 }
365
366 std::string Producer = getClangFullVersion();
367
368 // Figure out which version of the ObjC runtime we have.
369 unsigned RuntimeVers = 0;
370 if (LO.ObjC1)
371 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
372
373 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000374 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000375 TheCU = DBuilder.createCompileUnit(
376 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
377 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000378 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000379 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000380 : llvm::DIBuilder::FullDebug,
381 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000382}
383
384/// CreateType - Get the Basic type from the cache or create a new
385/// one if necessary.
386llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000387 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000388 StringRef BTName;
389 switch (BT->getKind()) {
390#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000391#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000392#include "clang/AST/BuiltinTypes.def"
393 case BuiltinType::Dependent:
394 llvm_unreachable("Unexpected builtin type");
395 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000396 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000397 case BuiltinType::Void:
398 return llvm::DIType();
399 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000400 if (!ClassTy)
401 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
402 "objc_class", TheCU,
403 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000404 return ClassTy;
405 case BuiltinType::ObjCId: {
406 // typedef struct objc_class *Class;
407 // typedef struct objc_object {
408 // Class isa;
409 // } *id;
410
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000411 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 return ObjTy;
413
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000414 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000415 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
416 "objc_class", TheCU,
417 getOrCreateMainFile(), 0);
418
419 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000420
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
422
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000423 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000424 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
425 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000426
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000427 DBuilder.replaceArrays(
428 ObjTy,
429 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
430 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 return ObjTy;
432 }
433 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000434 if (!SelTy)
435 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
436 "objc_selector", TheCU,
437 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 return SelTy;
439 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000440
441 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000442 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000443 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000444 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000445 OCLImage1dArrayDITy);
446 case BuiltinType::OCLImage1dBuffer:
447 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
448 OCLImage1dBufferDITy);
449 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000450 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000451 case BuiltinType::OCLImage2dArray:
452 return getOrCreateStructPtrType("opencl_image2d_array_t",
453 OCLImage2dArrayDITy);
454 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000455 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000456 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000457 return DBuilder.createBasicType(
458 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
459 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000460 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000462
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000464 case BuiltinType::Char_U:
465 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
466 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::SChar:
469 Encoding = llvm::dwarf::DW_ATE_signed_char;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 case BuiltinType::Char32:
473 Encoding = llvm::dwarf::DW_ATE_UTF;
474 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000475 case BuiltinType::UShort:
476 case BuiltinType::UInt:
477 case BuiltinType::UInt128:
478 case BuiltinType::ULong:
479 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000480 case BuiltinType::ULongLong:
481 Encoding = llvm::dwarf::DW_ATE_unsigned;
482 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000483 case BuiltinType::Short:
484 case BuiltinType::Int:
485 case BuiltinType::Int128:
486 case BuiltinType::Long:
487 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000488 case BuiltinType::LongLong:
489 Encoding = llvm::dwarf::DW_ATE_signed;
490 break;
491 case BuiltinType::Bool:
492 Encoding = llvm::dwarf::DW_ATE_boolean;
493 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 case BuiltinType::Half:
495 case BuiltinType::Float:
496 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000497 case BuiltinType::Double:
498 Encoding = llvm::dwarf::DW_ATE_float;
499 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 }
501
502 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000503 case BuiltinType::Long:
504 BTName = "long int";
505 break;
506 case BuiltinType::LongLong:
507 BTName = "long long int";
508 break;
509 case BuiltinType::ULong:
510 BTName = "long unsigned int";
511 break;
512 case BuiltinType::ULongLong:
513 BTName = "long long unsigned int";
514 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000515 default:
516 BTName = BT->getName(CGM.getLangOpts());
517 break;
518 }
519 // Bit size, align and offset of the type.
520 uint64_t Size = CGM.getContext().getTypeSize(BT);
521 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000522 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000523 return DbgTy;
524}
525
526llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
527 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000528 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 if (Ty->isComplexIntegerType())
530 Encoding = llvm::dwarf::DW_ATE_lo_user;
531
532 uint64_t Size = CGM.getContext().getTypeSize(Ty);
533 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000534 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000535 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000536
537 return DbgTy;
538}
539
540/// CreateCVRType - Get the qualified type from the cache or create
541/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000542llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000543 QualifierCollector Qc;
544 const Type *T = Qc.strip(Ty);
545
546 // Ignore these qualifiers for now.
547 Qc.removeObjCGCAttr();
548 Qc.removeAddressSpace();
549 Qc.removeObjCLifetime();
550
551 // We will create one Derived type for one qualifier and recurse to handle any
552 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000553 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000554 if (Qc.hasConst()) {
555 Tag = llvm::dwarf::DW_TAG_const_type;
556 Qc.removeConst();
557 } else if (Qc.hasVolatile()) {
558 Tag = llvm::dwarf::DW_TAG_volatile_type;
559 Qc.removeVolatile();
560 } else if (Qc.hasRestrict()) {
561 Tag = llvm::dwarf::DW_TAG_restrict_type;
562 Qc.removeRestrict();
563 } else {
564 assert(Qc.empty() && "Unknown type qualifier for debug info");
565 return getOrCreateType(QualType(T, 0), Unit);
566 }
567
David Blaikie99dab3b2013-09-04 22:03:57 +0000568 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000569
570 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
571 // CVR derived types.
572 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000573
Guy Benyei11169dd2012-12-18 14:30:41 +0000574 return DbgTy;
575}
576
577llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
578 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000579
580 // The frontend treats 'id' as a typedef to an ObjCObjectType,
581 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
582 // debug info, we want to emit 'id' in both cases.
583 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000584 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000585
Eric Christophere7b87e52014-10-26 23:40:33 +0000586 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
587 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000588 return DbgTy;
589}
590
Eric Christophere7b87e52014-10-26 23:40:33 +0000591llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000592 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 Ty->getPointeeType(), Unit);
594}
595
Manman Rene0064d82013-08-29 23:19:58 +0000596/// In C++ mode, types have linkage, so we can rely on the ODR and
597/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000598static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
599 CodeGenModule &CGM,
600 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000601 SmallString<256> FullName;
602 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
603 // For now, only apply ODR with C++.
604 const TagDecl *TD = Ty->getDecl();
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000605 if (TheCU->getSourceLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
Manman Rene0064d82013-08-29 23:19:58 +0000606 !TD->isExternallyVisible())
607 return FullName;
608 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
609 if (CGM.getTarget().getCXXABI().isMicrosoft())
610 return FullName;
611
612 // TODO: This is using the RTTI name. Is there a better way to get
613 // a unique string for a type?
614 llvm::raw_svector_ostream Out(FullName);
615 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
616 Out.flush();
617 return FullName;
618}
619
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000620static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
621 llvm::dwarf::Tag Tag;
622 if (RD->isStruct() || RD->isInterface())
623 Tag = llvm::dwarf::DW_TAG_structure_type;
624 else if (RD->isUnion())
625 Tag = llvm::dwarf::DW_TAG_union_type;
626 else {
627 // FIXME: This could be a struct type giving a default visibility different
628 // than C++ class type, but needs llvm metadata changes first.
629 assert(RD->isClass());
630 Tag = llvm::dwarf::DW_TAG_class_type;
631 }
632 return Tag;
633}
634
Guy Benyei11169dd2012-12-18 14:30:41 +0000635// Creates a forward declaration for a RecordDecl in the given context.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000636llvm::MDCompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000637CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000638 llvm::MDScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000639 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000640 if (llvm::MDType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
641 return cast<llvm::MDCompositeType>(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000642 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
643 unsigned Line = getLineNumber(RD->getLocation());
644 StringRef RDName = getClassName(RD);
645
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000646 uint64_t Size = 0;
647 uint64_t Align = 0;
648
649 const RecordDecl *D = RD->getDefinition();
650 if (D && D->isCompleteDefinition()) {
651 Size = CGM.getContext().getTypeSize(Ty);
652 Align = CGM.getContext().getTypeAlign(Ty);
653 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000654
655 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000656 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000657 llvm::MDCompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000658 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000659 llvm::DebugNode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000660 ReplaceMap.emplace_back(
661 std::piecewise_construct, std::make_tuple(Ty),
662 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000663 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000664}
665
Ed Masteda706022014-05-07 12:49:30 +0000666llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000667 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000668 QualType PointeeTy,
669 llvm::DIFile Unit) {
670 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
671 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000672 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000673
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 // Bit size, align and offset of the type.
675 // Size is always the size of a pointer. We can't use getTypeSize here
676 // because that does not return the correct value for references.
677 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000678 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
680
David Blaikie99dab3b2013-09-04 22:03:57 +0000681 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
682 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000683}
684
Eric Christopher0fdcb312013-05-16 00:52:20 +0000685llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
686 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000687 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000688 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000689 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
690 TheCU, getOrCreateMainFile(), 0);
691 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
692 Cache = DBuilder.createPointerType(Cache, Size);
693 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000694}
695
Guy Benyei11169dd2012-12-18 14:30:41 +0000696llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
697 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000698 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 return BlockLiteralGeneric;
700
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000701 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702 llvm::DIType FieldTy;
703 QualType FType;
704 uint64_t FieldSize, FieldOffset;
705 unsigned FieldAlign;
706 llvm::DIArray Elements;
707 llvm::DIType EltTy, DescTy;
708
709 FieldOffset = 0;
710 FType = CGM.getContext().UnsignedLongTy;
711 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
712 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
713
714 Elements = DBuilder.getOrCreateArray(EltTys);
715 EltTys.clear();
716
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000717 unsigned Flags = llvm::DebugNode::FlagAppleBlock;
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 unsigned LineNo = getLineNumber(CurLoc);
719
Eric Christophere7b87e52014-10-26 23:40:33 +0000720 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
721 FieldOffset, 0, Flags, llvm::DIType(),
722 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000723
724 // Bit size, align and offset of the type.
725 uint64_t Size = CGM.getContext().getTypeSize(Ty);
726
727 DescTy = DBuilder.createPointerType(EltTy, Size);
728
729 FieldOffset = 0;
730 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
731 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
732 FType = CGM.getContext().IntTy;
733 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
734 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000735 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000736 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
737
738 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
739 FieldTy = DescTy;
740 FieldSize = CGM.getContext().getTypeSize(Ty);
741 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000742 FieldTy =
743 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
744 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 EltTys.push_back(FieldTy);
746
747 FieldOffset += FieldSize;
748 Elements = DBuilder.getOrCreateArray(EltTys);
749
Eric Christophere7b87e52014-10-26 23:40:33 +0000750 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
751 LineNo, FieldOffset, 0, Flags,
752 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000753
Guy Benyei11169dd2012-12-18 14:30:41 +0000754 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
755 return BlockLiteralGeneric;
756}
757
Eric Christophere7b87e52014-10-26 23:40:33 +0000758llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
759 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000760 assert(Ty->isTypeAlias());
761 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000762
763 SmallString<128> NS;
764 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000765 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
766 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000767
768 TemplateSpecializationType::PrintTemplateArgumentList(
769 OS, Ty->getArgs(), Ty->getNumArgs(),
770 CGM.getContext().getPrintingPolicy());
771
Eric Christophere7b87e52014-10-26 23:40:33 +0000772 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
773 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000774
775 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000776 return DBuilder.createTypedef(
777 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
778 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000779}
780
David Blaikie99dab3b2013-09-04 22:03:57 +0000781llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000782 // We don't set size information, but do specify where the typedef was
783 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000784 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000785
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000786 // Typedefs are derived from some other type.
787 return DBuilder.createTypedef(
788 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
789 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
790 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000791}
792
793llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
794 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000795 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000796
797 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000798 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000799
800 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000801 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000802 if (isa<FunctionNoProtoType>(Ty))
803 EltTys.push_back(DBuilder.createUnspecifiedParameter());
804 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000805 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
806 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000807 if (FPT->isVariadic())
808 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000809 }
810
Manman Ren67f005e2014-07-28 22:24:34 +0000811 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000812 return DBuilder.createSubroutineType(Unit, EltTypeArray);
813}
814
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000815/// Convert an AccessSpecifier into the corresponding DebugNode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000816/// As an optimization, return 0 if the access specifier equals the
817/// default for the containing type.
818static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
819 AccessSpecifier Default = clang::AS_none;
820 if (RD && RD->isClass())
821 Default = clang::AS_private;
822 else if (RD && (RD->isStruct() || RD->isUnion()))
823 Default = clang::AS_public;
824
825 if (Access == Default)
826 return 0;
827
Eric Christophere7b87e52014-10-26 23:40:33 +0000828 switch (Access) {
829 case clang::AS_private:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000830 return llvm::DebugNode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000831 case clang::AS_protected:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000832 return llvm::DebugNode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000833 case clang::AS_public:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000834 return llvm::DebugNode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000835 case clang::AS_none:
836 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000837 }
838 llvm_unreachable("unexpected access enumerator");
839}
Guy Benyei11169dd2012-12-18 14:30:41 +0000840
Eric Christophere7b87e52014-10-26 23:40:33 +0000841llvm::DIType CGDebugInfo::createFieldType(
842 StringRef name, QualType type, uint64_t sizeInBitsOverride,
843 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
844 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 llvm::DIType debugType = getOrCreateType(type, tunit);
846
847 // Get the location for the field.
848 llvm::DIFile file = getOrCreateFile(loc);
849 unsigned line = getLineNumber(loc);
850
David Majnemer34b57492014-07-30 01:30:47 +0000851 uint64_t SizeInBits = 0;
852 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000853 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000854 TypeInfo TI = CGM.getContext().getTypeInfo(type);
855 SizeInBits = TI.Width;
856 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000857
858 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000859 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000860 }
861
Adrian Prantl21361fb2014-08-29 22:44:27 +0000862 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000863 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
864 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000865}
866
Eric Christopher91a31902013-01-16 01:22:32 +0000867/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000868void CGDebugInfo::CollectRecordLambdaFields(
869 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
870 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000871 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
872 // has the name and the location of the variable so we should iterate over
873 // both concurrently.
874 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
875 RecordDecl::field_iterator Field = CXXDecl->field_begin();
876 unsigned fieldno = 0;
877 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000878 E = CXXDecl->captures_end();
879 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000880 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000881 if (C.capturesVariable()) {
882 VarDecl *V = C.getCapturedVar();
883 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
884 StringRef VName = V->getName();
885 uint64_t SizeInBitsOverride = 0;
886 if (Field->isBitField()) {
887 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
888 assert(SizeInBitsOverride && "found named 0-width bitfield");
889 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000890 llvm::DIType fieldType = createFieldType(
891 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
892 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
893 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000894 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000895 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000896 // TODO: Need to handle 'this' in some way by probably renaming the
897 // this of the lambda class and having a field member of 'this' or
898 // by using AT_object_pointer for the function and having that be
899 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000900 FieldDecl *f = *Field;
901 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
902 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000903 llvm::DIType fieldType = createFieldType(
904 "this", type, 0, f->getLocation(), f->getAccess(),
905 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000906
907 elements.push_back(fieldType);
908 }
909 }
910}
911
David Blaikie6943dea2013-08-20 01:28:15 +0000912/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000913llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
914 llvm::DIType RecordTy,
915 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000916 // Create the descriptor for the static variable, with or without
917 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000918 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000919 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
920 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
921
Eric Christopher91a31902013-01-16 01:22:32 +0000922 unsigned LineNumber = getLineNumber(Var->getLocation());
923 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000924 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000925 if (Var->getInit()) {
926 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000927 if (Value) {
928 if (Value->isInt())
929 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
930 if (Value->isFloat())
931 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
932 }
Eric Christopher91a31902013-01-16 01:22:32 +0000933 }
934
Adrian Prantl21361fb2014-08-29 22:44:27 +0000935 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000936 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
937 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000938 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000939 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000940}
941
942/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000943void CGDebugInfo::CollectRecordNormalField(
944 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000945 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000946 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000947 StringRef name = field->getName();
948 QualType type = field->getType();
949
950 // Ignore unnamed fields unless they're anonymous structs/unions.
951 if (name.empty() && !type->isRecordType())
952 return;
953
954 uint64_t SizeInBitsOverride = 0;
955 if (field->isBitField()) {
956 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
957 assert(SizeInBitsOverride && "found named 0-width bitfield");
958 }
959
Eric Christophere7b87e52014-10-26 23:40:33 +0000960 llvm::DIType fieldType =
961 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
962 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000963
964 elements.push_back(fieldType);
965}
966
Guy Benyei11169dd2012-12-18 14:30:41 +0000967/// CollectRecordFields - A helper function to collect debug info for
968/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000969void CGDebugInfo::CollectRecordFields(
970 const RecordDecl *record, llvm::DIFile tunit,
971 SmallVectorImpl<llvm::Metadata *> &elements,
972 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000973 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
974
Eric Christopher91a31902013-01-16 01:22:32 +0000975 if (CXXDecl && CXXDecl->isLambda())
976 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
977 else {
978 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000979
Eric Christopher91a31902013-01-16 01:22:32 +0000980 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000981 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000982
Eric Christopher91a31902013-01-16 01:22:32 +0000983 // Static and non-static members should appear in the same order as
984 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000985 for (const auto *I : record->decls())
986 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000987 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000988 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000989 if (MI != StaticDataMemberCache.end()) {
990 assert(MI->second &&
991 "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000992 elements.push_back(cast<llvm::MDDerivedTypeBase>(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.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001010llvm::MDSubroutineType *
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())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001015 return cast_or_null<llvm::MDSubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001016 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001017 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1018 Func, Unit);
1019}
David Blaikie2aaf0652013-01-07 22:24:59 +00001020
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001021llvm::MDSubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001022 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001023 // Add "this" pointer.
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001024 llvm::DITypeArray Args(
1025 cast<llvm::MDSubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
1026 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001027 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001028
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001029 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001030
1031 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001032 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001033
David Blaikie2aaf0652013-01-07 22:24:59 +00001034 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001035 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001036 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1037 // Create pointer type directly in this case.
1038 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1039 QualType PointeeTy = ThisPtrTy->getPointeeType();
1040 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001041 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001042 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1043 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001044 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001045 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001046 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001047 // TODO: This and the artificial type below are misleading, the
1048 // types aren't artificial the argument is, but the current
1049 // metadata doesn't represent that.
1050 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1051 Elts.push_back(ThisPtrType);
1052 } else {
1053 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001054 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001055 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1056 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001057 }
1058
1059 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001060 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1061 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062
Manman Ren67f005e2014-07-28 22:24:34 +00001063 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001064
Adrian Prantl0630eb72013-12-18 21:48:18 +00001065 unsigned Flags = 0;
1066 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001067 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001068 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001069 Flags |= llvm::DebugNode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001070
1071 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072}
1073
Eric Christopherb2a008c2013-05-16 00:45:12 +00001074/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001075/// inside a function.
1076static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1077 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1078 return isFunctionLocalClass(NRD);
1079 if (isa<FunctionDecl>(RD->getDeclContext()))
1080 return true;
1081 return false;
1082}
1083
1084/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1085/// a single member function GlobalDecl.
1086llvm::DISubprogram
1087CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001088 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001089 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001090 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001091
Guy Benyei11169dd2012-12-18 14:30:41 +00001092 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001093 llvm::MDSubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001094
1095 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1096 // make sense to give a single ctor/dtor a linkage name.
1097 StringRef MethodLinkageName;
1098 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1099 MethodLinkageName = CGM.getMangledName(Method);
1100
1101 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001102 llvm::DIFile MethodDefUnit;
1103 unsigned MethodLine = 0;
1104 if (!Method->isImplicit()) {
1105 MethodDefUnit = getOrCreateFile(Method->getLocation());
1106 MethodLine = getLineNumber(Method->getLocation());
1107 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001108
1109 // Collect virtual method info.
1110 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001111 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001113
Guy Benyei11169dd2012-12-18 14:30:41 +00001114 if (Method->isVirtual()) {
1115 if (Method->isPure())
1116 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1117 else
1118 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001119
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 // It doesn't make sense to give a virtual destructor a vtable index,
1121 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001122 // FIXME: Add proper support for debug info for virtual calls in
1123 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1124 // lookup if we have multiple or virtual inheritance.
1125 if (!isa<CXXDestructorDecl>(Method) &&
1126 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001127 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001128 ContainingType = RecordTy;
1129 }
1130
1131 unsigned Flags = 0;
1132 if (Method->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001133 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001134 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001135 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1136 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001137 Flags |= llvm::DebugNode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001138 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001139 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001140 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001141 Flags |= llvm::DebugNode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001142 }
1143 if (Method->hasPrototype())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001144 Flags |= llvm::DebugNode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001145 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001146 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001147 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001148 Flags |= llvm::DebugNode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001149
1150 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001151 llvm::DISubprogram SP = DBuilder.createMethod(
1152 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1153 MethodTy, /*isLocalToUnit=*/false,
1154 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001155 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001156
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001157 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001158
1159 return SP;
1160}
1161
1162/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001163/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001164/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001165void CGDebugInfo::CollectCXXMemberFunctions(
1166 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001167 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001168
1169 // Since we want more than just the individual member decls if we
1170 // have templated functions iterate over every declaration to gather
1171 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001172 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001173 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1174 // If the member is implicit, don't add it to the member list. This avoids
1175 // the member being added to type units by LLVM, while still allowing it
1176 // to be emitted into the type declaration/reference inside the compile
1177 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001178 // FIXME: Handle Using(Shadow?)Decls here to create
1179 // DW_TAG_imported_declarations inside the class for base decls brought into
1180 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1181 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1182 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001183 if (!Method || Method->isImplicit())
1184 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001185
1186 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1187 continue;
1188
David Blaikiefd580722014-10-06 05:18:55 +00001189 // Reuse the existing member function declaration if it exists.
1190 // It may be associated with the declaration of the type & should be
1191 // reused as we're building the definition.
1192 //
1193 // This situation can arise in the vtable-based debug info reduction where
1194 // implicit members are emitted in a non-vtable TU.
1195 auto MI = SPCache.find(Method->getCanonicalDecl());
1196 EltTys.push_back(MI == SPCache.end()
1197 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001198 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001200}
Guy Benyei11169dd2012-12-18 14:30:41 +00001201
Guy Benyei11169dd2012-12-18 14:30:41 +00001202/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001203/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001204/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001205void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001206 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001207 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001208
1209 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001210 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001211 unsigned BFlags = 0;
1212 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001213
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001215 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001216
Aaron Ballman574705e2014-03-13 15:41:46 +00001217 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001218 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1219 // virtual base offset offset is -ve. The code generator emits dwarf
1220 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001221 BaseOffset = 0 - CGM.getItaniumVTableContext()
1222 .getVirtualBaseOffsetOffset(RD, Base)
1223 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001224 } else {
1225 // In the MS ABI, store the vbtable offset, which is analogous to the
1226 // vbase offset offset in Itanium.
1227 BaseOffset =
1228 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1229 }
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001230 BFlags = llvm::DebugNode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001231 } else
1232 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1233 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1234 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001235
Adrian Prantl21361fb2014-08-29 22:44:27 +00001236 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001237 llvm::DIType DTy = DBuilder.createInheritance(
1238 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001239 EltTys.push_back(DTy);
1240 }
1241}
1242
1243/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001244llvm::DIArray
1245CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1246 ArrayRef<TemplateArgument> TAList,
1247 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001248 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1250 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001251 StringRef Name;
1252 if (TPList)
1253 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001254 switch (TA.getKind()) {
1255 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001256 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1257 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001258 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001259 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001260 } break;
1261 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001262 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1263 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001264 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001265 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001266 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1267 TemplateParams.push_back(TVP);
1268 } break;
1269 case TemplateArgument::Declaration: {
1270 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001271 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001272 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001273 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001274 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001275 // Variable pointer template parameters have a value that is the address
1276 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001277 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001278 V = CGM.GetAddrOfGlobalVar(VD);
1279 // Member function pointers have special support for building them, though
1280 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001281 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001282 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001283 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001284 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001285 // Member data pointers have special handling too to compute the fixed
1286 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001287 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001288 // These five lines (& possibly the above member function pointer
1289 // handling) might be able to be refactored to use similar code in
1290 // CodeGenModule::getMemberPointerConstant
1291 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1292 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001293 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001294 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001295 }
1296 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001297 DBuilder.createTemplateValueParameter(
1298 TheCU, Name, TTy,
1299 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001300 TemplateParams.push_back(TVP);
1301 } break;
1302 case TemplateArgument::NullPtr: {
1303 QualType T = TA.getNullPtrType();
1304 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001305 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001306 // Special case member data pointer null values since they're actually -1
1307 // instead of zero.
1308 if (const MemberPointerType *MPT =
1309 dyn_cast<MemberPointerType>(T.getTypePtr()))
1310 // But treat member function pointers as simple zero integers because
1311 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1312 // CodeGen grows handling for values of non-null member function
1313 // pointers then perhaps we could remove this special case and rely on
1314 // EmitNullMemberPointer for member function pointers.
1315 if (MPT->isMemberDataPointer())
1316 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1317 if (!V)
1318 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1319 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001320 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1321 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001322 TemplateParams.push_back(TVP);
1323 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001324 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001325 llvm::DITemplateValueParameter
1326 TVP = DBuilder.createTemplateTemplateParameter(
1327 TheCU, Name, llvm::DIType(),
1328 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001329 TemplateParams.push_back(TVP);
1330 } break;
1331 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001332 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1333 TheCU, Name, llvm::DIType(),
1334 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001335 TemplateParams.push_back(TVP);
1336 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001337 case TemplateArgument::Expression: {
1338 const Expr *E = TA.getAsExpr();
1339 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001340 if (E->isGLValue())
1341 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001342 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001343 assert(V && "Expression in template argument isn't constant");
1344 llvm::DIType TTy = getOrCreateType(T, Unit);
1345 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001346 DBuilder.createTemplateValueParameter(
1347 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001348 TemplateParams.push_back(TVP);
1349 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001350 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001351 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001352 case TemplateArgument::Null:
1353 llvm_unreachable(
1354 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 }
1356 }
1357 return DBuilder.getOrCreateArray(TemplateParams);
1358}
1359
1360/// CollectFunctionTemplateParams - A helper function to collect debug
1361/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001362llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1363 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 if (FD->getTemplatedKind() ==
1365 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001366 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1367 ->getTemplate()
1368 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001369 return CollectTemplateParams(
1370 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001371 }
1372 return llvm::DIArray();
1373}
1374
1375/// CollectCXXTemplateParams - A helper function to collect debug info for
1376/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001377llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1378 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001379 // Always get the full list of parameters, not just the ones from
1380 // the specialization.
1381 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001382 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001383 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001384 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001385}
1386
1387/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1388llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001389 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001390 return VTablePtrType;
1391
1392 ASTContext &Context = CGM.getContext();
1393
1394 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001395 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001396 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1398 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001399 llvm::DIType vtbl_ptr_type =
1400 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1402 return VTablePtrType;
1403}
1404
1405/// getVTableName - Get vtable name for the given Class.
1406StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001407 // Copy the gdb compatible name on the side and use its reference.
1408 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001409}
1410
Guy Benyei11169dd2012-12-18 14:30:41 +00001411/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1412/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001413void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001414 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1416
1417 // If there is a primary base then it will hold vtable info.
1418 if (RL.getPrimaryBase())
1419 return;
1420
1421 // If this class is not dynamic then there is not any vtable info to collect.
1422 if (!RD->isDynamicClass())
1423 return;
1424
1425 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001426 llvm::DIType VPTR = DBuilder.createMemberType(
1427 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001428 llvm::DebugNode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001429 EltTys.push_back(VPTR);
1430}
1431
Eric Christopherb2a008c2013-05-16 00:45:12 +00001432/// getOrCreateRecordType - Emit record type's standalone debug info.
1433llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001435 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1437 return T;
1438}
1439
1440/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1441/// debug info.
1442llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001443 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001444 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001446 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001447 return T;
1448}
1449
David Blaikie483a9da2014-05-06 18:35:21 +00001450void CGDebugInfo::completeType(const EnumDecl *ED) {
1451 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1452 return;
1453 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001454 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001455 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001456 if (I == TypeCache.end() || !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001457 return;
1458 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001459 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001460 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001461}
1462
David Blaikieb2e86eb2013-08-15 20:49:17 +00001463void CGDebugInfo::completeType(const RecordDecl *RD) {
1464 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1465 !CGM.getLangOpts().CPlusPlus)
1466 completeRequiredType(RD);
1467}
1468
1469void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001470 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1471 return;
1472
David Blaikie6943dea2013-08-20 01:28:15 +00001473 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1474 if (CXXDecl->isDynamicClass())
1475 return;
1476
David Blaikieb2e86eb2013-08-15 20:49:17 +00001477 QualType Ty = CGM.getContext().getRecordType(RD);
1478 llvm::DIType T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001479 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001480 completeClassData(RD);
1481}
1482
1483void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1484 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001485 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001486 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001487 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001488 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001489 if (I != TypeCache.end() && !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001490 return;
1491 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001492 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();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001542 llvm::DIType T = cast_or_null<llvm::MDType>(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
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001566 auto *FwdDecl =
1567 cast<llvm::MDCompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001568
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001569 const RecordDecl *D = RD->getDefinition();
1570 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 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
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001605 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001606 FwdDecl =
1607 llvm::MDNode::replaceWithPermanent(llvm::TempMDCompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001608
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001609 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001610 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001611}
1612
1613/// CreateType - get objective-c object type.
1614llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1615 llvm::DIFile Unit) {
1616 // Ignore protocols.
1617 return getOrCreateType(Ty->getBaseType(), Unit);
1618}
1619
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001620/// \return true if Getter has the default name for the property PD.
1621static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1622 const ObjCMethodDecl *Getter) {
1623 assert(PD);
1624 if (!Getter)
1625 return true;
1626
1627 assert(Getter->getDeclName().isObjCZeroArgSelector());
1628 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001629 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001630}
1631
1632/// \return true if Setter has the default name for the property PD.
1633static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1634 const ObjCMethodDecl *Setter) {
1635 assert(PD);
1636 if (!Setter)
1637 return true;
1638
1639 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001640 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001641 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001642}
1643
Guy Benyei11169dd2012-12-18 14:30:41 +00001644/// CreateType - get objective-c interface type.
1645llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1646 llvm::DIFile Unit) {
1647 ObjCInterfaceDecl *ID = Ty->getDecl();
1648 if (!ID)
1649 return llvm::DIType();
1650
1651 // Get overall information about the record type for the debug info.
1652 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1653 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001654 auto RuntimeLang =
1655 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001656
1657 // If this is just a forward declaration return a special forward-declaration
1658 // debug type since we won't be able to lay out the entire type.
1659 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001660 if (!Def || !Def->getImplementation()) {
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001661 llvm::DIType FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001662 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1663 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001664 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001665 return FwdDecl;
1666 }
1667
David Blaikieef8a9512014-05-05 23:23:53 +00001668 return CreateTypeDefinition(Ty, Unit);
1669}
1670
Eric Christophere7b87e52014-10-26 23:40:33 +00001671llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1672 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001673 ObjCInterfaceDecl *ID = Ty->getDecl();
1674 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1675 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001676 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001677
1678 // Bit size, align and offset of the type.
1679 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1680 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1681
1682 unsigned Flags = 0;
1683 if (ID->getImplementation())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001684 Flags |= llvm::DebugNode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001686 llvm::MDCompositeType *RealDecl = DBuilder.createStructType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001687 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1688 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001689
David Blaikieef8a9512014-05-05 23:23:53 +00001690 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001691 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001692
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001693 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001694 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001695 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696
1697 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001698 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001699
1700 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1701 if (SClass) {
1702 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001703 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001704 if (!SClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001706
Eric Christophere7b87e52014-10-26 23:40:33 +00001707 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 EltTys.push_back(InhTag);
1709 }
1710
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001711 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001712 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001713 SourceLocation Loc = PD->getLocation();
1714 llvm::DIFile PUnit = getOrCreateFile(Loc);
1715 unsigned PLine = getLineNumber(Loc);
1716 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1717 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001718 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1719 PD->getName(), PUnit, PLine,
1720 hasDefaultGetterName(PD, Getter) ? ""
1721 : getSelectorName(PD->getGetterName()),
1722 hasDefaultSetterName(PD, Setter) ? ""
1723 : getSelectorName(PD->getSetterName()),
1724 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 EltTys.push_back(PropertyNode);
1726 }
1727
1728 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1729 unsigned FieldNo = 0;
1730 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1731 Field = Field->getNextIvar(), ++FieldNo) {
1732 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001733 if (!FieldTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001735
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 StringRef FieldName = Field->getName();
1737
1738 // Ignore unnamed fields.
1739 if (FieldName.empty())
1740 continue;
1741
1742 // Get the location for the field.
1743 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1744 unsigned FieldLine = getLineNumber(Field->getLocation());
1745 QualType FType = Field->getType();
1746 uint64_t FieldSize = 0;
1747 unsigned FieldAlign = 0;
1748
1749 if (!FType->isIncompleteArrayType()) {
1750
1751 // Bit size, align and offset of the type.
1752 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001753 ? Field->getBitWidthValue(CGM.getContext())
1754 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001755 FieldAlign = CGM.getContext().getTypeAlign(FType);
1756 }
1757
1758 uint64_t FieldOffset;
1759 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1760 // We don't know the runtime offset of an ivar if we're using the
1761 // non-fragile ABI. For bitfields, use the bit offset into the first
1762 // byte of storage of the bitfield. For other fields, use zero.
1763 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001764 FieldOffset =
1765 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001766 FieldOffset %= CGM.getContext().getCharWidth();
1767 } else {
1768 FieldOffset = 0;
1769 }
1770 } else {
1771 FieldOffset = RL.getFieldOffset(FieldNo);
1772 }
1773
1774 unsigned Flags = 0;
1775 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001776 Flags = llvm::DebugNode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001777 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001778 Flags = llvm::DebugNode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001779 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001780 Flags = llvm::DebugNode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001781
Craig Topper8a13c412014-05-21 05:09:00 +00001782 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001784 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001785 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001787 SourceLocation Loc = PD->getLocation();
1788 llvm::DIFile PUnit = getOrCreateFile(Loc);
1789 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1791 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001792 PropertyNode = DBuilder.createObjCProperty(
1793 PD->getName(), PUnit, PLine,
1794 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1795 PD->getGetterName()),
1796 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1797 PD->getSetterName()),
1798 PD->getPropertyAttributes(),
1799 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 }
1801 }
1802 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001803 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1804 FieldSize, FieldAlign, FieldOffset, Flags,
1805 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001806 EltTys.push_back(FieldTy);
1807 }
1808
1809 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001810 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001811
Guy Benyei11169dd2012-12-18 14:30:41 +00001812 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001813 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001814}
1815
1816llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1817 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1818 int64_t Count = Ty->getNumElements();
1819 if (Count == 0)
1820 // If number of elements are not known then this is an unbounded array.
1821 // Use Count == -1 to express such arrays.
1822 Count = -1;
1823
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001824 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1826
1827 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1828 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1829
1830 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1831}
1832
Eric Christophere7b87e52014-10-26 23:40:33 +00001833llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001834 uint64_t Size;
1835 uint64_t Align;
1836
1837 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1838 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1839 Size = 0;
1840 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001841 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 } else if (Ty->isIncompleteArrayType()) {
1843 Size = 0;
1844 if (Ty->getElementType()->isIncompleteType())
1845 Align = 0;
1846 else
1847 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001848 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001849 Size = 0;
1850 Align = 0;
1851 } else {
1852 // Size and align of the whole array, not the element type.
1853 Size = CGM.getContext().getTypeSize(Ty);
1854 Align = CGM.getContext().getTypeAlign(Ty);
1855 }
1856
1857 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1858 // interior arrays, do we care? Why aren't nested arrays represented the
1859 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001860 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 QualType EltTy(Ty, 0);
1862 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1863 // If the number of elements is known, then count is that number. Otherwise,
1864 // it's -1. This allows us to represent a subrange with an array of 0
1865 // elements, like this:
1866 //
1867 // struct foo {
1868 // int x[0];
1869 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001870 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1872 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001873
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 // FIXME: Verify this is right for VLAs.
1875 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1876 EltTy = Ty->getElementType();
1877 }
1878
1879 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1880
Eric Christophere7b87e52014-10-26 23:40:33 +00001881 llvm::DIType DbgTy = DBuilder.createArrayType(
1882 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 return DbgTy;
1884}
1885
Eric Christopherb2a008c2013-05-16 00:45:12 +00001886llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001888 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1889 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001890}
1891
Eric Christopherb2a008c2013-05-16 00:45:12 +00001892llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001894 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1895 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001896}
1897
Eric Christopherb2a008c2013-05-16 00:45:12 +00001898llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001900 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1901 if (!Ty->getPointeeType()->isFunctionType())
1902 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001903 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001904 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001905
1906 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001907 Ty->getPointeeType()->getAs<FunctionProtoType>();
1908 return DBuilder.createMemberPointerType(
1909 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1910 Ty->getClass(), FPT->getTypeQuals())),
1911 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001912 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001913}
1914
Eric Christophere7b87e52014-10-26 23:40:33 +00001915llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001916 // Ignore the atomic wrapping
1917 // FIXME: What is the correct representation?
1918 return getOrCreateType(Ty->getValueType(), U);
1919}
1920
1921/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001922llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001923 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 uint64_t Size = 0;
1925 uint64_t Align = 0;
1926 if (!ED->getTypeForDecl()->isIncompleteType()) {
1927 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1928 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1929 }
1930
Manman Rene0064d82013-08-29 23:19:58 +00001931 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1932
Guy Benyei11169dd2012-12-18 14:30:41 +00001933 // If this is just a forward declaration, construct an appropriately
1934 // marked node and just return it.
1935 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001936 llvm::MDScope *EDContext =
1937 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001938 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1939 unsigned Line = getLineNumber(ED->getLocation());
1940 StringRef EDName = ED->getName();
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001941 llvm::DIType RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001942 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001943 0, Size, Align, llvm::DebugNode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001944 ReplaceMap.emplace_back(
1945 std::piecewise_construct, std::make_tuple(Ty),
1946 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001947 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001948 }
1949
David Blaikie483a9da2014-05-06 18:35:21 +00001950 return CreateTypeDefinition(Ty);
1951}
1952
1953llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1954 const EnumDecl *ED = Ty->getDecl();
1955 uint64_t Size = 0;
1956 uint64_t Align = 0;
1957 if (!ED->getTypeForDecl()->isIncompleteType()) {
1958 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1959 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1960 }
1961
1962 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1963
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001965 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001967 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001968 Enumerators.push_back(DBuilder.createEnumerator(
1969 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 }
1971
1972 // Return a CompositeType for the enum itself.
1973 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1974
1975 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1976 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001977 llvm::MDScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001978 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1979 llvm::DIType ClassTy = ED->isFixed()
1980 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1981 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001982 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001983 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1984 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 return DbgTy;
1986}
1987
David Blaikie05491062013-01-21 04:37:12 +00001988static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1989 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001990 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001991 Qualifiers InnerQuals = T.getLocalQualifiers();
1992 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1993 // that is already there.
1994 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1995 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 QualType LastT = T;
1997 switch (T->getTypeClass()) {
1998 default:
David Blaikie05491062013-01-21 04:37:12 +00001999 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002000 case Type::TemplateSpecialization: {
2001 const auto *Spec = cast<TemplateSpecializationType>(T);
2002 if (Spec->isTypeAlias())
2003 return C.getQualifiedType(T.getTypePtr(), Quals);
2004 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002005 break;
2006 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 case Type::TypeOfExpr:
2008 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2009 break;
2010 case Type::TypeOf:
2011 T = cast<TypeOfType>(T)->getUnderlyingType();
2012 break;
2013 case Type::Decltype:
2014 T = cast<DecltypeType>(T)->getUnderlyingType();
2015 break;
2016 case Type::UnaryTransform:
2017 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2018 break;
2019 case Type::Attributed:
2020 T = cast<AttributedType>(T)->getEquivalentType();
2021 break;
2022 case Type::Elaborated:
2023 T = cast<ElaboratedType>(T)->getNamedType();
2024 break;
2025 case Type::Paren:
2026 T = cast<ParenType>(T)->getInnerType();
2027 break;
David Blaikie05491062013-01-21 04:37:12 +00002028 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 break;
2031 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002032 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002033 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002034 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 break;
2036 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002037
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002039 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 } while (true);
2041}
2042
Eric Christopher0fdcb312013-05-16 00:52:20 +00002043/// getType - Get the type from the cache or return null type if it doesn't
2044/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002045llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2046
2047 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002048 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002049
David Blaikief427b002014-05-06 03:42:01 +00002050 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002051 if (it != TypeCache.end()) {
2052 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002053 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002054 return cast<llvm::MDType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 }
2056
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002057 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002058}
2059
David Blaikie0e716b42014-03-03 23:48:23 +00002060void CGDebugInfo::completeTemplateDefinition(
2061 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002062 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2063 return;
2064
David Blaikie0e716b42014-03-03 23:48:23 +00002065 completeClassData(&SD);
2066 // In case this type has no member function definitions being emitted, ensure
2067 // it is retained
2068 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2069}
2070
Guy Benyei11169dd2012-12-18 14:30:41 +00002071/// getOrCreateType - Get the type from the cache or create a new
2072/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002073llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 if (Ty.isNull())
2075 return llvm::DIType();
2076
2077 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002078 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002079
David Blaikieef8a9512014-05-05 23:23:53 +00002080 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002081 return T;
2082
2083 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002084 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002085 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002086
2087 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002088 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002089
Guy Benyei11169dd2012-12-18 14:30:41 +00002090 return Res;
2091}
2092
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002093/// Currently the checksum of an interface includes the number of
2094/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002095unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002096 // The assumption is that the number of ivars can only increase
2097 // monotonically, so it is safe to just use their current number as
2098 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002099 unsigned Sum = 0;
2100 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002101 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002102 ++Sum;
2103
2104 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002105}
2106
2107ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2108 switch (Ty->getTypeClass()) {
2109 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002110 return getObjCInterfaceDecl(
2111 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002112 case Type::ObjCInterface:
2113 return cast<ObjCInterfaceType>(Ty)->getDecl();
2114 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002115 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002116 }
2117}
2118
Guy Benyei11169dd2012-12-18 14:30:41 +00002119/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002120llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 // Handle qualifiers, which recursively handles what they refer to.
2122 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002123 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002124
Guy Benyei11169dd2012-12-18 14:30:41 +00002125 // Work out details of type.
2126 switch (Ty->getTypeClass()) {
2127#define TYPE(Class, Base)
2128#define ABSTRACT_TYPE(Class, Base)
2129#define NON_CANONICAL_TYPE(Class, Base)
2130#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2131#include "clang/AST/TypeNodes.def"
2132 llvm_unreachable("Dependent types cannot show up in debug information");
2133
2134 case Type::ExtVector:
2135 case Type::Vector:
2136 return CreateType(cast<VectorType>(Ty), Unit);
2137 case Type::ObjCObjectPointer:
2138 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2139 case Type::ObjCObject:
2140 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2141 case Type::ObjCInterface:
2142 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2143 case Type::Builtin:
2144 return CreateType(cast<BuiltinType>(Ty));
2145 case Type::Complex:
2146 return CreateType(cast<ComplexType>(Ty));
2147 case Type::Pointer:
2148 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002149 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002150 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002151 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002152 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002153 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002154 case Type::BlockPointer:
2155 return CreateType(cast<BlockPointerType>(Ty), Unit);
2156 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002157 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002159 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002160 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002161 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 case Type::FunctionProto:
2163 case Type::FunctionNoProto:
2164 return CreateType(cast<FunctionType>(Ty), Unit);
2165 case Type::ConstantArray:
2166 case Type::VariableArray:
2167 case Type::IncompleteArray:
2168 return CreateType(cast<ArrayType>(Ty), Unit);
2169
2170 case Type::LValueReference:
2171 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2172 case Type::RValueReference:
2173 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2174
2175 case Type::MemberPointer:
2176 return CreateType(cast<MemberPointerType>(Ty), Unit);
2177
2178 case Type::Atomic:
2179 return CreateType(cast<AtomicType>(Ty), Unit);
2180
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002182 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2183
David Blaikie42edade2014-11-11 20:44:45 +00002184 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002185 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 case Type::Elaborated:
2187 case Type::Paren:
2188 case Type::SubstTemplateTypeParm:
2189 case Type::TypeOfExpr:
2190 case Type::TypeOf:
2191 case Type::Decltype:
2192 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002193 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002194 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002196
David Blaikie42edade2014-11-11 20:44:45 +00002197 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002198}
2199
2200/// getOrCreateLimitedType - Get the type from the cache or create a new
2201/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002202llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002203 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002204 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002205
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002206 auto *T = cast_or_null<llvm::MDCompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002207
2208 // We may have cached a forward decl when we could have created
2209 // a non-forward decl. Go ahead and create a non-forward decl
2210 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002211 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002212 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002213
2214 // Otherwise create the type.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002215 llvm::MDCompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002216
2217 // Propagate members from the declaration to the definition
2218 // CreateType(const RecordType*) will overwrite this with the members in the
2219 // correct order if the full type is needed.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002220 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002221
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002223 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 return Res;
2225}
2226
2227// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002228llvm::MDCompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002230
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 // Get overall information about the record type for the debug info.
2232 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2233 unsigned Line = getLineNumber(RD->getLocation());
2234 StringRef RDName = getClassName(RD);
2235
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002236 llvm::MDScope *RDContext =
Eric Christopher07429ff2013-10-15 21:22:34 +00002237 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002238
David Blaikied2785892013-08-18 17:36:19 +00002239 // If we ended up creating the type during the context chain construction,
2240 // just return that.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002241 auto *T = cast_or_null<llvm::MDCompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002242 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002243 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002244 return T;
David Blaikied2785892013-08-18 17:36:19 +00002245
Adrian Prantl381e7552014-02-04 21:29:50 +00002246 // If this is just a forward or incomplete declaration, construct an
2247 // appropriately marked node and just return it.
2248 const RecordDecl *D = RD->getDefinition();
2249 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002250 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002251
2252 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2253 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002254
Manman Rene0064d82013-08-29 23:19:58 +00002255 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2256
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002257 llvm::MDCompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
2258 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2259 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002261 RegionMap[Ty->getDecl()].reset(RealDecl);
2262 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002263
David Blaikieadfbf992013-08-18 16:55:33 +00002264 if (const ClassTemplateSpecializationDecl *TSpecial =
2265 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002266 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2267 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002268 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002269}
2270
David Blaikieadfbf992013-08-18 16:55:33 +00002271void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002272 llvm::MDCompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002273 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002274 llvm::MDCompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002275 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2276 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002277 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002278 while (1) {
2279 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2280 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2281 if (PBT && !BRL.isPrimaryBaseVirtual())
2282 PBase = PBT;
2283 else
2284 break;
2285 }
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002286 ContainingType = cast<llvm::MDCompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002287 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2288 getOrCreateFile(RD->getLocation())));
2289 } else if (RD->isDynamicClass())
2290 ContainingType = RealDecl;
2291
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002292 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002293}
2294
Guy Benyei11169dd2012-12-18 14:30:41 +00002295/// CreateMemberType - Create new member and increase Offset by FType's size.
2296llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002297 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2299 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2300 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002301 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2302 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 *Offset += FieldSize;
2304 return Ty;
2305}
2306
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002307void CGDebugInfo::collectFunctionDeclProps(
2308 GlobalDecl GD, llvm::DIFile Unit, StringRef &Name, StringRef &LinkageName,
2309 llvm::MDScope *&FDContext, llvm::DIArray &TParamsArray, unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002310 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2311 Name = getFunctionName(FD);
2312 // Use mangled name as linkage name for C/C++ functions.
2313 if (FD->hasPrototype()) {
2314 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002315 Flags |= llvm::DebugNode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002316 }
2317 // No need to replicate the linkage name if it isn't different from the
2318 // subprogram name, no need to have it at all unless coverage is enabled or
2319 // debug is set to more than just line tables.
2320 if (LinkageName == Name ||
2321 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2322 !CGM.getCodeGenOpts().EmitGcovNotes &&
2323 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2324 LinkageName = StringRef();
2325
2326 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2327 if (const NamespaceDecl *NSDecl =
2328 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2329 FDContext = getOrCreateNameSpace(NSDecl);
2330 else if (const RecordDecl *RDecl =
2331 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2332 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2333 // Collect template parameters.
2334 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2335 }
2336}
2337
2338void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2339 unsigned &LineNo, QualType &T,
2340 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002341 llvm::MDScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002342 Unit = getOrCreateFile(VD->getLocation());
2343 LineNo = getLineNumber(VD->getLocation());
2344
2345 setLocation(VD->getLocation());
2346
2347 T = VD->getType();
2348 if (T->isIncompleteArrayType()) {
2349 // CodeGen turns int[] into int[1] so we'll do the same here.
2350 llvm::APInt ConstVal(32, 1);
2351 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2352
2353 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2354 ArrayType::Normal, 0);
2355 }
2356
2357 Name = VD->getName();
2358 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2359 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2360 LinkageName = CGM.getMangledName(VD);
2361 if (LinkageName == Name)
2362 LinkageName = StringRef();
2363
2364 // Since we emit declarations (DW_AT_members) for static members, place the
2365 // definition of those static members in the namespace they were declared in
2366 // in the source code (the lexical decl context).
2367 // FIXME: Generalize this for even non-member global variables where the
2368 // declaration and definition may have different lexical decl contexts, once
2369 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002370 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2371 : VD->getDeclContext();
2372 // When a record type contains an in-line initialization of a static data
2373 // member, and the record type is marked as __declspec(dllexport), an implicit
2374 // definition of the member will be created in the record context. DWARF
2375 // doesn't seem to have a nice way to describe this in a form that consumers
2376 // are likely to understand, so fake the "normal" situation of a definition
2377 // outside the class by putting it in the global scope.
2378 if (DC->isRecord())
2379 DC = CGM.getContext().getTranslationUnitDecl();
2380 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002381}
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);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002390 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002391 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());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002402 llvm::MDSubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002403 DContext, Name, LinkageName, Unit, Line,
2404 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2405 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2406 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002407 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002408 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2409 std::make_tuple(CanonDecl),
2410 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002411 return SP;
2412}
2413
2414llvm::DIGlobalVariable
2415CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2416 QualType T;
2417 StringRef Name, LinkageName;
2418 SourceLocation Loc = VD->getLocation();
2419 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002420 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002421 unsigned Line = getLineNumber(Loc);
2422
2423 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2424 llvm::DIGlobalVariable GV =
2425 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2426 Line, getOrCreateType(T, Unit),
2427 !VD->isExternallyVisible(),
2428 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002429 FwdDeclReplaceMap.emplace_back(
2430 std::piecewise_construct,
2431 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2432 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002433 return GV;
2434}
2435
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002436llvm::DebugNode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002437 // We only need a declaration (not a definition) of the type - so use whatever
2438 // we would otherwise do to get a type for a pointee. (forward declarations in
2439 // limited debug info, full definitions (if the type definition is available)
2440 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002441 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2442 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002443 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002444 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002445
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002446 if (I != DeclCache.end())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002447 return dyn_cast_or_null<llvm::DebugNode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002448
2449 // No definition for now. Emit a forward definition that might be
2450 // merged with a potential upcoming definition.
2451 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2452 return getFunctionForwardDeclaration(FD);
2453 else if (const auto *VD = dyn_cast<VarDecl>(D))
2454 return getGlobalVariableForwardDeclaration(VD);
2455
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002456 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002457}
2458
Guy Benyei11169dd2012-12-18 14:30:41 +00002459/// getFunctionDeclaration - Return debug info descriptor to describe method
2460/// declaration for the given method definition.
2461llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002462 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002463 return llvm::DISubprogram();
2464
Guy Benyei11169dd2012-12-18 14:30:41 +00002465 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002466 if (!FD)
2467 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002468
2469 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002470 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002471
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002472 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002473 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002474 if (const CXXMethodDecl *MD =
2475 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002476 llvm::DICompositeType T = cast<llvm::MDCompositeType>(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002477 llvm::DISubprogram SP =
2478 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002479 return SP;
2480 }
2481 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002483 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2484 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 return SP;
2486 }
2487
Aaron Ballman86c93902014-03-06 23:45:36 +00002488 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002489 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002490 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002491 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2492 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 return SP;
2494 }
2495 }
2496 return llvm::DISubprogram();
2497}
2498
2499// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2500// implicit parameter "this".
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002501llvm::MDSubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2502 QualType FnType,
2503 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002504 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002505 // Create fake but valid subroutine type. Otherwise
2506 // llvm::DISubprogram::Verify() would return false, and
2507 // subprogram DIE will miss DW_AT_decl_file and
2508 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002509 return DBuilder.createSubroutineType(F,
2510 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002511
2512 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2513 return getOrCreateMethodType(Method, F);
2514 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2515 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002516 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002517
2518 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002519 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002520
2521 // Replace the instancetype keyword with the actual type.
2522 if (ResultTy == CGM.getContext().getObjCInstanceType())
2523 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002524 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002525
Adrian Prantl7bec9032013-05-10 21:08:31 +00002526 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002528 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2529 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2530 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 // "_cmd" pointer is always second argument.
2532 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2533 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2534 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002535 for (const auto *PI : OMethod->params())
2536 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002537 // Variadic methods need a special marker at the end of the type list.
2538 if (OMethod->isVariadic())
2539 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002540
Manman Ren67f005e2014-07-28 22:24:34 +00002541 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002542 return DBuilder.createSubroutineType(F, EltTypeArray);
2543 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002544
Adrian Prantl800faef2014-02-25 23:42:18 +00002545 // Handle variadic function types; they need an additional
2546 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002547 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2548 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002549 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002550 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2551 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2552 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2553 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2554 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002555 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002556 return DBuilder.createSubroutineType(F, EltTypeArray);
2557 }
2558
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002559 return cast<llvm::MDSubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002560}
2561
2562/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002563void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2564 SourceLocation ScopeLoc, QualType FnType,
2565 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002566
2567 StringRef Name;
2568 StringRef LinkageName;
2569
2570 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2571
2572 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002573 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002574
Guy Benyei11169dd2012-12-18 14:30:41 +00002575 unsigned Flags = 0;
2576 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002577 llvm::MDScope *FDContext = Unit;
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 llvm::DIArray TParamsArray;
2579 if (!HasDecl) {
2580 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002581 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002582 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2583 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002584 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002586 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(FI->second);
2587 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002588 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002589 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002590 return;
2591 }
2592 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002593 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2594 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2596 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002597 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002598 } else {
2599 // Use llvm function name.
2600 Name = Fn->getName();
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002601 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 }
2603 if (!Name.empty() && Name[0] == '\01')
2604 Name = Name.substr(1);
2605
Adrian Prantl42d71b92014-04-10 23:21:53 +00002606 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002607 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002608 // Artificial functions without a location should not silently reuse CurLoc.
2609 if (Loc.isInvalid())
2610 CurLoc = SourceLocation();
2611 }
2612 unsigned LineNo = getLineNumber(Loc);
2613 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002614
Eric Christopher8018e412014-03-27 18:50:35 +00002615 // FIXME: The function declaration we're constructing here is mostly reusing
2616 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2617 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2618 // all subprograms instead of the actual context since subprogram definitions
2619 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002620 llvm::DISubprogram SP = DBuilder.createFunction(
2621 FDContext, Name, LinkageName, Unit, LineNo,
2622 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2623 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002624 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002625 // We might get here with a VarDecl in the case we're generating
2626 // code for the initialization of globals. Do not record these decls
2627 // as they will overwrite the actual VarDecl Decl in the cache.
2628 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002629 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002630
Adrian Prantlbebb8932014-03-21 21:01:58 +00002631 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002632 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002633
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002635 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002636}
2637
2638/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002639/// information in the source file. If the location is invalid, the
2640/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002641void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002642 // Update our current location
2643 setLocation(Loc);
2644
Eric Christophere7b87e52014-10-26 23:40:33 +00002645 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2646 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002647
Adrian Prantle83b1302014-01-07 22:05:52 +00002648 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002649 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002650 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002651}
2652
2653/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2654/// the stack.
2655void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002656 llvm::MDNode *Back = nullptr;
2657 if (!LexicalBlockStack.empty())
2658 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002659 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002660 cast<llvm::MDScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002661 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002662}
2663
2664/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2665/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002666void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2667 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002668 // Set our current location.
2669 setLocation(Loc);
2670
Guy Benyei11169dd2012-12-18 14:30:41 +00002671 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002672 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2673 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002674
2675 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2676 return;
2677
2678 // Create a new lexical block and push it on the stack.
2679 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002680}
2681
2682/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2683/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002684void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2685 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2687
2688 // Provide an entry in the line table for the end of the block.
2689 EmitLocation(Builder, Loc);
2690
David Blaikie60a877b2014-10-22 19:34:33 +00002691 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2692 return;
2693
Guy Benyei11169dd2012-12-18 14:30:41 +00002694 LexicalBlockStack.pop_back();
2695}
2696
2697/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2698void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2699 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2700 unsigned RCount = FnBeginRegionCount.back();
2701 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2702
2703 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002704 while (LexicalBlockStack.size() != RCount) {
2705 // Provide an entry in the line table for the end of the block.
2706 EmitLocation(Builder, CurLoc);
2707 LexicalBlockStack.pop_back();
2708 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 FnBeginRegionCount.pop_back();
2710}
2711
Eric Christopherb2a008c2013-05-16 00:45:12 +00002712// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002713// See BuildByRefType.
2714llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2715 uint64_t *XOffset) {
2716
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002717 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 QualType FType;
2719 uint64_t FieldSize, FieldOffset;
2720 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002721
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002723 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002724
2725 FieldOffset = 0;
2726 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2727 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2728 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2729 FType = CGM.getContext().IntTy;
2730 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2731 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2732
2733 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2734 if (HasCopyAndDispose) {
2735 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002736 EltTys.push_back(
2737 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2738 EltTys.push_back(
2739 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 }
2741 bool HasByrefExtendedLayout;
2742 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002743 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2744 HasByrefExtendedLayout) &&
2745 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002746 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002747 EltTys.push_back(
2748 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002749 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002750
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2752 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002753 CGM.getTarget().getPointerAlign(0))) {
2754 CharUnits FieldOffsetInBytes =
2755 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2756 CharUnits AlignedOffsetInBytes =
2757 FieldOffsetInBytes.RoundUpToAlignment(Align);
2758 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002759
Guy Benyei11169dd2012-12-18 14:30:41 +00002760 if (NumPaddingBytes.isPositive()) {
2761 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2762 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2763 pad, ArrayType::Normal, 0);
2764 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2765 }
2766 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002767
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002769 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 FieldSize = CGM.getContext().getTypeSize(FType);
2771 FieldAlign = CGM.getContext().toBits(Align);
2772
Eric Christopherb2a008c2013-05-16 00:45:12 +00002773 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002774 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2775 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 EltTys.push_back(FieldTy);
2777 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002778
Guy Benyei11169dd2012-12-18 14:30:41 +00002779 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002780
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002781 unsigned Flags = llvm::DebugNode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002782
Guy Benyei11169dd2012-12-18 14:30:41 +00002783 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002784 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002785}
2786
2787/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002788void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002789 llvm::Value *Storage, unsigned ArgNo,
2790 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002791 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2793
David Blaikie7fceebf2013-08-19 03:37:48 +00002794 bool Unwritten =
2795 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2796 cast<Decl>(VD->getDeclContext())->isImplicit());
2797 llvm::DIFile Unit;
2798 if (!Unwritten)
2799 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 llvm::DIType Ty;
2801 uint64_t XOffset = 0;
2802 if (VD->hasAttr<BlocksAttr>())
2803 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002804 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002805 Ty = getOrCreateType(VD->getType(), Unit);
2806
2807 // If there is no debug info for this type then do not emit debug info
2808 // for this variable.
2809 if (!Ty)
2810 return;
2811
Guy Benyei11169dd2012-12-18 14:30:41 +00002812 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002813 unsigned Line = 0;
2814 unsigned Column = 0;
2815 if (!Unwritten) {
2816 Line = getLineNumber(VD->getLocation());
2817 Column = getColumnNumber(VD->getLocation());
2818 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002819 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 unsigned Flags = 0;
2821 if (VD->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002822 Flags |= llvm::DebugNode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002823 // If this is the first argument and it is implicit then
2824 // give it an object pointer flag.
2825 // FIXME: There has to be a better way to do this, but for static
2826 // functions there won't be an implicit param at arg1 and
2827 // otherwise it is 'self' or 'this'.
2828 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002829 Flags |= llvm::DebugNode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002830 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002831 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2832 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002833 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002834
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002835 auto *Scope = cast<llvm::MDScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002836
2837 StringRef Name = VD->getName();
2838 if (!Name.empty()) {
2839 if (VD->hasAttr<BlocksAttr>()) {
2840 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002841 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 // offset of __forwarding field
2843 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002844 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002845 Expr.push_back(offset.getQuantity());
2846 Expr.push_back(llvm::dwarf::DW_OP_deref);
2847 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 // offset of x field
2849 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002850 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002851
2852 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002853 llvm::DIVariable D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002854 Tag, Scope, VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002855
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002857 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2858 llvm::DebugLoc::get(Line, Column, Scope),
2859 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002860 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002861 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002862 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002863 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2864 // If VD is an anonymous union then Storage represents value for
2865 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002867 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002868 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2870 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002871
Guy Benyei11169dd2012-12-18 14:30:41 +00002872 // Ignore unnamed fields. Do not ignore unnamed records.
2873 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2874 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002875
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002877 llvm::DIVariable D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002878 Tag, Scope, FieldName, Unit, Line, FieldTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00002879 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002880
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002882 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2883 llvm::DebugLoc::get(Line, Column, Scope),
2884 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 }
David Blaikie219c7d92013-01-05 20:03:07 +00002886 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 }
2888 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002889
2890 // Create the descriptor for the variable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002891 llvm::DIVariable D =
2892 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2893 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002894
2895 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002896 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2897 llvm::DebugLoc::get(Line, Column, Scope),
2898 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002899}
2900
2901void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2902 llvm::Value *Storage,
2903 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002904 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002905 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2906}
2907
Adrian Prantlde17db32013-03-29 19:20:29 +00002908/// Look up the completed type for a self pointer in the TypeCache and
2909/// create a copy of it with the ObjectPointer and Artificial flags
2910/// set. If the type is not cached, a new one is created. This should
2911/// never happen though, since creating a type for the implicit self
2912/// argument implies that we already parsed the interface definition
2913/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002914llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2915 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002916 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002917 if (CachedTy)
2918 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002919 return DBuilder.createObjectPointerType(Ty);
2920}
2921
Eric Christophere7b87e52014-10-26 23:40:33 +00002922void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2923 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002924 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002925 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002927
Craig Topper8a13c412014-05-21 05:09:00 +00002928 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002929 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002930
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002932
Guy Benyei11169dd2012-12-18 14:30:41 +00002933 uint64_t XOffset = 0;
2934 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2935 llvm::DIType Ty;
2936 if (isByRef)
2937 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002938 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 Ty = getOrCreateType(VD->getType(), Unit);
2940
2941 // Self is passed along as an implicit non-arg variable in a
2942 // block. Mark it as the object pointer.
2943 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002944 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002945
2946 // Get location information.
2947 unsigned Line = getLineNumber(VD->getLocation());
2948 unsigned Column = getColumnNumber(VD->getLocation());
2949
2950 const llvm::DataLayout &target = CGM.getDataLayout();
2951
2952 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002953 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002954 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2955
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002956 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002957 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002958 addr.push_back(llvm::dwarf::DW_OP_deref);
2959 addr.push_back(llvm::dwarf::DW_OP_plus);
2960 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002961 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002962 addr.push_back(llvm::dwarf::DW_OP_deref);
2963 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002964 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002965 offset =
2966 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002967 addr.push_back(offset.getQuantity());
2968 addr.push_back(llvm::dwarf::DW_OP_deref);
2969 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 // offset of x field
2971 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002972 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 }
2974
2975 // Create the descriptor for the variable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002976 llvm::DIVariable D = DBuilder.createLocalVariable(
2977 llvm::dwarf::DW_TAG_auto_variable,
2978 cast<llvm::MDLocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
2979 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002980
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002982 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2983 if (InsertPoint)
2984 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2985 InsertPoint);
2986 else
2987 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2988 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002989}
2990
2991/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2992/// variable declaration.
2993void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2994 unsigned ArgNo,
2995 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002996 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002997 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2998}
2999
3000namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003001struct BlockLayoutChunk {
3002 uint64_t OffsetInBits;
3003 const BlockDecl::Capture *Capture;
3004};
3005bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3006 return l.OffsetInBits < r.OffsetInBits;
3007}
Guy Benyei11169dd2012-12-18 14:30:41 +00003008}
3009
3010void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003011 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003012 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003013 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003015 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003016 ASTContext &C = CGM.getContext();
3017 const BlockDecl *blockDecl = block.getBlockDecl();
3018
3019 // Collect some general information about the block's location.
3020 SourceLocation loc = blockDecl->getCaretLocation();
3021 llvm::DIFile tunit = getOrCreateFile(loc);
3022 unsigned line = getLineNumber(loc);
3023 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003024
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 // Build the debug-info type for the block literal.
3026 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3027
3028 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003029 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003030
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003031 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3033 blockLayout->getElementOffsetInBits(0),
3034 tunit, tunit));
3035 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3036 blockLayout->getElementOffsetInBits(1),
3037 tunit, tunit));
3038 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3039 blockLayout->getElementOffsetInBits(2),
3040 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003041 auto *FnTy = block.getBlockExpr()->getFunctionType();
3042 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3043 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 blockLayout->getElementOffsetInBits(3),
3045 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003046 fields.push_back(createFieldType(
3047 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3048 ? C.getBlockDescriptorExtendedType()
3049 : C.getBlockDescriptorType()),
3050 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003051
3052 // We want to sort the captures by offset, not because DWARF
3053 // requires this, but because we're paranoid about debuggers.
3054 SmallVector<BlockLayoutChunk, 8> chunks;
3055
3056 // 'this' capture.
3057 if (blockDecl->capturesCXXThis()) {
3058 BlockLayoutChunk chunk;
3059 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003060 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003061 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 chunks.push_back(chunk);
3063 }
3064
3065 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003066 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 const VarDecl *variable = capture.getVariable();
3068 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3069
3070 // Ignore constant captures.
3071 if (captureInfo.isConstant())
3072 continue;
3073
3074 BlockLayoutChunk chunk;
3075 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003076 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003077 chunk.Capture = &capture;
3078 chunks.push_back(chunk);
3079 }
3080
3081 // Sort by offset.
3082 llvm::array_pod_sort(chunks.begin(), chunks.end());
3083
Eric Christophere7b87e52014-10-26 23:40:33 +00003084 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3085 e = chunks.end();
3086 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 uint64_t offsetInBits = i->OffsetInBits;
3088 const BlockDecl::Capture *capture = i->Capture;
3089
3090 // If we have a null capture, this must be the C++ 'this' capture.
3091 if (!capture) {
3092 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003093 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 QualType type = method->getThisType(C);
3095
3096 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3097 offsetInBits, tunit, tunit));
3098 continue;
3099 }
3100
3101 const VarDecl *variable = capture->getVariable();
3102 StringRef name = variable->getName();
3103
3104 llvm::DIType fieldType;
3105 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003106 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003107
3108 // FIXME: this creates a second copy of this type!
3109 uint64_t xoffset;
3110 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003111 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3112 fieldType =
3113 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3114 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003116 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3117 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003118 }
3119 fields.push_back(fieldType);
3120 }
3121
3122 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003123 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3124 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003125
3126 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3127
3128 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003129 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3130 CGM.getContext().toBits(block.BlockSize),
3131 CGM.getContext().toBits(block.BlockAlign), 0,
3132 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3134
3135 // Get overall information about the block.
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00003136 unsigned flags = llvm::DebugNode::FlagArtificial;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003137 auto *scope = cast<llvm::MDLocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003138
3139 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003140 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003141 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3142 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003143
Adrian Prantl616bef42013-03-14 21:52:59 +00003144 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003145 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003146 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003147 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003148 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003149 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003150
Adrian Prantl616bef42013-03-14 21:52:59 +00003151 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003152 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3153 llvm::DebugLoc::get(line, column, scope),
3154 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003155}
3156
David Blaikie6943dea2013-08-20 01:28:15 +00003157/// If D is an out-of-class definition of a static data member of a class, find
3158/// its corresponding in-class declaration.
3159llvm::DIDerivedType
3160CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3161 if (!D->isStaticDataMember())
3162 return llvm::DIDerivedType();
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003164 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003165 if (MI != StaticDataMemberCache.end()) {
3166 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003167 return cast<llvm::MDDerivedTypeBase>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003168 }
David Blaikiece763042013-08-20 21:49:21 +00003169
3170 // If the member wasn't found in the cache, lazily construct and add it to the
3171 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003172 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003173 llvm::DICompositeType Ctxt =
3174 cast<llvm::MDCompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003175 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003176}
3177
Eric Christophercab9fae2014-04-10 05:20:00 +00003178/// Recursively collect all of the member fields of a global anonymous decl and
3179/// create static variables for them. The first time this is called it needs
3180/// to be on a union and then from there we can have additional unnamed fields.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003181llvm::DIGlobalVariable CGDebugInfo::CollectAnonRecordDecls(
3182 const RecordDecl *RD, llvm::DIFile Unit, unsigned LineNo,
3183 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::MDScope *DContext) {
Eric Christophercab9fae2014-04-10 05:20:00 +00003184 llvm::DIGlobalVariable GV;
3185
3186 for (const auto *Field : RD->fields()) {
3187 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3188 StringRef FieldName = Field->getName();
3189
3190 // Ignore unnamed fields, but recurse into anonymous records.
3191 if (FieldName.empty()) {
3192 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3193 if (RT)
3194 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3195 Var, DContext);
3196 continue;
3197 }
3198 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003199 GV = DBuilder.createGlobalVariable(
3200 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3201 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003202 }
3203 return GV;
3204}
3205
Guy Benyei11169dd2012-12-18 14:30:41 +00003206/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003207void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003208 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003209 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003210 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003211 llvm::DIFile Unit;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003212 llvm::MDScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003213 unsigned LineNo;
3214 StringRef DeclName, LinkageName;
3215 QualType T;
3216 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003217
3218 // Attempt to store one global variable for the declaration - even if we
3219 // emit a lot of fields.
3220 llvm::DIGlobalVariable GV;
3221
3222 // If this is an anonymous union then we'll want to emit a global
3223 // variable for each member of the anonymous union so that it's possible
3224 // to find the name of any field in the union.
3225 if (T->isUnionType() && DeclName.empty()) {
3226 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003227 assert(RD->isAnonymousStructOrUnion() &&
3228 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003229 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3230 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003231 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003232 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3233 Var->hasInternalLinkage(), Var,
3234 getOrCreateStaticDataMemberDeclarationOrNull(D));
3235 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003236 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003237}
3238
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003239/// EmitGlobalVariable - Emit global variable's debug info.
3240void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3241 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003242 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003243 // Create the descriptor for the variable.
3244 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3245 StringRef Name = VD->getName();
3246 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3247 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3248 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3249 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3250 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3251 }
3252 // Do not use DIGlobalVariable for enums.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003253 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003254 return;
David Blaikiea15565562014-04-04 20:56:17 +00003255 // Do not emit separate definitions for function local const/statics.
3256 if (isa<FunctionDecl>(VD->getDeclContext()))
3257 return;
David Blaikiebb113912014-04-05 07:23:17 +00003258 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003259 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003260 if (VarD->isStaticDataMember()) {
3261 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3262 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003263 // Ensure that the type is retained even though it's otherwise unreferenced.
3264 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003265 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003266 return;
3267 }
3268
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003269 llvm::MDScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003270 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3271
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003272 auto &GV = DeclCache[VD];
3273 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003274 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003275 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003276 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003277 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003278}
3279
3280llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3281 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003282 return cast<llvm::MDScope>(LexicalBlockStack.back());
David Blaikiebd483762013-05-20 04:58:53 +00003283 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003284}
3285
David Blaikie9f88fe82013-04-22 06:13:21 +00003286void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003287 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3288 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003289 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003290 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3291 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003292 getLineNumber(UD.getLocation()));
3293}
3294
David Blaikiebd483762013-05-20 04:58:53 +00003295void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3296 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3297 return;
3298 assert(UD.shadow_size() &&
3299 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3300 // Emitting one decl is sufficient - debuggers can detect that this is an
3301 // overloaded name & provide lookup for all the overloads.
3302 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003303 if (llvm::DebugNode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003304 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003305 DBuilder.createImportedDeclaration(
3306 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3307 getLineNumber(USD.getLocation()));
3308}
3309
David Blaikief121b932013-05-20 22:50:41 +00003310llvm::DIImportedEntity
3311CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3312 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003313 return llvm::DIImportedEntity();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003314 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003315 if (VH)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003316 return cast<llvm::MDImportedEntity>(VH);
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003317 llvm::DIImportedEntity R;
David Blaikief121b932013-05-20 22:50:41 +00003318 if (const NamespaceAliasDecl *Underlying =
3319 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3320 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003321 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003322 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3323 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3324 NA.getName());
3325 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003326 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003327 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3328 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3329 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003330 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003331 return R;
3332}
3333
Guy Benyei11169dd2012-12-18 14:30:41 +00003334/// getOrCreateNamesSpace - Return namespace descriptor for the given
3335/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003336llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003337CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003338 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003339 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003341 return cast<llvm::MDNamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003342
Guy Benyei11169dd2012-12-18 14:30:41 +00003343 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3344 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003345 llvm::MDScope *Context =
3346 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00003347 llvm::DINameSpace NS =
3348 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003349 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003350 return NS;
3351}
3352
3353void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003354 // Creating types might create further types - invalidating the current
3355 // element and the size(), so don't cache/reference them.
3356 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3357 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003358 llvm::MDType *Ty = E.Type->getDecl()->getDefinition()
3359 ? CreateTypeDefinition(E.Type, E.Unit)
3360 : E.Decl;
3361 DBuilder.replaceTemporary(llvm::TempMDType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003362 }
3363
David Blaikief427b002014-05-06 03:42:01 +00003364 for (auto p : ReplaceMap) {
3365 assert(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003366 auto *Ty = cast<llvm::MDType>(p.second);
3367 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003368
David Blaikief427b002014-05-06 03:42:01 +00003369 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003370 assert(it != TypeCache.end());
3371 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003372
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003373 DBuilder.replaceTemporary(llvm::TempMDType(Ty),
3374 cast<llvm::MDType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003375 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003376
Frederic Rissd253ed62014-11-18 03:40:51 +00003377 for (const auto &p : FwdDeclReplaceMap) {
3378 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003379 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003380 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003381
3382 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003383 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003384 // with ourselves, that will destroy the temporary MDNode and
3385 // replace it with a standard one, avoiding leaking memory.
3386 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003387 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003388 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003389 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003390
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003391 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003392 }
3393
Adrian Prantl73409ce2013-03-11 18:33:46 +00003394 // We keep our own list of retained types, because we need to look
3395 // up the final type in the type cache.
3396 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3397 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003398 DBuilder.retainType(cast<llvm::MDType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003399
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 DBuilder.finalize();
3401}
David Blaikie66088d52014-09-24 17:01:27 +00003402
3403void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3404 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3405 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003406
3407 if (llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
3408 // Don't ignore in case of explicit cast where it is referenced indirectly.
3409 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003410}