blob: 69976d76f2ee73b5d7bd42b4aa032148e9c3d59d [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:
Eric Christophere7b87e52014-10-26 23:40:33 +0000531 case BuiltinType::Double:
532 Encoding = llvm::dwarf::DW_ATE_float;
533 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 }
535
536 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000537 case BuiltinType::Long:
538 BTName = "long int";
539 break;
540 case BuiltinType::LongLong:
541 BTName = "long long int";
542 break;
543 case BuiltinType::ULong:
544 BTName = "long unsigned int";
545 break;
546 case BuiltinType::ULongLong:
547 BTName = "long long unsigned int";
548 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 default:
550 BTName = BT->getName(CGM.getLangOpts());
551 break;
552 }
553 // Bit size, align and offset of the type.
554 uint64_t Size = CGM.getContext().getTypeSize(BT);
555 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000556 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000557}
558
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000559llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000561 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 if (Ty->isComplexIntegerType())
563 Encoding = llvm::dwarf::DW_ATE_lo_user;
564
565 uint64_t Size = CGM.getContext().getTypeSize(Ty);
566 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000567 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568}
569
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000570llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
571 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 QualifierCollector Qc;
573 const Type *T = Qc.strip(Ty);
574
575 // Ignore these qualifiers for now.
576 Qc.removeObjCGCAttr();
577 Qc.removeAddressSpace();
578 Qc.removeObjCLifetime();
579
580 // We will create one Derived type for one qualifier and recurse to handle any
581 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000582 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000583 if (Qc.hasConst()) {
584 Tag = llvm::dwarf::DW_TAG_const_type;
585 Qc.removeConst();
586 } else if (Qc.hasVolatile()) {
587 Tag = llvm::dwarf::DW_TAG_volatile_type;
588 Qc.removeVolatile();
589 } else if (Qc.hasRestrict()) {
590 Tag = llvm::dwarf::DW_TAG_restrict_type;
591 Qc.removeRestrict();
592 } else {
593 assert(Qc.empty() && "Unknown type qualifier for debug info");
594 return getOrCreateType(QualType(T, 0), Unit);
595 }
596
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000597 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000598
599 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
600 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000601 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000602}
603
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000604llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
605 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000606
607 // The frontend treats 'id' as a typedef to an ObjCObjectType,
608 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
609 // debug info, we want to emit 'id' in both cases.
610 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000611 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000612
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000613 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
614 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000615}
616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000617llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
618 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000619 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 Ty->getPointeeType(), Unit);
621}
622
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000623/// \return whether a C++ mangling exists for the type defined by TD.
624static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
625 switch (TheCU->getSourceLanguage()) {
626 case llvm::dwarf::DW_LANG_C_plus_plus:
627 return true;
628 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
629 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
630 default:
631 return false;
632 }
633}
634
Manman Rene0064d82013-08-29 23:19:58 +0000635/// In C++ mode, types have linkage, so we can rely on the ODR and
636/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000637static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
638 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000639 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000640 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000641 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000642
643 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000644 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000645
Manman Rene0064d82013-08-29 23:19:58 +0000646 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
647 if (CGM.getTarget().getCXXABI().isMicrosoft())
648 return FullName;
649
650 // TODO: This is using the RTTI name. Is there a better way to get
651 // a unique string for a type?
652 llvm::raw_svector_ostream Out(FullName);
653 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000654 return FullName;
655}
656
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000657/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000658static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
659 llvm::dwarf::Tag Tag;
660 if (RD->isStruct() || RD->isInterface())
661 Tag = llvm::dwarf::DW_TAG_structure_type;
662 else if (RD->isUnion())
663 Tag = llvm::dwarf::DW_TAG_union_type;
664 else {
665 // FIXME: This could be a struct type giving a default visibility different
666 // than C++ class type, but needs llvm metadata changes first.
667 assert(RD->isClass());
668 Tag = llvm::dwarf::DW_TAG_class_type;
669 }
670 return Tag;
671}
672
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000673llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000674CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000675 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000676 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000677 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
678 return cast<llvm::DICompositeType>(T);
679 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000680 unsigned Line = getLineNumber(RD->getLocation());
681 StringRef RDName = getClassName(RD);
682
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000683 uint64_t Size = 0;
684 uint64_t Align = 0;
685
686 const RecordDecl *D = RD->getDefinition();
687 if (D && D->isCompleteDefinition()) {
688 Size = CGM.getContext().getTypeSize(Ty);
689 Align = CGM.getContext().getTypeAlign(Ty);
690 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000691
692 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000693 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000694 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000695 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000696 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000697 ReplaceMap.emplace_back(
698 std::piecewise_construct, std::make_tuple(Ty),
699 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000700 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000701}
702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000703llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000704 const Type *Ty,
705 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000706 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 // Bit size, align and offset of the type.
708 // Size is always the size of a pointer. We can't use getTypeSize here
709 // because that does not return the correct value for references.
710 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000711 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
713
Keno Fischer87842f32015-11-16 09:04:13 +0000714 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
715 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
716 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
717 Size, Align);
718 else
719 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
720 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721}
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
724 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000725 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000726 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000727 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
728 TheCU, getOrCreateMainFile(), 0);
729 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
730 Cache = DBuilder.createPointerType(Cache, Size);
731 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000732}
733
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000734llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
735 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000736 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000737 QualType FType;
738 uint64_t FieldSize, FieldOffset;
739 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000740 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000741
742 FieldOffset = 0;
743 FType = CGM.getContext().UnsignedLongTy;
744 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
745 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
746
747 Elements = DBuilder.getOrCreateArray(EltTys);
748 EltTys.clear();
749
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000750 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000751 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000752
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000753 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000754 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000755 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000756
757 // Bit size, align and offset of the type.
758 uint64_t Size = CGM.getContext().getTypeSize(Ty);
759
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000760 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000761
762 FieldOffset = 0;
763 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
764 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
765 FType = CGM.getContext().IntTy;
766 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
767 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000768 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000769 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
770
771 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 FieldSize = CGM.getContext().getTypeSize(Ty);
773 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000774 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000775 FieldSize, FieldAlign, FieldOffset,
776 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000777
778 FieldOffset += FieldSize;
779 Elements = DBuilder.getOrCreateArray(EltTys);
780
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000781 // The __block_literal_generic structs are marked with a special
782 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
783 // the debugger needs to know about. To allow type uniquing, emit
784 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000785 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000786 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000787 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000788
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000789 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000790}
791
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
793 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000794 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000795 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000796
797 SmallString<128> NS;
798 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000799 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
800 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000801
802 TemplateSpecializationType::PrintTemplateArgumentList(
803 OS, Ty->getArgs(), Ty->getNumArgs(),
804 CGM.getContext().getPrintingPolicy());
805
Eric Christophere7b87e52014-10-26 23:40:33 +0000806 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
807 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000808
809 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000810 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
811 getLineNumber(Loc),
812 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000813}
814
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
816 llvm::DIFile *Unit) {
Amjad Aboud0ccbfa32016-04-29 16:08:08 +0000817 TypedefNameDecl *TD = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +0000818 // We don't set size information, but do specify where the typedef was
819 // declared.
Amjad Aboud0ccbfa32016-04-29 16:08:08 +0000820 SourceLocation Loc = TD->getLocation();
821
822 llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000823
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000824 // Typedefs are derived from some other type.
825 return DBuilder.createTypedef(
826 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
827 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Aboud0ccbfa32016-04-29 16:08:08 +0000828 TDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000829}
830
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000831llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
832 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000833 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000834
835 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000836 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000837
838 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000839 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 if (isa<FunctionNoProtoType>(Ty))
841 EltTys.push_back(DBuilder.createUnspecifiedParameter());
842 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000843 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
844 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000845 if (FPT->isVariadic())
846 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000847 }
848
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000849 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +0000850 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +0000851}
852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000853/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000854/// As an optimization, return 0 if the access specifier equals the
855/// default for the containing type.
856static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
857 AccessSpecifier Default = clang::AS_none;
858 if (RD && RD->isClass())
859 Default = clang::AS_private;
860 else if (RD && (RD->isStruct() || RD->isUnion()))
861 Default = clang::AS_public;
862
863 if (Access == Default)
864 return 0;
865
Eric Christophere7b87e52014-10-26 23:40:33 +0000866 switch (Access) {
867 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000868 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000869 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000870 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000871 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000872 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000873 case clang::AS_none:
874 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000875 }
876 llvm_unreachable("unexpected access enumerator");
877}
Guy Benyei11169dd2012-12-18 14:30:41 +0000878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000879llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000880 StringRef name, QualType type, uint64_t sizeInBitsOverride,
881 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000882 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
883 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000884
885 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000886 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000887 unsigned line = getLineNumber(loc);
888
David Majnemer34b57492014-07-30 01:30:47 +0000889 uint64_t SizeInBits = 0;
890 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000891 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000892 TypeInfo TI = CGM.getContext().getTypeInfo(type);
893 SizeInBits = TI.Width;
894 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000895
896 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000897 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000898 }
899
Adrian Prantl21361fb2014-08-29 22:44:27 +0000900 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000901 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
902 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000903}
904
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000905void CGDebugInfo::CollectRecordLambdaFields(
906 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000907 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000908 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
909 // has the name and the location of the variable so we should iterate over
910 // both concurrently.
911 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
912 RecordDecl::field_iterator Field = CXXDecl->field_begin();
913 unsigned fieldno = 0;
914 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000915 E = CXXDecl->captures_end();
916 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000917 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000918 if (C.capturesVariable()) {
919 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000920 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000921 StringRef VName = V->getName();
922 uint64_t SizeInBitsOverride = 0;
923 if (Field->isBitField()) {
924 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
925 assert(SizeInBitsOverride && "found named 0-width bitfield");
926 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000927 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000928 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
929 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
930 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000931 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000932 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000933 // TODO: Need to handle 'this' in some way by probably renaming the
934 // this of the lambda class and having a field member of 'this' or
935 // by using AT_object_pointer for the function and having that be
936 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000937 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000938 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000939 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000940 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000941 "this", type, 0, f->getLocation(), f->getAccess(),
942 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000943
944 elements.push_back(fieldType);
945 }
946 }
947}
948
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000949llvm::DIDerivedType *
950CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000951 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000952 // Create the descriptor for the static variable, with or without
953 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000954 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000955 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
956 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000957
Eric Christopher91a31902013-01-16 01:22:32 +0000958 unsigned LineNumber = getLineNumber(Var->getLocation());
959 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000960 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000961 if (Var->getInit()) {
962 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000963 if (Value) {
964 if (Value->isInt())
965 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
966 if (Value->isFloat())
967 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
968 }
Eric Christopher91a31902013-01-16 01:22:32 +0000969 }
970
Adrian Prantl21361fb2014-08-29 22:44:27 +0000971 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000972 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000973 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000974 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000975 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000976}
977
Eric Christophere7b87e52014-10-26 23:40:33 +0000978void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000979 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
980 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000981 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000982 StringRef name = field->getName();
983 QualType type = field->getType();
984
985 // Ignore unnamed fields unless they're anonymous structs/unions.
986 if (name.empty() && !type->isRecordType())
987 return;
988
989 uint64_t SizeInBitsOverride = 0;
990 if (field->isBitField()) {
991 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
992 assert(SizeInBitsOverride && "found named 0-width bitfield");
993 }
994
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000995 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000996 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
997 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000998
999 elements.push_back(fieldType);
1000}
1001
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001002void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001003 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001004 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001005 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001006 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1007
Eric Christopher91a31902013-01-16 01:22:32 +00001008 if (CXXDecl && CXXDecl->isLambda())
1009 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1010 else {
1011 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001012
Eric Christopher91a31902013-01-16 01:22:32 +00001013 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001014 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001015
Eric Christopher91a31902013-01-16 01:22:32 +00001016 // Static and non-static members should appear in the same order as
1017 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001018 for (const auto *I : record->decls())
1019 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001020 if (V->hasAttr<NoDebugAttr>())
1021 continue;
David Blaikiece763042013-08-20 21:49:21 +00001022 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001023 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001024 if (MI != StaticDataMemberCache.end()) {
1025 assert(MI->second &&
1026 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001027 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001028 } else {
1029 auto Field = CreateRecordStaticField(V, RecordTy, record);
1030 elements.push_back(Field);
1031 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001032 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001033 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1034 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001035
1036 // Bump field number for next field.
1037 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001038 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001039 }
1040}
1041
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001042llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001043CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001044 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001045 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001046 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001047 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001048 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001049 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1050 Func, Unit);
1051}
David Blaikie2aaf0652013-01-07 22:24:59 +00001052
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001053llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1054 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001055 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056 llvm::DITypeRefArray Args(
1057 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001058 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001059 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001060
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001061 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001062
1063 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001064 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001065
David Blaikie2aaf0652013-01-07 22:24:59 +00001066 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001067 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001068 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1069 // Create pointer type directly in this case.
1070 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1071 QualType PointeeTy = ThisPtrTy->getPointeeType();
1072 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001073 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001074 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001075 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1076 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001077 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001078 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001079 // TODO: This and the artificial type below are misleading, the
1080 // types aren't artificial the argument is, but the current
1081 // metadata doesn't represent that.
1082 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1083 Elts.push_back(ThisPtrType);
1084 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001085 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001086 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001087 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1088 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001089 }
1090
1091 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001092 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1093 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001094
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001095 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001096
Adrian Prantl0630eb72013-12-18 21:48:18 +00001097 unsigned Flags = 0;
1098 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001099 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001100 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001101 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001102
Eric Christopher28a6db52015-10-15 06:56:08 +00001103 return DBuilder.createSubroutineType(EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001104}
1105
Eric Christopherb2a008c2013-05-16 00:45:12 +00001106/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001107/// inside a function.
1108static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1109 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1110 return isFunctionLocalClass(NRD);
1111 if (isa<FunctionDecl>(RD->getDeclContext()))
1112 return true;
1113 return false;
1114}
1115
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001116llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1117 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001118 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001119 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001120
Guy Benyei11169dd2012-12-18 14:30:41 +00001121 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001123
1124 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1125 // make sense to give a single ctor/dtor a linkage name.
1126 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001127 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1128 // property to use here. It may've been intended to model "is non-external
1129 // type" but misses cases of non-function-local but non-external classes such
1130 // as those in anonymous namespaces as well as the reverse - external types
1131 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1133 MethodLinkageName = CGM.getMangledName(Method);
1134
1135 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001136 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001137 unsigned MethodLine = 0;
1138 if (!Method->isImplicit()) {
1139 MethodDefUnit = getOrCreateFile(Method->getLocation());
1140 MethodLine = getLineNumber(Method->getLocation());
1141 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001142
1143 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001144 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001145 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001147
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 if (Method->isVirtual()) {
1149 if (Method->isPure())
1150 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1151 else
1152 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001153
Guy Benyei11169dd2012-12-18 14:30:41 +00001154 // It doesn't make sense to give a virtual destructor a vtable index,
1155 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001156 // FIXME: Add proper support for debug info for virtual calls in
1157 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1158 // lookup if we have multiple or virtual inheritance.
1159 if (!isa<CXXDestructorDecl>(Method) &&
1160 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001161 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001162 ContainingType = RecordTy;
1163 }
1164
1165 unsigned Flags = 0;
1166 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001167 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001168 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001169 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1170 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001171 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001172 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001173 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001175 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001176 }
1177 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001178 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001179 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001180 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001181 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001182 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001183
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001184 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1185 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001186 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1187 MethodTy, /*isLocalToUnit=*/false,
1188 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001189 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001190
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001191 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001192
1193 return SP;
1194}
1195
Eric Christophere7b87e52014-10-26 23:40:33 +00001196void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001197 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1198 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001199
1200 // Since we want more than just the individual member decls if we
1201 // have templated functions iterate over every declaration to gather
1202 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001203 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001204 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1205 // If the member is implicit, don't add it to the member list. This avoids
1206 // the member being added to type units by LLVM, while still allowing it
1207 // to be emitted into the type declaration/reference inside the compile
1208 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001209 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001210 // FIXME: Handle Using(Shadow?)Decls here to create
1211 // DW_TAG_imported_declarations inside the class for base decls brought into
1212 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1213 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1214 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001215 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001216 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001217
1218 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1219 continue;
1220
David Blaikiefd580722014-10-06 05:18:55 +00001221 // Reuse the existing member function declaration if it exists.
1222 // It may be associated with the declaration of the type & should be
1223 // reused as we're building the definition.
1224 //
1225 // This situation can arise in the vtable-based debug info reduction where
1226 // implicit members are emitted in a non-vtable TU.
1227 auto MI = SPCache.find(Method->getCanonicalDecl());
1228 EltTys.push_back(MI == SPCache.end()
1229 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001230 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001231 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001232}
Guy Benyei11169dd2012-12-18 14:30:41 +00001233
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001235 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001238 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001239 unsigned BFlags = 0;
1240 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001241
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001243 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001244
Aaron Ballman574705e2014-03-13 15:41:46 +00001245 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001246 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1247 // virtual base offset offset is -ve. The code generator emits dwarf
1248 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001249 BaseOffset = 0 - CGM.getItaniumVTableContext()
1250 .getVirtualBaseOffsetOffset(RD, Base)
1251 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001252 } else {
1253 // In the MS ABI, store the vbtable offset, which is analogous to the
1254 // vbase offset offset in Itanium.
1255 BaseOffset =
1256 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1257 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001259 } else
1260 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1261 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1262 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001263
Adrian Prantl21361fb2014-08-29 22:44:27 +00001264 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001266 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001267 EltTys.push_back(DTy);
1268 }
1269}
1270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001272CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1273 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001274 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001275 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1277 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001278 StringRef Name;
1279 if (TPList)
1280 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001281 switch (TA.getKind()) {
1282 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001283 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001284 TemplateParams.push_back(
1285 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001286 } break;
1287 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001288 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001289 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1290 TheCU, Name, TTy,
1291 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001292 } break;
1293 case TemplateArgument::Declaration: {
1294 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001295 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001296 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001297 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001298 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001299 // Variable pointer template parameters have a value that is the address
1300 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001301 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001302 V = CGM.GetAddrOfGlobalVar(VD);
1303 // Member function pointers have special support for building them, though
1304 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001305 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001306 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001307 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001308 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001309 // Member data pointers have special handling too to compute the fixed
1310 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001311 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001312 // These five lines (& possibly the above member function pointer
1313 // handling) might be able to be refactored to use similar code in
1314 // CodeGenModule::getMemberPointerConstant
1315 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1316 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001317 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001318 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001319 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001320 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1321 TheCU, Name, TTy,
1322 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001323 } break;
1324 case TemplateArgument::NullPtr: {
1325 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001326 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001327 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001328 // Special case member data pointer null values since they're actually -1
1329 // instead of zero.
1330 if (const MemberPointerType *MPT =
1331 dyn_cast<MemberPointerType>(T.getTypePtr()))
1332 // But treat member function pointers as simple zero integers because
1333 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1334 // CodeGen grows handling for values of non-null member function
1335 // pointers then perhaps we could remove this special case and rely on
1336 // EmitNullMemberPointer for member function pointers.
1337 if (MPT->isMemberDataPointer())
1338 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1339 if (!V)
1340 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001341 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1342 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001343 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001344 case TemplateArgument::Template:
1345 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001346 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001347 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1348 break;
1349 case TemplateArgument::Pack:
1350 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1351 TheCU, Name, nullptr,
1352 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1353 break;
David Majnemer5559d472013-08-24 08:21:10 +00001354 case TemplateArgument::Expression: {
1355 const Expr *E = TA.getAsExpr();
1356 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001357 if (E->isGLValue())
1358 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001359 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001360 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001362 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1363 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001364 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001365 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001366 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001367 case TemplateArgument::Null:
1368 llvm_unreachable(
1369 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001370 }
1371 }
1372 return DBuilder.getOrCreateArray(TemplateParams);
1373}
1374
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001375llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001376CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001377 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 if (FD->getTemplatedKind() ==
1379 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001380 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1381 ->getTemplate()
1382 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001383 return CollectTemplateParams(
1384 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001387}
1388
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1390 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001391 // Always get the full list of parameters, not just the ones from
1392 // the specialization.
1393 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001394 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001395 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001396 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001397}
1398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001400 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 return VTablePtrType;
1402
1403 ASTContext &Context = CGM.getContext();
1404
1405 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001406 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001407 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001408 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001410 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001411 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001412 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1413 return VTablePtrType;
1414}
1415
Guy Benyei11169dd2012-12-18 14:30:41 +00001416StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001417 // Copy the gdb compatible name on the side and use its reference.
1418 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001419}
1420
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001421void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001422 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001423 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1424
1425 // If there is a primary base then it will hold vtable info.
1426 if (RL.getPrimaryBase())
1427 return;
1428
1429 // If this class is not dynamic then there is not any vtable info to collect.
1430 if (!RD->isDynamicClass())
1431 return;
1432
1433 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001434 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001435 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001436 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 EltTys.push_back(VPTR);
1438}
1439
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001440llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001441 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001442 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 return T;
1445}
1446
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001448 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001449 return getOrCreateStandaloneType(D, Loc);
1450}
1451
1452llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1453 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001454 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001455 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001456 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001457 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001458
Adrian Prantl73409ce2013-03-11 18:33:46 +00001459 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001460 return T;
1461}
1462
Amjad Aboud0ccbfa32016-04-29 16:08:08 +00001463void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {
1464 assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&
1465 "D is already mapped to lexical block scope");
1466 if (!LexicalBlockStack.empty())
1467 LexicalBlockMap[&D] = LexicalBlockStack.back();
1468}
1469
1470llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D,
1471 QualType Ty) {
1472 auto I = LexicalBlockMap.find(&D);
1473 if (I != LexicalBlockMap.end()) {
1474 RetainedTypes.push_back(Ty.getAsOpaquePtr());
1475 return I->second;
1476 }
1477 return getDeclContextDescriptor(cast<Decl>(&D));
1478}
1479
David Blaikie483a9da2014-05-06 18:35:21 +00001480void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001481 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001482 return;
1483 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001484 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001485 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001486 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001487 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001488 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001489 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001490 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001491}
1492
David Blaikieb2e86eb2013-08-15 20:49:17 +00001493void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001494 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001495 !CGM.getLangOpts().CPlusPlus)
1496 completeRequiredType(RD);
1497}
1498
1499void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001500 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001501 return;
1502
David Blaikie6943dea2013-08-20 01:28:15 +00001503 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1504 if (CXXDecl->isDynamicClass())
1505 return;
1506
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001507 if (DebugTypeExtRefs && RD->isFromASTFile())
1508 return;
1509
David Blaikieb2e86eb2013-08-15 20:49:17 +00001510 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001511 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001512 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001513 completeClassData(RD);
1514}
1515
1516void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001517 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001518 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001519 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001520 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001521 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001522 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001523 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001524 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001525 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001526 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001527}
1528
David Blaikie0e716b42014-03-03 23:48:23 +00001529static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1530 CXXRecordDecl::method_iterator End) {
1531 for (; I != End; ++I)
1532 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001533 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1534 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001535 return true;
1536 return false;
1537}
1538
Adrian Prantl05fefa42016-04-25 20:52:40 +00001539/// Does a type definition exist in an imported clang module?
1540static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001541 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001542 return false;
1543 if (!RD->isExternallyVisible() && RD->getName().empty())
1544 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001545 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1546 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001547 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1548 // Make sure the instantiation is actually in a module.
1549 if (CXXDecl->field_begin() != CXXDecl->field_end())
1550 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001551 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001552
Adrian Prantl05fefa42016-04-25 20:52:40 +00001553 return true;
1554}
1555
Benjamin Kramer8c305922016-02-02 11:06:51 +00001556static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1557 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001558 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001559 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001560 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001561
Benjamin Kramer8c305922016-02-02 11:06:51 +00001562 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001563 return false;
1564
1565 if (!LangOpts.CPlusPlus)
1566 return false;
1567
1568 if (!RD->isCompleteDefinitionRequired())
1569 return true;
1570
1571 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1572
1573 if (!CXXDecl)
1574 return false;
1575
1576 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1577 return true;
1578
1579 TemplateSpecializationKind Spec = TSK_Undeclared;
1580 if (const ClassTemplateSpecializationDecl *SD =
1581 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1582 Spec = SD->getSpecializationKind();
1583
1584 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1585 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1586 CXXDecl->method_end()))
1587 return true;
1588
1589 return false;
1590}
1591
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001592llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001593 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001594 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001595 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1596 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001597 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001598 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001599 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001600 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001601
David Blaikieb2e86eb2013-08-15 20:49:17 +00001602 return CreateTypeDefinition(Ty);
1603}
1604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001605llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001606 RecordDecl *RD = Ty->getDecl();
1607
Guy Benyei11169dd2012-12-18 14:30:41 +00001608 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001609 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001610
1611 // Records and classes and unions can all be recursive. To handle them, we
1612 // first generate a debug descriptor for the struct as a forward declaration.
1613 // Then (if it is a definition) we go through and get debug info for all of
1614 // its members. Finally, we create a descriptor for the complete type (which
1615 // may refer to the forward decl if the struct is recursive) and replace all
1616 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001617 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001618
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001619 const RecordDecl *D = RD->getDefinition();
1620 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001621 return FwdDecl;
1622
David Blaikieadfbf992013-08-18 16:55:33 +00001623 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1624 CollectContainingType(CXXDecl, FwdDecl);
1625
Guy Benyei11169dd2012-12-18 14:30:41 +00001626 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001627 LexicalBlockStack.emplace_back(&*FwdDecl);
1628 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001629
Guy Benyei11169dd2012-12-18 14:30:41 +00001630 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001631 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001632 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001633
1634 // Note: The split of CXXDecl information here is intentional, the
1635 // gdb tests will depend on a certain ordering at printout. The debug
1636 // information offsets are still correct if we merge them all together
1637 // though.
1638 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1639 if (CXXDecl) {
1640 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1641 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1642 }
1643
Eric Christopher91a31902013-01-16 01:22:32 +00001644 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001646 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001648
1649 LexicalBlockStack.pop_back();
1650 RegionMap.erase(Ty->getDecl());
1651
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001652 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001653 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001654
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001655 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001656 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001657 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001658
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001659 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001660 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001661}
1662
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001663llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1664 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001665 // Ignore protocols.
1666 return getOrCreateType(Ty->getBaseType(), Unit);
1667}
1668
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001669/// \return true if Getter has the default name for the property PD.
1670static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1671 const ObjCMethodDecl *Getter) {
1672 assert(PD);
1673 if (!Getter)
1674 return true;
1675
1676 assert(Getter->getDeclName().isObjCZeroArgSelector());
1677 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001678 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001679}
1680
1681/// \return true if Setter has the default name for the property PD.
1682static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1683 const ObjCMethodDecl *Setter) {
1684 assert(PD);
1685 if (!Setter)
1686 return true;
1687
1688 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001689 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001690 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001691}
1692
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1694 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001695 ObjCInterfaceDecl *ID = Ty->getDecl();
1696 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001697 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001699 // Return a forward declaration if this type was imported from a clang module,
1700 // and this is not the compile unit with the implementation of the type (which
1701 // may contain hidden ivars).
1702 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1703 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001704 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1705 ID->getName(),
1706 getDeclContextDescriptor(ID), Unit, 0);
1707
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001709 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001710 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001711 auto RuntimeLang =
1712 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001713
1714 // If this is just a forward declaration return a special forward-declaration
1715 // debug type since we won't be able to lay out the entire type.
1716 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001717 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001718 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001719 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001720 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1721 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001722 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001723 return FwdDecl;
1724 }
1725
David Blaikieef8a9512014-05-05 23:23:53 +00001726 return CreateTypeDefinition(Ty, Unit);
1727}
1728
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001729llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001730CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1731 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001732 // Use the Module pointer as the key into the cache. This is a
1733 // nullptr if the "Module" is a PCH, which is safe because we don't
1734 // support chained PCH debug info, so there can only be a single PCH.
1735 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001736 auto ModRef = ModuleCache.find(M);
1737 if (ModRef != ModuleCache.end())
1738 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001739
1740 // Macro definitions that were defined with "-D" on the command line.
1741 SmallString<128> ConfigMacros;
1742 {
1743 llvm::raw_svector_ostream OS(ConfigMacros);
1744 const auto &PPOpts = CGM.getPreprocessorOpts();
1745 unsigned I = 0;
1746 // Translate the macro definitions back into a commmand line.
1747 for (auto &M : PPOpts.Macros) {
1748 if (++I > 1)
1749 OS << " ";
1750 const std::string &Macro = M.first;
1751 bool Undef = M.second;
1752 OS << "\"-" << (Undef ? 'U' : 'D');
1753 for (char c : Macro)
1754 switch (c) {
1755 case '\\' : OS << "\\\\"; break;
1756 case '"' : OS << "\\\""; break;
1757 default: OS << c;
1758 }
1759 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001760 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001761 }
Adrian Prantl66689202015-09-18 23:01:45 +00001762
Adrian Prantl835e6632015-09-24 16:10:10 +00001763 bool IsRootModule = M ? !M->Parent : true;
1764 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001765 // PCH files don't have a signature field in the control block,
1766 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001767 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001768 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001769 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001770 Mod.getPath(), TheCU->getProducer(), true,
1771 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001772 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001773 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001774 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001775 llvm::DIModule *Parent =
1776 IsRootModule ? nullptr
1777 : getOrCreateModuleRef(
1778 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1779 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001780 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001781 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1782 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001783 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001784 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001785}
1786
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001787llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1788 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001789 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001790 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001791 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001792 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001793
1794 // Bit size, align and offset of the type.
1795 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1796 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1797
1798 unsigned Flags = 0;
1799 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001800 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001801
Adrian Prantlfd696112015-10-01 00:48:51 +00001802 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001803 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001804 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1805 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001806
David Blaikieef8a9512014-05-05 23:23:53 +00001807 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001808 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001809
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001810 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001811 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001812 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001813
1814 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001815 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001816
1817 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1818 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001819 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001820 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001821 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001822 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001823
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001824 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 EltTys.push_back(InhTag);
1826 }
1827
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001828 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001829 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001830 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001831 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 unsigned PLine = getLineNumber(Loc);
1833 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1834 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001835 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1836 PD->getName(), PUnit, PLine,
1837 hasDefaultGetterName(PD, Getter) ? ""
1838 : getSelectorName(PD->getGetterName()),
1839 hasDefaultSetterName(PD, Setter) ? ""
1840 : getSelectorName(PD->getSetterName()),
1841 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001843 };
1844 {
1845 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001846 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001847 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001848 PropertySet.insert(PD->getIdentifier());
1849 AddProperty(PD);
1850 }
Manman Renefe1bac2016-01-27 20:00:32 +00001851 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001852 // Don't emit duplicate metadata for properties that were already in a
1853 // class extension.
1854 if (!PropertySet.insert(PD->getIdentifier()).second)
1855 continue;
1856 AddProperty(PD);
1857 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001858 }
1859
1860 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1861 unsigned FieldNo = 0;
1862 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1863 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001864 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001865 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001866 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001867
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 StringRef FieldName = Field->getName();
1869
1870 // Ignore unnamed fields.
1871 if (FieldName.empty())
1872 continue;
1873
1874 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001875 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001876 unsigned FieldLine = getLineNumber(Field->getLocation());
1877 QualType FType = Field->getType();
1878 uint64_t FieldSize = 0;
1879 unsigned FieldAlign = 0;
1880
1881 if (!FType->isIncompleteArrayType()) {
1882
1883 // Bit size, align and offset of the type.
1884 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001885 ? Field->getBitWidthValue(CGM.getContext())
1886 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 FieldAlign = CGM.getContext().getTypeAlign(FType);
1888 }
1889
1890 uint64_t FieldOffset;
1891 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1892 // We don't know the runtime offset of an ivar if we're using the
1893 // non-fragile ABI. For bitfields, use the bit offset into the first
1894 // byte of storage of the bitfield. For other fields, use zero.
1895 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001896 FieldOffset =
1897 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 FieldOffset %= CGM.getContext().getCharWidth();
1899 } else {
1900 FieldOffset = 0;
1901 }
1902 } else {
1903 FieldOffset = RL.getFieldOffset(FieldNo);
1904 }
1905
1906 unsigned Flags = 0;
1907 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001908 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001910 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001911 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001912 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001913
Craig Topper8a13c412014-05-21 05:09:00 +00001914 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001916 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001917 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001919 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001920 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001921 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1923 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001924 PropertyNode = DBuilder.createObjCProperty(
1925 PD->getName(), PUnit, PLine,
1926 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1927 PD->getGetterName()),
1928 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1929 PD->getSetterName()),
1930 PD->getPropertyAttributes(),
1931 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 }
1933 }
1934 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001935 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1936 FieldSize, FieldAlign, FieldOffset, Flags,
1937 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001938 EltTys.push_back(FieldTy);
1939 }
1940
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001941 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001942 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001943
Guy Benyei11169dd2012-12-18 14:30:41 +00001944 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001945 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001946}
1947
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001948llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1949 llvm::DIFile *Unit) {
1950 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001951 int64_t Count = Ty->getNumElements();
1952 if (Count == 0)
1953 // If number of elements are not known then this is an unbounded array.
1954 // Use Count == -1 to express such arrays.
1955 Count = -1;
1956
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001957 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001958 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001959
1960 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1961 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1962
1963 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1964}
1965
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001966llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 uint64_t Size;
1968 uint64_t Align;
1969
1970 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1971 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1972 Size = 0;
1973 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001974 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001975 } else if (Ty->isIncompleteArrayType()) {
1976 Size = 0;
1977 if (Ty->getElementType()->isIncompleteType())
1978 Align = 0;
1979 else
1980 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001981 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001982 Size = 0;
1983 Align = 0;
1984 } else {
1985 // Size and align of the whole array, not the element type.
1986 Size = CGM.getContext().getTypeSize(Ty);
1987 Align = CGM.getContext().getTypeAlign(Ty);
1988 }
1989
1990 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1991 // interior arrays, do we care? Why aren't nested arrays represented the
1992 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001993 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001994 QualType EltTy(Ty, 0);
1995 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1996 // If the number of elements is known, then count is that number. Otherwise,
1997 // it's -1. This allows us to represent a subrange with an array of 0
1998 // elements, like this:
1999 //
2000 // struct foo {
2001 // int x[0];
2002 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002003 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2005 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002006
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 // FIXME: Verify this is right for VLAs.
2008 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2009 EltTy = Ty->getElementType();
2010 }
2011
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002012 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002013
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002014 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2015 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002016}
2017
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002018llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2019 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002020 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2021 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002022}
2023
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002024llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2025 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002026 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2027 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002028}
2029
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002030llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2031 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00002032 uint64_t Size =
2033 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002034 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002035 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002036 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00002037 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002038
2039 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002040 Ty->getPointeeType()->getAs<FunctionProtoType>();
2041 return DBuilder.createMemberPointerType(
2042 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2043 Ty->getClass(), FPT->getTypeQuals())),
2044 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00002045 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00002046}
2047
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002048llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002049 // Ignore the atomic wrapping
2050 // FIXME: What is the correct representation?
2051 return getOrCreateType(Ty->getValueType(), U);
2052}
2053
Xiuli Pan9c14e282016-01-09 12:53:17 +00002054llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2055 llvm::DIFile *U) {
2056 return getOrCreateType(Ty->getElementType(), U);
2057}
2058
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002059llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002060 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002061
Guy Benyei11169dd2012-12-18 14:30:41 +00002062 uint64_t Size = 0;
2063 uint64_t Align = 0;
2064 if (!ED->getTypeForDecl()->isIncompleteType()) {
2065 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2066 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2067 }
2068
Manman Rene0064d82013-08-29 23:19:58 +00002069 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2070
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002071 bool isImportedFromModule =
2072 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2073
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 // If this is just a forward declaration, construct an appropriately
2075 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002076 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002077 // Note that it is possible for enums to be created as part of
2078 // their own declcontext. In this case a FwdDecl will be created
2079 // twice. This doesn't cause a problem because both FwdDecls are
2080 // entered into the ReplaceMap: finalize() will replace the first
2081 // FwdDecl with the second and then replace the second with
2082 // complete type.
Amjad Aboud0ccbfa32016-04-29 16:08:08 +00002083 llvm::DIScope *EDContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002084 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002085 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2086 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002087
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 unsigned Line = getLineNumber(ED->getLocation());
2089 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002090 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002091 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2092 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002093
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002094 ReplaceMap.emplace_back(
2095 std::piecewise_construct, std::make_tuple(Ty),
2096 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002097 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 }
2099
David Blaikie483a9da2014-05-06 18:35:21 +00002100 return CreateTypeDefinition(Ty);
2101}
2102
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002103llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002104 const EnumDecl *ED = Ty->getDecl();
2105 uint64_t Size = 0;
2106 uint64_t Align = 0;
2107 if (!ED->getTypeForDecl()->isIncompleteType()) {
2108 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2109 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2110 }
2111
2112 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2113
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002114 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002115 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002117 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002118 Enumerators.push_back(DBuilder.createEnumerator(
2119 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002120 }
2121
2122 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002123 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002124
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002125 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 unsigned Line = getLineNumber(ED->getLocation());
Amjad Aboud0ccbfa32016-04-29 16:08:08 +00002127 llvm::DIScope *EnumContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002128 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002129 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2130 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2131 Line, Size, Align, EltArray, ClassTy,
2132 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002133}
2134
David Blaikie05491062013-01-21 04:37:12 +00002135static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2136 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002137 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002138 Qualifiers InnerQuals = T.getLocalQualifiers();
2139 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2140 // that is already there.
2141 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2142 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 QualType LastT = T;
2144 switch (T->getTypeClass()) {
2145 default:
David Blaikie05491062013-01-21 04:37:12 +00002146 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002147 case Type::TemplateSpecialization: {
2148 const auto *Spec = cast<TemplateSpecializationType>(T);
2149 if (Spec->isTypeAlias())
2150 return C.getQualifiedType(T.getTypePtr(), Quals);
2151 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002152 break;
2153 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002154 case Type::TypeOfExpr:
2155 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2156 break;
2157 case Type::TypeOf:
2158 T = cast<TypeOfType>(T)->getUnderlyingType();
2159 break;
2160 case Type::Decltype:
2161 T = cast<DecltypeType>(T)->getUnderlyingType();
2162 break;
2163 case Type::UnaryTransform:
2164 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2165 break;
2166 case Type::Attributed:
2167 T = cast<AttributedType>(T)->getEquivalentType();
2168 break;
2169 case Type::Elaborated:
2170 T = cast<ElaboratedType>(T)->getNamedType();
2171 break;
2172 case Type::Paren:
2173 T = cast<ParenType>(T)->getInnerType();
2174 break;
David Blaikie05491062013-01-21 04:37:12 +00002175 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 break;
2178 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002179 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002180 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002181 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 break;
2183 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002184
Guy Benyei11169dd2012-12-18 14:30:41 +00002185 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002186 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002187 } while (true);
2188}
2189
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002190llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002191
2192 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002193 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002194
David Blaikief427b002014-05-06 03:42:01 +00002195 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 if (it != TypeCache.end()) {
2197 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002198 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002199 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 }
2201
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002202 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002203}
2204
David Blaikie0e716b42014-03-03 23:48:23 +00002205void CGDebugInfo::completeTemplateDefinition(
2206 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002207 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002208 return;
2209
David Blaikie0e716b42014-03-03 23:48:23 +00002210 completeClassData(&SD);
2211 // In case this type has no member function definitions being emitted, ensure
2212 // it is retained
2213 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2214}
2215
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002216llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002218 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002219
2220 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002221 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002222
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002223 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 return T;
2225
Adrian Prantlca844182015-09-11 17:23:03 +00002226 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002227 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002228
2229 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002230 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002231
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 return Res;
2233}
2234
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002235llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002236 // A forward declaration inside a module header does not belong to the module.
2237 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2238 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002239 if (DebugTypeExtRefs && D->isFromASTFile()) {
2240 // Record a reference to an imported clang module or precompiled header.
2241 auto *Reader = CGM.getContext().getExternalSource();
2242 auto Idx = D->getOwningModuleID();
2243 auto Info = Reader->getSourceDescriptor(Idx);
2244 if (Info)
2245 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2246 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002247 // We are building a clang module or a precompiled header.
2248 //
2249 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2250 // and it wouldn't be necessary to specify the parent scope
2251 // because the type is already unique by definition (it would look
2252 // like the output of -fno-standalone-debug). On the other hand,
2253 // the parent scope helps a consumer to quickly locate the object
2254 // file where the type's definition is located, so it might be
2255 // best to make this behavior a command line or debugger tuning
2256 // option.
2257 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2258 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002259 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002260 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2261 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002262 } else {
2263 // This the precompiled header being built.
2264 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002265 }
2266 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002267
Adrian Prantl9402cef2015-09-20 16:51:35 +00002268 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002269}
2270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002271llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 // Handle qualifiers, which recursively handles what they refer to.
2273 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002274 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002275
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 // Work out details of type.
2277 switch (Ty->getTypeClass()) {
2278#define TYPE(Class, Base)
2279#define ABSTRACT_TYPE(Class, Base)
2280#define NON_CANONICAL_TYPE(Class, Base)
2281#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2282#include "clang/AST/TypeNodes.def"
2283 llvm_unreachable("Dependent types cannot show up in debug information");
2284
2285 case Type::ExtVector:
2286 case Type::Vector:
2287 return CreateType(cast<VectorType>(Ty), Unit);
2288 case Type::ObjCObjectPointer:
2289 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2290 case Type::ObjCObject:
2291 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2292 case Type::ObjCInterface:
2293 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2294 case Type::Builtin:
2295 return CreateType(cast<BuiltinType>(Ty));
2296 case Type::Complex:
2297 return CreateType(cast<ComplexType>(Ty));
2298 case Type::Pointer:
2299 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002300 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002301 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002302 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002303 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002304 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 case Type::BlockPointer:
2306 return CreateType(cast<BlockPointerType>(Ty), Unit);
2307 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002308 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002310 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002311 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002312 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 case Type::FunctionProto:
2314 case Type::FunctionNoProto:
2315 return CreateType(cast<FunctionType>(Ty), Unit);
2316 case Type::ConstantArray:
2317 case Type::VariableArray:
2318 case Type::IncompleteArray:
2319 return CreateType(cast<ArrayType>(Ty), Unit);
2320
2321 case Type::LValueReference:
2322 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2323 case Type::RValueReference:
2324 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2325
2326 case Type::MemberPointer:
2327 return CreateType(cast<MemberPointerType>(Ty), Unit);
2328
2329 case Type::Atomic:
2330 return CreateType(cast<AtomicType>(Ty), Unit);
2331
Xiuli Pan9c14e282016-01-09 12:53:17 +00002332 case Type::Pipe:
2333 return CreateType(cast<PipeType>(Ty), Unit);
2334
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002336 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2337
David Blaikie42edade2014-11-11 20:44:45 +00002338 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002339 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002340 case Type::Elaborated:
2341 case Type::Paren:
2342 case Type::SubstTemplateTypeParm:
2343 case Type::TypeOfExpr:
2344 case Type::TypeOf:
2345 case Type::Decltype:
2346 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002347 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002348 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002350
David Blaikie42edade2014-11-11 20:44:45 +00002351 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002352}
2353
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002354llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2355 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002356 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002357
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002358 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002359
2360 // We may have cached a forward decl when we could have created
2361 // a non-forward decl. Go ahead and create a non-forward decl
2362 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002363 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002364 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002365
2366 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002367 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002368
2369 // Propagate members from the declaration to the definition
2370 // CreateType(const RecordType*) will overwrite this with the members in the
2371 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002372 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002373
Guy Benyei11169dd2012-12-18 14:30:41 +00002374 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002375 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002376 return Res;
2377}
2378
2379// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002380llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002381 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002382
Guy Benyei11169dd2012-12-18 14:30:41 +00002383 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002384 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 unsigned Line = getLineNumber(RD->getLocation());
2386 StringRef RDName = getClassName(RD);
2387
Amjad Aboud0ccbfa32016-04-29 16:08:08 +00002388 llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002389
David Blaikied2785892013-08-18 17:36:19 +00002390 // If we ended up creating the type during the context chain construction,
2391 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002393 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002394 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002395 return T;
David Blaikied2785892013-08-18 17:36:19 +00002396
Adrian Prantl381e7552014-02-04 21:29:50 +00002397 // If this is just a forward or incomplete declaration, construct an
2398 // appropriately marked node and just return it.
2399 const RecordDecl *D = RD->getDefinition();
2400 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002401 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002402
2403 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2404 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002405
Manman Rene0064d82013-08-29 23:19:58 +00002406 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2407
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002408 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002409 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2410 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002411
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002412 // Elements of composite types usually have back to the type, creating
2413 // uniquing cycles. Distinct nodes are more efficient.
2414 switch (RealDecl->getTag()) {
2415 default:
2416 llvm_unreachable("invalid composite type tag");
2417
2418 case llvm::dwarf::DW_TAG_array_type:
2419 case llvm::dwarf::DW_TAG_enumeration_type:
2420 // Array elements and most enumeration elements don't have back references,
2421 // so they don't tend to be involved in uniquing cycles and there is some
2422 // chance of merging them when linking together two modules. Only make
2423 // them distinct if they are ODR-uniqued.
2424 if (FullName.empty())
2425 break;
2426
2427 case llvm::dwarf::DW_TAG_structure_type:
2428 case llvm::dwarf::DW_TAG_union_type:
2429 case llvm::dwarf::DW_TAG_class_type:
2430 // Immediatley resolve to a distinct node.
2431 RealDecl =
2432 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2433 break;
2434 }
2435
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002436 RegionMap[Ty->getDecl()].reset(RealDecl);
2437 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002438
David Blaikieadfbf992013-08-18 16:55:33 +00002439 if (const ClassTemplateSpecializationDecl *TSpecial =
2440 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002442 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002443 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002444}
2445
David Blaikieadfbf992013-08-18 16:55:33 +00002446void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002447 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002448 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002449 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002450 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2451 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002452 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002453 while (1) {
2454 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2455 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2456 if (PBT && !BRL.isPrimaryBaseVirtual())
2457 PBase = PBT;
2458 else
2459 break;
2460 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002462 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2463 getOrCreateFile(RD->getLocation())));
2464 } else if (RD->isDynamicClass())
2465 ContainingType = RealDecl;
2466
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002467 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002468}
2469
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002470llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002471 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002473 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2474 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002475 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002476 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 *Offset += FieldSize;
2478 return Ty;
2479}
2480
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002481void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002482 StringRef &Name,
2483 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484 llvm::DIScope *&FDContext,
2485 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002486 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002487 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2488 Name = getFunctionName(FD);
2489 // Use mangled name as linkage name for C/C++ functions.
2490 if (FD->hasPrototype()) {
2491 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002493 }
2494 // No need to replicate the linkage name if it isn't different from the
2495 // subprogram name, no need to have it at all unless coverage is enabled or
2496 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002497 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2498 !CGM.getCodeGenOpts().EmitGcovNotes &&
2499 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002500 LinkageName = StringRef();
2501
Benjamin Kramer8c305922016-02-02 11:06:51 +00002502 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002503 if (const NamespaceDecl *NSDecl =
2504 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2505 FDContext = getOrCreateNameSpace(NSDecl);
2506 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002507 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2508 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2509 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2510 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002511 // Collect template parameters.
2512 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2513 }
2514}
2515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002516void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002517 unsigned &LineNo, QualType &T,
2518 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002519 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002520 Unit = getOrCreateFile(VD->getLocation());
2521 LineNo = getLineNumber(VD->getLocation());
2522
2523 setLocation(VD->getLocation());
2524
2525 T = VD->getType();
2526 if (T->isIncompleteArrayType()) {
2527 // CodeGen turns int[] into int[1] so we'll do the same here.
2528 llvm::APInt ConstVal(32, 1);
2529 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2530
2531 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2532 ArrayType::Normal, 0);
2533 }
2534
2535 Name = VD->getName();
2536 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2537 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2538 LinkageName = CGM.getMangledName(VD);
2539 if (LinkageName == Name)
2540 LinkageName = StringRef();
2541
2542 // Since we emit declarations (DW_AT_members) for static members, place the
2543 // definition of those static members in the namespace they were declared in
2544 // in the source code (the lexical decl context).
2545 // FIXME: Generalize this for even non-member global variables where the
2546 // declaration and definition may have different lexical decl contexts, once
2547 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002548 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2549 : VD->getDeclContext();
2550 // When a record type contains an in-line initialization of a static data
2551 // member, and the record type is marked as __declspec(dllexport), an implicit
2552 // definition of the member will be created in the record context. DWARF
2553 // doesn't seem to have a nice way to describe this in a form that consumers
2554 // are likely to understand, so fake the "normal" situation of a definition
2555 // outside the class by putting it in the global scope.
2556 if (DC->isRecord())
2557 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002558
Amjad Aboud0ccbfa32016-04-29 16:08:08 +00002559 if (VD->isStaticLocal()) {
2560 // Get context for static locals (that are technically globals) the same way
2561 // we do for "local" locals -- by using current lexical block.
2562 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2563 VDContext = LexicalBlockStack.back();
2564 } else {
2565 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2566 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
2567 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002568}
2569
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002570llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002571CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002572 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002573 StringRef Name, LinkageName;
2574 unsigned Flags = 0;
2575 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002576 llvm::DIFile *Unit = getOrCreateFile(Loc);
2577 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002578 unsigned Line = getLineNumber(Loc);
2579
2580 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2581 TParamsArray, Flags);
2582 // Build function type.
2583 SmallVector<QualType, 16> ArgTypes;
2584 for (const ParmVarDecl *Parm: FD->parameters())
2585 ArgTypes.push_back(Parm->getType());
2586 QualType FnType =
2587 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2588 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002589 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002590 DContext, Name, LinkageName, Unit, Line,
2591 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002592 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002593 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002594 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002595 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2596 std::make_tuple(CanonDecl),
2597 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002598 return SP;
2599}
2600
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002601llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002602CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2603 QualType T;
2604 StringRef Name, LinkageName;
2605 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002606 llvm::DIFile *Unit = getOrCreateFile(Loc);
2607 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002608 unsigned Line = getLineNumber(Loc);
2609
2610 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002611 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2612 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2613 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002614 FwdDeclReplaceMap.emplace_back(
2615 std::piecewise_construct,
2616 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2617 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002618 return GV;
2619}
2620
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002621llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002622 // We only need a declaration (not a definition) of the type - so use whatever
2623 // we would otherwise do to get a type for a pointee. (forward declarations in
2624 // limited debug info, full definitions (if the type definition is available)
2625 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002626 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2627 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002628 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002629 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002630
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002631 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002632 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002633
2634 // No definition for now. Emit a forward definition that might be
2635 // merged with a potential upcoming definition.
2636 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2637 return getFunctionForwardDeclaration(FD);
2638 else if (const auto *VD = dyn_cast<VarDecl>(D))
2639 return getGlobalVariableForwardDeclaration(VD);
2640
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002641 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002642}
2643
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002644llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002645 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002646 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002647
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002649 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002650 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002651
2652 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002653 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002654
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002655 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002656 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002657 if (const CXXMethodDecl *MD =
2658 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002659 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002660 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002661 }
2662 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002663 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002664 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002665 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002666 return SP;
2667 }
2668
Aaron Ballman86c93902014-03-06 23:45:36 +00002669 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002670 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002671 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002672 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002673 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 return SP;
2675 }
2676 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002677 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002678}
2679
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002680// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002681// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002682llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002683 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002684 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002685 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002686 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2687 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002688 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002689
2690 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2691 return getOrCreateMethodType(Method, F);
2692 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2693 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002694 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002695
2696 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002697 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002698
2699 // Replace the instancetype keyword with the actual type.
2700 if (ResultTy == CGM.getContext().getObjCInstanceType())
2701 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002702 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002703
Adrian Prantl7bec9032013-05-10 21:08:31 +00002704 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002706 QualType SelfDeclTy;
2707 if (auto *SelfDecl = OMethod->getSelfDecl())
2708 SelfDeclTy = SelfDecl->getType();
2709 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2710 if (FPT->getNumParams() > 1)
2711 SelfDeclTy = FPT->getParamType(0);
2712 if (!SelfDeclTy.isNull())
2713 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002715 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002716 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002718 for (const auto *PI : OMethod->params())
2719 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002720 // Variadic methods need a special marker at the end of the type list.
2721 if (OMethod->isVariadic())
2722 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002723
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002724 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Eric Christopher28a6db52015-10-15 06:56:08 +00002725 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002727
Adrian Prantl800faef2014-02-25 23:42:18 +00002728 // Handle variadic function types; they need an additional
2729 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002730 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2731 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002732 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002733 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2734 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2735 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2736 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2737 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002738 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +00002739 return DBuilder.createSubroutineType(EltTypeArray);
Adrian Prantld45ba252014-02-25 19:38:11 +00002740 }
2741
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002742 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002743}
2744
Eric Christophere7b87e52014-10-26 23:40:33 +00002745void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2746 SourceLocation ScopeLoc, QualType FnType,
2747 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002748
2749 StringRef Name;
2750 StringRef LinkageName;
2751
2752 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2753
2754 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002755 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002756
Guy Benyei11169dd2012-12-18 14:30:41 +00002757 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002758 llvm::DIFile *Unit = getOrCreateFile(Loc);
2759 llvm::DIScope *FDContext = Unit;
2760 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 if (!HasDecl) {
2762 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002763 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002765 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002766 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002769 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002770 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002771 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 return;
2773 }
2774 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002775 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2776 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2778 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002779 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 } else {
2781 // Use llvm function name.
2782 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002783 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 }
2785 if (!Name.empty() && Name[0] == '\01')
2786 Name = Name.substr(1);
2787
Adrian Prantl42d71b92014-04-10 23:21:53 +00002788 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002789 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002790 // Artificial functions without a location should not silently reuse CurLoc.
2791 if (Loc.isInvalid())
2792 CurLoc = SourceLocation();
2793 }
2794 unsigned LineNo = getLineNumber(Loc);
2795 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002796
Eric Christopher8018e412014-03-27 18:50:35 +00002797 // FIXME: The function declaration we're constructing here is mostly reusing
2798 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2799 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2800 // all subprograms instead of the actual context since subprogram definitions
2801 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002802 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002803 FDContext, Name, LinkageName, Unit, LineNo,
2804 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002805 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002806 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002807 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002808 // We might get here with a VarDecl in the case we're generating
2809 // code for the initialization of globals. Do not record these decls
2810 // as they will overwrite the actual VarDecl Decl in the cache.
2811 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002812 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002813
Adrian Prantlbebb8932014-03-21 21:01:58 +00002814 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002815 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002816
Guy Benyei11169dd2012-12-18 14:30:41 +00002817 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002818 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002819}
2820
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002821void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2822 QualType FnType) {
2823 StringRef Name;
2824 StringRef LinkageName;
2825
2826 const Decl *D = GD.getDecl();
2827 if (!D)
2828 return;
2829
2830 unsigned Flags = 0;
2831 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002832 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002833 llvm::DINodeArray TParamsArray;
2834 if (isa<FunctionDecl>(D)) {
2835 // If there is a DISubprogram for this function available then use it.
2836 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2837 TParamsArray, Flags);
2838 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2839 Name = getObjCMethodName(OMD);
2840 Flags |= llvm::DINode::FlagPrototyped;
2841 } else {
2842 llvm_unreachable("not a function or ObjC method");
2843 }
2844 if (!Name.empty() && Name[0] == '\01')
2845 Name = Name.substr(1);
2846
2847 if (D->isImplicit()) {
2848 Flags |= llvm::DINode::FlagArtificial;
2849 // Artificial functions without a location should not silently reuse CurLoc.
2850 if (Loc.isInvalid())
2851 CurLoc = SourceLocation();
2852 }
2853 unsigned LineNo = getLineNumber(Loc);
2854 unsigned ScopeLine = 0;
2855
Adrian Prantle76bda52016-04-15 15:55:45 +00002856 DBuilder.retainType(DBuilder.createFunction(
2857 FDContext, Name, LinkageName, Unit, LineNo,
2858 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2859 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2860 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002861}
2862
David Blaikie835afb22015-01-21 23:08:17 +00002863void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 // Update our current location
2865 setLocation(Loc);
2866
Eric Christophere7b87e52014-10-26 23:40:33 +00002867 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2868 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002869
Adrian Prantle83b1302014-01-07 22:05:52 +00002870 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002871 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002872 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002873}
2874
Guy Benyei11169dd2012-12-18 14:30:41 +00002875void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002876 llvm::MDNode *Back = nullptr;
2877 if (!LexicalBlockStack.empty())
2878 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002879 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002880 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002881 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002882}
2883
Eric Christopher0fdcb312013-05-16 00:52:20 +00002884void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2885 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002886 // Set our current location.
2887 setLocation(Loc);
2888
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002890 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2891 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002892
Benjamin Kramer8c305922016-02-02 11:06:51 +00002893 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002894 return;
2895
2896 // Create a new lexical block and push it on the stack.
2897 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002898}
2899
Eric Christopher0fdcb312013-05-16 00:52:20 +00002900void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2901 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2903
2904 // Provide an entry in the line table for the end of the block.
2905 EmitLocation(Builder, Loc);
2906
Benjamin Kramer8c305922016-02-02 11:06:51 +00002907 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002908 return;
2909
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 LexicalBlockStack.pop_back();
2911}
2912
Guy Benyei11169dd2012-12-18 14:30:41 +00002913void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2914 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2915 unsigned RCount = FnBeginRegionCount.back();
2916 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2917
2918 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002919 while (LexicalBlockStack.size() != RCount) {
2920 // Provide an entry in the line table for the end of the block.
2921 EmitLocation(Builder, CurLoc);
2922 LexicalBlockStack.pop_back();
2923 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002924 FnBeginRegionCount.pop_back();
2925}
2926
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002927llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002928 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002929
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002930 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 QualType FType;
2932 uint64_t FieldSize, FieldOffset;
2933 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002934
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002935 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002936 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002937
2938 FieldOffset = 0;
2939 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2940 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2941 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2942 FType = CGM.getContext().IntTy;
2943 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2944 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2945
2946 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2947 if (HasCopyAndDispose) {
2948 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002949 EltTys.push_back(
2950 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2951 EltTys.push_back(
2952 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002953 }
2954 bool HasByrefExtendedLayout;
2955 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002956 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2957 HasByrefExtendedLayout) &&
2958 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002959 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002960 EltTys.push_back(
2961 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002962 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002963
Guy Benyei11169dd2012-12-18 14:30:41 +00002964 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2965 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002966 CGM.getTarget().getPointerAlign(0))) {
2967 CharUnits FieldOffsetInBytes =
2968 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00002969 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00002970 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002971
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 if (NumPaddingBytes.isPositive()) {
2973 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2974 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2975 pad, ArrayType::Normal, 0);
2976 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2977 }
2978 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002979
Guy Benyei11169dd2012-12-18 14:30:41 +00002980 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002981 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 FieldSize = CGM.getContext().getTypeSize(FType);
2983 FieldAlign = CGM.getContext().toBits(Align);
2984
Eric Christopherb2a008c2013-05-16 00:45:12 +00002985 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002986 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2987 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002988 EltTys.push_back(FieldTy);
2989 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002990
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002991 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002992
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002993 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002994
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002996 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002997}
2998
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002999void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3000 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003001 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003002 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3004
David Blaikie7fceebf2013-08-19 03:37:48 +00003005 bool Unwritten =
3006 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3007 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003008 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003009 if (!Unwritten)
3010 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003011 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 uint64_t XOffset = 0;
3013 if (VD->hasAttr<BlocksAttr>())
3014 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003015 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003016 Ty = getOrCreateType(VD->getType(), Unit);
3017
3018 // If there is no debug info for this type then do not emit debug info
3019 // for this variable.
3020 if (!Ty)
3021 return;
3022
Guy Benyei11169dd2012-12-18 14:30:41 +00003023 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003024 unsigned Line = 0;
3025 unsigned Column = 0;
3026 if (!Unwritten) {
3027 Line = getLineNumber(VD->getLocation());
3028 Column = getColumnNumber(VD->getLocation());
3029 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003030 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 unsigned Flags = 0;
3032 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003033 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003034 // If this is the first argument and it is implicit then
3035 // give it an object pointer flag.
3036 // FIXME: There has to be a better way to do this, but for static
3037 // functions there won't be an implicit param at arg1 and
3038 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003039 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003040 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003041 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003042 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3043 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003044 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003045
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003046 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003047
3048 StringRef Name = VD->getName();
3049 if (!Name.empty()) {
3050 if (VD->hasAttr<BlocksAttr>()) {
3051 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003052 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 // offset of __forwarding field
3054 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003055 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003056 Expr.push_back(offset.getQuantity());
3057 Expr.push_back(llvm::dwarf::DW_OP_deref);
3058 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 // offset of x field
3060 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003061 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003062
3063 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003064 auto *D = ArgNo
3065 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3066 *ArgNo, Unit, Line, Ty)
3067 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3068 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003069
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003071 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3072 llvm::DebugLoc::get(Line, Column, Scope),
3073 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003075 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003076 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003077 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3078 // If VD is an anonymous union then Storage represents value for
3079 // all union fields.
3080 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3081 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003082 // GDB has trouble finding local variables in anonymous unions, so we emit
3083 // artifical local variables for each of the members.
3084 //
3085 // FIXME: Remove this code as soon as GDB supports this.
3086 // The debug info verifier in LLVM operates based on the assumption that a
3087 // variable has the same size as its storage and we had to disable the check
3088 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003089 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003090 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003091 StringRef FieldName = Field->getName();
3092
3093 // Ignore unnamed fields. Do not ignore unnamed records.
3094 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3095 continue;
3096
3097 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003098 auto *D = DBuilder.createAutoVariable(
3099 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3100 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003101
3102 // Insert an llvm.dbg.declare into the current block.
3103 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3104 llvm::DebugLoc::get(Line, Column, Scope),
3105 Builder.GetInsertBlock());
3106 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003107 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003109
3110 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003111 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003112 ArgNo
3113 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3114 Ty, CGM.getLangOpts().Optimize,
3115 Flags)
3116 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3117 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003118
3119 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003120 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3121 llvm::DebugLoc::get(Line, Column, Scope),
3122 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003123}
3124
3125void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3126 llvm::Value *Storage,
3127 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003128 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003129 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003130}
3131
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003132llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3133 llvm::DIType *Ty) {
3134 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003135 if (CachedTy)
3136 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003137 return DBuilder.createObjectPointerType(Ty);
3138}
3139
Eric Christophere7b87e52014-10-26 23:40:33 +00003140void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3141 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003142 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003143 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003145
Craig Topper8a13c412014-05-21 05:09:00 +00003146 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003147 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003148
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003150
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003152 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3153 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 if (isByRef)
3155 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003156 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 Ty = getOrCreateType(VD->getType(), Unit);
3158
3159 // Self is passed along as an implicit non-arg variable in a
3160 // block. Mark it as the object pointer.
3161 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003162 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003163
3164 // Get location information.
3165 unsigned Line = getLineNumber(VD->getLocation());
3166 unsigned Column = getColumnNumber(VD->getLocation());
3167
3168 const llvm::DataLayout &target = CGM.getDataLayout();
3169
3170 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003171 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3173
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003174 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003175 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003176 addr.push_back(llvm::dwarf::DW_OP_deref);
3177 addr.push_back(llvm::dwarf::DW_OP_plus);
3178 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003179 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003180 addr.push_back(llvm::dwarf::DW_OP_deref);
3181 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003183 offset =
3184 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003185 addr.push_back(offset.getQuantity());
3186 addr.push_back(llvm::dwarf::DW_OP_deref);
3187 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 // offset of x field
3189 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003190 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003191 }
3192
3193 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003194 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003195 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003196 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003197
Guy Benyei11169dd2012-12-18 14:30:41 +00003198 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003199 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3200 if (InsertPoint)
3201 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3202 InsertPoint);
3203 else
3204 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3205 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003206}
3207
Guy Benyei11169dd2012-12-18 14:30:41 +00003208void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3209 unsigned ArgNo,
3210 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003211 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003212 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003213}
3214
3215namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003216struct BlockLayoutChunk {
3217 uint64_t OffsetInBits;
3218 const BlockDecl::Capture *Capture;
3219};
3220bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3221 return l.OffsetInBits < r.OffsetInBits;
3222}
Guy Benyei11169dd2012-12-18 14:30:41 +00003223}
3224
3225void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003226 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003227 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003228 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003230 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003231 ASTContext &C = CGM.getContext();
3232 const BlockDecl *blockDecl = block.getBlockDecl();
3233
3234 // Collect some general information about the block's location.
3235 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003236 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 unsigned line = getLineNumber(loc);
3238 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003239
Guy Benyei11169dd2012-12-18 14:30:41 +00003240 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003241 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003242
3243 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003244 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003245
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003246 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003247 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3248 blockLayout->getElementOffsetInBits(0),
3249 tunit, tunit));
3250 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3251 blockLayout->getElementOffsetInBits(1),
3252 tunit, tunit));
3253 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3254 blockLayout->getElementOffsetInBits(2),
3255 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003256 auto *FnTy = block.getBlockExpr()->getFunctionType();
3257 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3258 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003259 blockLayout->getElementOffsetInBits(3),
3260 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003261 fields.push_back(createFieldType(
3262 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3263 ? C.getBlockDescriptorExtendedType()
3264 : C.getBlockDescriptorType()),
3265 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003266
3267 // We want to sort the captures by offset, not because DWARF
3268 // requires this, but because we're paranoid about debuggers.
3269 SmallVector<BlockLayoutChunk, 8> chunks;
3270
3271 // 'this' capture.
3272 if (blockDecl->capturesCXXThis()) {
3273 BlockLayoutChunk chunk;
3274 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003275 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003276 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 chunks.push_back(chunk);
3278 }
3279
3280 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003281 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003282 const VarDecl *variable = capture.getVariable();
3283 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3284
3285 // Ignore constant captures.
3286 if (captureInfo.isConstant())
3287 continue;
3288
3289 BlockLayoutChunk chunk;
3290 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003291 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003292 chunk.Capture = &capture;
3293 chunks.push_back(chunk);
3294 }
3295
3296 // Sort by offset.
3297 llvm::array_pod_sort(chunks.begin(), chunks.end());
3298
Eric Christophere7b87e52014-10-26 23:40:33 +00003299 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3300 e = chunks.end();
3301 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 uint64_t offsetInBits = i->OffsetInBits;
3303 const BlockDecl::Capture *capture = i->Capture;
3304
3305 // If we have a null capture, this must be the C++ 'this' capture.
3306 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003307 QualType type;
3308 if (auto *Method =
3309 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3310 type = Method->getThisType(C);
3311 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3312 type = QualType(RDecl->getTypeForDecl(), 0);
3313 else
3314 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003315
3316 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3317 offsetInBits, tunit, tunit));
3318 continue;
3319 }
3320
3321 const VarDecl *variable = capture->getVariable();
3322 StringRef name = variable->getName();
3323
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003324 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003325 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003326 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003327
3328 // FIXME: this creates a second copy of this type!
3329 uint64_t xoffset;
3330 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003331 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3332 fieldType =
3333 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3334 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003335 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003336 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3337 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003338 }
3339 fields.push_back(fieldType);
3340 }
3341
3342 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003343 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3344 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003345
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003346 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003347
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003348 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003349 tunit, typeName.str(), tunit, line,
3350 CGM.getContext().toBits(block.BlockSize),
3351 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003352 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3353
3354 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003355 unsigned flags = llvm::DINode::FlagArtificial;
3356 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003357
3358 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003359 auto *debugVar = DBuilder.createParameterVariable(
3360 scope, Arg->getName(), ArgNo, tunit, line, type,
3361 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003362
Adrian Prantl616bef42013-03-14 21:52:59 +00003363 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003364 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003365 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003366 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003367 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003368 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003369
Adrian Prantl616bef42013-03-14 21:52:59 +00003370 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003371 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3372 llvm::DebugLoc::get(line, column, scope),
3373 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003374}
3375
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003376llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003377CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3378 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003379 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003380
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003381 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003382 if (MI != StaticDataMemberCache.end()) {
3383 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003384 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003385 }
David Blaikiece763042013-08-20 21:49:21 +00003386
3387 // If the member wasn't found in the cache, lazily construct and add it to the
3388 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003389 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003390 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003391 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003392}
3393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003394llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3395 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3396 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3397 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003398
3399 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003400 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003401 StringRef FieldName = Field->getName();
3402
3403 // Ignore unnamed fields, but recurse into anonymous records.
3404 if (FieldName.empty()) {
3405 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3406 if (RT)
3407 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3408 Var, DContext);
3409 continue;
3410 }
3411 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003412 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3413 LineNo, FieldTy,
3414 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003415 }
3416 return GV;
3417}
3418
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003419void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003421 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003422 if (D->hasAttr<NoDebugAttr>())
3423 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003424 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003425 llvm::DIFile *Unit = nullptr;
3426 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003427 unsigned LineNo;
3428 StringRef DeclName, LinkageName;
3429 QualType T;
3430 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003431
3432 // Attempt to store one global variable for the declaration - even if we
3433 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003434 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003435
3436 // If this is an anonymous union then we'll want to emit a global
3437 // variable for each member of the anonymous union so that it's possible
3438 // to find the name of any field in the union.
3439 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003440 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003441 assert(RD->isAnonymousStructOrUnion() &&
3442 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003443 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3444 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003445 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003446 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3447 Var->hasInternalLinkage(), Var,
3448 getOrCreateStaticDataMemberDeclarationOrNull(D));
3449 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003450 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003451}
3452
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003453void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3454 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003455 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003456 if (VD->hasAttr<NoDebugAttr>())
3457 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003458 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003460 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003461 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003462 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3463 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3464 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3465 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3466 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003467 // Do not use global variables for enums.
3468 //
3469 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003470 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003471 return;
David Blaikiea15565562014-04-04 20:56:17 +00003472 // Do not emit separate definitions for function local const/statics.
3473 if (isa<FunctionDecl>(VD->getDeclContext()))
3474 return;
David Blaikiebb113912014-04-05 07:23:17 +00003475 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003476 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003477 if (VarD->isStaticDataMember()) {
3478 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003479 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003480 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003481 //
3482 // FIXME: This is probably unnecessary, since Ty should reference RD
3483 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003484 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003485 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003486 return;
3487 }
3488
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003489 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003490
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003491 auto &GV = DeclCache[VD];
3492 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003493 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003494 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003495 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003496 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003497}
3498
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003499llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003500 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003501 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003502 llvm::DIScope *Mod = getParentModuleOrNull(D);
3503 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003504}
3505
David Blaikie9f88fe82013-04-22 06:13:21 +00003506void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003507 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003508 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003509 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3510 if (!NSDecl->isAnonymousNamespace() ||
3511 CGM.getCodeGenOpts().DebugExplicitImport) {
3512 DBuilder.createImportedModule(
3513 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3514 getOrCreateNameSpace(NSDecl),
3515 getLineNumber(UD.getLocation()));
3516 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003517}
3518
David Blaikiebd483762013-05-20 04:58:53 +00003519void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003520 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003521 return;
3522 assert(UD.shadow_size() &&
3523 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3524 // Emitting one decl is sufficient - debuggers can detect that this is an
3525 // overloaded name & provide lookup for all the overloads.
3526 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003527 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003528 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003529 DBuilder.createImportedDeclaration(
3530 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3531 getLineNumber(USD.getLocation()));
3532}
3533
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003534void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantl8a634c12015-12-18 19:44:31 +00003535 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003536 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003537 DBuilder.createImportedDeclaration(
3538 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3539 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3540 getLineNumber(ID.getLocation()));
3541 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003542}
3543
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003544llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003545CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003546 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003547 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003548 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003549 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003550 return cast<llvm::DIImportedEntity>(VH);
3551 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003552 if (const NamespaceAliasDecl *Underlying =
3553 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3554 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003555 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003556 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3557 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3558 NA.getName());
3559 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003560 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003561 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3562 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3563 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003564 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003565 return R;
3566}
3567
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003568llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003569CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003570 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003571 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003572 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003573 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003574
Guy Benyei11169dd2012-12-18 14:30:41 +00003575 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003576 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003577 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003578 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003579 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003580 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003581 return NS;
3582}
3583
Adrian Prantla5206ce2015-09-22 23:26:43 +00003584void CGDebugInfo::setDwoId(uint64_t Signature) {
3585 assert(TheCU && "no main compile unit");
3586 TheCU->setDWOId(Signature);
3587}
3588
3589
Guy Benyei11169dd2012-12-18 14:30:41 +00003590void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003591 // Creating types might create further types - invalidating the current
3592 // element and the size(), so don't cache/reference them.
3593 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3594 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003595 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003596 ? CreateTypeDefinition(E.Type, E.Unit)
3597 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003598 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003599 }
3600
David Blaikief427b002014-05-06 03:42:01 +00003601 for (auto p : ReplaceMap) {
3602 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003603 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003604 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003605
David Blaikief427b002014-05-06 03:42:01 +00003606 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003607 assert(it != TypeCache.end());
3608 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003609
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003610 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3611 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003612 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003613
Frederic Rissd253ed62014-11-18 03:40:51 +00003614 for (const auto &p : FwdDeclReplaceMap) {
3615 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003616 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003617 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003618
3619 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003620 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003621 // with ourselves, that will destroy the temporary MDNode and
3622 // replace it with a standard one, avoiding leaking memory.
3623 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003624 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003625 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003626 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003627
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003628 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003629 }
3630
Adrian Prantl73409ce2013-03-11 18:33:46 +00003631 // We keep our own list of retained types, because we need to look
3632 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003633 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003634 if (auto MD = TypeCache[RT])
3635 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003636
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 DBuilder.finalize();
3638}
David Blaikie66088d52014-09-24 17:01:27 +00003639
3640void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003641 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003642 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003643
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003644 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003645 // Don't ignore in case of explicit cast where it is referenced indirectly.
3646 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003647}