blob: 4af49c26d8e0f070d2c7006b3f32f7b0ab9d3690 [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.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000640llvm::MDCompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000641CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000642 llvm::MDScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000643 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000644 if (llvm::MDType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
645 return cast<llvm::MDCompositeType>(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);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000661 llvm::MDCompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000662 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000663 llvm::DebugNode::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
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000721 unsigned Flags = llvm::DebugNode::FlagAppleBlock;
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 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();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000780 return DBuilder.createTypedef(
781 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
782 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000783}
784
David Blaikie99dab3b2013-09-04 22:03:57 +0000785llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000786 // We don't set size information, but do specify where the typedef was
787 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000788 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000789
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000790 // Typedefs are derived from some other type.
791 return DBuilder.createTypedef(
792 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
793 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
794 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000795}
796
797llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
798 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000799 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000800
801 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000802 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000803
804 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000805 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000806 if (isa<FunctionNoProtoType>(Ty))
807 EltTys.push_back(DBuilder.createUnspecifiedParameter());
808 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000809 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
810 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000811 if (FPT->isVariadic())
812 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000813 }
814
Manman Ren67f005e2014-07-28 22:24:34 +0000815 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 return DBuilder.createSubroutineType(Unit, EltTypeArray);
817}
818
Adrian Prantl21361fb2014-08-29 22:44:27 +0000819/// Convert an AccessSpecifier into the corresponding DIDescriptor flag.
820/// As an optimization, return 0 if the access specifier equals the
821/// default for the containing type.
822static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
823 AccessSpecifier Default = clang::AS_none;
824 if (RD && RD->isClass())
825 Default = clang::AS_private;
826 else if (RD && (RD->isStruct() || RD->isUnion()))
827 Default = clang::AS_public;
828
829 if (Access == Default)
830 return 0;
831
Eric Christophere7b87e52014-10-26 23:40:33 +0000832 switch (Access) {
833 case clang::AS_private:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000834 return llvm::DebugNode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000835 case clang::AS_protected:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000836 return llvm::DebugNode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000837 case clang::AS_public:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000838 return llvm::DebugNode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000839 case clang::AS_none:
840 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000841 }
842 llvm_unreachable("unexpected access enumerator");
843}
Guy Benyei11169dd2012-12-18 14:30:41 +0000844
Eric Christophere7b87e52014-10-26 23:40:33 +0000845llvm::DIType CGDebugInfo::createFieldType(
846 StringRef name, QualType type, uint64_t sizeInBitsOverride,
847 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
848 llvm::DIFile tunit, llvm::DIScope scope, const RecordDecl *RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 llvm::DIType debugType = getOrCreateType(type, tunit);
850
851 // Get the location for the field.
852 llvm::DIFile file = getOrCreateFile(loc);
853 unsigned line = getLineNumber(loc);
854
David Majnemer34b57492014-07-30 01:30:47 +0000855 uint64_t SizeInBits = 0;
856 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000857 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000858 TypeInfo TI = CGM.getContext().getTypeInfo(type);
859 SizeInBits = TI.Width;
860 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000861
862 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000863 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000864 }
865
Adrian Prantl21361fb2014-08-29 22:44:27 +0000866 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000867 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
868 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000869}
870
Eric Christopher91a31902013-01-16 01:22:32 +0000871/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000872void CGDebugInfo::CollectRecordLambdaFields(
873 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
874 llvm::DIType RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000875 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
876 // has the name and the location of the variable so we should iterate over
877 // both concurrently.
878 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
879 RecordDecl::field_iterator Field = CXXDecl->field_begin();
880 unsigned fieldno = 0;
881 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000882 E = CXXDecl->captures_end();
883 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000884 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000885 if (C.capturesVariable()) {
886 VarDecl *V = C.getCapturedVar();
887 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
888 StringRef VName = V->getName();
889 uint64_t SizeInBitsOverride = 0;
890 if (Field->isBitField()) {
891 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
892 assert(SizeInBitsOverride && "found named 0-width bitfield");
893 }
Eric Christophere7b87e52014-10-26 23:40:33 +0000894 llvm::DIType fieldType = createFieldType(
895 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
896 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
897 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000898 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000899 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000900 // TODO: Need to handle 'this' in some way by probably renaming the
901 // this of the lambda class and having a field member of 'this' or
902 // by using AT_object_pointer for the function and having that be
903 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000904 FieldDecl *f = *Field;
905 llvm::DIFile VUnit = getOrCreateFile(f->getLocation());
906 QualType type = f->getType();
Eric Christophere7b87e52014-10-26 23:40:33 +0000907 llvm::DIType fieldType = createFieldType(
908 "this", type, 0, f->getLocation(), f->getAccess(),
909 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000910
911 elements.push_back(fieldType);
912 }
913 }
914}
915
David Blaikie6943dea2013-08-20 01:28:15 +0000916/// Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000917llvm::DIDerivedType CGDebugInfo::CreateRecordStaticField(const VarDecl *Var,
918 llvm::DIType RecordTy,
919 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000920 // Create the descriptor for the static variable, with or without
921 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000922 Var = Var->getCanonicalDecl();
Eric Christopher91a31902013-01-16 01:22:32 +0000923 llvm::DIFile VUnit = getOrCreateFile(Var->getLocation());
924 llvm::DIType VTy = getOrCreateType(Var->getType(), VUnit);
925
Eric Christopher91a31902013-01-16 01:22:32 +0000926 unsigned LineNumber = getLineNumber(Var->getLocation());
927 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000928 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000929 if (Var->getInit()) {
930 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000931 if (Value) {
932 if (Value->isInt())
933 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
934 if (Value->isFloat())
935 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
936 }
Eric Christopher91a31902013-01-16 01:22:32 +0000937 }
938
Adrian Prantl21361fb2014-08-29 22:44:27 +0000939 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
David Blaikieae019462013-08-15 22:50:29 +0000940 llvm::DIDerivedType GV = DBuilder.createStaticMemberType(
941 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000942 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000943 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000944}
945
946/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000947void CGDebugInfo::CollectRecordNormalField(
948 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000949 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000950 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000951 StringRef name = field->getName();
952 QualType type = field->getType();
953
954 // Ignore unnamed fields unless they're anonymous structs/unions.
955 if (name.empty() && !type->isRecordType())
956 return;
957
958 uint64_t SizeInBitsOverride = 0;
959 if (field->isBitField()) {
960 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
961 assert(SizeInBitsOverride && "found named 0-width bitfield");
962 }
963
Eric Christophere7b87e52014-10-26 23:40:33 +0000964 llvm::DIType fieldType =
965 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
966 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000967
968 elements.push_back(fieldType);
969}
970
Guy Benyei11169dd2012-12-18 14:30:41 +0000971/// CollectRecordFields - A helper function to collect debug info for
972/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000973void CGDebugInfo::CollectRecordFields(
974 const RecordDecl *record, llvm::DIFile tunit,
975 SmallVectorImpl<llvm::Metadata *> &elements,
976 llvm::DICompositeType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000977 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
978
Eric Christopher91a31902013-01-16 01:22:32 +0000979 if (CXXDecl && CXXDecl->isLambda())
980 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
981 else {
982 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000983
Eric Christopher91a31902013-01-16 01:22:32 +0000984 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000985 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000986
Eric Christopher91a31902013-01-16 01:22:32 +0000987 // Static and non-static members should appear in the same order as
988 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000989 for (const auto *I : record->decls())
990 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000991 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000992 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000993 if (MI != StaticDataMemberCache.end()) {
994 assert(MI->second &&
995 "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000996 elements.push_back(cast<llvm::MDDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000997 } else {
998 auto Field = CreateRecordStaticField(V, RecordTy, record);
999 elements.push_back(Field);
1000 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001001 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001002 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1003 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001004
1005 // Bump field number for next field.
1006 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001007 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001008 }
1009}
1010
1011/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1012/// function type is not updated to include implicit "this" pointer. Use this
1013/// routine to get a method type which includes "this" pointer.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001014llvm::MDSubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001015CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
1016 llvm::DIFile Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001017 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001018 if (Method->isStatic())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001019 return cast_or_null<llvm::MDSubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001020 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001021 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1022 Func, Unit);
1023}
David Blaikie2aaf0652013-01-07 22:24:59 +00001024
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001025llvm::MDSubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
David Blaikie7eb06852013-01-07 23:06:35 +00001026 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001027 // Add "this" pointer.
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001028 llvm::DITypeArray Args(
1029 cast<llvm::MDSubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
1030 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001031 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001032
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001033 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001034
1035 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001036 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001037
David Blaikie2aaf0652013-01-07 22:24:59 +00001038 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001039 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001040 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1041 // Create pointer type directly in this case.
1042 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1043 QualType PointeeTy = ThisPtrTy->getPointeeType();
1044 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001045 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001046 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1047 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher0fdcb312013-05-16 00:52:20 +00001048 llvm::DIType ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001049 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001050 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001051 // TODO: This and the artificial type below are misleading, the
1052 // types aren't artificial the argument is, but the current
1053 // metadata doesn't represent that.
1054 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1055 Elts.push_back(ThisPtrType);
1056 } else {
1057 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001058 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001059 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1060 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001061 }
1062
1063 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001064 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1065 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001066
Manman Ren67f005e2014-07-28 22:24:34 +00001067 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001068
Adrian Prantl0630eb72013-12-18 21:48:18 +00001069 unsigned Flags = 0;
1070 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001071 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001072 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001073 Flags |= llvm::DebugNode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001074
1075 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076}
1077
Eric Christopherb2a008c2013-05-16 00:45:12 +00001078/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001079/// inside a function.
1080static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1081 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1082 return isFunctionLocalClass(NRD);
1083 if (isa<FunctionDecl>(RD->getDeclContext()))
1084 return true;
1085 return false;
1086}
1087
1088/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
1089/// a single member function GlobalDecl.
1090llvm::DISubprogram
1091CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Eric Christophere7b87e52014-10-26 23:40:33 +00001092 llvm::DIFile Unit, llvm::DIType RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001093 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001094 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001095
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001097 llvm::MDSubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001098
1099 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1100 // make sense to give a single ctor/dtor a linkage name.
1101 StringRef MethodLinkageName;
1102 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1103 MethodLinkageName = CGM.getMangledName(Method);
1104
1105 // Get the location for the method.
David Blaikie7fceebf2013-08-19 03:37:48 +00001106 llvm::DIFile MethodDefUnit;
1107 unsigned MethodLine = 0;
1108 if (!Method->isImplicit()) {
1109 MethodDefUnit = getOrCreateFile(Method->getLocation());
1110 MethodLine = getLineNumber(Method->getLocation());
1111 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001112
1113 // Collect virtual method info.
1114 llvm::DIType ContainingType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001115 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001117
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 if (Method->isVirtual()) {
1119 if (Method->isPure())
1120 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1121 else
1122 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 // It doesn't make sense to give a virtual destructor a vtable index,
1125 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001126 // FIXME: Add proper support for debug info for virtual calls in
1127 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1128 // lookup if we have multiple or virtual inheritance.
1129 if (!isa<CXXDestructorDecl>(Method) &&
1130 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001131 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 ContainingType = RecordTy;
1133 }
1134
1135 unsigned Flags = 0;
1136 if (Method->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001137 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001138 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1140 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001141 Flags |= llvm::DebugNode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001142 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001143 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001145 Flags |= llvm::DebugNode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 }
1147 if (Method->hasPrototype())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001148 Flags |= llvm::DebugNode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001149 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001150 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001151 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001152 Flags |= llvm::DebugNode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001153
1154 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00001155 llvm::DISubprogram SP = DBuilder.createMethod(
1156 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1157 MethodTy, /*isLocalToUnit=*/false,
1158 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001159 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001160
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001161 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001162
1163 return SP;
1164}
1165
1166/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001167/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001168/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001169void CGDebugInfo::CollectCXXMemberFunctions(
1170 const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001171 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001172
1173 // Since we want more than just the individual member decls if we
1174 // have templated functions iterate over every declaration to gather
1175 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001176 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001177 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1178 // If the member is implicit, don't add it to the member list. This avoids
1179 // the member being added to type units by LLVM, while still allowing it
1180 // to be emitted into the type declaration/reference inside the compile
1181 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001182 // FIXME: Handle Using(Shadow?)Decls here to create
1183 // DW_TAG_imported_declarations inside the class for base decls brought into
1184 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1185 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1186 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001187 if (!Method || Method->isImplicit())
1188 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001189
1190 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1191 continue;
1192
David Blaikiefd580722014-10-06 05:18:55 +00001193 // Reuse the existing member function declaration if it exists.
1194 // It may be associated with the declaration of the type & should be
1195 // reused as we're building the definition.
1196 //
1197 // This situation can arise in the vtable-based debug info reduction where
1198 // implicit members are emitted in a non-vtable TU.
1199 auto MI = SPCache.find(Method->getCanonicalDecl());
1200 EltTys.push_back(MI == SPCache.end()
1201 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001202 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001203 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001204}
Guy Benyei11169dd2012-12-18 14:30:41 +00001205
Guy Benyei11169dd2012-12-18 14:30:41 +00001206/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001207/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001208/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001209void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001210 SmallVectorImpl<llvm::Metadata *> &EltTys,
Eric Christophere7b87e52014-10-26 23:40:33 +00001211 llvm::DIType RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001212
1213 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001214 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 unsigned BFlags = 0;
1216 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001217
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001219 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001220
Aaron Ballman574705e2014-03-13 15:41:46 +00001221 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001222 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1223 // virtual base offset offset is -ve. The code generator emits dwarf
1224 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001225 BaseOffset = 0 - CGM.getItaniumVTableContext()
1226 .getVirtualBaseOffsetOffset(RD, Base)
1227 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001228 } else {
1229 // In the MS ABI, store the vbtable offset, which is analogous to the
1230 // vbase offset offset in Itanium.
1231 BaseOffset =
1232 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1233 }
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001234 BFlags = llvm::DebugNode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 } else
1236 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1237 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1238 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001239
Adrian Prantl21361fb2014-08-29 22:44:27 +00001240 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001241 llvm::DIType DTy = DBuilder.createInheritance(
1242 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 EltTys.push_back(DTy);
1244 }
1245}
1246
1247/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001248llvm::DIArray
1249CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1250 ArrayRef<TemplateArgument> TAList,
1251 llvm::DIFile Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001252 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1254 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001255 StringRef Name;
1256 if (TPList)
1257 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001258 switch (TA.getKind()) {
1259 case TemplateArgument::Type: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1261 llvm::DITemplateTypeParameter TTP =
David Blaikie47c11502013-06-22 18:59:18 +00001262 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 TemplateParams.push_back(TTP);
David Blaikie38079fd2013-05-10 21:53:14 +00001264 } break;
1265 case TemplateArgument::Integral: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001266 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
1267 llvm::DITemplateValueParameter TVP =
David Blaikie38079fd2013-05-10 21:53:14 +00001268 DBuilder.createTemplateValueParameter(
David Blaikie47c11502013-06-22 18:59:18 +00001269 TheCU, Name, TTy,
David Blaikie38079fd2013-05-10 21:53:14 +00001270 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()));
1271 TemplateParams.push_back(TVP);
1272 } break;
1273 case TemplateArgument::Declaration: {
1274 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001275 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
David Blaikie38079fd2013-05-10 21:53:14 +00001276 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001277 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001278 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001279 // Variable pointer template parameters have a value that is the address
1280 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001281 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001282 V = CGM.GetAddrOfGlobalVar(VD);
1283 // Member function pointers have special support for building them, though
1284 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001285 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001286 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001287 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001288 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001289 // Member data pointers have special handling too to compute the fixed
1290 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001291 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001292 // These five lines (& possibly the above member function pointer
1293 // handling) might be able to be refactored to use similar code in
1294 // CodeGenModule::getMemberPointerConstant
1295 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1296 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001297 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001298 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001299 }
1300 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001301 DBuilder.createTemplateValueParameter(
1302 TheCU, Name, TTy,
1303 cast_or_null<llvm::Constant>(V->stripPointerCasts()));
David Blaikie38079fd2013-05-10 21:53:14 +00001304 TemplateParams.push_back(TVP);
1305 } break;
1306 case TemplateArgument::NullPtr: {
1307 QualType T = TA.getNullPtrType();
1308 llvm::DIType TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001309 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001310 // Special case member data pointer null values since they're actually -1
1311 // instead of zero.
1312 if (const MemberPointerType *MPT =
1313 dyn_cast<MemberPointerType>(T.getTypePtr()))
1314 // But treat member function pointers as simple zero integers because
1315 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1316 // CodeGen grows handling for values of non-null member function
1317 // pointers then perhaps we could remove this special case and rely on
1318 // EmitNullMemberPointer for member function pointers.
1319 if (MPT->isMemberDataPointer())
1320 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1321 if (!V)
1322 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
1323 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001324 DBuilder.createTemplateValueParameter(TheCU, Name, TTy,
1325 cast<llvm::Constant>(V));
David Blaikie38079fd2013-05-10 21:53:14 +00001326 TemplateParams.push_back(TVP);
1327 } break;
David Blaikie47c11502013-06-22 18:59:18 +00001328 case TemplateArgument::Template: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001329 llvm::DITemplateValueParameter
1330 TVP = DBuilder.createTemplateTemplateParameter(
1331 TheCU, Name, llvm::DIType(),
1332 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString());
David Blaikie47c11502013-06-22 18:59:18 +00001333 TemplateParams.push_back(TVP);
1334 } break;
1335 case TemplateArgument::Pack: {
Eric Christophere7b87e52014-10-26 23:40:33 +00001336 llvm::DITemplateValueParameter TVP = DBuilder.createTemplateParameterPack(
1337 TheCU, Name, llvm::DIType(),
1338 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit));
David Blaikie47c11502013-06-22 18:59:18 +00001339 TemplateParams.push_back(TVP);
1340 } break;
David Majnemer5559d472013-08-24 08:21:10 +00001341 case TemplateArgument::Expression: {
1342 const Expr *E = TA.getAsExpr();
1343 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001344 if (E->isGLValue())
1345 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001346 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001347 assert(V && "Expression in template argument isn't constant");
1348 llvm::DIType TTy = getOrCreateType(T, Unit);
1349 llvm::DITemplateValueParameter TVP =
Duncan P. N. Exon Smith2f68dad2014-11-15 00:24:50 +00001350 DBuilder.createTemplateValueParameter(
1351 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001352 TemplateParams.push_back(TVP);
1353 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001354 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001355 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001356 case TemplateArgument::Null:
1357 llvm_unreachable(
1358 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001359 }
1360 }
1361 return DBuilder.getOrCreateArray(TemplateParams);
1362}
1363
1364/// CollectFunctionTemplateParams - A helper function to collect debug
1365/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001366llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
1367 llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 if (FD->getTemplatedKind() ==
1369 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001370 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1371 ->getTemplate()
1372 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001373 return CollectTemplateParams(
1374 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 }
1376 return llvm::DIArray();
1377}
1378
1379/// CollectCXXTemplateParams - A helper function to collect debug info for
1380/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001381llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
1382 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001383 // Always get the full list of parameters, not just the ones from
1384 // the specialization.
1385 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001386 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001387 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001388 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001389}
1390
1391/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
1392llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001393 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 return VTablePtrType;
1395
1396 ASTContext &Context = CGM.getContext();
1397
1398 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001399 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001400 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
1402 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001403 llvm::DIType vtbl_ptr_type =
1404 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001405 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1406 return VTablePtrType;
1407}
1408
1409/// getVTableName - Get vtable name for the given Class.
1410StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001411 // Copy the gdb compatible name on the side and use its reference.
1412 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001413}
1414
Guy Benyei11169dd2012-12-18 14:30:41 +00001415/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1416/// debug info entry in EltTys vector.
Eric Christophere7b87e52014-10-26 23:40:33 +00001417void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001418 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001419 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1420
1421 // If there is a primary base then it will hold vtable info.
1422 if (RL.getPrimaryBase())
1423 return;
1424
1425 // If this class is not dynamic then there is not any vtable info to collect.
1426 if (!RD->isDynamicClass())
1427 return;
1428
1429 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00001430 llvm::DIType VPTR = DBuilder.createMemberType(
1431 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001432 llvm::DebugNode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001433 EltTys.push_back(VPTR);
1434}
1435
Eric Christopherb2a008c2013-05-16 00:45:12 +00001436/// getOrCreateRecordType - Emit record type's standalone debug info.
1437llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001439 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
1441 return T;
1442}
1443
1444/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1445/// debug info.
1446llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
Eric Christopherc0c5d462013-02-21 22:35:08 +00001447 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001448 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00001449 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001450 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001451 return T;
1452}
1453
David Blaikie483a9da2014-05-06 18:35:21 +00001454void CGDebugInfo::completeType(const EnumDecl *ED) {
1455 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1456 return;
1457 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001458 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001459 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001460 if (I == TypeCache.end() || !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001461 return;
1462 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001463 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001464 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001465}
1466
David Blaikieb2e86eb2013-08-15 20:49:17 +00001467void CGDebugInfo::completeType(const RecordDecl *RD) {
1468 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1469 !CGM.getLangOpts().CPlusPlus)
1470 completeRequiredType(RD);
1471}
1472
1473void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001474 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1475 return;
1476
David Blaikie6943dea2013-08-20 01:28:15 +00001477 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1478 if (CXXDecl->isDynamicClass())
1479 return;
1480
David Blaikieb2e86eb2013-08-15 20:49:17 +00001481 QualType Ty = CGM.getContext().getRecordType(RD);
1482 llvm::DIType T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001483 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001484 completeClassData(RD);
1485}
1486
1487void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1488 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001489 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001490 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001491 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001492 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001493 if (I != TypeCache.end() && !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001494 return;
1495 llvm::DIType Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001496 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001497 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001498}
1499
David Blaikie0e716b42014-03-03 23:48:23 +00001500static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1501 CXXRecordDecl::method_iterator End) {
1502 for (; I != End; ++I)
1503 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001504 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1505 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001506 return true;
1507 return false;
1508}
1509
1510static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1511 const RecordDecl *RD,
1512 const LangOptions &LangOpts) {
1513 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1514 return false;
1515
1516 if (!LangOpts.CPlusPlus)
1517 return false;
1518
1519 if (!RD->isCompleteDefinitionRequired())
1520 return true;
1521
1522 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1523
1524 if (!CXXDecl)
1525 return false;
1526
1527 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1528 return true;
1529
1530 TemplateSpecializationKind Spec = TSK_Undeclared;
1531 if (const ClassTemplateSpecializationDecl *SD =
1532 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1533 Spec = SD->getSpecializationKind();
1534
1535 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1536 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1537 CXXDecl->method_end()))
1538 return true;
1539
1540 return false;
1541}
1542
Guy Benyei11169dd2012-12-18 14:30:41 +00001543/// CreateType - get structure or union type.
David Blaikie99dab3b2013-09-04 22:03:57 +00001544llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001546 llvm::DIType T = cast_or_null<llvm::MDType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001547 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001548 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001549 T = getOrCreateRecordFwdDecl(
1550 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001551 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001552 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001553
David Blaikieb2e86eb2013-08-15 20:49:17 +00001554 return CreateTypeDefinition(Ty);
1555}
1556
1557llvm::DIType CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
1558 RecordDecl *RD = Ty->getDecl();
1559
Guy Benyei11169dd2012-12-18 14:30:41 +00001560 // Get overall information about the record type for the debug info.
1561 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1562
1563 // Records and classes and unions can all be recursive. To handle them, we
1564 // first generate a debug descriptor for the struct as a forward declaration.
1565 // Then (if it is a definition) we go through and get debug info for all of
1566 // its members. Finally, we create a descriptor for the complete type (which
1567 // may refer to the forward decl if the struct is recursive) and replace all
1568 // uses of the forward declaration with the final definition.
1569
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001570 auto *FwdDecl =
1571 cast<llvm::MDCompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001573 const RecordDecl *D = RD->getDefinition();
1574 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 return FwdDecl;
1576
David Blaikieadfbf992013-08-18 16:55:33 +00001577 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1578 CollectContainingType(CXXDecl, FwdDecl);
1579
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001581 LexicalBlockStack.emplace_back(&*FwdDecl);
1582 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001585 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001586 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001587
1588 // Note: The split of CXXDecl information here is intentional, the
1589 // gdb tests will depend on a certain ordering at printout. The debug
1590 // information offsets are still correct if we merge them all together
1591 // though.
1592 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1593 if (CXXDecl) {
1594 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1595 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1596 }
1597
Eric Christopher91a31902013-01-16 01:22:32 +00001598 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001599 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001600 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001602
1603 LexicalBlockStack.pop_back();
1604 RegionMap.erase(Ty->getDecl());
1605
1606 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001607 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001608
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001609 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001610 FwdDecl =
1611 llvm::MDNode::replaceWithPermanent(llvm::TempMDCompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001612
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001613 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001614 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001615}
1616
1617/// CreateType - get objective-c object type.
1618llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1619 llvm::DIFile Unit) {
1620 // Ignore protocols.
1621 return getOrCreateType(Ty->getBaseType(), Unit);
1622}
1623
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001624/// \return true if Getter has the default name for the property PD.
1625static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1626 const ObjCMethodDecl *Getter) {
1627 assert(PD);
1628 if (!Getter)
1629 return true;
1630
1631 assert(Getter->getDeclName().isObjCZeroArgSelector());
1632 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001633 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001634}
1635
1636/// \return true if Setter has the default name for the property PD.
1637static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1638 const ObjCMethodDecl *Setter) {
1639 assert(PD);
1640 if (!Setter)
1641 return true;
1642
1643 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001644 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001645 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001646}
1647
Guy Benyei11169dd2012-12-18 14:30:41 +00001648/// CreateType - get objective-c interface type.
1649llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1650 llvm::DIFile Unit) {
1651 ObjCInterfaceDecl *ID = Ty->getDecl();
1652 if (!ID)
1653 return llvm::DIType();
1654
1655 // Get overall information about the record type for the debug info.
1656 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1657 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001658 auto RuntimeLang =
1659 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001660
1661 // If this is just a forward declaration return a special forward-declaration
1662 // debug type since we won't be able to lay out the entire type.
1663 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001664 if (!Def || !Def->getImplementation()) {
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001665 llvm::DIType FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001666 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1667 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001668 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001669 return FwdDecl;
1670 }
1671
David Blaikieef8a9512014-05-05 23:23:53 +00001672 return CreateTypeDefinition(Ty, Unit);
1673}
1674
Eric Christophere7b87e52014-10-26 23:40:33 +00001675llvm::DIType CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1676 llvm::DIFile Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001677 ObjCInterfaceDecl *ID = Ty->getDecl();
1678 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
1679 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001680 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
1682 // Bit size, align and offset of the type.
1683 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1684 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1685
1686 unsigned Flags = 0;
1687 if (ID->getImplementation())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001688 Flags |= llvm::DebugNode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001689
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001690 llvm::MDCompositeType *RealDecl = DBuilder.createStructType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001691 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, llvm::DIType(),
1692 llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001693
David Blaikieef8a9512014-05-05 23:23:53 +00001694 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001695 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001697 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001698 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001699 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001700
1701 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001702 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001703
1704 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1705 if (SClass) {
1706 llvm::DIType SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001707 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001708 if (!SClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001709 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001710
Eric Christophere7b87e52014-10-26 23:40:33 +00001711 llvm::DIType InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001712 EltTys.push_back(InhTag);
1713 }
1714
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001715 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001716 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 SourceLocation Loc = PD->getLocation();
1718 llvm::DIFile PUnit = getOrCreateFile(Loc);
1719 unsigned PLine = getLineNumber(Loc);
1720 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1721 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001722 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1723 PD->getName(), PUnit, PLine,
1724 hasDefaultGetterName(PD, Getter) ? ""
1725 : getSelectorName(PD->getGetterName()),
1726 hasDefaultSetterName(PD, Setter) ? ""
1727 : getSelectorName(PD->getSetterName()),
1728 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 EltTys.push_back(PropertyNode);
1730 }
1731
1732 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1733 unsigned FieldNo = 0;
1734 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1735 Field = Field->getNextIvar(), ++FieldNo) {
1736 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001737 if (!FieldTy)
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 return llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001739
Guy Benyei11169dd2012-12-18 14:30:41 +00001740 StringRef FieldName = Field->getName();
1741
1742 // Ignore unnamed fields.
1743 if (FieldName.empty())
1744 continue;
1745
1746 // Get the location for the field.
1747 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1748 unsigned FieldLine = getLineNumber(Field->getLocation());
1749 QualType FType = Field->getType();
1750 uint64_t FieldSize = 0;
1751 unsigned FieldAlign = 0;
1752
1753 if (!FType->isIncompleteArrayType()) {
1754
1755 // Bit size, align and offset of the type.
1756 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001757 ? Field->getBitWidthValue(CGM.getContext())
1758 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001759 FieldAlign = CGM.getContext().getTypeAlign(FType);
1760 }
1761
1762 uint64_t FieldOffset;
1763 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1764 // We don't know the runtime offset of an ivar if we're using the
1765 // non-fragile ABI. For bitfields, use the bit offset into the first
1766 // byte of storage of the bitfield. For other fields, use zero.
1767 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001768 FieldOffset =
1769 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 FieldOffset %= CGM.getContext().getCharWidth();
1771 } else {
1772 FieldOffset = 0;
1773 }
1774 } else {
1775 FieldOffset = RL.getFieldOffset(FieldNo);
1776 }
1777
1778 unsigned Flags = 0;
1779 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001780 Flags = llvm::DebugNode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001782 Flags = llvm::DebugNode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001783 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001784 Flags = llvm::DebugNode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001785
Craig Topper8a13c412014-05-21 05:09:00 +00001786 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001787 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001788 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001789 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001791 SourceLocation Loc = PD->getLocation();
1792 llvm::DIFile PUnit = getOrCreateFile(Loc);
1793 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001794 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1795 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001796 PropertyNode = DBuilder.createObjCProperty(
1797 PD->getName(), PUnit, PLine,
1798 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1799 PD->getGetterName()),
1800 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1801 PD->getSetterName()),
1802 PD->getPropertyAttributes(),
1803 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 }
1805 }
1806 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001807 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1808 FieldSize, FieldAlign, FieldOffset, Flags,
1809 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001810 EltTys.push_back(FieldTy);
1811 }
1812
1813 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001814 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001815
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001817 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001818}
1819
1820llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
1821 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1822 int64_t Count = Ty->getNumElements();
1823 if (Count == 0)
1824 // If number of elements are not known then this is an unbounded array.
1825 // Use Count == -1 to express such arrays.
1826 Count = -1;
1827
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001828 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1830
1831 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1832 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1833
1834 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1835}
1836
Eric Christophere7b87e52014-10-26 23:40:33 +00001837llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001838 uint64_t Size;
1839 uint64_t Align;
1840
1841 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1842 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1843 Size = 0;
1844 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001845 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001846 } else if (Ty->isIncompleteArrayType()) {
1847 Size = 0;
1848 if (Ty->getElementType()->isIncompleteType())
1849 Align = 0;
1850 else
1851 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001852 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001853 Size = 0;
1854 Align = 0;
1855 } else {
1856 // Size and align of the whole array, not the element type.
1857 Size = CGM.getContext().getTypeSize(Ty);
1858 Align = CGM.getContext().getTypeAlign(Ty);
1859 }
1860
1861 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1862 // interior arrays, do we care? Why aren't nested arrays represented the
1863 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001864 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 QualType EltTy(Ty, 0);
1866 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1867 // If the number of elements is known, then count is that number. Otherwise,
1868 // it's -1. This allows us to represent a subrange with an array of 0
1869 // elements, like this:
1870 //
1871 // struct foo {
1872 // int x[0];
1873 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001874 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1876 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001877
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 // FIXME: Verify this is right for VLAs.
1879 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1880 EltTy = Ty->getElementType();
1881 }
1882
1883 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1884
Eric Christophere7b87e52014-10-26 23:40:33 +00001885 llvm::DIType DbgTy = DBuilder.createArrayType(
1886 Size, Align, getOrCreateType(EltTy, Unit), SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 return DbgTy;
1888}
1889
Eric Christopherb2a008c2013-05-16 00:45:12 +00001890llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001892 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1893 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001894}
1895
Eric Christopherb2a008c2013-05-16 00:45:12 +00001896llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001897 llvm::DIFile Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1899 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001900}
1901
Eric Christopherb2a008c2013-05-16 00:45:12 +00001902llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 llvm::DIFile U) {
David Blaikie2c705ca2013-01-19 19:20:56 +00001904 llvm::DIType ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
1905 if (!Ty->getPointeeType()->isFunctionType())
1906 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001907 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001908 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001909
1910 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001911 Ty->getPointeeType()->getAs<FunctionProtoType>();
1912 return DBuilder.createMemberPointerType(
1913 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1914 Ty->getClass(), FPT->getTypeQuals())),
1915 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001916 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001917}
1918
Eric Christophere7b87e52014-10-26 23:40:33 +00001919llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // Ignore the atomic wrapping
1921 // FIXME: What is the correct representation?
1922 return getOrCreateType(Ty->getValueType(), U);
1923}
1924
1925/// CreateEnumType - get enumeration type.
Manman Ren501ecf92013-08-28 21:46:36 +00001926llvm::DIType CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001927 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 uint64_t Size = 0;
1929 uint64_t Align = 0;
1930 if (!ED->getTypeForDecl()->isIncompleteType()) {
1931 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1932 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1933 }
1934
Manman Rene0064d82013-08-29 23:19:58 +00001935 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1936
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 // If this is just a forward declaration, construct an appropriately
1938 // marked node and just return it.
1939 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001940 llvm::MDScope *EDContext =
1941 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1943 unsigned Line = getLineNumber(ED->getLocation());
1944 StringRef EDName = ED->getName();
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001945 llvm::DIType RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001946 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001947 0, Size, Align, llvm::DebugNode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001948 ReplaceMap.emplace_back(
1949 std::piecewise_construct, std::make_tuple(Ty),
1950 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001951 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 }
1953
David Blaikie483a9da2014-05-06 18:35:21 +00001954 return CreateTypeDefinition(Ty);
1955}
1956
1957llvm::DIType CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
1958 const EnumDecl *ED = Ty->getDecl();
1959 uint64_t Size = 0;
1960 uint64_t Align = 0;
1961 if (!ED->getTypeForDecl()->isIncompleteType()) {
1962 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1963 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1964 }
1965
1966 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1967
Guy Benyei11169dd2012-12-18 14:30:41 +00001968 // Create DIEnumerator elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001969 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001971 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001972 Enumerators.push_back(DBuilder.createEnumerator(
1973 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 }
1975
1976 // Return a CompositeType for the enum itself.
1977 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1978
1979 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1980 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001981 llvm::MDScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001982 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
1983 llvm::DIType ClassTy = ED->isFixed()
1984 ? getOrCreateType(ED->getIntegerType(), DefUnit)
1985 : llvm::DIType();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001986 llvm::DIType DbgTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001987 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1988 Size, Align, EltArray, ClassTy, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 return DbgTy;
1990}
1991
David Blaikie05491062013-01-21 04:37:12 +00001992static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1993 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001994 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001995 Qualifiers InnerQuals = T.getLocalQualifiers();
1996 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1997 // that is already there.
1998 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1999 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 QualType LastT = T;
2001 switch (T->getTypeClass()) {
2002 default:
David Blaikie05491062013-01-21 04:37:12 +00002003 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002004 case Type::TemplateSpecialization: {
2005 const auto *Spec = cast<TemplateSpecializationType>(T);
2006 if (Spec->isTypeAlias())
2007 return C.getQualifiedType(T.getTypePtr(), Quals);
2008 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002009 break;
2010 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 case Type::TypeOfExpr:
2012 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2013 break;
2014 case Type::TypeOf:
2015 T = cast<TypeOfType>(T)->getUnderlyingType();
2016 break;
2017 case Type::Decltype:
2018 T = cast<DecltypeType>(T)->getUnderlyingType();
2019 break;
2020 case Type::UnaryTransform:
2021 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2022 break;
2023 case Type::Attributed:
2024 T = cast<AttributedType>(T)->getEquivalentType();
2025 break;
2026 case Type::Elaborated:
2027 T = cast<ElaboratedType>(T)->getNamedType();
2028 break;
2029 case Type::Paren:
2030 T = cast<ParenType>(T)->getInnerType();
2031 break;
David Blaikie05491062013-01-21 04:37:12 +00002032 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 break;
2035 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002036 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002037 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002038 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002039 break;
2040 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002041
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002043 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 } while (true);
2045}
2046
Eric Christopher0fdcb312013-05-16 00:52:20 +00002047/// getType - Get the type from the cache or return null type if it doesn't
2048/// exist.
Guy Benyei11169dd2012-12-18 14:30:41 +00002049llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
2050
2051 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002052 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002053
David Blaikief427b002014-05-06 03:42:01 +00002054 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 if (it != TypeCache.end()) {
2056 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002057 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002058 return cast<llvm::MDType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 }
2060
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002061 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002062}
2063
David Blaikie0e716b42014-03-03 23:48:23 +00002064void CGDebugInfo::completeTemplateDefinition(
2065 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002066 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2067 return;
2068
David Blaikie0e716b42014-03-03 23:48:23 +00002069 completeClassData(&SD);
2070 // In case this type has no member function definitions being emitted, ensure
2071 // it is retained
2072 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2073}
2074
Guy Benyei11169dd2012-12-18 14:30:41 +00002075/// getOrCreateType - Get the type from the cache or create a new
2076/// one if necessary.
David Blaikie99dab3b2013-09-04 22:03:57 +00002077llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 if (Ty.isNull())
2079 return llvm::DIType();
2080
2081 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002082 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002083
David Blaikieef8a9512014-05-05 23:23:53 +00002084 if (llvm::DIType T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002085 return T;
2086
2087 // Otherwise create the type.
David Blaikie99dab3b2013-09-04 22:03:57 +00002088 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002089 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002090
2091 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002092 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002093
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 return Res;
2095}
2096
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002097/// Currently the checksum of an interface includes the number of
2098/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002099unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002100 // The assumption is that the number of ivars can only increase
2101 // monotonically, so it is safe to just use their current number as
2102 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002103 unsigned Sum = 0;
2104 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002105 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002106 ++Sum;
2107
2108 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002109}
2110
2111ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2112 switch (Ty->getTypeClass()) {
2113 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002114 return getObjCInterfaceDecl(
2115 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002116 case Type::ObjCInterface:
2117 return cast<ObjCInterfaceType>(Ty)->getDecl();
2118 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002119 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002120 }
2121}
2122
Guy Benyei11169dd2012-12-18 14:30:41 +00002123/// CreateTypeNode - Create a new debug type node.
David Blaikie99dab3b2013-09-04 22:03:57 +00002124llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002125 // Handle qualifiers, which recursively handles what they refer to.
2126 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002127 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002128
Guy Benyei11169dd2012-12-18 14:30:41 +00002129 // Work out details of type.
2130 switch (Ty->getTypeClass()) {
2131#define TYPE(Class, Base)
2132#define ABSTRACT_TYPE(Class, Base)
2133#define NON_CANONICAL_TYPE(Class, Base)
2134#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2135#include "clang/AST/TypeNodes.def"
2136 llvm_unreachable("Dependent types cannot show up in debug information");
2137
2138 case Type::ExtVector:
2139 case Type::Vector:
2140 return CreateType(cast<VectorType>(Ty), Unit);
2141 case Type::ObjCObjectPointer:
2142 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2143 case Type::ObjCObject:
2144 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2145 case Type::ObjCInterface:
2146 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2147 case Type::Builtin:
2148 return CreateType(cast<BuiltinType>(Ty));
2149 case Type::Complex:
2150 return CreateType(cast<ComplexType>(Ty));
2151 case Type::Pointer:
2152 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002153 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002154 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002155 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002156 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002157 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 case Type::BlockPointer:
2159 return CreateType(cast<BlockPointerType>(Ty), Unit);
2160 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002161 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002163 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002165 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002166 case Type::FunctionProto:
2167 case Type::FunctionNoProto:
2168 return CreateType(cast<FunctionType>(Ty), Unit);
2169 case Type::ConstantArray:
2170 case Type::VariableArray:
2171 case Type::IncompleteArray:
2172 return CreateType(cast<ArrayType>(Ty), Unit);
2173
2174 case Type::LValueReference:
2175 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2176 case Type::RValueReference:
2177 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2178
2179 case Type::MemberPointer:
2180 return CreateType(cast<MemberPointerType>(Ty), Unit);
2181
2182 case Type::Atomic:
2183 return CreateType(cast<AtomicType>(Ty), Unit);
2184
Guy Benyei11169dd2012-12-18 14:30:41 +00002185 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002186 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2187
David Blaikie42edade2014-11-11 20:44:45 +00002188 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002189 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002190 case Type::Elaborated:
2191 case Type::Paren:
2192 case Type::SubstTemplateTypeParm:
2193 case Type::TypeOfExpr:
2194 case Type::TypeOf:
2195 case Type::Decltype:
2196 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002197 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002198 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002200
David Blaikie42edade2014-11-11 20:44:45 +00002201 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002202}
2203
2204/// getOrCreateLimitedType - Get the type from the cache or create a new
2205/// limited type if necessary.
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002206llvm::DIType CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Eric Christopherc0c5d462013-02-21 22:35:08 +00002207 llvm::DIFile Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002208 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002209
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002210 auto *T = cast_or_null<llvm::MDCompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002211
2212 // We may have cached a forward decl when we could have created
2213 // a non-forward decl. Go ahead and create a non-forward decl
2214 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002215 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002216 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002217
2218 // Otherwise create the type.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002219 llvm::MDCompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002220
2221 // Propagate members from the declaration to the definition
2222 // CreateType(const RecordType*) will overwrite this with the members in the
2223 // correct order if the full type is needed.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002224 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002225
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002227 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 return Res;
2229}
2230
2231// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002232llvm::MDCompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002233 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002234
Guy Benyei11169dd2012-12-18 14:30:41 +00002235 // Get overall information about the record type for the debug info.
2236 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
2237 unsigned Line = getLineNumber(RD->getLocation());
2238 StringRef RDName = getClassName(RD);
2239
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002240 llvm::MDScope *RDContext =
Eric Christopher07429ff2013-10-15 21:22:34 +00002241 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002242
David Blaikied2785892013-08-18 17:36:19 +00002243 // If we ended up creating the type during the context chain construction,
2244 // just return that.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002245 auto *T = cast_or_null<llvm::MDCompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002246 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002247 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002248 return T;
David Blaikied2785892013-08-18 17:36:19 +00002249
Adrian Prantl381e7552014-02-04 21:29:50 +00002250 // If this is just a forward or incomplete declaration, construct an
2251 // appropriately marked node and just return it.
2252 const RecordDecl *D = RD->getDefinition();
2253 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002254 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002255
2256 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2257 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002258
Manman Rene0064d82013-08-29 23:19:58 +00002259 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2260
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002261 llvm::MDCompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
2262 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2263 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002264
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002265 RegionMap[Ty->getDecl()].reset(RealDecl);
2266 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002267
David Blaikieadfbf992013-08-18 16:55:33 +00002268 if (const ClassTemplateSpecializationDecl *TSpecial =
2269 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002270 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2271 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002272 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002273}
2274
David Blaikieadfbf992013-08-18 16:55:33 +00002275void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002276 llvm::MDCompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002277 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002278 llvm::MDCompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002279 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2280 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002281 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002282 while (1) {
2283 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2284 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2285 if (PBT && !BRL.isPrimaryBaseVirtual())
2286 PBase = PBT;
2287 else
2288 break;
2289 }
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002290 ContainingType = cast<llvm::MDCompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002291 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2292 getOrCreateFile(RD->getLocation())));
2293 } else if (RD->isDynamicClass())
2294 ContainingType = RealDecl;
2295
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002296 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002297}
2298
Guy Benyei11169dd2012-12-18 14:30:41 +00002299/// CreateMemberType - Create new member and increase Offset by FType's size.
2300llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Eric Christophere7b87e52014-10-26 23:40:33 +00002301 StringRef Name, uint64_t *Offset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002302 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
2303 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2304 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Eric Christophere7b87e52014-10-26 23:40:33 +00002305 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2306 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 *Offset += FieldSize;
2308 return Ty;
2309}
2310
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002311void CGDebugInfo::collectFunctionDeclProps(
2312 GlobalDecl GD, llvm::DIFile Unit, StringRef &Name, StringRef &LinkageName,
2313 llvm::MDScope *&FDContext, llvm::DIArray &TParamsArray, unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002314 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2315 Name = getFunctionName(FD);
2316 // Use mangled name as linkage name for C/C++ functions.
2317 if (FD->hasPrototype()) {
2318 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002319 Flags |= llvm::DebugNode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002320 }
2321 // No need to replicate the linkage name if it isn't different from the
2322 // subprogram name, no need to have it at all unless coverage is enabled or
2323 // debug is set to more than just line tables.
2324 if (LinkageName == Name ||
2325 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2326 !CGM.getCodeGenOpts().EmitGcovNotes &&
2327 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2328 LinkageName = StringRef();
2329
2330 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2331 if (const NamespaceDecl *NSDecl =
2332 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2333 FDContext = getOrCreateNameSpace(NSDecl);
2334 else if (const RecordDecl *RDecl =
2335 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2336 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2337 // Collect template parameters.
2338 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2339 }
2340}
2341
2342void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile &Unit,
2343 unsigned &LineNo, QualType &T,
2344 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002345 llvm::MDScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002346 Unit = getOrCreateFile(VD->getLocation());
2347 LineNo = getLineNumber(VD->getLocation());
2348
2349 setLocation(VD->getLocation());
2350
2351 T = VD->getType();
2352 if (T->isIncompleteArrayType()) {
2353 // CodeGen turns int[] into int[1] so we'll do the same here.
2354 llvm::APInt ConstVal(32, 1);
2355 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2356
2357 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2358 ArrayType::Normal, 0);
2359 }
2360
2361 Name = VD->getName();
2362 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2363 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2364 LinkageName = CGM.getMangledName(VD);
2365 if (LinkageName == Name)
2366 LinkageName = StringRef();
2367
2368 // Since we emit declarations (DW_AT_members) for static members, place the
2369 // definition of those static members in the namespace they were declared in
2370 // in the source code (the lexical decl context).
2371 // FIXME: Generalize this for even non-member global variables where the
2372 // declaration and definition may have different lexical decl contexts, once
2373 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002374 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2375 : VD->getDeclContext();
2376 // When a record type contains an in-line initialization of a static data
2377 // member, and the record type is marked as __declspec(dllexport), an implicit
2378 // definition of the member will be created in the record context. DWARF
2379 // doesn't seem to have a nice way to describe this in a form that consumers
2380 // are likely to understand, so fake the "normal" situation of a definition
2381 // outside the class by putting it in the global scope.
2382 if (DC->isRecord())
2383 DC = CGM.getContext().getTranslationUnitDecl();
2384 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002385}
2386
Frederic Rissd253ed62014-11-18 03:40:51 +00002387llvm::DISubprogram
2388CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2389 llvm::DIArray TParamsArray;
2390 StringRef Name, LinkageName;
2391 unsigned Flags = 0;
2392 SourceLocation Loc = FD->getLocation();
2393 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002394 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002395 unsigned Line = getLineNumber(Loc);
2396
2397 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2398 TParamsArray, Flags);
2399 // Build function type.
2400 SmallVector<QualType, 16> ArgTypes;
2401 for (const ParmVarDecl *Parm: FD->parameters())
2402 ArgTypes.push_back(Parm->getType());
2403 QualType FnType =
2404 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2405 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002406 llvm::MDSubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002407 DContext, Name, LinkageName, Unit, Line,
2408 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2409 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2410 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002411 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002412 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2413 std::make_tuple(CanonDecl),
2414 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002415 return SP;
2416}
2417
2418llvm::DIGlobalVariable
2419CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2420 QualType T;
2421 StringRef Name, LinkageName;
2422 SourceLocation Loc = VD->getLocation();
2423 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002424 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002425 unsigned Line = getLineNumber(Loc);
2426
2427 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
2428 llvm::DIGlobalVariable GV =
2429 DBuilder.createTempGlobalVariableFwdDecl(DContext, Name, LinkageName, Unit,
2430 Line, getOrCreateType(T, Unit),
2431 !VD->isExternallyVisible(),
2432 nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002433 FwdDeclReplaceMap.emplace_back(
2434 std::piecewise_construct,
2435 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2436 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002437 return GV;
2438}
2439
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002440llvm::DebugNode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002441 // We only need a declaration (not a definition) of the type - so use whatever
2442 // we would otherwise do to get a type for a pointee. (forward declarations in
2443 // limited debug info, full definitions (if the type definition is available)
2444 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002445 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2446 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002447 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002448 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002449
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002450 if (I != DeclCache.end())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002451 return dyn_cast_or_null<llvm::DebugNode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002452
2453 // No definition for now. Emit a forward definition that might be
2454 // merged with a potential upcoming definition.
2455 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2456 return getFunctionForwardDeclaration(FD);
2457 else if (const auto *VD = dyn_cast<VarDecl>(D))
2458 return getGlobalVariableForwardDeclaration(VD);
2459
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002460 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002461}
2462
Guy Benyei11169dd2012-12-18 14:30:41 +00002463/// getFunctionDeclaration - Return debug info descriptor to describe method
2464/// declaration for the given method definition.
2465llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002466 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002467 return llvm::DISubprogram();
2468
Guy Benyei11169dd2012-12-18 14:30:41 +00002469 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002470 if (!FD)
2471 return llvm::DISubprogram();
Guy Benyei11169dd2012-12-18 14:30:41 +00002472
2473 // Setup context.
David Blaikiefd07c602013-08-09 17:20:05 +00002474 llvm::DIScope S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002475
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002476 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002477 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002478 if (const CXXMethodDecl *MD =
2479 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002480 llvm::DICompositeType T = cast<llvm::MDCompositeType>(S);
Eric Christopherf86c4052013-08-28 23:12:10 +00002481 llvm::DISubprogram SP =
2482 CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), T);
David Blaikiefd07c602013-08-09 17:20:05 +00002483 return SP;
2484 }
2485 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002486 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002487 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2488 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 return SP;
2490 }
2491
Aaron Ballman86c93902014-03-06 23:45:36 +00002492 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002493 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002495 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2496 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 return SP;
2498 }
2499 }
2500 return llvm::DISubprogram();
2501}
2502
2503// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
2504// implicit parameter "this".
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002505llvm::MDSubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2506 QualType FnType,
2507 llvm::DIFile F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002508 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
David Blaikie18cfbc52013-06-22 00:09:36 +00002509 // Create fake but valid subroutine type. Otherwise
2510 // llvm::DISubprogram::Verify() would return false, and
2511 // subprogram DIE will miss DW_AT_decl_file and
2512 // DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002513 return DBuilder.createSubroutineType(F,
2514 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002515
2516 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2517 return getOrCreateMethodType(Method, F);
2518 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2519 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002520 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002521
2522 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002523 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002524
2525 // Replace the instancetype keyword with the actual type.
2526 if (ResultTy == CGM.getContext().getObjCInstanceType())
2527 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002528 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002529
Adrian Prantl7bec9032013-05-10 21:08:31 +00002530 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002532 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
2533 llvm::DIType SelfTy = getOrCreateType(SelfDeclTy, F);
2534 Elts.push_back(CreateSelfType(SelfDeclTy, SelfTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002535 // "_cmd" pointer is always second argument.
2536 llvm::DIType CmdTy = getOrCreateType(OMethod->getCmdDecl()->getType(), F);
2537 Elts.push_back(DBuilder.createArtificialType(CmdTy));
2538 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002539 for (const auto *PI : OMethod->params())
2540 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002541 // Variadic methods need a special marker at the end of the type list.
2542 if (OMethod->isVariadic())
2543 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002544
Manman Ren67f005e2014-07-28 22:24:34 +00002545 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002546 return DBuilder.createSubroutineType(F, EltTypeArray);
2547 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002548
Adrian Prantl800faef2014-02-25 23:42:18 +00002549 // Handle variadic function types; they need an additional
2550 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002551 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2552 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002553 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002554 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2555 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2556 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2557 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2558 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002559 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002560 return DBuilder.createSubroutineType(F, EltTypeArray);
2561 }
2562
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002563 return cast<llvm::MDSubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002564}
2565
2566/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002567void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2568 SourceLocation ScopeLoc, QualType FnType,
2569 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002570
2571 StringRef Name;
2572 StringRef LinkageName;
2573
2574 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2575
2576 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002577 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002578
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 unsigned Flags = 0;
2580 llvm::DIFile Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002581 llvm::MDScope *FDContext = Unit;
Guy Benyei11169dd2012-12-18 14:30:41 +00002582 llvm::DIArray TParamsArray;
2583 if (!HasDecl) {
2584 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002585 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2587 // If there is a DISubprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002588 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002590 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(FI->second);
2591 if (SP && SP->isDefinition()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002593 LexicalBlockStack.emplace_back(SPN);
2594 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 return;
2596 }
2597 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002598 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2599 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2601 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002602 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002603 } else {
2604 // Use llvm function name.
2605 Name = Fn->getName();
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002606 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 }
2608 if (!Name.empty() && Name[0] == '\01')
2609 Name = Name.substr(1);
2610
Adrian Prantl42d71b92014-04-10 23:21:53 +00002611 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002612 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002613 // Artificial functions without a location should not silently reuse CurLoc.
2614 if (Loc.isInvalid())
2615 CurLoc = SourceLocation();
2616 }
2617 unsigned LineNo = getLineNumber(Loc);
2618 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002619
Eric Christopher8018e412014-03-27 18:50:35 +00002620 // FIXME: The function declaration we're constructing here is mostly reusing
2621 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2622 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2623 // all subprograms instead of the actual context since subprogram definitions
2624 // are emitted as CU level entities by the backend.
Eric Christophere7b87e52014-10-26 23:40:33 +00002625 llvm::DISubprogram SP = DBuilder.createFunction(
2626 FDContext, Name, LinkageName, Unit, LineNo,
2627 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2628 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002629 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002630 // We might get here with a VarDecl in the case we're generating
2631 // code for the initialization of globals. Do not record these decls
2632 // as they will overwrite the actual VarDecl Decl in the cache.
2633 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002634 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002635
Adrian Prantlbebb8932014-03-21 21:01:58 +00002636 // Push the function onto the lexical block stack.
Guy Benyei11169dd2012-12-18 14:30:41 +00002637 llvm::MDNode *SPN = SP;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002638 LexicalBlockStack.emplace_back(SPN);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002639
Guy Benyei11169dd2012-12-18 14:30:41 +00002640 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002641 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002642}
2643
2644/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002645/// information in the source file. If the location is invalid, the
2646/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002647void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 // Update our current location
2649 setLocation(Loc);
2650
Eric Christophere7b87e52014-10-26 23:40:33 +00002651 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2652 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002653
Adrian Prantle83b1302014-01-07 22:05:52 +00002654 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002655 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002656 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002657}
2658
2659/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2660/// the stack.
2661void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002662 llvm::MDNode *Back = nullptr;
2663 if (!LexicalBlockStack.empty())
2664 Back = LexicalBlockStack.back().get();
David Blaikief9ea2422014-06-02 16:32:05 +00002665 llvm::DIDescriptor D = DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002666 cast<llvm::MDScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002667 getColumnNumber(CurLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00002668 llvm::MDNode *DN = D;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002669 LexicalBlockStack.emplace_back(DN);
Guy Benyei11169dd2012-12-18 14:30:41 +00002670}
2671
2672/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2673/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002674void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2675 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 // Set our current location.
2677 setLocation(Loc);
2678
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002680 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2681 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002682
2683 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2684 return;
2685
2686 // Create a new lexical block and push it on the stack.
2687 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002688}
2689
2690/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2691/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002692void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2693 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002694 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2695
2696 // Provide an entry in the line table for the end of the block.
2697 EmitLocation(Builder, Loc);
2698
David Blaikie60a877b2014-10-22 19:34:33 +00002699 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2700 return;
2701
Guy Benyei11169dd2012-12-18 14:30:41 +00002702 LexicalBlockStack.pop_back();
2703}
2704
2705/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2706void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2707 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2708 unsigned RCount = FnBeginRegionCount.back();
2709 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2710
2711 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002712 while (LexicalBlockStack.size() != RCount) {
2713 // Provide an entry in the line table for the end of the block.
2714 EmitLocation(Builder, CurLoc);
2715 LexicalBlockStack.pop_back();
2716 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 FnBeginRegionCount.pop_back();
2718}
2719
Eric Christopherb2a008c2013-05-16 00:45:12 +00002720// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002721// See BuildByRefType.
2722llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2723 uint64_t *XOffset) {
2724
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002725 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 QualType FType;
2727 uint64_t FieldSize, FieldOffset;
2728 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002729
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002731 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002732
2733 FieldOffset = 0;
2734 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2735 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2736 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2737 FType = CGM.getContext().IntTy;
2738 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2739 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2740
2741 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2742 if (HasCopyAndDispose) {
2743 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002744 EltTys.push_back(
2745 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2746 EltTys.push_back(
2747 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002748 }
2749 bool HasByrefExtendedLayout;
2750 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002751 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2752 HasByrefExtendedLayout) &&
2753 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002754 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002755 EltTys.push_back(
2756 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002757 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002758
Guy Benyei11169dd2012-12-18 14:30:41 +00002759 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2760 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002761 CGM.getTarget().getPointerAlign(0))) {
2762 CharUnits FieldOffsetInBytes =
2763 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2764 CharUnits AlignedOffsetInBytes =
2765 FieldOffsetInBytes.RoundUpToAlignment(Align);
2766 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002767
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 if (NumPaddingBytes.isPositive()) {
2769 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2770 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2771 pad, ArrayType::Normal, 0);
2772 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2773 }
2774 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002775
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 FType = Type;
David Blaikief427b002014-05-06 03:42:01 +00002777 llvm::DIType FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 FieldSize = CGM.getContext().getTypeSize(FType);
2779 FieldAlign = CGM.getContext().toBits(Align);
2780
Eric Christopherb2a008c2013-05-16 00:45:12 +00002781 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002782 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2783 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 EltTys.push_back(FieldTy);
2785 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002786
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002788
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002789 unsigned Flags = llvm::DebugNode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002790
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
David Blaikie6d4fe152013-02-25 01:07:08 +00002792 llvm::DIType(), Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002793}
2794
2795/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002796void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002797 llvm::Value *Storage, unsigned ArgNo,
2798 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002799 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2801
David Blaikie7fceebf2013-08-19 03:37:48 +00002802 bool Unwritten =
2803 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2804 cast<Decl>(VD->getDeclContext())->isImplicit());
2805 llvm::DIFile Unit;
2806 if (!Unwritten)
2807 Unit = getOrCreateFile(VD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 llvm::DIType Ty;
2809 uint64_t XOffset = 0;
2810 if (VD->hasAttr<BlocksAttr>())
2811 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002812 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 Ty = getOrCreateType(VD->getType(), Unit);
2814
2815 // If there is no debug info for this type then do not emit debug info
2816 // for this variable.
2817 if (!Ty)
2818 return;
2819
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002821 unsigned Line = 0;
2822 unsigned Column = 0;
2823 if (!Unwritten) {
2824 Line = getLineNumber(VD->getLocation());
2825 Column = getColumnNumber(VD->getLocation());
2826 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002827 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 unsigned Flags = 0;
2829 if (VD->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002830 Flags |= llvm::DebugNode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 // If this is the first argument and it is implicit then
2832 // give it an object pointer flag.
2833 // FIXME: There has to be a better way to do this, but for static
2834 // functions there won't be an implicit param at arg1 and
2835 // otherwise it is 'self' or 'this'.
2836 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002837 Flags |= llvm::DebugNode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002838 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002839 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2840 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002841 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002842
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002843 auto *Scope = cast<llvm::MDScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002844
2845 StringRef Name = VD->getName();
2846 if (!Name.empty()) {
2847 if (VD->hasAttr<BlocksAttr>()) {
2848 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002849 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 // offset of __forwarding field
2851 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002852 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002853 Expr.push_back(offset.getQuantity());
2854 Expr.push_back(llvm::dwarf::DW_OP_deref);
2855 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 // offset of x field
2857 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002858 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002859
2860 // Create the descriptor for the variable.
Eric Christophere7b87e52014-10-26 23:40:33 +00002861 llvm::DIVariable D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002862 Tag, Scope, VD->getName(), Unit, Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002863
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002865 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2866 llvm::DebugLoc::get(Line, Column, Scope),
2867 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002869 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002870 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002871 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2872 // If VD is an anonymous union then Storage represents value for
2873 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002874 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002875 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002876 for (const auto *Field : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002877 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
2878 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002879
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 // Ignore unnamed fields. Do not ignore unnamed records.
2881 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2882 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002883
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00002885 llvm::DIVariable D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002886 Tag, Scope, FieldName, Unit, Line, FieldTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00002887 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002888
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002890 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2891 llvm::DebugLoc::get(Line, Column, Scope),
2892 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002893 }
David Blaikie219c7d92013-01-05 20:03:07 +00002894 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 }
2896 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002897
2898 // Create the descriptor for the variable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002899 llvm::DIVariable D =
2900 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2901 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002902
2903 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002904 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2905 llvm::DebugLoc::get(Line, Column, Scope),
2906 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002907}
2908
2909void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2910 llvm::Value *Storage,
2911 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002912 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002913 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2914}
2915
Adrian Prantlde17db32013-03-29 19:20:29 +00002916/// Look up the completed type for a self pointer in the TypeCache and
2917/// create a copy of it with the ObjectPointer and Artificial flags
2918/// set. If the type is not cached, a new one is created. This should
2919/// never happen though, since creating a type for the implicit self
2920/// argument implies that we already parsed the interface definition
2921/// and the ivar declarations in the implementation.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002922llvm::DIType CGDebugInfo::CreateSelfType(const QualType &QualTy,
2923 llvm::DIType Ty) {
Adrian Prantlde17db32013-03-29 19:20:29 +00002924 llvm::DIType CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002925 if (CachedTy)
2926 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002927 return DBuilder.createObjectPointerType(Ty);
2928}
2929
Eric Christophere7b87e52014-10-26 23:40:33 +00002930void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2931 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002932 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002933 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002935
Craig Topper8a13c412014-05-21 05:09:00 +00002936 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002938
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002940
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 uint64_t XOffset = 0;
2942 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2943 llvm::DIType Ty;
2944 if (isByRef)
2945 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002946 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 Ty = getOrCreateType(VD->getType(), Unit);
2948
2949 // Self is passed along as an implicit non-arg variable in a
2950 // block. Mark it as the object pointer.
2951 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002952 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002953
2954 // Get location information.
2955 unsigned Line = getLineNumber(VD->getLocation());
2956 unsigned Column = getColumnNumber(VD->getLocation());
2957
2958 const llvm::DataLayout &target = CGM.getDataLayout();
2959
2960 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002961 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002962 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2963
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002964 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002965 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002966 addr.push_back(llvm::dwarf::DW_OP_deref);
2967 addr.push_back(llvm::dwarf::DW_OP_plus);
2968 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002970 addr.push_back(llvm::dwarf::DW_OP_deref);
2971 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002973 offset =
2974 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002975 addr.push_back(offset.getQuantity());
2976 addr.push_back(llvm::dwarf::DW_OP_deref);
2977 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 // offset of x field
2979 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002980 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 }
2982
2983 // Create the descriptor for the variable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002984 llvm::DIVariable D = DBuilder.createLocalVariable(
2985 llvm::dwarf::DW_TAG_auto_variable,
2986 cast<llvm::MDLocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
2987 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002988
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002990 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2991 if (InsertPoint)
2992 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2993 InsertPoint);
2994 else
2995 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2996 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002997}
2998
2999/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3000/// variable declaration.
3001void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3002 unsigned ArgNo,
3003 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003004 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3006}
3007
3008namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003009struct BlockLayoutChunk {
3010 uint64_t OffsetInBits;
3011 const BlockDecl::Capture *Capture;
3012};
3013bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3014 return l.OffsetInBits < r.OffsetInBits;
3015}
Guy Benyei11169dd2012-12-18 14:30:41 +00003016}
3017
3018void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003019 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003020 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003021 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003023 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 ASTContext &C = CGM.getContext();
3025 const BlockDecl *blockDecl = block.getBlockDecl();
3026
3027 // Collect some general information about the block's location.
3028 SourceLocation loc = blockDecl->getCaretLocation();
3029 llvm::DIFile tunit = getOrCreateFile(loc);
3030 unsigned line = getLineNumber(loc);
3031 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003032
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 // Build the debug-info type for the block literal.
3034 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3035
3036 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003037 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003038
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003039 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3041 blockLayout->getElementOffsetInBits(0),
3042 tunit, tunit));
3043 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3044 blockLayout->getElementOffsetInBits(1),
3045 tunit, tunit));
3046 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3047 blockLayout->getElementOffsetInBits(2),
3048 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003049 auto *FnTy = block.getBlockExpr()->getFunctionType();
3050 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3051 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 blockLayout->getElementOffsetInBits(3),
3053 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003054 fields.push_back(createFieldType(
3055 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3056 ? C.getBlockDescriptorExtendedType()
3057 : C.getBlockDescriptorType()),
3058 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003059
3060 // We want to sort the captures by offset, not because DWARF
3061 // requires this, but because we're paranoid about debuggers.
3062 SmallVector<BlockLayoutChunk, 8> chunks;
3063
3064 // 'this' capture.
3065 if (blockDecl->capturesCXXThis()) {
3066 BlockLayoutChunk chunk;
3067 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003068 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003069 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 chunks.push_back(chunk);
3071 }
3072
3073 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003074 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 const VarDecl *variable = capture.getVariable();
3076 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3077
3078 // Ignore constant captures.
3079 if (captureInfo.isConstant())
3080 continue;
3081
3082 BlockLayoutChunk chunk;
3083 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003084 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003085 chunk.Capture = &capture;
3086 chunks.push_back(chunk);
3087 }
3088
3089 // Sort by offset.
3090 llvm::array_pod_sort(chunks.begin(), chunks.end());
3091
Eric Christophere7b87e52014-10-26 23:40:33 +00003092 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3093 e = chunks.end();
3094 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 uint64_t offsetInBits = i->OffsetInBits;
3096 const BlockDecl::Capture *capture = i->Capture;
3097
3098 // If we have a null capture, this must be the C++ 'this' capture.
3099 if (!capture) {
3100 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003101 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 QualType type = method->getThisType(C);
3103
3104 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3105 offsetInBits, tunit, tunit));
3106 continue;
3107 }
3108
3109 const VarDecl *variable = capture->getVariable();
3110 StringRef name = variable->getName();
3111
3112 llvm::DIType fieldType;
3113 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003114 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003115
3116 // FIXME: this creates a second copy of this type!
3117 uint64_t xoffset;
3118 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003119 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3120 fieldType =
3121 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3122 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003123 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003124 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3125 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003126 }
3127 fields.push_back(fieldType);
3128 }
3129
3130 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003131 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3132 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003133
3134 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3135
3136 llvm::DIType type =
Eric Christophere7b87e52014-10-26 23:40:33 +00003137 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
3138 CGM.getContext().toBits(block.BlockSize),
3139 CGM.getContext().toBits(block.BlockAlign), 0,
3140 llvm::DIType(), fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3142
3143 // Get overall information about the block.
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00003144 unsigned flags = llvm::DebugNode::FlagArtificial;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003145 auto *scope = cast<llvm::MDLocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003146
3147 // Create the descriptor for the parameter.
Eric Christophere7b87e52014-10-26 23:40:33 +00003148 llvm::DIVariable debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003149 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3150 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003151
Adrian Prantl616bef42013-03-14 21:52:59 +00003152 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003153 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003154 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003155 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003156 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003157 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003158
Adrian Prantl616bef42013-03-14 21:52:59 +00003159 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003160 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3161 llvm::DebugLoc::get(line, column, scope),
3162 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003163}
3164
David Blaikie6943dea2013-08-20 01:28:15 +00003165/// If D is an out-of-class definition of a static data member of a class, find
3166/// its corresponding in-class declaration.
3167llvm::DIDerivedType
3168CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3169 if (!D->isStaticDataMember())
3170 return llvm::DIDerivedType();
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003171
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003172 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003173 if (MI != StaticDataMemberCache.end()) {
3174 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003175 return cast<llvm::MDDerivedTypeBase>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003176 }
David Blaikiece763042013-08-20 21:49:21 +00003177
3178 // If the member wasn't found in the cache, lazily construct and add it to the
3179 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003180 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003181 llvm::DICompositeType Ctxt =
3182 cast<llvm::MDCompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003183 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003184}
3185
Eric Christophercab9fae2014-04-10 05:20:00 +00003186/// Recursively collect all of the member fields of a global anonymous decl and
3187/// create static variables for them. The first time this is called it needs
3188/// to be on a union and then from there we can have additional unnamed fields.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003189llvm::DIGlobalVariable CGDebugInfo::CollectAnonRecordDecls(
3190 const RecordDecl *RD, llvm::DIFile Unit, unsigned LineNo,
3191 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::MDScope *DContext) {
Eric Christophercab9fae2014-04-10 05:20:00 +00003192 llvm::DIGlobalVariable GV;
3193
3194 for (const auto *Field : RD->fields()) {
3195 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
3196 StringRef FieldName = Field->getName();
3197
3198 // Ignore unnamed fields, but recurse into anonymous records.
3199 if (FieldName.empty()) {
3200 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3201 if (RT)
3202 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3203 Var, DContext);
3204 continue;
3205 }
3206 // Use VarDecl's Tag, Scope and Line number.
Eric Christophere7b87e52014-10-26 23:40:33 +00003207 GV = DBuilder.createGlobalVariable(
3208 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3209 Var->hasInternalLinkage(), Var, llvm::DIDerivedType());
Eric Christophercab9fae2014-04-10 05:20:00 +00003210 }
3211 return GV;
3212}
3213
Guy Benyei11169dd2012-12-18 14:30:41 +00003214/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003215void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003216 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003217 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 // Create global variable debug descriptor.
Frederic Riss9db79f12014-11-18 03:40:46 +00003219 llvm::DIFile Unit;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003220 llvm::MDScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003221 unsigned LineNo;
3222 StringRef DeclName, LinkageName;
3223 QualType T;
3224 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003225
3226 // Attempt to store one global variable for the declaration - even if we
3227 // emit a lot of fields.
3228 llvm::DIGlobalVariable GV;
3229
3230 // If this is an anonymous union then we'll want to emit a global
3231 // variable for each member of the anonymous union so that it's possible
3232 // to find the name of any field in the union.
3233 if (T->isUnionType() && DeclName.empty()) {
3234 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003235 assert(RD->isAnonymousStructOrUnion() &&
3236 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003237 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3238 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003239 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003240 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3241 Var->hasInternalLinkage(), Var,
3242 getOrCreateStaticDataMemberDeclarationOrNull(D));
3243 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003244 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003245}
3246
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003247/// EmitGlobalVariable - Emit global variable's debug info.
3248void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3249 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003250 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003251 // Create the descriptor for the variable.
3252 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
3253 StringRef Name = VD->getName();
3254 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
3255 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3256 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3257 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3258 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3259 }
3260 // Do not use DIGlobalVariable for enums.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003261 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003262 return;
David Blaikiea15565562014-04-04 20:56:17 +00003263 // Do not emit separate definitions for function local const/statics.
3264 if (isa<FunctionDecl>(VD->getDeclContext()))
3265 return;
David Blaikiebb113912014-04-05 07:23:17 +00003266 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003267 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003268 if (VarD->isStaticDataMember()) {
3269 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3270 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003271 // Ensure that the type is retained even though it's otherwise unreferenced.
3272 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003273 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003274 return;
3275 }
3276
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003277 llvm::MDScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003278 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3279
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003280 auto &GV = DeclCache[VD];
3281 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003282 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003283 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003284 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003285 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003286}
3287
3288llvm::DIScope CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
3289 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003290 return cast<llvm::MDScope>(LexicalBlockStack.back());
David Blaikiebd483762013-05-20 04:58:53 +00003291 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003292}
3293
David Blaikie9f88fe82013-04-22 06:13:21 +00003294void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003295 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3296 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003297 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003298 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3299 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003300 getLineNumber(UD.getLocation()));
3301}
3302
David Blaikiebd483762013-05-20 04:58:53 +00003303void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3304 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3305 return;
3306 assert(UD.shadow_size() &&
3307 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3308 // Emitting one decl is sufficient - debuggers can detect that this is an
3309 // overloaded name & provide lookup for all the overloads.
3310 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003311 if (llvm::DebugNode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003312 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003313 DBuilder.createImportedDeclaration(
3314 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3315 getLineNumber(USD.getLocation()));
3316}
3317
David Blaikief121b932013-05-20 22:50:41 +00003318llvm::DIImportedEntity
3319CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3320 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003321 return llvm::DIImportedEntity();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003322 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003323 if (VH)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003324 return cast<llvm::MDImportedEntity>(VH);
Duncan P. N. Exon Smitha346e032015-02-26 04:44:27 +00003325 llvm::DIImportedEntity R;
David Blaikief121b932013-05-20 22:50:41 +00003326 if (const NamespaceAliasDecl *Underlying =
3327 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3328 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003329 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003330 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3331 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3332 NA.getName());
3333 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003334 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003335 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3336 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3337 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003338 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003339 return R;
3340}
3341
Guy Benyei11169dd2012-12-18 14:30:41 +00003342/// getOrCreateNamesSpace - Return namespace descriptor for the given
3343/// namespace decl.
Eric Christopherb2a008c2013-05-16 00:45:12 +00003344llvm::DINameSpace
Guy Benyei11169dd2012-12-18 14:30:41 +00003345CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003346 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003347 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003348 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003349 return cast<llvm::MDNamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003350
Guy Benyei11169dd2012-12-18 14:30:41 +00003351 unsigned LineNo = getLineNumber(NSDecl->getLocation());
3352 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003353 llvm::MDScope *Context =
3354 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00003355 llvm::DINameSpace NS =
3356 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003357 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003358 return NS;
3359}
3360
3361void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003362 // Creating types might create further types - invalidating the current
3363 // element and the size(), so don't cache/reference them.
3364 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3365 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003366 llvm::MDType *Ty = E.Type->getDecl()->getDefinition()
3367 ? CreateTypeDefinition(E.Type, E.Unit)
3368 : E.Decl;
3369 DBuilder.replaceTemporary(llvm::TempMDType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003370 }
3371
David Blaikief427b002014-05-06 03:42:01 +00003372 for (auto p : ReplaceMap) {
3373 assert(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003374 auto *Ty = cast<llvm::MDType>(p.second);
3375 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003376
David Blaikief427b002014-05-06 03:42:01 +00003377 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003378 assert(it != TypeCache.end());
3379 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003380
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003381 DBuilder.replaceTemporary(llvm::TempMDType(Ty),
3382 cast<llvm::MDType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003383 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003384
Frederic Rissd253ed62014-11-18 03:40:51 +00003385 for (const auto &p : FwdDeclReplaceMap) {
3386 assert(p.second);
3387 llvm::DIDescriptor FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003388 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003389
3390 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003391 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003392 // with ourselves, that will destroy the temporary MDNode and
3393 // replace it with a standard one, avoiding leaking memory.
3394 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003395 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003396 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003397 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003398
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003399 DBuilder.replaceTemporary(llvm::TempMDNode(FwdDecl),
3400 cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003401 }
3402
Adrian Prantl73409ce2013-03-11 18:33:46 +00003403 // We keep our own list of retained types, because we need to look
3404 // up the final type in the type cache.
3405 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3406 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003407 DBuilder.retainType(cast<llvm::MDType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003408
Guy Benyei11169dd2012-12-18 14:30:41 +00003409 DBuilder.finalize();
3410}
David Blaikie66088d52014-09-24 17:01:27 +00003411
3412void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3413 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3414 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003415
3416 if (llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
3417 // Don't ignore in case of explicit cast where it is referenced indirectly.
3418 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003419}