blob: e158d2110f820baaa293dcd31c9773a4bbe8ff24 [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"
Saleem Abdulrasool94cfc602016-04-07 17:49:44 +000026#include "clang/Basic/CodeGenOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000027#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000030#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000031#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000032#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000033#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000035#include "llvm/IR/Constants.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/Intrinsics.h"
40#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000042#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043using namespace clang;
44using namespace clang::CodeGen;
45
46CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000047 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000048 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000049 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000050 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
51 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000052 CreateCompileUnit();
53}
54
55CGDebugInfo::~CGDebugInfo() {
56 assert(LexicalBlockStack.empty() &&
57 "Region stack mismatch, stack not empty!");
58}
59
David Blaikie66e41972015-01-14 07:38:27 +000060ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000061 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000062 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000063 init(TemporaryLocation);
64}
65
Adrian Prantl39428e72015-02-03 18:40:42 +000066ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000067 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000068 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000069 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000070 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000071}
72
73void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000074 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000075 auto *DI = CGF->getDebugInfo();
76 if (!DI) {
77 CGF = nullptr;
78 return;
David Blaikie66e41972015-01-14 07:38:27 +000079 }
David Blaikied7057d92015-08-12 23:49:57 +000080
81 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
82 if (TemporaryLocation.isValid()) {
83 DI->EmitLocation(CGF->Builder, TemporaryLocation);
84 return;
85 }
86
87 if (DefaultToEmpty) {
88 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
89 return;
90 }
91
92 // Construct a location that has a valid scope, but no line info.
93 assert(!DI->LexicalBlockStack.empty());
94 CGF->Builder.SetCurrentDebugLocation(
95 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000096}
97
David Blaikie9b479662015-01-25 01:19:10 +000098ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000099 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000100 init(E->getExprLoc());
101}
102
David Blaikie66e41972015-01-14 07:38:27 +0000103ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000104 : CGF(&CGF) {
105 if (!CGF.getDebugInfo()) {
106 this->CGF = nullptr;
107 return;
David Blaikie66e41972015-01-14 07:38:27 +0000108 }
David Blaikied7057d92015-08-12 23:49:57 +0000109 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
110 if (Loc)
111 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000112}
113
114ApplyDebugLocation::~ApplyDebugLocation() {
115 // Query CGF so the location isn't overwritten when location updates are
116 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000117 if (CGF)
118 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000119}
120
Guy Benyei11169dd2012-12-18 14:30:41 +0000121void CGDebugInfo::setLocation(SourceLocation Loc) {
122 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000123 if (Loc.isInvalid())
124 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
126 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
127
128 // If we've changed files in the middle of a lexical scope go ahead
129 // and create a new lexical scope with file node if it's different
130 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000131 if (LexicalBlockStack.empty())
132 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000133
134 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000135 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000137
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000138 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 return;
140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000141 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000143 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
144 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000145 } else if (isa<llvm::DILexicalBlock>(Scope) ||
146 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000148 LexicalBlockStack.emplace_back(
149 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 }
151}
152
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000153llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000154 llvm::DIScope *Mod = getParentModuleOrNull(D);
155 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
156 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000157}
158
159llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
160 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000161 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000162 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000164 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000166 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000167 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 }
169
170 // Check namespace.
171 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000172 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000173
David Blaikiebfa52742013-04-19 06:56:38 +0000174 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
175 if (!RDecl->isDependentType())
176 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000178 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179}
180
Guy Benyei11169dd2012-12-18 14:30:41 +0000181StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000182 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000184 FunctionTemplateSpecializationInfo *Info =
185 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000186
187 if (!Info && FII && !CGM.getCodeGenOpts().EmitCodeView)
Guy Benyei11169dd2012-12-18 14:30:41 +0000188 return FII->getName();
189
190 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000191 SmallString<128> NS;
192 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000193 PrintingPolicy Policy(CGM.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000194
Reid Kleckner60103382015-12-16 02:04:40 +0000195 if (CGM.getCodeGenOpts().EmitCodeView) {
196 // Print a fully qualified name like MSVC would.
197 Policy.MSVCFormatting = true;
198 FD->printQualifiedName(OS, Policy);
199 } else {
200 // Print the unqualified name with some template arguments. This is what
201 // DWARF-based debuggers expect.
202 FD->printName(OS);
203 // Add any template specialization args.
204 if (Info) {
205 const TemplateArgumentList *TArgs = Info->TemplateArguments;
206 const TemplateArgument *Args = TArgs->data();
207 unsigned NumArgs = TArgs->size();
208 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
209 Policy);
210 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000211 }
212
213 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000214 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000215}
216
217StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
218 SmallString<256> MethodName;
219 llvm::raw_svector_ostream OS(MethodName);
220 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
221 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000222 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000223 dyn_cast<const ObjCImplementationDecl>(DC)) {
224 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000225 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000226 dyn_cast<const ObjCInterfaceDecl>(DC)) {
227 OS << OID->getName();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000228 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
229 if (OC->IsClassExtension()) {
230 OS << OC->getClassInterface()->getName();
231 } else {
232 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
233 << OC->getIdentifier()->getNameStart() << ')';
234 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000235 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000236 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
237 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
238 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000239 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000240 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000242 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000243 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000244 ClassTy.print(OS, PrintingPolicy(LangOptions()));
245 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000246 }
247 OS << ' ' << OMD->getSelector().getAsString() << ']';
248
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000249 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000250}
251
Guy Benyei11169dd2012-12-18 14:30:41 +0000252StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000253 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000254}
255
Eric Christophere7b87e52014-10-26 23:40:33 +0000256StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000257 // quick optimization to avoid having to intern strings that are already
258 // stored reliably elsewhere
259 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000260 return RD->getName();
261
David Blaikie65813a32014-04-02 18:21:09 +0000262 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000263 {
David Blaikie65813a32014-04-02 18:21:09 +0000264 llvm::raw_svector_ostream OS(Name);
265 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
266 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000267 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000268
269 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000270 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000271}
272
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000273llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000274 if (!Loc.isValid())
275 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000276 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
277 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000278
279 SourceManager &SM = CGM.getContext().getSourceManager();
280 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
281
282 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
283 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000284 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
285 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000286
287 // Cache the results.
288 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000289 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000290
291 if (it != DIFileCache.end()) {
292 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000293 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000294 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000295 }
296
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000297 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
298 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000299
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000300 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000301 return F;
302}
303
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000304llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000305 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
306 remapDIPath(TheCU->getDirectory()));
307}
308
309std::string CGDebugInfo::remapDIPath(StringRef Path) const {
310 for (const auto &Entry : DebugPrefixMap)
311 if (Path.startswith(Entry.first))
312 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
313 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000314}
315
Guy Benyei11169dd2012-12-18 14:30:41 +0000316unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
317 if (Loc.isInvalid() && CurLoc.isInvalid())
318 return 0;
319 SourceManager &SM = CGM.getContext().getSourceManager();
320 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000321 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000322}
323
Adrian Prantlc7822422013-03-12 20:43:25 +0000324unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000325 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000326 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 return 0;
328
329 // If the location is invalid then use the current column.
330 if (Loc.isInvalid() && CurLoc.isInvalid())
331 return 0;
332 SourceManager &SM = CGM.getContext().getSourceManager();
333 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000334 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000335}
336
337StringRef CGDebugInfo::getCurrentDirname() {
338 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
339 return CGM.getCodeGenOpts().DebugCompilationDir;
340
341 if (!CWDName.empty())
342 return CWDName;
343 SmallString<256> CWD;
344 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000345 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000346}
347
Guy Benyei11169dd2012-12-18 14:30:41 +0000348void CGDebugInfo::CreateCompileUnit() {
349
David Blaikieaabde052014-05-14 00:29:00 +0000350 // Should we be asking the SourceManager for the main file name, instead of
351 // accepting it as an argument? This just causes the main file name to
352 // mismatch with source locations and create extra lexical scopes or
353 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
354 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
355 // because that's what the SourceManager says)
356
Guy Benyei11169dd2012-12-18 14:30:41 +0000357 // Get absolute path name.
358 SourceManager &SM = CGM.getContext().getSourceManager();
359 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
360 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000361 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000362
363 // The main file name provided via the "-main-file-name" option contains just
364 // the file name itself with no path information. This file name may have had
365 // a relative path, so we look into the actual file entry for the main
366 // file to determine the real absolute path for the file.
367 std::string MainFileDir;
368 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000369 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000370 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000371 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
372 llvm::sys::path::append(MainFileDirSS, MainFileName);
373 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000374 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000375 }
376
Ed Masteda706022014-05-07 12:49:30 +0000377 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000378 const LangOptions &LO = CGM.getLangOpts();
379 if (LO.CPlusPlus) {
380 if (LO.ObjC1)
381 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
382 else
383 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
384 } else if (LO.ObjC1) {
385 LangTag = llvm::dwarf::DW_LANG_ObjC;
386 } else if (LO.C99) {
387 LangTag = llvm::dwarf::DW_LANG_C99;
388 } else {
389 LangTag = llvm::dwarf::DW_LANG_C89;
390 }
391
392 std::string Producer = getClangFullVersion();
393
394 // Figure out which version of the ObjC runtime we have.
395 unsigned RuntimeVers = 0;
396 if (LO.ObjC1)
397 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
398
399 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000401 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000402 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
403 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000404 CGM.getCodeGenOpts().SplitDwarfFile,
Benjamin Kramer8c305922016-02-02 11:06:51 +0000405 DebugKind <= codegenoptions::DebugLineTablesOnly
Adrian Prantl3563c552016-03-31 23:57:45 +0000406 ? llvm::DICompileUnit::LineTablesOnly
407 : llvm::DICompileUnit::FullDebug,
Benjamin Kramer8c305922016-02-02 11:06:51 +0000408 0 /* DWOid */, DebugKind != codegenoptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000409}
410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000411llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000412 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 StringRef BTName;
414 switch (BT->getKind()) {
415#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000416#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000417#include "clang/AST/BuiltinTypes.def"
418 case BuiltinType::Dependent:
419 llvm_unreachable("Unexpected builtin type");
420 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000421 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000423 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000425 if (!ClassTy)
426 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
427 "objc_class", TheCU,
428 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 return ClassTy;
430 case BuiltinType::ObjCId: {
431 // typedef struct objc_class *Class;
432 // typedef struct objc_object {
433 // Class isa;
434 // } *id;
435
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000436 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000437 return ObjTy;
438
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000439 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
441 "objc_class", TheCU,
442 getOrCreateMainFile(), 0);
443
444 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000445
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000446 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000447
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000448 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000449 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000450 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000451
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000452 DBuilder.replaceArrays(
453 ObjTy,
454 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
455 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000456 return ObjTy;
457 }
458 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000459 if (!SelTy)
460 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
461 "objc_selector", TheCU,
462 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 return SelTy;
464 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000465
Alexey Bader954ba212016-04-08 13:40:33 +0000466#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
467 case BuiltinType::Id: \
468 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
469 SingletonId);
470#include "clang/AST/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000471 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 return DBuilder.createBasicType(
473 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
474 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000475 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000477 case BuiltinType::OCLClkEvent:
478 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
479 case BuiltinType::OCLQueue:
480 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
481 case BuiltinType::OCLNDRange:
482 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
483 case BuiltinType::OCLReserveID:
484 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000485
Guy Benyei11169dd2012-12-18 14:30:41 +0000486 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000487 case BuiltinType::Char_U:
488 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
489 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000491 case BuiltinType::SChar:
492 Encoding = llvm::dwarf::DW_ATE_signed_char;
493 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000495 case BuiltinType::Char32:
496 Encoding = llvm::dwarf::DW_ATE_UTF;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::UShort:
499 case BuiltinType::UInt:
500 case BuiltinType::UInt128:
501 case BuiltinType::ULong:
502 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000503 case BuiltinType::ULongLong:
504 Encoding = llvm::dwarf::DW_ATE_unsigned;
505 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 case BuiltinType::Short:
507 case BuiltinType::Int:
508 case BuiltinType::Int128:
509 case BuiltinType::Long:
510 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000511 case BuiltinType::LongLong:
512 Encoding = llvm::dwarf::DW_ATE_signed;
513 break;
514 case BuiltinType::Bool:
515 Encoding = llvm::dwarf::DW_ATE_boolean;
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 case BuiltinType::Half:
518 case BuiltinType::Float:
519 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000520 case BuiltinType::Double:
521 Encoding = llvm::dwarf::DW_ATE_float;
522 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000523 }
524
525 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 case BuiltinType::Long:
527 BTName = "long int";
528 break;
529 case BuiltinType::LongLong:
530 BTName = "long long int";
531 break;
532 case BuiltinType::ULong:
533 BTName = "long unsigned int";
534 break;
535 case BuiltinType::ULongLong:
536 BTName = "long long unsigned int";
537 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000538 default:
539 BTName = BT->getName(CGM.getLangOpts());
540 break;
541 }
542 // Bit size, align and offset of the type.
543 uint64_t Size = CGM.getContext().getTypeSize(BT);
544 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000545 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000546}
547
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000548llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000550 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 if (Ty->isComplexIntegerType())
552 Encoding = llvm::dwarf::DW_ATE_lo_user;
553
554 uint64_t Size = CGM.getContext().getTypeSize(Ty);
555 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000556 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000557}
558
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000559llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
560 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 QualifierCollector Qc;
562 const Type *T = Qc.strip(Ty);
563
564 // Ignore these qualifiers for now.
565 Qc.removeObjCGCAttr();
566 Qc.removeAddressSpace();
567 Qc.removeObjCLifetime();
568
569 // We will create one Derived type for one qualifier and recurse to handle any
570 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000571 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 if (Qc.hasConst()) {
573 Tag = llvm::dwarf::DW_TAG_const_type;
574 Qc.removeConst();
575 } else if (Qc.hasVolatile()) {
576 Tag = llvm::dwarf::DW_TAG_volatile_type;
577 Qc.removeVolatile();
578 } else if (Qc.hasRestrict()) {
579 Tag = llvm::dwarf::DW_TAG_restrict_type;
580 Qc.removeRestrict();
581 } else {
582 assert(Qc.empty() && "Unknown type qualifier for debug info");
583 return getOrCreateType(QualType(T, 0), Unit);
584 }
585
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000586 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000587
588 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
589 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000590 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000591}
592
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000593llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
594 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000595
596 // The frontend treats 'id' as a typedef to an ObjCObjectType,
597 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
598 // debug info, we want to emit 'id' in both cases.
599 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000600 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000601
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000602 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
603 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604}
605
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000606llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
607 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000608 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 Ty->getPointeeType(), Unit);
610}
611
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000612/// \return whether a C++ mangling exists for the type defined by TD.
613static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
614 switch (TheCU->getSourceLanguage()) {
615 case llvm::dwarf::DW_LANG_C_plus_plus:
616 return true;
617 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
618 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
619 default:
620 return false;
621 }
622}
623
Manman Rene0064d82013-08-29 23:19:58 +0000624/// In C++ mode, types have linkage, so we can rely on the ODR and
625/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000626static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
627 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000628 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000629 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000630 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000631
632 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000633 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000634
Manman Rene0064d82013-08-29 23:19:58 +0000635 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
636 if (CGM.getTarget().getCXXABI().isMicrosoft())
637 return FullName;
638
639 // TODO: This is using the RTTI name. Is there a better way to get
640 // a unique string for a type?
641 llvm::raw_svector_ostream Out(FullName);
642 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000643 return FullName;
644}
645
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000646/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000647static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
648 llvm::dwarf::Tag Tag;
649 if (RD->isStruct() || RD->isInterface())
650 Tag = llvm::dwarf::DW_TAG_structure_type;
651 else if (RD->isUnion())
652 Tag = llvm::dwarf::DW_TAG_union_type;
653 else {
654 // FIXME: This could be a struct type giving a default visibility different
655 // than C++ class type, but needs llvm metadata changes first.
656 assert(RD->isClass());
657 Tag = llvm::dwarf::DW_TAG_class_type;
658 }
659 return Tag;
660}
661
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000662llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000663CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000664 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000665 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000666 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
667 return cast<llvm::DICompositeType>(T);
668 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 unsigned Line = getLineNumber(RD->getLocation());
670 StringRef RDName = getClassName(RD);
671
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000672 uint64_t Size = 0;
673 uint64_t Align = 0;
674
675 const RecordDecl *D = RD->getDefinition();
676 if (D && D->isCompleteDefinition()) {
677 Size = CGM.getContext().getTypeSize(Ty);
678 Align = CGM.getContext().getTypeAlign(Ty);
679 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000680
681 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000682 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000683 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000684 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000685 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000686 ReplaceMap.emplace_back(
687 std::piecewise_construct, std::make_tuple(Ty),
688 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000689 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000690}
691
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000693 const Type *Ty,
694 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000696 // Bit size, align and offset of the type.
697 // Size is always the size of a pointer. We can't use getTypeSize here
698 // because that does not return the correct value for references.
699 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000700 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000701 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
702
Keno Fischer87842f32015-11-16 09:04:13 +0000703 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
704 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
705 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
706 Size, Align);
707 else
708 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
709 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000710}
711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000712llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
713 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000714 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000715 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000716 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
717 TheCU, getOrCreateMainFile(), 0);
718 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
719 Cache = DBuilder.createPointerType(Cache, Size);
720 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000721}
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
724 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000725 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000726 QualType FType;
727 uint64_t FieldSize, FieldOffset;
728 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000729 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000730
731 FieldOffset = 0;
732 FType = CGM.getContext().UnsignedLongTy;
733 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
734 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
735
736 Elements = DBuilder.getOrCreateArray(EltTys);
737 EltTys.clear();
738
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000739 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000740 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000741
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000742 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000743 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000744 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745
746 // Bit size, align and offset of the type.
747 uint64_t Size = CGM.getContext().getTypeSize(Ty);
748
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000749 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000750
751 FieldOffset = 0;
752 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
753 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
754 FType = CGM.getContext().IntTy;
755 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
756 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000757 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000758 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
759
760 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000761 FieldSize = CGM.getContext().getTypeSize(Ty);
762 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000763 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000764 FieldSize, FieldAlign, FieldOffset,
765 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000766
767 FieldOffset += FieldSize;
768 Elements = DBuilder.getOrCreateArray(EltTys);
769
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000770 // The __block_literal_generic structs are marked with a special
771 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
772 // the debugger needs to know about. To allow type uniquing, emit
773 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000774 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000775 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000776 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000777
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000778 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000779}
780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
782 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000783 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000784 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000785
786 SmallString<128> NS;
787 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000788 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
789 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000790
791 TemplateSpecializationType::PrintTemplateArgumentList(
792 OS, Ty->getArgs(), Ty->getNumArgs(),
793 CGM.getContext().getPrintingPolicy());
794
Eric Christophere7b87e52014-10-26 23:40:33 +0000795 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
796 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000797
798 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000799 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
800 getLineNumber(Loc),
801 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000802}
803
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000804llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
805 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000806 // We don't set size information, but do specify where the typedef was
807 // declared.
Reid Kleckner00381aa2016-03-24 20:38:43 +0000808 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000809
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000810 // Typedefs are derived from some other type.
811 return DBuilder.createTypedef(
812 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
813 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Reid Kleckner00381aa2016-03-24 20:38:43 +0000814 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000815}
816
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000817llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
818 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000819 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000820
821 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000822 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000823
824 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000825 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000826 if (isa<FunctionNoProtoType>(Ty))
827 EltTys.push_back(DBuilder.createUnspecifiedParameter());
828 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000829 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
830 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000831 if (FPT->isVariadic())
832 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000833 }
834
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000835 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +0000836 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +0000837}
838
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000839/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000840/// As an optimization, return 0 if the access specifier equals the
841/// default for the containing type.
842static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
843 AccessSpecifier Default = clang::AS_none;
844 if (RD && RD->isClass())
845 Default = clang::AS_private;
846 else if (RD && (RD->isStruct() || RD->isUnion()))
847 Default = clang::AS_public;
848
849 if (Access == Default)
850 return 0;
851
Eric Christophere7b87e52014-10-26 23:40:33 +0000852 switch (Access) {
853 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000854 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000855 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000856 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000857 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000858 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000859 case clang::AS_none:
860 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000861 }
862 llvm_unreachable("unexpected access enumerator");
863}
Guy Benyei11169dd2012-12-18 14:30:41 +0000864
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000865llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000866 StringRef name, QualType type, uint64_t sizeInBitsOverride,
867 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000868 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
869 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000870
871 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000872 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000873 unsigned line = getLineNumber(loc);
874
David Majnemer34b57492014-07-30 01:30:47 +0000875 uint64_t SizeInBits = 0;
876 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000877 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000878 TypeInfo TI = CGM.getContext().getTypeInfo(type);
879 SizeInBits = TI.Width;
880 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000881
882 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000883 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000884 }
885
Adrian Prantl21361fb2014-08-29 22:44:27 +0000886 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000887 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
888 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000889}
890
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000891void CGDebugInfo::CollectRecordLambdaFields(
892 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000893 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000894 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
895 // has the name and the location of the variable so we should iterate over
896 // both concurrently.
897 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
898 RecordDecl::field_iterator Field = CXXDecl->field_begin();
899 unsigned fieldno = 0;
900 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000901 E = CXXDecl->captures_end();
902 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000903 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000904 if (C.capturesVariable()) {
905 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000906 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000907 StringRef VName = V->getName();
908 uint64_t SizeInBitsOverride = 0;
909 if (Field->isBitField()) {
910 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
911 assert(SizeInBitsOverride && "found named 0-width bitfield");
912 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000913 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000914 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
915 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
916 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000917 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000918 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000919 // TODO: Need to handle 'this' in some way by probably renaming the
920 // this of the lambda class and having a field member of 'this' or
921 // by using AT_object_pointer for the function and having that be
922 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000923 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000924 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000925 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000926 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000927 "this", type, 0, f->getLocation(), f->getAccess(),
928 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000929
930 elements.push_back(fieldType);
931 }
932 }
933}
934
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000935llvm::DIDerivedType *
936CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000937 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000938 // Create the descriptor for the static variable, with or without
939 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000940 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
942 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000943
Eric Christopher91a31902013-01-16 01:22:32 +0000944 unsigned LineNumber = getLineNumber(Var->getLocation());
945 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000946 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000947 if (Var->getInit()) {
948 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000949 if (Value) {
950 if (Value->isInt())
951 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
952 if (Value->isFloat())
953 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
954 }
Eric Christopher91a31902013-01-16 01:22:32 +0000955 }
956
Adrian Prantl21361fb2014-08-29 22:44:27 +0000957 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000958 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000959 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000960 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000961 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000962}
963
Eric Christophere7b87e52014-10-26 23:40:33 +0000964void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000965 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
966 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000967 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000968 StringRef name = field->getName();
969 QualType type = field->getType();
970
971 // Ignore unnamed fields unless they're anonymous structs/unions.
972 if (name.empty() && !type->isRecordType())
973 return;
974
975 uint64_t SizeInBitsOverride = 0;
976 if (field->isBitField()) {
977 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
978 assert(SizeInBitsOverride && "found named 0-width bitfield");
979 }
980
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000981 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000982 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
983 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000984
985 elements.push_back(fieldType);
986}
987
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000988void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000989 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000990 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000991 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000992 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
993
Eric Christopher91a31902013-01-16 01:22:32 +0000994 if (CXXDecl && CXXDecl->isLambda())
995 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
996 else {
997 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000998
Eric Christopher91a31902013-01-16 01:22:32 +0000999 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001000 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001001
Eric Christopher91a31902013-01-16 01:22:32 +00001002 // Static and non-static members should appear in the same order as
1003 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001004 for (const auto *I : record->decls())
1005 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001006 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001007 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001008 if (MI != StaticDataMemberCache.end()) {
1009 assert(MI->second &&
1010 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001011 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001012 } else {
1013 auto Field = CreateRecordStaticField(V, RecordTy, record);
1014 elements.push_back(Field);
1015 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001016 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001017 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1018 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001019
1020 // Bump field number for next field.
1021 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001023 }
1024}
1025
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001026llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001027CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001028 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001029 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001030 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001032 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001033 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1034 Func, Unit);
1035}
David Blaikie2aaf0652013-01-07 22:24:59 +00001036
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001037llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1038 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001039 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001040 llvm::DITypeRefArray Args(
1041 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001042 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001043 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001045 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001046
1047 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001048 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001049
David Blaikie2aaf0652013-01-07 22:24:59 +00001050 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001051 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001052 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1053 // Create pointer type directly in this case.
1054 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1055 QualType PointeeTy = ThisPtrTy->getPointeeType();
1056 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001057 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001058 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001059 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1060 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001061 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001062 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001063 // TODO: This and the artificial type below are misleading, the
1064 // types aren't artificial the argument is, but the current
1065 // metadata doesn't represent that.
1066 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1067 Elts.push_back(ThisPtrType);
1068 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001069 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001070 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001071 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1072 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001073 }
1074
1075 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001076 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1077 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001078
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001079 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001080
Adrian Prantl0630eb72013-12-18 21:48:18 +00001081 unsigned Flags = 0;
1082 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001083 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001084 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001085 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001086
Eric Christopher28a6db52015-10-15 06:56:08 +00001087 return DBuilder.createSubroutineType(EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001088}
1089
Eric Christopherb2a008c2013-05-16 00:45:12 +00001090/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001091/// inside a function.
1092static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1093 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1094 return isFunctionLocalClass(NRD);
1095 if (isa<FunctionDecl>(RD->getDeclContext()))
1096 return true;
1097 return false;
1098}
1099
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001100llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1101 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001102 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001103 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001106 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001107
1108 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1109 // make sense to give a single ctor/dtor a linkage name.
1110 StringRef MethodLinkageName;
1111 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1112 MethodLinkageName = CGM.getMangledName(Method);
1113
1114 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001115 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001116 unsigned MethodLine = 0;
1117 if (!Method->isImplicit()) {
1118 MethodDefUnit = getOrCreateFile(Method->getLocation());
1119 MethodLine = getLineNumber(Method->getLocation());
1120 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001121
1122 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001124 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001126
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 if (Method->isVirtual()) {
1128 if (Method->isPure())
1129 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1130 else
1131 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001132
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 // It doesn't make sense to give a virtual destructor a vtable index,
1134 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001135 // FIXME: Add proper support for debug info for virtual calls in
1136 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1137 // lookup if we have multiple or virtual inheritance.
1138 if (!isa<CXXDestructorDecl>(Method) &&
1139 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001140 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001141 ContainingType = RecordTy;
1142 }
1143
1144 unsigned Flags = 0;
1145 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001146 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001147 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1149 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001151 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001152 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001153 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001154 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001155 }
1156 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001158 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001159 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001160 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001161 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001162
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001163 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1164 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001165 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1166 MethodTy, /*isLocalToUnit=*/false,
1167 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001168 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001169
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001170 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001171
1172 return SP;
1173}
1174
Eric Christophere7b87e52014-10-26 23:40:33 +00001175void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001176 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1177 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001178
1179 // Since we want more than just the individual member decls if we
1180 // have templated functions iterate over every declaration to gather
1181 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001182 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001183 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1184 // If the member is implicit, don't add it to the member list. This avoids
1185 // the member being added to type units by LLVM, while still allowing it
1186 // to be emitted into the type declaration/reference inside the compile
1187 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001188 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001189 // FIXME: Handle Using(Shadow?)Decls here to create
1190 // DW_TAG_imported_declarations inside the class for base decls brought into
1191 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1192 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1193 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001194 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001195 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001196
1197 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1198 continue;
1199
David Blaikiefd580722014-10-06 05:18:55 +00001200 // Reuse the existing member function declaration if it exists.
1201 // It may be associated with the declaration of the type & should be
1202 // reused as we're building the definition.
1203 //
1204 // This situation can arise in the vtable-based debug info reduction where
1205 // implicit members are emitted in a non-vtable TU.
1206 auto MI = SPCache.find(Method->getCanonicalDecl());
1207 EltTys.push_back(MI == SPCache.end()
1208 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001209 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001210 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001211}
Guy Benyei11169dd2012-12-18 14:30:41 +00001212
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001213void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001214 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001215 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001217 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 unsigned BFlags = 0;
1219 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001220
Guy Benyei11169dd2012-12-18 14:30:41 +00001221 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001222 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001223
Aaron Ballman574705e2014-03-13 15:41:46 +00001224 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001225 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1226 // virtual base offset offset is -ve. The code generator emits dwarf
1227 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001228 BaseOffset = 0 - CGM.getItaniumVTableContext()
1229 .getVirtualBaseOffsetOffset(RD, Base)
1230 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001231 } else {
1232 // In the MS ABI, store the vbtable offset, which is analogous to the
1233 // vbase offset offset in Itanium.
1234 BaseOffset =
1235 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1236 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001237 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 } else
1239 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1240 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1241 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001242
Adrian Prantl21361fb2014-08-29 22:44:27 +00001243 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001244 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001245 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 EltTys.push_back(DTy);
1247 }
1248}
1249
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001250llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001251CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1252 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001253 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001254 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001255 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1256 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001257 StringRef Name;
1258 if (TPList)
1259 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001260 switch (TA.getKind()) {
1261 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001262 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001263 TemplateParams.push_back(
1264 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001265 } break;
1266 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001267 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001268 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1269 TheCU, Name, TTy,
1270 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001271 } break;
1272 case TemplateArgument::Declaration: {
1273 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001274 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001275 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001276 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001277 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001278 // Variable pointer template parameters have a value that is the address
1279 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001280 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001281 V = CGM.GetAddrOfGlobalVar(VD);
1282 // Member function pointers have special support for building them, though
1283 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001284 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001285 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001286 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001287 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001288 // Member data pointers have special handling too to compute the fixed
1289 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001290 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001291 // These five lines (& possibly the above member function pointer
1292 // handling) might be able to be refactored to use similar code in
1293 // CodeGenModule::getMemberPointerConstant
1294 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1295 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001296 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001297 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001298 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001299 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1300 TheCU, Name, TTy,
1301 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001302 } break;
1303 case TemplateArgument::NullPtr: {
1304 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001305 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001306 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001307 // Special case member data pointer null values since they're actually -1
1308 // instead of zero.
1309 if (const MemberPointerType *MPT =
1310 dyn_cast<MemberPointerType>(T.getTypePtr()))
1311 // But treat member function pointers as simple zero integers because
1312 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1313 // CodeGen grows handling for values of non-null member function
1314 // pointers then perhaps we could remove this special case and rely on
1315 // EmitNullMemberPointer for member function pointers.
1316 if (MPT->isMemberDataPointer())
1317 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1318 if (!V)
1319 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001320 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1321 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001322 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001323 case TemplateArgument::Template:
1324 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001325 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001326 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1327 break;
1328 case TemplateArgument::Pack:
1329 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1330 TheCU, Name, nullptr,
1331 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1332 break;
David Majnemer5559d472013-08-24 08:21:10 +00001333 case TemplateArgument::Expression: {
1334 const Expr *E = TA.getAsExpr();
1335 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001336 if (E->isGLValue())
1337 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001338 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001339 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001340 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001341 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1342 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001343 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001344 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001345 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001346 case TemplateArgument::Null:
1347 llvm_unreachable(
1348 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001349 }
1350 }
1351 return DBuilder.getOrCreateArray(TemplateParams);
1352}
1353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001354llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001355CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001356 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 if (FD->getTemplatedKind() ==
1358 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001359 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1360 ->getTemplate()
1361 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001362 return CollectTemplateParams(
1363 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001366}
1367
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001368llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1369 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001370 // Always get the full list of parameters, not just the ones from
1371 // the specialization.
1372 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001373 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001374 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001375 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001376}
1377
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001378llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001379 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 return VTablePtrType;
1381
1382 ASTContext &Context = CGM.getContext();
1383
1384 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001385 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001387 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001390 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001391 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1392 return VTablePtrType;
1393}
1394
Guy Benyei11169dd2012-12-18 14:30:41 +00001395StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001396 // Copy the gdb compatible name on the side and use its reference.
1397 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001398}
1399
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001400void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001401 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1403
1404 // If there is a primary base then it will hold vtable info.
1405 if (RL.getPrimaryBase())
1406 return;
1407
1408 // If this class is not dynamic then there is not any vtable info to collect.
1409 if (!RD->isDynamicClass())
1410 return;
1411
1412 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001413 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001414 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001415 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001416 EltTys.push_back(VPTR);
1417}
1418
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001419llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001420 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001421 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001422 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001423 return T;
1424}
1425
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001427 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001428 return getOrCreateStandaloneType(D, Loc);
1429}
1430
1431llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1432 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001433 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001434 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001435 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001436 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001437
1438 // Composite types with UIDs were already retained by DIBuilder
1439 // because they are only referenced by name in the IR.
1440 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1441 if (!CTy->getIdentifier().empty())
1442 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001443 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 return T;
1445}
1446
David Blaikie483a9da2014-05-06 18:35:21 +00001447void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001448 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001449 return;
1450 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001451 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001452 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001453 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001454 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001455 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001456 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001457 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001458}
1459
David Blaikieb2e86eb2013-08-15 20:49:17 +00001460void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001461 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001462 !CGM.getLangOpts().CPlusPlus)
1463 completeRequiredType(RD);
1464}
1465
1466void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001467 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001468 return;
1469
David Blaikie6943dea2013-08-20 01:28:15 +00001470 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1471 if (CXXDecl->isDynamicClass())
1472 return;
1473
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001474 if (DebugTypeExtRefs && RD->isFromASTFile())
1475 return;
1476
David Blaikieb2e86eb2013-08-15 20:49:17 +00001477 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001478 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001479 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001480 completeClassData(RD);
1481}
1482
1483void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001484 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001485 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001486 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001487 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001488 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001489 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001490 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001491 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001492 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001493 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001494}
1495
David Blaikie0e716b42014-03-03 23:48:23 +00001496static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1497 CXXRecordDecl::method_iterator End) {
1498 for (; I != End; ++I)
1499 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001500 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1501 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001502 return true;
1503 return false;
1504}
1505
Benjamin Kramer8c305922016-02-02 11:06:51 +00001506static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1507 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001508 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001509 // Does the type exist in an imported clang module?
Adrian Prantl43e00812016-01-19 23:42:53 +00001510 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition() &&
Adrian Prantl8f55b662016-01-20 01:29:34 +00001511 (RD->isExternallyVisible() || !RD->getName().empty()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001512 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001513
Benjamin Kramer8c305922016-02-02 11:06:51 +00001514 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001515 return false;
1516
1517 if (!LangOpts.CPlusPlus)
1518 return false;
1519
1520 if (!RD->isCompleteDefinitionRequired())
1521 return true;
1522
1523 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1524
1525 if (!CXXDecl)
1526 return false;
1527
1528 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1529 return true;
1530
1531 TemplateSpecializationKind Spec = TSK_Undeclared;
1532 if (const ClassTemplateSpecializationDecl *SD =
1533 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1534 Spec = SD->getSpecializationKind();
1535
1536 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1537 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1538 CXXDecl->method_end()))
1539 return true;
1540
1541 return false;
1542}
1543
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001546 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001547 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1548 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001549 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001550 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
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
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001558 RecordDecl *RD = Ty->getDecl();
1559
Guy Benyei11169dd2012-12-18 14:30:41 +00001560 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001561 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001562
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.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001569 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001570
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001571 const RecordDecl *D = RD->getDefinition();
1572 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 return FwdDecl;
1574
David Blaikieadfbf992013-08-18 16:55:33 +00001575 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1576 CollectContainingType(CXXDecl, FwdDecl);
1577
Guy Benyei11169dd2012-12-18 14:30:41 +00001578 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001579 LexicalBlockStack.emplace_back(&*FwdDecl);
1580 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001581
Guy Benyei11169dd2012-12-18 14:30:41 +00001582 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001583 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001584 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001585
1586 // Note: The split of CXXDecl information here is intentional, the
1587 // gdb tests will depend on a certain ordering at printout. The debug
1588 // information offsets are still correct if we merge them all together
1589 // though.
1590 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1591 if (CXXDecl) {
1592 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1593 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1594 }
1595
Eric Christopher91a31902013-01-16 01:22:32 +00001596 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001598 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001599 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001600
1601 LexicalBlockStack.pop_back();
1602 RegionMap.erase(Ty->getDecl());
1603
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001604 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001605 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001606
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001607 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001608 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001609 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001610
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001611 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001612 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001613}
1614
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001615llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1616 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001617 // Ignore protocols.
1618 return getOrCreateType(Ty->getBaseType(), Unit);
1619}
1620
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001621/// \return true if Getter has the default name for the property PD.
1622static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1623 const ObjCMethodDecl *Getter) {
1624 assert(PD);
1625 if (!Getter)
1626 return true;
1627
1628 assert(Getter->getDeclName().isObjCZeroArgSelector());
1629 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001630 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001631}
1632
1633/// \return true if Setter has the default name for the property PD.
1634static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1635 const ObjCMethodDecl *Setter) {
1636 assert(PD);
1637 if (!Setter)
1638 return true;
1639
1640 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001641 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001642 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001643}
1644
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001645llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1646 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 ObjCInterfaceDecl *ID = Ty->getDecl();
1648 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001649 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001650
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001651 // Return a forward declaration if this type was imported from a clang module.
1652 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1653 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1654 ID->getName(),
1655 getDeclContextDescriptor(ID), Unit, 0);
1656
Guy Benyei11169dd2012-12-18 14:30:41 +00001657 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001658 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001659 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001660 auto RuntimeLang =
1661 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001662
1663 // If this is just a forward declaration return a special forward-declaration
1664 // debug type since we won't be able to lay out the entire type.
1665 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001666 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001667 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001668 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001669 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1670 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001671 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001672 return FwdDecl;
1673 }
1674
David Blaikieef8a9512014-05-05 23:23:53 +00001675 return CreateTypeDefinition(Ty, Unit);
1676}
1677
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001678llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001679CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1680 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001681 // Use the Module pointer as the key into the cache. This is a
1682 // nullptr if the "Module" is a PCH, which is safe because we don't
1683 // support chained PCH debug info, so there can only be a single PCH.
1684 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001685 auto ModRef = ModuleCache.find(M);
1686 if (ModRef != ModuleCache.end())
1687 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001688
1689 // Macro definitions that were defined with "-D" on the command line.
1690 SmallString<128> ConfigMacros;
1691 {
1692 llvm::raw_svector_ostream OS(ConfigMacros);
1693 const auto &PPOpts = CGM.getPreprocessorOpts();
1694 unsigned I = 0;
1695 // Translate the macro definitions back into a commmand line.
1696 for (auto &M : PPOpts.Macros) {
1697 if (++I > 1)
1698 OS << " ";
1699 const std::string &Macro = M.first;
1700 bool Undef = M.second;
1701 OS << "\"-" << (Undef ? 'U' : 'D');
1702 for (char c : Macro)
1703 switch (c) {
1704 case '\\' : OS << "\\\\"; break;
1705 case '"' : OS << "\\\""; break;
1706 default: OS << c;
1707 }
1708 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001709 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001710 }
Adrian Prantl66689202015-09-18 23:01:45 +00001711
Adrian Prantl835e6632015-09-24 16:10:10 +00001712 bool IsRootModule = M ? !M->Parent : true;
1713 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001714 // PCH files don't have a signature field in the control block,
1715 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001716 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001717 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001718 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001719 Mod.getPath(), TheCU->getProducer(), true,
1720 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001721 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001722 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001723 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001724 llvm::DIModule *Parent =
1725 IsRootModule ? nullptr
1726 : getOrCreateModuleRef(
1727 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1728 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001729 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001730 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1731 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001732 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001733 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001734}
1735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001736llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1737 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001738 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001739 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001740 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001741 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001742
1743 // Bit size, align and offset of the type.
1744 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1745 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1746
1747 unsigned Flags = 0;
1748 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001749 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001750
Adrian Prantlfd696112015-10-01 00:48:51 +00001751 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001752 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001753 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1754 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001755
David Blaikieef8a9512014-05-05 23:23:53 +00001756 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001757 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001758
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001759 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001760 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001761 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001762
1763 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001764 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001765
1766 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1767 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001768 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001769 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001770 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001771 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001772
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001773 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 EltTys.push_back(InhTag);
1775 }
1776
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001777 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001778 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001780 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 unsigned PLine = getLineNumber(Loc);
1782 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1783 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001784 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1785 PD->getName(), PUnit, PLine,
1786 hasDefaultGetterName(PD, Getter) ? ""
1787 : getSelectorName(PD->getGetterName()),
1788 hasDefaultSetterName(PD, Setter) ? ""
1789 : getSelectorName(PD->getSetterName()),
1790 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001792 };
1793 {
1794 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001795 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001796 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001797 PropertySet.insert(PD->getIdentifier());
1798 AddProperty(PD);
1799 }
Manman Renefe1bac2016-01-27 20:00:32 +00001800 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001801 // Don't emit duplicate metadata for properties that were already in a
1802 // class extension.
1803 if (!PropertySet.insert(PD->getIdentifier()).second)
1804 continue;
1805 AddProperty(PD);
1806 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 }
1808
1809 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1810 unsigned FieldNo = 0;
1811 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1812 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001813 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001814 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001815 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001816
Guy Benyei11169dd2012-12-18 14:30:41 +00001817 StringRef FieldName = Field->getName();
1818
1819 // Ignore unnamed fields.
1820 if (FieldName.empty())
1821 continue;
1822
1823 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001824 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 unsigned FieldLine = getLineNumber(Field->getLocation());
1826 QualType FType = Field->getType();
1827 uint64_t FieldSize = 0;
1828 unsigned FieldAlign = 0;
1829
1830 if (!FType->isIncompleteArrayType()) {
1831
1832 // Bit size, align and offset of the type.
1833 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001834 ? Field->getBitWidthValue(CGM.getContext())
1835 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 FieldAlign = CGM.getContext().getTypeAlign(FType);
1837 }
1838
1839 uint64_t FieldOffset;
1840 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1841 // We don't know the runtime offset of an ivar if we're using the
1842 // non-fragile ABI. For bitfields, use the bit offset into the first
1843 // byte of storage of the bitfield. For other fields, use zero.
1844 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001845 FieldOffset =
1846 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001847 FieldOffset %= CGM.getContext().getCharWidth();
1848 } else {
1849 FieldOffset = 0;
1850 }
1851 } else {
1852 FieldOffset = RL.getFieldOffset(FieldNo);
1853 }
1854
1855 unsigned Flags = 0;
1856 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001857 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001858 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001859 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001860 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001861 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001862
Craig Topper8a13c412014-05-21 05:09:00 +00001863 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001865 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001866 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001867 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001868 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001869 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001870 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1872 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001873 PropertyNode = DBuilder.createObjCProperty(
1874 PD->getName(), PUnit, PLine,
1875 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1876 PD->getGetterName()),
1877 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1878 PD->getSetterName()),
1879 PD->getPropertyAttributes(),
1880 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 }
1882 }
1883 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001884 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1885 FieldSize, FieldAlign, FieldOffset, Flags,
1886 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 EltTys.push_back(FieldTy);
1888 }
1889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001891 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001892
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001894 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001895}
1896
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001897llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1898 llvm::DIFile *Unit) {
1899 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001900 int64_t Count = Ty->getNumElements();
1901 if (Count == 0)
1902 // If number of elements are not known then this is an unbounded array.
1903 // Use Count == -1 to express such arrays.
1904 Count = -1;
1905
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001906 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001907 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001908
1909 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1910 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1911
1912 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1913}
1914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001915llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001916 uint64_t Size;
1917 uint64_t Align;
1918
1919 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1920 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1921 Size = 0;
1922 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001923 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 } else if (Ty->isIncompleteArrayType()) {
1925 Size = 0;
1926 if (Ty->getElementType()->isIncompleteType())
1927 Align = 0;
1928 else
1929 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001930 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 Size = 0;
1932 Align = 0;
1933 } else {
1934 // Size and align of the whole array, not the element type.
1935 Size = CGM.getContext().getTypeSize(Ty);
1936 Align = CGM.getContext().getTypeAlign(Ty);
1937 }
1938
1939 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1940 // interior arrays, do we care? Why aren't nested arrays represented the
1941 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001942 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001943 QualType EltTy(Ty, 0);
1944 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1945 // If the number of elements is known, then count is that number. Otherwise,
1946 // it's -1. This allows us to represent a subrange with an array of 0
1947 // elements, like this:
1948 //
1949 // struct foo {
1950 // int x[0];
1951 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001952 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1954 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001955
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 // FIXME: Verify this is right for VLAs.
1957 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1958 EltTy = Ty->getElementType();
1959 }
1960
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001961 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001962
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001963 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1964 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001965}
1966
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001967llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1968 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001969 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1970 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001971}
1972
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1974 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001975 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1976 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001977}
1978
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001979llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1980 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001981 uint64_t Size =
1982 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001983 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001984 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001985 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001986 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001987
1988 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001989 Ty->getPointeeType()->getAs<FunctionProtoType>();
1990 return DBuilder.createMemberPointerType(
1991 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1992 Ty->getClass(), FPT->getTypeQuals())),
1993 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001994 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001995}
1996
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001997llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 // Ignore the atomic wrapping
1999 // FIXME: What is the correct representation?
2000 return getOrCreateType(Ty->getValueType(), U);
2001}
2002
Xiuli Pan9c14e282016-01-09 12:53:17 +00002003llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2004 llvm::DIFile *U) {
2005 return getOrCreateType(Ty->getElementType(), U);
2006}
2007
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002008llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002009 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002010
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 uint64_t Size = 0;
2012 uint64_t Align = 0;
2013 if (!ED->getTypeForDecl()->isIncompleteType()) {
2014 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2015 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2016 }
2017
Manman Rene0064d82013-08-29 23:19:58 +00002018 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2019
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002020 bool isImportedFromModule =
2021 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2022
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 // If this is just a forward declaration, construct an appropriately
2024 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002025 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002026 // Note that it is possible for enums to be created as part of
2027 // their own declcontext. In this case a FwdDecl will be created
2028 // twice. This doesn't cause a problem because both FwdDecls are
2029 // entered into the ReplaceMap: finalize() will replace the first
2030 // FwdDecl with the second and then replace the second with
2031 // complete type.
Reid Kleckner00381aa2016-03-24 20:38:43 +00002032 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002033 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002034 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2035 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002036
Guy Benyei11169dd2012-12-18 14:30:41 +00002037 unsigned Line = getLineNumber(ED->getLocation());
2038 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002039 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002040 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2041 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002042
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002043 ReplaceMap.emplace_back(
2044 std::piecewise_construct, std::make_tuple(Ty),
2045 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002046 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002047 }
2048
David Blaikie483a9da2014-05-06 18:35:21 +00002049 return CreateTypeDefinition(Ty);
2050}
2051
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002052llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002053 const EnumDecl *ED = Ty->getDecl();
2054 uint64_t Size = 0;
2055 uint64_t Align = 0;
2056 if (!ED->getTypeForDecl()->isIncompleteType()) {
2057 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2058 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2059 }
2060
2061 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2062
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002063 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002064 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002065 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002066 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002067 Enumerators.push_back(DBuilder.createEnumerator(
2068 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 }
2070
2071 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002072 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002073
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002074 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002075 unsigned Line = getLineNumber(ED->getLocation());
Reid Kleckner00381aa2016-03-24 20:38:43 +00002076 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002077 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002078 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2079 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2080 Line, Size, Align, EltArray, ClassTy,
2081 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002082}
2083
David Blaikie05491062013-01-21 04:37:12 +00002084static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2085 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002086 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002087 Qualifiers InnerQuals = T.getLocalQualifiers();
2088 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2089 // that is already there.
2090 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2091 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002092 QualType LastT = T;
2093 switch (T->getTypeClass()) {
2094 default:
David Blaikie05491062013-01-21 04:37:12 +00002095 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002096 case Type::TemplateSpecialization: {
2097 const auto *Spec = cast<TemplateSpecializationType>(T);
2098 if (Spec->isTypeAlias())
2099 return C.getQualifiedType(T.getTypePtr(), Quals);
2100 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002101 break;
2102 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002103 case Type::TypeOfExpr:
2104 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2105 break;
2106 case Type::TypeOf:
2107 T = cast<TypeOfType>(T)->getUnderlyingType();
2108 break;
2109 case Type::Decltype:
2110 T = cast<DecltypeType>(T)->getUnderlyingType();
2111 break;
2112 case Type::UnaryTransform:
2113 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2114 break;
2115 case Type::Attributed:
2116 T = cast<AttributedType>(T)->getEquivalentType();
2117 break;
2118 case Type::Elaborated:
2119 T = cast<ElaboratedType>(T)->getNamedType();
2120 break;
2121 case Type::Paren:
2122 T = cast<ParenType>(T)->getInnerType();
2123 break;
David Blaikie05491062013-01-21 04:37:12 +00002124 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002125 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 break;
2127 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002128 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002129 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002130 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002131 break;
2132 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002133
Guy Benyei11169dd2012-12-18 14:30:41 +00002134 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002135 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 } while (true);
2137}
2138
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002139llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002140
2141 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002142 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002143
David Blaikief427b002014-05-06 03:42:01 +00002144 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 if (it != TypeCache.end()) {
2146 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002147 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002148 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 }
2150
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002151 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002152}
2153
David Blaikie0e716b42014-03-03 23:48:23 +00002154void CGDebugInfo::completeTemplateDefinition(
2155 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002156 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002157 return;
2158
David Blaikie0e716b42014-03-03 23:48:23 +00002159 completeClassData(&SD);
2160 // In case this type has no member function definitions being emitted, ensure
2161 // it is retained
2162 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2163}
2164
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002165llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002166 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002167 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002168
2169 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002170 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002172 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 return T;
2174
Adrian Prantlca844182015-09-11 17:23:03 +00002175 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002176 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002177
2178 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002179 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002180
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 return Res;
2182}
2183
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002184llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002185 // A forward declaration inside a module header does not belong to the module.
2186 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2187 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002188 if (DebugTypeExtRefs && D->isFromASTFile()) {
2189 // Record a reference to an imported clang module or precompiled header.
2190 auto *Reader = CGM.getContext().getExternalSource();
2191 auto Idx = D->getOwningModuleID();
2192 auto Info = Reader->getSourceDescriptor(Idx);
2193 if (Info)
2194 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2195 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002196 // We are building a clang module or a precompiled header.
2197 //
2198 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2199 // and it wouldn't be necessary to specify the parent scope
2200 // because the type is already unique by definition (it would look
2201 // like the output of -fno-standalone-debug). On the other hand,
2202 // the parent scope helps a consumer to quickly locate the object
2203 // file where the type's definition is located, so it might be
2204 // best to make this behavior a command line or debugger tuning
2205 // option.
2206 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2207 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002208 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002209 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2210 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002211 } else {
2212 // This the precompiled header being built.
2213 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002214 }
2215 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002216
Adrian Prantl9402cef2015-09-20 16:51:35 +00002217 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002218}
2219
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002220llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 // Handle qualifiers, which recursively handles what they refer to.
2222 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002223 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002224
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 // Work out details of type.
2226 switch (Ty->getTypeClass()) {
2227#define TYPE(Class, Base)
2228#define ABSTRACT_TYPE(Class, Base)
2229#define NON_CANONICAL_TYPE(Class, Base)
2230#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2231#include "clang/AST/TypeNodes.def"
2232 llvm_unreachable("Dependent types cannot show up in debug information");
2233
2234 case Type::ExtVector:
2235 case Type::Vector:
2236 return CreateType(cast<VectorType>(Ty), Unit);
2237 case Type::ObjCObjectPointer:
2238 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2239 case Type::ObjCObject:
2240 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2241 case Type::ObjCInterface:
2242 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2243 case Type::Builtin:
2244 return CreateType(cast<BuiltinType>(Ty));
2245 case Type::Complex:
2246 return CreateType(cast<ComplexType>(Ty));
2247 case Type::Pointer:
2248 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002249 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002250 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002251 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002252 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002253 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002254 case Type::BlockPointer:
2255 return CreateType(cast<BlockPointerType>(Ty), Unit);
2256 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002257 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002259 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002261 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 case Type::FunctionProto:
2263 case Type::FunctionNoProto:
2264 return CreateType(cast<FunctionType>(Ty), Unit);
2265 case Type::ConstantArray:
2266 case Type::VariableArray:
2267 case Type::IncompleteArray:
2268 return CreateType(cast<ArrayType>(Ty), Unit);
2269
2270 case Type::LValueReference:
2271 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2272 case Type::RValueReference:
2273 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2274
2275 case Type::MemberPointer:
2276 return CreateType(cast<MemberPointerType>(Ty), Unit);
2277
2278 case Type::Atomic:
2279 return CreateType(cast<AtomicType>(Ty), Unit);
2280
Xiuli Pan9c14e282016-01-09 12:53:17 +00002281 case Type::Pipe:
2282 return CreateType(cast<PipeType>(Ty), Unit);
2283
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002285 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2286
David Blaikie42edade2014-11-11 20:44:45 +00002287 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002288 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 case Type::Elaborated:
2290 case Type::Paren:
2291 case Type::SubstTemplateTypeParm:
2292 case Type::TypeOfExpr:
2293 case Type::TypeOf:
2294 case Type::Decltype:
2295 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002296 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002297 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002299
David Blaikie42edade2014-11-11 20:44:45 +00002300 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002301}
2302
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002303llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2304 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002305 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002306
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002307 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002308
2309 // We may have cached a forward decl when we could have created
2310 // a non-forward decl. Go ahead and create a non-forward decl
2311 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002312 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002313 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002314
2315 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002317
2318 // Propagate members from the declaration to the definition
2319 // CreateType(const RecordType*) will overwrite this with the members in the
2320 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002321 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002322
Guy Benyei11169dd2012-12-18 14:30:41 +00002323 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002324 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002325 return Res;
2326}
2327
2328// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002329llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002331
Guy Benyei11169dd2012-12-18 14:30:41 +00002332 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002333 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 unsigned Line = getLineNumber(RD->getLocation());
2335 StringRef RDName = getClassName(RD);
2336
Reid Kleckner00381aa2016-03-24 20:38:43 +00002337 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002338
David Blaikied2785892013-08-18 17:36:19 +00002339 // If we ended up creating the type during the context chain construction,
2340 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002341 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002342 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002343 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002344 return T;
David Blaikied2785892013-08-18 17:36:19 +00002345
Adrian Prantl381e7552014-02-04 21:29:50 +00002346 // If this is just a forward or incomplete declaration, construct an
2347 // appropriately marked node and just return it.
2348 const RecordDecl *D = RD->getDefinition();
2349 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002350 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002351
2352 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2353 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002354
Manman Rene0064d82013-08-29 23:19:58 +00002355 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2356
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002357 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002358 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2359 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002360
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002361 RegionMap[Ty->getDecl()].reset(RealDecl);
2362 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002363
David Blaikieadfbf992013-08-18 16:55:33 +00002364 if (const ClassTemplateSpecializationDecl *TSpecial =
2365 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002367 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002368 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002369}
2370
David Blaikieadfbf992013-08-18 16:55:33 +00002371void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002372 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002373 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002374 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002375 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2376 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002377 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002378 while (1) {
2379 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2380 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2381 if (PBT && !BRL.isPrimaryBaseVirtual())
2382 PBase = PBT;
2383 else
2384 break;
2385 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002386 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002387 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2388 getOrCreateFile(RD->getLocation())));
2389 } else if (RD->isDynamicClass())
2390 ContainingType = RealDecl;
2391
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002392 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002393}
2394
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002395llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002396 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002397 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002398 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2399 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002400 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002401 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 *Offset += FieldSize;
2403 return Ty;
2404}
2405
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002406void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002407 StringRef &Name,
2408 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002409 llvm::DIScope *&FDContext,
2410 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002411 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002412 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2413 Name = getFunctionName(FD);
2414 // Use mangled name as linkage name for C/C++ functions.
2415 if (FD->hasPrototype()) {
2416 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002417 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002418 }
2419 // No need to replicate the linkage name if it isn't different from the
2420 // subprogram name, no need to have it at all unless coverage is enabled or
2421 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002422 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2423 !CGM.getCodeGenOpts().EmitGcovNotes &&
2424 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002425 LinkageName = StringRef();
2426
Benjamin Kramer8c305922016-02-02 11:06:51 +00002427 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002428 if (const NamespaceDecl *NSDecl =
2429 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2430 FDContext = getOrCreateNameSpace(NSDecl);
2431 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002432 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2433 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2434 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2435 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002436 // Collect template parameters.
2437 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2438 }
2439}
2440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002442 unsigned &LineNo, QualType &T,
2443 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002444 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002445 Unit = getOrCreateFile(VD->getLocation());
2446 LineNo = getLineNumber(VD->getLocation());
2447
2448 setLocation(VD->getLocation());
2449
2450 T = VD->getType();
2451 if (T->isIncompleteArrayType()) {
2452 // CodeGen turns int[] into int[1] so we'll do the same here.
2453 llvm::APInt ConstVal(32, 1);
2454 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2455
2456 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2457 ArrayType::Normal, 0);
2458 }
2459
2460 Name = VD->getName();
2461 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2462 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2463 LinkageName = CGM.getMangledName(VD);
2464 if (LinkageName == Name)
2465 LinkageName = StringRef();
2466
2467 // Since we emit declarations (DW_AT_members) for static members, place the
2468 // definition of those static members in the namespace they were declared in
2469 // in the source code (the lexical decl context).
2470 // FIXME: Generalize this for even non-member global variables where the
2471 // declaration and definition may have different lexical decl contexts, once
2472 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002473 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2474 : VD->getDeclContext();
2475 // When a record type contains an in-line initialization of a static data
2476 // member, and the record type is marked as __declspec(dllexport), an implicit
2477 // definition of the member will be created in the record context. DWARF
2478 // doesn't seem to have a nice way to describe this in a form that consumers
2479 // are likely to understand, so fake the "normal" situation of a definition
2480 // outside the class by putting it in the global scope.
2481 if (DC->isRecord())
2482 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002483
Reid Kleckner00381aa2016-03-24 20:38:43 +00002484 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2485 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002486}
2487
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002488llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002489CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002490 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002491 StringRef Name, LinkageName;
2492 unsigned Flags = 0;
2493 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DIFile *Unit = getOrCreateFile(Loc);
2495 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002496 unsigned Line = getLineNumber(Loc);
2497
2498 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2499 TParamsArray, Flags);
2500 // Build function type.
2501 SmallVector<QualType, 16> ArgTypes;
2502 for (const ParmVarDecl *Parm: FD->parameters())
2503 ArgTypes.push_back(Parm->getType());
2504 QualType FnType =
2505 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2506 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002507 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002508 DContext, Name, LinkageName, Unit, Line,
2509 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002510 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002511 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002512 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002513 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2514 std::make_tuple(CanonDecl),
2515 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002516 return SP;
2517}
2518
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002519llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002520CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2521 QualType T;
2522 StringRef Name, LinkageName;
2523 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002524 llvm::DIFile *Unit = getOrCreateFile(Loc);
2525 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002526 unsigned Line = getLineNumber(Loc);
2527
2528 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002529 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2530 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2531 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002532 FwdDeclReplaceMap.emplace_back(
2533 std::piecewise_construct,
2534 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2535 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002536 return GV;
2537}
2538
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002539llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002540 // We only need a declaration (not a definition) of the type - so use whatever
2541 // we would otherwise do to get a type for a pointee. (forward declarations in
2542 // limited debug info, full definitions (if the type definition is available)
2543 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002544 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2545 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002546 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002547 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002548
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002549 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002551
2552 // No definition for now. Emit a forward definition that might be
2553 // merged with a potential upcoming definition.
2554 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2555 return getFunctionForwardDeclaration(FD);
2556 else if (const auto *VD = dyn_cast<VarDecl>(D))
2557 return getGlobalVariableForwardDeclaration(VD);
2558
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002559 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002560}
2561
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002562llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002563 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002564 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002565
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002567 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002568 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002569
2570 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002571 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002572
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002573 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002574 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002575 if (const CXXMethodDecl *MD =
2576 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002577 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002578 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002579 }
2580 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002582 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002583 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 return SP;
2585 }
2586
Aaron Ballman86c93902014-03-06 23:45:36 +00002587 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002588 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002590 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002591 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 return SP;
2593 }
2594 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002595 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002596}
2597
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002598// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002599// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002600llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002601 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002602 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002603 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002604 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2605 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002606 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002607
2608 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2609 return getOrCreateMethodType(Method, F);
2610 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2611 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002612 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002613
2614 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002615 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002616
2617 // Replace the instancetype keyword with the actual type.
2618 if (ResultTy == CGM.getContext().getObjCInstanceType())
2619 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002620 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002621
Adrian Prantl7bec9032013-05-10 21:08:31 +00002622 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002624 QualType SelfDeclTy;
2625 if (auto *SelfDecl = OMethod->getSelfDecl())
2626 SelfDeclTy = SelfDecl->getType();
2627 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2628 if (FPT->getNumParams() > 1)
2629 SelfDeclTy = FPT->getParamType(0);
2630 if (!SelfDeclTy.isNull())
2631 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002632 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002633 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002634 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002635 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002636 for (const auto *PI : OMethod->params())
2637 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002638 // Variadic methods need a special marker at the end of the type list.
2639 if (OMethod->isVariadic())
2640 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002641
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002642 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Eric Christopher28a6db52015-10-15 06:56:08 +00002643 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002645
Adrian Prantl800faef2014-02-25 23:42:18 +00002646 // Handle variadic function types; they need an additional
2647 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002648 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2649 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002650 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002651 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2652 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2653 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2654 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2655 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002656 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +00002657 return DBuilder.createSubroutineType(EltTypeArray);
Adrian Prantld45ba252014-02-25 19:38:11 +00002658 }
2659
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002660 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002661}
2662
Eric Christophere7b87e52014-10-26 23:40:33 +00002663void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2664 SourceLocation ScopeLoc, QualType FnType,
2665 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002666
2667 StringRef Name;
2668 StringRef LinkageName;
2669
2670 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2671
2672 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002673 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002674
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002676 llvm::DIFile *Unit = getOrCreateFile(Loc);
2677 llvm::DIScope *FDContext = Unit;
2678 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 if (!HasDecl) {
2680 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002681 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002683 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002684 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002686 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002687 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002688 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002689 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 return;
2691 }
2692 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002693 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2694 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002695 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2696 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002697 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002698 } else {
2699 // Use llvm function name.
2700 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002701 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002702 }
2703 if (!Name.empty() && Name[0] == '\01')
2704 Name = Name.substr(1);
2705
Adrian Prantl42d71b92014-04-10 23:21:53 +00002706 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002707 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002708 // Artificial functions without a location should not silently reuse CurLoc.
2709 if (Loc.isInvalid())
2710 CurLoc = SourceLocation();
2711 }
2712 unsigned LineNo = getLineNumber(Loc);
2713 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002714
Eric Christopher8018e412014-03-27 18:50:35 +00002715 // FIXME: The function declaration we're constructing here is mostly reusing
2716 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2717 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2718 // all subprograms instead of the actual context since subprogram definitions
2719 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002720 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002721 FDContext, Name, LinkageName, Unit, LineNo,
2722 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002723 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002724 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002725 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002726 // We might get here with a VarDecl in the case we're generating
2727 // code for the initialization of globals. Do not record these decls
2728 // as they will overwrite the actual VarDecl Decl in the cache.
2729 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002730 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002731
Adrian Prantlbebb8932014-03-21 21:01:58 +00002732 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002733 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002734
Guy Benyei11169dd2012-12-18 14:30:41 +00002735 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002736 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002737}
2738
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002739void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2740 QualType FnType) {
2741 StringRef Name;
2742 StringRef LinkageName;
2743
2744 const Decl *D = GD.getDecl();
2745 if (!D)
2746 return;
2747
2748 unsigned Flags = 0;
2749 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002750 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002751 llvm::DINodeArray TParamsArray;
2752 if (isa<FunctionDecl>(D)) {
2753 // If there is a DISubprogram for this function available then use it.
2754 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2755 TParamsArray, Flags);
2756 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2757 Name = getObjCMethodName(OMD);
2758 Flags |= llvm::DINode::FlagPrototyped;
2759 } else {
2760 llvm_unreachable("not a function or ObjC method");
2761 }
2762 if (!Name.empty() && Name[0] == '\01')
2763 Name = Name.substr(1);
2764
2765 if (D->isImplicit()) {
2766 Flags |= llvm::DINode::FlagArtificial;
2767 // Artificial functions without a location should not silently reuse CurLoc.
2768 if (Loc.isInvalid())
2769 CurLoc = SourceLocation();
2770 }
2771 unsigned LineNo = getLineNumber(Loc);
2772 unsigned ScopeLine = 0;
2773
2774 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2775 getOrCreateFunctionType(D, FnType, Unit),
2776 false /*internalLinkage*/, true /*definition*/,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002777 ScopeLine, Flags, CGM.getLangOpts().Optimize,
2778 TParamsArray.get(), getFunctionDeclaration(D));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002779}
2780
David Blaikie835afb22015-01-21 23:08:17 +00002781void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 // Update our current location
2783 setLocation(Loc);
2784
Eric Christophere7b87e52014-10-26 23:40:33 +00002785 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2786 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002787
Adrian Prantle83b1302014-01-07 22:05:52 +00002788 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002789 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002790 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002791}
2792
Guy Benyei11169dd2012-12-18 14:30:41 +00002793void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002794 llvm::MDNode *Back = nullptr;
2795 if (!LexicalBlockStack.empty())
2796 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002797 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002798 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002799 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002800}
2801
Eric Christopher0fdcb312013-05-16 00:52:20 +00002802void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2803 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 // Set our current location.
2805 setLocation(Loc);
2806
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002808 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2809 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002810
Benjamin Kramer8c305922016-02-02 11:06:51 +00002811 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002812 return;
2813
2814 // Create a new lexical block and push it on the stack.
2815 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002816}
2817
Eric Christopher0fdcb312013-05-16 00:52:20 +00002818void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2819 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2821
2822 // Provide an entry in the line table for the end of the block.
2823 EmitLocation(Builder, Loc);
2824
Benjamin Kramer8c305922016-02-02 11:06:51 +00002825 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002826 return;
2827
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 LexicalBlockStack.pop_back();
2829}
2830
Guy Benyei11169dd2012-12-18 14:30:41 +00002831void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2832 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2833 unsigned RCount = FnBeginRegionCount.back();
2834 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2835
2836 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002837 while (LexicalBlockStack.size() != RCount) {
2838 // Provide an entry in the line table for the end of the block.
2839 EmitLocation(Builder, CurLoc);
2840 LexicalBlockStack.pop_back();
2841 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 FnBeginRegionCount.pop_back();
2843}
2844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002845llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002846 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002847
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002848 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 QualType FType;
2850 uint64_t FieldSize, FieldOffset;
2851 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002853 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002854 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002855
2856 FieldOffset = 0;
2857 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2858 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2859 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2860 FType = CGM.getContext().IntTy;
2861 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2862 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2863
2864 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2865 if (HasCopyAndDispose) {
2866 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002867 EltTys.push_back(
2868 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2869 EltTys.push_back(
2870 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 }
2872 bool HasByrefExtendedLayout;
2873 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002874 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2875 HasByrefExtendedLayout) &&
2876 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002877 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002878 EltTys.push_back(
2879 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002880 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002881
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2883 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002884 CGM.getTarget().getPointerAlign(0))) {
2885 CharUnits FieldOffsetInBytes =
2886 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00002887 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00002888 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002889
Guy Benyei11169dd2012-12-18 14:30:41 +00002890 if (NumPaddingBytes.isPositive()) {
2891 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2892 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2893 pad, ArrayType::Normal, 0);
2894 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2895 }
2896 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002897
Guy Benyei11169dd2012-12-18 14:30:41 +00002898 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002899 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002900 FieldSize = CGM.getContext().getTypeSize(FType);
2901 FieldAlign = CGM.getContext().toBits(Align);
2902
Eric Christopherb2a008c2013-05-16 00:45:12 +00002903 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002904 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2905 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 EltTys.push_back(FieldTy);
2907 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002908
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002909 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002910
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002911 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002912
Guy Benyei11169dd2012-12-18 14:30:41 +00002913 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002914 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002915}
2916
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002917void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2918 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002919 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002920 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2922
David Blaikie7fceebf2013-08-19 03:37:48 +00002923 bool Unwritten =
2924 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2925 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002926 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002927 if (!Unwritten)
2928 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002929 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 uint64_t XOffset = 0;
2931 if (VD->hasAttr<BlocksAttr>())
2932 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002933 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 Ty = getOrCreateType(VD->getType(), Unit);
2935
2936 // If there is no debug info for this type then do not emit debug info
2937 // for this variable.
2938 if (!Ty)
2939 return;
2940
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002942 unsigned Line = 0;
2943 unsigned Column = 0;
2944 if (!Unwritten) {
2945 Line = getLineNumber(VD->getLocation());
2946 Column = getColumnNumber(VD->getLocation());
2947 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002948 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002949 unsigned Flags = 0;
2950 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002951 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002952 // If this is the first argument and it is implicit then
2953 // give it an object pointer flag.
2954 // FIXME: There has to be a better way to do this, but for static
2955 // functions there won't be an implicit param at arg1 and
2956 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002957 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002958 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002959 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002960 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2961 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002962 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002963
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002964 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002965
2966 StringRef Name = VD->getName();
2967 if (!Name.empty()) {
2968 if (VD->hasAttr<BlocksAttr>()) {
2969 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002970 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002971 // offset of __forwarding field
2972 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002973 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002974 Expr.push_back(offset.getQuantity());
2975 Expr.push_back(llvm::dwarf::DW_OP_deref);
2976 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002977 // offset of x field
2978 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002979 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002980
2981 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002982 auto *D = ArgNo
2983 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2984 *ArgNo, Unit, Line, Ty)
2985 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2986 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002987
Guy Benyei11169dd2012-12-18 14:30:41 +00002988 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002989 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2990 llvm::DebugLoc::get(Line, Column, Scope),
2991 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002993 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002994 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002995 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2996 // If VD is an anonymous union then Storage represents value for
2997 // all union fields.
2998 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2999 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003000 // GDB has trouble finding local variables in anonymous unions, so we emit
3001 // artifical local variables for each of the members.
3002 //
3003 // FIXME: Remove this code as soon as GDB supports this.
3004 // The debug info verifier in LLVM operates based on the assumption that a
3005 // variable has the same size as its storage and we had to disable the check
3006 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003007 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003008 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003009 StringRef FieldName = Field->getName();
3010
3011 // Ignore unnamed fields. Do not ignore unnamed records.
3012 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3013 continue;
3014
3015 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003016 auto *D = DBuilder.createAutoVariable(
3017 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3018 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003019
3020 // Insert an llvm.dbg.declare into the current block.
3021 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3022 llvm::DebugLoc::get(Line, Column, Scope),
3023 Builder.GetInsertBlock());
3024 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003025 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003027
3028 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003029 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003030 ArgNo
3031 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3032 Ty, CGM.getLangOpts().Optimize,
3033 Flags)
3034 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3035 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003036
3037 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003038 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3039 llvm::DebugLoc::get(Line, Column, Scope),
3040 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003041}
3042
3043void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3044 llvm::Value *Storage,
3045 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003046 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003047 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003048}
3049
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003050llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3051 llvm::DIType *Ty) {
3052 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003053 if (CachedTy)
3054 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003055 return DBuilder.createObjectPointerType(Ty);
3056}
3057
Eric Christophere7b87e52014-10-26 23:40:33 +00003058void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3059 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003060 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003061 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003063
Craig Topper8a13c412014-05-21 05:09:00 +00003064 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003065 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003066
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003068
Guy Benyei11169dd2012-12-18 14:30:41 +00003069 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003070 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3071 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 if (isByRef)
3073 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003074 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 Ty = getOrCreateType(VD->getType(), Unit);
3076
3077 // Self is passed along as an implicit non-arg variable in a
3078 // block. Mark it as the object pointer.
3079 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003080 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081
3082 // Get location information.
3083 unsigned Line = getLineNumber(VD->getLocation());
3084 unsigned Column = getColumnNumber(VD->getLocation());
3085
3086 const llvm::DataLayout &target = CGM.getDataLayout();
3087
3088 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003089 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3091
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003092 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003093 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003094 addr.push_back(llvm::dwarf::DW_OP_deref);
3095 addr.push_back(llvm::dwarf::DW_OP_plus);
3096 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003098 addr.push_back(llvm::dwarf::DW_OP_deref);
3099 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003100 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003101 offset =
3102 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003103 addr.push_back(offset.getQuantity());
3104 addr.push_back(llvm::dwarf::DW_OP_deref);
3105 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003106 // offset of x field
3107 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003108 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003109 }
3110
3111 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003112 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003113 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003114 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003115
Guy Benyei11169dd2012-12-18 14:30:41 +00003116 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003117 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3118 if (InsertPoint)
3119 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3120 InsertPoint);
3121 else
3122 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3123 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003124}
3125
Guy Benyei11169dd2012-12-18 14:30:41 +00003126void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3127 unsigned ArgNo,
3128 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003129 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003130 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003131}
3132
3133namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003134struct BlockLayoutChunk {
3135 uint64_t OffsetInBits;
3136 const BlockDecl::Capture *Capture;
3137};
3138bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3139 return l.OffsetInBits < r.OffsetInBits;
3140}
Guy Benyei11169dd2012-12-18 14:30:41 +00003141}
3142
3143void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003144 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003145 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003146 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003147 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003148 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 ASTContext &C = CGM.getContext();
3150 const BlockDecl *blockDecl = block.getBlockDecl();
3151
3152 // Collect some general information about the block's location.
3153 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003154 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 unsigned line = getLineNumber(loc);
3156 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003157
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003159 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003160
3161 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003164 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003165 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3166 blockLayout->getElementOffsetInBits(0),
3167 tunit, tunit));
3168 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3169 blockLayout->getElementOffsetInBits(1),
3170 tunit, tunit));
3171 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3172 blockLayout->getElementOffsetInBits(2),
3173 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003174 auto *FnTy = block.getBlockExpr()->getFunctionType();
3175 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3176 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 blockLayout->getElementOffsetInBits(3),
3178 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003179 fields.push_back(createFieldType(
3180 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3181 ? C.getBlockDescriptorExtendedType()
3182 : C.getBlockDescriptorType()),
3183 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
3185 // We want to sort the captures by offset, not because DWARF
3186 // requires this, but because we're paranoid about debuggers.
3187 SmallVector<BlockLayoutChunk, 8> chunks;
3188
3189 // 'this' capture.
3190 if (blockDecl->capturesCXXThis()) {
3191 BlockLayoutChunk chunk;
3192 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003193 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003194 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003195 chunks.push_back(chunk);
3196 }
3197
3198 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003199 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003200 const VarDecl *variable = capture.getVariable();
3201 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3202
3203 // Ignore constant captures.
3204 if (captureInfo.isConstant())
3205 continue;
3206
3207 BlockLayoutChunk chunk;
3208 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003209 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003210 chunk.Capture = &capture;
3211 chunks.push_back(chunk);
3212 }
3213
3214 // Sort by offset.
3215 llvm::array_pod_sort(chunks.begin(), chunks.end());
3216
Eric Christophere7b87e52014-10-26 23:40:33 +00003217 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3218 e = chunks.end();
3219 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 uint64_t offsetInBits = i->OffsetInBits;
3221 const BlockDecl::Capture *capture = i->Capture;
3222
3223 // If we have a null capture, this must be the C++ 'this' capture.
3224 if (!capture) {
3225 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003226 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003227 QualType type = method->getThisType(C);
3228
3229 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3230 offsetInBits, tunit, tunit));
3231 continue;
3232 }
3233
3234 const VarDecl *variable = capture->getVariable();
3235 StringRef name = variable->getName();
3236
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003237 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003238 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003239 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003240
3241 // FIXME: this creates a second copy of this type!
3242 uint64_t xoffset;
3243 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003244 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3245 fieldType =
3246 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3247 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003249 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3250 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 }
3252 fields.push_back(fieldType);
3253 }
3254
3255 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003256 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3257 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003258
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003259 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003260
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003261 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003262 tunit, typeName.str(), tunit, line,
3263 CGM.getContext().toBits(block.BlockSize),
3264 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003265 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3266
3267 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003268 unsigned flags = llvm::DINode::FlagArtificial;
3269 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003270
3271 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003272 auto *debugVar = DBuilder.createParameterVariable(
3273 scope, Arg->getName(), ArgNo, tunit, line, type,
3274 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003275
Adrian Prantl616bef42013-03-14 21:52:59 +00003276 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003277 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003278 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003279 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003280 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003281 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003282
Adrian Prantl616bef42013-03-14 21:52:59 +00003283 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003284 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3285 llvm::DebugLoc::get(line, column, scope),
3286 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003287}
3288
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003289llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003290CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3291 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003292 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003293
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003294 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003295 if (MI != StaticDataMemberCache.end()) {
3296 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003297 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003298 }
David Blaikiece763042013-08-20 21:49:21 +00003299
3300 // If the member wasn't found in the cache, lazily construct and add it to the
3301 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003302 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003303 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003304 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003305}
3306
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003307llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3308 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3309 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3310 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003311
3312 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003314 StringRef FieldName = Field->getName();
3315
3316 // Ignore unnamed fields, but recurse into anonymous records.
3317 if (FieldName.empty()) {
3318 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3319 if (RT)
3320 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3321 Var, DContext);
3322 continue;
3323 }
3324 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003325 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3326 LineNo, FieldTy,
3327 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003328 }
3329 return GV;
3330}
3331
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003332void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003333 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003334 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003335 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003336 llvm::DIFile *Unit = nullptr;
3337 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003338 unsigned LineNo;
3339 StringRef DeclName, LinkageName;
3340 QualType T;
3341 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003342
3343 // Attempt to store one global variable for the declaration - even if we
3344 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003345 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003346
3347 // If this is an anonymous union then we'll want to emit a global
3348 // variable for each member of the anonymous union so that it's possible
3349 // to find the name of any field in the union.
3350 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003351 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003352 assert(RD->isAnonymousStructOrUnion() &&
3353 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003354 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3355 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003356 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003357 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3358 Var->hasInternalLinkage(), Var,
3359 getOrCreateStaticDataMemberDeclarationOrNull(D));
3360 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003361 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003362}
3363
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003364void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3365 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003366 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003367 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003368 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003369 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003370 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003371 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3372 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3373 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3374 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3375 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003376 // Do not use global variables for enums.
3377 //
3378 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003379 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003380 return;
David Blaikiea15565562014-04-04 20:56:17 +00003381 // Do not emit separate definitions for function local const/statics.
3382 if (isa<FunctionDecl>(VD->getDeclContext()))
3383 return;
David Blaikiebb113912014-04-05 07:23:17 +00003384 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003385 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003386 if (VarD->isStaticDataMember()) {
3387 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003388 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003389 // Ensure that the type is retained even though it's otherwise unreferenced.
3390 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003391 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003392 return;
3393 }
3394
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003395 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003396
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003397 auto &GV = DeclCache[VD];
3398 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003399 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003400 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003401 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003402 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003403}
3404
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003405llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003406 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003407 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003408 llvm::DIScope *Mod = getParentModuleOrNull(D);
3409 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003410}
3411
David Blaikie9f88fe82013-04-22 06:13:21 +00003412void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003413 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003414 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003415 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3416 if (!NSDecl->isAnonymousNamespace() ||
3417 CGM.getCodeGenOpts().DebugExplicitImport) {
3418 DBuilder.createImportedModule(
3419 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3420 getOrCreateNameSpace(NSDecl),
3421 getLineNumber(UD.getLocation()));
3422 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003423}
3424
David Blaikiebd483762013-05-20 04:58:53 +00003425void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003426 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003427 return;
3428 assert(UD.shadow_size() &&
3429 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3430 // Emitting one decl is sufficient - debuggers can detect that this is an
3431 // overloaded name & provide lookup for all the overloads.
3432 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003433 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003434 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003435 DBuilder.createImportedDeclaration(
3436 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3437 getLineNumber(USD.getLocation()));
3438}
3439
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003440void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantl8a634c12015-12-18 19:44:31 +00003441 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003442 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003443 DBuilder.createImportedDeclaration(
3444 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3445 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3446 getLineNumber(ID.getLocation()));
3447 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003448}
3449
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003450llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003451CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003452 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003453 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003454 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003455 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003456 return cast<llvm::DIImportedEntity>(VH);
3457 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003458 if (const NamespaceAliasDecl *Underlying =
3459 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3460 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003461 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003462 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3463 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3464 NA.getName());
3465 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003466 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003467 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3468 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3469 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003470 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003471 return R;
3472}
3473
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003474llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003475CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003476 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003477 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003478 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003479 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003480
Guy Benyei11169dd2012-12-18 14:30:41 +00003481 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003482 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003483 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003485 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003486 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003487 return NS;
3488}
3489
Adrian Prantla5206ce2015-09-22 23:26:43 +00003490void CGDebugInfo::setDwoId(uint64_t Signature) {
3491 assert(TheCU && "no main compile unit");
3492 TheCU->setDWOId(Signature);
3493}
3494
3495
Guy Benyei11169dd2012-12-18 14:30:41 +00003496void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003497 // Creating types might create further types - invalidating the current
3498 // element and the size(), so don't cache/reference them.
3499 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3500 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003501 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003502 ? CreateTypeDefinition(E.Type, E.Unit)
3503 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003504 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003505 }
3506
David Blaikief427b002014-05-06 03:42:01 +00003507 for (auto p : ReplaceMap) {
3508 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003509 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003510 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003511
David Blaikief427b002014-05-06 03:42:01 +00003512 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003513 assert(it != TypeCache.end());
3514 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003516 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3517 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003518 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003519
Frederic Rissd253ed62014-11-18 03:40:51 +00003520 for (const auto &p : FwdDeclReplaceMap) {
3521 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003522 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003523 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003524
3525 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003526 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003527 // with ourselves, that will destroy the temporary MDNode and
3528 // replace it with a standard one, avoiding leaking memory.
3529 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003530 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003531 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003532 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003533
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003534 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003535 }
3536
Adrian Prantl73409ce2013-03-11 18:33:46 +00003537 // We keep our own list of retained types, because we need to look
3538 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003539 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003540 if (auto MD = TypeCache[RT])
3541 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003542
Guy Benyei11169dd2012-12-18 14:30:41 +00003543 DBuilder.finalize();
3544}
David Blaikie66088d52014-09-24 17:01:27 +00003545
3546void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003547 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003548 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003549
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003550 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003551 // Don't ignore in case of explicit cast where it is referenced indirectly.
3552 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003553}