blob: a63f5582cd90adf610c4312928cac45b4fde0fc6 [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"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000029#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
Adrian Prantl826824e2016-04-08 22:43:06 +0000399 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
400 switch (DebugKind) {
401 case codegenoptions::NoDebugInfo:
402 case codegenoptions::LocTrackingOnly:
403 EmissionKind = llvm::DICompileUnit::NoDebug;
404 break;
405 case codegenoptions::DebugLineTablesOnly:
406 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
407 break;
408 case codegenoptions::LimitedDebugInfo:
409 case codegenoptions::FullDebugInfo:
410 EmissionKind = llvm::DICompileUnit::FullDebug;
411 break;
412 }
413
Guy Benyei11169dd2012-12-18 14:30:41 +0000414 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000415 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000416 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000417 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
418 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantl826824e2016-04-08 22:43:06 +0000419 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei11169dd2012-12-18 14:30:41 +0000420}
421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000422llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000423 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 StringRef BTName;
425 switch (BT->getKind()) {
426#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000427#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000428#include "clang/AST/BuiltinTypes.def"
429 case BuiltinType::Dependent:
430 llvm_unreachable("Unexpected builtin type");
431 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000432 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000433 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000434 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000436 if (!ClassTy)
437 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
438 "objc_class", TheCU,
439 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 return ClassTy;
441 case BuiltinType::ObjCId: {
442 // typedef struct objc_class *Class;
443 // typedef struct objc_object {
444 // Class isa;
445 // } *id;
446
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000447 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000448 return ObjTy;
449
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000450 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000451 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
452 "objc_class", TheCU,
453 getOrCreateMainFile(), 0);
454
455 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000456
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000457 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000458
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000459 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000460 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000461 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000462
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000463 DBuilder.replaceArrays(
464 ObjTy,
465 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
466 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 return ObjTy;
468 }
469 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000470 if (!SelTy)
471 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
472 "objc_selector", TheCU,
473 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000474 return SelTy;
475 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000476
Alexey Bader954ba212016-04-08 13:40:33 +0000477#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
478 case BuiltinType::Id: \
479 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
480 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000481#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000482 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000483 return DBuilder.createBasicType(
484 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
485 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000486 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000487 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000488 case BuiltinType::OCLClkEvent:
489 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
490 case BuiltinType::OCLQueue:
491 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
492 case BuiltinType::OCLNDRange:
493 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
494 case BuiltinType::OCLReserveID:
495 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000496
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000498 case BuiltinType::Char_U:
499 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
500 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000501 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000502 case BuiltinType::SChar:
503 Encoding = llvm::dwarf::DW_ATE_signed_char;
504 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000506 case BuiltinType::Char32:
507 Encoding = llvm::dwarf::DW_ATE_UTF;
508 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000509 case BuiltinType::UShort:
510 case BuiltinType::UInt:
511 case BuiltinType::UInt128:
512 case BuiltinType::ULong:
513 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000514 case BuiltinType::ULongLong:
515 Encoding = llvm::dwarf::DW_ATE_unsigned;
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 case BuiltinType::Short:
518 case BuiltinType::Int:
519 case BuiltinType::Int128:
520 case BuiltinType::Long:
521 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000522 case BuiltinType::LongLong:
523 Encoding = llvm::dwarf::DW_ATE_signed;
524 break;
525 case BuiltinType::Bool:
526 Encoding = llvm::dwarf::DW_ATE_boolean;
527 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 case BuiltinType::Half:
529 case BuiltinType::Float:
530 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000531 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000532 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000533 // FIXME: For targets where long double and __float128 have the same size,
534 // they are currently indistinguishable in the debugger without some
535 // special treatment. However, there is currently no consensus on encoding
536 // and this should be updated once a DWARF encoding exists for distinct
537 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000538 Encoding = llvm::dwarf::DW_ATE_float;
539 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 }
541
542 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000543 case BuiltinType::Long:
544 BTName = "long int";
545 break;
546 case BuiltinType::LongLong:
547 BTName = "long long int";
548 break;
549 case BuiltinType::ULong:
550 BTName = "long unsigned int";
551 break;
552 case BuiltinType::ULongLong:
553 BTName = "long long unsigned int";
554 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 default:
556 BTName = BT->getName(CGM.getLangOpts());
557 break;
558 }
559 // Bit size, align and offset of the type.
560 uint64_t Size = CGM.getContext().getTypeSize(BT);
561 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000562 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000563}
564
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000565llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000567 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 if (Ty->isComplexIntegerType())
569 Encoding = llvm::dwarf::DW_ATE_lo_user;
570
571 uint64_t Size = CGM.getContext().getTypeSize(Ty);
572 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000573 return DBuilder.createBasicType("complex", 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::CreateQualifiedType(QualType Ty,
577 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000578 QualifierCollector Qc;
579 const Type *T = Qc.strip(Ty);
580
581 // Ignore these qualifiers for now.
582 Qc.removeObjCGCAttr();
583 Qc.removeAddressSpace();
584 Qc.removeObjCLifetime();
585
586 // We will create one Derived type for one qualifier and recurse to handle any
587 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000588 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 if (Qc.hasConst()) {
590 Tag = llvm::dwarf::DW_TAG_const_type;
591 Qc.removeConst();
592 } else if (Qc.hasVolatile()) {
593 Tag = llvm::dwarf::DW_TAG_volatile_type;
594 Qc.removeVolatile();
595 } else if (Qc.hasRestrict()) {
596 Tag = llvm::dwarf::DW_TAG_restrict_type;
597 Qc.removeRestrict();
598 } else {
599 assert(Qc.empty() && "Unknown type qualifier for debug info");
600 return getOrCreateType(QualType(T, 0), Unit);
601 }
602
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000603 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604
605 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
606 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000607 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000608}
609
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000610llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
611 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000612
613 // The frontend treats 'id' as a typedef to an ObjCObjectType,
614 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
615 // debug info, we want to emit 'id' in both cases.
616 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000617 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000618
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000619 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
620 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000621}
622
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000623llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
624 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000625 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000626 Ty->getPointeeType(), Unit);
627}
628
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000629/// \return whether a C++ mangling exists for the type defined by TD.
630static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
631 switch (TheCU->getSourceLanguage()) {
632 case llvm::dwarf::DW_LANG_C_plus_plus:
633 return true;
634 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
635 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
636 default:
637 return false;
638 }
639}
640
Manman Rene0064d82013-08-29 23:19:58 +0000641/// In C++ mode, types have linkage, so we can rely on the ODR and
642/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000643static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
644 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000645 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000646 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000647 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000648
649 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000650 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000651
Manman Rene0064d82013-08-29 23:19:58 +0000652 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
653 if (CGM.getTarget().getCXXABI().isMicrosoft())
654 return FullName;
655
656 // TODO: This is using the RTTI name. Is there a better way to get
657 // a unique string for a type?
658 llvm::raw_svector_ostream Out(FullName);
659 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000660 return FullName;
661}
662
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000663/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000664static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
665 llvm::dwarf::Tag Tag;
666 if (RD->isStruct() || RD->isInterface())
667 Tag = llvm::dwarf::DW_TAG_structure_type;
668 else if (RD->isUnion())
669 Tag = llvm::dwarf::DW_TAG_union_type;
670 else {
671 // FIXME: This could be a struct type giving a default visibility different
672 // than C++ class type, but needs llvm metadata changes first.
673 assert(RD->isClass());
674 Tag = llvm::dwarf::DW_TAG_class_type;
675 }
676 return Tag;
677}
678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000679llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000680CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000681 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000682 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000683 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
684 return cast<llvm::DICompositeType>(T);
685 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000686 unsigned Line = getLineNumber(RD->getLocation());
687 StringRef RDName = getClassName(RD);
688
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000689 uint64_t Size = 0;
690 uint64_t Align = 0;
691
692 const RecordDecl *D = RD->getDefinition();
693 if (D && D->isCompleteDefinition()) {
694 Size = CGM.getContext().getTypeSize(Ty);
695 Align = CGM.getContext().getTypeAlign(Ty);
696 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000697
698 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000699 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000700 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000701 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000703 ReplaceMap.emplace_back(
704 std::piecewise_construct, std::make_tuple(Ty),
705 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000706 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000707}
708
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000709llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000710 const Type *Ty,
711 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000712 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000713 // Bit size, align and offset of the type.
714 // Size is always the size of a pointer. We can't use getTypeSize here
715 // because that does not return the correct value for references.
716 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000717 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
719
Keno Fischer87842f32015-11-16 09:04:13 +0000720 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
721 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
722 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
723 Size, Align);
724 else
725 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
726 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727}
728
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000729llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
730 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000731 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000732 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000733 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
734 TheCU, getOrCreateMainFile(), 0);
735 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
736 Cache = DBuilder.createPointerType(Cache, Size);
737 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000738}
739
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000740llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
741 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000742 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000743 QualType FType;
744 uint64_t FieldSize, FieldOffset;
745 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000746 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000747
748 FieldOffset = 0;
749 FType = CGM.getContext().UnsignedLongTy;
750 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
751 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
752
753 Elements = DBuilder.getOrCreateArray(EltTys);
754 EltTys.clear();
755
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000756 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000757 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000758
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000759 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000760 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000761 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000762
763 // Bit size, align and offset of the type.
764 uint64_t Size = CGM.getContext().getTypeSize(Ty);
765
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000766 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000767
768 FieldOffset = 0;
769 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
770 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
771 FType = CGM.getContext().IntTy;
772 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
773 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000774 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000775 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
776
777 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 FieldSize = CGM.getContext().getTypeSize(Ty);
779 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000780 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000781 FieldSize, FieldAlign, FieldOffset,
782 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000783
784 FieldOffset += FieldSize;
785 Elements = DBuilder.getOrCreateArray(EltTys);
786
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000787 // The __block_literal_generic structs are marked with a special
788 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
789 // the debugger needs to know about. To allow type uniquing, emit
790 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000791 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000792 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000793 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000794
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000795 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000796}
797
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000798llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
799 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000800 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000801 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000802
803 SmallString<128> NS;
804 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000805 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
806 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000807
808 TemplateSpecializationType::PrintTemplateArgumentList(
809 OS, Ty->getArgs(), Ty->getNumArgs(),
810 CGM.getContext().getPrintingPolicy());
811
Eric Christophere7b87e52014-10-26 23:40:33 +0000812 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
813 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000814
815 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000816 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
817 getLineNumber(Loc),
818 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000819}
820
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000821llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
822 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000823 // We don't set size information, but do specify where the typedef was
824 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000825 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000826
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000827 // Typedefs are derived from some other type.
828 return DBuilder.createTypedef(
829 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
830 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000831 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000832}
833
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000834llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
835 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000836 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000837
838 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000839 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000840
841 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000842 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000843 if (isa<FunctionNoProtoType>(Ty))
844 EltTys.push_back(DBuilder.createUnspecifiedParameter());
845 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000846 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
847 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000848 if (FPT->isVariadic())
849 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000850 }
851
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000852 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +0000853 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +0000854}
855
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000856/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000857/// As an optimization, return 0 if the access specifier equals the
858/// default for the containing type.
859static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
860 AccessSpecifier Default = clang::AS_none;
861 if (RD && RD->isClass())
862 Default = clang::AS_private;
863 else if (RD && (RD->isStruct() || RD->isUnion()))
864 Default = clang::AS_public;
865
866 if (Access == Default)
867 return 0;
868
Eric Christophere7b87e52014-10-26 23:40:33 +0000869 switch (Access) {
870 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000871 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000872 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000873 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000874 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000876 case clang::AS_none:
877 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000878 }
879 llvm_unreachable("unexpected access enumerator");
880}
Guy Benyei11169dd2012-12-18 14:30:41 +0000881
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000882llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000883 StringRef name, QualType type, uint64_t sizeInBitsOverride,
884 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000885 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
886 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000887
888 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000889 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000890 unsigned line = getLineNumber(loc);
891
David Majnemer34b57492014-07-30 01:30:47 +0000892 uint64_t SizeInBits = 0;
893 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000894 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000895 TypeInfo TI = CGM.getContext().getTypeInfo(type);
896 SizeInBits = TI.Width;
897 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000898
899 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000900 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000901 }
902
Adrian Prantl21361fb2014-08-29 22:44:27 +0000903 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000904 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
905 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000906}
907
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000908void CGDebugInfo::CollectRecordLambdaFields(
909 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000910 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000911 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
912 // has the name and the location of the variable so we should iterate over
913 // both concurrently.
914 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
915 RecordDecl::field_iterator Field = CXXDecl->field_begin();
916 unsigned fieldno = 0;
917 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000918 E = CXXDecl->captures_end();
919 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000920 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000921 if (C.capturesVariable()) {
922 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000923 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000924 StringRef VName = V->getName();
925 uint64_t SizeInBitsOverride = 0;
926 if (Field->isBitField()) {
927 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
928 assert(SizeInBitsOverride && "found named 0-width bitfield");
929 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000930 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000931 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
932 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
933 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000934 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000935 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000936 // TODO: Need to handle 'this' in some way by probably renaming the
937 // this of the lambda class and having a field member of 'this' or
938 // by using AT_object_pointer for the function and having that be
939 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000940 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000942 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000943 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000944 "this", type, 0, f->getLocation(), f->getAccess(),
945 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000946
947 elements.push_back(fieldType);
948 }
949 }
950}
951
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000952llvm::DIDerivedType *
953CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000954 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000955 // Create the descriptor for the static variable, with or without
956 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000957 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000958 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
959 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000960
Eric Christopher91a31902013-01-16 01:22:32 +0000961 unsigned LineNumber = getLineNumber(Var->getLocation());
962 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000963 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000964 if (Var->getInit()) {
965 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000966 if (Value) {
967 if (Value->isInt())
968 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
969 if (Value->isFloat())
970 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
971 }
Eric Christopher91a31902013-01-16 01:22:32 +0000972 }
973
Adrian Prantl21361fb2014-08-29 22:44:27 +0000974 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000975 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000976 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000977 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000978 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000979}
980
Eric Christophere7b87e52014-10-26 23:40:33 +0000981void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000982 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
983 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000984 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000985 StringRef name = field->getName();
986 QualType type = field->getType();
987
988 // Ignore unnamed fields unless they're anonymous structs/unions.
989 if (name.empty() && !type->isRecordType())
990 return;
991
992 uint64_t SizeInBitsOverride = 0;
993 if (field->isBitField()) {
994 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
995 assert(SizeInBitsOverride && "found named 0-width bitfield");
996 }
997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000998 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000999 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
1000 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001001
1002 elements.push_back(fieldType);
1003}
1004
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001005void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001006 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001007 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001008 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001009 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1010
Eric Christopher91a31902013-01-16 01:22:32 +00001011 if (CXXDecl && CXXDecl->isLambda())
1012 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1013 else {
1014 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001015
Eric Christopher91a31902013-01-16 01:22:32 +00001016 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001017 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001018
Eric Christopher91a31902013-01-16 01:22:32 +00001019 // Static and non-static members should appear in the same order as
1020 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001021 for (const auto *I : record->decls())
1022 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001023 if (V->hasAttr<NoDebugAttr>())
1024 continue;
David Blaikiece763042013-08-20 21:49:21 +00001025 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001026 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001027 if (MI != StaticDataMemberCache.end()) {
1028 assert(MI->second &&
1029 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001030 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001031 } else {
1032 auto Field = CreateRecordStaticField(V, RecordTy, record);
1033 elements.push_back(Field);
1034 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001035 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001036 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1037 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001038
1039 // Bump field number for next field.
1040 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001041 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001042 }
1043}
1044
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001045llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001046CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001047 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001048 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001049 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001050 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001051 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001052 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1053 Func, Unit);
1054}
David Blaikie2aaf0652013-01-07 22:24:59 +00001055
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1057 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001059 llvm::DITypeRefArray Args(
1060 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001061 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001062 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001063
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001064 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001065
1066 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001067 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001068
David Blaikie2aaf0652013-01-07 22:24:59 +00001069 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001070 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001071 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1072 // Create pointer type directly in this case.
1073 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1074 QualType PointeeTy = ThisPtrTy->getPointeeType();
1075 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001076 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001077 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1079 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001080 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001081 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001082 // TODO: This and the artificial type below are misleading, the
1083 // types aren't artificial the argument is, but the current
1084 // metadata doesn't represent that.
1085 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1086 Elts.push_back(ThisPtrType);
1087 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001088 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001089 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001090 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1091 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001092 }
1093
1094 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001095 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1096 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001097
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001098 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001099
Adrian Prantl0630eb72013-12-18 21:48:18 +00001100 unsigned Flags = 0;
1101 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001103 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001105
Eric Christopher28a6db52015-10-15 06:56:08 +00001106 return DBuilder.createSubroutineType(EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001107}
1108
Eric Christopherb2a008c2013-05-16 00:45:12 +00001109/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001110/// inside a function.
1111static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1112 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1113 return isFunctionLocalClass(NRD);
1114 if (isa<FunctionDecl>(RD->getDeclContext()))
1115 return true;
1116 return false;
1117}
1118
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1120 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001121 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001122 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
1127 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1128 // make sense to give a single ctor/dtor a linkage name.
1129 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001130 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1131 // property to use here. It may've been intended to model "is non-external
1132 // type" but misses cases of non-function-local but non-external classes such
1133 // as those in anonymous namespaces as well as the reverse - external types
1134 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001135 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1136 MethodLinkageName = CGM.getMangledName(Method);
1137
1138 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001139 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001140 unsigned MethodLine = 0;
1141 if (!Method->isImplicit()) {
1142 MethodDefUnit = getOrCreateFile(Method->getLocation());
1143 MethodLine = getLineNumber(Method->getLocation());
1144 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001145
1146 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001147 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001150
Guy Benyei11169dd2012-12-18 14:30:41 +00001151 if (Method->isVirtual()) {
1152 if (Method->isPure())
1153 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1154 else
1155 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001156
Guy Benyei11169dd2012-12-18 14:30:41 +00001157 // It doesn't make sense to give a virtual destructor a vtable index,
1158 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001159 // FIXME: Add proper support for debug info for virtual calls in
1160 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1161 // lookup if we have multiple or virtual inheritance.
1162 if (!isa<CXXDestructorDecl>(Method) &&
1163 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001164 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 ContainingType = RecordTy;
1166 }
1167
1168 unsigned Flags = 0;
1169 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001170 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001171 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001172 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1173 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001175 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001176 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001177 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001178 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001179 }
1180 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001181 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001182 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001183 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001184 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001185 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001186
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1188 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001189 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1190 MethodTy, /*isLocalToUnit=*/false,
1191 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001192 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001193
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001194 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001195
1196 return SP;
1197}
1198
Eric Christophere7b87e52014-10-26 23:40:33 +00001199void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001200 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1201 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001202
1203 // Since we want more than just the individual member decls if we
1204 // have templated functions iterate over every declaration to gather
1205 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001206 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001207 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1208 // If the member is implicit, don't add it to the member list. This avoids
1209 // the member being added to type units by LLVM, while still allowing it
1210 // to be emitted into the type declaration/reference inside the compile
1211 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001212 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001213 // FIXME: Handle Using(Shadow?)Decls here to create
1214 // DW_TAG_imported_declarations inside the class for base decls brought into
1215 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1216 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1217 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001218 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001219 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001220
1221 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1222 continue;
1223
David Blaikiefd580722014-10-06 05:18:55 +00001224 // Reuse the existing member function declaration if it exists.
1225 // It may be associated with the declaration of the type & should be
1226 // reused as we're building the definition.
1227 //
1228 // This situation can arise in the vtable-based debug info reduction where
1229 // implicit members are emitted in a non-vtable TU.
1230 auto MI = SPCache.find(Method->getCanonicalDecl());
1231 EltTys.push_back(MI == SPCache.end()
1232 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001233 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001234 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001235}
Guy Benyei11169dd2012-12-18 14:30:41 +00001236
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001237void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001238 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001241 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 unsigned BFlags = 0;
1243 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001244
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001246 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001247
Aaron Ballman574705e2014-03-13 15:41:46 +00001248 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001249 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1250 // virtual base offset offset is -ve. The code generator emits dwarf
1251 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001252 BaseOffset = 0 - CGM.getItaniumVTableContext()
1253 .getVirtualBaseOffsetOffset(RD, Base)
1254 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001255 } else {
1256 // In the MS ABI, store the vbtable offset, which is analogous to the
1257 // vbase offset offset in Itanium.
1258 BaseOffset =
1259 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1260 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001261 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001262 } else
1263 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1264 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1265 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001266
Adrian Prantl21361fb2014-08-29 22:44:27 +00001267 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001268 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001269 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001270 EltTys.push_back(DTy);
1271 }
1272}
1273
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001274llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001275CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1276 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001277 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001278 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1280 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001281 StringRef Name;
1282 if (TPList)
1283 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001284 switch (TA.getKind()) {
1285 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001286 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001287 TemplateParams.push_back(
1288 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001289 } break;
1290 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001291 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001292 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1293 TheCU, Name, TTy,
1294 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001295 } break;
1296 case TemplateArgument::Declaration: {
1297 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001298 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001299 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001300 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001301 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001302 // Variable pointer template parameters have a value that is the address
1303 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001304 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001305 V = CGM.GetAddrOfGlobalVar(VD);
1306 // Member function pointers have special support for building them, though
1307 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001308 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001309 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001310 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001311 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001312 // Member data pointers have special handling too to compute the fixed
1313 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001314 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001315 // These five lines (& possibly the above member function pointer
1316 // handling) might be able to be refactored to use similar code in
1317 // CodeGenModule::getMemberPointerConstant
1318 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1319 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001320 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001321 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001322 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001323 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1324 TheCU, Name, TTy,
1325 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001326 } break;
1327 case TemplateArgument::NullPtr: {
1328 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001329 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001330 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001331 // Special case member data pointer null values since they're actually -1
1332 // instead of zero.
1333 if (const MemberPointerType *MPT =
1334 dyn_cast<MemberPointerType>(T.getTypePtr()))
1335 // But treat member function pointers as simple zero integers because
1336 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1337 // CodeGen grows handling for values of non-null member function
1338 // pointers then perhaps we could remove this special case and rely on
1339 // EmitNullMemberPointer for member function pointers.
1340 if (MPT->isMemberDataPointer())
1341 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1342 if (!V)
1343 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001344 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1345 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001346 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001347 case TemplateArgument::Template:
1348 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001349 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001350 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1351 break;
1352 case TemplateArgument::Pack:
1353 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1354 TheCU, Name, nullptr,
1355 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1356 break;
David Majnemer5559d472013-08-24 08:21:10 +00001357 case TemplateArgument::Expression: {
1358 const Expr *E = TA.getAsExpr();
1359 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001360 if (E->isGLValue())
1361 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001362 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001363 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001364 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001365 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1366 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001367 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001368 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001369 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001370 case TemplateArgument::Null:
1371 llvm_unreachable(
1372 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 }
1374 }
1375 return DBuilder.getOrCreateArray(TemplateParams);
1376}
1377
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001378llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001379CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001380 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001381 if (FD->getTemplatedKind() ==
1382 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001383 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1384 ->getTemplate()
1385 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001386 return CollectTemplateParams(
1387 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001390}
1391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1393 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001394 // Always get the full list of parameters, not just the ones from
1395 // the specialization.
1396 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001397 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001398 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001399 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001400}
1401
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001402llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001403 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 return VTablePtrType;
1405
1406 ASTContext &Context = CGM.getContext();
1407
1408 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001409 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001410 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001411 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001412 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001413 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001414 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1416 return VTablePtrType;
1417}
1418
Guy Benyei11169dd2012-12-18 14:30:41 +00001419StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001420 // Copy the gdb compatible name on the side and use its reference.
1421 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001422}
1423
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001425 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1427
1428 // If there is a primary base then it will hold vtable info.
1429 if (RL.getPrimaryBase())
1430 return;
1431
1432 // If this class is not dynamic then there is not any vtable info to collect.
1433 if (!RD->isDynamicClass())
1434 return;
1435
1436 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001437 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001438 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001439 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 EltTys.push_back(VPTR);
1441}
1442
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001444 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001445 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001446 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001447 return T;
1448}
1449
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001450llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001451 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001452 return getOrCreateStandaloneType(D, Loc);
1453}
1454
1455llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1456 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001457 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001458 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001459 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001460 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001461
Adrian Prantl73409ce2013-03-11 18:33:46 +00001462 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001463 return T;
1464}
1465
David Blaikie483a9da2014-05-06 18:35:21 +00001466void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001467 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001468 return;
1469 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001470 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001471 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001473 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001474 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001475 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001476 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001477}
1478
David Blaikieb2e86eb2013-08-15 20:49:17 +00001479void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001480 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001481 !CGM.getLangOpts().CPlusPlus)
1482 completeRequiredType(RD);
1483}
1484
1485void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001486 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001487 return;
1488
David Blaikie6943dea2013-08-20 01:28:15 +00001489 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1490 if (CXXDecl->isDynamicClass())
1491 return;
1492
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001493 if (DebugTypeExtRefs && RD->isFromASTFile())
1494 return;
1495
David Blaikieb2e86eb2013-08-15 20:49:17 +00001496 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001497 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001498 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001499 completeClassData(RD);
1500}
1501
1502void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001503 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001504 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001505 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001506 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001507 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001509 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001510 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001511 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001512 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001513}
1514
David Blaikie0e716b42014-03-03 23:48:23 +00001515static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1516 CXXRecordDecl::method_iterator End) {
1517 for (; I != End; ++I)
1518 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001519 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1520 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001521 return true;
1522 return false;
1523}
1524
Adrian Prantl05fefa42016-04-25 20:52:40 +00001525/// Does a type definition exist in an imported clang module?
1526static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001527 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001528 return false;
1529 if (!RD->isExternallyVisible() && RD->getName().empty())
1530 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001531 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1532 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001533 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1534 // Make sure the instantiation is actually in a module.
1535 if (CXXDecl->field_begin() != CXXDecl->field_end())
1536 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001537 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001538
Adrian Prantl05fefa42016-04-25 20:52:40 +00001539 return true;
1540}
1541
Benjamin Kramer8c305922016-02-02 11:06:51 +00001542static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1543 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001544 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001545 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001546 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001547
Benjamin Kramer8c305922016-02-02 11:06:51 +00001548 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001549 return false;
1550
1551 if (!LangOpts.CPlusPlus)
1552 return false;
1553
1554 if (!RD->isCompleteDefinitionRequired())
1555 return true;
1556
1557 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1558
1559 if (!CXXDecl)
1560 return false;
1561
1562 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1563 return true;
1564
1565 TemplateSpecializationKind Spec = TSK_Undeclared;
1566 if (const ClassTemplateSpecializationDecl *SD =
1567 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1568 Spec = SD->getSpecializationKind();
1569
1570 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1571 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1572 CXXDecl->method_end()))
1573 return true;
1574
1575 return false;
1576}
1577
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001578llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001580 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001581 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1582 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001583 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001584 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001585 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001586 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001587
David Blaikieb2e86eb2013-08-15 20:49:17 +00001588 return CreateTypeDefinition(Ty);
1589}
1590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001591llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001592 RecordDecl *RD = Ty->getDecl();
1593
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001595 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001596
1597 // Records and classes and unions can all be recursive. To handle them, we
1598 // first generate a debug descriptor for the struct as a forward declaration.
1599 // Then (if it is a definition) we go through and get debug info for all of
1600 // its members. Finally, we create a descriptor for the complete type (which
1601 // may refer to the forward decl if the struct is recursive) and replace all
1602 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001603 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001604
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001605 const RecordDecl *D = RD->getDefinition();
1606 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001607 return FwdDecl;
1608
David Blaikieadfbf992013-08-18 16:55:33 +00001609 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1610 CollectContainingType(CXXDecl, FwdDecl);
1611
Guy Benyei11169dd2012-12-18 14:30:41 +00001612 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001613 LexicalBlockStack.emplace_back(&*FwdDecl);
1614 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001615
Guy Benyei11169dd2012-12-18 14:30:41 +00001616 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001617 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001618 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001619
1620 // Note: The split of CXXDecl information here is intentional, the
1621 // gdb tests will depend on a certain ordering at printout. The debug
1622 // information offsets are still correct if we merge them all together
1623 // though.
1624 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1625 if (CXXDecl) {
1626 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1627 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1628 }
1629
Eric Christopher91a31902013-01-16 01:22:32 +00001630 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001632 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001633 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001634
1635 LexicalBlockStack.pop_back();
1636 RegionMap.erase(Ty->getDecl());
1637
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001638 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001639 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001640
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001641 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001642 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001643 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001644
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001645 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001646 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001647}
1648
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001649llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1650 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001651 // Ignore protocols.
1652 return getOrCreateType(Ty->getBaseType(), Unit);
1653}
1654
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001655/// \return true if Getter has the default name for the property PD.
1656static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1657 const ObjCMethodDecl *Getter) {
1658 assert(PD);
1659 if (!Getter)
1660 return true;
1661
1662 assert(Getter->getDeclName().isObjCZeroArgSelector());
1663 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001664 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001665}
1666
1667/// \return true if Setter has the default name for the property PD.
1668static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1669 const ObjCMethodDecl *Setter) {
1670 assert(PD);
1671 if (!Setter)
1672 return true;
1673
1674 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001675 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001676 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001677}
1678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1680 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001681 ObjCInterfaceDecl *ID = Ty->getDecl();
1682 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001683 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001685 // Return a forward declaration if this type was imported from a clang module,
1686 // and this is not the compile unit with the implementation of the type (which
1687 // may contain hidden ivars).
1688 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1689 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001690 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1691 ID->getName(),
1692 getDeclContextDescriptor(ID), Unit, 0);
1693
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001695 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001697 auto RuntimeLang =
1698 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001699
1700 // If this is just a forward declaration return a special forward-declaration
1701 // debug type since we won't be able to lay out the entire type.
1702 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001703 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001704 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001705 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001706 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1707 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001708 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001709 return FwdDecl;
1710 }
1711
David Blaikieef8a9512014-05-05 23:23:53 +00001712 return CreateTypeDefinition(Ty, Unit);
1713}
1714
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001715llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001716CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1717 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001718 // Use the Module pointer as the key into the cache. This is a
1719 // nullptr if the "Module" is a PCH, which is safe because we don't
1720 // support chained PCH debug info, so there can only be a single PCH.
1721 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001722 auto ModRef = ModuleCache.find(M);
1723 if (ModRef != ModuleCache.end())
1724 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001725
1726 // Macro definitions that were defined with "-D" on the command line.
1727 SmallString<128> ConfigMacros;
1728 {
1729 llvm::raw_svector_ostream OS(ConfigMacros);
1730 const auto &PPOpts = CGM.getPreprocessorOpts();
1731 unsigned I = 0;
1732 // Translate the macro definitions back into a commmand line.
1733 for (auto &M : PPOpts.Macros) {
1734 if (++I > 1)
1735 OS << " ";
1736 const std::string &Macro = M.first;
1737 bool Undef = M.second;
1738 OS << "\"-" << (Undef ? 'U' : 'D');
1739 for (char c : Macro)
1740 switch (c) {
1741 case '\\' : OS << "\\\\"; break;
1742 case '"' : OS << "\\\""; break;
1743 default: OS << c;
1744 }
1745 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001746 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001747 }
Adrian Prantl66689202015-09-18 23:01:45 +00001748
Adrian Prantl835e6632015-09-24 16:10:10 +00001749 bool IsRootModule = M ? !M->Parent : true;
1750 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001751 // PCH files don't have a signature field in the control block,
1752 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001753 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001754 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001755 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001756 Mod.getPath(), TheCU->getProducer(), true,
1757 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001758 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001759 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001760 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001761 llvm::DIModule *Parent =
1762 IsRootModule ? nullptr
1763 : getOrCreateModuleRef(
1764 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1765 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001766 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001767 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1768 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001769 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001770 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001771}
1772
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001773llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1774 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001775 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001776 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001777 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001778 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001779
1780 // Bit size, align and offset of the type.
1781 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1782 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1783
1784 unsigned Flags = 0;
1785 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001786 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001787
Adrian Prantlfd696112015-10-01 00:48:51 +00001788 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001789 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001790 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1791 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001792
David Blaikieef8a9512014-05-05 23:23:53 +00001793 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001794 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001795
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001796 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001797 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001798 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799
1800 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001801 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001802
1803 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1804 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001805 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001806 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001807 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001808 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001809
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001810 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001811 EltTys.push_back(InhTag);
1812 }
1813
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001814 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001815 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001817 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 unsigned PLine = getLineNumber(Loc);
1819 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1820 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001821 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1822 PD->getName(), PUnit, PLine,
1823 hasDefaultGetterName(PD, Getter) ? ""
1824 : getSelectorName(PD->getGetterName()),
1825 hasDefaultSetterName(PD, Setter) ? ""
1826 : getSelectorName(PD->getSetterName()),
1827 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001829 };
1830 {
1831 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001832 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001833 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001834 PropertySet.insert(PD->getIdentifier());
1835 AddProperty(PD);
1836 }
Manman Renefe1bac2016-01-27 20:00:32 +00001837 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001838 // Don't emit duplicate metadata for properties that were already in a
1839 // class extension.
1840 if (!PropertySet.insert(PD->getIdentifier()).second)
1841 continue;
1842 AddProperty(PD);
1843 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001844 }
1845
1846 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1847 unsigned FieldNo = 0;
1848 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1849 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001850 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001851 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001852 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001853
Guy Benyei11169dd2012-12-18 14:30:41 +00001854 StringRef FieldName = Field->getName();
1855
1856 // Ignore unnamed fields.
1857 if (FieldName.empty())
1858 continue;
1859
1860 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001861 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 unsigned FieldLine = getLineNumber(Field->getLocation());
1863 QualType FType = Field->getType();
1864 uint64_t FieldSize = 0;
1865 unsigned FieldAlign = 0;
1866
1867 if (!FType->isIncompleteArrayType()) {
1868
1869 // Bit size, align and offset of the type.
1870 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001871 ? Field->getBitWidthValue(CGM.getContext())
1872 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001873 FieldAlign = CGM.getContext().getTypeAlign(FType);
1874 }
1875
1876 uint64_t FieldOffset;
1877 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1878 // We don't know the runtime offset of an ivar if we're using the
1879 // non-fragile ABI. For bitfields, use the bit offset into the first
1880 // byte of storage of the bitfield. For other fields, use zero.
1881 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001882 FieldOffset =
1883 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 FieldOffset %= CGM.getContext().getCharWidth();
1885 } else {
1886 FieldOffset = 0;
1887 }
1888 } else {
1889 FieldOffset = RL.getFieldOffset(FieldNo);
1890 }
1891
1892 unsigned Flags = 0;
1893 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001894 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001895 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001896 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001897 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001898 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001899
Craig Topper8a13c412014-05-21 05:09:00 +00001900 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001901 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001902 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001903 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001904 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001905 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001906 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001907 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001908 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1909 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001910 PropertyNode = DBuilder.createObjCProperty(
1911 PD->getName(), PUnit, PLine,
1912 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1913 PD->getGetterName()),
1914 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1915 PD->getSetterName()),
1916 PD->getPropertyAttributes(),
1917 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 }
1919 }
1920 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001921 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1922 FieldSize, FieldAlign, FieldOffset, Flags,
1923 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 EltTys.push_back(FieldTy);
1925 }
1926
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001927 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001928 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001929
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001931 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001932}
1933
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001934llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1935 llvm::DIFile *Unit) {
1936 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 int64_t Count = Ty->getNumElements();
1938 if (Count == 0)
1939 // If number of elements are not known then this is an unbounded array.
1940 // Use Count == -1 to express such arrays.
1941 Count = -1;
1942
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001943 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001944 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001945
1946 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1947 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1948
1949 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1950}
1951
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001952llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 uint64_t Size;
1954 uint64_t Align;
1955
1956 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1957 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1958 Size = 0;
1959 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001960 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 } else if (Ty->isIncompleteArrayType()) {
1962 Size = 0;
1963 if (Ty->getElementType()->isIncompleteType())
1964 Align = 0;
1965 else
1966 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001967 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001968 Size = 0;
1969 Align = 0;
1970 } else {
1971 // Size and align of the whole array, not the element type.
1972 Size = CGM.getContext().getTypeSize(Ty);
1973 Align = CGM.getContext().getTypeAlign(Ty);
1974 }
1975
1976 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1977 // interior arrays, do we care? Why aren't nested arrays represented the
1978 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001979 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001980 QualType EltTy(Ty, 0);
1981 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1982 // If the number of elements is known, then count is that number. Otherwise,
1983 // it's -1. This allows us to represent a subrange with an array of 0
1984 // elements, like this:
1985 //
1986 // struct foo {
1987 // int x[0];
1988 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001989 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001990 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1991 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001992
Guy Benyei11169dd2012-12-18 14:30:41 +00001993 // FIXME: Verify this is right for VLAs.
1994 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1995 EltTy = Ty->getElementType();
1996 }
1997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001998 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001999
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002000 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2001 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002002}
2003
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002004llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2005 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002006 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2007 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002008}
2009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002010llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2011 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002012 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2013 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002014}
2015
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002016llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2017 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00002018 uint64_t Size =
2019 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002020 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002021 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002022 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00002023 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002024
2025 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002026 Ty->getPointeeType()->getAs<FunctionProtoType>();
2027 return DBuilder.createMemberPointerType(
2028 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2029 Ty->getClass(), FPT->getTypeQuals())),
2030 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00002031 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00002032}
2033
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002034llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 // Ignore the atomic wrapping
2036 // FIXME: What is the correct representation?
2037 return getOrCreateType(Ty->getValueType(), U);
2038}
2039
Xiuli Pan9c14e282016-01-09 12:53:17 +00002040llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2041 llvm::DIFile *U) {
2042 return getOrCreateType(Ty->getElementType(), U);
2043}
2044
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002045llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002046 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002047
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 uint64_t Size = 0;
2049 uint64_t Align = 0;
2050 if (!ED->getTypeForDecl()->isIncompleteType()) {
2051 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2052 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2053 }
2054
Manman Rene0064d82013-08-29 23:19:58 +00002055 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2056
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002057 bool isImportedFromModule =
2058 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2059
Guy Benyei11169dd2012-12-18 14:30:41 +00002060 // If this is just a forward declaration, construct an appropriately
2061 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002062 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002063 // Note that it is possible for enums to be created as part of
2064 // their own declcontext. In this case a FwdDecl will be created
2065 // twice. This doesn't cause a problem because both FwdDecls are
2066 // entered into the ReplaceMap: finalize() will replace the first
2067 // FwdDecl with the second and then replace the second with
2068 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002069 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002070 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002071 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2072 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002073
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 unsigned Line = getLineNumber(ED->getLocation());
2075 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002076 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002077 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2078 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002079
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002080 ReplaceMap.emplace_back(
2081 std::piecewise_construct, std::make_tuple(Ty),
2082 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002083 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002084 }
2085
David Blaikie483a9da2014-05-06 18:35:21 +00002086 return CreateTypeDefinition(Ty);
2087}
2088
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002089llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002090 const EnumDecl *ED = Ty->getDecl();
2091 uint64_t Size = 0;
2092 uint64_t Align = 0;
2093 if (!ED->getTypeForDecl()->isIncompleteType()) {
2094 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2095 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2096 }
2097
2098 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2099
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002100 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002101 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002103 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002104 Enumerators.push_back(DBuilder.createEnumerator(
2105 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002106 }
2107
2108 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002109 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002110
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002111 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002113 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002114 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002115 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2116 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2117 Line, Size, Align, EltArray, ClassTy,
2118 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002119}
2120
David Blaikie05491062013-01-21 04:37:12 +00002121static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2122 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002124 Qualifiers InnerQuals = T.getLocalQualifiers();
2125 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2126 // that is already there.
2127 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2128 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002129 QualType LastT = T;
2130 switch (T->getTypeClass()) {
2131 default:
David Blaikie05491062013-01-21 04:37:12 +00002132 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002133 case Type::TemplateSpecialization: {
2134 const auto *Spec = cast<TemplateSpecializationType>(T);
2135 if (Spec->isTypeAlias())
2136 return C.getQualifiedType(T.getTypePtr(), Quals);
2137 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002138 break;
2139 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 case Type::TypeOfExpr:
2141 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2142 break;
2143 case Type::TypeOf:
2144 T = cast<TypeOfType>(T)->getUnderlyingType();
2145 break;
2146 case Type::Decltype:
2147 T = cast<DecltypeType>(T)->getUnderlyingType();
2148 break;
2149 case Type::UnaryTransform:
2150 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2151 break;
2152 case Type::Attributed:
2153 T = cast<AttributedType>(T)->getEquivalentType();
2154 break;
2155 case Type::Elaborated:
2156 T = cast<ElaboratedType>(T)->getNamedType();
2157 break;
2158 case Type::Paren:
2159 T = cast<ParenType>(T)->getInnerType();
2160 break;
David Blaikie05491062013-01-21 04:37:12 +00002161 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 break;
2164 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002165 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002166 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002167 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 break;
2169 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002170
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002172 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 } while (true);
2174}
2175
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002176llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002177
2178 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002179 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002180
David Blaikief427b002014-05-06 03:42:01 +00002181 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 if (it != TypeCache.end()) {
2183 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002184 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002185 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 }
2187
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002188 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002189}
2190
David Blaikie0e716b42014-03-03 23:48:23 +00002191void CGDebugInfo::completeTemplateDefinition(
2192 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002193 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002194 return;
2195
David Blaikie0e716b42014-03-03 23:48:23 +00002196 completeClassData(&SD);
2197 // In case this type has no member function definitions being emitted, ensure
2198 // it is retained
2199 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2200}
2201
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002202llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002203 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002204 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002205
2206 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002207 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002209 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 return T;
2211
Adrian Prantlca844182015-09-11 17:23:03 +00002212 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002213 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002214
2215 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002216 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002217
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 return Res;
2219}
2220
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002221llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002222 // A forward declaration inside a module header does not belong to the module.
2223 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2224 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002225 if (DebugTypeExtRefs && D->isFromASTFile()) {
2226 // Record a reference to an imported clang module or precompiled header.
2227 auto *Reader = CGM.getContext().getExternalSource();
2228 auto Idx = D->getOwningModuleID();
2229 auto Info = Reader->getSourceDescriptor(Idx);
2230 if (Info)
2231 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2232 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002233 // We are building a clang module or a precompiled header.
2234 //
2235 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2236 // and it wouldn't be necessary to specify the parent scope
2237 // because the type is already unique by definition (it would look
2238 // like the output of -fno-standalone-debug). On the other hand,
2239 // the parent scope helps a consumer to quickly locate the object
2240 // file where the type's definition is located, so it might be
2241 // best to make this behavior a command line or debugger tuning
2242 // option.
2243 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2244 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002245 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002246 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2247 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002248 } else {
2249 // This the precompiled header being built.
2250 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002251 }
2252 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002253
Adrian Prantl9402cef2015-09-20 16:51:35 +00002254 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002255}
2256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002257llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 // Handle qualifiers, which recursively handles what they refer to.
2259 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002260 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002261
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 // Work out details of type.
2263 switch (Ty->getTypeClass()) {
2264#define TYPE(Class, Base)
2265#define ABSTRACT_TYPE(Class, Base)
2266#define NON_CANONICAL_TYPE(Class, Base)
2267#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2268#include "clang/AST/TypeNodes.def"
2269 llvm_unreachable("Dependent types cannot show up in debug information");
2270
2271 case Type::ExtVector:
2272 case Type::Vector:
2273 return CreateType(cast<VectorType>(Ty), Unit);
2274 case Type::ObjCObjectPointer:
2275 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2276 case Type::ObjCObject:
2277 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2278 case Type::ObjCInterface:
2279 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2280 case Type::Builtin:
2281 return CreateType(cast<BuiltinType>(Ty));
2282 case Type::Complex:
2283 return CreateType(cast<ComplexType>(Ty));
2284 case Type::Pointer:
2285 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002286 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002287 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002288 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002289 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002290 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002291 case Type::BlockPointer:
2292 return CreateType(cast<BlockPointerType>(Ty), Unit);
2293 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002294 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002296 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002298 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002299 case Type::FunctionProto:
2300 case Type::FunctionNoProto:
2301 return CreateType(cast<FunctionType>(Ty), Unit);
2302 case Type::ConstantArray:
2303 case Type::VariableArray:
2304 case Type::IncompleteArray:
2305 return CreateType(cast<ArrayType>(Ty), Unit);
2306
2307 case Type::LValueReference:
2308 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2309 case Type::RValueReference:
2310 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2311
2312 case Type::MemberPointer:
2313 return CreateType(cast<MemberPointerType>(Ty), Unit);
2314
2315 case Type::Atomic:
2316 return CreateType(cast<AtomicType>(Ty), Unit);
2317
Xiuli Pan9c14e282016-01-09 12:53:17 +00002318 case Type::Pipe:
2319 return CreateType(cast<PipeType>(Ty), Unit);
2320
Guy Benyei11169dd2012-12-18 14:30:41 +00002321 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002322 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2323
David Blaikie42edade2014-11-11 20:44:45 +00002324 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002325 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002326 case Type::Elaborated:
2327 case Type::Paren:
2328 case Type::SubstTemplateTypeParm:
2329 case Type::TypeOfExpr:
2330 case Type::TypeOf:
2331 case Type::Decltype:
2332 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002333 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002334 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002336
David Blaikie42edade2014-11-11 20:44:45 +00002337 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002338}
2339
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002340llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2341 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002342 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002343
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002344 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002345
2346 // We may have cached a forward decl when we could have created
2347 // a non-forward decl. Go ahead and create a non-forward decl
2348 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002349 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002350 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002351
2352 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002353 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002354
2355 // Propagate members from the declaration to the definition
2356 // CreateType(const RecordType*) will overwrite this with the members in the
2357 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002358 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002359
Guy Benyei11169dd2012-12-18 14:30:41 +00002360 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002361 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002362 return Res;
2363}
2364
2365// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002367 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002368
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002370 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002371 unsigned Line = getLineNumber(RD->getLocation());
2372 StringRef RDName = getClassName(RD);
2373
Amjad Abouddc4531e2016-04-30 01:44:38 +00002374 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002375
David Blaikied2785892013-08-18 17:36:19 +00002376 // If we ended up creating the type during the context chain construction,
2377 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002378 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002379 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002380 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002381 return T;
David Blaikied2785892013-08-18 17:36:19 +00002382
Adrian Prantl381e7552014-02-04 21:29:50 +00002383 // If this is just a forward or incomplete declaration, construct an
2384 // appropriately marked node and just return it.
2385 const RecordDecl *D = RD->getDefinition();
2386 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002387 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002388
2389 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2390 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002391
Manman Rene0064d82013-08-29 23:19:58 +00002392 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002394 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002395 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2396 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002397
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002398 // Elements of composite types usually have back to the type, creating
2399 // uniquing cycles. Distinct nodes are more efficient.
2400 switch (RealDecl->getTag()) {
2401 default:
2402 llvm_unreachable("invalid composite type tag");
2403
2404 case llvm::dwarf::DW_TAG_array_type:
2405 case llvm::dwarf::DW_TAG_enumeration_type:
2406 // Array elements and most enumeration elements don't have back references,
2407 // so they don't tend to be involved in uniquing cycles and there is some
2408 // chance of merging them when linking together two modules. Only make
2409 // them distinct if they are ODR-uniqued.
2410 if (FullName.empty())
2411 break;
2412
2413 case llvm::dwarf::DW_TAG_structure_type:
2414 case llvm::dwarf::DW_TAG_union_type:
2415 case llvm::dwarf::DW_TAG_class_type:
2416 // Immediatley resolve to a distinct node.
2417 RealDecl =
2418 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2419 break;
2420 }
2421
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002422 RegionMap[Ty->getDecl()].reset(RealDecl);
2423 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002424
David Blaikieadfbf992013-08-18 16:55:33 +00002425 if (const ClassTemplateSpecializationDecl *TSpecial =
2426 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002427 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002428 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002429 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430}
2431
David Blaikieadfbf992013-08-18 16:55:33 +00002432void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002433 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002434 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002435 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002436 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2437 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002438 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002439 while (1) {
2440 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2441 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2442 if (PBT && !BRL.isPrimaryBaseVirtual())
2443 PBase = PBT;
2444 else
2445 break;
2446 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002447 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002448 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2449 getOrCreateFile(RD->getLocation())));
2450 } else if (RD->isDynamicClass())
2451 ContainingType = RealDecl;
2452
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002453 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002454}
2455
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002456llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002457 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002458 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2460 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002462 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 *Offset += FieldSize;
2464 return Ty;
2465}
2466
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002468 StringRef &Name,
2469 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002470 llvm::DIScope *&FDContext,
2471 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002472 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002473 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2474 Name = getFunctionName(FD);
2475 // Use mangled name as linkage name for C/C++ functions.
2476 if (FD->hasPrototype()) {
2477 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002478 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002479 }
2480 // No need to replicate the linkage name if it isn't different from the
2481 // subprogram name, no need to have it at all unless coverage is enabled or
2482 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002483 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2484 !CGM.getCodeGenOpts().EmitGcovNotes &&
2485 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002486 LinkageName = StringRef();
2487
Benjamin Kramer8c305922016-02-02 11:06:51 +00002488 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002489 if (const NamespaceDecl *NSDecl =
2490 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2491 FDContext = getOrCreateNameSpace(NSDecl);
2492 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002493 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2494 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2495 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2496 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002497 // Collect template parameters.
2498 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2499 }
2500}
2501
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002502void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002503 unsigned &LineNo, QualType &T,
2504 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002505 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002506 Unit = getOrCreateFile(VD->getLocation());
2507 LineNo = getLineNumber(VD->getLocation());
2508
2509 setLocation(VD->getLocation());
2510
2511 T = VD->getType();
2512 if (T->isIncompleteArrayType()) {
2513 // CodeGen turns int[] into int[1] so we'll do the same here.
2514 llvm::APInt ConstVal(32, 1);
2515 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2516
2517 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2518 ArrayType::Normal, 0);
2519 }
2520
2521 Name = VD->getName();
2522 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2523 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2524 LinkageName = CGM.getMangledName(VD);
2525 if (LinkageName == Name)
2526 LinkageName = StringRef();
2527
2528 // Since we emit declarations (DW_AT_members) for static members, place the
2529 // definition of those static members in the namespace they were declared in
2530 // in the source code (the lexical decl context).
2531 // FIXME: Generalize this for even non-member global variables where the
2532 // declaration and definition may have different lexical decl contexts, once
2533 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002534 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2535 : VD->getDeclContext();
2536 // When a record type contains an in-line initialization of a static data
2537 // member, and the record type is marked as __declspec(dllexport), an implicit
2538 // definition of the member will be created in the record context. DWARF
2539 // doesn't seem to have a nice way to describe this in a form that consumers
2540 // are likely to understand, so fake the "normal" situation of a definition
2541 // outside the class by putting it in the global scope.
2542 if (DC->isRecord())
2543 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002544
Amjad Abouddc4531e2016-04-30 01:44:38 +00002545 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2546 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002547}
2548
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002549llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002550CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002551 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002552 StringRef Name, LinkageName;
2553 unsigned Flags = 0;
2554 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002555 llvm::DIFile *Unit = getOrCreateFile(Loc);
2556 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002557 unsigned Line = getLineNumber(Loc);
2558
2559 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2560 TParamsArray, Flags);
2561 // Build function type.
2562 SmallVector<QualType, 16> ArgTypes;
2563 for (const ParmVarDecl *Parm: FD->parameters())
2564 ArgTypes.push_back(Parm->getType());
2565 QualType FnType =
2566 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2567 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002568 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002569 DContext, Name, LinkageName, Unit, Line,
2570 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002571 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002572 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002573 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002574 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2575 std::make_tuple(CanonDecl),
2576 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002577 return SP;
2578}
2579
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002580llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002581CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2582 QualType T;
2583 StringRef Name, LinkageName;
2584 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002585 llvm::DIFile *Unit = getOrCreateFile(Loc);
2586 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002587 unsigned Line = getLineNumber(Loc);
2588
2589 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002590 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2591 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2592 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002593 FwdDeclReplaceMap.emplace_back(
2594 std::piecewise_construct,
2595 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2596 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002597 return GV;
2598}
2599
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002600llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002601 // We only need a declaration (not a definition) of the type - so use whatever
2602 // we would otherwise do to get a type for a pointee. (forward declarations in
2603 // limited debug info, full definitions (if the type definition is available)
2604 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002605 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2606 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002607 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002608 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002609
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002610 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002611 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002612
2613 // No definition for now. Emit a forward definition that might be
2614 // merged with a potential upcoming definition.
2615 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2616 return getFunctionForwardDeclaration(FD);
2617 else if (const auto *VD = dyn_cast<VarDecl>(D))
2618 return getGlobalVariableForwardDeclaration(VD);
2619
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002620 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002621}
2622
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002623llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002624 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002625 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002626
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002628 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002629 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002630
2631 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002632 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002633
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002634 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002635 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002636 if (const CXXMethodDecl *MD =
2637 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002638 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002639 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002640 }
2641 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002642 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002643 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002644 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002645 return SP;
2646 }
2647
Aaron Ballman86c93902014-03-06 23:45:36 +00002648 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002649 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002650 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002651 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002652 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 return SP;
2654 }
2655 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002656 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002657}
2658
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002659// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002660// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002661llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002662 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002663 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002664 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002665 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2666 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002667 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002668
2669 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2670 return getOrCreateMethodType(Method, F);
2671 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2672 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002673 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002674
2675 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002676 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002677
2678 // Replace the instancetype keyword with the actual type.
2679 if (ResultTy == CGM.getContext().getObjCInstanceType())
2680 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002681 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002682
Adrian Prantl7bec9032013-05-10 21:08:31 +00002683 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002684 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002685 QualType SelfDeclTy;
2686 if (auto *SelfDecl = OMethod->getSelfDecl())
2687 SelfDeclTy = SelfDecl->getType();
2688 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2689 if (FPT->getNumParams() > 1)
2690 SelfDeclTy = FPT->getParamType(0);
2691 if (!SelfDeclTy.isNull())
2692 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002693 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002694 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002695 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002696 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002697 for (const auto *PI : OMethod->params())
2698 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002699 // Variadic methods need a special marker at the end of the type list.
2700 if (OMethod->isVariadic())
2701 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002703 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Eric Christopher28a6db52015-10-15 06:56:08 +00002704 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002706
Adrian Prantl800faef2014-02-25 23:42:18 +00002707 // Handle variadic function types; they need an additional
2708 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002709 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2710 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002711 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002712 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2713 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2714 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2715 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2716 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002717 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +00002718 return DBuilder.createSubroutineType(EltTypeArray);
Adrian Prantld45ba252014-02-25 19:38:11 +00002719 }
2720
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002721 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002722}
2723
Eric Christophere7b87e52014-10-26 23:40:33 +00002724void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2725 SourceLocation ScopeLoc, QualType FnType,
2726 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002727
2728 StringRef Name;
2729 StringRef LinkageName;
2730
2731 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2732
2733 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002734 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002735
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002737 llvm::DIFile *Unit = getOrCreateFile(Loc);
2738 llvm::DIScope *FDContext = Unit;
2739 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 if (!HasDecl) {
2741 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002742 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002744 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002745 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002747 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002748 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002749 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002750 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 return;
2752 }
2753 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002754 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2755 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2757 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002758 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002759 } else {
2760 // Use llvm function name.
2761 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002762 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002763 }
2764 if (!Name.empty() && Name[0] == '\01')
2765 Name = Name.substr(1);
2766
Adrian Prantl42d71b92014-04-10 23:21:53 +00002767 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002769 // Artificial functions without a location should not silently reuse CurLoc.
2770 if (Loc.isInvalid())
2771 CurLoc = SourceLocation();
2772 }
2773 unsigned LineNo = getLineNumber(Loc);
2774 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002775
Eric Christopher8018e412014-03-27 18:50:35 +00002776 // FIXME: The function declaration we're constructing here is mostly reusing
2777 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2778 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2779 // all subprograms instead of the actual context since subprogram definitions
2780 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002781 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002782 FDContext, Name, LinkageName, Unit, LineNo,
2783 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002784 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002785 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002786 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002787 // We might get here with a VarDecl in the case we're generating
2788 // code for the initialization of globals. Do not record these decls
2789 // as they will overwrite the actual VarDecl Decl in the cache.
2790 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002791 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002792
Adrian Prantlbebb8932014-03-21 21:01:58 +00002793 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002794 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002795
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002797 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002798}
2799
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002800void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2801 QualType FnType) {
2802 StringRef Name;
2803 StringRef LinkageName;
2804
2805 const Decl *D = GD.getDecl();
2806 if (!D)
2807 return;
2808
2809 unsigned Flags = 0;
2810 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002811 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002812 llvm::DINodeArray TParamsArray;
2813 if (isa<FunctionDecl>(D)) {
2814 // If there is a DISubprogram for this function available then use it.
2815 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2816 TParamsArray, Flags);
2817 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2818 Name = getObjCMethodName(OMD);
2819 Flags |= llvm::DINode::FlagPrototyped;
2820 } else {
2821 llvm_unreachable("not a function or ObjC method");
2822 }
2823 if (!Name.empty() && Name[0] == '\01')
2824 Name = Name.substr(1);
2825
2826 if (D->isImplicit()) {
2827 Flags |= llvm::DINode::FlagArtificial;
2828 // Artificial functions without a location should not silently reuse CurLoc.
2829 if (Loc.isInvalid())
2830 CurLoc = SourceLocation();
2831 }
2832 unsigned LineNo = getLineNumber(Loc);
2833 unsigned ScopeLine = 0;
2834
Adrian Prantle76bda52016-04-15 15:55:45 +00002835 DBuilder.retainType(DBuilder.createFunction(
2836 FDContext, Name, LinkageName, Unit, LineNo,
2837 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2838 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2839 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002840}
2841
David Blaikie835afb22015-01-21 23:08:17 +00002842void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002843 // Update our current location
2844 setLocation(Loc);
2845
Eric Christophere7b87e52014-10-26 23:40:33 +00002846 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2847 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002848
Adrian Prantle83b1302014-01-07 22:05:52 +00002849 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002850 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002851 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002852}
2853
Guy Benyei11169dd2012-12-18 14:30:41 +00002854void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002855 llvm::MDNode *Back = nullptr;
2856 if (!LexicalBlockStack.empty())
2857 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002858 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002859 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002860 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002861}
2862
Eric Christopher0fdcb312013-05-16 00:52:20 +00002863void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2864 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002865 // Set our current location.
2866 setLocation(Loc);
2867
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002869 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2870 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002871
Benjamin Kramer8c305922016-02-02 11:06:51 +00002872 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002873 return;
2874
2875 // Create a new lexical block and push it on the stack.
2876 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002877}
2878
Eric Christopher0fdcb312013-05-16 00:52:20 +00002879void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2880 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2882
2883 // Provide an entry in the line table for the end of the block.
2884 EmitLocation(Builder, Loc);
2885
Benjamin Kramer8c305922016-02-02 11:06:51 +00002886 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002887 return;
2888
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 LexicalBlockStack.pop_back();
2890}
2891
Guy Benyei11169dd2012-12-18 14:30:41 +00002892void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2893 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2894 unsigned RCount = FnBeginRegionCount.back();
2895 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2896
2897 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002898 while (LexicalBlockStack.size() != RCount) {
2899 // Provide an entry in the line table for the end of the block.
2900 EmitLocation(Builder, CurLoc);
2901 LexicalBlockStack.pop_back();
2902 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 FnBeginRegionCount.pop_back();
2904}
2905
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002906llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002907 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002908
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002909 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 QualType FType;
2911 uint64_t FieldSize, FieldOffset;
2912 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002913
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002914 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002915 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002916
2917 FieldOffset = 0;
2918 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2919 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2920 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2921 FType = CGM.getContext().IntTy;
2922 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2923 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2924
2925 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2926 if (HasCopyAndDispose) {
2927 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002928 EltTys.push_back(
2929 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2930 EltTys.push_back(
2931 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 }
2933 bool HasByrefExtendedLayout;
2934 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002935 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2936 HasByrefExtendedLayout) &&
2937 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002938 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002939 EltTys.push_back(
2940 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002941 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002942
Guy Benyei11169dd2012-12-18 14:30:41 +00002943 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2944 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002945 CGM.getTarget().getPointerAlign(0))) {
2946 CharUnits FieldOffsetInBytes =
2947 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00002948 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00002949 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002950
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 if (NumPaddingBytes.isPositive()) {
2952 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2953 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2954 pad, ArrayType::Normal, 0);
2955 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2956 }
2957 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002958
Guy Benyei11169dd2012-12-18 14:30:41 +00002959 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002960 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002961 FieldSize = CGM.getContext().getTypeSize(FType);
2962 FieldAlign = CGM.getContext().toBits(Align);
2963
Eric Christopherb2a008c2013-05-16 00:45:12 +00002964 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002965 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2966 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002967 EltTys.push_back(FieldTy);
2968 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002970 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002972 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002973
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002975 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002976}
2977
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002978void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2979 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002980 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002981 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2983
David Blaikie7fceebf2013-08-19 03:37:48 +00002984 bool Unwritten =
2985 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2986 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002987 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002988 if (!Unwritten)
2989 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002990 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 uint64_t XOffset = 0;
2992 if (VD->hasAttr<BlocksAttr>())
2993 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002994 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 Ty = getOrCreateType(VD->getType(), Unit);
2996
2997 // If there is no debug info for this type then do not emit debug info
2998 // for this variable.
2999 if (!Ty)
3000 return;
3001
Guy Benyei11169dd2012-12-18 14:30:41 +00003002 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003003 unsigned Line = 0;
3004 unsigned Column = 0;
3005 if (!Unwritten) {
3006 Line = getLineNumber(VD->getLocation());
3007 Column = getColumnNumber(VD->getLocation());
3008 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003009 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 unsigned Flags = 0;
3011 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003012 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003013 // If this is the first argument and it is implicit then
3014 // give it an object pointer flag.
3015 // FIXME: There has to be a better way to do this, but for static
3016 // functions there won't be an implicit param at arg1 and
3017 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003018 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003019 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003020 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003021 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3022 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003023 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003024
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003025 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003026
3027 StringRef Name = VD->getName();
3028 if (!Name.empty()) {
3029 if (VD->hasAttr<BlocksAttr>()) {
3030 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003031 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 // offset of __forwarding field
3033 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003034 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003035 Expr.push_back(offset.getQuantity());
3036 Expr.push_back(llvm::dwarf::DW_OP_deref);
3037 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 // offset of x field
3039 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003040 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003041
3042 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003043 auto *D = ArgNo
3044 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3045 *ArgNo, Unit, Line, Ty)
3046 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3047 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003048
Guy Benyei11169dd2012-12-18 14:30:41 +00003049 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003050 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3051 llvm::DebugLoc::get(Line, Column, Scope),
3052 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003054 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003055 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003056 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3057 // If VD is an anonymous union then Storage represents value for
3058 // all union fields.
3059 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3060 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003061 // GDB has trouble finding local variables in anonymous unions, so we emit
3062 // artifical local variables for each of the members.
3063 //
3064 // FIXME: Remove this code as soon as GDB supports this.
3065 // The debug info verifier in LLVM operates based on the assumption that a
3066 // variable has the same size as its storage and we had to disable the check
3067 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003068 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003069 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003070 StringRef FieldName = Field->getName();
3071
3072 // Ignore unnamed fields. Do not ignore unnamed records.
3073 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3074 continue;
3075
3076 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003077 auto *D = DBuilder.createAutoVariable(
3078 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3079 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003080
3081 // Insert an llvm.dbg.declare into the current block.
3082 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3083 llvm::DebugLoc::get(Line, Column, Scope),
3084 Builder.GetInsertBlock());
3085 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003086 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003088
3089 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003090 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003091 ArgNo
3092 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3093 Ty, CGM.getLangOpts().Optimize,
3094 Flags)
3095 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3096 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003097
3098 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003099 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3100 llvm::DebugLoc::get(Line, Column, Scope),
3101 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003102}
3103
3104void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3105 llvm::Value *Storage,
3106 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003107 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003108 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003109}
3110
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003111llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3112 llvm::DIType *Ty) {
3113 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003114 if (CachedTy)
3115 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003116 return DBuilder.createObjectPointerType(Ty);
3117}
3118
Eric Christophere7b87e52014-10-26 23:40:33 +00003119void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3120 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003121 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003122 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003123 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003124
Craig Topper8a13c412014-05-21 05:09:00 +00003125 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003126 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003127
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003129
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003131 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3132 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 if (isByRef)
3134 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003135 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003136 Ty = getOrCreateType(VD->getType(), Unit);
3137
3138 // Self is passed along as an implicit non-arg variable in a
3139 // block. Mark it as the object pointer.
3140 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003141 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003142
3143 // Get location information.
3144 unsigned Line = getLineNumber(VD->getLocation());
3145 unsigned Column = getColumnNumber(VD->getLocation());
3146
3147 const llvm::DataLayout &target = CGM.getDataLayout();
3148
3149 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003150 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3152
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003153 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003154 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003155 addr.push_back(llvm::dwarf::DW_OP_deref);
3156 addr.push_back(llvm::dwarf::DW_OP_plus);
3157 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003159 addr.push_back(llvm::dwarf::DW_OP_deref);
3160 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003161 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 offset =
3163 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003164 addr.push_back(offset.getQuantity());
3165 addr.push_back(llvm::dwarf::DW_OP_deref);
3166 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003167 // offset of x field
3168 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003169 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 }
3171
3172 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003173 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003174 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003175 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003176
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003178 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3179 if (InsertPoint)
3180 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3181 InsertPoint);
3182 else
3183 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3184 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003185}
3186
Guy Benyei11169dd2012-12-18 14:30:41 +00003187void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3188 unsigned ArgNo,
3189 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003190 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003191 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003192}
3193
3194namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003195struct BlockLayoutChunk {
3196 uint64_t OffsetInBits;
3197 const BlockDecl::Capture *Capture;
3198};
3199bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3200 return l.OffsetInBits < r.OffsetInBits;
3201}
Guy Benyei11169dd2012-12-18 14:30:41 +00003202}
3203
3204void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003205 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003206 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003207 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003208 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003209 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003210 ASTContext &C = CGM.getContext();
3211 const BlockDecl *blockDecl = block.getBlockDecl();
3212
3213 // Collect some general information about the block's location.
3214 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003215 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003216 unsigned line = getLineNumber(loc);
3217 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003218
Guy Benyei11169dd2012-12-18 14:30:41 +00003219 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003220 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003221
3222 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003223 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003224
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003225 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003226 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3227 blockLayout->getElementOffsetInBits(0),
3228 tunit, tunit));
3229 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3230 blockLayout->getElementOffsetInBits(1),
3231 tunit, tunit));
3232 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3233 blockLayout->getElementOffsetInBits(2),
3234 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003235 auto *FnTy = block.getBlockExpr()->getFunctionType();
3236 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3237 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003238 blockLayout->getElementOffsetInBits(3),
3239 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003240 fields.push_back(createFieldType(
3241 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3242 ? C.getBlockDescriptorExtendedType()
3243 : C.getBlockDescriptorType()),
3244 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003245
3246 // We want to sort the captures by offset, not because DWARF
3247 // requires this, but because we're paranoid about debuggers.
3248 SmallVector<BlockLayoutChunk, 8> chunks;
3249
3250 // 'this' capture.
3251 if (blockDecl->capturesCXXThis()) {
3252 BlockLayoutChunk chunk;
3253 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003254 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003255 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003256 chunks.push_back(chunk);
3257 }
3258
3259 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003260 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003261 const VarDecl *variable = capture.getVariable();
3262 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3263
3264 // Ignore constant captures.
3265 if (captureInfo.isConstant())
3266 continue;
3267
3268 BlockLayoutChunk chunk;
3269 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003270 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003271 chunk.Capture = &capture;
3272 chunks.push_back(chunk);
3273 }
3274
3275 // Sort by offset.
3276 llvm::array_pod_sort(chunks.begin(), chunks.end());
3277
Eric Christophere7b87e52014-10-26 23:40:33 +00003278 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3279 e = chunks.end();
3280 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003281 uint64_t offsetInBits = i->OffsetInBits;
3282 const BlockDecl::Capture *capture = i->Capture;
3283
3284 // If we have a null capture, this must be the C++ 'this' capture.
3285 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003286 QualType type;
3287 if (auto *Method =
3288 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3289 type = Method->getThisType(C);
3290 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3291 type = QualType(RDecl->getTypeForDecl(), 0);
3292 else
3293 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003294
3295 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3296 offsetInBits, tunit, tunit));
3297 continue;
3298 }
3299
3300 const VarDecl *variable = capture->getVariable();
3301 StringRef name = variable->getName();
3302
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003303 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003304 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003305 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003306
3307 // FIXME: this creates a second copy of this type!
3308 uint64_t xoffset;
3309 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003310 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3311 fieldType =
3312 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3313 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003315 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3316 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 }
3318 fields.push_back(fieldType);
3319 }
3320
3321 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003322 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3323 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003324
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003325 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003326
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003327 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003328 tunit, typeName.str(), tunit, line,
3329 CGM.getContext().toBits(block.BlockSize),
3330 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003331 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3332
3333 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003334 unsigned flags = llvm::DINode::FlagArtificial;
3335 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003336
3337 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003338 auto *debugVar = DBuilder.createParameterVariable(
3339 scope, Arg->getName(), ArgNo, tunit, line, type,
3340 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003341
Adrian Prantl616bef42013-03-14 21:52:59 +00003342 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003343 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003344 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003345 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003346 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003347 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003348
Adrian Prantl616bef42013-03-14 21:52:59 +00003349 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003350 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3351 llvm::DebugLoc::get(line, column, scope),
3352 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003353}
3354
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003355llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003356CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3357 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003358 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003359
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003360 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003361 if (MI != StaticDataMemberCache.end()) {
3362 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003363 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003364 }
David Blaikiece763042013-08-20 21:49:21 +00003365
3366 // If the member wasn't found in the cache, lazily construct and add it to the
3367 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003368 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003369 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003370 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003371}
3372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003373llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3374 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3375 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3376 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003377
3378 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003379 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003380 StringRef FieldName = Field->getName();
3381
3382 // Ignore unnamed fields, but recurse into anonymous records.
3383 if (FieldName.empty()) {
3384 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3385 if (RT)
3386 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3387 Var, DContext);
3388 continue;
3389 }
3390 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003391 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3392 LineNo, FieldTy,
3393 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003394 }
3395 return GV;
3396}
3397
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003398void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003399 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003400 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003401 if (D->hasAttr<NoDebugAttr>())
3402 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003403 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003404 llvm::DIFile *Unit = nullptr;
3405 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003406 unsigned LineNo;
3407 StringRef DeclName, LinkageName;
3408 QualType T;
3409 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003410
3411 // Attempt to store one global variable for the declaration - even if we
3412 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003413 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003414
3415 // If this is an anonymous union then we'll want to emit a global
3416 // variable for each member of the anonymous union so that it's possible
3417 // to find the name of any field in the union.
3418 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003419 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003420 assert(RD->isAnonymousStructOrUnion() &&
3421 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003422 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3423 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003424 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003425 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3426 Var->hasInternalLinkage(), Var,
3427 getOrCreateStaticDataMemberDeclarationOrNull(D));
3428 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003429 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003430}
3431
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003432void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3433 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003434 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003435 if (VD->hasAttr<NoDebugAttr>())
3436 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003437 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003439 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003440 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003441 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3442 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3443 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3444 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3445 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003446 // Do not use global variables for enums.
3447 //
3448 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003449 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003450 return;
David Blaikiea15565562014-04-04 20:56:17 +00003451 // Do not emit separate definitions for function local const/statics.
3452 if (isa<FunctionDecl>(VD->getDeclContext()))
3453 return;
David Blaikiebb113912014-04-05 07:23:17 +00003454 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003455 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003456 if (VarD->isStaticDataMember()) {
3457 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003458 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003459 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003460 //
3461 // FIXME: This is probably unnecessary, since Ty should reference RD
3462 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003463 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003464 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003465 return;
3466 }
3467
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003468 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003469
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003470 auto &GV = DeclCache[VD];
3471 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003472 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003473 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003474 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003475 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003476}
3477
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003478llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003479 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003480 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003481 llvm::DIScope *Mod = getParentModuleOrNull(D);
3482 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003483}
3484
David Blaikie9f88fe82013-04-22 06:13:21 +00003485void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003486 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003487 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003488 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3489 if (!NSDecl->isAnonymousNamespace() ||
3490 CGM.getCodeGenOpts().DebugExplicitImport) {
3491 DBuilder.createImportedModule(
3492 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3493 getOrCreateNameSpace(NSDecl),
3494 getLineNumber(UD.getLocation()));
3495 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003496}
3497
David Blaikiebd483762013-05-20 04:58:53 +00003498void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003499 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003500 return;
3501 assert(UD.shadow_size() &&
3502 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3503 // Emitting one decl is sufficient - debuggers can detect that this is an
3504 // overloaded name & provide lookup for all the overloads.
3505 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003506 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003507 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003508 DBuilder.createImportedDeclaration(
3509 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3510 getLineNumber(USD.getLocation()));
3511}
3512
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003513void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003514 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3515 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003516 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003517 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003518 DBuilder.createImportedDeclaration(
3519 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3520 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3521 getLineNumber(ID.getLocation()));
3522 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003523}
3524
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003525llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003526CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003527 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003528 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003529 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003530 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003531 return cast<llvm::DIImportedEntity>(VH);
3532 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003533 if (const NamespaceAliasDecl *Underlying =
3534 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3535 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003536 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003537 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3538 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3539 NA.getName());
3540 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003541 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003542 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3543 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3544 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003545 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003546 return R;
3547}
3548
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003549llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003550CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003551 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003552 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003553 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003554 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003555
Guy Benyei11169dd2012-12-18 14:30:41 +00003556 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003557 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003558 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003559 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003560 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003561 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 return NS;
3563}
3564
Adrian Prantla5206ce2015-09-22 23:26:43 +00003565void CGDebugInfo::setDwoId(uint64_t Signature) {
3566 assert(TheCU && "no main compile unit");
3567 TheCU->setDWOId(Signature);
3568}
3569
3570
Guy Benyei11169dd2012-12-18 14:30:41 +00003571void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003572 // Creating types might create further types - invalidating the current
3573 // element and the size(), so don't cache/reference them.
3574 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3575 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003576 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003577 ? CreateTypeDefinition(E.Type, E.Unit)
3578 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003579 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003580 }
3581
David Blaikief427b002014-05-06 03:42:01 +00003582 for (auto p : ReplaceMap) {
3583 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003584 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003585 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003586
David Blaikief427b002014-05-06 03:42:01 +00003587 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003588 assert(it != TypeCache.end());
3589 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003591 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3592 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003593 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003594
Frederic Rissd253ed62014-11-18 03:40:51 +00003595 for (const auto &p : FwdDeclReplaceMap) {
3596 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003597 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003598 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003599
3600 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003601 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003602 // with ourselves, that will destroy the temporary MDNode and
3603 // replace it with a standard one, avoiding leaking memory.
3604 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003605 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003606 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003607 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003608
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003609 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003610 }
3611
Adrian Prantl73409ce2013-03-11 18:33:46 +00003612 // We keep our own list of retained types, because we need to look
3613 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003614 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003615 if (auto MD = TypeCache[RT])
3616 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003617
Guy Benyei11169dd2012-12-18 14:30:41 +00003618 DBuilder.finalize();
3619}
David Blaikie66088d52014-09-24 17:01:27 +00003620
3621void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003622 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003623 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003624
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003625 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003626 // Don't ignore in case of explicit cast where it is referenced indirectly.
3627 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003628}