blob: 441135dc90d0ee5746a295b02a2cfc1fb918b1be [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
Reid Kleckner829398e2016-06-17 16:11:20 +0000187 if (!Info && FII)
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());
Reid Kleckner829398e2016-06-17 16:11:20 +0000194 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Guy Benyei11169dd2012-12-18 14:30:41 +0000195
Reid Kleckner829398e2016-06-17 16:11:20 +0000196 // Print the unqualified name with some template arguments.
197 FD->printName(OS);
198 // Add any template specialization args.
199 if (Info) {
200 const TemplateArgumentList *TArgs = Info->TemplateArguments;
201 const TemplateArgument *Args = TArgs->data();
202 unsigned NumArgs = TArgs->size();
203 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
204 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000205 }
206
207 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000208 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000209}
210
211StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
212 SmallString<256> MethodName;
213 llvm::raw_svector_ostream OS(MethodName);
214 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
215 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000216 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 dyn_cast<const ObjCImplementationDecl>(DC)) {
218 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000219 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000220 dyn_cast<const ObjCInterfaceDecl>(DC)) {
221 OS << OID->getName();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000222 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
223 if (OC->IsClassExtension()) {
224 OS << OC->getClassInterface()->getName();
225 } else {
226 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
227 << OC->getIdentifier()->getNameStart() << ')';
228 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000229 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000230 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
231 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
232 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000233 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000234 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000235 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000236 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000237 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000238 ClassTy.print(OS, PrintingPolicy(LangOptions()));
239 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000240 }
241 OS << ' ' << OMD->getSelector().getAsString() << ']';
242
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000243 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000244}
245
Guy Benyei11169dd2012-12-18 14:30:41 +0000246StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000247 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000248}
249
Eric Christophere7b87e52014-10-26 23:40:33 +0000250StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000251 // quick optimization to avoid having to intern strings that are already
252 // stored reliably elsewhere
253 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000254 return RD->getName();
255
David Blaikie65813a32014-04-02 18:21:09 +0000256 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000257 {
David Blaikie65813a32014-04-02 18:21:09 +0000258 llvm::raw_svector_ostream OS(Name);
259 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
260 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000261 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000262
263 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000264 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000265}
266
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000267llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000268 if (!Loc.isValid())
269 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000270 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
271 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000272
273 SourceManager &SM = CGM.getContext().getSourceManager();
274 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
275
276 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
277 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000278 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
279 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000280
281 // Cache the results.
282 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000283 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000284
285 if (it != DIFileCache.end()) {
286 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000287 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000288 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000289 }
290
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000291 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
292 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000293
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000294 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000295 return F;
296}
297
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000298llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000299 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
300 remapDIPath(TheCU->getDirectory()));
301}
302
303std::string CGDebugInfo::remapDIPath(StringRef Path) const {
304 for (const auto &Entry : DebugPrefixMap)
305 if (Path.startswith(Entry.first))
306 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
307 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000308}
309
Guy Benyei11169dd2012-12-18 14:30:41 +0000310unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
311 if (Loc.isInvalid() && CurLoc.isInvalid())
312 return 0;
313 SourceManager &SM = CGM.getContext().getSourceManager();
314 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000315 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000316}
317
Adrian Prantlc7822422013-03-12 20:43:25 +0000318unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000319 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000320 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000321 return 0;
322
323 // If the location is invalid then use the current column.
324 if (Loc.isInvalid() && CurLoc.isInvalid())
325 return 0;
326 SourceManager &SM = CGM.getContext().getSourceManager();
327 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000328 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000329}
330
331StringRef CGDebugInfo::getCurrentDirname() {
332 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
333 return CGM.getCodeGenOpts().DebugCompilationDir;
334
335 if (!CWDName.empty())
336 return CWDName;
337 SmallString<256> CWD;
338 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000339 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000340}
341
Guy Benyei11169dd2012-12-18 14:30:41 +0000342void CGDebugInfo::CreateCompileUnit() {
343
David Blaikieaabde052014-05-14 00:29:00 +0000344 // Should we be asking the SourceManager for the main file name, instead of
345 // accepting it as an argument? This just causes the main file name to
346 // mismatch with source locations and create extra lexical scopes or
347 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
348 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
349 // because that's what the SourceManager says)
350
Guy Benyei11169dd2012-12-18 14:30:41 +0000351 // Get absolute path name.
352 SourceManager &SM = CGM.getContext().getSourceManager();
353 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
354 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000355 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000356
357 // The main file name provided via the "-main-file-name" option contains just
358 // the file name itself with no path information. This file name may have had
359 // a relative path, so we look into the actual file entry for the main
360 // file to determine the real absolute path for the file.
361 std::string MainFileDir;
362 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000363 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000364 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000365 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
366 llvm::sys::path::append(MainFileDirSS, MainFileName);
367 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000368 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000369 }
370
Ed Masteda706022014-05-07 12:49:30 +0000371 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 const LangOptions &LO = CGM.getLangOpts();
373 if (LO.CPlusPlus) {
374 if (LO.ObjC1)
375 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
376 else
377 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
378 } else if (LO.ObjC1) {
379 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000380 } else if (LO.RenderScript) {
381 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000382 } else if (LO.C99) {
383 LangTag = llvm::dwarf::DW_LANG_C99;
384 } else {
385 LangTag = llvm::dwarf::DW_LANG_C89;
386 }
387
388 std::string Producer = getClangFullVersion();
389
390 // Figure out which version of the ObjC runtime we have.
391 unsigned RuntimeVers = 0;
392 if (LO.ObjC1)
393 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
394
Adrian Prantl826824e2016-04-08 22:43:06 +0000395 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
396 switch (DebugKind) {
397 case codegenoptions::NoDebugInfo:
398 case codegenoptions::LocTrackingOnly:
399 EmissionKind = llvm::DICompileUnit::NoDebug;
400 break;
401 case codegenoptions::DebugLineTablesOnly:
402 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
403 break;
404 case codegenoptions::LimitedDebugInfo:
405 case codegenoptions::FullDebugInfo:
406 EmissionKind = llvm::DICompileUnit::FullDebug;
407 break;
408 }
409
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000411 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000412 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000413 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
414 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantl826824e2016-04-08 22:43:06 +0000415 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei11169dd2012-12-18 14:30:41 +0000416}
417
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000418llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000419 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000420 StringRef BTName;
421 switch (BT->getKind()) {
422#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000423#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000424#include "clang/AST/BuiltinTypes.def"
425 case BuiltinType::Dependent:
426 llvm_unreachable("Unexpected builtin type");
427 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000428 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000430 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000432 if (!ClassTy)
433 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
434 "objc_class", TheCU,
435 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 return ClassTy;
437 case BuiltinType::ObjCId: {
438 // typedef struct objc_class *Class;
439 // typedef struct objc_object {
440 // Class isa;
441 // } *id;
442
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000443 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000444 return ObjTy;
445
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000446 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
448 "objc_class", TheCU,
449 getOrCreateMainFile(), 0);
450
451 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000452
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000453 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000454
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000455 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000456 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000457 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000458
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000459 DBuilder.replaceArrays(
460 ObjTy,
461 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
462 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 return ObjTy;
464 }
465 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000466 if (!SelTy)
467 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
468 "objc_selector", TheCU,
469 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000470 return SelTy;
471 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000472
Alexey Bader954ba212016-04-08 13:40:33 +0000473#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
474 case BuiltinType::Id: \
475 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
476 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000477#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000478 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000479 return DBuilder.createBasicType(
480 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
481 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000482 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000483 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000484 case BuiltinType::OCLClkEvent:
485 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
486 case BuiltinType::OCLQueue:
487 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
488 case BuiltinType::OCLNDRange:
489 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
490 case BuiltinType::OCLReserveID:
491 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000492
Guy Benyei11169dd2012-12-18 14:30:41 +0000493 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 case BuiltinType::Char_U:
495 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
496 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000498 case BuiltinType::SChar:
499 Encoding = llvm::dwarf::DW_ATE_signed_char;
500 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000501 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000502 case BuiltinType::Char32:
503 Encoding = llvm::dwarf::DW_ATE_UTF;
504 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 case BuiltinType::UShort:
506 case BuiltinType::UInt:
507 case BuiltinType::UInt128:
508 case BuiltinType::ULong:
509 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000510 case BuiltinType::ULongLong:
511 Encoding = llvm::dwarf::DW_ATE_unsigned;
512 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 case BuiltinType::Short:
514 case BuiltinType::Int:
515 case BuiltinType::Int128:
516 case BuiltinType::Long:
517 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000518 case BuiltinType::LongLong:
519 Encoding = llvm::dwarf::DW_ATE_signed;
520 break;
521 case BuiltinType::Bool:
522 Encoding = llvm::dwarf::DW_ATE_boolean;
523 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000524 case BuiltinType::Half:
525 case BuiltinType::Float:
526 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000527 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000528 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000529 // FIXME: For targets where long double and __float128 have the same size,
530 // they are currently indistinguishable in the debugger without some
531 // special treatment. However, there is currently no consensus on encoding
532 // and this should be updated once a DWARF encoding exists for distinct
533 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000534 Encoding = llvm::dwarf::DW_ATE_float;
535 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000536 }
537
538 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 case BuiltinType::Long:
540 BTName = "long int";
541 break;
542 case BuiltinType::LongLong:
543 BTName = "long long int";
544 break;
545 case BuiltinType::ULong:
546 BTName = "long unsigned int";
547 break;
548 case BuiltinType::ULongLong:
549 BTName = "long long unsigned int";
550 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 default:
552 BTName = BT->getName(CGM.getLangOpts());
553 break;
554 }
555 // Bit size, align and offset of the type.
556 uint64_t Size = CGM.getContext().getTypeSize(BT);
557 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000558 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000559}
560
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000561llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000563 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000564 if (Ty->isComplexIntegerType())
565 Encoding = llvm::dwarf::DW_ATE_lo_user;
566
567 uint64_t Size = CGM.getContext().getTypeSize(Ty);
568 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000569 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000570}
571
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000572llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
573 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000574 QualifierCollector Qc;
575 const Type *T = Qc.strip(Ty);
576
577 // Ignore these qualifiers for now.
578 Qc.removeObjCGCAttr();
579 Qc.removeAddressSpace();
580 Qc.removeObjCLifetime();
581
582 // We will create one Derived type for one qualifier and recurse to handle any
583 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000584 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 if (Qc.hasConst()) {
586 Tag = llvm::dwarf::DW_TAG_const_type;
587 Qc.removeConst();
588 } else if (Qc.hasVolatile()) {
589 Tag = llvm::dwarf::DW_TAG_volatile_type;
590 Qc.removeVolatile();
591 } else if (Qc.hasRestrict()) {
592 Tag = llvm::dwarf::DW_TAG_restrict_type;
593 Qc.removeRestrict();
594 } else {
595 assert(Qc.empty() && "Unknown type qualifier for debug info");
596 return getOrCreateType(QualType(T, 0), Unit);
597 }
598
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000599 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000600
601 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
602 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000603 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604}
605
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000606llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
607 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000608
609 // The frontend treats 'id' as a typedef to an ObjCObjectType,
610 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
611 // debug info, we want to emit 'id' in both cases.
612 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000613 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000614
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000615 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
616 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000617}
618
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000619llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
620 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000621 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000622 Ty->getPointeeType(), Unit);
623}
624
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000625/// \return whether a C++ mangling exists for the type defined by TD.
626static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
627 switch (TheCU->getSourceLanguage()) {
628 case llvm::dwarf::DW_LANG_C_plus_plus:
629 return true;
630 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
631 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
632 default:
633 return false;
634 }
635}
636
Manman Rene0064d82013-08-29 23:19:58 +0000637/// In C++ mode, types have linkage, so we can rely on the ODR and
638/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000639static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
640 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000641 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000642 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000643 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000644
645 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000646 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000647
Manman Rene0064d82013-08-29 23:19:58 +0000648 // TODO: This is using the RTTI name. Is there a better way to get
649 // a unique string for a type?
650 llvm::raw_svector_ostream Out(FullName);
651 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000652 return FullName;
653}
654
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000655/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000656static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
657 llvm::dwarf::Tag Tag;
658 if (RD->isStruct() || RD->isInterface())
659 Tag = llvm::dwarf::DW_TAG_structure_type;
660 else if (RD->isUnion())
661 Tag = llvm::dwarf::DW_TAG_union_type;
662 else {
663 // FIXME: This could be a struct type giving a default visibility different
664 // than C++ class type, but needs llvm metadata changes first.
665 assert(RD->isClass());
666 Tag = llvm::dwarf::DW_TAG_class_type;
667 }
668 return Tag;
669}
670
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000671llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000672CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000673 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000674 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000675 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
676 return cast<llvm::DICompositeType>(T);
677 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 unsigned Line = getLineNumber(RD->getLocation());
679 StringRef RDName = getClassName(RD);
680
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000681 uint64_t Size = 0;
682 uint64_t Align = 0;
683
684 const RecordDecl *D = RD->getDefinition();
685 if (D && D->isCompleteDefinition()) {
686 Size = CGM.getContext().getTypeSize(Ty);
687 Align = CGM.getContext().getTypeAlign(Ty);
688 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000689
690 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000691 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000693 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000694 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000695 ReplaceMap.emplace_back(
696 std::piecewise_construct, std::make_tuple(Ty),
697 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000698 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000699}
700
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000701llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000702 const Type *Ty,
703 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000704 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000705 // Bit size, align and offset of the type.
706 // Size is always the size of a pointer. We can't use getTypeSize here
707 // because that does not return the correct value for references.
708 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000709 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000710 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
711
Keno Fischer87842f32015-11-16 09:04:13 +0000712 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
713 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
714 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
715 Size, Align);
716 else
717 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
718 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000719}
720
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000721llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
722 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000723 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000724 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000725 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
726 TheCU, getOrCreateMainFile(), 0);
727 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
728 Cache = DBuilder.createPointerType(Cache, Size);
729 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000730}
731
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000732llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
733 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000734 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000735 QualType FType;
736 uint64_t FieldSize, FieldOffset;
737 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000738 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000739
740 FieldOffset = 0;
741 FType = CGM.getContext().UnsignedLongTy;
742 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
743 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
744
745 Elements = DBuilder.getOrCreateArray(EltTys);
746 EltTys.clear();
747
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000748 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000749 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000750
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000751 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000752 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000753 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000754
755 // Bit size, align and offset of the type.
756 uint64_t Size = CGM.getContext().getTypeSize(Ty);
757
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000758 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000759
760 FieldOffset = 0;
761 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
762 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
763 FType = CGM.getContext().IntTy;
764 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
765 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000766 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000767 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
768
769 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000770 FieldSize = CGM.getContext().getTypeSize(Ty);
771 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000772 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000773 FieldSize, FieldAlign, FieldOffset,
774 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000775
776 FieldOffset += FieldSize;
777 Elements = DBuilder.getOrCreateArray(EltTys);
778
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000779 // The __block_literal_generic structs are marked with a special
780 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
781 // the debugger needs to know about. To allow type uniquing, emit
782 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000783 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000784 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000785 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000786
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000787 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000788}
789
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000790llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
791 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000792 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000793 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000794
795 SmallString<128> NS;
796 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000797 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
798 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000799
800 TemplateSpecializationType::PrintTemplateArgumentList(
801 OS, Ty->getArgs(), Ty->getNumArgs(),
802 CGM.getContext().getPrintingPolicy());
803
Eric Christophere7b87e52014-10-26 23:40:33 +0000804 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
805 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000806
807 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000808 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
809 getLineNumber(Loc),
810 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000811}
812
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000813llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
814 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000815 // We don't set size information, but do specify where the typedef was
816 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000817 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000818
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000819 // Typedefs are derived from some other type.
820 return DBuilder.createTypedef(
821 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
822 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000823 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000824}
825
Reid Klecknerf00f8032016-06-08 20:41:54 +0000826static unsigned getDwarfCC(CallingConv CC) {
827 switch (CC) {
828 case CC_C:
829 // Avoid emitting DW_AT_calling_convention if the C convention was used.
830 return 0;
831
832 case CC_X86StdCall:
833 return llvm::dwarf::DW_CC_BORLAND_stdcall;
834 case CC_X86FastCall:
835 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
836 case CC_X86ThisCall:
837 return llvm::dwarf::DW_CC_BORLAND_thiscall;
838 case CC_X86VectorCall:
839 return llvm::dwarf::DW_CC_LLVM_vectorcall;
840 case CC_X86Pascal:
841 return llvm::dwarf::DW_CC_BORLAND_pascal;
842
843 // FIXME: Create new DW_CC_ codes for these calling conventions.
844 case CC_X86_64Win64:
845 case CC_X86_64SysV:
846 case CC_AAPCS:
847 case CC_AAPCS_VFP:
848 case CC_IntelOclBicc:
849 case CC_SpirFunction:
850 case CC_SpirKernel:
851 case CC_Swift:
852 case CC_PreserveMost:
853 case CC_PreserveAll:
854 return 0;
855 }
856 return 0;
857}
858
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000859llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
860 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000861 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000862
863 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000864 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000865
866 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000867 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000868 if (isa<FunctionNoProtoType>(Ty))
869 EltTys.push_back(DBuilder.createUnspecifiedParameter());
870 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000871 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
872 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000873 if (FPT->isVariadic())
874 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000875 }
876
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +0000878 return DBuilder.createSubroutineType(EltTypeArray, 0,
879 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000880}
881
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000882/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000883/// As an optimization, return 0 if the access specifier equals the
884/// default for the containing type.
885static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
886 AccessSpecifier Default = clang::AS_none;
887 if (RD && RD->isClass())
888 Default = clang::AS_private;
889 else if (RD && (RD->isStruct() || RD->isUnion()))
890 Default = clang::AS_public;
891
892 if (Access == Default)
893 return 0;
894
Eric Christophere7b87e52014-10-26 23:40:33 +0000895 switch (Access) {
896 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000897 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000898 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000899 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000900 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000901 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000902 case clang::AS_none:
903 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000904 }
905 llvm_unreachable("unexpected access enumerator");
906}
Guy Benyei11169dd2012-12-18 14:30:41 +0000907
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000909 StringRef name, QualType type, uint64_t sizeInBitsOverride,
910 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000911 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
912 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000913
914 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000915 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000916 unsigned line = getLineNumber(loc);
917
David Majnemer34b57492014-07-30 01:30:47 +0000918 uint64_t SizeInBits = 0;
919 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000920 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000921 TypeInfo TI = CGM.getContext().getTypeInfo(type);
922 SizeInBits = TI.Width;
923 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000924
925 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000926 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000927 }
928
Adrian Prantl21361fb2014-08-29 22:44:27 +0000929 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000930 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
931 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000932}
933
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000934void CGDebugInfo::CollectRecordLambdaFields(
935 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000936 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000937 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
938 // has the name and the location of the variable so we should iterate over
939 // both concurrently.
940 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
941 RecordDecl::field_iterator Field = CXXDecl->field_begin();
942 unsigned fieldno = 0;
943 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000944 E = CXXDecl->captures_end();
945 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000946 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000947 if (C.capturesVariable()) {
948 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000949 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000950 StringRef VName = V->getName();
951 uint64_t SizeInBitsOverride = 0;
952 if (Field->isBitField()) {
953 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
954 assert(SizeInBitsOverride && "found named 0-width bitfield");
955 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000956 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000957 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
958 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
959 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000960 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000961 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000962 // TODO: Need to handle 'this' in some way by probably renaming the
963 // this of the lambda class and having a field member of 'this' or
964 // by using AT_object_pointer for the function and having that be
965 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000966 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000967 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000968 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000969 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000970 "this", type, 0, f->getLocation(), f->getAccess(),
971 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000972
973 elements.push_back(fieldType);
974 }
975 }
976}
977
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000978llvm::DIDerivedType *
979CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000980 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000981 // Create the descriptor for the static variable, with or without
982 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000983 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000984 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
985 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000986
Eric Christopher91a31902013-01-16 01:22:32 +0000987 unsigned LineNumber = getLineNumber(Var->getLocation());
988 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000989 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000990 if (Var->getInit()) {
991 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000992 if (Value) {
993 if (Value->isInt())
994 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
995 if (Value->isFloat())
996 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
997 }
Eric Christopher91a31902013-01-16 01:22:32 +0000998 }
999
Adrian Prantl21361fb2014-08-29 22:44:27 +00001000 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001001 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +00001002 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001003 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001004 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001005}
1006
Eric Christophere7b87e52014-10-26 23:40:33 +00001007void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001008 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1009 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001010 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001011 StringRef name = field->getName();
1012 QualType type = field->getType();
1013
1014 // Ignore unnamed fields unless they're anonymous structs/unions.
1015 if (name.empty() && !type->isRecordType())
1016 return;
1017
1018 uint64_t SizeInBitsOverride = 0;
1019 if (field->isBitField()) {
1020 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
1021 assert(SizeInBitsOverride && "found named 0-width bitfield");
1022 }
1023
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001024 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001025 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
1026 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001027
1028 elements.push_back(fieldType);
1029}
1030
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001031void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001032 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001033 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001034 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001035 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1036
Eric Christopher91a31902013-01-16 01:22:32 +00001037 if (CXXDecl && CXXDecl->isLambda())
1038 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1039 else {
1040 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001041
Eric Christopher91a31902013-01-16 01:22:32 +00001042 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001043 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001044
Eric Christopher91a31902013-01-16 01:22:32 +00001045 // Static and non-static members should appear in the same order as
1046 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001047 for (const auto *I : record->decls())
1048 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001049 if (V->hasAttr<NoDebugAttr>())
1050 continue;
David Blaikiece763042013-08-20 21:49:21 +00001051 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001052 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001053 if (MI != StaticDataMemberCache.end()) {
1054 assert(MI->second &&
1055 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001056 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001057 } else {
1058 auto Field = CreateRecordStaticField(V, RecordTy, record);
1059 elements.push_back(Field);
1060 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001061 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001062 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1063 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001064
1065 // Bump field number for next field.
1066 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001067 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001068 }
1069}
1070
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001071llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001072CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001073 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001074 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001075 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001076 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001077 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001078 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1079 Func, Unit);
1080}
David Blaikie2aaf0652013-01-07 22:24:59 +00001081
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001082llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1083 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001084 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001085 llvm::DITypeRefArray Args(
1086 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001087 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001088 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001089
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001090 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001091
1092 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001093 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001094
David Blaikie2aaf0652013-01-07 22:24:59 +00001095 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001096 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001097 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1098 // Create pointer type directly in this case.
1099 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1100 QualType PointeeTy = ThisPtrTy->getPointeeType();
1101 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001102 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001103 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1105 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001106 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001107 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001108 // TODO: This and the artificial type below are misleading, the
1109 // types aren't artificial the argument is, but the current
1110 // metadata doesn't represent that.
1111 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1112 Elts.push_back(ThisPtrType);
1113 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001114 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001115 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001116 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1117 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 }
1119
1120 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001121 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1122 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001123
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001124 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001125
Adrian Prantl0630eb72013-12-18 21:48:18 +00001126 unsigned Flags = 0;
1127 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001128 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001129 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001130 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001131
Reid Klecknerf00f8032016-06-08 20:41:54 +00001132 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1133 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001134}
1135
Eric Christopherb2a008c2013-05-16 00:45:12 +00001136/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001137/// inside a function.
1138static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1139 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1140 return isFunctionLocalClass(NRD);
1141 if (isa<FunctionDecl>(RD->getDeclContext()))
1142 return true;
1143 return false;
1144}
1145
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001146llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1147 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001149 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001150
Guy Benyei11169dd2012-12-18 14:30:41 +00001151 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001152 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001153
1154 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1155 // make sense to give a single ctor/dtor a linkage name.
1156 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001157 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1158 // property to use here. It may've been intended to model "is non-external
1159 // type" but misses cases of non-function-local but non-external classes such
1160 // as those in anonymous namespaces as well as the reverse - external types
1161 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001162 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1163 MethodLinkageName = CGM.getMangledName(Method);
1164
1165 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001166 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001167 unsigned MethodLine = 0;
1168 if (!Method->isImplicit()) {
1169 MethodDefUnit = getOrCreateFile(Method->getLocation());
1170 MethodLine = getLineNumber(Method->getLocation());
1171 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001172
1173 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001175 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001176 unsigned VIndex = 0;
Reid Klecknerfb727182016-06-17 22:27:59 +00001177 unsigned Flags = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001178
Guy Benyei11169dd2012-12-18 14:30:41 +00001179 if (Method->isVirtual()) {
1180 if (Method->isPure())
1181 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1182 else
1183 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001184
Reid Kleckner216d0a12016-06-16 20:08:51 +00001185 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1186 // It doesn't make sense to give a virtual destructor a vtable index,
1187 // since a single destructor has two entries in the vtable.
1188 if (!isa<CXXDestructorDecl>(Method))
1189 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1190 } else {
1191 // Emit MS ABI vftable information. There is only one entry for the
1192 // deleting dtor.
1193 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1194 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1195 MicrosoftVTableContext::MethodVFTableLocation ML =
1196 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1197 VIndex = ML.Index;
1198 // FIXME: Pass down ML.VFPtrOffset and ML.VBTableIndex. The debugger needs
1199 // these to synthesize a call to a virtual method in a complex inheritance
1200 // hierarchy.
1201 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001202 ContainingType = RecordTy;
1203 }
1204
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001206 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001207 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001208 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1209 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001210 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001211 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001212 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001213 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 }
1216 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001218 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001219 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001220 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001221 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001222
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001223 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1224 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001225 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1226 MethodTy, /*isLocalToUnit=*/false,
1227 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001228 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001229
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001230 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001231
1232 return SP;
1233}
1234
Eric Christophere7b87e52014-10-26 23:40:33 +00001235void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1237 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001238
1239 // Since we want more than just the individual member decls if we
1240 // have templated functions iterate over every declaration to gather
1241 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001242 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001243 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1244 // If the member is implicit, don't add it to the member list. This avoids
1245 // the member being added to type units by LLVM, while still allowing it
1246 // to be emitted into the type declaration/reference inside the compile
1247 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001248 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001249 // FIXME: Handle Using(Shadow?)Decls here to create
1250 // DW_TAG_imported_declarations inside the class for base decls brought into
1251 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1252 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1253 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001254 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001255 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001256
1257 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1258 continue;
1259
David Blaikiefd580722014-10-06 05:18:55 +00001260 // Reuse the existing member function declaration if it exists.
1261 // It may be associated with the declaration of the type & should be
1262 // reused as we're building the definition.
1263 //
1264 // This situation can arise in the vtable-based debug info reduction where
1265 // implicit members are emitted in a non-vtable TU.
1266 auto MI = SPCache.find(Method->getCanonicalDecl());
1267 EltTys.push_back(MI == SPCache.end()
1268 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001269 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001270 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001271}
Guy Benyei11169dd2012-12-18 14:30:41 +00001272
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001273void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001274 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001275 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001277 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001278 unsigned BFlags = 0;
1279 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001280
Guy Benyei11169dd2012-12-18 14:30:41 +00001281 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001282 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001283
Aaron Ballman574705e2014-03-13 15:41:46 +00001284 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001285 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1286 // virtual base offset offset is -ve. The code generator emits dwarf
1287 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001288 BaseOffset = 0 - CGM.getItaniumVTableContext()
1289 .getVirtualBaseOffsetOffset(RD, Base)
1290 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001291 } else {
1292 // In the MS ABI, store the vbtable offset, which is analogous to the
1293 // vbase offset offset in Itanium.
1294 BaseOffset =
1295 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1296 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001297 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001298 } else
1299 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1300 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1301 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001302
Adrian Prantl21361fb2014-08-29 22:44:27 +00001303 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001304 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001305 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 EltTys.push_back(DTy);
1307 }
1308}
1309
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001310llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001311CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1312 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001313 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001314 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001315 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1316 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001317 StringRef Name;
1318 if (TPList)
1319 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001320 switch (TA.getKind()) {
1321 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001322 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001323 TemplateParams.push_back(
1324 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001325 } break;
1326 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001327 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001328 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1329 TheCU, Name, TTy,
1330 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001331 } break;
1332 case TemplateArgument::Declaration: {
1333 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001334 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001335 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001336 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001337 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001338 // Variable pointer template parameters have a value that is the address
1339 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001340 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001341 V = CGM.GetAddrOfGlobalVar(VD);
1342 // Member function pointers have special support for building them, though
1343 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001344 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001345 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001346 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001347 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001348 // Member data pointers have special handling too to compute the fixed
1349 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001350 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001351 // These five lines (& possibly the above member function pointer
1352 // handling) might be able to be refactored to use similar code in
1353 // CodeGenModule::getMemberPointerConstant
1354 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1355 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001356 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001357 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001358 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001359 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1360 TheCU, Name, TTy,
1361 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001362 } break;
1363 case TemplateArgument::NullPtr: {
1364 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001366 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001367 // Special case member data pointer null values since they're actually -1
1368 // instead of zero.
1369 if (const MemberPointerType *MPT =
1370 dyn_cast<MemberPointerType>(T.getTypePtr()))
1371 // But treat member function pointers as simple zero integers because
1372 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1373 // CodeGen grows handling for values of non-null member function
1374 // pointers then perhaps we could remove this special case and rely on
1375 // EmitNullMemberPointer for member function pointers.
1376 if (MPT->isMemberDataPointer())
1377 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1378 if (!V)
1379 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001380 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1381 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001382 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001383 case TemplateArgument::Template:
1384 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001385 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001386 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1387 break;
1388 case TemplateArgument::Pack:
1389 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1390 TheCU, Name, nullptr,
1391 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1392 break;
David Majnemer5559d472013-08-24 08:21:10 +00001393 case TemplateArgument::Expression: {
1394 const Expr *E = TA.getAsExpr();
1395 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001396 if (E->isGLValue())
1397 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001398 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001399 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001400 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001401 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1402 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001403 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001404 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001405 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001406 case TemplateArgument::Null:
1407 llvm_unreachable(
1408 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 }
1410 }
1411 return DBuilder.getOrCreateArray(TemplateParams);
1412}
1413
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001415CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001416 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 if (FD->getTemplatedKind() ==
1418 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001419 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1420 ->getTemplate()
1421 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001422 return CollectTemplateParams(
1423 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001424 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001425 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001426}
1427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001428llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1429 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001430 // Always get the full list of parameters, not just the ones from
1431 // the specialization.
1432 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001433 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001434 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001435 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436}
1437
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001438llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001439 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 return VTablePtrType;
1441
1442 ASTContext &Context = CGM.getContext();
1443
1444 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001445 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001446 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001447 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001448 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001449 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001450 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001451 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1452 return VTablePtrType;
1453}
1454
Guy Benyei11169dd2012-12-18 14:30:41 +00001455StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001456 // Copy the gdb compatible name on the side and use its reference.
1457 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001458}
1459
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001460void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001461 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001462 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1463
1464 // If there is a primary base then it will hold vtable info.
1465 if (RL.getPrimaryBase())
1466 return;
1467
1468 // If this class is not dynamic then there is not any vtable info to collect.
1469 if (!RD->isDynamicClass())
1470 return;
1471
1472 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001473 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001474 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001475 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001476 EltTys.push_back(VPTR);
1477}
1478
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001479llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001480 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001481 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001482 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001483 return T;
1484}
1485
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001486llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001487 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001488 return getOrCreateStandaloneType(D, Loc);
1489}
1490
1491llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1492 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001493 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001494 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001495 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001496 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001497
Adrian Prantl73409ce2013-03-11 18:33:46 +00001498 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001499 return T;
1500}
1501
David Blaikie483a9da2014-05-06 18:35:21 +00001502void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001503 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001504 return;
1505 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001506 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001507 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001509 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001510 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001511 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001512 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001513}
1514
David Blaikieb2e86eb2013-08-15 20:49:17 +00001515void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001516 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001517 !CGM.getLangOpts().CPlusPlus)
1518 completeRequiredType(RD);
1519}
1520
1521void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001522 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001523 return;
1524
David Blaikie6943dea2013-08-20 01:28:15 +00001525 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1526 if (CXXDecl->isDynamicClass())
1527 return;
1528
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001529 if (DebugTypeExtRefs && RD->isFromASTFile())
1530 return;
1531
David Blaikieb2e86eb2013-08-15 20:49:17 +00001532 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001533 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001534 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001535 completeClassData(RD);
1536}
1537
1538void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001539 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001540 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001541 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001542 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001543 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001545 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001546 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001547 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001548 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001549}
1550
David Blaikie0e716b42014-03-03 23:48:23 +00001551static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1552 CXXRecordDecl::method_iterator End) {
1553 for (; I != End; ++I)
1554 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001555 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1556 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001557 return true;
1558 return false;
1559}
1560
Adrian Prantl05fefa42016-04-25 20:52:40 +00001561/// Does a type definition exist in an imported clang module?
1562static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001563 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001564 return false;
1565 if (!RD->isExternallyVisible() && RD->getName().empty())
1566 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001567 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1568 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001569 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1570 // Make sure the instantiation is actually in a module.
1571 if (CXXDecl->field_begin() != CXXDecl->field_end())
1572 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001573 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001574
Adrian Prantl05fefa42016-04-25 20:52:40 +00001575 return true;
1576}
1577
Benjamin Kramer8c305922016-02-02 11:06:51 +00001578static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1579 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001580 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001581 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001582 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001583
Benjamin Kramer8c305922016-02-02 11:06:51 +00001584 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001585 return false;
1586
1587 if (!LangOpts.CPlusPlus)
1588 return false;
1589
1590 if (!RD->isCompleteDefinitionRequired())
1591 return true;
1592
1593 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1594
1595 if (!CXXDecl)
1596 return false;
1597
1598 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1599 return true;
1600
1601 TemplateSpecializationKind Spec = TSK_Undeclared;
1602 if (const ClassTemplateSpecializationDecl *SD =
1603 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1604 Spec = SD->getSpecializationKind();
1605
1606 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1607 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1608 CXXDecl->method_end()))
1609 return true;
1610
1611 return false;
1612}
1613
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001614llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001616 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001617 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1618 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001619 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001620 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001621 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001622 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001623
David Blaikieb2e86eb2013-08-15 20:49:17 +00001624 return CreateTypeDefinition(Ty);
1625}
1626
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001627llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001628 RecordDecl *RD = Ty->getDecl();
1629
Guy Benyei11169dd2012-12-18 14:30:41 +00001630 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001631 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001632
1633 // Records and classes and unions can all be recursive. To handle them, we
1634 // first generate a debug descriptor for the struct as a forward declaration.
1635 // Then (if it is a definition) we go through and get debug info for all of
1636 // its members. Finally, we create a descriptor for the complete type (which
1637 // may refer to the forward decl if the struct is recursive) and replace all
1638 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001639 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001640
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001641 const RecordDecl *D = RD->getDefinition();
1642 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 return FwdDecl;
1644
David Blaikieadfbf992013-08-18 16:55:33 +00001645 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1646 CollectContainingType(CXXDecl, FwdDecl);
1647
Guy Benyei11169dd2012-12-18 14:30:41 +00001648 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001649 LexicalBlockStack.emplace_back(&*FwdDecl);
1650 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001651
Guy Benyei11169dd2012-12-18 14:30:41 +00001652 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001653 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001654 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001655
1656 // Note: The split of CXXDecl information here is intentional, the
1657 // gdb tests will depend on a certain ordering at printout. The debug
1658 // information offsets are still correct if we merge them all together
1659 // though.
1660 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1661 if (CXXDecl) {
1662 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1663 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1664 }
1665
Eric Christopher91a31902013-01-16 01:22:32 +00001666 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001668 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001669 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001670
1671 LexicalBlockStack.pop_back();
1672 RegionMap.erase(Ty->getDecl());
1673
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001674 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001675 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001676
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001677 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001678 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001680
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001681 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001682 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001683}
1684
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001685llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1686 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 // Ignore protocols.
1688 return getOrCreateType(Ty->getBaseType(), Unit);
1689}
1690
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001691/// \return true if Getter has the default name for the property PD.
1692static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1693 const ObjCMethodDecl *Getter) {
1694 assert(PD);
1695 if (!Getter)
1696 return true;
1697
1698 assert(Getter->getDeclName().isObjCZeroArgSelector());
1699 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001700 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001701}
1702
1703/// \return true if Setter has the default name for the property PD.
1704static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1705 const ObjCMethodDecl *Setter) {
1706 assert(PD);
1707 if (!Setter)
1708 return true;
1709
1710 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001711 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001712 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001713}
1714
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001715llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1716 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 ObjCInterfaceDecl *ID = Ty->getDecl();
1718 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001719 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001720
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001721 // Return a forward declaration if this type was imported from a clang module,
1722 // and this is not the compile unit with the implementation of the type (which
1723 // may contain hidden ivars).
1724 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1725 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001726 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1727 ID->getName(),
1728 getDeclContextDescriptor(ID), Unit, 0);
1729
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001731 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001732 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001733 auto RuntimeLang =
1734 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001735
1736 // If this is just a forward declaration return a special forward-declaration
1737 // debug type since we won't be able to lay out the entire type.
1738 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001739 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001740 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001741 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001742 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1743 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001744 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 return FwdDecl;
1746 }
1747
David Blaikieef8a9512014-05-05 23:23:53 +00001748 return CreateTypeDefinition(Ty, Unit);
1749}
1750
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001751llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001752CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1753 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001754 // Use the Module pointer as the key into the cache. This is a
1755 // nullptr if the "Module" is a PCH, which is safe because we don't
1756 // support chained PCH debug info, so there can only be a single PCH.
1757 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001758 auto ModRef = ModuleCache.find(M);
1759 if (ModRef != ModuleCache.end())
1760 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001761
1762 // Macro definitions that were defined with "-D" on the command line.
1763 SmallString<128> ConfigMacros;
1764 {
1765 llvm::raw_svector_ostream OS(ConfigMacros);
1766 const auto &PPOpts = CGM.getPreprocessorOpts();
1767 unsigned I = 0;
1768 // Translate the macro definitions back into a commmand line.
1769 for (auto &M : PPOpts.Macros) {
1770 if (++I > 1)
1771 OS << " ";
1772 const std::string &Macro = M.first;
1773 bool Undef = M.second;
1774 OS << "\"-" << (Undef ? 'U' : 'D');
1775 for (char c : Macro)
1776 switch (c) {
1777 case '\\' : OS << "\\\\"; break;
1778 case '"' : OS << "\\\""; break;
1779 default: OS << c;
1780 }
1781 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001782 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001783 }
Adrian Prantl66689202015-09-18 23:01:45 +00001784
Adrian Prantl835e6632015-09-24 16:10:10 +00001785 bool IsRootModule = M ? !M->Parent : true;
1786 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001787 // PCH files don't have a signature field in the control block,
1788 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001789 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001790 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001791 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001792 Mod.getPath(), TheCU->getProducer(), true,
1793 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001794 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001795 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001796 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001797 llvm::DIModule *Parent =
1798 IsRootModule ? nullptr
1799 : getOrCreateModuleRef(
1800 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1801 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001802 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001803 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1804 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001805 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001806 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001807}
1808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001809llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1810 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001811 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001812 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001813 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001814 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001815
1816 // Bit size, align and offset of the type.
1817 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1818 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1819
1820 unsigned Flags = 0;
1821 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001822 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001823
Adrian Prantlfd696112015-10-01 00:48:51 +00001824 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001825 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001826 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1827 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001828
David Blaikieef8a9512014-05-05 23:23:53 +00001829 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001830 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001831
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001832 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001833 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001834 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001835
1836 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001837 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001838
1839 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1840 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001841 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001842 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001843 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001844 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001845
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001846 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001847 EltTys.push_back(InhTag);
1848 }
1849
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001850 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001851 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001853 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001854 unsigned PLine = getLineNumber(Loc);
1855 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1856 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001857 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1858 PD->getName(), PUnit, PLine,
1859 hasDefaultGetterName(PD, Getter) ? ""
1860 : getSelectorName(PD->getGetterName()),
1861 hasDefaultSetterName(PD, Setter) ? ""
1862 : getSelectorName(PD->getSetterName()),
1863 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001865 };
1866 {
1867 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001868 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001869 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001870 PropertySet.insert(PD->getIdentifier());
1871 AddProperty(PD);
1872 }
Manman Renefe1bac2016-01-27 20:00:32 +00001873 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001874 // Don't emit duplicate metadata for properties that were already in a
1875 // class extension.
1876 if (!PropertySet.insert(PD->getIdentifier()).second)
1877 continue;
1878 AddProperty(PD);
1879 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 }
1881
1882 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1883 unsigned FieldNo = 0;
1884 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1885 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001886 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001887 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001888 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001889
Guy Benyei11169dd2012-12-18 14:30:41 +00001890 StringRef FieldName = Field->getName();
1891
1892 // Ignore unnamed fields.
1893 if (FieldName.empty())
1894 continue;
1895
1896 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001897 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 unsigned FieldLine = getLineNumber(Field->getLocation());
1899 QualType FType = Field->getType();
1900 uint64_t FieldSize = 0;
1901 unsigned FieldAlign = 0;
1902
1903 if (!FType->isIncompleteArrayType()) {
1904
1905 // Bit size, align and offset of the type.
1906 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001907 ? Field->getBitWidthValue(CGM.getContext())
1908 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 FieldAlign = CGM.getContext().getTypeAlign(FType);
1910 }
1911
1912 uint64_t FieldOffset;
1913 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1914 // We don't know the runtime offset of an ivar if we're using the
1915 // non-fragile ABI. For bitfields, use the bit offset into the first
1916 // byte of storage of the bitfield. For other fields, use zero.
1917 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001918 FieldOffset =
1919 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 FieldOffset %= CGM.getContext().getCharWidth();
1921 } else {
1922 FieldOffset = 0;
1923 }
1924 } else {
1925 FieldOffset = RL.getFieldOffset(FieldNo);
1926 }
1927
1928 unsigned Flags = 0;
1929 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001932 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001933 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001934 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001935
Craig Topper8a13c412014-05-21 05:09:00 +00001936 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001938 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001939 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001940 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001941 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001942 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001943 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001944 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1945 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001946 PropertyNode = DBuilder.createObjCProperty(
1947 PD->getName(), PUnit, PLine,
1948 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1949 PD->getGetterName()),
1950 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1951 PD->getSetterName()),
1952 PD->getPropertyAttributes(),
1953 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001954 }
1955 }
1956 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001957 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1958 FieldSize, FieldAlign, FieldOffset, Flags,
1959 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001960 EltTys.push_back(FieldTy);
1961 }
1962
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001963 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001964 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001965
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001967 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001968}
1969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001970llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1971 llvm::DIFile *Unit) {
1972 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001973 int64_t Count = Ty->getNumElements();
1974 if (Count == 0)
1975 // If number of elements are not known then this is an unbounded array.
1976 // Use Count == -1 to express such arrays.
1977 Count = -1;
1978
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001979 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001980 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001981
1982 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1983 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1984
1985 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1986}
1987
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001988llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 uint64_t Size;
1990 uint64_t Align;
1991
1992 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1993 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1994 Size = 0;
1995 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001996 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 } else if (Ty->isIncompleteArrayType()) {
1998 Size = 0;
1999 if (Ty->getElementType()->isIncompleteType())
2000 Align = 0;
2001 else
2002 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00002003 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 Size = 0;
2005 Align = 0;
2006 } else {
2007 // Size and align of the whole array, not the element type.
2008 Size = CGM.getContext().getTypeSize(Ty);
2009 Align = CGM.getContext().getTypeAlign(Ty);
2010 }
2011
2012 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2013 // interior arrays, do we care? Why aren't nested arrays represented the
2014 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002015 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 QualType EltTy(Ty, 0);
2017 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2018 // If the number of elements is known, then count is that number. Otherwise,
2019 // it's -1. This allows us to represent a subrange with an array of 0
2020 // elements, like this:
2021 //
2022 // struct foo {
2023 // int x[0];
2024 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002025 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2027 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002028
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 // FIXME: Verify this is right for VLAs.
2030 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2031 EltTy = Ty->getElementType();
2032 }
2033
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002034 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002035
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002036 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2037 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002038}
2039
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002040llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2041 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002042 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2043 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002044}
2045
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002046llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2047 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002048 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2049 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002050}
2051
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002052llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2053 llvm::DIFile *U) {
Reid Klecknerfb727182016-06-17 22:27:59 +00002054 unsigned Flags = 0;
2055 uint64_t Size = 0;
2056
2057 if (!Ty->isIncompleteType()) {
2058 Size = CGM.getContext().getTypeSize(Ty);
2059
2060 // Set the MS inheritance model. There is no flag for the unspecified model.
2061 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2062 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2063 case MSInheritanceAttr::Keyword_single_inheritance:
2064 Flags |= llvm::DINode::FlagSingleInheritance;
2065 break;
2066 case MSInheritanceAttr::Keyword_multiple_inheritance:
2067 Flags |= llvm::DINode::FlagMultipleInheritance;
2068 break;
2069 case MSInheritanceAttr::Keyword_virtual_inheritance:
2070 Flags |= llvm::DINode::FlagVirtualInheritance;
2071 break;
2072 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2073 break;
2074 }
2075 }
2076 }
2077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002078 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002079 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002080 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002081 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2082 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002083
2084 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002085 Ty->getPointeeType()->getAs<FunctionProtoType>();
2086 return DBuilder.createMemberPointerType(
2087 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2088 Ty->getClass(), FPT->getTypeQuals())),
2089 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002090 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002091}
2092
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002093llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 // Ignore the atomic wrapping
2095 // FIXME: What is the correct representation?
2096 return getOrCreateType(Ty->getValueType(), U);
2097}
2098
Xiuli Pan9c14e282016-01-09 12:53:17 +00002099llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2100 llvm::DIFile *U) {
2101 return getOrCreateType(Ty->getElementType(), U);
2102}
2103
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002104llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002105 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002106
Guy Benyei11169dd2012-12-18 14:30:41 +00002107 uint64_t Size = 0;
2108 uint64_t Align = 0;
2109 if (!ED->getTypeForDecl()->isIncompleteType()) {
2110 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2111 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2112 }
2113
Manman Rene0064d82013-08-29 23:19:58 +00002114 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2115
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002116 bool isImportedFromModule =
2117 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2118
Guy Benyei11169dd2012-12-18 14:30:41 +00002119 // If this is just a forward declaration, construct an appropriately
2120 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002121 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002122 // Note that it is possible for enums to be created as part of
2123 // their own declcontext. In this case a FwdDecl will be created
2124 // twice. This doesn't cause a problem because both FwdDecls are
2125 // entered into the ReplaceMap: finalize() will replace the first
2126 // FwdDecl with the second and then replace the second with
2127 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002128 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002129 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002130 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2131 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002132
Guy Benyei11169dd2012-12-18 14:30:41 +00002133 unsigned Line = getLineNumber(ED->getLocation());
2134 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002135 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002136 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2137 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002138
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002139 ReplaceMap.emplace_back(
2140 std::piecewise_construct, std::make_tuple(Ty),
2141 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002142 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 }
2144
David Blaikie483a9da2014-05-06 18:35:21 +00002145 return CreateTypeDefinition(Ty);
2146}
2147
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002148llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002149 const EnumDecl *ED = Ty->getDecl();
2150 uint64_t Size = 0;
2151 uint64_t Align = 0;
2152 if (!ED->getTypeForDecl()->isIncompleteType()) {
2153 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2154 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2155 }
2156
2157 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2158
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002159 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002160 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002161 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002162 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002163 Enumerators.push_back(DBuilder.createEnumerator(
2164 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 }
2166
2167 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002168 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002169
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002170 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002172 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002173 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002174 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2175 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2176 Line, Size, Align, EltArray, ClassTy,
2177 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002178}
2179
David Blaikie05491062013-01-21 04:37:12 +00002180static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2181 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002183 Qualifiers InnerQuals = T.getLocalQualifiers();
2184 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2185 // that is already there.
2186 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2187 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 QualType LastT = T;
2189 switch (T->getTypeClass()) {
2190 default:
David Blaikie05491062013-01-21 04:37:12 +00002191 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002192 case Type::TemplateSpecialization: {
2193 const auto *Spec = cast<TemplateSpecializationType>(T);
2194 if (Spec->isTypeAlias())
2195 return C.getQualifiedType(T.getTypePtr(), Quals);
2196 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002197 break;
2198 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 case Type::TypeOfExpr:
2200 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2201 break;
2202 case Type::TypeOf:
2203 T = cast<TypeOfType>(T)->getUnderlyingType();
2204 break;
2205 case Type::Decltype:
2206 T = cast<DecltypeType>(T)->getUnderlyingType();
2207 break;
2208 case Type::UnaryTransform:
2209 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2210 break;
2211 case Type::Attributed:
2212 T = cast<AttributedType>(T)->getEquivalentType();
2213 break;
2214 case Type::Elaborated:
2215 T = cast<ElaboratedType>(T)->getNamedType();
2216 break;
2217 case Type::Paren:
2218 T = cast<ParenType>(T)->getInnerType();
2219 break;
David Blaikie05491062013-01-21 04:37:12 +00002220 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 break;
2223 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002224 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002225 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002226 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 break;
2228 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002229
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002231 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 } while (true);
2233}
2234
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002235llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002236
2237 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002238 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002239
David Blaikief427b002014-05-06 03:42:01 +00002240 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 if (it != TypeCache.end()) {
2242 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002243 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002244 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 }
2246
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002247 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002248}
2249
David Blaikie0e716b42014-03-03 23:48:23 +00002250void CGDebugInfo::completeTemplateDefinition(
2251 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002252 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002253 return;
2254
David Blaikie0e716b42014-03-03 23:48:23 +00002255 completeClassData(&SD);
2256 // In case this type has no member function definitions being emitted, ensure
2257 // it is retained
2258 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2259}
2260
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002261llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002263 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002264
2265 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002266 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002267
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002268 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002269 return T;
2270
Adrian Prantlca844182015-09-11 17:23:03 +00002271 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002272 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002273
2274 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002275 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002276
Guy Benyei11169dd2012-12-18 14:30:41 +00002277 return Res;
2278}
2279
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002280llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002281 // A forward declaration inside a module header does not belong to the module.
2282 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2283 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002284 if (DebugTypeExtRefs && D->isFromASTFile()) {
2285 // Record a reference to an imported clang module or precompiled header.
2286 auto *Reader = CGM.getContext().getExternalSource();
2287 auto Idx = D->getOwningModuleID();
2288 auto Info = Reader->getSourceDescriptor(Idx);
2289 if (Info)
2290 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2291 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002292 // We are building a clang module or a precompiled header.
2293 //
2294 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2295 // and it wouldn't be necessary to specify the parent scope
2296 // because the type is already unique by definition (it would look
2297 // like the output of -fno-standalone-debug). On the other hand,
2298 // the parent scope helps a consumer to quickly locate the object
2299 // file where the type's definition is located, so it might be
2300 // best to make this behavior a command line or debugger tuning
2301 // option.
2302 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2303 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002304 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002305 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2306 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002307 } else {
2308 // This the precompiled header being built.
2309 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002310 }
2311 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002312
Adrian Prantl9402cef2015-09-20 16:51:35 +00002313 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002314}
2315
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 // Handle qualifiers, which recursively handles what they refer to.
2318 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002319 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002320
Guy Benyei11169dd2012-12-18 14:30:41 +00002321 // Work out details of type.
2322 switch (Ty->getTypeClass()) {
2323#define TYPE(Class, Base)
2324#define ABSTRACT_TYPE(Class, Base)
2325#define NON_CANONICAL_TYPE(Class, Base)
2326#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2327#include "clang/AST/TypeNodes.def"
2328 llvm_unreachable("Dependent types cannot show up in debug information");
2329
2330 case Type::ExtVector:
2331 case Type::Vector:
2332 return CreateType(cast<VectorType>(Ty), Unit);
2333 case Type::ObjCObjectPointer:
2334 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2335 case Type::ObjCObject:
2336 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2337 case Type::ObjCInterface:
2338 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2339 case Type::Builtin:
2340 return CreateType(cast<BuiltinType>(Ty));
2341 case Type::Complex:
2342 return CreateType(cast<ComplexType>(Ty));
2343 case Type::Pointer:
2344 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002345 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002346 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002347 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002348 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002349 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002350 case Type::BlockPointer:
2351 return CreateType(cast<BlockPointerType>(Ty), Unit);
2352 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002353 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002355 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002356 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002357 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002358 case Type::FunctionProto:
2359 case Type::FunctionNoProto:
2360 return CreateType(cast<FunctionType>(Ty), Unit);
2361 case Type::ConstantArray:
2362 case Type::VariableArray:
2363 case Type::IncompleteArray:
2364 return CreateType(cast<ArrayType>(Ty), Unit);
2365
2366 case Type::LValueReference:
2367 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2368 case Type::RValueReference:
2369 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2370
2371 case Type::MemberPointer:
2372 return CreateType(cast<MemberPointerType>(Ty), Unit);
2373
2374 case Type::Atomic:
2375 return CreateType(cast<AtomicType>(Ty), Unit);
2376
Xiuli Pan9c14e282016-01-09 12:53:17 +00002377 case Type::Pipe:
2378 return CreateType(cast<PipeType>(Ty), Unit);
2379
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002381 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2382
David Blaikie42edade2014-11-11 20:44:45 +00002383 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002384 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 case Type::Elaborated:
2386 case Type::Paren:
2387 case Type::SubstTemplateTypeParm:
2388 case Type::TypeOfExpr:
2389 case Type::TypeOf:
2390 case Type::Decltype:
2391 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002392 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002393 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002395
David Blaikie42edade2014-11-11 20:44:45 +00002396 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002397}
2398
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002399llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2400 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002401 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002402
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002403 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002404
2405 // We may have cached a forward decl when we could have created
2406 // a non-forward decl. Go ahead and create a non-forward decl
2407 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002408 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002409 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002410
2411 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002413
2414 // Propagate members from the declaration to the definition
2415 // CreateType(const RecordType*) will overwrite this with the members in the
2416 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002417 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002418
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002420 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002421 return Res;
2422}
2423
2424// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002425llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002427
Guy Benyei11169dd2012-12-18 14:30:41 +00002428 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002429 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 unsigned Line = getLineNumber(RD->getLocation());
2431 StringRef RDName = getClassName(RD);
2432
Amjad Abouddc4531e2016-04-30 01:44:38 +00002433 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002434
David Blaikied2785892013-08-18 17:36:19 +00002435 // If we ended up creating the type during the context chain construction,
2436 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002438 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002439 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002440 return T;
David Blaikied2785892013-08-18 17:36:19 +00002441
Adrian Prantl381e7552014-02-04 21:29:50 +00002442 // If this is just a forward or incomplete declaration, construct an
2443 // appropriately marked node and just return it.
2444 const RecordDecl *D = RD->getDefinition();
2445 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002446 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002447
2448 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2449 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002450
Manman Rene0064d82013-08-29 23:19:58 +00002451 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2452
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002453 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002454 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2455 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002456
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002457 // Elements of composite types usually have back to the type, creating
2458 // uniquing cycles. Distinct nodes are more efficient.
2459 switch (RealDecl->getTag()) {
2460 default:
2461 llvm_unreachable("invalid composite type tag");
2462
2463 case llvm::dwarf::DW_TAG_array_type:
2464 case llvm::dwarf::DW_TAG_enumeration_type:
2465 // Array elements and most enumeration elements don't have back references,
2466 // so they don't tend to be involved in uniquing cycles and there is some
2467 // chance of merging them when linking together two modules. Only make
2468 // them distinct if they are ODR-uniqued.
2469 if (FullName.empty())
2470 break;
2471
2472 case llvm::dwarf::DW_TAG_structure_type:
2473 case llvm::dwarf::DW_TAG_union_type:
2474 case llvm::dwarf::DW_TAG_class_type:
2475 // Immediatley resolve to a distinct node.
2476 RealDecl =
2477 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2478 break;
2479 }
2480
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002481 RegionMap[Ty->getDecl()].reset(RealDecl);
2482 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002483
David Blaikieadfbf992013-08-18 16:55:33 +00002484 if (const ClassTemplateSpecializationDecl *TSpecial =
2485 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002486 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002487 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002488 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002489}
2490
David Blaikieadfbf992013-08-18 16:55:33 +00002491void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002493 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002495 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2496 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002497 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002498 while (1) {
2499 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2500 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2501 if (PBT && !BRL.isPrimaryBaseVirtual())
2502 PBase = PBT;
2503 else
2504 break;
2505 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002506 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002507 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2508 getOrCreateFile(RD->getLocation())));
2509 } else if (RD->isDynamicClass())
2510 ContainingType = RealDecl;
2511
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002512 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002513}
2514
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002515llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002516 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002517 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2519 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002520 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002521 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002522 *Offset += FieldSize;
2523 return Ty;
2524}
2525
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002526void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002527 StringRef &Name,
2528 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002529 llvm::DIScope *&FDContext,
2530 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002531 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002532 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2533 Name = getFunctionName(FD);
2534 // Use mangled name as linkage name for C/C++ functions.
2535 if (FD->hasPrototype()) {
2536 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002537 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002538 }
2539 // No need to replicate the linkage name if it isn't different from the
2540 // subprogram name, no need to have it at all unless coverage is enabled or
2541 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002542 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2543 !CGM.getCodeGenOpts().EmitGcovNotes &&
2544 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002545 LinkageName = StringRef();
2546
Benjamin Kramer8c305922016-02-02 11:06:51 +00002547 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002548 if (const NamespaceDecl *NSDecl =
2549 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2550 FDContext = getOrCreateNameSpace(NSDecl);
2551 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002552 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2553 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2554 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2555 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002556 // Collect template parameters.
2557 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2558 }
2559}
2560
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002561void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002562 unsigned &LineNo, QualType &T,
2563 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002564 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002565 Unit = getOrCreateFile(VD->getLocation());
2566 LineNo = getLineNumber(VD->getLocation());
2567
2568 setLocation(VD->getLocation());
2569
2570 T = VD->getType();
2571 if (T->isIncompleteArrayType()) {
2572 // CodeGen turns int[] into int[1] so we'll do the same here.
2573 llvm::APInt ConstVal(32, 1);
2574 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2575
2576 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2577 ArrayType::Normal, 0);
2578 }
2579
2580 Name = VD->getName();
2581 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2582 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2583 LinkageName = CGM.getMangledName(VD);
2584 if (LinkageName == Name)
2585 LinkageName = StringRef();
2586
2587 // Since we emit declarations (DW_AT_members) for static members, place the
2588 // definition of those static members in the namespace they were declared in
2589 // in the source code (the lexical decl context).
2590 // FIXME: Generalize this for even non-member global variables where the
2591 // declaration and definition may have different lexical decl contexts, once
2592 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002593 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2594 : VD->getDeclContext();
2595 // When a record type contains an in-line initialization of a static data
2596 // member, and the record type is marked as __declspec(dllexport), an implicit
2597 // definition of the member will be created in the record context. DWARF
2598 // doesn't seem to have a nice way to describe this in a form that consumers
2599 // are likely to understand, so fake the "normal" situation of a definition
2600 // outside the class by putting it in the global scope.
2601 if (DC->isRecord())
2602 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002603
Amjad Abouddc4531e2016-04-30 01:44:38 +00002604 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2605 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002606}
2607
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002608llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002609CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002610 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002611 StringRef Name, LinkageName;
2612 unsigned Flags = 0;
2613 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002614 llvm::DIFile *Unit = getOrCreateFile(Loc);
2615 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002616 unsigned Line = getLineNumber(Loc);
2617
2618 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2619 TParamsArray, Flags);
2620 // Build function type.
2621 SmallVector<QualType, 16> ArgTypes;
2622 for (const ParmVarDecl *Parm: FD->parameters())
2623 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002624 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2625 QualType FnType = CGM.getContext().getFunctionType(
2626 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002627 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002628 DContext, Name, LinkageName, Unit, Line,
2629 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002630 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002631 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002632 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002633 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2634 std::make_tuple(CanonDecl),
2635 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002636 return SP;
2637}
2638
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002639llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002640CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2641 QualType T;
2642 StringRef Name, LinkageName;
2643 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002644 llvm::DIFile *Unit = getOrCreateFile(Loc);
2645 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002646 unsigned Line = getLineNumber(Loc);
2647
2648 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002649 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2650 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2651 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002652 FwdDeclReplaceMap.emplace_back(
2653 std::piecewise_construct,
2654 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2655 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002656 return GV;
2657}
2658
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002659llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002660 // We only need a declaration (not a definition) of the type - so use whatever
2661 // we would otherwise do to get a type for a pointee. (forward declarations in
2662 // limited debug info, full definitions (if the type definition is available)
2663 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002664 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2665 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002666 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002667 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002668
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002669 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002670 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002671
2672 // No definition for now. Emit a forward definition that might be
2673 // merged with a potential upcoming definition.
2674 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2675 return getFunctionForwardDeclaration(FD);
2676 else if (const auto *VD = dyn_cast<VarDecl>(D))
2677 return getGlobalVariableForwardDeclaration(VD);
2678
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002679 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002680}
2681
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002682llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002683 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002684 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002685
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002687 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002688 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002689
2690 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002691 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002692
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002693 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002694 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002695 if (const CXXMethodDecl *MD =
2696 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002697 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002698 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002699 }
2700 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002702 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002703 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002704 return SP;
2705 }
2706
Aaron Ballman86c93902014-03-06 23:45:36 +00002707 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002708 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002710 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002711 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002712 return SP;
2713 }
2714 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002715 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002716}
2717
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002718// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002719// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002720llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002721 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002722 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002723 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002724 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2725 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002726 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002727
2728 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2729 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002730
2731 const auto *FTy = FnType->getAs<FunctionType>();
2732 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2733
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2735 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002736 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002737
2738 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002739 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002740
2741 // Replace the instancetype keyword with the actual type.
2742 if (ResultTy == CGM.getContext().getObjCInstanceType())
2743 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002744 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002745
Adrian Prantl7bec9032013-05-10 21:08:31 +00002746 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002748 QualType SelfDeclTy;
2749 if (auto *SelfDecl = OMethod->getSelfDecl())
2750 SelfDeclTy = SelfDecl->getType();
2751 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2752 if (FPT->getNumParams() > 1)
2753 SelfDeclTy = FPT->getParamType(0);
2754 if (!SelfDeclTy.isNull())
2755 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002757 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002758 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002759 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002760 for (const auto *PI : OMethod->params())
2761 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002762 // Variadic methods need a special marker at the end of the type list.
2763 if (OMethod->isVariadic())
2764 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002765
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002766 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002767 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002769
Adrian Prantl800faef2014-02-25 23:42:18 +00002770 // Handle variadic function types; they need an additional
2771 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002772 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2773 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002774 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002775 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2776 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2777 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2778 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2779 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002780 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002781 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002782 }
2783
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002784 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002785}
2786
Eric Christophere7b87e52014-10-26 23:40:33 +00002787void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2788 SourceLocation ScopeLoc, QualType FnType,
2789 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002790
2791 StringRef Name;
2792 StringRef LinkageName;
2793
2794 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2795
2796 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002797 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002798
Guy Benyei11169dd2012-12-18 14:30:41 +00002799 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002800 llvm::DIFile *Unit = getOrCreateFile(Loc);
2801 llvm::DIScope *FDContext = Unit;
2802 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 if (!HasDecl) {
2804 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002805 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002806 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002807 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002808 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002810 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002811 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002812 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002813 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 return;
2815 }
2816 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002817 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2818 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2820 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002821 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002822 } else {
2823 // Use llvm function name.
2824 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002825 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 }
2827 if (!Name.empty() && Name[0] == '\01')
2828 Name = Name.substr(1);
2829
Adrian Prantl42d71b92014-04-10 23:21:53 +00002830 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002831 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002832 // Artificial functions without a location should not silently reuse CurLoc.
2833 if (Loc.isInvalid())
2834 CurLoc = SourceLocation();
2835 }
2836 unsigned LineNo = getLineNumber(Loc);
2837 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002838
Eric Christopher8018e412014-03-27 18:50:35 +00002839 // FIXME: The function declaration we're constructing here is mostly reusing
2840 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2841 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2842 // all subprograms instead of the actual context since subprogram definitions
2843 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002844 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002845 FDContext, Name, LinkageName, Unit, LineNo,
2846 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002847 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002848 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002849 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002850 // We might get here with a VarDecl in the case we're generating
2851 // code for the initialization of globals. Do not record these decls
2852 // as they will overwrite the actual VarDecl Decl in the cache.
2853 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002854 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002855
Adrian Prantlbebb8932014-03-21 21:01:58 +00002856 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002857 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002858
Guy Benyei11169dd2012-12-18 14:30:41 +00002859 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002860 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002861}
2862
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002863void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2864 QualType FnType) {
2865 StringRef Name;
2866 StringRef LinkageName;
2867
2868 const Decl *D = GD.getDecl();
2869 if (!D)
2870 return;
2871
2872 unsigned Flags = 0;
2873 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002874 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002875 llvm::DINodeArray TParamsArray;
2876 if (isa<FunctionDecl>(D)) {
2877 // If there is a DISubprogram for this function available then use it.
2878 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2879 TParamsArray, Flags);
2880 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2881 Name = getObjCMethodName(OMD);
2882 Flags |= llvm::DINode::FlagPrototyped;
2883 } else {
2884 llvm_unreachable("not a function or ObjC method");
2885 }
2886 if (!Name.empty() && Name[0] == '\01')
2887 Name = Name.substr(1);
2888
2889 if (D->isImplicit()) {
2890 Flags |= llvm::DINode::FlagArtificial;
2891 // Artificial functions without a location should not silently reuse CurLoc.
2892 if (Loc.isInvalid())
2893 CurLoc = SourceLocation();
2894 }
2895 unsigned LineNo = getLineNumber(Loc);
2896 unsigned ScopeLine = 0;
2897
Adrian Prantle76bda52016-04-15 15:55:45 +00002898 DBuilder.retainType(DBuilder.createFunction(
2899 FDContext, Name, LinkageName, Unit, LineNo,
2900 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2901 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2902 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002903}
2904
David Blaikie835afb22015-01-21 23:08:17 +00002905void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 // Update our current location
2907 setLocation(Loc);
2908
Eric Christophere7b87e52014-10-26 23:40:33 +00002909 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2910 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002911
Adrian Prantle83b1302014-01-07 22:05:52 +00002912 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002913 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002914 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002915}
2916
Guy Benyei11169dd2012-12-18 14:30:41 +00002917void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002918 llvm::MDNode *Back = nullptr;
2919 if (!LexicalBlockStack.empty())
2920 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002921 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002922 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002923 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002924}
2925
Eric Christopher0fdcb312013-05-16 00:52:20 +00002926void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2927 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 // Set our current location.
2929 setLocation(Loc);
2930
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002932 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2933 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002934
Benjamin Kramer8c305922016-02-02 11:06:51 +00002935 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002936 return;
2937
2938 // Create a new lexical block and push it on the stack.
2939 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002940}
2941
Eric Christopher0fdcb312013-05-16 00:52:20 +00002942void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2943 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002944 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2945
2946 // Provide an entry in the line table for the end of the block.
2947 EmitLocation(Builder, Loc);
2948
Benjamin Kramer8c305922016-02-02 11:06:51 +00002949 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002950 return;
2951
Guy Benyei11169dd2012-12-18 14:30:41 +00002952 LexicalBlockStack.pop_back();
2953}
2954
Guy Benyei11169dd2012-12-18 14:30:41 +00002955void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2956 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2957 unsigned RCount = FnBeginRegionCount.back();
2958 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2959
2960 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002961 while (LexicalBlockStack.size() != RCount) {
2962 // Provide an entry in the line table for the end of the block.
2963 EmitLocation(Builder, CurLoc);
2964 LexicalBlockStack.pop_back();
2965 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002966 FnBeginRegionCount.pop_back();
2967}
2968
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002969llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002970 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002971
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002972 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 QualType FType;
2974 uint64_t FieldSize, FieldOffset;
2975 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002976
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002977 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002978 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002979
2980 FieldOffset = 0;
2981 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2982 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2983 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2984 FType = CGM.getContext().IntTy;
2985 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2986 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2987
2988 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2989 if (HasCopyAndDispose) {
2990 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002991 EltTys.push_back(
2992 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2993 EltTys.push_back(
2994 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 }
2996 bool HasByrefExtendedLayout;
2997 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002998 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2999 HasByrefExtendedLayout) &&
3000 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003001 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003002 EltTys.push_back(
3003 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003004 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003005
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3007 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003008 CGM.getTarget().getPointerAlign(0))) {
3009 CharUnits FieldOffsetInBytes =
3010 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003011 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003012 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003013
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 if (NumPaddingBytes.isPositive()) {
3015 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3016 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3017 pad, ArrayType::Normal, 0);
3018 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3019 }
3020 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003021
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003023 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 FieldSize = CGM.getContext().getTypeSize(FType);
3025 FieldAlign = CGM.getContext().toBits(Align);
3026
Eric Christopherb2a008c2013-05-16 00:45:12 +00003027 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003028 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3029 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 EltTys.push_back(FieldTy);
3031 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003032
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003033 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003034
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003035 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003036
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003038 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003039}
3040
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003041void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3042 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003043 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003044 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003046 if (VD->hasAttr<NoDebugAttr>())
3047 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003048
David Blaikie7fceebf2013-08-19 03:37:48 +00003049 bool Unwritten =
3050 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3051 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003052 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003053 if (!Unwritten)
3054 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003055 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003056 uint64_t XOffset = 0;
3057 if (VD->hasAttr<BlocksAttr>())
3058 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003059 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 Ty = getOrCreateType(VD->getType(), Unit);
3061
3062 // If there is no debug info for this type then do not emit debug info
3063 // for this variable.
3064 if (!Ty)
3065 return;
3066
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003068 unsigned Line = 0;
3069 unsigned Column = 0;
3070 if (!Unwritten) {
3071 Line = getLineNumber(VD->getLocation());
3072 Column = getColumnNumber(VD->getLocation());
3073 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003074 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 unsigned Flags = 0;
3076 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003077 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 // If this is the first argument and it is implicit then
3079 // give it an object pointer flag.
3080 // FIXME: There has to be a better way to do this, but for static
3081 // functions there won't be an implicit param at arg1 and
3082 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003083 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003084 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003085 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003086 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3087 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003088 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003089
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003090 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003091
3092 StringRef Name = VD->getName();
3093 if (!Name.empty()) {
3094 if (VD->hasAttr<BlocksAttr>()) {
3095 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003096 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 // offset of __forwarding field
3098 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003099 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003100 Expr.push_back(offset.getQuantity());
3101 Expr.push_back(llvm::dwarf::DW_OP_deref);
3102 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003103 // offset of x field
3104 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003105 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003106
3107 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003108 auto *D = ArgNo
3109 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3110 *ArgNo, Unit, Line, Ty)
3111 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3112 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003113
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003115 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3116 llvm::DebugLoc::get(Line, Column, Scope),
3117 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003118 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003119 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003120 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003121 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3122 // If VD is an anonymous union then Storage represents value for
3123 // all union fields.
3124 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3125 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003126 // GDB has trouble finding local variables in anonymous unions, so we emit
3127 // artifical local variables for each of the members.
3128 //
3129 // FIXME: Remove this code as soon as GDB supports this.
3130 // The debug info verifier in LLVM operates based on the assumption that a
3131 // variable has the same size as its storage and we had to disable the check
3132 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003133 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003134 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003135 StringRef FieldName = Field->getName();
3136
3137 // Ignore unnamed fields. Do not ignore unnamed records.
3138 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3139 continue;
3140
3141 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003142 auto *D = DBuilder.createAutoVariable(
3143 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3144 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003145
3146 // Insert an llvm.dbg.declare into the current block.
3147 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3148 llvm::DebugLoc::get(Line, Column, Scope),
3149 Builder.GetInsertBlock());
3150 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003151 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003152 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003153
3154 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003155 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003156 ArgNo
3157 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3158 Ty, CGM.getLangOpts().Optimize,
3159 Flags)
3160 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3161 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003162
3163 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003164 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3165 llvm::DebugLoc::get(Line, Column, Scope),
3166 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003167}
3168
3169void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3170 llvm::Value *Storage,
3171 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003172 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003173 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003174}
3175
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003176llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3177 llvm::DIType *Ty) {
3178 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003179 if (CachedTy)
3180 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003181 return DBuilder.createObjectPointerType(Ty);
3182}
3183
Eric Christophere7b87e52014-10-26 23:40:33 +00003184void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3185 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003186 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003187 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003189
Craig Topper8a13c412014-05-21 05:09:00 +00003190 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003191 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003192 if (VD->hasAttr<NoDebugAttr>())
3193 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003194
Guy Benyei11169dd2012-12-18 14:30:41 +00003195 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003196
Guy Benyei11169dd2012-12-18 14:30:41 +00003197 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003198 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3199 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003200 if (isByRef)
3201 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003202 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 Ty = getOrCreateType(VD->getType(), Unit);
3204
3205 // Self is passed along as an implicit non-arg variable in a
3206 // block. Mark it as the object pointer.
3207 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003208 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003209
3210 // Get location information.
3211 unsigned Line = getLineNumber(VD->getLocation());
3212 unsigned Column = getColumnNumber(VD->getLocation());
3213
3214 const llvm::DataLayout &target = CGM.getDataLayout();
3215
3216 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003217 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3219
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003220 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003221 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003222 addr.push_back(llvm::dwarf::DW_OP_deref);
3223 addr.push_back(llvm::dwarf::DW_OP_plus);
3224 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003226 addr.push_back(llvm::dwarf::DW_OP_deref);
3227 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003228 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003229 offset =
3230 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003231 addr.push_back(offset.getQuantity());
3232 addr.push_back(llvm::dwarf::DW_OP_deref);
3233 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 // offset of x field
3235 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003236 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 }
3238
3239 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003240 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003241 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003242 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003243
Guy Benyei11169dd2012-12-18 14:30:41 +00003244 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003245 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3246 if (InsertPoint)
3247 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3248 InsertPoint);
3249 else
3250 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3251 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003252}
3253
Guy Benyei11169dd2012-12-18 14:30:41 +00003254void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3255 unsigned ArgNo,
3256 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003257 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003258 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003259}
3260
3261namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003262struct BlockLayoutChunk {
3263 uint64_t OffsetInBits;
3264 const BlockDecl::Capture *Capture;
3265};
3266bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3267 return l.OffsetInBits < r.OffsetInBits;
3268}
Guy Benyei11169dd2012-12-18 14:30:41 +00003269}
3270
3271void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003272 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003273 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003274 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003275 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003276 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 ASTContext &C = CGM.getContext();
3278 const BlockDecl *blockDecl = block.getBlockDecl();
3279
3280 // Collect some general information about the block's location.
3281 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003282 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003283 unsigned line = getLineNumber(loc);
3284 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003285
Guy Benyei11169dd2012-12-18 14:30:41 +00003286 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003287 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003288
3289 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003290 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003291
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003292 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003293 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3294 blockLayout->getElementOffsetInBits(0),
3295 tunit, tunit));
3296 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3297 blockLayout->getElementOffsetInBits(1),
3298 tunit, tunit));
3299 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3300 blockLayout->getElementOffsetInBits(2),
3301 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003302 auto *FnTy = block.getBlockExpr()->getFunctionType();
3303 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3304 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 blockLayout->getElementOffsetInBits(3),
3306 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003307 fields.push_back(createFieldType(
3308 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3309 ? C.getBlockDescriptorExtendedType()
3310 : C.getBlockDescriptorType()),
3311 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003312
3313 // We want to sort the captures by offset, not because DWARF
3314 // requires this, but because we're paranoid about debuggers.
3315 SmallVector<BlockLayoutChunk, 8> chunks;
3316
3317 // 'this' capture.
3318 if (blockDecl->capturesCXXThis()) {
3319 BlockLayoutChunk chunk;
3320 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003321 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003322 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003323 chunks.push_back(chunk);
3324 }
3325
3326 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003327 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003328 const VarDecl *variable = capture.getVariable();
3329 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3330
3331 // Ignore constant captures.
3332 if (captureInfo.isConstant())
3333 continue;
3334
3335 BlockLayoutChunk chunk;
3336 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003337 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003338 chunk.Capture = &capture;
3339 chunks.push_back(chunk);
3340 }
3341
3342 // Sort by offset.
3343 llvm::array_pod_sort(chunks.begin(), chunks.end());
3344
Eric Christophere7b87e52014-10-26 23:40:33 +00003345 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3346 e = chunks.end();
3347 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003348 uint64_t offsetInBits = i->OffsetInBits;
3349 const BlockDecl::Capture *capture = i->Capture;
3350
3351 // If we have a null capture, this must be the C++ 'this' capture.
3352 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003353 QualType type;
3354 if (auto *Method =
3355 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3356 type = Method->getThisType(C);
3357 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3358 type = QualType(RDecl->getTypeForDecl(), 0);
3359 else
3360 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003361
3362 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3363 offsetInBits, tunit, tunit));
3364 continue;
3365 }
3366
3367 const VarDecl *variable = capture->getVariable();
3368 StringRef name = variable->getName();
3369
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003370 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003371 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003372 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003373
3374 // FIXME: this creates a second copy of this type!
3375 uint64_t xoffset;
3376 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003377 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3378 fieldType =
3379 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3380 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003381 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003382 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3383 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003384 }
3385 fields.push_back(fieldType);
3386 }
3387
3388 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003389 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3390 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003392 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003394 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003395 tunit, typeName.str(), tunit, line,
3396 CGM.getContext().toBits(block.BlockSize),
3397 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3399
3400 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003401 unsigned flags = llvm::DINode::FlagArtificial;
3402 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003403
3404 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003405 auto *debugVar = DBuilder.createParameterVariable(
3406 scope, Arg->getName(), ArgNo, tunit, line, type,
3407 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003408
Adrian Prantl616bef42013-03-14 21:52:59 +00003409 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003410 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003411 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003412 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003413 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003414 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003415
Adrian Prantl616bef42013-03-14 21:52:59 +00003416 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003417 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3418 llvm::DebugLoc::get(line, column, scope),
3419 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003420}
3421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003422llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003423CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3424 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003425 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003426
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003427 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003428 if (MI != StaticDataMemberCache.end()) {
3429 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003430 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003431 }
David Blaikiece763042013-08-20 21:49:21 +00003432
3433 // If the member wasn't found in the cache, lazily construct and add it to the
3434 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003435 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003436 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003437 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003438}
3439
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003440llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3441 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3442 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3443 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003444
3445 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003446 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003447 StringRef FieldName = Field->getName();
3448
3449 // Ignore unnamed fields, but recurse into anonymous records.
3450 if (FieldName.empty()) {
3451 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3452 if (RT)
3453 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3454 Var, DContext);
3455 continue;
3456 }
3457 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003458 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3459 LineNo, FieldTy,
3460 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003461 }
3462 return GV;
3463}
3464
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003465void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003466 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003467 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003468 if (D->hasAttr<NoDebugAttr>())
3469 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003470 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003471 llvm::DIFile *Unit = nullptr;
3472 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003473 unsigned LineNo;
3474 StringRef DeclName, LinkageName;
3475 QualType T;
3476 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003477
3478 // Attempt to store one global variable for the declaration - even if we
3479 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003480 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003481
3482 // If this is an anonymous union then we'll want to emit a global
3483 // variable for each member of the anonymous union so that it's possible
3484 // to find the name of any field in the union.
3485 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003486 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003487 assert(RD->isAnonymousStructOrUnion() &&
3488 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003489 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3490 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003491 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003492 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3493 Var->hasInternalLinkage(), Var,
3494 getOrCreateStaticDataMemberDeclarationOrNull(D));
3495 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003496 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003497}
3498
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003499void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3500 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003501 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003502 if (VD->hasAttr<NoDebugAttr>())
3503 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003504 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003505 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003506 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003507 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003508 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3509 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3510 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3511 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3512 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003513 // Do not use global variables for enums.
3514 //
3515 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003516 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003517 return;
David Blaikiea15565562014-04-04 20:56:17 +00003518 // Do not emit separate definitions for function local const/statics.
3519 if (isa<FunctionDecl>(VD->getDeclContext()))
3520 return;
David Blaikiebb113912014-04-05 07:23:17 +00003521 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003522 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003523 if (VarD->isStaticDataMember()) {
3524 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003525 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003526 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003527 //
3528 // FIXME: This is probably unnecessary, since Ty should reference RD
3529 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003530 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003531 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003532 return;
3533 }
3534
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003535 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003536
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003537 auto &GV = DeclCache[VD];
3538 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003539 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003540 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003541 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003542 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003543}
3544
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003545llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003546 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003547 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003548 llvm::DIScope *Mod = getParentModuleOrNull(D);
3549 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003550}
3551
David Blaikie9f88fe82013-04-22 06:13:21 +00003552void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003553 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003554 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003555 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3556 if (!NSDecl->isAnonymousNamespace() ||
3557 CGM.getCodeGenOpts().DebugExplicitImport) {
3558 DBuilder.createImportedModule(
3559 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3560 getOrCreateNameSpace(NSDecl),
3561 getLineNumber(UD.getLocation()));
3562 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003563}
3564
David Blaikiebd483762013-05-20 04:58:53 +00003565void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003566 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003567 return;
3568 assert(UD.shadow_size() &&
3569 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3570 // Emitting one decl is sufficient - debuggers can detect that this is an
3571 // overloaded name & provide lookup for all the overloads.
3572 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003573 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003574 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003575 DBuilder.createImportedDeclaration(
3576 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3577 getLineNumber(USD.getLocation()));
3578}
3579
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003580void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003581 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3582 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003583 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003584 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003585 DBuilder.createImportedDeclaration(
3586 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3587 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3588 getLineNumber(ID.getLocation()));
3589 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003590}
3591
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003592llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003593CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003594 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003595 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003596 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003597 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003598 return cast<llvm::DIImportedEntity>(VH);
3599 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003600 if (const NamespaceAliasDecl *Underlying =
3601 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3602 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003603 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003604 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3605 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3606 NA.getName());
3607 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003608 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003609 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3610 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3611 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003612 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003613 return R;
3614}
3615
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003616llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003617CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003618 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003619 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003620 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003621 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003622
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003624 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003625 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003626 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003627 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003628 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003629 return NS;
3630}
3631
Adrian Prantla5206ce2015-09-22 23:26:43 +00003632void CGDebugInfo::setDwoId(uint64_t Signature) {
3633 assert(TheCU && "no main compile unit");
3634 TheCU->setDWOId(Signature);
3635}
3636
3637
Guy Benyei11169dd2012-12-18 14:30:41 +00003638void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003639 // Creating types might create further types - invalidating the current
3640 // element and the size(), so don't cache/reference them.
3641 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3642 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003643 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003644 ? CreateTypeDefinition(E.Type, E.Unit)
3645 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003646 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003647 }
3648
David Blaikief427b002014-05-06 03:42:01 +00003649 for (auto p : ReplaceMap) {
3650 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003651 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003652 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003653
David Blaikief427b002014-05-06 03:42:01 +00003654 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003655 assert(it != TypeCache.end());
3656 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003657
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003658 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3659 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003660 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003661
Frederic Rissd253ed62014-11-18 03:40:51 +00003662 for (const auto &p : FwdDeclReplaceMap) {
3663 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003664 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003665 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003666
3667 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003668 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003669 // with ourselves, that will destroy the temporary MDNode and
3670 // replace it with a standard one, avoiding leaking memory.
3671 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003672 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003673 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003674 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003675
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003676 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003677 }
3678
Adrian Prantl73409ce2013-03-11 18:33:46 +00003679 // We keep our own list of retained types, because we need to look
3680 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003681 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003682 if (auto MD = TypeCache[RT])
3683 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003684
Guy Benyei11169dd2012-12-18 14:30:41 +00003685 DBuilder.finalize();
3686}
David Blaikie66088d52014-09-24 17:01:27 +00003687
3688void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003689 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003690 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003691
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003692 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003693 // Don't ignore in case of explicit cast where it is referenced indirectly.
3694 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003695}