blob: 78e3978e0ff986bd7546a38069794439eb3807e7 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
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,
Diego Novillo913690c2014-06-24 17:02:17 +0000405 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000406 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000407 : llvm::DIBuilder::FullDebug,
Adrian Prantlb67dbce2015-09-11 18:45:02 +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
466 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000467 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000468 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000469 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000470 OCLImage1dArrayDITy);
471 case BuiltinType::OCLImage1dBuffer:
472 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
473 OCLImage1dBufferDITy);
474 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000475 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000476 case BuiltinType::OCLImage2dArray:
477 return getOrCreateStructPtrType("opencl_image2d_array_t",
478 OCLImage2dArrayDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000479 case BuiltinType::OCLImage2dDepth:
480 return getOrCreateStructPtrType("opencl_image2d_depth_t",
481 OCLImage2dDepthDITy);
482 case BuiltinType::OCLImage2dArrayDepth:
483 return getOrCreateStructPtrType("opencl_image2d_array_depth_t",
484 OCLImage2dArrayDepthDITy);
485 case BuiltinType::OCLImage2dMSAA:
486 return getOrCreateStructPtrType("opencl_image2d_msaa_t",
487 OCLImage2dMSAADITy);
488 case BuiltinType::OCLImage2dArrayMSAA:
489 return getOrCreateStructPtrType("opencl_image2d_array_msaa_t",
490 OCLImage2dArrayMSAADITy);
491 case BuiltinType::OCLImage2dMSAADepth:
492 return getOrCreateStructPtrType("opencl_image2d_msaa_depth_t",
493 OCLImage2dMSAADepthDITy);
494 case BuiltinType::OCLImage2dArrayMSAADepth:
495 return getOrCreateStructPtrType("opencl_image2d_array_msaa_depth_t",
496 OCLImage2dArrayMSAADepthDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000497 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000498 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000499 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000500 return DBuilder.createBasicType(
501 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
502 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000503 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000504 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000505 case BuiltinType::OCLClkEvent:
506 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
507 case BuiltinType::OCLQueue:
508 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
509 case BuiltinType::OCLNDRange:
510 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
511 case BuiltinType::OCLReserveID:
512 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000513
Guy Benyei11169dd2012-12-18 14:30:41 +0000514 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000515 case BuiltinType::Char_U:
516 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
517 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000519 case BuiltinType::SChar:
520 Encoding = llvm::dwarf::DW_ATE_signed_char;
521 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000523 case BuiltinType::Char32:
524 Encoding = llvm::dwarf::DW_ATE_UTF;
525 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 case BuiltinType::UShort:
527 case BuiltinType::UInt:
528 case BuiltinType::UInt128:
529 case BuiltinType::ULong:
530 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000531 case BuiltinType::ULongLong:
532 Encoding = llvm::dwarf::DW_ATE_unsigned;
533 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 case BuiltinType::Short:
535 case BuiltinType::Int:
536 case BuiltinType::Int128:
537 case BuiltinType::Long:
538 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 case BuiltinType::LongLong:
540 Encoding = llvm::dwarf::DW_ATE_signed;
541 break;
542 case BuiltinType::Bool:
543 Encoding = llvm::dwarf::DW_ATE_boolean;
544 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 case BuiltinType::Half:
546 case BuiltinType::Float:
547 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000548 case BuiltinType::Double:
549 Encoding = llvm::dwarf::DW_ATE_float;
550 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 }
552
553 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000554 case BuiltinType::Long:
555 BTName = "long int";
556 break;
557 case BuiltinType::LongLong:
558 BTName = "long long int";
559 break;
560 case BuiltinType::ULong:
561 BTName = "long unsigned int";
562 break;
563 case BuiltinType::ULongLong:
564 BTName = "long long unsigned int";
565 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 default:
567 BTName = BT->getName(CGM.getLangOpts());
568 break;
569 }
570 // Bit size, align and offset of the type.
571 uint64_t Size = CGM.getContext().getTypeSize(BT);
572 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000573 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000574}
575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000576llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000577 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000578 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 if (Ty->isComplexIntegerType())
580 Encoding = llvm::dwarf::DW_ATE_lo_user;
581
582 uint64_t Size = CGM.getContext().getTypeSize(Ty);
583 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000584 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000585}
586
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000587llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
588 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 QualifierCollector Qc;
590 const Type *T = Qc.strip(Ty);
591
592 // Ignore these qualifiers for now.
593 Qc.removeObjCGCAttr();
594 Qc.removeAddressSpace();
595 Qc.removeObjCLifetime();
596
597 // We will create one Derived type for one qualifier and recurse to handle any
598 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000599 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000600 if (Qc.hasConst()) {
601 Tag = llvm::dwarf::DW_TAG_const_type;
602 Qc.removeConst();
603 } else if (Qc.hasVolatile()) {
604 Tag = llvm::dwarf::DW_TAG_volatile_type;
605 Qc.removeVolatile();
606 } else if (Qc.hasRestrict()) {
607 Tag = llvm::dwarf::DW_TAG_restrict_type;
608 Qc.removeRestrict();
609 } else {
610 assert(Qc.empty() && "Unknown type qualifier for debug info");
611 return getOrCreateType(QualType(T, 0), Unit);
612 }
613
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000614 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000615
616 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
617 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000618 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000619}
620
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000621llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
622 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000623
624 // The frontend treats 'id' as a typedef to an ObjCObjectType,
625 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
626 // debug info, we want to emit 'id' in both cases.
627 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000628 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000629
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000630 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
631 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000632}
633
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000634llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
635 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000636 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000637 Ty->getPointeeType(), Unit);
638}
639
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000640/// \return whether a C++ mangling exists for the type defined by TD.
641static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
642 switch (TheCU->getSourceLanguage()) {
643 case llvm::dwarf::DW_LANG_C_plus_plus:
644 return true;
645 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
646 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
647 default:
648 return false;
649 }
650}
651
Manman Rene0064d82013-08-29 23:19:58 +0000652/// In C++ mode, types have linkage, so we can rely on the ODR and
653/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000654static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
655 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000656 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000657 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000658 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000659
660 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000661 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000662
Manman Rene0064d82013-08-29 23:19:58 +0000663 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
664 if (CGM.getTarget().getCXXABI().isMicrosoft())
665 return FullName;
666
667 // TODO: This is using the RTTI name. Is there a better way to get
668 // a unique string for a type?
669 llvm::raw_svector_ostream Out(FullName);
670 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000671 return FullName;
672}
673
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000674/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000675static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
676 llvm::dwarf::Tag Tag;
677 if (RD->isStruct() || RD->isInterface())
678 Tag = llvm::dwarf::DW_TAG_structure_type;
679 else if (RD->isUnion())
680 Tag = llvm::dwarf::DW_TAG_union_type;
681 else {
682 // FIXME: This could be a struct type giving a default visibility different
683 // than C++ class type, but needs llvm metadata changes first.
684 assert(RD->isClass());
685 Tag = llvm::dwarf::DW_TAG_class_type;
686 }
687 return Tag;
688}
689
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000690llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000691CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000693 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000694 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
695 return cast<llvm::DICompositeType>(T);
696 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000697 unsigned Line = getLineNumber(RD->getLocation());
698 StringRef RDName = getClassName(RD);
699
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000700 uint64_t Size = 0;
701 uint64_t Align = 0;
702
703 const RecordDecl *D = RD->getDefinition();
704 if (D && D->isCompleteDefinition()) {
705 Size = CGM.getContext().getTypeSize(Ty);
706 Align = CGM.getContext().getTypeAlign(Ty);
707 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000708
709 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000710 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000711 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000712 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000713 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000714 ReplaceMap.emplace_back(
715 std::piecewise_construct, std::make_tuple(Ty),
716 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000717 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000718}
719
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000720llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000721 const Type *Ty,
722 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000724 // Bit size, align and offset of the type.
725 // Size is always the size of a pointer. We can't use getTypeSize here
726 // because that does not return the correct value for references.
727 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000728 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000729 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
730
Keno Fischer87842f32015-11-16 09:04:13 +0000731 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
732 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
733 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
734 Size, Align);
735 else
736 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
737 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000738}
739
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000740llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
741 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000742 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000743 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000744 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
745 TheCU, getOrCreateMainFile(), 0);
746 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
747 Cache = DBuilder.createPointerType(Cache, Size);
748 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000749}
750
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000751llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
752 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000753 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000754 QualType FType;
755 uint64_t FieldSize, FieldOffset;
756 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000757 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000758
759 FieldOffset = 0;
760 FType = CGM.getContext().UnsignedLongTy;
761 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
762 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
763
764 Elements = DBuilder.getOrCreateArray(EltTys);
765 EltTys.clear();
766
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000767 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000768 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000769
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000770 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000771 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000772 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000773
774 // Bit size, align and offset of the type.
775 uint64_t Size = CGM.getContext().getTypeSize(Ty);
776
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000777 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000778
779 FieldOffset = 0;
780 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
781 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
782 FType = CGM.getContext().IntTy;
783 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
784 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000785 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000786 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
787
788 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000789 FieldSize = CGM.getContext().getTypeSize(Ty);
790 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000791 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000792 FieldSize, FieldAlign, FieldOffset,
793 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000794
795 FieldOffset += FieldSize;
796 Elements = DBuilder.getOrCreateArray(EltTys);
797
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000798 // The __block_literal_generic structs are marked with a special
799 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
800 // the debugger needs to know about. To allow type uniquing, emit
801 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000802 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000803 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000804 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000805
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000806 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000807}
808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000809llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
810 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000811 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000813
814 SmallString<128> NS;
815 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000816 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
817 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000818
819 TemplateSpecializationType::PrintTemplateArgumentList(
820 OS, Ty->getArgs(), Ty->getNumArgs(),
821 CGM.getContext().getPrintingPolicy());
822
Eric Christophere7b87e52014-10-26 23:40:33 +0000823 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
824 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000825
826 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000827 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
828 getLineNumber(Loc),
829 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000830}
831
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000832llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
833 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000834 // We don't set size information, but do specify where the typedef was
835 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000836 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000837
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000838 // Typedefs are derived from some other type.
839 return DBuilder.createTypedef(
840 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
841 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000842 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000843}
844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
846 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000847 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000848
849 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000850 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000851
852 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000853 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 if (isa<FunctionNoProtoType>(Ty))
855 EltTys.push_back(DBuilder.createUnspecifiedParameter());
856 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000857 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
858 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000859 if (FPT->isVariadic())
860 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000861 }
862
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000863 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +0000864 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +0000865}
866
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000867/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000868/// As an optimization, return 0 if the access specifier equals the
869/// default for the containing type.
870static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
871 AccessSpecifier Default = clang::AS_none;
872 if (RD && RD->isClass())
873 Default = clang::AS_private;
874 else if (RD && (RD->isStruct() || RD->isUnion()))
875 Default = clang::AS_public;
876
877 if (Access == Default)
878 return 0;
879
Eric Christophere7b87e52014-10-26 23:40:33 +0000880 switch (Access) {
881 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000882 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000883 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000884 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000885 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000886 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000887 case clang::AS_none:
888 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000889 }
890 llvm_unreachable("unexpected access enumerator");
891}
Guy Benyei11169dd2012-12-18 14:30:41 +0000892
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000893llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000894 StringRef name, QualType type, uint64_t sizeInBitsOverride,
895 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000896 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
897 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000898
899 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000900 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000901 unsigned line = getLineNumber(loc);
902
David Majnemer34b57492014-07-30 01:30:47 +0000903 uint64_t SizeInBits = 0;
904 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000905 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000906 TypeInfo TI = CGM.getContext().getTypeInfo(type);
907 SizeInBits = TI.Width;
908 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000909
910 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000911 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000912 }
913
Adrian Prantl21361fb2014-08-29 22:44:27 +0000914 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000915 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
916 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000917}
918
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000919void CGDebugInfo::CollectRecordLambdaFields(
920 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000921 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000922 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
923 // has the name and the location of the variable so we should iterate over
924 // both concurrently.
925 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
926 RecordDecl::field_iterator Field = CXXDecl->field_begin();
927 unsigned fieldno = 0;
928 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000929 E = CXXDecl->captures_end();
930 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000931 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000932 if (C.capturesVariable()) {
933 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000934 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000935 StringRef VName = V->getName();
936 uint64_t SizeInBitsOverride = 0;
937 if (Field->isBitField()) {
938 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
939 assert(SizeInBitsOverride && "found named 0-width bitfield");
940 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000942 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
943 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
944 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000945 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000946 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000947 // TODO: Need to handle 'this' in some way by probably renaming the
948 // this of the lambda class and having a field member of 'this' or
949 // by using AT_object_pointer for the function and having that be
950 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000951 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000952 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000953 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000955 "this", type, 0, f->getLocation(), f->getAccess(),
956 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000957
958 elements.push_back(fieldType);
959 }
960 }
961}
962
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000963llvm::DIDerivedType *
964CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000965 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000966 // Create the descriptor for the static variable, with or without
967 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000968 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000969 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
970 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000971
Eric Christopher91a31902013-01-16 01:22:32 +0000972 unsigned LineNumber = getLineNumber(Var->getLocation());
973 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000974 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000975 if (Var->getInit()) {
976 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000977 if (Value) {
978 if (Value->isInt())
979 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
980 if (Value->isFloat())
981 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
982 }
Eric Christopher91a31902013-01-16 01:22:32 +0000983 }
984
Adrian Prantl21361fb2014-08-29 22:44:27 +0000985 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000986 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000987 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000988 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000989 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000990}
991
Eric Christophere7b87e52014-10-26 23:40:33 +0000992void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000993 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
994 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000995 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000996 StringRef name = field->getName();
997 QualType type = field->getType();
998
999 // Ignore unnamed fields unless they're anonymous structs/unions.
1000 if (name.empty() && !type->isRecordType())
1001 return;
1002
1003 uint64_t SizeInBitsOverride = 0;
1004 if (field->isBitField()) {
1005 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
1006 assert(SizeInBitsOverride && "found named 0-width bitfield");
1007 }
1008
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001009 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001010 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
1011 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001012
1013 elements.push_back(fieldType);
1014}
1015
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001016void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001017 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001018 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001019 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001020 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1021
Eric Christopher91a31902013-01-16 01:22:32 +00001022 if (CXXDecl && CXXDecl->isLambda())
1023 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1024 else {
1025 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001026
Eric Christopher91a31902013-01-16 01:22:32 +00001027 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001028 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001029
Eric Christopher91a31902013-01-16 01:22:32 +00001030 // Static and non-static members should appear in the same order as
1031 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001032 for (const auto *I : record->decls())
1033 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001034 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001035 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001036 if (MI != StaticDataMemberCache.end()) {
1037 assert(MI->second &&
1038 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001039 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001040 } else {
1041 auto Field = CreateRecordStaticField(V, RecordTy, record);
1042 elements.push_back(Field);
1043 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001044 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001045 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1046 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001047
1048 // Bump field number for next field.
1049 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001051 }
1052}
1053
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001054llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001055CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001057 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001058 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001059 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001060 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001061 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1062 Func, Unit);
1063}
David Blaikie2aaf0652013-01-07 22:24:59 +00001064
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001065llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1066 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001067 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001068 llvm::DITypeRefArray Args(
1069 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001070 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001071 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001072
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001073 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001074
1075 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001076 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001077
David Blaikie2aaf0652013-01-07 22:24:59 +00001078 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001079 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001080 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1081 // Create pointer type directly in this case.
1082 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1083 QualType PointeeTy = ThisPtrTy->getPointeeType();
1084 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001085 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001086 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001087 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1088 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001089 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001090 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001091 // TODO: This and the artificial type below are misleading, the
1092 // types aren't artificial the argument is, but the current
1093 // metadata doesn't represent that.
1094 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1095 Elts.push_back(ThisPtrType);
1096 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001097 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001098 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001099 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1100 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001101 }
1102
1103 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001104 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1105 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001106
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001107 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001108
Adrian Prantl0630eb72013-12-18 21:48:18 +00001109 unsigned Flags = 0;
1110 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001111 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001112 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001113 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001114
Eric Christopher28a6db52015-10-15 06:56:08 +00001115 return DBuilder.createSubroutineType(EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001116}
1117
Eric Christopherb2a008c2013-05-16 00:45:12 +00001118/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001119/// inside a function.
1120static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1121 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1122 return isFunctionLocalClass(NRD);
1123 if (isa<FunctionDecl>(RD->getDeclContext()))
1124 return true;
1125 return false;
1126}
1127
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001128llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1129 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001130 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001131 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001132
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001134 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001135
1136 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1137 // make sense to give a single ctor/dtor a linkage name.
1138 StringRef MethodLinkageName;
1139 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1140 MethodLinkageName = CGM.getMangledName(Method);
1141
1142 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001143 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001144 unsigned MethodLine = 0;
1145 if (!Method->isImplicit()) {
1146 MethodDefUnit = getOrCreateFile(Method->getLocation());
1147 MethodLine = getLineNumber(Method->getLocation());
1148 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001149
1150 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001151 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001152 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001153 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001154
Guy Benyei11169dd2012-12-18 14:30:41 +00001155 if (Method->isVirtual()) {
1156 if (Method->isPure())
1157 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1158 else
1159 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001160
Guy Benyei11169dd2012-12-18 14:30:41 +00001161 // It doesn't make sense to give a virtual destructor a vtable index,
1162 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001163 // FIXME: Add proper support for debug info for virtual calls in
1164 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1165 // lookup if we have multiple or virtual inheritance.
1166 if (!isa<CXXDestructorDecl>(Method) &&
1167 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001168 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001169 ContainingType = RecordTy;
1170 }
1171
1172 unsigned Flags = 0;
1173 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001175 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001176 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1177 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001178 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001179 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001180 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001181 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001182 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001183 }
1184 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001185 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001186 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001188 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001189 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001190
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001191 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1192 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001193 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1194 MethodTy, /*isLocalToUnit=*/false,
1195 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001196 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001197
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001198 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001199
1200 return SP;
1201}
1202
Eric Christophere7b87e52014-10-26 23:40:33 +00001203void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001204 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1205 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001206
1207 // Since we want more than just the individual member decls if we
1208 // have templated functions iterate over every declaration to gather
1209 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001210 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001211 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1212 // If the member is implicit, don't add it to the member list. This avoids
1213 // the member being added to type units by LLVM, while still allowing it
1214 // to be emitted into the type declaration/reference inside the compile
1215 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001216 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001217 // FIXME: Handle Using(Shadow?)Decls here to create
1218 // DW_TAG_imported_declarations inside the class for base decls brought into
1219 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1220 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1221 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001222 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001223 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001224
1225 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1226 continue;
1227
David Blaikiefd580722014-10-06 05:18:55 +00001228 // Reuse the existing member function declaration if it exists.
1229 // It may be associated with the declaration of the type & should be
1230 // reused as we're building the definition.
1231 //
1232 // This situation can arise in the vtable-based debug info reduction where
1233 // implicit members are emitted in a non-vtable TU.
1234 auto MI = SPCache.find(Method->getCanonicalDecl());
1235 EltTys.push_back(MI == SPCache.end()
1236 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001237 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001239}
Guy Benyei11169dd2012-12-18 14:30:41 +00001240
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001241void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001242 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001243 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001245 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 unsigned BFlags = 0;
1247 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001248
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001250 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001251
Aaron Ballman574705e2014-03-13 15:41:46 +00001252 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001253 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1254 // virtual base offset offset is -ve. The code generator emits dwarf
1255 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001256 BaseOffset = 0 - CGM.getItaniumVTableContext()
1257 .getVirtualBaseOffsetOffset(RD, Base)
1258 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001259 } else {
1260 // In the MS ABI, store the vbtable offset, which is analogous to the
1261 // vbase offset offset in Itanium.
1262 BaseOffset =
1263 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1264 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001266 } else
1267 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1268 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1269 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001270
Adrian Prantl21361fb2014-08-29 22:44:27 +00001271 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001272 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001273 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001274 EltTys.push_back(DTy);
1275 }
1276}
1277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001279CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1280 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001281 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001282 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001283 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1284 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001285 StringRef Name;
1286 if (TPList)
1287 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001288 switch (TA.getKind()) {
1289 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001290 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001291 TemplateParams.push_back(
1292 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001293 } break;
1294 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001295 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001296 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1297 TheCU, Name, TTy,
1298 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001299 } break;
1300 case TemplateArgument::Declaration: {
1301 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001302 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001303 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001304 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001305 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001306 // Variable pointer template parameters have a value that is the address
1307 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001308 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001309 V = CGM.GetAddrOfGlobalVar(VD);
1310 // Member function pointers have special support for building them, though
1311 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001312 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001313 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001314 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001315 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001316 // Member data pointers have special handling too to compute the fixed
1317 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001318 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001319 // These five lines (& possibly the above member function pointer
1320 // handling) might be able to be refactored to use similar code in
1321 // CodeGenModule::getMemberPointerConstant
1322 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1323 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001324 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001325 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001326 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001327 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1328 TheCU, Name, TTy,
1329 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001330 } break;
1331 case TemplateArgument::NullPtr: {
1332 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001334 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001335 // Special case member data pointer null values since they're actually -1
1336 // instead of zero.
1337 if (const MemberPointerType *MPT =
1338 dyn_cast<MemberPointerType>(T.getTypePtr()))
1339 // But treat member function pointers as simple zero integers because
1340 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1341 // CodeGen grows handling for values of non-null member function
1342 // pointers then perhaps we could remove this special case and rely on
1343 // EmitNullMemberPointer for member function pointers.
1344 if (MPT->isMemberDataPointer())
1345 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1346 if (!V)
1347 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001348 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1349 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001350 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001351 case TemplateArgument::Template:
1352 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001353 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001354 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1355 break;
1356 case TemplateArgument::Pack:
1357 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1358 TheCU, Name, nullptr,
1359 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1360 break;
David Majnemer5559d472013-08-24 08:21:10 +00001361 case TemplateArgument::Expression: {
1362 const Expr *E = TA.getAsExpr();
1363 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001364 if (E->isGLValue())
1365 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001366 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001367 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001368 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001369 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1370 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001371 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001372 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001373 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001374 case TemplateArgument::Null:
1375 llvm_unreachable(
1376 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001377 }
1378 }
1379 return DBuilder.getOrCreateArray(TemplateParams);
1380}
1381
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001382llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001383CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001384 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 if (FD->getTemplatedKind() ==
1386 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001387 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1388 ->getTemplate()
1389 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001390 return CollectTemplateParams(
1391 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001393 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001394}
1395
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001396llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1397 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001398 // Always get the full list of parameters, not just the ones from
1399 // the specialization.
1400 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001401 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001402 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001403 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001404}
1405
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001406llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001407 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 return VTablePtrType;
1409
1410 ASTContext &Context = CGM.getContext();
1411
1412 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001413 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001415 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001416 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001417 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001418 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001419 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1420 return VTablePtrType;
1421}
1422
Guy Benyei11169dd2012-12-18 14:30:41 +00001423StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001424 // Copy the gdb compatible name on the side and use its reference.
1425 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001426}
1427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001428void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001429 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001430 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1431
1432 // If there is a primary base then it will hold vtable info.
1433 if (RL.getPrimaryBase())
1434 return;
1435
1436 // If this class is not dynamic then there is not any vtable info to collect.
1437 if (!RD->isDynamicClass())
1438 return;
1439
1440 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001442 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 EltTys.push_back(VPTR);
1445}
1446
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001448 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001449 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001450 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001451 return T;
1452}
1453
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001454llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001455 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001456 return getOrCreateStandaloneType(D, Loc);
1457}
1458
1459llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1460 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001461 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001462 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001463 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001464 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001465
1466 // Composite types with UIDs were already retained by DIBuilder
1467 // because they are only referenced by name in the IR.
1468 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1469 if (!CTy->getIdentifier().empty())
1470 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001471 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001472 return T;
1473}
1474
David Blaikie483a9da2014-05-06 18:35:21 +00001475void CGDebugInfo::completeType(const EnumDecl *ED) {
1476 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1477 return;
1478 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001479 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001480 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001481 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001482 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001483 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001484 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001485 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001486}
1487
David Blaikieb2e86eb2013-08-15 20:49:17 +00001488void CGDebugInfo::completeType(const RecordDecl *RD) {
1489 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1490 !CGM.getLangOpts().CPlusPlus)
1491 completeRequiredType(RD);
1492}
1493
1494void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001495 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1496 return;
1497
David Blaikie6943dea2013-08-20 01:28:15 +00001498 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1499 if (CXXDecl->isDynamicClass())
1500 return;
1501
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001502 if (DebugTypeExtRefs && RD->isFromASTFile())
1503 return;
1504
David Blaikieb2e86eb2013-08-15 20:49:17 +00001505 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001506 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001507 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001508 completeClassData(RD);
1509}
1510
1511void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1512 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001513 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001514 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001515 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001516 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001517 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001518 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001519 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001520 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001521 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001522}
1523
David Blaikie0e716b42014-03-03 23:48:23 +00001524static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1525 CXXRecordDecl::method_iterator End) {
1526 for (; I != End; ++I)
1527 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001528 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1529 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001530 return true;
1531 return false;
1532}
1533
1534static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001535 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001536 const RecordDecl *RD,
1537 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001538 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001539 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001540 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001541
David Blaikie0e716b42014-03-03 23:48:23 +00001542 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1543 return false;
1544
1545 if (!LangOpts.CPlusPlus)
1546 return false;
1547
1548 if (!RD->isCompleteDefinitionRequired())
1549 return true;
1550
1551 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1552
1553 if (!CXXDecl)
1554 return false;
1555
1556 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1557 return true;
1558
1559 TemplateSpecializationKind Spec = TSK_Undeclared;
1560 if (const ClassTemplateSpecializationDecl *SD =
1561 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1562 Spec = SD->getSpecializationKind();
1563
1564 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1565 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1566 CXXDecl->method_end()))
1567 return true;
1568
1569 return false;
1570}
1571
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001572llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001574 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001575 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1576 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001577 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001578 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001579 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001580 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001581
David Blaikieb2e86eb2013-08-15 20:49:17 +00001582 return CreateTypeDefinition(Ty);
1583}
1584
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001585llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001586 RecordDecl *RD = Ty->getDecl();
1587
Guy Benyei11169dd2012-12-18 14:30:41 +00001588 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001589 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001590
1591 // Records and classes and unions can all be recursive. To handle them, we
1592 // first generate a debug descriptor for the struct as a forward declaration.
1593 // Then (if it is a definition) we go through and get debug info for all of
1594 // its members. Finally, we create a descriptor for the complete type (which
1595 // may refer to the forward decl if the struct is recursive) and replace all
1596 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001597 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001599 const RecordDecl *D = RD->getDefinition();
1600 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 return FwdDecl;
1602
David Blaikieadfbf992013-08-18 16:55:33 +00001603 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1604 CollectContainingType(CXXDecl, FwdDecl);
1605
Guy Benyei11169dd2012-12-18 14:30:41 +00001606 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001607 LexicalBlockStack.emplace_back(&*FwdDecl);
1608 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001609
Guy Benyei11169dd2012-12-18 14:30:41 +00001610 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001611 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001612 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001613
1614 // Note: The split of CXXDecl information here is intentional, the
1615 // gdb tests will depend on a certain ordering at printout. The debug
1616 // information offsets are still correct if we merge them all together
1617 // though.
1618 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1619 if (CXXDecl) {
1620 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1621 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1622 }
1623
Eric Christopher91a31902013-01-16 01:22:32 +00001624 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001625 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001626 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001627 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001628
1629 LexicalBlockStack.pop_back();
1630 RegionMap.erase(Ty->getDecl());
1631
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001632 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001633 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001634
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001635 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001636 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001637 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001638
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001639 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001640 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001641}
1642
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001643llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1644 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 // Ignore protocols.
1646 return getOrCreateType(Ty->getBaseType(), Unit);
1647}
1648
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001649/// \return true if Getter has the default name for the property PD.
1650static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1651 const ObjCMethodDecl *Getter) {
1652 assert(PD);
1653 if (!Getter)
1654 return true;
1655
1656 assert(Getter->getDeclName().isObjCZeroArgSelector());
1657 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001658 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001659}
1660
1661/// \return true if Setter has the default name for the property PD.
1662static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1663 const ObjCMethodDecl *Setter) {
1664 assert(PD);
1665 if (!Setter)
1666 return true;
1667
1668 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001669 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001670 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001671}
1672
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001673llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1674 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001675 ObjCInterfaceDecl *ID = Ty->getDecl();
1676 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001677 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001679 // Return a forward declaration if this type was imported from a clang module.
1680 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1681 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1682 ID->getName(),
1683 getDeclContextDescriptor(ID), Unit, 0);
1684
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001686 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001688 auto RuntimeLang =
1689 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001690
1691 // If this is just a forward declaration return a special forward-declaration
1692 // debug type since we won't be able to lay out the entire type.
1693 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001694 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001695 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001696 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001697 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1698 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001699 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 return FwdDecl;
1701 }
1702
David Blaikieef8a9512014-05-05 23:23:53 +00001703 return CreateTypeDefinition(Ty, Unit);
1704}
1705
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001706llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001707CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1708 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001709 // Use the Module pointer as the key into the cache. This is a
1710 // nullptr if the "Module" is a PCH, which is safe because we don't
1711 // support chained PCH debug info, so there can only be a single PCH.
1712 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001713 auto ModRef = ModuleCache.find(M);
1714 if (ModRef != ModuleCache.end())
1715 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001716
1717 // Macro definitions that were defined with "-D" on the command line.
1718 SmallString<128> ConfigMacros;
1719 {
1720 llvm::raw_svector_ostream OS(ConfigMacros);
1721 const auto &PPOpts = CGM.getPreprocessorOpts();
1722 unsigned I = 0;
1723 // Translate the macro definitions back into a commmand line.
1724 for (auto &M : PPOpts.Macros) {
1725 if (++I > 1)
1726 OS << " ";
1727 const std::string &Macro = M.first;
1728 bool Undef = M.second;
1729 OS << "\"-" << (Undef ? 'U' : 'D');
1730 for (char c : Macro)
1731 switch (c) {
1732 case '\\' : OS << "\\\\"; break;
1733 case '"' : OS << "\\\""; break;
1734 default: OS << c;
1735 }
1736 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001737 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001738 }
Adrian Prantl66689202015-09-18 23:01:45 +00001739
Adrian Prantl835e6632015-09-24 16:10:10 +00001740 bool IsRootModule = M ? !M->Parent : true;
1741 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantl66689202015-09-18 23:01:45 +00001742 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001743 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001744 Mod.getPath(), TheCU->getProducer(), true,
1745 StringRef(), 0, Mod.getASTFile(),
1746 llvm::DIBuilder::FullDebug, Mod.getSignature());
Adrian Prantl66689202015-09-18 23:01:45 +00001747 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001748 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001749 llvm::DIModule *Parent =
1750 IsRootModule ? nullptr
1751 : getOrCreateModuleRef(
1752 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1753 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001754 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001755 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1756 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001757 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001758 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001759}
1760
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001761llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1762 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001763 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001764 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001765 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001766 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001767
1768 // Bit size, align and offset of the type.
1769 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1770 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1771
1772 unsigned Flags = 0;
1773 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001774 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001775
Adrian Prantlfd696112015-10-01 00:48:51 +00001776 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001777 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001778 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1779 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001780
David Blaikieef8a9512014-05-05 23:23:53 +00001781 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001782 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001783
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001784 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001785 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001786 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001787
1788 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001789 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001790
1791 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1792 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001793 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001794 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001795 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001796 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001797
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001798 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 EltTys.push_back(InhTag);
1800 }
1801
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001802 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001803 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001805 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001806 unsigned PLine = getLineNumber(Loc);
1807 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1808 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001809 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1810 PD->getName(), PUnit, PLine,
1811 hasDefaultGetterName(PD, Getter) ? ""
1812 : getSelectorName(PD->getGetterName()),
1813 hasDefaultSetterName(PD, Setter) ? ""
1814 : getSelectorName(PD->getSetterName()),
1815 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001817 };
1818 {
1819 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001820 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
1821 for (auto *PD : ClassExt->properties()) {
1822 PropertySet.insert(PD->getIdentifier());
1823 AddProperty(PD);
1824 }
Nico Weber7123bca2015-12-04 19:14:14 +00001825 for (const auto *PD : ID->properties()) {
1826 // Don't emit duplicate metadata for properties that were already in a
1827 // class extension.
1828 if (!PropertySet.insert(PD->getIdentifier()).second)
1829 continue;
1830 AddProperty(PD);
1831 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 }
1833
1834 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1835 unsigned FieldNo = 0;
1836 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1837 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001838 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001839 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001840 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001841
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 StringRef FieldName = Field->getName();
1843
1844 // Ignore unnamed fields.
1845 if (FieldName.empty())
1846 continue;
1847
1848 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001849 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001850 unsigned FieldLine = getLineNumber(Field->getLocation());
1851 QualType FType = Field->getType();
1852 uint64_t FieldSize = 0;
1853 unsigned FieldAlign = 0;
1854
1855 if (!FType->isIncompleteArrayType()) {
1856
1857 // Bit size, align and offset of the type.
1858 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001859 ? Field->getBitWidthValue(CGM.getContext())
1860 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 FieldAlign = CGM.getContext().getTypeAlign(FType);
1862 }
1863
1864 uint64_t FieldOffset;
1865 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1866 // We don't know the runtime offset of an ivar if we're using the
1867 // non-fragile ABI. For bitfields, use the bit offset into the first
1868 // byte of storage of the bitfield. For other fields, use zero.
1869 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001870 FieldOffset =
1871 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872 FieldOffset %= CGM.getContext().getCharWidth();
1873 } else {
1874 FieldOffset = 0;
1875 }
1876 } else {
1877 FieldOffset = RL.getFieldOffset(FieldNo);
1878 }
1879
1880 unsigned Flags = 0;
1881 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001882 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001884 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001885 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001886 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001887
Craig Topper8a13c412014-05-21 05:09:00 +00001888 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001890 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001891 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001892 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001893 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001894 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001895 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001896 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1897 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 PropertyNode = DBuilder.createObjCProperty(
1899 PD->getName(), PUnit, PLine,
1900 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1901 PD->getGetterName()),
1902 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1903 PD->getSetterName()),
1904 PD->getPropertyAttributes(),
1905 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 }
1907 }
1908 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001909 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1910 FieldSize, FieldAlign, FieldOffset, Flags,
1911 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001912 EltTys.push_back(FieldTy);
1913 }
1914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001915 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001916 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001917
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001919 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001920}
1921
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001922llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1923 llvm::DIFile *Unit) {
1924 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001925 int64_t Count = Ty->getNumElements();
1926 if (Count == 0)
1927 // If number of elements are not known then this is an unbounded array.
1928 // Use Count == -1 to express such arrays.
1929 Count = -1;
1930
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001931 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001932 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001933
1934 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1935 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1936
1937 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1938}
1939
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001940llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 uint64_t Size;
1942 uint64_t Align;
1943
1944 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1945 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1946 Size = 0;
1947 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001948 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 } else if (Ty->isIncompleteArrayType()) {
1950 Size = 0;
1951 if (Ty->getElementType()->isIncompleteType())
1952 Align = 0;
1953 else
1954 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001955 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 Size = 0;
1957 Align = 0;
1958 } else {
1959 // Size and align of the whole array, not the element type.
1960 Size = CGM.getContext().getTypeSize(Ty);
1961 Align = CGM.getContext().getTypeAlign(Ty);
1962 }
1963
1964 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1965 // interior arrays, do we care? Why aren't nested arrays represented the
1966 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001967 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001968 QualType EltTy(Ty, 0);
1969 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1970 // If the number of elements is known, then count is that number. Otherwise,
1971 // it's -1. This allows us to represent a subrange with an array of 0
1972 // elements, like this:
1973 //
1974 // struct foo {
1975 // int x[0];
1976 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001977 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1979 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001980
Guy Benyei11169dd2012-12-18 14:30:41 +00001981 // FIXME: Verify this is right for VLAs.
1982 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1983 EltTy = Ty->getElementType();
1984 }
1985
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001987
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001988 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1989 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001990}
1991
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001992llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1993 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001994 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1995 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001996}
1997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001998llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1999 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002000 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2001 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002002}
2003
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002004llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2005 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00002006 uint64_t Size =
2007 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002008 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002009 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002010 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00002011 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002012
2013 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002014 Ty->getPointeeType()->getAs<FunctionProtoType>();
2015 return DBuilder.createMemberPointerType(
2016 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2017 Ty->getClass(), FPT->getTypeQuals())),
2018 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00002019 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00002020}
2021
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002022llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 // Ignore the atomic wrapping
2024 // FIXME: What is the correct representation?
2025 return getOrCreateType(Ty->getValueType(), U);
2026}
2027
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002028llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002029 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002030
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 uint64_t Size = 0;
2032 uint64_t Align = 0;
2033 if (!ED->getTypeForDecl()->isIncompleteType()) {
2034 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2035 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2036 }
2037
Manman Rene0064d82013-08-29 23:19:58 +00002038 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2039
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002040 bool isImportedFromModule =
2041 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2042
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 // If this is just a forward declaration, construct an appropriately
2044 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002045 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002046 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002047 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 unsigned Line = getLineNumber(ED->getLocation());
2049 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002050 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00002051 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002052 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002053 ReplaceMap.emplace_back(
2054 std::piecewise_construct, std::make_tuple(Ty),
2055 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002056 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002057 }
2058
David Blaikie483a9da2014-05-06 18:35:21 +00002059 return CreateTypeDefinition(Ty);
2060}
2061
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002062llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002063 const EnumDecl *ED = Ty->getDecl();
2064 uint64_t Size = 0;
2065 uint64_t Align = 0;
2066 if (!ED->getTypeForDecl()->isIncompleteType()) {
2067 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2068 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2069 }
2070
2071 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2072
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002073 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002074 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002075 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002076 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002077 Enumerators.push_back(DBuilder.createEnumerator(
2078 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002079 }
2080
2081 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002082 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002083
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002084 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002085 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002086 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002087 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002088 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2089 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2090 Line, Size, Align, EltArray, ClassTy,
2091 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002092}
2093
David Blaikie05491062013-01-21 04:37:12 +00002094static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2095 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002096 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002097 Qualifiers InnerQuals = T.getLocalQualifiers();
2098 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2099 // that is already there.
2100 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2101 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 QualType LastT = T;
2103 switch (T->getTypeClass()) {
2104 default:
David Blaikie05491062013-01-21 04:37:12 +00002105 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002106 case Type::TemplateSpecialization: {
2107 const auto *Spec = cast<TemplateSpecializationType>(T);
2108 if (Spec->isTypeAlias())
2109 return C.getQualifiedType(T.getTypePtr(), Quals);
2110 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002111 break;
2112 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002113 case Type::TypeOfExpr:
2114 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2115 break;
2116 case Type::TypeOf:
2117 T = cast<TypeOfType>(T)->getUnderlyingType();
2118 break;
2119 case Type::Decltype:
2120 T = cast<DecltypeType>(T)->getUnderlyingType();
2121 break;
2122 case Type::UnaryTransform:
2123 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2124 break;
2125 case Type::Attributed:
2126 T = cast<AttributedType>(T)->getEquivalentType();
2127 break;
2128 case Type::Elaborated:
2129 T = cast<ElaboratedType>(T)->getNamedType();
2130 break;
2131 case Type::Paren:
2132 T = cast<ParenType>(T)->getInnerType();
2133 break;
David Blaikie05491062013-01-21 04:37:12 +00002134 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 break;
2137 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002138 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002139 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002140 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002141 break;
2142 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002143
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002145 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 } while (true);
2147}
2148
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002149llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002150
2151 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002152 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002153
David Blaikief427b002014-05-06 03:42:01 +00002154 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002155 if (it != TypeCache.end()) {
2156 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002157 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002158 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 }
2160
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002161 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002162}
2163
David Blaikie0e716b42014-03-03 23:48:23 +00002164void CGDebugInfo::completeTemplateDefinition(
2165 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002166 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2167 return;
2168
David Blaikie0e716b42014-03-03 23:48:23 +00002169 completeClassData(&SD);
2170 // In case this type has no member function definitions being emitted, ensure
2171 // it is retained
2172 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2173}
2174
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002175llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002177 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002178
2179 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002180 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002181
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002182 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002183 return T;
2184
Adrian Prantlca844182015-09-11 17:23:03 +00002185 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002186 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002187
2188 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002189 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002190
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 return Res;
2192}
2193
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002194llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002195 // A forward declaration inside a module header does not belong to the module.
2196 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2197 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002198 if (DebugTypeExtRefs && D->isFromASTFile()) {
2199 // Record a reference to an imported clang module or precompiled header.
2200 auto *Reader = CGM.getContext().getExternalSource();
2201 auto Idx = D->getOwningModuleID();
2202 auto Info = Reader->getSourceDescriptor(Idx);
2203 if (Info)
2204 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2205 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002206 // We are building a clang module or a precompiled header.
2207 //
2208 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2209 // and it wouldn't be necessary to specify the parent scope
2210 // because the type is already unique by definition (it would look
2211 // like the output of -fno-standalone-debug). On the other hand,
2212 // the parent scope helps a consumer to quickly locate the object
2213 // file where the type's definition is located, so it might be
2214 // best to make this behavior a command line or debugger tuning
2215 // option.
2216 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2217 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
2218 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2219 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
2220 }
2221 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002222
Adrian Prantl9402cef2015-09-20 16:51:35 +00002223 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002224}
2225
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002226llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 // Handle qualifiers, which recursively handles what they refer to.
2228 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002229 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002230
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 // Work out details of type.
2232 switch (Ty->getTypeClass()) {
2233#define TYPE(Class, Base)
2234#define ABSTRACT_TYPE(Class, Base)
2235#define NON_CANONICAL_TYPE(Class, Base)
2236#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2237#include "clang/AST/TypeNodes.def"
2238 llvm_unreachable("Dependent types cannot show up in debug information");
2239
2240 case Type::ExtVector:
2241 case Type::Vector:
2242 return CreateType(cast<VectorType>(Ty), Unit);
2243 case Type::ObjCObjectPointer:
2244 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2245 case Type::ObjCObject:
2246 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2247 case Type::ObjCInterface:
2248 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2249 case Type::Builtin:
2250 return CreateType(cast<BuiltinType>(Ty));
2251 case Type::Complex:
2252 return CreateType(cast<ComplexType>(Ty));
2253 case Type::Pointer:
2254 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002255 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002256 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002257 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002258 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002259 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 case Type::BlockPointer:
2261 return CreateType(cast<BlockPointerType>(Ty), Unit);
2262 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002263 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002264 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002265 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002266 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002267 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002268 case Type::FunctionProto:
2269 case Type::FunctionNoProto:
2270 return CreateType(cast<FunctionType>(Ty), Unit);
2271 case Type::ConstantArray:
2272 case Type::VariableArray:
2273 case Type::IncompleteArray:
2274 return CreateType(cast<ArrayType>(Ty), Unit);
2275
2276 case Type::LValueReference:
2277 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2278 case Type::RValueReference:
2279 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2280
2281 case Type::MemberPointer:
2282 return CreateType(cast<MemberPointerType>(Ty), Unit);
2283
2284 case Type::Atomic:
2285 return CreateType(cast<AtomicType>(Ty), Unit);
2286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002288 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2289
David Blaikie42edade2014-11-11 20:44:45 +00002290 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002291 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 case Type::Elaborated:
2293 case Type::Paren:
2294 case Type::SubstTemplateTypeParm:
2295 case Type::TypeOfExpr:
2296 case Type::TypeOf:
2297 case Type::Decltype:
2298 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002299 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002300 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002301 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002302
David Blaikie42edade2014-11-11 20:44:45 +00002303 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002304}
2305
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002306llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2307 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002308 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002309
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002310 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002311
2312 // We may have cached a forward decl when we could have created
2313 // a non-forward decl. Go ahead and create a non-forward decl
2314 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002315 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002316 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
2318 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002319 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002320
2321 // Propagate members from the declaration to the definition
2322 // CreateType(const RecordType*) will overwrite this with the members in the
2323 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002324 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002325
Guy Benyei11169dd2012-12-18 14:30:41 +00002326 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002327 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002328 return Res;
2329}
2330
2331// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002332llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002333 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002334
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002336 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002337 unsigned Line = getLineNumber(RD->getLocation());
2338 StringRef RDName = getClassName(RD);
2339
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002340 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002341
David Blaikied2785892013-08-18 17:36:19 +00002342 // If we ended up creating the type during the context chain construction,
2343 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002344 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002345 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002346 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002347 return T;
David Blaikied2785892013-08-18 17:36:19 +00002348
Adrian Prantl381e7552014-02-04 21:29:50 +00002349 // If this is just a forward or incomplete declaration, construct an
2350 // appropriately marked node and just return it.
2351 const RecordDecl *D = RD->getDefinition();
2352 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002353 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002354
2355 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2356 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002357
Manman Rene0064d82013-08-29 23:19:58 +00002358 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2359
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002360 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002361 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2362 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002363
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002364 RegionMap[Ty->getDecl()].reset(RealDecl);
2365 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002366
David Blaikieadfbf992013-08-18 16:55:33 +00002367 if (const ClassTemplateSpecializationDecl *TSpecial =
2368 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002370 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002371 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002372}
2373
David Blaikieadfbf992013-08-18 16:55:33 +00002374void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002375 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002376 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002377 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002378 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2379 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002380 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002381 while (1) {
2382 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2383 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2384 if (PBT && !BRL.isPrimaryBaseVirtual())
2385 PBase = PBT;
2386 else
2387 break;
2388 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002389 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002390 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2391 getOrCreateFile(RD->getLocation())));
2392 } else if (RD->isDynamicClass())
2393 ContainingType = RealDecl;
2394
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002395 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002396}
2397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002398llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002399 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002400 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002401 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2402 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002403 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002404 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002405 *Offset += FieldSize;
2406 return Ty;
2407}
2408
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002409void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002410 StringRef &Name,
2411 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 llvm::DIScope *&FDContext,
2413 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002414 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002415 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2416 Name = getFunctionName(FD);
2417 // Use mangled name as linkage name for C/C++ functions.
2418 if (FD->hasPrototype()) {
2419 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002420 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002421 }
2422 // No need to replicate the linkage name if it isn't different from the
2423 // subprogram name, no need to have it at all unless coverage is enabled or
2424 // debug is set to more than just line tables.
2425 if (LinkageName == Name ||
2426 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2427 !CGM.getCodeGenOpts().EmitGcovNotes &&
2428 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2429 LinkageName = StringRef();
2430
2431 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2432 if (const NamespaceDecl *NSDecl =
2433 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2434 FDContext = getOrCreateNameSpace(NSDecl);
2435 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002436 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2437 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2438 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2439 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002440 // Collect template parameters.
2441 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2442 }
2443}
2444
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002445void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002446 unsigned &LineNo, QualType &T,
2447 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002448 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002449 Unit = getOrCreateFile(VD->getLocation());
2450 LineNo = getLineNumber(VD->getLocation());
2451
2452 setLocation(VD->getLocation());
2453
2454 T = VD->getType();
2455 if (T->isIncompleteArrayType()) {
2456 // CodeGen turns int[] into int[1] so we'll do the same here.
2457 llvm::APInt ConstVal(32, 1);
2458 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2459
2460 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2461 ArrayType::Normal, 0);
2462 }
2463
2464 Name = VD->getName();
2465 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2466 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2467 LinkageName = CGM.getMangledName(VD);
2468 if (LinkageName == Name)
2469 LinkageName = StringRef();
2470
2471 // Since we emit declarations (DW_AT_members) for static members, place the
2472 // definition of those static members in the namespace they were declared in
2473 // in the source code (the lexical decl context).
2474 // FIXME: Generalize this for even non-member global variables where the
2475 // declaration and definition may have different lexical decl contexts, once
2476 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002477 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2478 : VD->getDeclContext();
2479 // When a record type contains an in-line initialization of a static data
2480 // member, and the record type is marked as __declspec(dllexport), an implicit
2481 // definition of the member will be created in the record context. DWARF
2482 // doesn't seem to have a nice way to describe this in a form that consumers
2483 // are likely to understand, so fake the "normal" situation of a definition
2484 // outside the class by putting it in the global scope.
2485 if (DC->isRecord())
2486 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002487
2488 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2489 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002490}
2491
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002493CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002495 StringRef Name, LinkageName;
2496 unsigned Flags = 0;
2497 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002498 llvm::DIFile *Unit = getOrCreateFile(Loc);
2499 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002500 unsigned Line = getLineNumber(Loc);
2501
2502 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2503 TParamsArray, Flags);
2504 // Build function type.
2505 SmallVector<QualType, 16> ArgTypes;
2506 for (const ParmVarDecl *Parm: FD->parameters())
2507 ArgTypes.push_back(Parm->getType());
2508 QualType FnType =
2509 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2510 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002511 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002512 DContext, Name, LinkageName, Unit, Line,
2513 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002514 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002515 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002516 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002517 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2518 std::make_tuple(CanonDecl),
2519 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002520 return SP;
2521}
2522
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002523llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002524CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2525 QualType T;
2526 StringRef Name, LinkageName;
2527 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002528 llvm::DIFile *Unit = getOrCreateFile(Loc);
2529 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002530 unsigned Line = getLineNumber(Loc);
2531
2532 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002533 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2534 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2535 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002536 FwdDeclReplaceMap.emplace_back(
2537 std::piecewise_construct,
2538 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2539 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002540 return GV;
2541}
2542
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002543llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002544 // We only need a declaration (not a definition) of the type - so use whatever
2545 // we would otherwise do to get a type for a pointee. (forward declarations in
2546 // limited debug info, full definitions (if the type definition is available)
2547 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002548 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2549 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002550 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002551 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002552
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002553 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002554 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002555
2556 // No definition for now. Emit a forward definition that might be
2557 // merged with a potential upcoming definition.
2558 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2559 return getFunctionForwardDeclaration(FD);
2560 else if (const auto *VD = dyn_cast<VarDecl>(D))
2561 return getGlobalVariableForwardDeclaration(VD);
2562
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002563 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002564}
2565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002566llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002567 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002568 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002569
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002571 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002572 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002573
2574 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002575 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002576
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002577 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002578 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002579 if (const CXXMethodDecl *MD =
2580 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002581 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002582 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002583 }
2584 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002586 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002587 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 return SP;
2589 }
2590
Aaron Ballman86c93902014-03-06 23:45:36 +00002591 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002592 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002594 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002595 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002596 return SP;
2597 }
2598 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002599 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002600}
2601
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002602// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002603// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002604llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002605 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002606 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002607 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002608 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2609 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002610 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002611
2612 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2613 return getOrCreateMethodType(Method, F);
2614 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2615 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002616 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002617
2618 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002619 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002620
2621 // Replace the instancetype keyword with the actual type.
2622 if (ResultTy == CGM.getContext().getObjCInstanceType())
2623 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002624 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002625
Adrian Prantl7bec9032013-05-10 21:08:31 +00002626 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002628 QualType SelfDeclTy;
2629 if (auto *SelfDecl = OMethod->getSelfDecl())
2630 SelfDeclTy = SelfDecl->getType();
2631 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2632 if (FPT->getNumParams() > 1)
2633 SelfDeclTy = FPT->getParamType(0);
2634 if (!SelfDeclTy.isNull())
2635 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002637 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002638 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002640 for (const auto *PI : OMethod->params())
2641 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002642 // Variadic methods need a special marker at the end of the type list.
2643 if (OMethod->isVariadic())
2644 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002645
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002646 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Eric Christopher28a6db52015-10-15 06:56:08 +00002647 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002649
Adrian Prantl800faef2014-02-25 23:42:18 +00002650 // Handle variadic function types; they need an additional
2651 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002652 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2653 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002654 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002655 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2656 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2657 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2658 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2659 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002660 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +00002661 return DBuilder.createSubroutineType(EltTypeArray);
Adrian Prantld45ba252014-02-25 19:38:11 +00002662 }
2663
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002664 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002665}
2666
Eric Christophere7b87e52014-10-26 23:40:33 +00002667void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2668 SourceLocation ScopeLoc, QualType FnType,
2669 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002670
2671 StringRef Name;
2672 StringRef LinkageName;
2673
2674 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2675
2676 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002677 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002678
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002680 llvm::DIFile *Unit = getOrCreateFile(Loc);
2681 llvm::DIScope *FDContext = Unit;
2682 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 if (!HasDecl) {
2684 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002685 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002687 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002688 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002689 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002690 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002691 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002692 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002693 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002694 return;
2695 }
2696 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002697 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2698 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2700 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002701 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002702 } else {
2703 // Use llvm function name.
2704 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002705 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 }
2707 if (!Name.empty() && Name[0] == '\01')
2708 Name = Name.substr(1);
2709
Adrian Prantl42d71b92014-04-10 23:21:53 +00002710 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002711 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002712 // Artificial functions without a location should not silently reuse CurLoc.
2713 if (Loc.isInvalid())
2714 CurLoc = SourceLocation();
2715 }
2716 unsigned LineNo = getLineNumber(Loc);
2717 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002718
Eric Christopher8018e412014-03-27 18:50:35 +00002719 // FIXME: The function declaration we're constructing here is mostly reusing
2720 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2721 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2722 // all subprograms instead of the actual context since subprogram definitions
2723 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002724 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002725 FDContext, Name, LinkageName, Unit, LineNo,
2726 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002727 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002728 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002729 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002730 // We might get here with a VarDecl in the case we're generating
2731 // code for the initialization of globals. Do not record these decls
2732 // as they will overwrite the actual VarDecl Decl in the cache.
2733 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002734 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002735
Adrian Prantlbebb8932014-03-21 21:01:58 +00002736 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002737 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002738
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002740 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002741}
2742
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002743void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2744 QualType FnType) {
2745 StringRef Name;
2746 StringRef LinkageName;
2747
2748 const Decl *D = GD.getDecl();
2749 if (!D)
2750 return;
2751
2752 unsigned Flags = 0;
2753 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002754 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002755 llvm::DINodeArray TParamsArray;
2756 if (isa<FunctionDecl>(D)) {
2757 // If there is a DISubprogram for this function available then use it.
2758 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2759 TParamsArray, Flags);
2760 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2761 Name = getObjCMethodName(OMD);
2762 Flags |= llvm::DINode::FlagPrototyped;
2763 } else {
2764 llvm_unreachable("not a function or ObjC method");
2765 }
2766 if (!Name.empty() && Name[0] == '\01')
2767 Name = Name.substr(1);
2768
2769 if (D->isImplicit()) {
2770 Flags |= llvm::DINode::FlagArtificial;
2771 // Artificial functions without a location should not silently reuse CurLoc.
2772 if (Loc.isInvalid())
2773 CurLoc = SourceLocation();
2774 }
2775 unsigned LineNo = getLineNumber(Loc);
2776 unsigned ScopeLine = 0;
2777
2778 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2779 getOrCreateFunctionType(D, FnType, Unit),
2780 false /*internalLinkage*/, true /*definition*/,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002781 ScopeLine, Flags, CGM.getLangOpts().Optimize,
2782 TParamsArray.get(), getFunctionDeclaration(D));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002783}
2784
David Blaikie835afb22015-01-21 23:08:17 +00002785void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002786 // Update our current location
2787 setLocation(Loc);
2788
Eric Christophere7b87e52014-10-26 23:40:33 +00002789 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2790 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002791
Adrian Prantle83b1302014-01-07 22:05:52 +00002792 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002793 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002794 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002795}
2796
Guy Benyei11169dd2012-12-18 14:30:41 +00002797void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002798 llvm::MDNode *Back = nullptr;
2799 if (!LexicalBlockStack.empty())
2800 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002801 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002802 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002803 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002804}
2805
Eric Christopher0fdcb312013-05-16 00:52:20 +00002806void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2807 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 // Set our current location.
2809 setLocation(Loc);
2810
Guy Benyei11169dd2012-12-18 14:30:41 +00002811 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002812 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2813 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002814
2815 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2816 return;
2817
2818 // Create a new lexical block and push it on the stack.
2819 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002820}
2821
Eric Christopher0fdcb312013-05-16 00:52:20 +00002822void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2823 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2825
2826 // Provide an entry in the line table for the end of the block.
2827 EmitLocation(Builder, Loc);
2828
David Blaikie60a877b2014-10-22 19:34:33 +00002829 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2830 return;
2831
Guy Benyei11169dd2012-12-18 14:30:41 +00002832 LexicalBlockStack.pop_back();
2833}
2834
Guy Benyei11169dd2012-12-18 14:30:41 +00002835void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2836 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2837 unsigned RCount = FnBeginRegionCount.back();
2838 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2839
2840 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002841 while (LexicalBlockStack.size() != RCount) {
2842 // Provide an entry in the line table for the end of the block.
2843 EmitLocation(Builder, CurLoc);
2844 LexicalBlockStack.pop_back();
2845 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 FnBeginRegionCount.pop_back();
2847}
2848
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002849llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002850 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002851
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002852 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 QualType FType;
2854 uint64_t FieldSize, FieldOffset;
2855 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002856
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002857 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002858 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002859
2860 FieldOffset = 0;
2861 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2862 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2863 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2864 FType = CGM.getContext().IntTy;
2865 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2866 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2867
2868 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2869 if (HasCopyAndDispose) {
2870 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002871 EltTys.push_back(
2872 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2873 EltTys.push_back(
2874 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002875 }
2876 bool HasByrefExtendedLayout;
2877 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002878 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2879 HasByrefExtendedLayout) &&
2880 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002881 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002882 EltTys.push_back(
2883 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002884 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002885
Guy Benyei11169dd2012-12-18 14:30:41 +00002886 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2887 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002888 CGM.getTarget().getPointerAlign(0))) {
2889 CharUnits FieldOffsetInBytes =
2890 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2891 CharUnits AlignedOffsetInBytes =
2892 FieldOffsetInBytes.RoundUpToAlignment(Align);
2893 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002894
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 if (NumPaddingBytes.isPositive()) {
2896 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2897 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2898 pad, ArrayType::Normal, 0);
2899 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2900 }
2901 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002902
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002904 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002905 FieldSize = CGM.getContext().getTypeSize(FType);
2906 FieldAlign = CGM.getContext().toBits(Align);
2907
Eric Christopherb2a008c2013-05-16 00:45:12 +00002908 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002909 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2910 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002911 EltTys.push_back(FieldTy);
2912 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002913
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002914 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002916 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002917
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002919 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002920}
2921
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002922void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2923 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002924 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002925 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2927
David Blaikie7fceebf2013-08-19 03:37:48 +00002928 bool Unwritten =
2929 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2930 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002931 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002932 if (!Unwritten)
2933 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002934 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002935 uint64_t XOffset = 0;
2936 if (VD->hasAttr<BlocksAttr>())
2937 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002938 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 Ty = getOrCreateType(VD->getType(), Unit);
2940
2941 // If there is no debug info for this type then do not emit debug info
2942 // for this variable.
2943 if (!Ty)
2944 return;
2945
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002947 unsigned Line = 0;
2948 unsigned Column = 0;
2949 if (!Unwritten) {
2950 Line = getLineNumber(VD->getLocation());
2951 Column = getColumnNumber(VD->getLocation());
2952 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002953 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002954 unsigned Flags = 0;
2955 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002956 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 // If this is the first argument and it is implicit then
2958 // give it an object pointer flag.
2959 // FIXME: There has to be a better way to do this, but for static
2960 // functions there won't be an implicit param at arg1 and
2961 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002962 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002963 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002964 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002965 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2966 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002967 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002968
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002969 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002970
2971 StringRef Name = VD->getName();
2972 if (!Name.empty()) {
2973 if (VD->hasAttr<BlocksAttr>()) {
2974 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002975 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 // offset of __forwarding field
2977 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002978 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002979 Expr.push_back(offset.getQuantity());
2980 Expr.push_back(llvm::dwarf::DW_OP_deref);
2981 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 // offset of x field
2983 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002984 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002985
2986 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002987 auto *D = ArgNo
2988 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2989 *ArgNo, Unit, Line, Ty)
2990 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2991 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002992
Guy Benyei11169dd2012-12-18 14:30:41 +00002993 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002994 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2995 llvm::DebugLoc::get(Line, Column, Scope),
2996 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002997 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002998 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002999 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003000 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3001 // If VD is an anonymous union then Storage represents value for
3002 // all union fields.
3003 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3004 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003005 // GDB has trouble finding local variables in anonymous unions, so we emit
3006 // artifical local variables for each of the members.
3007 //
3008 // FIXME: Remove this code as soon as GDB supports this.
3009 // The debug info verifier in LLVM operates based on the assumption that a
3010 // variable has the same size as its storage and we had to disable the check
3011 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003012 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003013 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003014 StringRef FieldName = Field->getName();
3015
3016 // Ignore unnamed fields. Do not ignore unnamed records.
3017 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3018 continue;
3019
3020 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003021 auto *D = DBuilder.createAutoVariable(
3022 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3023 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003024
3025 // Insert an llvm.dbg.declare into the current block.
3026 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3027 llvm::DebugLoc::get(Line, Column, Scope),
3028 Builder.GetInsertBlock());
3029 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003030 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003032
3033 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003034 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003035 ArgNo
3036 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3037 Ty, CGM.getLangOpts().Optimize,
3038 Flags)
3039 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3040 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003041
3042 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003043 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3044 llvm::DebugLoc::get(Line, Column, Scope),
3045 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003046}
3047
3048void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3049 llvm::Value *Storage,
3050 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003051 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003052 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003053}
3054
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003055llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3056 llvm::DIType *Ty) {
3057 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003058 if (CachedTy)
3059 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003060 return DBuilder.createObjectPointerType(Ty);
3061}
3062
Eric Christophere7b87e52014-10-26 23:40:33 +00003063void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3064 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003065 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003066 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003068
Craig Topper8a13c412014-05-21 05:09:00 +00003069 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003071
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003073
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003075 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3076 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003077 if (isByRef)
3078 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003079 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003080 Ty = getOrCreateType(VD->getType(), Unit);
3081
3082 // Self is passed along as an implicit non-arg variable in a
3083 // block. Mark it as the object pointer.
3084 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003085 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003086
3087 // Get location information.
3088 unsigned Line = getLineNumber(VD->getLocation());
3089 unsigned Column = getColumnNumber(VD->getLocation());
3090
3091 const llvm::DataLayout &target = CGM.getDataLayout();
3092
3093 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003094 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3096
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003097 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003098 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003099 addr.push_back(llvm::dwarf::DW_OP_deref);
3100 addr.push_back(llvm::dwarf::DW_OP_plus);
3101 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003103 addr.push_back(llvm::dwarf::DW_OP_deref);
3104 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003105 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003106 offset =
3107 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003108 addr.push_back(offset.getQuantity());
3109 addr.push_back(llvm::dwarf::DW_OP_deref);
3110 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 // offset of x field
3112 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003113 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 }
3115
3116 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003117 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003118 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003119 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003120
Guy Benyei11169dd2012-12-18 14:30:41 +00003121 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003122 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3123 if (InsertPoint)
3124 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3125 InsertPoint);
3126 else
3127 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3128 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003129}
3130
Guy Benyei11169dd2012-12-18 14:30:41 +00003131void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3132 unsigned ArgNo,
3133 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003134 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003135 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003136}
3137
3138namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003139struct BlockLayoutChunk {
3140 uint64_t OffsetInBits;
3141 const BlockDecl::Capture *Capture;
3142};
3143bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3144 return l.OffsetInBits < r.OffsetInBits;
3145}
Guy Benyei11169dd2012-12-18 14:30:41 +00003146}
3147
3148void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003149 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003150 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003151 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003152 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003153 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 ASTContext &C = CGM.getContext();
3155 const BlockDecl *blockDecl = block.getBlockDecl();
3156
3157 // Collect some general information about the block's location.
3158 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003159 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003160 unsigned line = getLineNumber(loc);
3161 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003162
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003164 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003165
3166 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003167 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003168
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003169 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3171 blockLayout->getElementOffsetInBits(0),
3172 tunit, tunit));
3173 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3174 blockLayout->getElementOffsetInBits(1),
3175 tunit, tunit));
3176 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3177 blockLayout->getElementOffsetInBits(2),
3178 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003179 auto *FnTy = block.getBlockExpr()->getFunctionType();
3180 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3181 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 blockLayout->getElementOffsetInBits(3),
3183 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003184 fields.push_back(createFieldType(
3185 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3186 ? C.getBlockDescriptorExtendedType()
3187 : C.getBlockDescriptorType()),
3188 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003189
3190 // We want to sort the captures by offset, not because DWARF
3191 // requires this, but because we're paranoid about debuggers.
3192 SmallVector<BlockLayoutChunk, 8> chunks;
3193
3194 // 'this' capture.
3195 if (blockDecl->capturesCXXThis()) {
3196 BlockLayoutChunk chunk;
3197 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003198 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003199 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003200 chunks.push_back(chunk);
3201 }
3202
3203 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003204 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 const VarDecl *variable = capture.getVariable();
3206 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3207
3208 // Ignore constant captures.
3209 if (captureInfo.isConstant())
3210 continue;
3211
3212 BlockLayoutChunk chunk;
3213 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003214 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003215 chunk.Capture = &capture;
3216 chunks.push_back(chunk);
3217 }
3218
3219 // Sort by offset.
3220 llvm::array_pod_sort(chunks.begin(), chunks.end());
3221
Eric Christophere7b87e52014-10-26 23:40:33 +00003222 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3223 e = chunks.end();
3224 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 uint64_t offsetInBits = i->OffsetInBits;
3226 const BlockDecl::Capture *capture = i->Capture;
3227
3228 // If we have a null capture, this must be the C++ 'this' capture.
3229 if (!capture) {
3230 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003231 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 QualType type = method->getThisType(C);
3233
3234 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3235 offsetInBits, tunit, tunit));
3236 continue;
3237 }
3238
3239 const VarDecl *variable = capture->getVariable();
3240 StringRef name = variable->getName();
3241
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003242 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003243 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003244 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003245
3246 // FIXME: this creates a second copy of this type!
3247 uint64_t xoffset;
3248 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003249 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3250 fieldType =
3251 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3252 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003253 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003254 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3255 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003256 }
3257 fields.push_back(fieldType);
3258 }
3259
3260 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003261 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3262 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003263
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003264 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003265
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003266 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003267 tunit, typeName.str(), tunit, line,
3268 CGM.getContext().toBits(block.BlockSize),
3269 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003270 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3271
3272 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003273 unsigned flags = llvm::DINode::FlagArtificial;
3274 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003275
3276 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003277 auto *debugVar = DBuilder.createParameterVariable(
3278 scope, Arg->getName(), ArgNo, tunit, line, type,
3279 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003280
Adrian Prantl616bef42013-03-14 21:52:59 +00003281 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003282 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003283 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003284 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003285 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003286 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003287
Adrian Prantl616bef42013-03-14 21:52:59 +00003288 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003289 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3290 llvm::DebugLoc::get(line, column, scope),
3291 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003292}
3293
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003294llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003295CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3296 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003297 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003298
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003299 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003300 if (MI != StaticDataMemberCache.end()) {
3301 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003302 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003303 }
David Blaikiece763042013-08-20 21:49:21 +00003304
3305 // If the member wasn't found in the cache, lazily construct and add it to the
3306 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003307 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003308 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003309 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003310}
3311
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003312llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3313 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3314 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3315 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003316
3317 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003318 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003319 StringRef FieldName = Field->getName();
3320
3321 // Ignore unnamed fields, but recurse into anonymous records.
3322 if (FieldName.empty()) {
3323 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3324 if (RT)
3325 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3326 Var, DContext);
3327 continue;
3328 }
3329 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003330 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3331 LineNo, FieldTy,
3332 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003333 }
3334 return GV;
3335}
3336
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003337void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003338 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003339 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003341 llvm::DIFile *Unit = nullptr;
3342 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003343 unsigned LineNo;
3344 StringRef DeclName, LinkageName;
3345 QualType T;
3346 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003347
3348 // Attempt to store one global variable for the declaration - even if we
3349 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003350 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003351
3352 // If this is an anonymous union then we'll want to emit a global
3353 // variable for each member of the anonymous union so that it's possible
3354 // to find the name of any field in the union.
3355 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003356 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003357 assert(RD->isAnonymousStructOrUnion() &&
3358 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003359 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3360 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003361 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003362 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3363 Var->hasInternalLinkage(), Var,
3364 getOrCreateStaticDataMemberDeclarationOrNull(D));
3365 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003366 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003367}
3368
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003369void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3370 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003371 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003372 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003373 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003374 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003375 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003376 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3377 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3378 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3379 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3380 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003381 // Do not use global variables for enums.
3382 //
3383 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003384 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003385 return;
David Blaikiea15565562014-04-04 20:56:17 +00003386 // Do not emit separate definitions for function local const/statics.
3387 if (isa<FunctionDecl>(VD->getDeclContext()))
3388 return;
David Blaikiebb113912014-04-05 07:23:17 +00003389 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003390 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003391 if (VarD->isStaticDataMember()) {
3392 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003393 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003394 // Ensure that the type is retained even though it's otherwise unreferenced.
3395 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003396 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003397 return;
3398 }
3399
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003400 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003401
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003402 auto &GV = DeclCache[VD];
3403 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003404 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003405 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003406 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003407 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003408}
3409
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003410llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003411 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003412 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003413 llvm::DIScope *Mod = getParentModuleOrNull(D);
3414 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003415}
3416
David Blaikie9f88fe82013-04-22 06:13:21 +00003417void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003418 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3419 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003420 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3421 if (!NSDecl->isAnonymousNamespace() ||
3422 CGM.getCodeGenOpts().DebugExplicitImport) {
3423 DBuilder.createImportedModule(
3424 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3425 getOrCreateNameSpace(NSDecl),
3426 getLineNumber(UD.getLocation()));
3427 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003428}
3429
David Blaikiebd483762013-05-20 04:58:53 +00003430void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3431 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3432 return;
3433 assert(UD.shadow_size() &&
3434 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3435 // Emitting one decl is sufficient - debuggers can detect that this is an
3436 // overloaded name & provide lookup for all the overloads.
3437 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003439 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003440 DBuilder.createImportedDeclaration(
3441 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3442 getLineNumber(USD.getLocation()));
3443}
3444
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003445void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantl8a634c12015-12-18 19:44:31 +00003446 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003447 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003448 DBuilder.createImportedDeclaration(
3449 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3450 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3451 getLineNumber(ID.getLocation()));
3452 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003453}
3454
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003455llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003456CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3457 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003458 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003459 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003460 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003461 return cast<llvm::DIImportedEntity>(VH);
3462 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003463 if (const NamespaceAliasDecl *Underlying =
3464 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3465 // This could cache & dedup here rather than relying on metadata deduping.
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 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3469 NA.getName());
3470 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003471 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003472 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3473 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3474 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003475 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003476 return R;
3477}
3478
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003479llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003480CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003481 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003482 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003485
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003487 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003488 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003489 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003490 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003491 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003492 return NS;
3493}
3494
Adrian Prantla5206ce2015-09-22 23:26:43 +00003495void CGDebugInfo::setDwoId(uint64_t Signature) {
3496 assert(TheCU && "no main compile unit");
3497 TheCU->setDWOId(Signature);
3498}
3499
3500
Guy Benyei11169dd2012-12-18 14:30:41 +00003501void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003502 // Creating types might create further types - invalidating the current
3503 // element and the size(), so don't cache/reference them.
3504 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3505 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003506 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003507 ? CreateTypeDefinition(E.Type, E.Unit)
3508 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003509 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003510 }
3511
David Blaikief427b002014-05-06 03:42:01 +00003512 for (auto p : ReplaceMap) {
3513 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003514 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003515 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003516
David Blaikief427b002014-05-06 03:42:01 +00003517 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003518 assert(it != TypeCache.end());
3519 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003520
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003521 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3522 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003523 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003524
Frederic Rissd253ed62014-11-18 03:40:51 +00003525 for (const auto &p : FwdDeclReplaceMap) {
3526 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003527 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003528 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003529
3530 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003531 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003532 // with ourselves, that will destroy the temporary MDNode and
3533 // replace it with a standard one, avoiding leaking memory.
3534 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003535 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003536 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003537 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003538
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003539 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003540 }
3541
Adrian Prantl73409ce2013-03-11 18:33:46 +00003542 // We keep our own list of retained types, because we need to look
3543 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003544 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003545 if (auto MD = TypeCache[RT])
3546 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003547
Guy Benyei11169dd2012-12-18 14:30:41 +00003548 DBuilder.finalize();
3549}
David Blaikie66088d52014-09-24 17:01:27 +00003550
3551void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3552 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3553 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003554
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003555 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003556 // Don't ignore in case of explicit cast where it is referenced indirectly.
3557 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003558}