blob: ed8bb20756f55e4886dacc09fff9d9468b4d4419 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000040#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041using namespace clang;
42using namespace clang::CodeGen;
43
44CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000045 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
46 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000047 CreateCompileUnit();
48}
49
50CGDebugInfo::~CGDebugInfo() {
51 assert(LexicalBlockStack.empty() &&
52 "Region stack mismatch, stack not empty!");
53}
54
David Blaikie66e41972015-01-14 07:38:27 +000055ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000056 SourceLocation TemporaryLocation)
David Blaikie66e41972015-01-14 07:38:27 +000057 : CGF(CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000058 init(TemporaryLocation);
59}
60
Adrian Prantl39428e72015-02-03 18:40:42 +000061ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000062 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000063 SourceLocation TemporaryLocation)
64 : CGF(CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000065 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000066}
67
68void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000069 bool DefaultToEmpty) {
David Blaikie66e41972015-01-14 07:38:27 +000070 if (auto *DI = CGF.getDebugInfo()) {
71 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Adrian Prantl39428e72015-02-03 18:40:42 +000072 if (TemporaryLocation.isInvalid()) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000073 if (DefaultToEmpty)
Adrian Prantl39428e72015-02-03 18:40:42 +000074 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc());
75 else {
76 // Construct a location that has a valid scope, but no line info.
77 assert(!DI->LexicalBlockStack.empty());
78 llvm::DIDescriptor Scope(DI->LexicalBlockStack.back());
79 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(0, 0, Scope));
80 }
81 } else
David Blaikie835afb22015-01-21 23:08:17 +000082 DI->EmitLocation(CGF.Builder, TemporaryLocation);
David Blaikie66e41972015-01-14 07:38:27 +000083 }
Adrian Prantl2e0637f2013-07-18 00:28:02 +000084}
85
David Blaikie9b479662015-01-25 01:19:10 +000086ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
87 : CGF(CGF) {
88 init(E->getExprLoc());
89}
90
David Blaikie66e41972015-01-14 07:38:27 +000091ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
92 : CGF(CGF) {
93 if (CGF.getDebugInfo()) {
94 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
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)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000130 llvm::DIDescriptor D = DBuilder.createLexicalBlockFile(
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +0000131 LBF->getScope(), getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000132 llvm::MDNode *N = D;
133 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000134 LexicalBlockStack.emplace_back(N);
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000135 } else if (isa<llvm::MDLexicalBlock>(Scope) ||
136 isa<llvm::MDSubprogram>(Scope)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000137 llvm::DIDescriptor D =
138 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 llvm::MDNode *N = D;
140 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000141 LexicalBlockStack.emplace_back(N);
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 }
143}
144
145/// getContextDescriptor - Get context info for the decl.
David Blaikiebfa52742013-04-19 06:56:38 +0000146llvm::DIScope CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 if (!Context)
148 return TheCU;
149
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000150 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000152 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000153 return dyn_cast_or_null<llvm::MDScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000154 }
155
156 // Check namespace.
157 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000158 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000159
David Blaikiebfa52742013-04-19 06:56:38 +0000160 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
161 if (!RDecl->isDependentType())
162 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000163 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000164 return TheCU;
165}
166
167/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000168/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000169/// is stored on the side.
170StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000171 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000172 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000173 FunctionTemplateSpecializationInfo *Info =
174 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000175 if (!Info && FII)
176 return FII->getName();
177
178 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000179 SmallString<128> NS;
180 llvm::raw_svector_ostream OS(NS);
181 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000182
183 // Add any template specialization args.
184 if (Info) {
185 const TemplateArgumentList *TArgs = Info->TemplateArguments;
186 const TemplateArgument *Args = TArgs->data();
187 unsigned NumArgs = TArgs->size();
188 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000189 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
190 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000191 }
192
193 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000194 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000195}
196
197StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
198 SmallString<256> MethodName;
199 llvm::raw_svector_ostream OS(MethodName);
200 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
201 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000202 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000203 dyn_cast<const ObjCImplementationDecl>(DC)) {
204 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000205 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000206 dyn_cast<const ObjCInterfaceDecl>(DC)) {
207 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000208 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000209 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
210 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
211 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000212 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000213 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000215 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000216 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000217 ClassTy.print(OS, PrintingPolicy(LangOptions()));
218 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000219 }
220 OS << ' ' << OMD->getSelector().getAsString() << ']';
221
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000222 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000223}
224
225/// getSelectorName - Return selector name. This is used for debugging
226/// info.
227StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000228 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000229}
230
231/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000232StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000233 // quick optimization to avoid having to intern strings that are already
234 // stored reliably elsewhere
235 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000236 return RD->getName();
237
David Blaikie65813a32014-04-02 18:21:09 +0000238 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 {
David Blaikie65813a32014-04-02 18:21:09 +0000240 llvm::raw_svector_ostream OS(Name);
241 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
242 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000243 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000244
245 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000246 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000247}
248
249/// getOrCreateFile - Get the file debug info descriptor for the input location.
250llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
251 if (!Loc.isValid())
252 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000253 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000254
255 SourceManager &SM = CGM.getContext().getSourceManager();
256 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
257
258 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
259 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000260 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
262 // Cache the results.
263 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000264 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000265
266 if (it != DIFileCache.end()) {
267 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000268 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000269 return cast<llvm::MDFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000270 }
271
272 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
273
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000274 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000275 return F;
276}
277
278/// getOrCreateMainFile - Get the file info for main compile unit.
279llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000280 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000281}
282
283/// getLineNumber - Get line number for the location. If location is invalid
284/// then use current location.
285unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
286 if (Loc.isInvalid() && CurLoc.isInvalid())
287 return 0;
288 SourceManager &SM = CGM.getContext().getSourceManager();
289 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000290 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000291}
292
293/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000294unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000295 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000296 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000297 return 0;
298
299 // If the location is invalid then use the current column.
300 if (Loc.isInvalid() && CurLoc.isInvalid())
301 return 0;
302 SourceManager &SM = CGM.getContext().getSourceManager();
303 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000304 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
307StringRef CGDebugInfo::getCurrentDirname() {
308 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
309 return CGM.getCodeGenOpts().DebugCompilationDir;
310
311 if (!CWDName.empty())
312 return CWDName;
313 SmallString<256> CWD;
314 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000315 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000316}
317
318/// CreateCompileUnit - Create new compile unit.
319void CGDebugInfo::CreateCompileUnit() {
320
David Blaikieaabde052014-05-14 00:29:00 +0000321 // Should we be asking the SourceManager for the main file name, instead of
322 // accepting it as an argument? This just causes the main file name to
323 // mismatch with source locations and create extra lexical scopes or
324 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
325 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
326 // because that's what the SourceManager says)
327
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 // Get absolute path name.
329 SourceManager &SM = CGM.getContext().getSourceManager();
330 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
331 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000332 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000333
334 // The main file name provided via the "-main-file-name" option contains just
335 // the file name itself with no path information. This file name may have had
336 // a relative path, so we look into the actual file entry for the main
337 // file to determine the real absolute path for the file.
338 std::string MainFileDir;
339 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
340 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000341 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000342 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
343 llvm::sys::path::append(MainFileDirSS, MainFileName);
344 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000345 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000346 }
347
348 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000349 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000350
351 // Save split dwarf file string.
352 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000353 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000354
Ed Masteda706022014-05-07 12:49:30 +0000355 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000356 const LangOptions &LO = CGM.getLangOpts();
357 if (LO.CPlusPlus) {
358 if (LO.ObjC1)
359 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
360 else
361 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
362 } else if (LO.ObjC1) {
363 LangTag = llvm::dwarf::DW_LANG_ObjC;
364 } else if (LO.C99) {
365 LangTag = llvm::dwarf::DW_LANG_C99;
366 } else {
367 LangTag = llvm::dwarf::DW_LANG_C89;
368 }
369
370 std::string Producer = getClangFullVersion();
371
372 // Figure out which version of the ObjC runtime we have.
373 unsigned RuntimeVers = 0;
374 if (LO.ObjC1)
375 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
376
377 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000379 TheCU = DBuilder.createCompileUnit(
380 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
381 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000382 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000383 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000384 : llvm::DIBuilder::FullDebug,
385 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000386}
387
388/// CreateType - Get the Basic type from the cache or create a new
389/// one if necessary.
390llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000391 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000392 StringRef BTName;
393 switch (BT->getKind()) {
394#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000395#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000396#include "clang/AST/BuiltinTypes.def"
397 case BuiltinType::Dependent:
398 llvm_unreachable("Unexpected builtin type");
399 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000400 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 case BuiltinType::Void:
402 return llvm::DIType();
403 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000404 if (!ClassTy)
405 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
406 "objc_class", TheCU,
407 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return ClassTy;
409 case BuiltinType::ObjCId: {
410 // typedef struct objc_class *Class;
411 // typedef struct objc_object {
412 // Class isa;
413 // } *id;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 return ObjTy;
417
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000418 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
420 "objc_class", TheCU,
421 getOrCreateMainFile(), 0);
422
423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000424
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 llvm::DIType ISATy = DBuilder.createPointerType(ClassTy, Size);
426
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000427 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000428 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
429 0, 0, 0, 0, llvm::DIType(), llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000430
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000431 DBuilder.replaceArrays(
432 ObjTy,
433 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
434 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return ObjTy;
436 }
437 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000438 if (!SelTy)
439 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
440 "objc_selector", TheCU,
441 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 return SelTy;
443 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444
445 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000448 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449 OCLImage1dArrayDITy);
450 case BuiltinType::OCLImage1dBuffer:
451 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
452 OCLImage1dBufferDITy);
453 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000455 case BuiltinType::OCLImage2dArray:
456 return getOrCreateStructPtrType("opencl_image2d_array_t",
457 OCLImage2dArrayDITy);
458 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000460 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 return DBuilder.createBasicType(
462 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
463 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000464 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000466
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::Char_U:
469 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 case BuiltinType::SChar:
473 Encoding = llvm::dwarf::DW_ATE_signed_char;
474 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000475 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::Char32:
477 Encoding = llvm::dwarf::DW_ATE_UTF;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 case BuiltinType::UShort:
480 case BuiltinType::UInt:
481 case BuiltinType::UInt128:
482 case BuiltinType::ULong:
483 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::ULongLong:
485 Encoding = llvm::dwarf::DW_ATE_unsigned;
486 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 case BuiltinType::Short:
488 case BuiltinType::Int:
489 case BuiltinType::Int128:
490 case BuiltinType::Long:
491 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000492 case BuiltinType::LongLong:
493 Encoding = llvm::dwarf::DW_ATE_signed;
494 break;
495 case BuiltinType::Bool:
496 Encoding = llvm::dwarf::DW_ATE_boolean;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::Half:
499 case BuiltinType::Float:
500 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000501 case BuiltinType::Double:
502 Encoding = llvm::dwarf::DW_ATE_float;
503 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 }
505
506 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000507 case BuiltinType::Long:
508 BTName = "long int";
509 break;
510 case BuiltinType::LongLong:
511 BTName = "long long int";
512 break;
513 case BuiltinType::ULong:
514 BTName = "long unsigned int";
515 break;
516 case BuiltinType::ULongLong:
517 BTName = "long long unsigned int";
518 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000519 default:
520 BTName = BT->getName(CGM.getLangOpts());
521 break;
522 }
523 // Bit size, align and offset of the type.
524 uint64_t Size = CGM.getContext().getTypeSize(BT);
525 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 llvm::DIType DbgTy = DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 return DbgTy;
528}
529
530llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
531 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000532 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000533 if (Ty->isComplexIntegerType())
534 Encoding = llvm::dwarf::DW_ATE_lo_user;
535
536 uint64_t Size = CGM.getContext().getTypeSize(Ty);
537 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000538 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000540
541 return DbgTy;
542}
543
544/// CreateCVRType - Get the qualified type from the cache or create
545/// a new one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +0000546llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000547 QualifierCollector Qc;
548 const Type *T = Qc.strip(Ty);
549
550 // Ignore these qualifiers for now.
551 Qc.removeObjCGCAttr();
552 Qc.removeAddressSpace();
553 Qc.removeObjCLifetime();
554
555 // We will create one Derived type for one qualifier and recurse to handle any
556 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000557 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000558 if (Qc.hasConst()) {
559 Tag = llvm::dwarf::DW_TAG_const_type;
560 Qc.removeConst();
561 } else if (Qc.hasVolatile()) {
562 Tag = llvm::dwarf::DW_TAG_volatile_type;
563 Qc.removeVolatile();
564 } else if (Qc.hasRestrict()) {
565 Tag = llvm::dwarf::DW_TAG_restrict_type;
566 Qc.removeRestrict();
567 } else {
568 assert(Qc.empty() && "Unknown type qualifier for debug info");
569 return getOrCreateType(QualType(T, 0), Unit);
570 }
571
David Blaikie99dab3b2013-09-04 22:03:57 +0000572 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000573
574 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
575 // CVR derived types.
576 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000577
Guy Benyei11169dd2012-12-18 14:30:41 +0000578 return DbgTy;
579}
580
581llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
582 llvm::DIFile Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000583
584 // The frontend treats 'id' as a typedef to an ObjCObjectType,
585 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
586 // debug info, we want to emit 'id' in both cases.
587 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000588 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000589
Eric Christophere7b87e52014-10-26 23:40:33 +0000590 llvm::DIType DbgTy = CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type,
591 Ty, Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 return DbgTy;
593}
594
Eric Christophere7b87e52014-10-26 23:40:33 +0000595llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, llvm::DIFile Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000596 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000597 Ty->getPointeeType(), Unit);
598}
599
Manman Rene0064d82013-08-29 23:19:58 +0000600/// In C++ mode, types have linkage, so we can rely on the ODR and
601/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000602static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
603 CodeGenModule &CGM,
604 llvm::DICompileUnit TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000605 SmallString<256> FullName;
606 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
607 // For now, only apply ODR with C++.
608 const TagDecl *TD = Ty->getDecl();
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000609 if (TheCU->getSourceLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
Manman Rene0064d82013-08-29 23:19:58 +0000610 !TD->isExternallyVisible())
611 return FullName;
612 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
613 if (CGM.getTarget().getCXXABI().isMicrosoft())
614 return FullName;
615
616 // TODO: This is using the RTTI name. Is there a better way to get
617 // a unique string for a type?
618 llvm::raw_svector_ostream Out(FullName);
619 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
620 Out.flush();
621 return FullName;
622}
623
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000624static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
625 llvm::dwarf::Tag Tag;
626 if (RD->isStruct() || RD->isInterface())
627 Tag = llvm::dwarf::DW_TAG_structure_type;
628 else if (RD->isUnion())
629 Tag = llvm::dwarf::DW_TAG_union_type;
630 else {
631 // FIXME: This could be a struct type giving a default visibility different
632 // than C++ class type, but needs llvm metadata changes first.
633 assert(RD->isClass());
634 Tag = llvm::dwarf::DW_TAG_class_type;
635 }
636 return Tag;
637}
638
Guy Benyei11169dd2012-12-18 14:30:41 +0000639// Creates a forward declaration for a RecordDecl in the given context.
David Blaikie8d5e1282013-08-20 21:03:29 +0000640llvm::DICompositeType
Manman Ren1b457022013-08-28 21:20:28 +0000641CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
David Blaikie8d5e1282013-08-20 21:03:29 +0000642 llvm::DIDescriptor Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000643 const RecordDecl *RD = Ty->getDecl();
David Blaikie4e7ef802013-08-15 20:17:25 +0000644 if (llvm::DIType T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000645 return cast<llvm::MDCompositeTypeBase>(T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000646 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
647 unsigned Line = getLineNumber(RD->getLocation());
648 StringRef RDName = getClassName(RD);
649
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000650 uint64_t Size = 0;
651 uint64_t Align = 0;
652
653 const RecordDecl *D = RD->getDefinition();
654 if (D && D->isCompleteDefinition()) {
655 Size = CGM.getContext().getTypeSize(Ty);
656 Align = CGM.getContext().getTypeAlign(Ty);
657 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000658
659 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000660 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000661 llvm::DICompositeType RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000662 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000663 llvm::DIDescriptor::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000664 ReplaceMap.emplace_back(
665 std::piecewise_construct, std::make_tuple(Ty),
666 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000667 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000668}
669
Ed Masteda706022014-05-07 12:49:30 +0000670llvm::DIType CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Eric Christopherb2a008c2013-05-16 00:45:12 +0000671 const Type *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 QualType PointeeTy,
673 llvm::DIFile Unit) {
674 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
675 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000676 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000677
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 // Bit size, align and offset of the type.
679 // Size is always the size of a pointer. We can't use getTypeSize here
680 // because that does not return the correct value for references.
681 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000682 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
684
David Blaikie99dab3b2013-09-04 22:03:57 +0000685 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
686 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687}
688
Eric Christopher0fdcb312013-05-16 00:52:20 +0000689llvm::DIType CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
690 llvm::DIType &Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000691 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000692 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000693 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
694 TheCU, getOrCreateMainFile(), 0);
695 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
696 Cache = DBuilder.createPointerType(Cache, Size);
697 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000698}
699
Guy Benyei11169dd2012-12-18 14:30:41 +0000700llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
701 llvm::DIFile Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000702 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000703 return BlockLiteralGeneric;
704
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000705 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 llvm::DIType FieldTy;
707 QualType FType;
708 uint64_t FieldSize, FieldOffset;
709 unsigned FieldAlign;
710 llvm::DIArray Elements;
711 llvm::DIType EltTy, DescTy;
712
713 FieldOffset = 0;
714 FType = CGM.getContext().UnsignedLongTy;
715 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
716 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
717
718 Elements = DBuilder.getOrCreateArray(EltTys);
719 EltTys.clear();
720
721 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
722 unsigned LineNo = getLineNumber(CurLoc);
723
Eric Christophere7b87e52014-10-26 23:40:33 +0000724 EltTy = DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
725 FieldOffset, 0, Flags, llvm::DIType(),
726 Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727
728 // Bit size, align and offset of the type.
729 uint64_t Size = CGM.getContext().getTypeSize(Ty);
730
731 DescTy = DBuilder.createPointerType(EltTy, Size);
732
733 FieldOffset = 0;
734 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
735 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
736 FType = CGM.getContext().IntTy;
737 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
738 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000739 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000740 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
741
742 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
743 FieldTy = DescTy;
744 FieldSize = CGM.getContext().getTypeSize(Ty);
745 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Eric Christophere7b87e52014-10-26 23:40:33 +0000746 FieldTy =
747 DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo, FieldSize,
748 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000749 EltTys.push_back(FieldTy);
750
751 FieldOffset += FieldSize;
752 Elements = DBuilder.getOrCreateArray(EltTys);
753
Eric Christophere7b87e52014-10-26 23:40:33 +0000754 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic", Unit,
755 LineNo, FieldOffset, 0, Flags,
756 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000757
Guy Benyei11169dd2012-12-18 14:30:41 +0000758 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
759 return BlockLiteralGeneric;
760}
761
Eric Christophere7b87e52014-10-26 23:40:33 +0000762llvm::DIType CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
763 llvm::DIFile Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000764 assert(Ty->isTypeAlias());
765 llvm::DIType Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000766
767 SmallString<128> NS;
768 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000769 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
770 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000771
772 TemplateSpecializationType::PrintTemplateArgumentList(
773 OS, Ty->getArgs(), Ty->getNumArgs(),
774 CGM.getContext().getPrintingPolicy());
775
Eric Christophere7b87e52014-10-26 23:40:33 +0000776 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
777 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000778
779 SourceLocation Loc = AliasDecl->getLocation();
780 llvm::DIFile File = getOrCreateFile(Loc);
781 unsigned Line = getLineNumber(Loc);
782
Eric Christophere7b87e52014-10-26 23:40:33 +0000783 llvm::DIDescriptor Ctxt =
784 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext()));
David Blaikief1b382e2014-04-06 17:14:06 +0000785
786 return DBuilder.createTypedef(Src, internString(OS.str()), File, Line, Ctxt);
787}
788
David Blaikie99dab3b2013-09-04 22:03:57 +0000789llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000790 // Typedefs are derived from some other type. If we have a typedef of a
791 // typedef, make sure to emit the whole chain.
David Blaikie99dab3b2013-09-04 22:03:57 +0000792 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000793 // We don't set size information, but do specify where the typedef was
794 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000795 SourceLocation Loc = Ty->getDecl()->getLocation();
796 llvm::DIFile File = getOrCreateFile(Loc);
797 unsigned Line = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000798 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000799
Guy Benyei11169dd2012-12-18 14:30:41 +0000800 llvm::DIDescriptor TypedefContext =
Eric Christophere7b87e52014-10-26 23:40:33 +0000801 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000802
Eric Christophere7b87e52014-10-26 23:40:33 +0000803 return DBuilder.createTypedef(Src, TyDecl->getName(), File, Line,
804 TypedefContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000805}
806
807llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
808 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000809 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000810
811 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000812 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000813
814 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000815 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 if (isa<FunctionNoProtoType>(Ty))
817 EltTys.push_back(DBuilder.createUnspecifiedParameter());
818 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000819 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
820 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000821 if (FPT->isVariadic())
822 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000823 }
824
Manman Ren67f005e2014-07-28 22:24:34 +0000825 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000826 return DBuilder.createSubroutineType(Unit, EltTypeArray);
827}
828
Adrian Prantl21361fb2014-08-29 22:44:27 +0000829/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
830/// As an optimization, return 0 if the access specifier equals the
831/// default for the containing type.
832static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
833 AccessSpecifier Default = clang::AS_none;
834 if (RD && RD->isClass())
835 Default = clang::AS_private;
836 else if (RD && (RD->isStruct() || RD->isUnion()))
837 Default = clang::AS_public;
838
839 if (Access == Default)
840 return 0;
841
Eric Christophere7b87e52014-10-26 23:40:33 +0000842 switch (Access) {
843 case clang::AS_private:
844 return llvm::DIDescriptor::FlagPrivate;
845 case clang::AS_protected:
846 return llvm::DIDescriptor::FlagProtected;
847 case clang::AS_public:
848 return llvm::DIDescriptor::FlagPublic;
849 case clang::AS_none:
850 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000851 }
852 llvm_unreachable("unexpected access enumerator");
853}
Guy Benyei11169dd2012-12-18 14:30:41 +0000854
Eric Christophere7b87e52014-10-26 23:40:33 +0000855llvm::DIType CGDebugInfo::createFieldType(
856 StringRef name, QualType type, uint64_t sizeInBitsOverride,
857 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
858 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 llvm::DIType debugType = getOrCreateType(type, tunit);
860
861 // Get the location for the field.
862 llvm::DIFile file = getOrCreateFile(loc);
863 unsigned line = getLineNumber(loc);
864
David Majnemer34b57492014-07-30 01:30:47 +0000865 uint64_t SizeInBits = 0;
866 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000867 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000868 TypeInfo TI = CGM.getContext().getTypeInfo(type);
869 SizeInBits = TI.Width;
870 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000871
872 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000873 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000874 }
875
Adrian Prantl21361fb2014-08-29 22:44:27 +0000876 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000877 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
878 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000879}
880
Eric Christopher91a31902013-01-16 01:22:32 +0000881/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000882void CGDebugInfo::CollectRecordLambdaFields(
883 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
884 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000885 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
886 // has the name and the location of the variable so we should iterate over
887 // both concurrently.
888 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
889 RecordDecl::field_iterator Field = CXXDecl->field_begin();
890 unsigned fieldno = 0;
891 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000892 E = CXXDecl->captures_end();
893 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000894 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000895 if (C.capturesVariable()) {
896 VarDecl *V = C.getCapturedVar();
897 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
898 StringRef VName = V->getName();
899 uint64_t SizeInBitsOverride = 0;
900 if (Field->isBitField()) {
901 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
902 assert(SizeInBitsOverride && "found named 0-width bitfield");
903 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000904 llvm::DIType fieldType = createFieldType(
905 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
906 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
907 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000908 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000909 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000910 // TODO: Need to handle 'this' in some way by probably renaming the
911 // this of the lambda class and having a field member of 'this' or
912 // by using AT_object_pointer for the function and having that be
913 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000914 FieldDecl *f = *Field;
915 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
916 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000917 llvm::DIType fieldType = createFieldType(
918 "this", type, 0, f->getLocation(), f->getAccess(),
919 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000920
921 elements.push_back(fieldType);
922 }
923 }
924}
925
David Blaikie6943dea2013-08-20 01:28:15 +0000926/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000927llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
928 llvm::DIType RecordTy,
929 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000930 // Create the descriptor for the static variable, with or without
931 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000932 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000933 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
934 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
935
Eric Christopher91a31902013-01-16 01:22:32 +0000936 unsigned LineNumber = getLineNumber(Var->getLocation());
937 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000938 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000939 if (Var->getInit()) {
940 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000941 if (Value) {
942 if (Value->isInt())
943 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
944 if (Value->isFloat())
945 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
946 }
Eric Christopher91a31902013-01-16 01:22:32 +0000947 }
948
Adrian Prantl21361fb2014-08-29 22:44:27 +0000949 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000950 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
951 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000952 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000953 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000954}
955
956/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000957void CGDebugInfo::CollectRecordNormalField(
958 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000959 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000960 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000961 StringRef name = field->getName();
962 QualType type = field->getType();
963
964 // Ignore unnamed fields unless they're anonymous structs/unions.
965 if (name.empty() && !type->isRecordType())
966 return;
967
968 uint64_t SizeInBitsOverride = 0;
969 if (field->isBitField()) {
970 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
971 assert(SizeInBitsOverride && "found named 0-width bitfield");
972 }
973
Eric Christophere7b87e52014-10-26 23:40:33 +0000974 llvm::DIType fieldType =
975 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
976 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000977
978 elements.push_back(fieldType);
979}
980
Guy Benyei11169dd2012-12-18 14:30:41 +0000981/// CollectRecordFields - A helper function to collect debug info for
982/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000983void CGDebugInfo::CollectRecordFields(
984 const RecordDecl *record, llvm::DIFile tunit,
985 SmallVectorImpl<llvm::Metadata *> &elements,
986 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000987 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
988
Eric Christopher91a31902013-01-16 01:22:32 +0000989 if (CXXDecl && CXXDecl->isLambda())
990 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
991 else {
992 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000993
Eric Christopher91a31902013-01-16 01:22:32 +0000994 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000995 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000996
Eric Christopher91a31902013-01-16 01:22:32 +0000997 // Static and non-static members should appear in the same order as
998 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000999 for (const auto *I : record->decls())
1000 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001001 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001002 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001003 if (MI != StaticDataMemberCache.end()) {
1004 assert(MI->second &&
1005 "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001006 elements.push_back(cast<llvm::MDDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +00001007 } else {
1008 auto Field = CreateRecordStaticField(V, RecordTy, record);
1009 elements.push_back(Field);
1010 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001011 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001012 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1013 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001014
1015 // Bump field number for next field.
1016 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001017 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001018 }
1019}
1020
1021/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1022/// function type is not updated to include implicit "this" pointer. Use this
1023/// routine to get a method type which includes "this" pointer.
David Blaikie469f0792013-05-22 23:22:42 +00001024llvm::DICompositeType
Guy Benyei11169dd2012-12-18 14:30:41 +00001025CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1026 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001027 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001028 if (Method->isStatic())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001029 return cast_or_null<llvm::MDCompositeTypeBase>(
1030 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001031 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1032 Func, Unit);
1033}
David Blaikie2aaf0652013-01-07 22:24:59 +00001034
David Blaikie469f0792013-05-22 23:22:42 +00001035llvm::DICompositeType CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001036 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001037 // Add "this" pointer.
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001038 llvm::DITypeArray Args(
1039 cast<llvm::MDSubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
1040 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001041 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001042
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001043 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
1045 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001046 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001047
David Blaikie2aaf0652013-01-07 22:24:59 +00001048 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001049 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001050 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1051 // Create pointer type directly in this case.
1052 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1053 QualType PointeeTy = ThisPtrTy->getPointeeType();
1054 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001055 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001056 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1057 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001058 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001059 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001060 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001061 // TODO: This and the artificial type below are misleading, the
1062 // types aren't artificial the argument is, but the current
1063 // metadata doesn't represent that.
1064 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1065 Elts.push_back(ThisPtrType);
1066 } else {
1067 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001068 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001069 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1070 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001071 }
1072
1073 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001074 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1075 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076
Manman Ren67f005e2014-07-28 22:24:34 +00001077 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001078
Adrian Prantl0630eb72013-12-18 21:48:18 +00001079 unsigned Flags = 0;
1080 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
1081 Flags |= llvm::DIDescriptor::FlagLValueReference;
1082 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
1083 Flags |= llvm::DIDescriptor::FlagRValueReference;
1084
1085 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001086}
1087
Eric Christopherb2a008c2013-05-16 00:45:12 +00001088/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001089/// inside a function.
1090static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1091 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1092 return isFunctionLocalClass(NRD);
1093 if (isa<FunctionDecl>(RD->getDeclContext()))
1094 return true;
1095 return false;
1096}
1097
1098/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1099/// a single member function GlobalDecl.
1100llvm::DISubprogram
1101CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001102 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001104 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001105
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 StringRef MethodName = getFunctionName(Method);
David Blaikie469f0792013-05-22 23:22:42 +00001107 llvm::DICompositeType MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001108
1109 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1110 // make sense to give a single ctor/dtor a linkage name.
1111 StringRef MethodLinkageName;
1112 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1113 MethodLinkageName = CGM.getMangledName(Method);
1114
1115 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001116 llvm::DIFile MethodDefUnit;
1117 unsigned MethodLine = 0;
1118 if (!Method->isImplicit()) {
1119 MethodDefUnit = getOrCreateFile(Method->getLocation());
1120 MethodLine = getLineNumber(Method->getLocation());
1121 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001122
1123 // Collect virtual method info.
1124 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001125 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001127
Guy Benyei11169dd2012-12-18 14:30:41 +00001128 if (Method->isVirtual()) {
1129 if (Method->isPure())
1130 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1131 else
1132 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001133
Guy Benyei11169dd2012-12-18 14:30:41 +00001134 // It doesn't make sense to give a virtual destructor a vtable index,
1135 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001136 // FIXME: Add proper support for debug info for virtual calls in
1137 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1138 // lookup if we have multiple or virtual inheritance.
1139 if (!isa<CXXDestructorDecl>(Method) &&
1140 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001141 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001142 ContainingType = RecordTy;
1143 }
1144
1145 unsigned Flags = 0;
1146 if (Method->isImplicit())
1147 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001148 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1150 if (CXXC->isExplicit())
1151 Flags |= llvm::DIDescriptor::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001152 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001153 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001154 if (CXXC->isExplicit())
1155 Flags |= llvm::DIDescriptor::FlagExplicit;
1156 }
1157 if (Method->hasPrototype())
1158 Flags |= llvm::DIDescriptor::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001159 if (Method->getRefQualifier() == RQ_LValue)
1160 Flags |= llvm::DIDescriptor::FlagLValueReference;
1161 if (Method->getRefQualifier() == RQ_RValue)
1162 Flags |= llvm::DIDescriptor::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001163
1164 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001165 llvm::DISubprogram SP = DBuilder.createMethod(
1166 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1167 MethodTy, /*isLocalToUnit=*/false,
1168 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001169 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001170
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001171 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001172
1173 return SP;
1174}
1175
1176/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001177/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001178/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001179void CGDebugInfo::CollectCXXMemberFunctions(
1180 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001181 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001182
1183 // Since we want more than just the individual member decls if we
1184 // have templated functions iterate over every declaration to gather
1185 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001186 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001187 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1188 // If the member is implicit, don't add it to the member list. This avoids
1189 // the member being added to type units by LLVM, while still allowing it
1190 // to be emitted into the type declaration/reference inside the compile
1191 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001192 // FIXME: Handle Using(Shadow?)Decls here to create
1193 // DW_TAG_imported_declarations inside the class for base decls brought into
1194 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1195 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1196 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001197 if (!Method || Method->isImplicit())
1198 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001199
1200 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1201 continue;
1202
David Blaikiefd580722014-10-06 05:18:55 +00001203 // Reuse the existing member function declaration if it exists.
1204 // It may be associated with the declaration of the type & should be
1205 // reused as we're building the definition.
1206 //
1207 // This situation can arise in the vtable-based debug info reduction where
1208 // implicit members are emitted in a non-vtable TU.
1209 auto MI = SPCache.find(Method->getCanonicalDecl());
1210 EltTys.push_back(MI == SPCache.end()
1211 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001212 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001213 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001214}
Guy Benyei11169dd2012-12-18 14:30:41 +00001215
Guy Benyei11169dd2012-12-18 14:30:41 +00001216/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001217/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001218/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001219void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001220 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001221 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001222
1223 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001224 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001225 unsigned BFlags = 0;
1226 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001227
Guy Benyei11169dd2012-12-18 14:30:41 +00001228 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001229 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001230
Aaron Ballman574705e2014-03-13 15:41:46 +00001231 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001232 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1233 // virtual base offset offset is -ve. The code generator emits dwarf
1234 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001235 BaseOffset = 0 - CGM.getItaniumVTableContext()
1236 .getVirtualBaseOffsetOffset(RD, Base)
1237 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001238 } else {
1239 // In the MS ABI, store the vbtable offset, which is analogous to the
1240 // vbase offset offset in Itanium.
1241 BaseOffset =
1242 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1243 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 BFlags = llvm::DIDescriptor::FlagVirtual;
1245 } else
1246 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1247 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1248 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001249
Adrian Prantl21361fb2014-08-29 22:44:27 +00001250 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001251 llvm::DIType DTy = DBuilder.createInheritance(
1252 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 EltTys.push_back(DTy);
1254 }
1255}
1256
1257/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001258llvm::DIArray
1259CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1260 ArrayRef<TemplateArgument> TAList,
1261 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001262 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1264 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001265 StringRef Name;
1266 if (TPList)
1267 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001268 switch (TA.getKind()) {
1269 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001270 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1271 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001272 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001273 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001274 } break;
1275 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1277 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001278 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001279 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001280 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1281 TemplateParams.push_back(TVP);
1282 } break;
1283 case TemplateArgument::Declaration: {
1284 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001285 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001286 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001287 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001288 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001289 // Variable pointer template parameters have a value that is the address
1290 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001291 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001292 V = CGM.GetAddrOfGlobalVar(VD);
1293 // Member function pointers have special support for building them, though
1294 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001295 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001296 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001297 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001298 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001299 // Member data pointers have special handling too to compute the fixed
1300 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001301 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001302 // These five lines (& possibly the above member function pointer
1303 // handling) might be able to be refactored to use similar code in
1304 // CodeGenModule::getMemberPointerConstant
1305 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1306 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001307 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001308 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001309 }
1310 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001311 DBuilder.createTemplateValueParameter(
1312 TheCU, Name, TTy,
1313 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001314 TemplateParams.push_back(TVP);
1315 } break;
1316 case TemplateArgument::NullPtr: {
1317 QualType T = TA.getNullPtrType();
1318 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001319 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001320 // Special case member data pointer null values since they're actually -1
1321 // instead of zero.
1322 if (const MemberPointerType *MPT =
1323 dyn_cast<MemberPointerType>(T.getTypePtr()))
1324 // But treat member function pointers as simple zero integers because
1325 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1326 // CodeGen grows handling for values of non-null member function
1327 // pointers then perhaps we could remove this special case and rely on
1328 // EmitNullMemberPointer for member function pointers.
1329 if (MPT->isMemberDataPointer())
1330 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1331 if (!V)
1332 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1333 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001334 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1335 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001336 TemplateParams.push_back(TVP);
1337 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001338 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001339 llvm::DITemplateValueParameter
1340 TVP = DBuilder.createTemplateTemplateParameter(
1341 TheCU, Name, llvm::DIType(),
1342 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001343 TemplateParams.push_back(TVP);
1344 } break;
1345 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001346 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1347 TheCU, Name, llvm::DIType(),
1348 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001349 TemplateParams.push_back(TVP);
1350 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001351 case TemplateArgument::Expression: {
1352 const Expr *E = TA.getAsExpr();
1353 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001354 if (E->isGLValue())
1355 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001356 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001357 assert(V && "Expression in template argument isn't constant");
1358 llvm::DIType TTy = getOrCreateType(T, Unit);
1359 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001360 DBuilder.createTemplateValueParameter(
1361 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001362 TemplateParams.push_back(TVP);
1363 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001364 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001365 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001366 case TemplateArgument::Null:
1367 llvm_unreachable(
1368 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 }
1370 }
1371 return DBuilder.getOrCreateArray(TemplateParams);
1372}
1373
1374/// CollectFunctionTemplateParams - A helper function to collect debug
1375/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001376llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1377 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 if (FD->getTemplatedKind() ==
1379 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001380 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1381 ->getTemplate()
1382 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001383 return CollectTemplateParams(
1384 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 }
1386 return llvm::DIArray();
1387}
1388
1389/// CollectCXXTemplateParams - A helper function to collect debug info for
1390/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001391llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1392 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001393 // Always get the full list of parameters, not just the ones from
1394 // the specialization.
1395 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001396 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001397 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001398 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001399}
1400
1401/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1402llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001403 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 return VTablePtrType;
1405
1406 ASTContext &Context = CGM.getContext();
1407
1408 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001409 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001410 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001411 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1412 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001413 llvm::DIType vtbl_ptr_type =
1414 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1416 return VTablePtrType;
1417}
1418
1419/// getVTableName - Get vtable name for the given Class.
1420StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001421 // Copy the gdb compatible name on the side and use its reference.
1422 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001423}
1424
Guy Benyei11169dd2012-12-18 14:30:41 +00001425/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1426/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001427void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001428 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001429 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1430
1431 // If there is a primary base then it will hold vtable info.
1432 if (RL.getPrimaryBase())
1433 return;
1434
1435 // If this class is not dynamic then there is not any vtable info to collect.
1436 if (!RD->isDynamicClass())
1437 return;
1438
1439 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001440 llvm::DIType VPTR = DBuilder.createMemberType(
1441 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1442 llvm::DIDescriptor::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 EltTys.push_back(VPTR);
1444}
1445
Eric Christopherb2a008c2013-05-16 00:45:12 +00001446/// getOrCreateRecordType - Emit record type's standalone debug info.
1447llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001448 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001449 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1451 return T;
1452}
1453
1454/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1455/// debug info.
1456llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001457 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001458 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001459 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001460 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 return T;
1462}
1463
David Blaikie483a9da2014-05-06 18:35:21 +00001464void CGDebugInfo::completeType(const EnumDecl *ED) {
1465 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1466 return;
1467 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001468 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001469 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001470 if (I == TypeCache.end() || !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001471 return;
1472 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001473 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001474 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001475}
1476
David Blaikieb2e86eb2013-08-15 20:49:17 +00001477void CGDebugInfo::completeType(const RecordDecl *RD) {
1478 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1479 !CGM.getLangOpts().CPlusPlus)
1480 completeRequiredType(RD);
1481}
1482
1483void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001484 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1485 return;
1486
David Blaikie6943dea2013-08-20 01:28:15 +00001487 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1488 if (CXXDecl->isDynamicClass())
1489 return;
1490
David Blaikieb2e86eb2013-08-15 20:49:17 +00001491 QualType Ty = CGM.getContext().getRecordType(RD);
1492 llvm::DIType T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001493 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001494 completeClassData(RD);
1495}
1496
1497void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1498 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001499 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001500 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001501 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001502 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001503 if (I != TypeCache.end() && !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001504 return;
1505 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001506 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001507 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001508}
1509
David Blaikie0e716b42014-03-03 23:48:23 +00001510static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1511 CXXRecordDecl::method_iterator End) {
1512 for (; I != End; ++I)
1513 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001514 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1515 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001516 return true;
1517 return false;
1518}
1519
1520static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1521 const RecordDecl *RD,
1522 const LangOptions &LangOpts) {
1523 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1524 return false;
1525
1526 if (!LangOpts.CPlusPlus)
1527 return false;
1528
1529 if (!RD->isCompleteDefinitionRequired())
1530 return true;
1531
1532 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1533
1534 if (!CXXDecl)
1535 return false;
1536
1537 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1538 return true;
1539
1540 TemplateSpecializationKind Spec = TSK_Undeclared;
1541 if (const ClassTemplateSpecializationDecl *SD =
1542 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1543 Spec = SD->getSpecializationKind();
1544
1545 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1546 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1547 CXXDecl->method_end()))
1548 return true;
1549
1550 return false;
1551}
1552
Guy Benyei11169dd2012-12-18 14:30:41 +00001553/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001554llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001555 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001556 llvm::DIType T = cast_or_null<llvm::MDType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001557 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001558 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001559 T = getOrCreateRecordFwdDecl(
1560 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001561 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001562 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001563
David Blaikieb2e86eb2013-08-15 20:49:17 +00001564 return CreateTypeDefinition(Ty);
1565}
1566
1567llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1568 RecordDecl *RD = Ty->getDecl();
1569
Guy Benyei11169dd2012-12-18 14:30:41 +00001570 // Get overall information about the record type for the debug info.
1571 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1572
1573 // Records and classes and unions can all be recursive. To handle them, we
1574 // first generate a debug descriptor for the struct as a forward declaration.
1575 // Then (if it is a definition) we go through and get debug info for all of
1576 // its members. Finally, we create a descriptor for the complete type (which
1577 // may refer to the forward decl if the struct is recursive) and replace all
1578 // uses of the forward declaration with the final definition.
1579
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001580 llvm::DICompositeType FwdDecl =
1581 cast<llvm::MDCompositeTypeBase>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001582
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001583 const RecordDecl *D = RD->getDefinition();
1584 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 return FwdDecl;
1586
David Blaikieadfbf992013-08-18 16:55:33 +00001587 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1588 CollectContainingType(CXXDecl, FwdDecl);
1589
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001591 LexicalBlockStack.emplace_back(&*FwdDecl);
1592 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001593
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001595 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001596 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001597
1598 // Note: The split of CXXDecl information here is intentional, the
1599 // gdb tests will depend on a certain ordering at printout. The debug
1600 // information offsets are still correct if we merge them all together
1601 // though.
1602 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1603 if (CXXDecl) {
1604 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1605 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1606 }
1607
Eric Christopher91a31902013-01-16 01:22:32 +00001608 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001610 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001612
1613 LexicalBlockStack.pop_back();
1614 RegionMap.erase(Ty->getDecl());
1615
1616 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001617 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001618
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001619 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001620 FwdDecl = llvm::MDNode::replaceWithPermanent(
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001621 llvm::TempMDCompositeTypeBase(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001622
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001623 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001624 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001625}
1626
1627/// CreateType - get objective-c object type.
1628llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1629 llvm::DIFile Unit) {
1630 // Ignore protocols.
1631 return getOrCreateType(Ty->getBaseType(), Unit);
1632}
1633
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001634/// \return true if Getter has the default name for the property PD.
1635static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1636 const ObjCMethodDecl *Getter) {
1637 assert(PD);
1638 if (!Getter)
1639 return true;
1640
1641 assert(Getter->getDeclName().isObjCZeroArgSelector());
1642 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001643 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001644}
1645
1646/// \return true if Setter has the default name for the property PD.
1647static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1648 const ObjCMethodDecl *Setter) {
1649 assert(PD);
1650 if (!Setter)
1651 return true;
1652
1653 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001654 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001655 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001656}
1657
Guy Benyei11169dd2012-12-18 14:30:41 +00001658/// CreateType - get objective-c interface type.
1659llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1660 llvm::DIFile Unit) {
1661 ObjCInterfaceDecl *ID = Ty->getDecl();
1662 if (!ID)
1663 return llvm::DIType();
1664
1665 // Get overall information about the record type for the debug info.
1666 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1667 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001668 auto RuntimeLang =
1669 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001670
1671 // If this is just a forward declaration return a special forward-declaration
1672 // debug type since we won't be able to lay out the entire type.
1673 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001674 if (!Def || !Def->getImplementation()) {
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001675 llvm::DIType FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001676 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1677 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001678 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001679 return FwdDecl;
1680 }
1681
David Blaikieef8a9512014-05-05 23:23:53 +00001682 return CreateTypeDefinition(Ty, Unit);
1683}
1684
Eric Christophere7b87e52014-10-26 23:40:33 +00001685llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1686 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001687 ObjCInterfaceDecl *ID = Ty->getDecl();
1688 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1689 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001690 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001691
1692 // Bit size, align and offset of the type.
1693 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1694 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1695
1696 unsigned Flags = 0;
1697 if (ID->getImplementation())
1698 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1699
Eric Christophere7b87e52014-10-26 23:40:33 +00001700 llvm::DICompositeType RealDecl = DBuilder.createStructType(
1701 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1702 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001703
David Blaikieef8a9512014-05-05 23:23:53 +00001704 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001705 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001706
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001707 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001708 LexicalBlockStack.emplace_back(static_cast<llvm::MDNode *>(RealDecl));
1709 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001710
1711 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001712 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001713
1714 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1715 if (SClass) {
1716 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001717 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001718 if (!SClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001719 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001720
Eric Christophere7b87e52014-10-26 23:40:33 +00001721 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 EltTys.push_back(InhTag);
1723 }
1724
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001725 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001726 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001727 SourceLocation Loc = PD->getLocation();
1728 llvm::DIFile PUnit = getOrCreateFile(Loc);
1729 unsigned PLine = getLineNumber(Loc);
1730 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1731 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001732 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1733 PD->getName(), PUnit, PLine,
1734 hasDefaultGetterName(PD, Getter) ? ""
1735 : getSelectorName(PD->getGetterName()),
1736 hasDefaultSetterName(PD, Setter) ? ""
1737 : getSelectorName(PD->getSetterName()),
1738 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 EltTys.push_back(PropertyNode);
1740 }
1741
1742 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1743 unsigned FieldNo = 0;
1744 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1745 Field = Field->getNextIvar(), ++FieldNo) {
1746 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001747 if (!FieldTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001749
Guy Benyei11169dd2012-12-18 14:30:41 +00001750 StringRef FieldName = Field->getName();
1751
1752 // Ignore unnamed fields.
1753 if (FieldName.empty())
1754 continue;
1755
1756 // Get the location for the field.
1757 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1758 unsigned FieldLine = getLineNumber(Field->getLocation());
1759 QualType FType = Field->getType();
1760 uint64_t FieldSize = 0;
1761 unsigned FieldAlign = 0;
1762
1763 if (!FType->isIncompleteArrayType()) {
1764
1765 // Bit size, align and offset of the type.
1766 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001767 ? Field->getBitWidthValue(CGM.getContext())
1768 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 FieldAlign = CGM.getContext().getTypeAlign(FType);
1770 }
1771
1772 uint64_t FieldOffset;
1773 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1774 // We don't know the runtime offset of an ivar if we're using the
1775 // non-fragile ABI. For bitfields, use the bit offset into the first
1776 // byte of storage of the bitfield. For other fields, use zero.
1777 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001778 FieldOffset =
1779 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001780 FieldOffset %= CGM.getContext().getCharWidth();
1781 } else {
1782 FieldOffset = 0;
1783 }
1784 } else {
1785 FieldOffset = RL.getFieldOffset(FieldNo);
1786 }
1787
1788 unsigned Flags = 0;
1789 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1790 Flags = llvm::DIDescriptor::FlagProtected;
1791 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1792 Flags = llvm::DIDescriptor::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001793 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
1794 Flags = llvm::DIDescriptor::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001795
Craig Topper8a13c412014-05-21 05:09:00 +00001796 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001797 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001798 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001799 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001801 SourceLocation Loc = PD->getLocation();
1802 llvm::DIFile PUnit = getOrCreateFile(Loc);
1803 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1805 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001806 PropertyNode = DBuilder.createObjCProperty(
1807 PD->getName(), PUnit, PLine,
1808 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1809 PD->getGetterName()),
1810 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1811 PD->getSetterName()),
1812 PD->getPropertyAttributes(),
1813 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001814 }
1815 }
1816 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001817 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1818 FieldSize, FieldAlign, FieldOffset, Flags,
1819 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001820 EltTys.push_back(FieldTy);
1821 }
1822
1823 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001824 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001825
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001827 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001828}
1829
1830llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1831 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1832 int64_t Count = Ty->getNumElements();
1833 if (Count == 0)
1834 // If number of elements are not known then this is an unbounded array.
1835 // Use Count == -1 to express such arrays.
1836 Count = -1;
1837
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001838 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1840
1841 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1842 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1843
1844 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1845}
1846
Eric Christophere7b87e52014-10-26 23:40:33 +00001847llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 uint64_t Size;
1849 uint64_t Align;
1850
1851 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1852 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1853 Size = 0;
1854 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001855 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 } else if (Ty->isIncompleteArrayType()) {
1857 Size = 0;
1858 if (Ty->getElementType()->isIncompleteType())
1859 Align = 0;
1860 else
1861 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001862 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001863 Size = 0;
1864 Align = 0;
1865 } else {
1866 // Size and align of the whole array, not the element type.
1867 Size = CGM.getContext().getTypeSize(Ty);
1868 Align = CGM.getContext().getTypeAlign(Ty);
1869 }
1870
1871 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1872 // interior arrays, do we care? Why aren't nested arrays represented the
1873 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001874 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 QualType EltTy(Ty, 0);
1876 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1877 // If the number of elements is known, then count is that number. Otherwise,
1878 // it's -1. This allows us to represent a subrange with an array of 0
1879 // elements, like this:
1880 //
1881 // struct foo {
1882 // int x[0];
1883 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001884 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001885 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1886 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001887
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 // FIXME: Verify this is right for VLAs.
1889 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1890 EltTy = Ty->getElementType();
1891 }
1892
1893 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1894
Eric Christophere7b87e52014-10-26 23:40:33 +00001895 llvm::DIType DbgTy = DBuilder.createArrayType(
1896 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001897 return DbgTy;
1898}
1899
Eric Christopherb2a008c2013-05-16 00:45:12 +00001900llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001901 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001902 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1903 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001904}
1905
Eric Christopherb2a008c2013-05-16 00:45:12 +00001906llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001908 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1909 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001910}
1911
Eric Christopherb2a008c2013-05-16 00:45:12 +00001912llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001913 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001914 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1915 if (!Ty->getPointeeType()->isFunctionType())
1916 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001917 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001918 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001919
1920 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001921 Ty->getPointeeType()->getAs<FunctionProtoType>();
1922 return DBuilder.createMemberPointerType(
1923 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1924 Ty->getClass(), FPT->getTypeQuals())),
1925 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001926 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001927}
1928
Eric Christophere7b87e52014-10-26 23:40:33 +00001929llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 // Ignore the atomic wrapping
1931 // FIXME: What is the correct representation?
1932 return getOrCreateType(Ty->getValueType(), U);
1933}
1934
1935/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001936llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001937 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001938 uint64_t Size = 0;
1939 uint64_t Align = 0;
1940 if (!ED->getTypeForDecl()->isIncompleteType()) {
1941 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1942 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1943 }
1944
Manman Rene0064d82013-08-29 23:19:58 +00001945 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1946
Guy Benyei11169dd2012-12-18 14:30:41 +00001947 // If this is just a forward declaration, construct an appropriately
1948 // marked node and just return it.
1949 if (!ED->getDefinition()) {
1950 llvm::DIDescriptor EDContext;
1951 EDContext = getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1952 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1953 unsigned Line = getLineNumber(ED->getLocation());
1954 StringRef EDName = ED->getName();
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001955 llvm::DIType RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001956 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001957 0, Size, Align, llvm::DIDescriptor::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001958 ReplaceMap.emplace_back(
1959 std::piecewise_construct, std::make_tuple(Ty),
1960 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001961 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001962 }
1963
David Blaikie483a9da2014-05-06 18:35:21 +00001964 return CreateTypeDefinition(Ty);
1965}
1966
1967llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1968 const EnumDecl *ED = Ty->getDecl();
1969 uint64_t Size = 0;
1970 uint64_t Align = 0;
1971 if (!ED->getTypeForDecl()->isIncompleteType()) {
1972 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1973 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1974 }
1975
1976 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1977
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001979 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001980 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001981 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001982 Enumerators.push_back(DBuilder.createEnumerator(
1983 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 }
1985
1986 // Return a CompositeType for the enum itself.
1987 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1988
1989 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1990 unsigned Line = getLineNumber(ED->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001991 llvm::DIDescriptor EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001992 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1993 llvm::DIType ClassTy = ED->isFixed()
1994 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1995 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001996 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001997 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1998 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 return DbgTy;
2000}
2001
David Blaikie05491062013-01-21 04:37:12 +00002002static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2003 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002005 Qualifiers InnerQuals = T.getLocalQualifiers();
2006 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2007 // that is already there.
2008 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2009 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002010 QualType LastT = T;
2011 switch (T->getTypeClass()) {
2012 default:
David Blaikie05491062013-01-21 04:37:12 +00002013 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002014 case Type::TemplateSpecialization: {
2015 const auto *Spec = cast<TemplateSpecializationType>(T);
2016 if (Spec->isTypeAlias())
2017 return C.getQualifiedType(T.getTypePtr(), Quals);
2018 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002019 break;
2020 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002021 case Type::TypeOfExpr:
2022 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2023 break;
2024 case Type::TypeOf:
2025 T = cast<TypeOfType>(T)->getUnderlyingType();
2026 break;
2027 case Type::Decltype:
2028 T = cast<DecltypeType>(T)->getUnderlyingType();
2029 break;
2030 case Type::UnaryTransform:
2031 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2032 break;
2033 case Type::Attributed:
2034 T = cast<AttributedType>(T)->getEquivalentType();
2035 break;
2036 case Type::Elaborated:
2037 T = cast<ElaboratedType>(T)->getNamedType();
2038 break;
2039 case Type::Paren:
2040 T = cast<ParenType>(T)->getInnerType();
2041 break;
David Blaikie05491062013-01-21 04:37:12 +00002042 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 break;
2045 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002046 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002047 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002048 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002049 break;
2050 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002051
Guy Benyei11169dd2012-12-18 14:30:41 +00002052 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002053 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 } while (true);
2055}
2056
Eric Christopher0fdcb312013-05-16 00:52:20 +00002057/// getType - Get the type from the cache or return null type if it doesn't
2058/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002059llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2060
2061 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002062 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002063
David Blaikief427b002014-05-06 03:42:01 +00002064 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002065 if (it != TypeCache.end()) {
2066 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002067 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002068 return cast<llvm::MDType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 }
2070
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002071 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002072}
2073
David Blaikie0e716b42014-03-03 23:48:23 +00002074void CGDebugInfo::completeTemplateDefinition(
2075 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002076 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2077 return;
2078
David Blaikie0e716b42014-03-03 23:48:23 +00002079 completeClassData(&SD);
2080 // In case this type has no member function definitions being emitted, ensure
2081 // it is retained
2082 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2083}
2084
Guy Benyei11169dd2012-12-18 14:30:41 +00002085/// getOrCreateType - Get the type from the cache or create a new
2086/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002087llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 if (Ty.isNull())
2089 return llvm::DIType();
2090
2091 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002092 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002093
David Blaikieef8a9512014-05-05 23:23:53 +00002094 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002095 return T;
2096
2097 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002098 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002099 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002100
2101 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002102 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
Guy Benyei11169dd2012-12-18 14:30:41 +00002104 return Res;
2105}
2106
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002107/// Currently the checksum of an interface includes the number of
2108/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002109unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002110 // The assumption is that the number of ivars can only increase
2111 // monotonically, so it is safe to just use their current number as
2112 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002113 unsigned Sum = 0;
2114 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002115 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002116 ++Sum;
2117
2118 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002119}
2120
2121ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2122 switch (Ty->getTypeClass()) {
2123 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002124 return getObjCInterfaceDecl(
2125 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002126 case Type::ObjCInterface:
2127 return cast<ObjCInterfaceType>(Ty)->getDecl();
2128 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002129 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002130 }
2131}
2132
Guy Benyei11169dd2012-12-18 14:30:41 +00002133/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002134llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 // Handle qualifiers, which recursively handles what they refer to.
2136 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002137 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002138
Guy Benyei11169dd2012-12-18 14:30:41 +00002139 // Work out details of type.
2140 switch (Ty->getTypeClass()) {
2141#define TYPE(Class, Base)
2142#define ABSTRACT_TYPE(Class, Base)
2143#define NON_CANONICAL_TYPE(Class, Base)
2144#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2145#include "clang/AST/TypeNodes.def"
2146 llvm_unreachable("Dependent types cannot show up in debug information");
2147
2148 case Type::ExtVector:
2149 case Type::Vector:
2150 return CreateType(cast<VectorType>(Ty), Unit);
2151 case Type::ObjCObjectPointer:
2152 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2153 case Type::ObjCObject:
2154 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2155 case Type::ObjCInterface:
2156 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2157 case Type::Builtin:
2158 return CreateType(cast<BuiltinType>(Ty));
2159 case Type::Complex:
2160 return CreateType(cast<ComplexType>(Ty));
2161 case Type::Pointer:
2162 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002163 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002164 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002165 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002166 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002167 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 case Type::BlockPointer:
2169 return CreateType(cast<BlockPointerType>(Ty), Unit);
2170 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002171 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002172 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002173 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002175 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 case Type::FunctionProto:
2177 case Type::FunctionNoProto:
2178 return CreateType(cast<FunctionType>(Ty), Unit);
2179 case Type::ConstantArray:
2180 case Type::VariableArray:
2181 case Type::IncompleteArray:
2182 return CreateType(cast<ArrayType>(Ty), Unit);
2183
2184 case Type::LValueReference:
2185 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2186 case Type::RValueReference:
2187 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2188
2189 case Type::MemberPointer:
2190 return CreateType(cast<MemberPointerType>(Ty), Unit);
2191
2192 case Type::Atomic:
2193 return CreateType(cast<AtomicType>(Ty), Unit);
2194
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002196 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2197
David Blaikie42edade2014-11-11 20:44:45 +00002198 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002199 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 case Type::Elaborated:
2201 case Type::Paren:
2202 case Type::SubstTemplateTypeParm:
2203 case Type::TypeOfExpr:
2204 case Type::TypeOf:
2205 case Type::Decltype:
2206 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002207 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002208 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002210
David Blaikie42edade2014-11-11 20:44:45 +00002211 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002212}
2213
2214/// getOrCreateLimitedType - Get the type from the cache or create a new
2215/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002216llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002217 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002218 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002219
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002220 auto *T = cast_or_null<llvm::MDCompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002221
2222 // We may have cached a forward decl when we could have created
2223 // a non-forward decl. Go ahead and create a non-forward decl
2224 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002225 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002226 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
2228 // Otherwise create the type.
David Blaikie8d5e1282013-08-20 21:03:29 +00002229 llvm::DICompositeType Res = CreateLimitedType(Ty);
2230
2231 // Propagate members from the declaration to the definition
2232 // CreateType(const RecordType*) will overwrite this with the members in the
2233 // correct order if the full type is needed.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002234 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002235
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002237 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 return Res;
2239}
2240
2241// TODO: Currently used for context chains when limiting debug info.
David Blaikie8d5e1282013-08-20 21:03:29 +00002242llvm::DICompositeType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002243 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002244
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 // Get overall information about the record type for the debug info.
2246 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2247 unsigned Line = getLineNumber(RD->getLocation());
2248 StringRef RDName = getClassName(RD);
2249
Eric Christopher07429ff2013-10-15 21:22:34 +00002250 llvm::DIDescriptor RDContext =
2251 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002252
David Blaikied2785892013-08-18 17:36:19 +00002253 // If we ended up creating the type during the context chain construction,
2254 // just return that.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002255 auto *T = cast_or_null<llvm::MDCompositeTypeBase>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002256 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002257 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002258 return T;
David Blaikied2785892013-08-18 17:36:19 +00002259
Adrian Prantl381e7552014-02-04 21:29:50 +00002260 // If this is just a forward or incomplete declaration, construct an
2261 // appropriately marked node and just return it.
2262 const RecordDecl *D = RD->getDefinition();
2263 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002264 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002265
2266 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2267 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
David Blaikie49ae6a72013-03-26 23:47:35 +00002268 llvm::DICompositeType RealDecl;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002269
Manman Rene0064d82013-08-29 23:19:58 +00002270 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2271
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002272 RealDecl = DBuilder.createReplaceableCompositeType(getTagForRecord(RD),
2273 RDName, RDContext, DefUnit, Line, 0, Size, Align, 0, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002274
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002275 RegionMap[Ty->getDecl()].reset(RealDecl);
2276 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002277
David Blaikieadfbf992013-08-18 16:55:33 +00002278 if (const ClassTemplateSpecializationDecl *TSpecial =
2279 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002280 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2281 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002282 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002283}
2284
David Blaikieadfbf992013-08-18 16:55:33 +00002285void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
2286 llvm::DICompositeType RealDecl) {
2287 // A class's primary base or the class itself contains the vtable.
2288 llvm::DICompositeType ContainingType;
2289 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2290 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002291 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002292 while (1) {
2293 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2294 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2295 if (PBT && !BRL.isPrimaryBaseVirtual())
2296 PBase = PBT;
2297 else
2298 break;
2299 }
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002300 ContainingType = cast<llvm::MDCompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002301 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2302 getOrCreateFile(RD->getLocation())));
2303 } else if (RD->isDynamicClass())
2304 ContainingType = RealDecl;
2305
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002306 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002307}
2308
Guy Benyei11169dd2012-12-18 14:30:41 +00002309/// CreateMemberType - Create new member and increase Offset by FType's size.
2310llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002311 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002312 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2313 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2314 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002315 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2316 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 *Offset += FieldSize;
2318 return Ty;
2319}
2320
Frederic Riss9db79f12014-11-18 03:40:46 +00002321void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD,
2322 llvm::DIFile Unit,
2323 StringRef &Name, StringRef &LinkageName,
2324 llvm::DIDescriptor &FDContext,
2325 llvm::DIArray &TParamsArray,
2326 unsigned &Flags) {
2327 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2328 Name = getFunctionName(FD);
2329 // Use mangled name as linkage name for C/C++ functions.
2330 if (FD->hasPrototype()) {
2331 LinkageName = CGM.getMangledName(GD);
2332 Flags |= llvm::DIDescriptor::FlagPrototyped;
2333 }
2334 // No need to replicate the linkage name if it isn't different from the
2335 // subprogram name, no need to have it at all unless coverage is enabled or
2336 // debug is set to more than just line tables.
2337 if (LinkageName == Name ||
2338 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2339 !CGM.getCodeGenOpts().EmitGcovNotes &&
2340 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2341 LinkageName = StringRef();
2342
2343 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2344 if (const NamespaceDecl *NSDecl =
2345 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2346 FDContext = getOrCreateNameSpace(NSDecl);
2347 else if (const RecordDecl *RDecl =
2348 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2349 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2350 // Collect template parameters.
2351 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2352 }
2353}
2354
2355void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2356 unsigned &LineNo, QualType &T,
2357 StringRef &Name, StringRef &LinkageName,
2358 llvm::DIDescriptor &VDContext) {
2359 Unit = getOrCreateFile(VD->getLocation());
2360 LineNo = getLineNumber(VD->getLocation());
2361
2362 setLocation(VD->getLocation());
2363
2364 T = VD->getType();
2365 if (T->isIncompleteArrayType()) {
2366 // CodeGen turns int[] into int[1] so we'll do the same here.
2367 llvm::APInt ConstVal(32, 1);
2368 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2369
2370 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2371 ArrayType::Normal, 0);
2372 }
2373
2374 Name = VD->getName();
2375 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2376 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2377 LinkageName = CGM.getMangledName(VD);
2378 if (LinkageName == Name)
2379 LinkageName = StringRef();
2380
2381 // Since we emit declarations (DW_AT_members) for static members, place the
2382 // definition of those static members in the namespace they were declared in
2383 // in the source code (the lexical decl context).
2384 // FIXME: Generalize this for even non-member global variables where the
2385 // declaration and definition may have different lexical decl contexts, once
2386 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002387 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2388 : VD->getDeclContext();
2389 // When a record type contains an in-line initialization of a static data
2390 // member, and the record type is marked as __declspec(dllexport), an implicit
2391 // definition of the member will be created in the record context. DWARF
2392 // doesn't seem to have a nice way to describe this in a form that consumers
2393 // are likely to understand, so fake the "normal" situation of a definition
2394 // outside the class by putting it in the global scope.
2395 if (DC->isRecord())
2396 DC = CGM.getContext().getTranslationUnitDecl();
2397 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002398}
2399
Frederic Rissd253ed62014-11-18 03:40:51 +00002400llvm::DISubprogram
2401CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2402 llvm::DIArray TParamsArray;
2403 StringRef Name, LinkageName;
2404 unsigned Flags = 0;
2405 SourceLocation Loc = FD->getLocation();
2406 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002407 llvm::DIDescriptor DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002408 unsigned Line = getLineNumber(Loc);
2409
2410 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2411 TParamsArray, Flags);
2412 // Build function type.
2413 SmallVector<QualType, 16> ArgTypes;
2414 for (const ParmVarDecl *Parm: FD->parameters())
2415 ArgTypes.push_back(Parm->getType());
2416 QualType FnType =
2417 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2418 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002419 llvm::DISubprogram SP = DBuilder.createTempFunctionFwdDecl(
2420 DContext, Name, LinkageName, Unit, Line,
2421 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2422 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2423 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002424 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002425 FwdDeclReplaceMap.emplace_back(
2426 std::piecewise_construct, std::make_tuple(CanonDecl),
2427 std::make_tuple(static_cast<llvm::Metadata *>(SP)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002428 return SP;
2429}
2430
2431llvm::DIGlobalVariable
2432CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2433 QualType T;
2434 StringRef Name, LinkageName;
2435 SourceLocation Loc = VD->getLocation();
2436 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002437 llvm::DIDescriptor DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002438 unsigned Line = getLineNumber(Loc);
2439
2440 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2441 llvm::DIGlobalVariable GV =
2442 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2443 Line, getOrCreateType(T, Unit),
2444 !VD->isExternallyVisible(),
2445 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002446 FwdDeclReplaceMap.emplace_back(
2447 std::piecewise_construct,
2448 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2449 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002450 return GV;
2451}
2452
Frederic Riss442293e2014-11-06 21:12:06 +00002453llvm::DIDescriptor CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002454 // We only need a declaration (not a definition) of the type - so use whatever
2455 // we would otherwise do to get a type for a pointee. (forward declarations in
2456 // limited debug info, full definitions (if the type definition is available)
2457 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002458 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2459 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002460 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002461 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002462
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002463 if (I != DeclCache.end())
2464 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Frederic Rissd253ed62014-11-18 03:40:51 +00002465
2466 // No definition for now. Emit a forward definition that might be
2467 // merged with a potential upcoming definition.
2468 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2469 return getFunctionForwardDeclaration(FD);
2470 else if (const auto *VD = dyn_cast<VarDecl>(D))
2471 return getGlobalVariableForwardDeclaration(VD);
2472
2473 return llvm::DIDescriptor();
David Blaikiebd483762013-05-20 04:58:53 +00002474}
2475
Guy Benyei11169dd2012-12-18 14:30:41 +00002476/// getFunctionDeclaration - Return debug info descriptor to describe method
2477/// declaration for the given method definition.
2478llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002479 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002480 return llvm::DISubprogram();
2481
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002483 if (!FD)
2484 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002485
2486 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002487 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002488
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002489 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002490 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002491 if (const CXXMethodDecl *MD =
2492 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002493 llvm::DICompositeType T = cast<llvm::MDCompositeType>(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002494 llvm::DISubprogram SP =
2495 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002496 return SP;
2497 }
2498 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002499 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002500 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2501 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 return SP;
2503 }
2504
Aaron Ballman86c93902014-03-06 23:45:36 +00002505 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002506 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002508 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2509 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 return SP;
2511 }
2512 }
2513 return llvm::DISubprogram();
2514}
2515
2516// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2517// implicit parameter "this".
David Blaikie469f0792013-05-22 23:22:42 +00002518llvm::DICompositeType CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2519 QualType FnType,
2520 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002521 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002522 // Create fake but valid subroutine type. Otherwise
2523 // llvm::DISubprogram::Verify() would return false, and
2524 // subprogram DIE will miss DW_AT_decl_file and
2525 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002526 return DBuilder.createSubroutineType(F,
2527 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002528
2529 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2530 return getOrCreateMethodType(Method, F);
2531 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2532 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002533 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002534
2535 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002536 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002537
2538 // Replace the instancetype keyword with the actual type.
2539 if (ResultTy == CGM.getContext().getObjCInstanceType())
2540 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002541 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002542
Adrian Prantl7bec9032013-05-10 21:08:31 +00002543 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002545 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2546 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2547 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 // "_cmd" pointer is always second argument.
2549 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2550 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2551 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002552 for (const auto *PI : OMethod->params())
2553 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002554 // Variadic methods need a special marker at the end of the type list.
2555 if (OMethod->isVariadic())
2556 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002557
Manman Ren67f005e2014-07-28 22:24:34 +00002558 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 return DBuilder.createSubroutineType(F, EltTypeArray);
2560 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002561
Adrian Prantl800faef2014-02-25 23:42:18 +00002562 // Handle variadic function types; they need an additional
2563 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002564 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2565 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002566 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002567 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2568 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2569 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2570 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2571 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002572 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002573 return DBuilder.createSubroutineType(F, EltTypeArray);
2574 }
2575
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002576 return cast<llvm::MDCompositeTypeBase>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002577}
2578
2579/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002580void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2581 SourceLocation ScopeLoc, QualType FnType,
2582 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002583
2584 StringRef Name;
2585 StringRef LinkageName;
2586
2587 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2588
2589 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002590 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002591
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 unsigned Flags = 0;
2593 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002594 llvm::DIDescriptor FDContext = Unit;
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 llvm::DIArray TParamsArray;
2596 if (!HasDecl) {
2597 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002598 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002599 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2600 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002601 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002603 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(FI->second);
2604 if (SP && SP->isDefinition()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002605 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002606 LexicalBlockStack.emplace_back(SPN);
2607 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 return;
2609 }
2610 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002611 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2612 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2614 Name = getObjCMethodName(OMD);
2615 Flags |= llvm::DIDescriptor::FlagPrototyped;
2616 } else {
2617 // Use llvm function name.
2618 Name = Fn->getName();
2619 Flags |= llvm::DIDescriptor::FlagPrototyped;
2620 }
2621 if (!Name.empty() && Name[0] == '\01')
2622 Name = Name.substr(1);
2623
Adrian Prantl42d71b92014-04-10 23:21:53 +00002624 if (!HasDecl || D->isImplicit()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 Flags |= llvm::DIDescriptor::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002626 // Artificial functions without a location should not silently reuse CurLoc.
2627 if (Loc.isInvalid())
2628 CurLoc = SourceLocation();
2629 }
2630 unsigned LineNo = getLineNumber(Loc);
2631 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002632
Eric Christopher8018e412014-03-27 18:50:35 +00002633 // FIXME: The function declaration we're constructing here is mostly reusing
2634 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2635 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2636 // all subprograms instead of the actual context since subprogram definitions
2637 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002638 llvm::DISubprogram SP = DBuilder.createFunction(
2639 FDContext, Name, LinkageName, Unit, LineNo,
2640 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2641 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002642 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002643 // We might get here with a VarDecl in the case we're generating
2644 // code for the initialization of globals. Do not record these decls
2645 // as they will overwrite the actual VarDecl Decl in the cache.
2646 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002647 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002648
Adrian Prantlbebb8932014-03-21 21:01:58 +00002649 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002650 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002651 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002652
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002654 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002655}
2656
2657/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002658/// information in the source file. If the location is invalid, the
2659/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002660void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 // Update our current location
2662 setLocation(Loc);
2663
Eric Christophere7b87e52014-10-26 23:40:33 +00002664 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2665 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002666
Adrian Prantle83b1302014-01-07 22:05:52 +00002667 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002668 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002669 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002670}
2671
2672/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2673/// the stack.
2674void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002675 llvm::MDNode *Back = nullptr;
2676 if (!LexicalBlockStack.empty())
2677 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002678 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002679 llvm::DIDescriptor(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
2680 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002681 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002682 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002683}
2684
2685/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2686/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002687void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2688 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002689 // Set our current location.
2690 setLocation(Loc);
2691
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002693 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2694 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002695
2696 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2697 return;
2698
2699 // Create a new lexical block and push it on the stack.
2700 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002701}
2702
2703/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2704/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002705void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2706 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2708
2709 // Provide an entry in the line table for the end of the block.
2710 EmitLocation(Builder, Loc);
2711
David Blaikie60a877b2014-10-22 19:34:33 +00002712 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2713 return;
2714
Guy Benyei11169dd2012-12-18 14:30:41 +00002715 LexicalBlockStack.pop_back();
2716}
2717
2718/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2719void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2720 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2721 unsigned RCount = FnBeginRegionCount.back();
2722 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2723
2724 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002725 while (LexicalBlockStack.size() != RCount) {
2726 // Provide an entry in the line table for the end of the block.
2727 EmitLocation(Builder, CurLoc);
2728 LexicalBlockStack.pop_back();
2729 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 FnBeginRegionCount.pop_back();
2731}
2732
Eric Christopherb2a008c2013-05-16 00:45:12 +00002733// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002734// See BuildByRefType.
2735llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2736 uint64_t *XOffset) {
2737
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002738 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 QualType FType;
2740 uint64_t FieldSize, FieldOffset;
2741 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002742
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002744 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002745
2746 FieldOffset = 0;
2747 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2748 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2749 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2750 FType = CGM.getContext().IntTy;
2751 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2752 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2753
2754 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2755 if (HasCopyAndDispose) {
2756 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002757 EltTys.push_back(
2758 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2759 EltTys.push_back(
2760 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 }
2762 bool HasByrefExtendedLayout;
2763 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002764 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2765 HasByrefExtendedLayout) &&
2766 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002767 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002768 EltTys.push_back(
2769 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002770 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002771
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2773 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002774 CGM.getTarget().getPointerAlign(0))) {
2775 CharUnits FieldOffsetInBytes =
2776 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2777 CharUnits AlignedOffsetInBytes =
2778 FieldOffsetInBytes.RoundUpToAlignment(Align);
2779 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002780
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 if (NumPaddingBytes.isPositive()) {
2782 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2783 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2784 pad, ArrayType::Normal, 0);
2785 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2786 }
2787 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002788
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002790 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 FieldSize = CGM.getContext().getTypeSize(FType);
2792 FieldAlign = CGM.getContext().toBits(Align);
2793
Eric Christopherb2a008c2013-05-16 00:45:12 +00002794 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002795 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2796 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002797 EltTys.push_back(FieldTy);
2798 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002799
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002801
Guy Benyei11169dd2012-12-18 14:30:41 +00002802 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002803
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002805 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002806}
2807
2808/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002809void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002810 llvm::Value *Storage, unsigned ArgNo,
2811 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002812 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2814
David Blaikie7fceebf2013-08-19 03:37:48 +00002815 bool Unwritten =
2816 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2817 cast<Decl>(VD->getDeclContext())->isImplicit());
2818 llvm::DIFile Unit;
2819 if (!Unwritten)
2820 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 llvm::DIType Ty;
2822 uint64_t XOffset = 0;
2823 if (VD->hasAttr<BlocksAttr>())
2824 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002825 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 Ty = getOrCreateType(VD->getType(), Unit);
2827
2828 // If there is no debug info for this type then do not emit debug info
2829 // for this variable.
2830 if (!Ty)
2831 return;
2832
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002834 unsigned Line = 0;
2835 unsigned Column = 0;
2836 if (!Unwritten) {
2837 Line = getLineNumber(VD->getLocation());
2838 Column = getColumnNumber(VD->getLocation());
2839 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002840 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002841 unsigned Flags = 0;
2842 if (VD->isImplicit())
2843 Flags |= llvm::DIDescriptor::FlagArtificial;
2844 // If this is the first argument and it is implicit then
2845 // give it an object pointer flag.
2846 // FIXME: There has to be a better way to do this, but for static
2847 // functions there won't be an implicit param at arg1 and
2848 // otherwise it is 'self' or 'this'.
2849 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
2850 Flags |= llvm::DIDescriptor::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002851 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002852 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2853 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002854 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002855
2856 llvm::MDNode *Scope = LexicalBlockStack.back();
2857
2858 StringRef Name = VD->getName();
2859 if (!Name.empty()) {
2860 if (VD->hasAttr<BlocksAttr>()) {
2861 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002862 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 // offset of __forwarding field
2864 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002865 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002866 Expr.push_back(offset.getQuantity());
2867 Expr.push_back(llvm::dwarf::DW_OP_deref);
2868 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 // offset of x field
2870 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002871 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002872
2873 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002874 llvm::DIVariable D = DBuilder.createLocalVariable(
2875 Tag, llvm::DIDescriptor(Scope), VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002876
Guy Benyei11169dd2012-12-18 14:30:41 +00002877 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002878 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2879 llvm::DebugLoc::get(Line, Column, Scope),
2880 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002882 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002883 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002884 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2885 // If VD is an anonymous union then Storage represents value for
2886 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002888 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002889 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002890 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2891 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002892
Guy Benyei11169dd2012-12-18 14:30:41 +00002893 // Ignore unnamed fields. Do not ignore unnamed records.
2894 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2895 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002896
Guy Benyei11169dd2012-12-18 14:30:41 +00002897 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002898 llvm::DIVariable D = DBuilder.createLocalVariable(
2899 Tag, llvm::DIDescriptor(Scope), FieldName, Unit, Line, FieldTy,
2900 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002901
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002903 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2904 llvm::DebugLoc::get(Line, Column, Scope),
2905 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 }
David Blaikie219c7d92013-01-05 20:03:07 +00002907 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 }
2909 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002910
2911 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002912 llvm::DIVariable D = DBuilder.createLocalVariable(
2913 Tag, llvm::DIDescriptor(Scope), Name, Unit, Line, Ty,
2914 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002915
2916 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002917 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2918 llvm::DebugLoc::get(Line, Column, Scope),
2919 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002920}
2921
2922void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2923 llvm::Value *Storage,
2924 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002925 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2927}
2928
Adrian Prantlde17db32013-03-29 19:20:29 +00002929/// Look up the completed type for a self pointer in the TypeCache and
2930/// create a copy of it with the ObjectPointer and Artificial flags
2931/// set. If the type is not cached, a new one is created. This should
2932/// never happen though, since creating a type for the implicit self
2933/// argument implies that we already parsed the interface definition
2934/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002935llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2936 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002937 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002938 if (CachedTy)
2939 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002940 return DBuilder.createObjectPointerType(Ty);
2941}
2942
Eric Christophere7b87e52014-10-26 23:40:33 +00002943void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2944 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002945 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002946 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002948
Craig Topper8a13c412014-05-21 05:09:00 +00002949 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002951
Guy Benyei11169dd2012-12-18 14:30:41 +00002952 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002953
Guy Benyei11169dd2012-12-18 14:30:41 +00002954 uint64_t XOffset = 0;
2955 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2956 llvm::DIType Ty;
2957 if (isByRef)
2958 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002959 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 Ty = getOrCreateType(VD->getType(), Unit);
2961
2962 // Self is passed along as an implicit non-arg variable in a
2963 // block. Mark it as the object pointer.
2964 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002965 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002966
2967 // Get location information.
2968 unsigned Line = getLineNumber(VD->getLocation());
2969 unsigned Column = getColumnNumber(VD->getLocation());
2970
2971 const llvm::DataLayout &target = CGM.getDataLayout();
2972
2973 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002974 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2976
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002977 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002978 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002979 addr.push_back(llvm::dwarf::DW_OP_deref);
2980 addr.push_back(llvm::dwarf::DW_OP_plus);
2981 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002983 addr.push_back(llvm::dwarf::DW_OP_deref);
2984 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002986 offset =
2987 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002988 addr.push_back(offset.getQuantity());
2989 addr.push_back(llvm::dwarf::DW_OP_deref);
2990 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 // offset of x field
2992 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002993 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002994 }
2995
2996 // Create the descriptor for the variable.
2997 llvm::DIVariable D =
Eric Christophere7b87e52014-10-26 23:40:33 +00002998 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_auto_variable,
2999 llvm::DIDescriptor(LexicalBlockStack.back()),
3000 VD->getName(), Unit, Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003001
Guy Benyei11169dd2012-12-18 14:30:41 +00003002 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003003 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3004 if (InsertPoint)
3005 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3006 InsertPoint);
3007 else
3008 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3009 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003010}
3011
3012/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3013/// variable declaration.
3014void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3015 unsigned ArgNo,
3016 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003017 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003018 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3019}
3020
3021namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003022struct BlockLayoutChunk {
3023 uint64_t OffsetInBits;
3024 const BlockDecl::Capture *Capture;
3025};
3026bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3027 return l.OffsetInBits < r.OffsetInBits;
3028}
Guy Benyei11169dd2012-12-18 14:30:41 +00003029}
3030
3031void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003032 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003033 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003034 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003035 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003036 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 ASTContext &C = CGM.getContext();
3038 const BlockDecl *blockDecl = block.getBlockDecl();
3039
3040 // Collect some general information about the block's location.
3041 SourceLocation loc = blockDecl->getCaretLocation();
3042 llvm::DIFile tunit = getOrCreateFile(loc);
3043 unsigned line = getLineNumber(loc);
3044 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003045
Guy Benyei11169dd2012-12-18 14:30:41 +00003046 // Build the debug-info type for the block literal.
3047 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3048
3049 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003050 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003051
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003052 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3054 blockLayout->getElementOffsetInBits(0),
3055 tunit, tunit));
3056 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3057 blockLayout->getElementOffsetInBits(1),
3058 tunit, tunit));
3059 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3060 blockLayout->getElementOffsetInBits(2),
3061 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003062 auto *FnTy = block.getBlockExpr()->getFunctionType();
3063 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3064 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003065 blockLayout->getElementOffsetInBits(3),
3066 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003067 fields.push_back(createFieldType(
3068 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3069 ? C.getBlockDescriptorExtendedType()
3070 : C.getBlockDescriptorType()),
3071 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003072
3073 // We want to sort the captures by offset, not because DWARF
3074 // requires this, but because we're paranoid about debuggers.
3075 SmallVector<BlockLayoutChunk, 8> chunks;
3076
3077 // 'this' capture.
3078 if (blockDecl->capturesCXXThis()) {
3079 BlockLayoutChunk chunk;
3080 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003081 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003082 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 chunks.push_back(chunk);
3084 }
3085
3086 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003087 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003088 const VarDecl *variable = capture.getVariable();
3089 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3090
3091 // Ignore constant captures.
3092 if (captureInfo.isConstant())
3093 continue;
3094
3095 BlockLayoutChunk chunk;
3096 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003097 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 chunk.Capture = &capture;
3099 chunks.push_back(chunk);
3100 }
3101
3102 // Sort by offset.
3103 llvm::array_pod_sort(chunks.begin(), chunks.end());
3104
Eric Christophere7b87e52014-10-26 23:40:33 +00003105 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3106 e = chunks.end();
3107 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 uint64_t offsetInBits = i->OffsetInBits;
3109 const BlockDecl::Capture *capture = i->Capture;
3110
3111 // If we have a null capture, this must be the C++ 'this' capture.
3112 if (!capture) {
3113 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003114 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 QualType type = method->getThisType(C);
3116
3117 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3118 offsetInBits, tunit, tunit));
3119 continue;
3120 }
3121
3122 const VarDecl *variable = capture->getVariable();
3123 StringRef name = variable->getName();
3124
3125 llvm::DIType fieldType;
3126 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003127 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003128
3129 // FIXME: this creates a second copy of this type!
3130 uint64_t xoffset;
3131 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003132 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3133 fieldType =
3134 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3135 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003136 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003137 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3138 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 }
3140 fields.push_back(fieldType);
3141 }
3142
3143 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003144 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3145 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003146
3147 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3148
3149 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003150 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3151 CGM.getContext().toBits(block.BlockSize),
3152 CGM.getContext().toBits(block.BlockAlign), 0,
3153 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3155
3156 // Get overall information about the block.
3157 unsigned flags = llvm::DIDescriptor::FlagArtificial;
3158 llvm::MDNode *scope = LexicalBlockStack.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00003159
3160 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003161 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
3162 llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope),
3163 Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags,
3164 ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003165
Adrian Prantl616bef42013-03-14 21:52:59 +00003166 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003167 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003168 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003169 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003170 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003171 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003172
Adrian Prantl616bef42013-03-14 21:52:59 +00003173 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003174 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3175 llvm::DebugLoc::get(line, column, scope),
3176 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003177}
3178
David Blaikie6943dea2013-08-20 01:28:15 +00003179/// If D is an out-of-class definition of a static data member of a class, find
3180/// its corresponding in-class declaration.
3181llvm::DIDerivedType
3182CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3183 if (!D->isStaticDataMember())
3184 return llvm::DIDerivedType();
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003185
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003186 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003187 if (MI != StaticDataMemberCache.end()) {
3188 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003189 return cast<llvm::MDDerivedTypeBase>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003190 }
David Blaikiece763042013-08-20 21:49:21 +00003191
3192 // If the member wasn't found in the cache, lazily construct and add it to the
3193 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003194 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003195 llvm::DICompositeType Ctxt =
3196 cast<llvm::MDCompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003197 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003198}
3199
Eric Christophercab9fae2014-04-10 05:20:00 +00003200/// Recursively collect all of the member fields of a global anonymous decl and
3201/// create static variables for them. The first time this is called it needs
3202/// to be on a union and then from there we can have additional unnamed fields.
3203llvm::DIGlobalVariable
3204CGDebugInfo::CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile Unit,
3205 unsigned LineNo, StringRef LinkageName,
3206 llvm::GlobalVariable *Var,
3207 llvm::DIDescriptor DContext) {
3208 llvm::DIGlobalVariable GV;
3209
3210 for (const auto *Field : RD->fields()) {
3211 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3212 StringRef FieldName = Field->getName();
3213
3214 // Ignore unnamed fields, but recurse into anonymous records.
3215 if (FieldName.empty()) {
3216 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3217 if (RT)
3218 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3219 Var, DContext);
3220 continue;
3221 }
3222 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003223 GV = DBuilder.createGlobalVariable(
3224 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3225 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003226 }
3227 return GV;
3228}
3229
Guy Benyei11169dd2012-12-18 14:30:41 +00003230/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003231void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003233 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003235 llvm::DIFile Unit;
3236 llvm::DIDescriptor DContext;
3237 unsigned LineNo;
3238 StringRef DeclName, LinkageName;
3239 QualType T;
3240 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003241
3242 // Attempt to store one global variable for the declaration - even if we
3243 // emit a lot of fields.
3244 llvm::DIGlobalVariable GV;
3245
3246 // If this is an anonymous union then we'll want to emit a global
3247 // variable for each member of the anonymous union so that it's possible
3248 // to find the name of any field in the union.
3249 if (T->isUnionType() && DeclName.empty()) {
3250 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003251 assert(RD->isAnonymousStructOrUnion() &&
3252 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003253 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3254 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003255 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003256 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3257 Var->hasInternalLinkage(), Var,
3258 getOrCreateStaticDataMemberDeclarationOrNull(D));
3259 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003260 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003261}
3262
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003263/// EmitGlobalVariable - Emit global variable's debug info.
3264void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3265 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003266 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003267 // Create the descriptor for the variable.
3268 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3269 StringRef Name = VD->getName();
3270 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3271 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3272 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3273 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3274 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3275 }
3276 // Do not use DIGlobalVariable for enums.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003277 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003278 return;
David Blaikiea15565562014-04-04 20:56:17 +00003279 // Do not emit separate definitions for function local const/statics.
3280 if (isa<FunctionDecl>(VD->getDeclContext()))
3281 return;
David Blaikiebb113912014-04-05 07:23:17 +00003282 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003283 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003284 if (VarD->isStaticDataMember()) {
3285 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3286 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003287 // Ensure that the type is retained even though it's otherwise unreferenced.
3288 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003289 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003290 return;
3291 }
3292
David Blaikieaf080852014-11-21 00:20:58 +00003293 llvm::DIDescriptor DContext =
3294 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3295
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003296 auto &GV = DeclCache[VD];
3297 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003298 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003299 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003300 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003301 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003302}
3303
3304llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3305 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003306 return cast<llvm::MDScope>(LexicalBlockStack.back());
David Blaikiebd483762013-05-20 04:58:53 +00003307 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003308}
3309
David Blaikie9f88fe82013-04-22 06:13:21 +00003310void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003311 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3312 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003313 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003314 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3315 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003316 getLineNumber(UD.getLocation()));
3317}
3318
David Blaikiebd483762013-05-20 04:58:53 +00003319void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3320 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3321 return;
3322 assert(UD.shadow_size() &&
3323 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3324 // Emitting one decl is sufficient - debuggers can detect that this is an
3325 // overloaded name & provide lookup for all the overloads.
3326 const UsingShadowDecl &USD = **UD.shadow_begin();
Frederic Riss442293e2014-11-06 21:12:06 +00003327 if (llvm::DIDescriptor Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003328 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003329 DBuilder.createImportedDeclaration(
3330 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3331 getLineNumber(USD.getLocation()));
3332}
3333
David Blaikief121b932013-05-20 22:50:41 +00003334llvm::DIImportedEntity
3335CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3336 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003337 return llvm::DIImportedEntity();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003338 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003339 if (VH)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003340 return cast<llvm::MDImportedEntity>(VH);
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003341 llvm::DIImportedEntity R;
David Blaikief121b932013-05-20 22:50:41 +00003342 if (const NamespaceAliasDecl *Underlying =
3343 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3344 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003345 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003346 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3347 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3348 NA.getName());
3349 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003350 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003351 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3352 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3353 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003354 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003355 return R;
3356}
3357
Guy Benyei11169dd2012-12-18 14:30:41 +00003358/// getOrCreateNamesSpace - Return namespace descriptor for the given
3359/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003360llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003361CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003362 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003363 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003365 return cast<llvm::MDNamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003366
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3368 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003369 llvm::DIDescriptor Context =
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
3371 llvm::DINameSpace NS =
3372 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003373 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003374 return NS;
3375}
3376
3377void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003378 // Creating types might create further types - invalidating the current
3379 // element and the size(), so don't cache/reference them.
3380 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3381 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003382 llvm::MDType *Ty = E.Type->getDecl()->getDefinition()
3383 ? CreateTypeDefinition(E.Type, E.Unit)
3384 : E.Decl;
3385 DBuilder.replaceTemporary(llvm::TempMDType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003386 }
3387
David Blaikief427b002014-05-06 03:42:01 +00003388 for (auto p : ReplaceMap) {
3389 assert(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003390 auto *Ty = cast<llvm::MDType>(p.second);
3391 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003392
David Blaikief427b002014-05-06 03:42:01 +00003393 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003394 assert(it != TypeCache.end());
3395 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003396
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003397 DBuilder.replaceTemporary(llvm::TempMDType(Ty),
3398 cast<llvm::MDType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003399 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003400
Frederic Rissd253ed62014-11-18 03:40:51 +00003401 for (const auto &p : FwdDeclReplaceMap) {
3402 assert(p.second);
3403 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003404 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003405
3406 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003407 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003408 // with ourselves, that will destroy the temporary MDNode and
3409 // replace it with a standard one, avoiding leaking memory.
3410 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003411 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003412 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003413 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003414
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003415 DBuilder.replaceTemporary(llvm::TempMDNode(FwdDecl),
3416 cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003417 }
3418
Adrian Prantl73409ce2013-03-11 18:33:46 +00003419 // We keep our own list of retained types, because we need to look
3420 // up the final type in the type cache.
3421 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3422 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003423 DBuilder.retainType(cast<llvm::MDType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003424
Guy Benyei11169dd2012-12-18 14:30:41 +00003425 DBuilder.finalize();
3426}
David Blaikie66088d52014-09-24 17:01:27 +00003427
3428void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3429 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3430 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003431
3432 if (llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
3433 // Don't ignore in case of explicit cast where it is referenced indirectly.
3434 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003435}