blob: 1ef5e215c69b5cffb05d261a3512b693fcfe5026 [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;
380 } else if (LO.C99) {
381 LangTag = llvm::dwarf::DW_LANG_C99;
382 } else {
383 LangTag = llvm::dwarf::DW_LANG_C89;
384 }
385
386 std::string Producer = getClangFullVersion();
387
388 // Figure out which version of the ObjC runtime we have.
389 unsigned RuntimeVers = 0;
390 if (LO.ObjC1)
391 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
392
Adrian Prantl826824e2016-04-08 22:43:06 +0000393 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
394 switch (DebugKind) {
395 case codegenoptions::NoDebugInfo:
396 case codegenoptions::LocTrackingOnly:
397 EmissionKind = llvm::DICompileUnit::NoDebug;
398 break;
399 case codegenoptions::DebugLineTablesOnly:
400 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
401 break;
402 case codegenoptions::LimitedDebugInfo:
403 case codegenoptions::FullDebugInfo:
404 EmissionKind = llvm::DICompileUnit::FullDebug;
405 break;
406 }
407
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000409 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000410 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000411 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
412 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantl826824e2016-04-08 22:43:06 +0000413 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */);
Guy Benyei11169dd2012-12-18 14:30:41 +0000414}
415
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000416llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000417 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000418 StringRef BTName;
419 switch (BT->getKind()) {
420#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000421#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000422#include "clang/AST/BuiltinTypes.def"
423 case BuiltinType::Dependent:
424 llvm_unreachable("Unexpected builtin type");
425 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000426 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000427 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000428 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000430 if (!ClassTy)
431 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
432 "objc_class", TheCU,
433 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 return ClassTy;
435 case BuiltinType::ObjCId: {
436 // typedef struct objc_class *Class;
437 // typedef struct objc_object {
438 // Class isa;
439 // } *id;
440
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000441 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 return ObjTy;
443
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000444 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000445 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
446 "objc_class", TheCU,
447 getOrCreateMainFile(), 0);
448
449 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000450
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000451 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000452
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000453 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000454 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000455 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000456
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000457 DBuilder.replaceArrays(
458 ObjTy,
459 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
460 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000461 return ObjTy;
462 }
463 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000464 if (!SelTy)
465 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
466 "objc_selector", TheCU,
467 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 return SelTy;
469 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000470
Alexey Bader954ba212016-04-08 13:40:33 +0000471#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
472 case BuiltinType::Id: \
473 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
474 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000475#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000476 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000477 return DBuilder.createBasicType(
478 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
479 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000480 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000481 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000482 case BuiltinType::OCLClkEvent:
483 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
484 case BuiltinType::OCLQueue:
485 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
486 case BuiltinType::OCLNDRange:
487 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
488 case BuiltinType::OCLReserveID:
489 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000490
Guy Benyei11169dd2012-12-18 14:30:41 +0000491 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000492 case BuiltinType::Char_U:
493 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
494 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000495 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000496 case BuiltinType::SChar:
497 Encoding = llvm::dwarf::DW_ATE_signed_char;
498 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000499 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000500 case BuiltinType::Char32:
501 Encoding = llvm::dwarf::DW_ATE_UTF;
502 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000503 case BuiltinType::UShort:
504 case BuiltinType::UInt:
505 case BuiltinType::UInt128:
506 case BuiltinType::ULong:
507 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000508 case BuiltinType::ULongLong:
509 Encoding = llvm::dwarf::DW_ATE_unsigned;
510 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000511 case BuiltinType::Short:
512 case BuiltinType::Int:
513 case BuiltinType::Int128:
514 case BuiltinType::Long:
515 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000516 case BuiltinType::LongLong:
517 Encoding = llvm::dwarf::DW_ATE_signed;
518 break;
519 case BuiltinType::Bool:
520 Encoding = llvm::dwarf::DW_ATE_boolean;
521 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 case BuiltinType::Half:
523 case BuiltinType::Float:
524 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000525 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000526 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000527 // FIXME: For targets where long double and __float128 have the same size,
528 // they are currently indistinguishable in the debugger without some
529 // special treatment. However, there is currently no consensus on encoding
530 // and this should be updated once a DWARF encoding exists for distinct
531 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000532 Encoding = llvm::dwarf::DW_ATE_float;
533 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 }
535
536 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000537 case BuiltinType::Long:
538 BTName = "long int";
539 break;
540 case BuiltinType::LongLong:
541 BTName = "long long int";
542 break;
543 case BuiltinType::ULong:
544 BTName = "long unsigned int";
545 break;
546 case BuiltinType::ULongLong:
547 BTName = "long long unsigned int";
548 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 default:
550 BTName = BT->getName(CGM.getLangOpts());
551 break;
552 }
553 // Bit size, align and offset of the type.
554 uint64_t Size = CGM.getContext().getTypeSize(BT);
555 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000556 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000557}
558
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000559llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000561 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 if (Ty->isComplexIntegerType())
563 Encoding = llvm::dwarf::DW_ATE_lo_user;
564
565 uint64_t Size = CGM.getContext().getTypeSize(Ty);
566 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000567 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568}
569
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000570llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
571 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 QualifierCollector Qc;
573 const Type *T = Qc.strip(Ty);
574
575 // Ignore these qualifiers for now.
576 Qc.removeObjCGCAttr();
577 Qc.removeAddressSpace();
578 Qc.removeObjCLifetime();
579
580 // We will create one Derived type for one qualifier and recurse to handle any
581 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000582 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000583 if (Qc.hasConst()) {
584 Tag = llvm::dwarf::DW_TAG_const_type;
585 Qc.removeConst();
586 } else if (Qc.hasVolatile()) {
587 Tag = llvm::dwarf::DW_TAG_volatile_type;
588 Qc.removeVolatile();
589 } else if (Qc.hasRestrict()) {
590 Tag = llvm::dwarf::DW_TAG_restrict_type;
591 Qc.removeRestrict();
592 } else {
593 assert(Qc.empty() && "Unknown type qualifier for debug info");
594 return getOrCreateType(QualType(T, 0), Unit);
595 }
596
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000597 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000598
599 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
600 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000601 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000602}
603
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000604llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
605 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000606
607 // The frontend treats 'id' as a typedef to an ObjCObjectType,
608 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
609 // debug info, we want to emit 'id' in both cases.
610 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000611 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000612
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000613 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
614 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000615}
616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000617llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
618 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000619 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 Ty->getPointeeType(), Unit);
621}
622
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000623/// \return whether a C++ mangling exists for the type defined by TD.
624static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
625 switch (TheCU->getSourceLanguage()) {
626 case llvm::dwarf::DW_LANG_C_plus_plus:
627 return true;
628 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
629 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
630 default:
631 return false;
632 }
633}
634
Manman Rene0064d82013-08-29 23:19:58 +0000635/// In C++ mode, types have linkage, so we can rely on the ODR and
636/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000637static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
638 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000639 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000640 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000641 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000642
643 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000644 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000645
Manman Rene0064d82013-08-29 23:19:58 +0000646 // TODO: This is using the RTTI name. Is there a better way to get
647 // a unique string for a type?
648 llvm::raw_svector_ostream Out(FullName);
649 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000650 return FullName;
651}
652
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000653/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000654static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
655 llvm::dwarf::Tag Tag;
656 if (RD->isStruct() || RD->isInterface())
657 Tag = llvm::dwarf::DW_TAG_structure_type;
658 else if (RD->isUnion())
659 Tag = llvm::dwarf::DW_TAG_union_type;
660 else {
661 // FIXME: This could be a struct type giving a default visibility different
662 // than C++ class type, but needs llvm metadata changes first.
663 assert(RD->isClass());
664 Tag = llvm::dwarf::DW_TAG_class_type;
665 }
666 return Tag;
667}
668
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000669llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000670CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000671 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000672 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000673 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
674 return cast<llvm::DICompositeType>(T);
675 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000676 unsigned Line = getLineNumber(RD->getLocation());
677 StringRef RDName = getClassName(RD);
678
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000679 uint64_t Size = 0;
680 uint64_t Align = 0;
681
682 const RecordDecl *D = RD->getDefinition();
683 if (D && D->isCompleteDefinition()) {
684 Size = CGM.getContext().getTypeSize(Ty);
685 Align = CGM.getContext().getTypeAlign(Ty);
686 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000687
688 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000689 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000690 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000691 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000693 ReplaceMap.emplace_back(
694 std::piecewise_construct, std::make_tuple(Ty),
695 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000696 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000697}
698
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000699llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000700 const Type *Ty,
701 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000703 // Bit size, align and offset of the type.
704 // Size is always the size of a pointer. We can't use getTypeSize here
705 // because that does not return the correct value for references.
706 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000707 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000708 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
709
Keno Fischer87842f32015-11-16 09:04:13 +0000710 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
711 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
712 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
713 Size, Align);
714 else
715 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
716 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717}
718
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000719llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
720 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000721 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000722 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000723 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
724 TheCU, getOrCreateMainFile(), 0);
725 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
726 Cache = DBuilder.createPointerType(Cache, Size);
727 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000728}
729
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000730llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
731 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000732 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000733 QualType FType;
734 uint64_t FieldSize, FieldOffset;
735 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000736 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000737
738 FieldOffset = 0;
739 FType = CGM.getContext().UnsignedLongTy;
740 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
741 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
742
743 Elements = DBuilder.getOrCreateArray(EltTys);
744 EltTys.clear();
745
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000746 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000747 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000748
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000749 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000750 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000751 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000752
753 // Bit size, align and offset of the type.
754 uint64_t Size = CGM.getContext().getTypeSize(Ty);
755
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000756 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000757
758 FieldOffset = 0;
759 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
760 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
761 FType = CGM.getContext().IntTy;
762 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
763 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000764 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000765 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
766
767 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 FieldSize = CGM.getContext().getTypeSize(Ty);
769 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000770 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000771 FieldSize, FieldAlign, FieldOffset,
772 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000773
774 FieldOffset += FieldSize;
775 Elements = DBuilder.getOrCreateArray(EltTys);
776
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000777 // The __block_literal_generic structs are marked with a special
778 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
779 // the debugger needs to know about. To allow type uniquing, emit
780 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000781 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000782 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000783 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000784
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000785 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000786}
787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000788llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
789 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000790 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000791 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000792
793 SmallString<128> NS;
794 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000795 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
796 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000797
798 TemplateSpecializationType::PrintTemplateArgumentList(
799 OS, Ty->getArgs(), Ty->getNumArgs(),
800 CGM.getContext().getPrintingPolicy());
801
Eric Christophere7b87e52014-10-26 23:40:33 +0000802 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
803 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000804
805 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000806 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
807 getLineNumber(Loc),
808 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000809}
810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000811llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
812 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000813 // We don't set size information, but do specify where the typedef was
814 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000815 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000816
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000817 // Typedefs are derived from some other type.
818 return DBuilder.createTypedef(
819 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
820 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000821 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000822}
823
Reid Klecknerf00f8032016-06-08 20:41:54 +0000824static unsigned getDwarfCC(CallingConv CC) {
825 switch (CC) {
826 case CC_C:
827 // Avoid emitting DW_AT_calling_convention if the C convention was used.
828 return 0;
829
830 case CC_X86StdCall:
831 return llvm::dwarf::DW_CC_BORLAND_stdcall;
832 case CC_X86FastCall:
833 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
834 case CC_X86ThisCall:
835 return llvm::dwarf::DW_CC_BORLAND_thiscall;
836 case CC_X86VectorCall:
837 return llvm::dwarf::DW_CC_LLVM_vectorcall;
838 case CC_X86Pascal:
839 return llvm::dwarf::DW_CC_BORLAND_pascal;
840
841 // FIXME: Create new DW_CC_ codes for these calling conventions.
842 case CC_X86_64Win64:
843 case CC_X86_64SysV:
844 case CC_AAPCS:
845 case CC_AAPCS_VFP:
846 case CC_IntelOclBicc:
847 case CC_SpirFunction:
848 case CC_SpirKernel:
849 case CC_Swift:
850 case CC_PreserveMost:
851 case CC_PreserveAll:
852 return 0;
853 }
854 return 0;
855}
856
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000857llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
858 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000859 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000860
861 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000862 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000863
864 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000865 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000866 if (isa<FunctionNoProtoType>(Ty))
867 EltTys.push_back(DBuilder.createUnspecifiedParameter());
868 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000869 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
870 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000871 if (FPT->isVariadic())
872 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000873 }
874
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +0000876 return DBuilder.createSubroutineType(EltTypeArray, 0,
877 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000878}
879
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000880/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000881/// As an optimization, return 0 if the access specifier equals the
882/// default for the containing type.
883static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
884 AccessSpecifier Default = clang::AS_none;
885 if (RD && RD->isClass())
886 Default = clang::AS_private;
887 else if (RD && (RD->isStruct() || RD->isUnion()))
888 Default = clang::AS_public;
889
890 if (Access == Default)
891 return 0;
892
Eric Christophere7b87e52014-10-26 23:40:33 +0000893 switch (Access) {
894 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000895 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000896 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000897 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000898 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000899 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000900 case clang::AS_none:
901 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000902 }
903 llvm_unreachable("unexpected access enumerator");
904}
Guy Benyei11169dd2012-12-18 14:30:41 +0000905
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000906llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000907 StringRef name, QualType type, uint64_t sizeInBitsOverride,
908 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000909 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
910 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000911
912 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000913 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000914 unsigned line = getLineNumber(loc);
915
David Majnemer34b57492014-07-30 01:30:47 +0000916 uint64_t SizeInBits = 0;
917 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000918 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000919 TypeInfo TI = CGM.getContext().getTypeInfo(type);
920 SizeInBits = TI.Width;
921 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000922
923 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000924 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000925 }
926
Adrian Prantl21361fb2014-08-29 22:44:27 +0000927 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000928 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
929 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000930}
931
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000932void CGDebugInfo::CollectRecordLambdaFields(
933 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000934 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000935 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
936 // has the name and the location of the variable so we should iterate over
937 // both concurrently.
938 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
939 RecordDecl::field_iterator Field = CXXDecl->field_begin();
940 unsigned fieldno = 0;
941 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000942 E = CXXDecl->captures_end();
943 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000944 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000945 if (C.capturesVariable()) {
946 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000947 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000948 StringRef VName = V->getName();
949 uint64_t SizeInBitsOverride = 0;
950 if (Field->isBitField()) {
951 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
952 assert(SizeInBitsOverride && "found named 0-width bitfield");
953 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000955 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
956 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
957 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000958 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000959 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000960 // TODO: Need to handle 'this' in some way by probably renaming the
961 // this of the lambda class and having a field member of 'this' or
962 // by using AT_object_pointer for the function and having that be
963 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000964 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000965 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000966 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000967 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000968 "this", type, 0, f->getLocation(), f->getAccess(),
969 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000970
971 elements.push_back(fieldType);
972 }
973 }
974}
975
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000976llvm::DIDerivedType *
977CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000978 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000979 // Create the descriptor for the static variable, with or without
980 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000981 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000982 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
983 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000984
Eric Christopher91a31902013-01-16 01:22:32 +0000985 unsigned LineNumber = getLineNumber(Var->getLocation());
986 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000987 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000988 if (Var->getInit()) {
989 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000990 if (Value) {
991 if (Value->isInt())
992 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
993 if (Value->isFloat())
994 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
995 }
Eric Christopher91a31902013-01-16 01:22:32 +0000996 }
997
Adrian Prantl21361fb2014-08-29 22:44:27 +0000998 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000999 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +00001000 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001001 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001002 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001003}
1004
Eric Christophere7b87e52014-10-26 23:40:33 +00001005void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001006 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1007 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001008 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001009 StringRef name = field->getName();
1010 QualType type = field->getType();
1011
1012 // Ignore unnamed fields unless they're anonymous structs/unions.
1013 if (name.empty() && !type->isRecordType())
1014 return;
1015
1016 uint64_t SizeInBitsOverride = 0;
1017 if (field->isBitField()) {
1018 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
1019 assert(SizeInBitsOverride && "found named 0-width bitfield");
1020 }
1021
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001022 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001023 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
1024 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001025
1026 elements.push_back(fieldType);
1027}
1028
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001029void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001030 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001031 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001032 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001033 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1034
Eric Christopher91a31902013-01-16 01:22:32 +00001035 if (CXXDecl && CXXDecl->isLambda())
1036 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1037 else {
1038 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001039
Eric Christopher91a31902013-01-16 01:22:32 +00001040 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001041 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001042
Eric Christopher91a31902013-01-16 01:22:32 +00001043 // Static and non-static members should appear in the same order as
1044 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001045 for (const auto *I : record->decls())
1046 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001047 if (V->hasAttr<NoDebugAttr>())
1048 continue;
David Blaikiece763042013-08-20 21:49:21 +00001049 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001050 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001051 if (MI != StaticDataMemberCache.end()) {
1052 assert(MI->second &&
1053 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001054 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001055 } else {
1056 auto Field = CreateRecordStaticField(V, RecordTy, record);
1057 elements.push_back(Field);
1058 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001059 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001060 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1061 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001062
1063 // Bump field number for next field.
1064 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001065 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 }
1067}
1068
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001069llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001070CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001071 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001072 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001073 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001074 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001075 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001076 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1077 Func, Unit);
1078}
David Blaikie2aaf0652013-01-07 22:24:59 +00001079
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1081 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001082 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001083 llvm::DITypeRefArray Args(
1084 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001085 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001086 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001087
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001088 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001089
1090 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001091 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001092
David Blaikie2aaf0652013-01-07 22:24:59 +00001093 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001094 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001095 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1096 // Create pointer type directly in this case.
1097 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1098 QualType PointeeTy = ThisPtrTy->getPointeeType();
1099 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001100 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001101 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1103 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001104 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001105 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001106 // TODO: This and the artificial type below are misleading, the
1107 // types aren't artificial the argument is, but the current
1108 // metadata doesn't represent that.
1109 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1110 Elts.push_back(ThisPtrType);
1111 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001112 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001113 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001114 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1115 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 }
1117
1118 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001119 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1120 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001121
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001123
Adrian Prantl0630eb72013-12-18 21:48:18 +00001124 unsigned Flags = 0;
1125 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001126 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001127 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001128 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001129
Reid Klecknerf00f8032016-06-08 20:41:54 +00001130 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1131 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001132}
1133
Eric Christopherb2a008c2013-05-16 00:45:12 +00001134/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001135/// inside a function.
1136static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1137 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1138 return isFunctionLocalClass(NRD);
1139 if (isa<FunctionDecl>(RD->getDeclContext()))
1140 return true;
1141 return false;
1142}
1143
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001144llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1145 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001146 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001147 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001151
1152 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1153 // make sense to give a single ctor/dtor a linkage name.
1154 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001155 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1156 // property to use here. It may've been intended to model "is non-external
1157 // type" but misses cases of non-function-local but non-external classes such
1158 // as those in anonymous namespaces as well as the reverse - external types
1159 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001160 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1161 MethodLinkageName = CGM.getMangledName(Method);
1162
1163 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001164 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001165 unsigned MethodLine = 0;
1166 if (!Method->isImplicit()) {
1167 MethodDefUnit = getOrCreateFile(Method->getLocation());
1168 MethodLine = getLineNumber(Method->getLocation());
1169 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001170
1171 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001172 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001173 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001175
Guy Benyei11169dd2012-12-18 14:30:41 +00001176 if (Method->isVirtual()) {
1177 if (Method->isPure())
1178 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1179 else
1180 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001181
Reid Kleckner216d0a12016-06-16 20:08:51 +00001182 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1183 // It doesn't make sense to give a virtual destructor a vtable index,
1184 // since a single destructor has two entries in the vtable.
1185 if (!isa<CXXDestructorDecl>(Method))
1186 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1187 } else {
1188 // Emit MS ABI vftable information. There is only one entry for the
1189 // deleting dtor.
1190 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1191 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1192 MicrosoftVTableContext::MethodVFTableLocation ML =
1193 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1194 VIndex = ML.Index;
1195 // FIXME: Pass down ML.VFPtrOffset and ML.VBTableIndex. The debugger needs
1196 // these to synthesize a call to a virtual method in a complex inheritance
1197 // hierarchy.
1198 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 ContainingType = RecordTy;
1200 }
1201
1202 unsigned Flags = 0;
1203 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001204 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001205 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001206 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1207 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001208 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001209 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001210 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001211 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001213 }
1214 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001215 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001216 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001218 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001219 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001220
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001221 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1222 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001223 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1224 MethodTy, /*isLocalToUnit=*/false,
1225 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001226 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001227
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001228 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001229
1230 return SP;
1231}
1232
Eric Christophere7b87e52014-10-26 23:40:33 +00001233void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1235 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001236
1237 // Since we want more than just the individual member decls if we
1238 // have templated functions iterate over every declaration to gather
1239 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001240 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001241 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1242 // If the member is implicit, don't add it to the member list. This avoids
1243 // the member being added to type units by LLVM, while still allowing it
1244 // to be emitted into the type declaration/reference inside the compile
1245 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001246 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001247 // FIXME: Handle Using(Shadow?)Decls here to create
1248 // DW_TAG_imported_declarations inside the class for base decls brought into
1249 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1250 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1251 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001252 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001253 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001254
1255 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1256 continue;
1257
David Blaikiefd580722014-10-06 05:18:55 +00001258 // Reuse the existing member function declaration if it exists.
1259 // It may be associated with the declaration of the type & should be
1260 // reused as we're building the definition.
1261 //
1262 // This situation can arise in the vtable-based debug info reduction where
1263 // implicit members are emitted in a non-vtable TU.
1264 auto MI = SPCache.find(Method->getCanonicalDecl());
1265 EltTys.push_back(MI == SPCache.end()
1266 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001267 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001268 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001269}
Guy Benyei11169dd2012-12-18 14:30:41 +00001270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001272 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001273 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001274 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001275 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001276 unsigned BFlags = 0;
1277 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001278
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001280 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001281
Aaron Ballman574705e2014-03-13 15:41:46 +00001282 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001283 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1284 // virtual base offset offset is -ve. The code generator emits dwarf
1285 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001286 BaseOffset = 0 - CGM.getItaniumVTableContext()
1287 .getVirtualBaseOffsetOffset(RD, Base)
1288 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001289 } else {
1290 // In the MS ABI, store the vbtable offset, which is analogous to the
1291 // vbase offset offset in Itanium.
1292 BaseOffset =
1293 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1294 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001295 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001296 } else
1297 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1298 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1299 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001300
Adrian Prantl21361fb2014-08-29 22:44:27 +00001301 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001302 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001303 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001304 EltTys.push_back(DTy);
1305 }
1306}
1307
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001308llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001309CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1310 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001311 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001312 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1314 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001315 StringRef Name;
1316 if (TPList)
1317 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001318 switch (TA.getKind()) {
1319 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001320 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001321 TemplateParams.push_back(
1322 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001323 } break;
1324 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001325 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001326 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1327 TheCU, Name, TTy,
1328 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001329 } break;
1330 case TemplateArgument::Declaration: {
1331 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001332 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001334 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001335 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001336 // Variable pointer template parameters have a value that is the address
1337 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001338 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001339 V = CGM.GetAddrOfGlobalVar(VD);
1340 // Member function pointers have special support for building them, though
1341 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001342 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001343 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001344 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001345 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001346 // Member data pointers have special handling too to compute the fixed
1347 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001348 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001349 // These five lines (& possibly the above member function pointer
1350 // handling) might be able to be refactored to use similar code in
1351 // CodeGenModule::getMemberPointerConstant
1352 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1353 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001354 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001355 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001356 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001357 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1358 TheCU, Name, TTy,
1359 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001360 } break;
1361 case TemplateArgument::NullPtr: {
1362 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001363 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001364 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001365 // Special case member data pointer null values since they're actually -1
1366 // instead of zero.
1367 if (const MemberPointerType *MPT =
1368 dyn_cast<MemberPointerType>(T.getTypePtr()))
1369 // But treat member function pointers as simple zero integers because
1370 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1371 // CodeGen grows handling for values of non-null member function
1372 // pointers then perhaps we could remove this special case and rely on
1373 // EmitNullMemberPointer for member function pointers.
1374 if (MPT->isMemberDataPointer())
1375 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1376 if (!V)
1377 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001378 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1379 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001380 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001381 case TemplateArgument::Template:
1382 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001383 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001384 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1385 break;
1386 case TemplateArgument::Pack:
1387 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1388 TheCU, Name, nullptr,
1389 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1390 break;
David Majnemer5559d472013-08-24 08:21:10 +00001391 case TemplateArgument::Expression: {
1392 const Expr *E = TA.getAsExpr();
1393 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001394 if (E->isGLValue())
1395 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001396 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001397 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001399 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1400 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001401 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001402 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001403 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001404 case TemplateArgument::Null:
1405 llvm_unreachable(
1406 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 }
1408 }
1409 return DBuilder.getOrCreateArray(TemplateParams);
1410}
1411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001413CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 if (FD->getTemplatedKind() ==
1416 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001417 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1418 ->getTemplate()
1419 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001420 return CollectTemplateParams(
1421 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001423 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001424}
1425
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1427 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001428 // Always get the full list of parameters, not just the ones from
1429 // the specialization.
1430 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001431 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001432 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001433 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001434}
1435
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001436llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001437 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 return VTablePtrType;
1439
1440 ASTContext &Context = CGM.getContext();
1441
1442 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001443 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001444 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001445 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001446 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001448 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001449 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1450 return VTablePtrType;
1451}
1452
Guy Benyei11169dd2012-12-18 14:30:41 +00001453StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001454 // Copy the gdb compatible name on the side and use its reference.
1455 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001456}
1457
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001458void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001459 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001460 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1461
1462 // If there is a primary base then it will hold vtable info.
1463 if (RL.getPrimaryBase())
1464 return;
1465
1466 // If this class is not dynamic then there is not any vtable info to collect.
1467 if (!RD->isDynamicClass())
1468 return;
1469
1470 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001471 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001472 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001473 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001474 EltTys.push_back(VPTR);
1475}
1476
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001477llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001478 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001479 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001480 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 return T;
1482}
1483
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001484llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001485 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001486 return getOrCreateStandaloneType(D, Loc);
1487}
1488
1489llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1490 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001491 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001492 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001493 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001494 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001495
Adrian Prantl73409ce2013-03-11 18:33:46 +00001496 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001497 return T;
1498}
1499
David Blaikie483a9da2014-05-06 18:35:21 +00001500void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001501 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001502 return;
1503 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001504 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001505 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001506 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001507 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001509 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001510 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001511}
1512
David Blaikieb2e86eb2013-08-15 20:49:17 +00001513void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001514 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001515 !CGM.getLangOpts().CPlusPlus)
1516 completeRequiredType(RD);
1517}
1518
1519void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001520 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00001521 return;
1522
David Blaikie6943dea2013-08-20 01:28:15 +00001523 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1524 if (CXXDecl->isDynamicClass())
1525 return;
1526
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001527 if (DebugTypeExtRefs && RD->isFromASTFile())
1528 return;
1529
David Blaikieb2e86eb2013-08-15 20:49:17 +00001530 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001531 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001532 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001533 completeClassData(RD);
1534}
1535
1536void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001537 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001538 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001539 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001540 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001541 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001542 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001543 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001545 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001546 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001547}
1548
David Blaikie0e716b42014-03-03 23:48:23 +00001549static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1550 CXXRecordDecl::method_iterator End) {
1551 for (; I != End; ++I)
1552 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001553 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1554 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001555 return true;
1556 return false;
1557}
1558
Adrian Prantl05fefa42016-04-25 20:52:40 +00001559/// Does a type definition exist in an imported clang module?
1560static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001561 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001562 return false;
1563 if (!RD->isExternallyVisible() && RD->getName().empty())
1564 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001565 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1566 assert(CXXDecl->isCompleteDefinition() && "incomplete record definition");
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001567 if (CXXDecl->getTemplateSpecializationKind() != TSK_Undeclared)
1568 // Make sure the instantiation is actually in a module.
1569 if (CXXDecl->field_begin() != CXXDecl->field_end())
1570 return CXXDecl->field_begin()->isFromASTFile();
Adrian Prantl94913712016-04-26 23:42:43 +00001571 }
Adrian Prantl0dabc2a2016-04-26 23:37:38 +00001572
Adrian Prantl05fefa42016-04-25 20:52:40 +00001573 return true;
1574}
1575
Benjamin Kramer8c305922016-02-02 11:06:51 +00001576static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1577 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001578 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001579 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001580 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001581
Benjamin Kramer8c305922016-02-02 11:06:51 +00001582 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001583 return false;
1584
1585 if (!LangOpts.CPlusPlus)
1586 return false;
1587
1588 if (!RD->isCompleteDefinitionRequired())
1589 return true;
1590
1591 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1592
1593 if (!CXXDecl)
1594 return false;
1595
1596 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1597 return true;
1598
1599 TemplateSpecializationKind Spec = TSK_Undeclared;
1600 if (const ClassTemplateSpecializationDecl *SD =
1601 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1602 Spec = SD->getSpecializationKind();
1603
1604 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1605 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1606 CXXDecl->method_end()))
1607 return true;
1608
1609 return false;
1610}
1611
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001612llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001613 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001614 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001615 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1616 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001617 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001618 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001619 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001620 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001621
David Blaikieb2e86eb2013-08-15 20:49:17 +00001622 return CreateTypeDefinition(Ty);
1623}
1624
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001625llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001626 RecordDecl *RD = Ty->getDecl();
1627
Guy Benyei11169dd2012-12-18 14:30:41 +00001628 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001629 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001630
1631 // Records and classes and unions can all be recursive. To handle them, we
1632 // first generate a debug descriptor for the struct as a forward declaration.
1633 // Then (if it is a definition) we go through and get debug info for all of
1634 // its members. Finally, we create a descriptor for the complete type (which
1635 // may refer to the forward decl if the struct is recursive) and replace all
1636 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001637 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001638
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001639 const RecordDecl *D = RD->getDefinition();
1640 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001641 return FwdDecl;
1642
David Blaikieadfbf992013-08-18 16:55:33 +00001643 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1644 CollectContainingType(CXXDecl, FwdDecl);
1645
Guy Benyei11169dd2012-12-18 14:30:41 +00001646 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001647 LexicalBlockStack.emplace_back(&*FwdDecl);
1648 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001649
Guy Benyei11169dd2012-12-18 14:30:41 +00001650 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001651 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001652 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001653
1654 // Note: The split of CXXDecl information here is intentional, the
1655 // gdb tests will depend on a certain ordering at printout. The debug
1656 // information offsets are still correct if we merge them all together
1657 // though.
1658 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1659 if (CXXDecl) {
1660 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1661 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1662 }
1663
Eric Christopher91a31902013-01-16 01:22:32 +00001664 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001665 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001666 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001668
1669 LexicalBlockStack.pop_back();
1670 RegionMap.erase(Ty->getDecl());
1671
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001672 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001673 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001675 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001676 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001678
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001679 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001680 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001681}
1682
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001683llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1684 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 // Ignore protocols.
1686 return getOrCreateType(Ty->getBaseType(), Unit);
1687}
1688
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001689/// \return true if Getter has the default name for the property PD.
1690static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1691 const ObjCMethodDecl *Getter) {
1692 assert(PD);
1693 if (!Getter)
1694 return true;
1695
1696 assert(Getter->getDeclName().isObjCZeroArgSelector());
1697 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001698 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001699}
1700
1701/// \return true if Setter has the default name for the property PD.
1702static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1703 const ObjCMethodDecl *Setter) {
1704 assert(PD);
1705 if (!Setter)
1706 return true;
1707
1708 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001709 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001710 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001711}
1712
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001713llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1714 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 ObjCInterfaceDecl *ID = Ty->getDecl();
1716 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001717 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001718
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001719 // Return a forward declaration if this type was imported from a clang module,
1720 // and this is not the compile unit with the implementation of the type (which
1721 // may contain hidden ivars).
1722 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1723 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001724 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1725 ID->getName(),
1726 getDeclContextDescriptor(ID), Unit, 0);
1727
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001731 auto RuntimeLang =
1732 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001733
1734 // If this is just a forward declaration return a special forward-declaration
1735 // debug type since we won't be able to lay out the entire type.
1736 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001737 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001738 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001739 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001740 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1741 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001742 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001743 return FwdDecl;
1744 }
1745
David Blaikieef8a9512014-05-05 23:23:53 +00001746 return CreateTypeDefinition(Ty, Unit);
1747}
1748
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001749llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001750CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1751 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001752 // Use the Module pointer as the key into the cache. This is a
1753 // nullptr if the "Module" is a PCH, which is safe because we don't
1754 // support chained PCH debug info, so there can only be a single PCH.
1755 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001756 auto ModRef = ModuleCache.find(M);
1757 if (ModRef != ModuleCache.end())
1758 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001759
1760 // Macro definitions that were defined with "-D" on the command line.
1761 SmallString<128> ConfigMacros;
1762 {
1763 llvm::raw_svector_ostream OS(ConfigMacros);
1764 const auto &PPOpts = CGM.getPreprocessorOpts();
1765 unsigned I = 0;
1766 // Translate the macro definitions back into a commmand line.
1767 for (auto &M : PPOpts.Macros) {
1768 if (++I > 1)
1769 OS << " ";
1770 const std::string &Macro = M.first;
1771 bool Undef = M.second;
1772 OS << "\"-" << (Undef ? 'U' : 'D');
1773 for (char c : Macro)
1774 switch (c) {
1775 case '\\' : OS << "\\\\"; break;
1776 case '"' : OS << "\\\""; break;
1777 default: OS << c;
1778 }
1779 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001780 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001781 }
Adrian Prantl66689202015-09-18 23:01:45 +00001782
Adrian Prantl835e6632015-09-24 16:10:10 +00001783 bool IsRootModule = M ? !M->Parent : true;
1784 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001785 // PCH files don't have a signature field in the control block,
1786 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001787 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001788 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001789 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001790 Mod.getPath(), TheCU->getProducer(), true,
1791 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001792 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001793 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001794 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001795 llvm::DIModule *Parent =
1796 IsRootModule ? nullptr
1797 : getOrCreateModuleRef(
1798 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1799 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001800 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001801 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1802 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001803 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001804 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001805}
1806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001807llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1808 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001809 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001810 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001811 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001812 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001813
1814 // Bit size, align and offset of the type.
1815 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1816 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1817
1818 unsigned Flags = 0;
1819 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001820 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001821
Adrian Prantlfd696112015-10-01 00:48:51 +00001822 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001823 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001824 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1825 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001826
David Blaikieef8a9512014-05-05 23:23:53 +00001827 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001828 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001829
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001830 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001831 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001832 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001833
1834 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001835 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001836
1837 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1838 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001839 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001840 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001841 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001842 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001843
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001844 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001845 EltTys.push_back(InhTag);
1846 }
1847
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001848 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00001849 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001850 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001851 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 unsigned PLine = getLineNumber(Loc);
1853 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1854 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001855 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1856 PD->getName(), PUnit, PLine,
1857 hasDefaultGetterName(PD, Getter) ? ""
1858 : getSelectorName(PD->getGetterName()),
1859 hasDefaultSetterName(PD, Setter) ? ""
1860 : getSelectorName(PD->getSetterName()),
1861 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00001863 };
1864 {
1865 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00001866 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00001867 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00001868 PropertySet.insert(PD->getIdentifier());
1869 AddProperty(PD);
1870 }
Manman Renefe1bac2016-01-27 20:00:32 +00001871 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00001872 // Don't emit duplicate metadata for properties that were already in a
1873 // class extension.
1874 if (!PropertySet.insert(PD->getIdentifier()).second)
1875 continue;
1876 AddProperty(PD);
1877 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 }
1879
1880 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1881 unsigned FieldNo = 0;
1882 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1883 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001884 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001885 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001886 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001887
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 StringRef FieldName = Field->getName();
1889
1890 // Ignore unnamed fields.
1891 if (FieldName.empty())
1892 continue;
1893
1894 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001895 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001896 unsigned FieldLine = getLineNumber(Field->getLocation());
1897 QualType FType = Field->getType();
1898 uint64_t FieldSize = 0;
1899 unsigned FieldAlign = 0;
1900
1901 if (!FType->isIncompleteArrayType()) {
1902
1903 // Bit size, align and offset of the type.
1904 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001905 ? Field->getBitWidthValue(CGM.getContext())
1906 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 FieldAlign = CGM.getContext().getTypeAlign(FType);
1908 }
1909
1910 uint64_t FieldOffset;
1911 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1912 // We don't know the runtime offset of an ivar if we're using the
1913 // non-fragile ABI. For bitfields, use the bit offset into the first
1914 // byte of storage of the bitfield. For other fields, use zero.
1915 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001916 FieldOffset =
1917 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 FieldOffset %= CGM.getContext().getCharWidth();
1919 } else {
1920 FieldOffset = 0;
1921 }
1922 } else {
1923 FieldOffset = RL.getFieldOffset(FieldNo);
1924 }
1925
1926 unsigned Flags = 0;
1927 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001928 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001929 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001931 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001932 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001933
Craig Topper8a13c412014-05-21 05:09:00 +00001934 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001936 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001937 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001938 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001939 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001940 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001941 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1943 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001944 PropertyNode = DBuilder.createObjCProperty(
1945 PD->getName(), PUnit, PLine,
1946 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1947 PD->getGetterName()),
1948 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1949 PD->getSetterName()),
1950 PD->getPropertyAttributes(),
1951 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 }
1953 }
1954 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001955 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1956 FieldSize, FieldAlign, FieldOffset, Flags,
1957 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001958 EltTys.push_back(FieldTy);
1959 }
1960
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001961 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001962 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001963
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001965 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001966}
1967
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1969 llvm::DIFile *Unit) {
1970 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001971 int64_t Count = Ty->getNumElements();
1972 if (Count == 0)
1973 // If number of elements are not known then this is an unbounded array.
1974 // Use Count == -1 to express such arrays.
1975 Count = -1;
1976
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001977 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001978 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001979
1980 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1981 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1982
1983 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1984}
1985
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 uint64_t Size;
1988 uint64_t Align;
1989
1990 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1991 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1992 Size = 0;
1993 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001994 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001995 } else if (Ty->isIncompleteArrayType()) {
1996 Size = 0;
1997 if (Ty->getElementType()->isIncompleteType())
1998 Align = 0;
1999 else
2000 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00002001 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 Size = 0;
2003 Align = 0;
2004 } else {
2005 // Size and align of the whole array, not the element type.
2006 Size = CGM.getContext().getTypeSize(Ty);
2007 Align = CGM.getContext().getTypeAlign(Ty);
2008 }
2009
2010 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2011 // interior arrays, do we care? Why aren't nested arrays represented the
2012 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002013 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 QualType EltTy(Ty, 0);
2015 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2016 // If the number of elements is known, then count is that number. Otherwise,
2017 // it's -1. This allows us to represent a subrange with an array of 0
2018 // elements, like this:
2019 //
2020 // struct foo {
2021 // int x[0];
2022 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002023 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
2025 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002026
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 // FIXME: Verify this is right for VLAs.
2028 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2029 EltTy = Ty->getElementType();
2030 }
2031
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002032 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002033
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002034 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2035 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002036}
2037
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002038llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2039 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002040 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2041 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002042}
2043
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002044llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2045 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002046 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2047 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002048}
2049
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002050llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2051 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00002052 uint64_t Size =
2053 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002054 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002055 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002056 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00002057 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002058
2059 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002060 Ty->getPointeeType()->getAs<FunctionProtoType>();
2061 return DBuilder.createMemberPointerType(
2062 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2063 Ty->getClass(), FPT->getTypeQuals())),
2064 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00002065 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00002066}
2067
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002068llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002069 // Ignore the atomic wrapping
2070 // FIXME: What is the correct representation?
2071 return getOrCreateType(Ty->getValueType(), U);
2072}
2073
Xiuli Pan9c14e282016-01-09 12:53:17 +00002074llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2075 llvm::DIFile *U) {
2076 return getOrCreateType(Ty->getElementType(), U);
2077}
2078
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002079llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002080 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002081
Guy Benyei11169dd2012-12-18 14:30:41 +00002082 uint64_t Size = 0;
2083 uint64_t Align = 0;
2084 if (!ED->getTypeForDecl()->isIncompleteType()) {
2085 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2086 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2087 }
2088
Manman Rene0064d82013-08-29 23:19:58 +00002089 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2090
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002091 bool isImportedFromModule =
2092 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2093
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 // If this is just a forward declaration, construct an appropriately
2095 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002096 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002097 // Note that it is possible for enums to be created as part of
2098 // their own declcontext. In this case a FwdDecl will be created
2099 // twice. This doesn't cause a problem because both FwdDecls are
2100 // entered into the ReplaceMap: finalize() will replace the first
2101 // FwdDecl with the second and then replace the second with
2102 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002103 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002104 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002105 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2106 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002107
Guy Benyei11169dd2012-12-18 14:30:41 +00002108 unsigned Line = getLineNumber(ED->getLocation());
2109 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002110 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002111 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2112 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002113
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002114 ReplaceMap.emplace_back(
2115 std::piecewise_construct, std::make_tuple(Ty),
2116 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002117 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002118 }
2119
David Blaikie483a9da2014-05-06 18:35:21 +00002120 return CreateTypeDefinition(Ty);
2121}
2122
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002123llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002124 const EnumDecl *ED = Ty->getDecl();
2125 uint64_t Size = 0;
2126 uint64_t Align = 0;
2127 if (!ED->getTypeForDecl()->isIncompleteType()) {
2128 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2129 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2130 }
2131
2132 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2133
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002134 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002135 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002137 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002138 Enumerators.push_back(DBuilder.createEnumerator(
2139 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 }
2141
2142 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002143 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002144
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002145 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002147 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002148 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002149 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2150 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2151 Line, Size, Align, EltArray, ClassTy,
2152 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002153}
2154
David Blaikie05491062013-01-21 04:37:12 +00002155static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2156 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002157 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002158 Qualifiers InnerQuals = T.getLocalQualifiers();
2159 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2160 // that is already there.
2161 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2162 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 QualType LastT = T;
2164 switch (T->getTypeClass()) {
2165 default:
David Blaikie05491062013-01-21 04:37:12 +00002166 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002167 case Type::TemplateSpecialization: {
2168 const auto *Spec = cast<TemplateSpecializationType>(T);
2169 if (Spec->isTypeAlias())
2170 return C.getQualifiedType(T.getTypePtr(), Quals);
2171 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002172 break;
2173 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 case Type::TypeOfExpr:
2175 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2176 break;
2177 case Type::TypeOf:
2178 T = cast<TypeOfType>(T)->getUnderlyingType();
2179 break;
2180 case Type::Decltype:
2181 T = cast<DecltypeType>(T)->getUnderlyingType();
2182 break;
2183 case Type::UnaryTransform:
2184 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2185 break;
2186 case Type::Attributed:
2187 T = cast<AttributedType>(T)->getEquivalentType();
2188 break;
2189 case Type::Elaborated:
2190 T = cast<ElaboratedType>(T)->getNamedType();
2191 break;
2192 case Type::Paren:
2193 T = cast<ParenType>(T)->getInnerType();
2194 break;
David Blaikie05491062013-01-21 04:37:12 +00002195 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002197 break;
2198 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002199 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002200 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002201 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 break;
2203 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002204
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002206 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 } while (true);
2208}
2209
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002210llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002211
2212 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002213 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002214
David Blaikief427b002014-05-06 03:42:01 +00002215 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 if (it != TypeCache.end()) {
2217 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002218 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002219 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 }
2221
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002222 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002223}
2224
David Blaikie0e716b42014-03-03 23:48:23 +00002225void CGDebugInfo::completeTemplateDefinition(
2226 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002227 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002228 return;
2229
David Blaikie0e716b42014-03-03 23:48:23 +00002230 completeClassData(&SD);
2231 // In case this type has no member function definitions being emitted, ensure
2232 // it is retained
2233 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2234}
2235
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002236llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002238 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002239
2240 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002241 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002242
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002243 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 return T;
2245
Adrian Prantlca844182015-09-11 17:23:03 +00002246 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002247 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002248
2249 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002250 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002251
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 return Res;
2253}
2254
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002255llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002256 // A forward declaration inside a module header does not belong to the module.
2257 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2258 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002259 if (DebugTypeExtRefs && D->isFromASTFile()) {
2260 // Record a reference to an imported clang module or precompiled header.
2261 auto *Reader = CGM.getContext().getExternalSource();
2262 auto Idx = D->getOwningModuleID();
2263 auto Info = Reader->getSourceDescriptor(Idx);
2264 if (Info)
2265 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2266 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002267 // We are building a clang module or a precompiled header.
2268 //
2269 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2270 // and it wouldn't be necessary to specify the parent scope
2271 // because the type is already unique by definition (it would look
2272 // like the output of -fno-standalone-debug). On the other hand,
2273 // the parent scope helps a consumer to quickly locate the object
2274 // file where the type's definition is located, so it might be
2275 // best to make this behavior a command line or debugger tuning
2276 // option.
2277 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2278 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002279 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002280 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2281 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002282 } else {
2283 // This the precompiled header being built.
2284 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002285 }
2286 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002287
Adrian Prantl9402cef2015-09-20 16:51:35 +00002288 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002289}
2290
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002291llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 // Handle qualifiers, which recursively handles what they refer to.
2293 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002294 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002295
Guy Benyei11169dd2012-12-18 14:30:41 +00002296 // Work out details of type.
2297 switch (Ty->getTypeClass()) {
2298#define TYPE(Class, Base)
2299#define ABSTRACT_TYPE(Class, Base)
2300#define NON_CANONICAL_TYPE(Class, Base)
2301#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2302#include "clang/AST/TypeNodes.def"
2303 llvm_unreachable("Dependent types cannot show up in debug information");
2304
2305 case Type::ExtVector:
2306 case Type::Vector:
2307 return CreateType(cast<VectorType>(Ty), Unit);
2308 case Type::ObjCObjectPointer:
2309 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2310 case Type::ObjCObject:
2311 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2312 case Type::ObjCInterface:
2313 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2314 case Type::Builtin:
2315 return CreateType(cast<BuiltinType>(Ty));
2316 case Type::Complex:
2317 return CreateType(cast<ComplexType>(Ty));
2318 case Type::Pointer:
2319 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002320 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002321 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002322 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002323 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002324 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002325 case Type::BlockPointer:
2326 return CreateType(cast<BlockPointerType>(Ty), Unit);
2327 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002328 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002329 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002330 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002331 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002332 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002333 case Type::FunctionProto:
2334 case Type::FunctionNoProto:
2335 return CreateType(cast<FunctionType>(Ty), Unit);
2336 case Type::ConstantArray:
2337 case Type::VariableArray:
2338 case Type::IncompleteArray:
2339 return CreateType(cast<ArrayType>(Ty), Unit);
2340
2341 case Type::LValueReference:
2342 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2343 case Type::RValueReference:
2344 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2345
2346 case Type::MemberPointer:
2347 return CreateType(cast<MemberPointerType>(Ty), Unit);
2348
2349 case Type::Atomic:
2350 return CreateType(cast<AtomicType>(Ty), Unit);
2351
Xiuli Pan9c14e282016-01-09 12:53:17 +00002352 case Type::Pipe:
2353 return CreateType(cast<PipeType>(Ty), Unit);
2354
Guy Benyei11169dd2012-12-18 14:30:41 +00002355 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002356 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2357
David Blaikie42edade2014-11-11 20:44:45 +00002358 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002359 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002360 case Type::Elaborated:
2361 case Type::Paren:
2362 case Type::SubstTemplateTypeParm:
2363 case Type::TypeOfExpr:
2364 case Type::TypeOf:
2365 case Type::Decltype:
2366 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002367 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002368 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002370
David Blaikie42edade2014-11-11 20:44:45 +00002371 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002372}
2373
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002374llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2375 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002376 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002377
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002378 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002379
2380 // We may have cached a forward decl when we could have created
2381 // a non-forward decl. Go ahead and create a non-forward decl
2382 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002383 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002384 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002385
2386 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002387 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002388
2389 // Propagate members from the declaration to the definition
2390 // CreateType(const RecordType*) will overwrite this with the members in the
2391 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002393
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002395 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002396 return Res;
2397}
2398
2399// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002400llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002401 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002402
Guy Benyei11169dd2012-12-18 14:30:41 +00002403 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002404 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002405 unsigned Line = getLineNumber(RD->getLocation());
2406 StringRef RDName = getClassName(RD);
2407
Amjad Abouddc4531e2016-04-30 01:44:38 +00002408 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002409
David Blaikied2785892013-08-18 17:36:19 +00002410 // If we ended up creating the type during the context chain construction,
2411 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002413 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002414 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002415 return T;
David Blaikied2785892013-08-18 17:36:19 +00002416
Adrian Prantl381e7552014-02-04 21:29:50 +00002417 // If this is just a forward or incomplete declaration, construct an
2418 // appropriately marked node and just return it.
2419 const RecordDecl *D = RD->getDefinition();
2420 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002421 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002422
2423 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2424 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002425
Manman Rene0064d82013-08-29 23:19:58 +00002426 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002428 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002429 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2430 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002431
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002432 // Elements of composite types usually have back to the type, creating
2433 // uniquing cycles. Distinct nodes are more efficient.
2434 switch (RealDecl->getTag()) {
2435 default:
2436 llvm_unreachable("invalid composite type tag");
2437
2438 case llvm::dwarf::DW_TAG_array_type:
2439 case llvm::dwarf::DW_TAG_enumeration_type:
2440 // Array elements and most enumeration elements don't have back references,
2441 // so they don't tend to be involved in uniquing cycles and there is some
2442 // chance of merging them when linking together two modules. Only make
2443 // them distinct if they are ODR-uniqued.
2444 if (FullName.empty())
2445 break;
2446
2447 case llvm::dwarf::DW_TAG_structure_type:
2448 case llvm::dwarf::DW_TAG_union_type:
2449 case llvm::dwarf::DW_TAG_class_type:
2450 // Immediatley resolve to a distinct node.
2451 RealDecl =
2452 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2453 break;
2454 }
2455
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002456 RegionMap[Ty->getDecl()].reset(RealDecl);
2457 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002458
David Blaikieadfbf992013-08-18 16:55:33 +00002459 if (const ClassTemplateSpecializationDecl *TSpecial =
2460 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002462 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002463 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002464}
2465
David Blaikieadfbf992013-08-18 16:55:33 +00002466void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002468 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002469 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002470 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2471 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002472 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002473 while (1) {
2474 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2475 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2476 if (PBT && !BRL.isPrimaryBaseVirtual())
2477 PBase = PBT;
2478 else
2479 break;
2480 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002481 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002482 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2483 getOrCreateFile(RD->getLocation())));
2484 } else if (RD->isDynamicClass())
2485 ContainingType = RealDecl;
2486
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002487 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002488}
2489
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002490llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002491 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2494 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002495 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002496 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 *Offset += FieldSize;
2498 return Ty;
2499}
2500
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002501void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002502 StringRef &Name,
2503 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002504 llvm::DIScope *&FDContext,
2505 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002506 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002507 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2508 Name = getFunctionName(FD);
2509 // Use mangled name as linkage name for C/C++ functions.
2510 if (FD->hasPrototype()) {
2511 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002512 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002513 }
2514 // No need to replicate the linkage name if it isn't different from the
2515 // subprogram name, no need to have it at all unless coverage is enabled or
2516 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002517 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2518 !CGM.getCodeGenOpts().EmitGcovNotes &&
2519 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002520 LinkageName = StringRef();
2521
Benjamin Kramer8c305922016-02-02 11:06:51 +00002522 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002523 if (const NamespaceDecl *NSDecl =
2524 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2525 FDContext = getOrCreateNameSpace(NSDecl);
2526 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002527 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2528 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2529 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2530 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002531 // Collect template parameters.
2532 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2533 }
2534}
2535
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002537 unsigned &LineNo, QualType &T,
2538 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002539 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002540 Unit = getOrCreateFile(VD->getLocation());
2541 LineNo = getLineNumber(VD->getLocation());
2542
2543 setLocation(VD->getLocation());
2544
2545 T = VD->getType();
2546 if (T->isIncompleteArrayType()) {
2547 // CodeGen turns int[] into int[1] so we'll do the same here.
2548 llvm::APInt ConstVal(32, 1);
2549 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2550
2551 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2552 ArrayType::Normal, 0);
2553 }
2554
2555 Name = VD->getName();
2556 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2557 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2558 LinkageName = CGM.getMangledName(VD);
2559 if (LinkageName == Name)
2560 LinkageName = StringRef();
2561
2562 // Since we emit declarations (DW_AT_members) for static members, place the
2563 // definition of those static members in the namespace they were declared in
2564 // in the source code (the lexical decl context).
2565 // FIXME: Generalize this for even non-member global variables where the
2566 // declaration and definition may have different lexical decl contexts, once
2567 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002568 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2569 : VD->getDeclContext();
2570 // When a record type contains an in-line initialization of a static data
2571 // member, and the record type is marked as __declspec(dllexport), an implicit
2572 // definition of the member will be created in the record context. DWARF
2573 // doesn't seem to have a nice way to describe this in a form that consumers
2574 // are likely to understand, so fake the "normal" situation of a definition
2575 // outside the class by putting it in the global scope.
2576 if (DC->isRecord())
2577 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002578
Amjad Abouddc4531e2016-04-30 01:44:38 +00002579 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2580 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002581}
2582
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002583llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002584CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002585 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002586 StringRef Name, LinkageName;
2587 unsigned Flags = 0;
2588 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002589 llvm::DIFile *Unit = getOrCreateFile(Loc);
2590 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002591 unsigned Line = getLineNumber(Loc);
2592
2593 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2594 TParamsArray, Flags);
2595 // Build function type.
2596 SmallVector<QualType, 16> ArgTypes;
2597 for (const ParmVarDecl *Parm: FD->parameters())
2598 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002599 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2600 QualType FnType = CGM.getContext().getFunctionType(
2601 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002602 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002603 DContext, Name, LinkageName, Unit, Line,
2604 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002605 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002606 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002607 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002608 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2609 std::make_tuple(CanonDecl),
2610 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002611 return SP;
2612}
2613
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002614llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002615CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2616 QualType T;
2617 StringRef Name, LinkageName;
2618 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002619 llvm::DIFile *Unit = getOrCreateFile(Loc);
2620 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002621 unsigned Line = getLineNumber(Loc);
2622
2623 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002624 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2625 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2626 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002627 FwdDeclReplaceMap.emplace_back(
2628 std::piecewise_construct,
2629 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2630 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002631 return GV;
2632}
2633
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002634llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002635 // We only need a declaration (not a definition) of the type - so use whatever
2636 // we would otherwise do to get a type for a pointee. (forward declarations in
2637 // limited debug info, full definitions (if the type definition is available)
2638 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002639 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2640 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002641 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002642 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002643
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002644 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002645 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002646
2647 // No definition for now. Emit a forward definition that might be
2648 // merged with a potential upcoming definition.
2649 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2650 return getFunctionForwardDeclaration(FD);
2651 else if (const auto *VD = dyn_cast<VarDecl>(D))
2652 return getGlobalVariableForwardDeclaration(VD);
2653
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002654 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002655}
2656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002657llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002658 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002659 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002660
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002662 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002663 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002664
2665 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002666 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002667
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002668 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002669 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002670 if (const CXXMethodDecl *MD =
2671 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002672 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002673 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002674 }
2675 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002677 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002678 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 return SP;
2680 }
2681
Aaron Ballman86c93902014-03-06 23:45:36 +00002682 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002683 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002684 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002685 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002686 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002687 return SP;
2688 }
2689 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002690 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002691}
2692
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002693// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002694// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002695llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002696 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002697 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002698 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002699 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2700 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002701 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002702
2703 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2704 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002705
2706 const auto *FTy = FnType->getAs<FunctionType>();
2707 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2708
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2710 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002711 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002712
2713 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002714 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002715
2716 // Replace the instancetype keyword with the actual type.
2717 if (ResultTy == CGM.getContext().getObjCInstanceType())
2718 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002719 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002720
Adrian Prantl7bec9032013-05-10 21:08:31 +00002721 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002723 QualType SelfDeclTy;
2724 if (auto *SelfDecl = OMethod->getSelfDecl())
2725 SelfDeclTy = SelfDecl->getType();
2726 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2727 if (FPT->getNumParams() > 1)
2728 SelfDeclTy = FPT->getParamType(0);
2729 if (!SelfDeclTy.isNull())
2730 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002732 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002733 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002735 for (const auto *PI : OMethod->params())
2736 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002737 // Variadic methods need a special marker at the end of the type list.
2738 if (OMethod->isVariadic())
2739 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002740
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002741 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002742 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002744
Adrian Prantl800faef2014-02-25 23:42:18 +00002745 // Handle variadic function types; they need an additional
2746 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002747 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2748 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002749 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002750 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2751 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2752 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2753 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2754 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002755 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002756 return DBuilder.createSubroutineType(EltTypeArray, 0, getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002757 }
2758
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002759 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002760}
2761
Eric Christophere7b87e52014-10-26 23:40:33 +00002762void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2763 SourceLocation ScopeLoc, QualType FnType,
2764 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002765
2766 StringRef Name;
2767 StringRef LinkageName;
2768
2769 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2770
2771 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002772 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002773
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002775 llvm::DIFile *Unit = getOrCreateFile(Loc);
2776 llvm::DIScope *FDContext = Unit;
2777 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 if (!HasDecl) {
2779 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002780 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002782 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002783 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002785 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002786 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002787 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002788 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002789 return;
2790 }
2791 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002792 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2793 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2795 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002796 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002797 } else {
2798 // Use llvm function name.
2799 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002800 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002801 }
2802 if (!Name.empty() && Name[0] == '\01')
2803 Name = Name.substr(1);
2804
Adrian Prantl42d71b92014-04-10 23:21:53 +00002805 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002806 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002807 // Artificial functions without a location should not silently reuse CurLoc.
2808 if (Loc.isInvalid())
2809 CurLoc = SourceLocation();
2810 }
2811 unsigned LineNo = getLineNumber(Loc);
2812 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002813
Eric Christopher8018e412014-03-27 18:50:35 +00002814 // FIXME: The function declaration we're constructing here is mostly reusing
2815 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2816 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2817 // all subprograms instead of the actual context since subprogram definitions
2818 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002819 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002820 FDContext, Name, LinkageName, Unit, LineNo,
2821 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002822 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002823 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002824 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002825 // We might get here with a VarDecl in the case we're generating
2826 // code for the initialization of globals. Do not record these decls
2827 // as they will overwrite the actual VarDecl Decl in the cache.
2828 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002829 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002830
Adrian Prantlbebb8932014-03-21 21:01:58 +00002831 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002832 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002833
Guy Benyei11169dd2012-12-18 14:30:41 +00002834 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002835 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002836}
2837
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002838void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2839 QualType FnType) {
2840 StringRef Name;
2841 StringRef LinkageName;
2842
2843 const Decl *D = GD.getDecl();
2844 if (!D)
2845 return;
2846
2847 unsigned Flags = 0;
2848 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002849 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002850 llvm::DINodeArray TParamsArray;
2851 if (isa<FunctionDecl>(D)) {
2852 // If there is a DISubprogram for this function available then use it.
2853 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2854 TParamsArray, Flags);
2855 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2856 Name = getObjCMethodName(OMD);
2857 Flags |= llvm::DINode::FlagPrototyped;
2858 } else {
2859 llvm_unreachable("not a function or ObjC method");
2860 }
2861 if (!Name.empty() && Name[0] == '\01')
2862 Name = Name.substr(1);
2863
2864 if (D->isImplicit()) {
2865 Flags |= llvm::DINode::FlagArtificial;
2866 // Artificial functions without a location should not silently reuse CurLoc.
2867 if (Loc.isInvalid())
2868 CurLoc = SourceLocation();
2869 }
2870 unsigned LineNo = getLineNumber(Loc);
2871 unsigned ScopeLine = 0;
2872
Adrian Prantle76bda52016-04-15 15:55:45 +00002873 DBuilder.retainType(DBuilder.createFunction(
2874 FDContext, Name, LinkageName, Unit, LineNo,
2875 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
2876 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
2877 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002878}
2879
David Blaikie835afb22015-01-21 23:08:17 +00002880void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 // Update our current location
2882 setLocation(Loc);
2883
Eric Christophere7b87e52014-10-26 23:40:33 +00002884 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2885 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002886
Adrian Prantle83b1302014-01-07 22:05:52 +00002887 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002888 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002889 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002890}
2891
Guy Benyei11169dd2012-12-18 14:30:41 +00002892void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002893 llvm::MDNode *Back = nullptr;
2894 if (!LexicalBlockStack.empty())
2895 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002896 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002897 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002898 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002899}
2900
Eric Christopher0fdcb312013-05-16 00:52:20 +00002901void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2902 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 // Set our current location.
2904 setLocation(Loc);
2905
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002907 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2908 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002909
Benjamin Kramer8c305922016-02-02 11:06:51 +00002910 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002911 return;
2912
2913 // Create a new lexical block and push it on the stack.
2914 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002915}
2916
Eric Christopher0fdcb312013-05-16 00:52:20 +00002917void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2918 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2920
2921 // Provide an entry in the line table for the end of the block.
2922 EmitLocation(Builder, Loc);
2923
Benjamin Kramer8c305922016-02-02 11:06:51 +00002924 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00002925 return;
2926
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 LexicalBlockStack.pop_back();
2928}
2929
Guy Benyei11169dd2012-12-18 14:30:41 +00002930void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2931 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2932 unsigned RCount = FnBeginRegionCount.back();
2933 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2934
2935 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002936 while (LexicalBlockStack.size() != RCount) {
2937 // Provide an entry in the line table for the end of the block.
2938 EmitLocation(Builder, CurLoc);
2939 LexicalBlockStack.pop_back();
2940 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 FnBeginRegionCount.pop_back();
2942}
2943
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002944llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002945 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002946
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002947 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 QualType FType;
2949 uint64_t FieldSize, FieldOffset;
2950 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002951
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002952 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002953 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002954
2955 FieldOffset = 0;
2956 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2957 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2958 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2959 FType = CGM.getContext().IntTy;
2960 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2961 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2962
2963 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2964 if (HasCopyAndDispose) {
2965 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002966 EltTys.push_back(
2967 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2968 EltTys.push_back(
2969 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 }
2971 bool HasByrefExtendedLayout;
2972 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002973 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2974 HasByrefExtendedLayout) &&
2975 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002976 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002977 EltTys.push_back(
2978 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002979 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002980
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2982 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002983 CGM.getTarget().getPointerAlign(0))) {
2984 CharUnits FieldOffsetInBytes =
2985 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00002986 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00002987 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002988
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 if (NumPaddingBytes.isPositive()) {
2990 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2991 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2992 pad, ArrayType::Normal, 0);
2993 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2994 }
2995 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002996
Guy Benyei11169dd2012-12-18 14:30:41 +00002997 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002998 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 FieldSize = CGM.getContext().getTypeSize(FType);
3000 FieldAlign = CGM.getContext().toBits(Align);
3001
Eric Christopherb2a008c2013-05-16 00:45:12 +00003002 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003003 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
3004 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 EltTys.push_back(FieldTy);
3006 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003007
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003008 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003010 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003011
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003013 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003014}
3015
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003016void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3017 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003018 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003019 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003021 if (VD->hasAttr<NoDebugAttr>())
3022 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003023
David Blaikie7fceebf2013-08-19 03:37:48 +00003024 bool Unwritten =
3025 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3026 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003027 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003028 if (!Unwritten)
3029 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003030 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 uint64_t XOffset = 0;
3032 if (VD->hasAttr<BlocksAttr>())
3033 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003034 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003035 Ty = getOrCreateType(VD->getType(), Unit);
3036
3037 // If there is no debug info for this type then do not emit debug info
3038 // for this variable.
3039 if (!Ty)
3040 return;
3041
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003043 unsigned Line = 0;
3044 unsigned Column = 0;
3045 if (!Unwritten) {
3046 Line = getLineNumber(VD->getLocation());
3047 Column = getColumnNumber(VD->getLocation());
3048 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003049 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003050 unsigned Flags = 0;
3051 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003052 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 // If this is the first argument and it is implicit then
3054 // give it an object pointer flag.
3055 // FIXME: There has to be a better way to do this, but for static
3056 // functions there won't be an implicit param at arg1 and
3057 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003058 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003059 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00003060 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003061 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3062 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003063 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003064
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003065 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003066
3067 StringRef Name = VD->getName();
3068 if (!Name.empty()) {
3069 if (VD->hasAttr<BlocksAttr>()) {
3070 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003071 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 // offset of __forwarding field
3073 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003074 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003075 Expr.push_back(offset.getQuantity());
3076 Expr.push_back(llvm::dwarf::DW_OP_deref);
3077 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 // offset of x field
3079 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003080 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003081
3082 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003083 auto *D = ArgNo
3084 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3085 *ArgNo, Unit, Line, Ty)
3086 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
3087 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003088
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003090 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3091 llvm::DebugLoc::get(Line, Column, Scope),
3092 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003093 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003094 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003095 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00003096 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
3097 // If VD is an anonymous union then Storage represents value for
3098 // all union fields.
3099 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
3100 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003101 // GDB has trouble finding local variables in anonymous unions, so we emit
3102 // artifical local variables for each of the members.
3103 //
3104 // FIXME: Remove this code as soon as GDB supports this.
3105 // The debug info verifier in LLVM operates based on the assumption that a
3106 // variable has the same size as its storage and we had to disable the check
3107 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003108 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003109 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003110 StringRef FieldName = Field->getName();
3111
3112 // Ignore unnamed fields. Do not ignore unnamed records.
3113 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3114 continue;
3115
3116 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003117 auto *D = DBuilder.createAutoVariable(
3118 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
3119 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003120
3121 // Insert an llvm.dbg.declare into the current block.
3122 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3123 llvm::DebugLoc::get(Line, Column, Scope),
3124 Builder.GetInsertBlock());
3125 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003126 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003128
3129 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003130 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003131 ArgNo
3132 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3133 Ty, CGM.getLangOpts().Optimize,
3134 Flags)
3135 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3136 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003137
3138 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003139 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3140 llvm::DebugLoc::get(Line, Column, Scope),
3141 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003142}
3143
3144void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3145 llvm::Value *Storage,
3146 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003147 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003148 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003149}
3150
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003151llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3152 llvm::DIType *Ty) {
3153 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003154 if (CachedTy)
3155 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003156 return DBuilder.createObjectPointerType(Ty);
3157}
3158
Eric Christophere7b87e52014-10-26 23:40:33 +00003159void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3160 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003161 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003162 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003164
Craig Topper8a13c412014-05-21 05:09:00 +00003165 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003167 if (VD->hasAttr<NoDebugAttr>())
3168 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003169
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003171
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003173 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3174 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 if (isByRef)
3176 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003177 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003178 Ty = getOrCreateType(VD->getType(), Unit);
3179
3180 // Self is passed along as an implicit non-arg variable in a
3181 // block. Mark it as the object pointer.
3182 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003183 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
3185 // Get location information.
3186 unsigned Line = getLineNumber(VD->getLocation());
3187 unsigned Column = getColumnNumber(VD->getLocation());
3188
3189 const llvm::DataLayout &target = CGM.getDataLayout();
3190
3191 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003192 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003193 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3194
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003195 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003196 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003197 addr.push_back(llvm::dwarf::DW_OP_deref);
3198 addr.push_back(llvm::dwarf::DW_OP_plus);
3199 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003200 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003201 addr.push_back(llvm::dwarf::DW_OP_deref);
3202 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003204 offset =
3205 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003206 addr.push_back(offset.getQuantity());
3207 addr.push_back(llvm::dwarf::DW_OP_deref);
3208 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003209 // offset of x field
3210 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003211 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003212 }
3213
3214 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003215 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003216 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003217 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003218
Guy Benyei11169dd2012-12-18 14:30:41 +00003219 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003220 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3221 if (InsertPoint)
3222 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3223 InsertPoint);
3224 else
3225 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3226 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003227}
3228
Guy Benyei11169dd2012-12-18 14:30:41 +00003229void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3230 unsigned ArgNo,
3231 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003232 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003233 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003234}
3235
3236namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003237struct BlockLayoutChunk {
3238 uint64_t OffsetInBits;
3239 const BlockDecl::Capture *Capture;
3240};
3241bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3242 return l.OffsetInBits < r.OffsetInBits;
3243}
Guy Benyei11169dd2012-12-18 14:30:41 +00003244}
3245
3246void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003247 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003248 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003249 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003250 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003251 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003252 ASTContext &C = CGM.getContext();
3253 const BlockDecl *blockDecl = block.getBlockDecl();
3254
3255 // Collect some general information about the block's location.
3256 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003257 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003258 unsigned line = getLineNumber(loc);
3259 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003260
Guy Benyei11169dd2012-12-18 14:30:41 +00003261 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003262 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003263
3264 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003265 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003266
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003267 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003268 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3269 blockLayout->getElementOffsetInBits(0),
3270 tunit, tunit));
3271 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3272 blockLayout->getElementOffsetInBits(1),
3273 tunit, tunit));
3274 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3275 blockLayout->getElementOffsetInBits(2),
3276 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003277 auto *FnTy = block.getBlockExpr()->getFunctionType();
3278 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3279 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 blockLayout->getElementOffsetInBits(3),
3281 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003282 fields.push_back(createFieldType(
3283 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3284 ? C.getBlockDescriptorExtendedType()
3285 : C.getBlockDescriptorType()),
3286 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003287
3288 // We want to sort the captures by offset, not because DWARF
3289 // requires this, but because we're paranoid about debuggers.
3290 SmallVector<BlockLayoutChunk, 8> chunks;
3291
3292 // 'this' capture.
3293 if (blockDecl->capturesCXXThis()) {
3294 BlockLayoutChunk chunk;
3295 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003296 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003297 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003298 chunks.push_back(chunk);
3299 }
3300
3301 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003302 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 const VarDecl *variable = capture.getVariable();
3304 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3305
3306 // Ignore constant captures.
3307 if (captureInfo.isConstant())
3308 continue;
3309
3310 BlockLayoutChunk chunk;
3311 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003312 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003313 chunk.Capture = &capture;
3314 chunks.push_back(chunk);
3315 }
3316
3317 // Sort by offset.
3318 llvm::array_pod_sort(chunks.begin(), chunks.end());
3319
Eric Christophere7b87e52014-10-26 23:40:33 +00003320 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3321 e = chunks.end();
3322 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003323 uint64_t offsetInBits = i->OffsetInBits;
3324 const BlockDecl::Capture *capture = i->Capture;
3325
3326 // If we have a null capture, this must be the C++ 'this' capture.
3327 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003328 QualType type;
3329 if (auto *Method =
3330 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3331 type = Method->getThisType(C);
3332 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3333 type = QualType(RDecl->getTypeForDecl(), 0);
3334 else
3335 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003336
3337 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3338 offsetInBits, tunit, tunit));
3339 continue;
3340 }
3341
3342 const VarDecl *variable = capture->getVariable();
3343 StringRef name = variable->getName();
3344
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003345 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003346 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003347 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003348
3349 // FIXME: this creates a second copy of this type!
3350 uint64_t xoffset;
3351 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003352 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3353 fieldType =
3354 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3355 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003357 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3358 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003359 }
3360 fields.push_back(fieldType);
3361 }
3362
3363 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003364 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3365 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003366
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003367 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003368
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003369 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003370 tunit, typeName.str(), tunit, line,
3371 CGM.getContext().toBits(block.BlockSize),
3372 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003373 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3374
3375 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003376 unsigned flags = llvm::DINode::FlagArtificial;
3377 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003378
3379 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003380 auto *debugVar = DBuilder.createParameterVariable(
3381 scope, Arg->getName(), ArgNo, tunit, line, type,
3382 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003383
Adrian Prantl616bef42013-03-14 21:52:59 +00003384 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003385 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003386 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003387 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003388 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003389 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003390
Adrian Prantl616bef42013-03-14 21:52:59 +00003391 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003392 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3393 llvm::DebugLoc::get(line, column, scope),
3394 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003395}
3396
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003397llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003398CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3399 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003400 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003401
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003402 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003403 if (MI != StaticDataMemberCache.end()) {
3404 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003405 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003406 }
David Blaikiece763042013-08-20 21:49:21 +00003407
3408 // If the member wasn't found in the cache, lazily construct and add it to the
3409 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003410 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003411 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003412 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003413}
3414
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003415llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3416 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3417 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3418 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003419
3420 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003421 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003422 StringRef FieldName = Field->getName();
3423
3424 // Ignore unnamed fields, but recurse into anonymous records.
3425 if (FieldName.empty()) {
3426 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3427 if (RT)
3428 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3429 Var, DContext);
3430 continue;
3431 }
3432 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003433 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3434 LineNo, FieldTy,
3435 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003436 }
3437 return GV;
3438}
3439
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003440void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003441 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003442 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003443 if (D->hasAttr<NoDebugAttr>())
3444 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003445 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003446 llvm::DIFile *Unit = nullptr;
3447 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003448 unsigned LineNo;
3449 StringRef DeclName, LinkageName;
3450 QualType T;
3451 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003452
3453 // Attempt to store one global variable for the declaration - even if we
3454 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003455 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003456
3457 // If this is an anonymous union then we'll want to emit a global
3458 // variable for each member of the anonymous union so that it's possible
3459 // to find the name of any field in the union.
3460 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003461 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003462 assert(RD->isAnonymousStructOrUnion() &&
3463 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003464 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3465 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003466 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003467 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3468 Var->hasInternalLinkage(), Var,
3469 getOrCreateStaticDataMemberDeclarationOrNull(D));
3470 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003471 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003472}
3473
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003474void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3475 llvm::Constant *Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003476 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003477 if (VD->hasAttr<NoDebugAttr>())
3478 return;
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003479 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003480 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003481 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003482 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003483 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3484 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3485 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3486 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3487 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003488 // Do not use global variables for enums.
3489 //
3490 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003491 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003492 return;
David Blaikiea15565562014-04-04 20:56:17 +00003493 // Do not emit separate definitions for function local const/statics.
3494 if (isa<FunctionDecl>(VD->getDeclContext()))
3495 return;
David Blaikiebb113912014-04-05 07:23:17 +00003496 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003497 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003498 if (VarD->isStaticDataMember()) {
3499 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003500 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003501 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003502 //
3503 // FIXME: This is probably unnecessary, since Ty should reference RD
3504 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003505 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003506 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003507 return;
3508 }
3509
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003510 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003511
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003512 auto &GV = DeclCache[VD];
3513 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003514 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003515 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003516 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003517 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003518}
3519
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003520llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003521 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003522 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003523 llvm::DIScope *Mod = getParentModuleOrNull(D);
3524 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003525}
3526
David Blaikie9f88fe82013-04-22 06:13:21 +00003527void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003528 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003529 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003530 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
3531 if (!NSDecl->isAnonymousNamespace() ||
3532 CGM.getCodeGenOpts().DebugExplicitImport) {
3533 DBuilder.createImportedModule(
3534 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3535 getOrCreateNameSpace(NSDecl),
3536 getLineNumber(UD.getLocation()));
3537 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003538}
3539
David Blaikiebd483762013-05-20 04:58:53 +00003540void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003541 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003542 return;
3543 assert(UD.shadow_size() &&
3544 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3545 // Emitting one decl is sufficient - debuggers can detect that this is an
3546 // overloaded name & provide lookup for all the overloads.
3547 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003548 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003549 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003550 DBuilder.createImportedDeclaration(
3551 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3552 getLineNumber(USD.getLocation()));
3553}
3554
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003555void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003556 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3557 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003558 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003559 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003560 DBuilder.createImportedDeclaration(
3561 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3562 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3563 getLineNumber(ID.getLocation()));
3564 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003565}
3566
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003567llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003568CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003569 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003570 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003571 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003572 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003573 return cast<llvm::DIImportedEntity>(VH);
3574 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003575 if (const NamespaceAliasDecl *Underlying =
3576 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3577 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003578 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003579 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3580 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3581 NA.getName());
3582 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003583 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003584 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3585 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3586 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003587 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003588 return R;
3589}
3590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003591llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003592CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003593 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003594 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003596 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003597
Guy Benyei11169dd2012-12-18 14:30:41 +00003598 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003599 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003600 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003601 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003602 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003603 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003604 return NS;
3605}
3606
Adrian Prantla5206ce2015-09-22 23:26:43 +00003607void CGDebugInfo::setDwoId(uint64_t Signature) {
3608 assert(TheCU && "no main compile unit");
3609 TheCU->setDWOId(Signature);
3610}
3611
3612
Guy Benyei11169dd2012-12-18 14:30:41 +00003613void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003614 // Creating types might create further types - invalidating the current
3615 // element and the size(), so don't cache/reference them.
3616 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3617 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003618 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003619 ? CreateTypeDefinition(E.Type, E.Unit)
3620 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003621 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003622 }
3623
David Blaikief427b002014-05-06 03:42:01 +00003624 for (auto p : ReplaceMap) {
3625 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003626 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003627 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003628
David Blaikief427b002014-05-06 03:42:01 +00003629 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003630 assert(it != TypeCache.end());
3631 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003632
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003633 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3634 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003636
Frederic Rissd253ed62014-11-18 03:40:51 +00003637 for (const auto &p : FwdDeclReplaceMap) {
3638 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003639 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003640 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003641
3642 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003643 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003644 // with ourselves, that will destroy the temporary MDNode and
3645 // replace it with a standard one, avoiding leaking memory.
3646 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003647 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003648 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003649 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003650
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003651 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003652 }
3653
Adrian Prantl73409ce2013-03-11 18:33:46 +00003654 // We keep our own list of retained types, because we need to look
3655 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003656 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003657 if (auto MD = TypeCache[RT])
3658 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003659
Guy Benyei11169dd2012-12-18 14:30:41 +00003660 DBuilder.finalize();
3661}
David Blaikie66088d52014-09-24 17:01:27 +00003662
3663void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003664 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003665 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003666
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003667 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003668 // Don't ignore in case of explicit cast where it is referenced indirectly.
3669 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003670}