blob: 9b53152fac40b39f39c1b9c6b2b7af206890e49c [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"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000030#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000031#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000032#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000034#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000035#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000037#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000044#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045using namespace clang;
46using namespace clang::CodeGen;
47
Victor Leschuka7ece032016-10-20 00:13:19 +000048static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
49 auto TI = Ctx.getTypeInfo(Ty);
50 return TI.AlignIsRequired ? TI.Align : 0;
51}
52
53static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
54 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
55}
56
57static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
58 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
59}
60
Guy Benyei11169dd2012-12-18 14:30:41 +000061CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000062 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000063 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000064 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000065 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
66 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000067 CreateCompileUnit();
68}
69
70CGDebugInfo::~CGDebugInfo() {
71 assert(LexicalBlockStack.empty() &&
72 "Region stack mismatch, stack not empty!");
73}
74
David Blaikie66e41972015-01-14 07:38:27 +000075ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000076 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000077 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000078 init(TemporaryLocation);
79}
80
Adrian Prantl39428e72015-02-03 18:40:42 +000081ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000082 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000083 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000084 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000085 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000086}
87
88void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000089 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000090 auto *DI = CGF->getDebugInfo();
91 if (!DI) {
92 CGF = nullptr;
93 return;
David Blaikie66e41972015-01-14 07:38:27 +000094 }
David Blaikied7057d92015-08-12 23:49:57 +000095
96 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
97 if (TemporaryLocation.isValid()) {
98 DI->EmitLocation(CGF->Builder, TemporaryLocation);
99 return;
100 }
101
102 if (DefaultToEmpty) {
103 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
104 return;
105 }
106
107 // Construct a location that has a valid scope, but no line info.
108 assert(!DI->LexicalBlockStack.empty());
109 CGF->Builder.SetCurrentDebugLocation(
110 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000111}
112
David Blaikie9b479662015-01-25 01:19:10 +0000113ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000114 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000115 init(E->getExprLoc());
116}
117
David Blaikie66e41972015-01-14 07:38:27 +0000118ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000119 : CGF(&CGF) {
120 if (!CGF.getDebugInfo()) {
121 this->CGF = nullptr;
122 return;
David Blaikie66e41972015-01-14 07:38:27 +0000123 }
David Blaikied7057d92015-08-12 23:49:57 +0000124 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
125 if (Loc)
126 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000127}
128
129ApplyDebugLocation::~ApplyDebugLocation() {
130 // Query CGF so the location isn't overwritten when location updates are
131 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000132 if (CGF)
133 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000134}
135
Guy Benyei11169dd2012-12-18 14:30:41 +0000136void CGDebugInfo::setLocation(SourceLocation Loc) {
137 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000138 if (Loc.isInvalid())
139 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000140
141 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
142
143 // If we've changed files in the middle of a lexical scope go ahead
144 // and create a new lexical scope with file node if it's different
145 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000146 if (LexicalBlockStack.empty())
147 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000148
149 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000150 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000152
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000153 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000154 return;
155
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000156 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000157 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000158 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
159 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000160 } else if (isa<llvm::DILexicalBlock>(Scope) ||
161 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000163 LexicalBlockStack.emplace_back(
164 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 }
166}
167
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000168llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000169 llvm::DIScope *Mod = getParentModuleOrNull(D);
170 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
171 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000172}
173
174llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
175 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000176 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000177 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000178
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000179 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000180 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000181 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000182 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 }
184
185 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000186 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000187 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000188
David Majnemer58ed0f32016-07-17 00:39:12 +0000189 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000190 if (!RDecl->isDependentType())
191 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000192 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000193 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000194}
195
Guy Benyei11169dd2012-12-18 14:30:41 +0000196StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000197 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000198 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000199 FunctionTemplateSpecializationInfo *Info =
200 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000201
Reid Kleckner31abd802016-06-30 17:41:31 +0000202 // Emit the unqualified name in normal operation. LLVM and the debugger can
203 // compute the fully qualified name from the scope chain. If we're only
204 // emitting line table info, there won't be any scope chains, so emit the
205 // fully qualified name here so that stack traces are more accurate.
206 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
207 // evaluating the size impact.
208 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
209 CGM.getCodeGenOpts().EmitCodeView;
210
211 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000212 return FII->getName();
213
Benjamin Kramer9170e912013-02-22 15:46:01 +0000214 SmallString<128> NS;
215 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000216 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000217 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000218 if (!UseQualifiedName)
219 FD->printName(OS);
220 else
221 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000222
Reid Kleckner829398e2016-06-17 16:11:20 +0000223 // Add any template specialization args.
224 if (Info) {
225 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000226 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000227 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000228 }
229
230 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000231 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000232}
233
234StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
235 SmallString<256> MethodName;
236 llvm::raw_svector_ostream OS(MethodName);
237 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
238 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000239 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000240 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000241 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000242 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000243 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000244 if (OC->IsClassExtension()) {
245 OS << OC->getClassInterface()->getName();
246 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000247 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000248 << OC->getIdentifier()->getNameStart() << ')';
249 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000250 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000251 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
252 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000253 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000254 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000255 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000256 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000257 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000258 ClassTy.print(OS, PrintingPolicy(LangOptions()));
259 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000260 }
261 OS << ' ' << OMD->getSelector().getAsString() << ']';
262
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000263 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000264}
265
Guy Benyei11169dd2012-12-18 14:30:41 +0000266StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000267 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000268}
269
Eric Christophere7b87e52014-10-26 23:40:33 +0000270StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000271 if (isa<ClassTemplateSpecializationDecl>(RD)) {
272 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000273 llvm::raw_svector_ostream OS(Name);
274 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
275 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000276
277 // Copy this name on the side and use its reference.
278 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000279 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000280
David Majnemerbc5976a2016-07-01 23:12:54 +0000281 // quick optimization to avoid having to intern strings that are already
282 // stored reliably elsewhere
283 if (const IdentifierInfo *II = RD->getIdentifier())
284 return II->getName();
285
286 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
287 // used to reconstruct the fully qualified type names.
288 if (CGM.getCodeGenOpts().EmitCodeView) {
289 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
290 assert(RD->getDeclContext() == D->getDeclContext() &&
291 "Typedef should not be in another decl context!");
292 assert(D->getDeclName().getAsIdentifierInfo() &&
293 "Typedef was not named!");
294 return D->getDeclName().getAsIdentifierInfo()->getName();
295 }
296
297 if (CGM.getLangOpts().CPlusPlus) {
298 StringRef Name;
299
300 ASTContext &Context = CGM.getContext();
301 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
302 // Anonymous types without a name for linkage purposes have their
303 // declarator mangled in if they have one.
304 Name = DD->getName();
305 else if (const TypedefNameDecl *TND =
306 Context.getTypedefNameForUnnamedTagDecl(RD))
307 // Anonymous types without a name for linkage purposes have their
308 // associate typedef mangled in if they have one.
309 Name = TND->getName();
310
311 if (!Name.empty()) {
312 SmallString<256> UnnamedType("<unnamed-type-");
313 UnnamedType += Name;
314 UnnamedType += '>';
315 return internString(UnnamedType);
316 }
317 }
318 }
319
320 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000321}
322
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000323llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 if (!Loc.isValid())
325 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000326 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
327 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000328
329 SourceManager &SM = CGM.getContext().getSourceManager();
330 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
331
332 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
333 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000334 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
335 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000336
337 // Cache the results.
338 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000339 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000340
341 if (it != DIFileCache.end()) {
342 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000343 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000344 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000345 }
346
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000347 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
348 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000349
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000350 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000351 return F;
352}
353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000354llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000355 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
356 remapDIPath(TheCU->getDirectory()));
357}
358
359std::string CGDebugInfo::remapDIPath(StringRef Path) const {
360 for (const auto &Entry : DebugPrefixMap)
361 if (Path.startswith(Entry.first))
362 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
363 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000364}
365
Guy Benyei11169dd2012-12-18 14:30:41 +0000366unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
367 if (Loc.isInvalid() && CurLoc.isInvalid())
368 return 0;
369 SourceManager &SM = CGM.getContext().getSourceManager();
370 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000371 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000372}
373
Adrian Prantlc7822422013-03-12 20:43:25 +0000374unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000375 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000376 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000377 return 0;
378
379 // If the location is invalid then use the current column.
380 if (Loc.isInvalid() && CurLoc.isInvalid())
381 return 0;
382 SourceManager &SM = CGM.getContext().getSourceManager();
383 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000384 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000385}
386
387StringRef CGDebugInfo::getCurrentDirname() {
388 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
389 return CGM.getCodeGenOpts().DebugCompilationDir;
390
391 if (!CWDName.empty())
392 return CWDName;
393 SmallString<256> CWD;
394 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000395 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000396}
397
Guy Benyei11169dd2012-12-18 14:30:41 +0000398void CGDebugInfo::CreateCompileUnit() {
399
David Blaikieaabde052014-05-14 00:29:00 +0000400 // Should we be asking the SourceManager for the main file name, instead of
401 // accepting it as an argument? This just causes the main file name to
402 // mismatch with source locations and create extra lexical scopes or
403 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
404 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
405 // because that's what the SourceManager says)
406
Guy Benyei11169dd2012-12-18 14:30:41 +0000407 // Get absolute path name.
408 SourceManager &SM = CGM.getContext().getSourceManager();
409 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
410 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000411 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000412
413 // The main file name provided via the "-main-file-name" option contains just
414 // the file name itself with no path information. This file name may have had
415 // a relative path, so we look into the actual file entry for the main
416 // file to determine the real absolute path for the file.
417 std::string MainFileDir;
418 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000419 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000420 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000421 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
422 llvm::sys::path::append(MainFileDirSS, MainFileName);
423 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000424 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 }
426
Ed Masteda706022014-05-07 12:49:30 +0000427 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 const LangOptions &LO = CGM.getLangOpts();
429 if (LO.CPlusPlus) {
430 if (LO.ObjC1)
431 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
432 else
433 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
434 } else if (LO.ObjC1) {
435 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000436 } else if (LO.RenderScript) {
437 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 } else if (LO.C99) {
439 LangTag = llvm::dwarf::DW_LANG_C99;
440 } else {
441 LangTag = llvm::dwarf::DW_LANG_C89;
442 }
443
444 std::string Producer = getClangFullVersion();
445
446 // Figure out which version of the ObjC runtime we have.
447 unsigned RuntimeVers = 0;
448 if (LO.ObjC1)
449 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
450
Adrian Prantl826824e2016-04-08 22:43:06 +0000451 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
452 switch (DebugKind) {
453 case codegenoptions::NoDebugInfo:
454 case codegenoptions::LocTrackingOnly:
455 EmissionKind = llvm::DICompileUnit::NoDebug;
456 break;
457 case codegenoptions::DebugLineTablesOnly:
458 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
459 break;
460 case codegenoptions::LimitedDebugInfo:
461 case codegenoptions::FullDebugInfo:
462 EmissionKind = llvm::DICompileUnit::FullDebug;
463 break;
464 }
465
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000468 TheCU = DBuilder.createCompileUnit(
Amjad Aboudfa9a17e2016-12-14 20:24:40 +0000469 LangTag, DBuilder.createFile(remapDIPath(MainFileName),
470 remapDIPath(getCurrentDirname())),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000471 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000472 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
473 CGM.getCodeGenOpts().SplitDwarfInlining);
Guy Benyei11169dd2012-12-18 14:30:41 +0000474}
475
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000476llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000477 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000478 StringRef BTName;
479 switch (BT->getKind()) {
480#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000481#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000482#include "clang/AST/BuiltinTypes.def"
483 case BuiltinType::Dependent:
484 llvm_unreachable("Unexpected builtin type");
485 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000486 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000488 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000489 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000490 if (!ClassTy)
491 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
492 "objc_class", TheCU,
493 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000494 return ClassTy;
495 case BuiltinType::ObjCId: {
496 // typedef struct objc_class *Class;
497 // typedef struct objc_object {
498 // Class isa;
499 // } *id;
500
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000501 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 return ObjTy;
503
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000504 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
506 "objc_class", TheCU,
507 getOrCreateMainFile(), 0);
508
509 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000510
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000511 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000512
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000513 ObjTy = DBuilder.createStructType(
514 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
515 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000516
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000517 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000518 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
519 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
520 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000521 return ObjTy;
522 }
523 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000524 if (!SelTy)
525 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
526 "objc_selector", TheCU,
527 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 return SelTy;
529 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000530
Alexey Bader954ba212016-04-08 13:40:33 +0000531#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
532 case BuiltinType::Id: \
533 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
534 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000535#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000536 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000537 return getOrCreateStructPtrType("opencl_sampler_t",
538 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000539 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000540 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000541 case BuiltinType::OCLClkEvent:
542 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
543 case BuiltinType::OCLQueue:
544 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
545 case BuiltinType::OCLNDRange:
546 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
547 case BuiltinType::OCLReserveID:
548 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000549
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000551 case BuiltinType::Char_U:
552 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
553 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000554 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000555 case BuiltinType::SChar:
556 Encoding = llvm::dwarf::DW_ATE_signed_char;
557 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000558 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000559 case BuiltinType::Char32:
560 Encoding = llvm::dwarf::DW_ATE_UTF;
561 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 case BuiltinType::UShort:
563 case BuiltinType::UInt:
564 case BuiltinType::UInt128:
565 case BuiltinType::ULong:
566 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000567 case BuiltinType::ULongLong:
568 Encoding = llvm::dwarf::DW_ATE_unsigned;
569 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000570 case BuiltinType::Short:
571 case BuiltinType::Int:
572 case BuiltinType::Int128:
573 case BuiltinType::Long:
574 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000575 case BuiltinType::LongLong:
576 Encoding = llvm::dwarf::DW_ATE_signed;
577 break;
578 case BuiltinType::Bool:
579 Encoding = llvm::dwarf::DW_ATE_boolean;
580 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000581 case BuiltinType::Half:
582 case BuiltinType::Float:
583 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000584 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000585 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000586 // FIXME: For targets where long double and __float128 have the same size,
587 // they are currently indistinguishable in the debugger without some
588 // special treatment. However, there is currently no consensus on encoding
589 // and this should be updated once a DWARF encoding exists for distinct
590 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000591 Encoding = llvm::dwarf::DW_ATE_float;
592 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 }
594
595 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000596 case BuiltinType::Long:
597 BTName = "long int";
598 break;
599 case BuiltinType::LongLong:
600 BTName = "long long int";
601 break;
602 case BuiltinType::ULong:
603 BTName = "long unsigned int";
604 break;
605 case BuiltinType::ULongLong:
606 BTName = "long long unsigned int";
607 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000608 default:
609 BTName = BT->getName(CGM.getLangOpts());
610 break;
611 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000612 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000613 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000614 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000615}
616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000617llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000618 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000619 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 if (Ty->isComplexIntegerType())
621 Encoding = llvm::dwarf::DW_ATE_lo_user;
622
623 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000624 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000625}
626
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000627llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
628 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000629 QualifierCollector Qc;
630 const Type *T = Qc.strip(Ty);
631
632 // Ignore these qualifiers for now.
633 Qc.removeObjCGCAttr();
634 Qc.removeAddressSpace();
635 Qc.removeObjCLifetime();
636
637 // We will create one Derived type for one qualifier and recurse to handle any
638 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000639 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 if (Qc.hasConst()) {
641 Tag = llvm::dwarf::DW_TAG_const_type;
642 Qc.removeConst();
643 } else if (Qc.hasVolatile()) {
644 Tag = llvm::dwarf::DW_TAG_volatile_type;
645 Qc.removeVolatile();
646 } else if (Qc.hasRestrict()) {
647 Tag = llvm::dwarf::DW_TAG_restrict_type;
648 Qc.removeRestrict();
649 } else {
650 assert(Qc.empty() && "Unknown type qualifier for debug info");
651 return getOrCreateType(QualType(T, 0), Unit);
652 }
653
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000654 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000655
656 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
657 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000658 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000659}
660
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000661llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
662 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000663
664 // The frontend treats 'id' as a typedef to an ObjCObjectType,
665 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
666 // debug info, we want to emit 'id' in both cases.
667 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000668 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000669
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000670 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
671 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000672}
673
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000674llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
675 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000676 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000677 Ty->getPointeeType(), Unit);
678}
679
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000680/// \return whether a C++ mangling exists for the type defined by TD.
681static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
682 switch (TheCU->getSourceLanguage()) {
683 case llvm::dwarf::DW_LANG_C_plus_plus:
684 return true;
685 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
686 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
687 default:
688 return false;
689 }
690}
691
Manman Rene0064d82013-08-29 23:19:58 +0000692/// In C++ mode, types have linkage, so we can rely on the ODR and
693/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000694static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
695 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000696 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000697 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000698 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000699
700 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000701 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000702
Manman Rene0064d82013-08-29 23:19:58 +0000703 // TODO: This is using the RTTI name. Is there a better way to get
704 // a unique string for a type?
705 llvm::raw_svector_ostream Out(FullName);
706 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000707 return FullName;
708}
709
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000710/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000711static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
712 llvm::dwarf::Tag Tag;
713 if (RD->isStruct() || RD->isInterface())
714 Tag = llvm::dwarf::DW_TAG_structure_type;
715 else if (RD->isUnion())
716 Tag = llvm::dwarf::DW_TAG_union_type;
717 else {
718 // FIXME: This could be a struct type giving a default visibility different
719 // than C++ class type, but needs llvm metadata changes first.
720 assert(RD->isClass());
721 Tag = llvm::dwarf::DW_TAG_class_type;
722 }
723 return Tag;
724}
725
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000726llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000727CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000728 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000729 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000730 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
731 return cast<llvm::DICompositeType>(T);
732 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000733 unsigned Line = getLineNumber(RD->getLocation());
734 StringRef RDName = getClassName(RD);
735
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000736 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000737 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000738
739 const RecordDecl *D = RD->getDefinition();
740 if (D && D->isCompleteDefinition()) {
741 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000742 Align = getDeclAlignIfRequired(D, CGM.getContext());
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000743 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000744
745 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000746 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000747 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000748 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000749 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000750 ReplaceMap.emplace_back(
751 std::piecewise_construct, std::make_tuple(Ty),
752 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000753 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000754}
755
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000756llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000757 const Type *Ty,
758 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000759 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 // Bit size, align and offset of the type.
761 // Size is always the size of a pointer. We can't use getTypeSize here
762 // because that does not return the correct value for references.
763 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000764 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +0000765 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +0000766
Keno Fischer87842f32015-11-16 09:04:13 +0000767 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
768 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
769 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
770 Size, Align);
771 else
772 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
773 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000774}
775
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000776llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
777 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000778 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000779 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000780 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
781 TheCU, getOrCreateMainFile(), 0);
782 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
783 Cache = DBuilder.createPointerType(Cache, Size);
784 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000785}
786
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000787llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
788 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000789 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000790 QualType FType;
791 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000792 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000793 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000794
795 FieldOffset = 0;
796 FType = CGM.getContext().UnsignedLongTy;
797 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
798 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
799
800 Elements = DBuilder.getOrCreateArray(EltTys);
801 EltTys.clear();
802
Leny Kholodov80c047d2016-09-06 10:48:04 +0000803 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000804 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000805
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000806 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000807 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000808 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000809
810 // Bit size, align and offset of the type.
811 uint64_t Size = CGM.getContext().getTypeSize(Ty);
812
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000813 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000814
815 FieldOffset = 0;
816 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
817 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
818 FType = CGM.getContext().IntTy;
819 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
820 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000821 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000822 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
823
824 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 FieldSize = CGM.getContext().getTypeSize(Ty);
826 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000827 EltTys.push_back(DBuilder.createMemberType(
828 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
829 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000830
831 FieldOffset += FieldSize;
832 Elements = DBuilder.getOrCreateArray(EltTys);
833
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000834 // The __block_literal_generic structs are marked with a special
835 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
836 // the debugger needs to know about. To allow type uniquing, emit
837 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000838 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000839 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000840 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000841
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000842 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000843}
844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
846 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000847 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000848 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000849
850 SmallString<128> NS;
851 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000852 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
853 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000854
855 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000856 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000857 CGM.getContext().getPrintingPolicy());
858
David Majnemer58ed0f32016-07-17 00:39:12 +0000859 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000860 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000861
862 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000863 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
864 getLineNumber(Loc),
865 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000866}
867
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000868llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
869 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000870 // We don't set size information, but do specify where the typedef was
871 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000872 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000873
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000874 // Typedefs are derived from some other type.
875 return DBuilder.createTypedef(
876 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
877 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000878 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000879}
880
Reid Klecknerf00f8032016-06-08 20:41:54 +0000881static unsigned getDwarfCC(CallingConv CC) {
882 switch (CC) {
883 case CC_C:
884 // Avoid emitting DW_AT_calling_convention if the C convention was used.
885 return 0;
886
887 case CC_X86StdCall:
888 return llvm::dwarf::DW_CC_BORLAND_stdcall;
889 case CC_X86FastCall:
890 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
891 case CC_X86ThisCall:
892 return llvm::dwarf::DW_CC_BORLAND_thiscall;
893 case CC_X86VectorCall:
894 return llvm::dwarf::DW_CC_LLVM_vectorcall;
895 case CC_X86Pascal:
896 return llvm::dwarf::DW_CC_BORLAND_pascal;
897
898 // FIXME: Create new DW_CC_ codes for these calling conventions.
899 case CC_X86_64Win64:
900 case CC_X86_64SysV:
901 case CC_AAPCS:
902 case CC_AAPCS_VFP:
903 case CC_IntelOclBicc:
904 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000905 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000906 case CC_Swift:
907 case CC_PreserveMost:
908 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000909 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000910 return 0;
911 }
912 return 0;
913}
914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000915llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
916 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000917 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000918
919 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000920 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000921
922 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000923 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000924 if (isa<FunctionNoProtoType>(Ty))
925 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000926 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
927 for (const QualType &ParamType : FPT->param_types())
928 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000929 if (FPT->isVariadic())
930 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 }
932
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000933 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000934 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000935 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000936}
937
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000938/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000939/// As an optimization, return 0 if the access specifier equals the
940/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000941static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
942 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +0000943 AccessSpecifier Default = clang::AS_none;
944 if (RD && RD->isClass())
945 Default = clang::AS_private;
946 else if (RD && (RD->isStruct() || RD->isUnion()))
947 Default = clang::AS_public;
948
949 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +0000950 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000951
Eric Christophere7b87e52014-10-26 23:40:33 +0000952 switch (Access) {
953 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000955 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000956 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000957 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000958 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000959 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +0000960 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000961 }
962 llvm_unreachable("unexpected access enumerator");
963}
Guy Benyei11169dd2012-12-18 14:30:41 +0000964
David Majnemerb4b671e2016-06-30 03:01:59 +0000965llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
966 llvm::DIScope *RecordTy,
967 const RecordDecl *RD) {
968 StringRef Name = BitFieldDecl->getName();
969 QualType Ty = BitFieldDecl->getType();
970 SourceLocation Loc = BitFieldDecl->getLocation();
971 llvm::DIFile *VUnit = getOrCreateFile(Loc);
972 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
973
974 // Get the location for the field.
975 llvm::DIFile *File = getOrCreateFile(Loc);
976 unsigned Line = getLineNumber(Loc);
977
978 const CGBitFieldInfo &BitFieldInfo =
979 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
980 uint64_t SizeInBits = BitFieldInfo.Size;
981 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +0000982 uint64_t StorageOffsetInBits =
983 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
984 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +0000985 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +0000986 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +0000987 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
988 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +0000989}
990
991llvm::DIType *
992CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
993 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +0000994 uint32_t AlignInBits, llvm::DIFile *tunit,
995 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000996 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000997
998 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000999 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001000 unsigned line = getLineNumber(loc);
1001
David Majnemer34b57492014-07-30 01:30:47 +00001002 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001003 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001004 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001005 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1006 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001007 if (!Align)
1008 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001009 }
1010
Leny Kholodov80c047d2016-09-06 10:48:04 +00001011 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001012 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001013 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001014}
1015
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001016void CGDebugInfo::CollectRecordLambdaFields(
1017 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001018 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001019 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1020 // has the name and the location of the variable so we should iterate over
1021 // both concurrently.
1022 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1023 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1024 unsigned fieldno = 0;
1025 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001026 E = CXXDecl->captures_end();
1027 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001028 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001029 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001030 SourceLocation Loc = C.getLocation();
1031 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001032 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001033 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001034 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001035 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001036 llvm::DIType *FieldType = createFieldType(
1037 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001038 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001039 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001040 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001041 // TODO: Need to handle 'this' in some way by probably renaming the
1042 // this of the lambda class and having a field member of 'this' or
1043 // by using AT_object_pointer for the function and having that be
1044 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001045 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001046 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001047 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001048 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001049 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001050 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001051
1052 elements.push_back(fieldType);
1053 }
1054 }
1055}
1056
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057llvm::DIDerivedType *
1058CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001059 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001060 // Create the descriptor for the static variable, with or without
1061 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001062 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001063 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1064 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001065
Eric Christopher91a31902013-01-16 01:22:32 +00001066 unsigned LineNumber = getLineNumber(Var->getLocation());
1067 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001068 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001069 if (Var->getInit()) {
1070 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001071 if (Value) {
1072 if (Value->isInt())
1073 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1074 if (Value->isFloat())
1075 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1076 }
Eric Christopher91a31902013-01-16 01:22:32 +00001077 }
1078
Leny Kholodov80c047d2016-09-06 10:48:04 +00001079 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001080 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001081 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001082 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001083 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001084 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001085}
1086
Eric Christophere7b87e52014-10-26 23:40:33 +00001087void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001088 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1089 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001090 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001091 StringRef name = field->getName();
1092 QualType type = field->getType();
1093
1094 // Ignore unnamed fields unless they're anonymous structs/unions.
1095 if (name.empty() && !type->isRecordType())
1096 return;
1097
David Majnemerb4b671e2016-06-30 03:01:59 +00001098 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001099 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001100 FieldType = createBitFieldType(field, RecordTy, RD);
1101 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001102 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001103 FieldType =
1104 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001105 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001106 }
1107
David Majnemerb4b671e2016-06-30 03:01:59 +00001108 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001109}
1110
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001111void CGDebugInfo::CollectRecordNestedRecord(
1112 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1113 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001114 // Injected class names are not considered nested records.
1115 if (isa<InjectedClassNameType>(Ty))
1116 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001117 SourceLocation Loc = RD->getLocation();
1118 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1119 elements.push_back(nestedType);
1120}
1121
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001122void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001124 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001126 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001127
Eric Christopher91a31902013-01-16 01:22:32 +00001128 if (CXXDecl && CXXDecl->isLambda())
1129 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1130 else {
1131 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001132
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001133 // Debug info for nested records is included in the member list only for
1134 // CodeView.
1135 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1136
Eric Christopher91a31902013-01-16 01:22:32 +00001137 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001138 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001139
Eric Christopher91a31902013-01-16 01:22:32 +00001140 // Static and non-static members should appear in the same order as
1141 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001142 for (const auto *I : record->decls())
1143 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001144 if (V->hasAttr<NoDebugAttr>())
1145 continue;
David Blaikiece763042013-08-20 21:49:21 +00001146 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001147 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001148 if (MI != StaticDataMemberCache.end()) {
1149 assert(MI->second &&
1150 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001151 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001152 } else {
1153 auto Field = CreateRecordStaticField(V, RecordTy, record);
1154 elements.push_back(Field);
1155 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001156 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001157 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1158 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001159
1160 // Bump field number for next field.
1161 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001162 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1163 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1164 nestedRec->getDeclContext() == record)
1165 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001166 }
1167}
1168
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001169llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001170CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001171 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001172 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001173 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001175 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001176 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1177 Func, Unit);
1178}
David Blaikie2aaf0652013-01-07 22:24:59 +00001179
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001180llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1181 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001183 llvm::DITypeRefArray Args(
1184 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001185 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001186 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001187
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001188 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001189
1190 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001191 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001192
David Blaikie2aaf0652013-01-07 22:24:59 +00001193 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001194 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001195 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1196 // Create pointer type directly in this case.
1197 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1198 QualType PointeeTy = ThisPtrTy->getPointeeType();
1199 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001200 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001201 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001202 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1203 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001204 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001205 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001206 // TODO: This and the artificial type below are misleading, the
1207 // types aren't artificial the argument is, but the current
1208 // metadata doesn't represent that.
1209 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1210 Elts.push_back(ThisPtrType);
1211 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001213 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001214 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1215 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 }
1217
1218 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001219 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1220 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001221
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001222 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001223
Leny Kholodov80c047d2016-09-06 10:48:04 +00001224 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001225 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001226 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001227 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001228 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001229
Reid Klecknerf00f8032016-06-08 20:41:54 +00001230 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1231 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001232}
1233
Eric Christopherb2a008c2013-05-16 00:45:12 +00001234/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001235/// inside a function.
1236static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001237 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 return isFunctionLocalClass(NRD);
1239 if (isa<FunctionDecl>(RD->getDeclContext()))
1240 return true;
1241 return false;
1242}
1243
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001244llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1245 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001246 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001247 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001248
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001250 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001251
1252 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1253 // make sense to give a single ctor/dtor a linkage name.
1254 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001255 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1256 // property to use here. It may've been intended to model "is non-external
1257 // type" but misses cases of non-function-local but non-external classes such
1258 // as those in anonymous namespaces as well as the reverse - external types
1259 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1261 MethodLinkageName = CGM.getMangledName(Method);
1262
1263 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001264 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001265 unsigned MethodLine = 0;
1266 if (!Method->isImplicit()) {
1267 MethodDefUnit = getOrCreateFile(Method->getLocation());
1268 MethodLine = getLineNumber(Method->getLocation());
1269 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001270
1271 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001272 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001273 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001274 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001275 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001276 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001277
Guy Benyei11169dd2012-12-18 14:30:41 +00001278 if (Method->isVirtual()) {
1279 if (Method->isPure())
1280 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1281 else
1282 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001283
Reid Kleckner216d0a12016-06-16 20:08:51 +00001284 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1285 // It doesn't make sense to give a virtual destructor a vtable index,
1286 // since a single destructor has two entries in the vtable.
1287 if (!isa<CXXDestructorDecl>(Method))
1288 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1289 } else {
1290 // Emit MS ABI vftable information. There is only one entry for the
1291 // deleting dtor.
1292 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1293 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1294 MicrosoftVTableContext::MethodVFTableLocation ML =
1295 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1296 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001297
1298 // CodeView only records the vftable offset in the class that introduces
1299 // the virtual method. This is possible because, unlike Itanium, the MS
1300 // C++ ABI does not include all virtual methods from non-primary bases in
1301 // the vtable for the most derived class. For example, if C inherits from
1302 // A and B, C's primary vftable will not include B's virtual methods.
1303 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1304 Flags |= llvm::DINode::FlagIntroducedVirtual;
1305
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001306 // The 'this' adjustment accounts for both the virtual and non-virtual
1307 // portions of the adjustment. Presumably the debugger only uses it when
1308 // it knows the dynamic type of an object.
1309 ThisAdjustment = CGM.getCXXABI()
1310 .getVirtualFunctionPrologueThisAdjustment(GD)
1311 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001312 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 ContainingType = RecordTy;
1314 }
1315
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001318 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001319 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001321 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001322 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001323 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001324 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001325 }
1326 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001327 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001328 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001329 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001330 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001331 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1334 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001335 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001336 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1337 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1338 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001339
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001340 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001341
1342 return SP;
1343}
1344
Eric Christophere7b87e52014-10-26 23:40:33 +00001345void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001346 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1347 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001348
1349 // Since we want more than just the individual member decls if we
1350 // have templated functions iterate over every declaration to gather
1351 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001352 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001353 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1354 // If the member is implicit, don't add it to the member list. This avoids
1355 // the member being added to type units by LLVM, while still allowing it
1356 // to be emitted into the type declaration/reference inside the compile
1357 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001358 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001359 // FIXME: Handle Using(Shadow?)Decls here to create
1360 // DW_TAG_imported_declarations inside the class for base decls brought into
1361 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1362 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1363 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001364 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001365 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001366
1367 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1368 continue;
1369
David Blaikiefd580722014-10-06 05:18:55 +00001370 // Reuse the existing member function declaration if it exists.
1371 // It may be associated with the declaration of the type & should be
1372 // reused as we're building the definition.
1373 //
1374 // This situation can arise in the vtable-based debug info reduction where
1375 // implicit members are emitted in a non-vtable TU.
1376 auto MI = SPCache.find(Method->getCanonicalDecl());
1377 EltTys.push_back(MI == SPCache.end()
1378 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001379 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001381}
Guy Benyei11169dd2012-12-18 14:30:41 +00001382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001383void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001384 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001386 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1387 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1388 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001389
Bob Haarmandff36732016-10-25 22:19:32 +00001390 // If we are generating CodeView debug info, we also need to emit records for
1391 // indirect virtual base classes.
1392 if (CGM.getCodeGenOpts().EmitCodeView) {
1393 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1394 llvm::DINode::FlagIndirectVirtualBase);
1395 }
1396}
1397
1398void CGDebugInfo::CollectCXXBasesAux(
1399 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1400 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1401 const CXXRecordDecl::base_class_const_range &Bases,
1402 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1403 llvm::DINode::DIFlags StartingFlags) {
1404 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1405 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001406 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001407 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001408 if (!SeenTypes.insert(Base).second)
1409 continue;
1410 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1411 llvm::DINode::DIFlags BFlags = StartingFlags;
1412 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001413
Aaron Ballman574705e2014-03-13 15:41:46 +00001414 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001415 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1416 // virtual base offset offset is -ve. The code generator emits dwarf
1417 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001418 BaseOffset = 0 - CGM.getItaniumVTableContext()
1419 .getVirtualBaseOffsetOffset(RD, Base)
1420 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001421 } else {
1422 // In the MS ABI, store the vbtable offset, which is analogous to the
1423 // vbase offset offset in Itanium.
1424 BaseOffset =
1425 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1426 }
Bob Haarmandff36732016-10-25 22:19:32 +00001427 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001428 } else
1429 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1430 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1431 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001432
Adrian Prantl21361fb2014-08-29 22:44:27 +00001433 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001434 llvm::DIType *DTy =
1435 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 EltTys.push_back(DTy);
1437 }
1438}
1439
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001440llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001441CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1442 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001444 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1446 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001447 StringRef Name;
1448 if (TPList)
1449 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001450 switch (TA.getKind()) {
1451 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001452 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001453 TemplateParams.push_back(
1454 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001455 } break;
1456 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001457 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001458 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1459 TheCU, Name, TTy,
1460 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001461 } break;
1462 case TemplateArgument::Declaration: {
1463 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001464 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001465 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001466 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001467 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001468 // Variable pointer template parameters have a value that is the address
1469 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001470 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001471 V = CGM.GetAddrOfGlobalVar(VD);
1472 // Member function pointers have special support for building them, though
1473 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001474 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001475 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001476 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001477 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001478 // Member data pointers have special handling too to compute the fixed
1479 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001480 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001481 // These five lines (& possibly the above member function pointer
1482 // handling) might be able to be refactored to use similar code in
1483 // CodeGenModule::getMemberPointerConstant
1484 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1485 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001486 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001487 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001488 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001489 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1490 TheCU, Name, TTy,
1491 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001492 } break;
1493 case TemplateArgument::NullPtr: {
1494 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001495 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001496 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001497 // Special case member data pointer null values since they're actually -1
1498 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001499 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001500 // But treat member function pointers as simple zero integers because
1501 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1502 // CodeGen grows handling for values of non-null member function
1503 // pointers then perhaps we could remove this special case and rely on
1504 // EmitNullMemberPointer for member function pointers.
1505 if (MPT->isMemberDataPointer())
1506 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1507 if (!V)
1508 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001509 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001510 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001511 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001512 case TemplateArgument::Template:
1513 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001514 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001515 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1516 break;
1517 case TemplateArgument::Pack:
1518 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1519 TheCU, Name, nullptr,
1520 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1521 break;
David Majnemer5559d472013-08-24 08:21:10 +00001522 case TemplateArgument::Expression: {
1523 const Expr *E = TA.getAsExpr();
1524 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001525 if (E->isGLValue())
1526 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001527 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001528 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001529 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001530 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001531 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001532 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001533 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001534 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001535 case TemplateArgument::Null:
1536 llvm_unreachable(
1537 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001538 }
1539 }
1540 return DBuilder.getOrCreateArray(TemplateParams);
1541}
1542
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001543llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001544CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001545 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 if (FD->getTemplatedKind() ==
1547 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001548 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1549 ->getTemplate()
1550 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001551 return CollectTemplateParams(
1552 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001553 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001555}
1556
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1558 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001559 // Always get the full list of parameters, not just the ones from
1560 // the specialization.
1561 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001562 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001563 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001564 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001565}
1566
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001567llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001568 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 return VTablePtrType;
1570
1571 ASTContext &Context = CGM.getContext();
1572
1573 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001574 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001575 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001576 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001577 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001578 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001579 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1581 return VTablePtrType;
1582}
1583
Guy Benyei11169dd2012-12-18 14:30:41 +00001584StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001585 // Copy the gdb compatible name on the side and use its reference.
1586 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001587}
1588
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001589void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001590 SmallVectorImpl<llvm::Metadata *> &EltTys,
1591 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 // If this class is not dynamic then there is not any vtable info to collect.
1593 if (!RD->isDynamicClass())
1594 return;
1595
Reid Kleckner59812422016-08-31 20:35:01 +00001596 // Don't emit any vtable shape or vptr info if this class doesn't have an
1597 // extendable vfptr. This can happen if the class doesn't have virtual
1598 // methods, or in the MS ABI if those virtual methods only come from virtually
1599 // inherited bases.
1600 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1601 if (!RL.hasExtendableVFPtr())
1602 return;
1603
Reid Klecknerdc124992016-08-31 16:11:43 +00001604 // CodeView needs to know how large the vtable of every dynamic class is, so
1605 // emit a special named pointer type into the element list. The vptr type
1606 // points to this type as well.
1607 llvm::DIType *VPtrTy = nullptr;
1608 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1609 CGM.getTarget().getCXXABI().isMicrosoft();
1610 if (NeedVTableShape) {
1611 uint64_t PtrWidth =
1612 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1613 const VTableLayout &VFTLayout =
1614 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1615 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001616 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001617 unsigned VTableWidth = PtrWidth * VSlotCount;
1618
1619 // Create a very wide void* type and insert it directly in the element list.
1620 llvm::DIType *VTableType =
1621 DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type");
1622 EltTys.push_back(VTableType);
1623
1624 // The vptr is a pointer to this special vtable type.
1625 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1626 }
1627
1628 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001629 if (RL.getPrimaryBase())
1630 return;
1631
1632 if (!VPtrTy)
1633 VPtrTy = getOrCreateVTablePtrType(Unit);
1634
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001636 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001637 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001638 llvm::DINode::FlagArtificial, VPtrTy);
1639 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001640}
1641
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001642llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001643 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001644 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001645 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001646 return T;
1647}
1648
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001649llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001650 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001651 return getOrCreateStandaloneType(D, Loc);
1652}
1653
1654llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1655 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001656 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001657 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001658 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001659 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001660
Adrian Prantl73409ce2013-03-11 18:33:46 +00001661 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001662 return T;
1663}
1664
David Blaikie483a9da2014-05-06 18:35:21 +00001665void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001666 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001667 return;
1668 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001669 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001670 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001671 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001672 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001673 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001674 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001675 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001676}
1677
David Blaikieb2e86eb2013-08-15 20:49:17 +00001678void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001679 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001680 !CGM.getLangOpts().CPlusPlus)
1681 completeRequiredType(RD);
1682}
1683
David Blaikie6943dea2013-08-20 01:28:15 +00001684void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001685 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001686 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001687 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001688 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001689 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001690 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001691 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001692 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001693 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001694 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001695}
1696
David Blaikie0e716b42014-03-03 23:48:23 +00001697static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1698 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001699 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1700 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001701 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001702 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001703 return true;
1704 return false;
1705}
1706
Adrian Prantl05fefa42016-04-25 20:52:40 +00001707/// Does a type definition exist in an imported clang module?
1708static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001709 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001710 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001711 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001712 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001713 if (!RD->isExternallyVisible() && RD->getName().empty())
1714 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001715 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001716 if (!CXXDecl->isCompleteDefinition())
1717 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001718 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1719 if (TemplateKind != TSK_Undeclared) {
1720 // This is a template, check the origin of the first member.
1721 if (CXXDecl->field_begin() == CXXDecl->field_end())
1722 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1723 if (!CXXDecl->field_begin()->isFromASTFile())
1724 return false;
1725 }
Adrian Prantl94913712016-04-26 23:42:43 +00001726 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001727 return true;
1728}
1729
Reid Klecknerc9404e12016-09-09 16:27:04 +00001730/// Return true if the class or any of its methods are marked dllimport.
1731static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1732 if (RD->hasAttr<DLLImportAttr>())
1733 return true;
1734 for (const CXXMethodDecl *MD : RD->methods())
1735 if (MD->hasAttr<DLLImportAttr>())
1736 return true;
1737 return false;
1738}
1739
Benjamin Kramer8c305922016-02-02 11:06:51 +00001740static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1741 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001742 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001743 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001744 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001745
Benjamin Kramer8c305922016-02-02 11:06:51 +00001746 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001747 return false;
1748
1749 if (!LangOpts.CPlusPlus)
1750 return false;
1751
1752 if (!RD->isCompleteDefinitionRequired())
1753 return true;
1754
David Majnemer58ed0f32016-07-17 00:39:12 +00001755 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001756
1757 if (!CXXDecl)
1758 return false;
1759
Adrian McCarthy99242982016-08-16 22:11:18 +00001760 // Only emit complete debug info for a dynamic class when its vtable is
1761 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001762 // across DLL boundaries, so skip this optimization if the class or any of its
1763 // methods are marked dllimport. This isn't a complete solution, since objects
1764 // without any dllimport methods can be used in one DLL and constructed in
1765 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001766 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001767 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001768 return true;
1769
1770 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001771 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001772 Spec = SD->getSpecializationKind();
1773
1774 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1775 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1776 CXXDecl->method_end()))
1777 return true;
1778
1779 return false;
1780}
1781
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001782void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1783 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1784 return;
1785
1786 QualType Ty = CGM.getContext().getRecordType(RD);
1787 llvm::DIType *T = getTypeOrNull(Ty);
1788 if (T && T->isForwardDecl())
1789 completeClassData(RD);
1790}
1791
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001792llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001794 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001795 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1796 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001797 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001798 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001799 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001800 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001801
David Blaikieb2e86eb2013-08-15 20:49:17 +00001802 return CreateTypeDefinition(Ty);
1803}
1804
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001805llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001806 RecordDecl *RD = Ty->getDecl();
1807
Guy Benyei11169dd2012-12-18 14:30:41 +00001808 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001809 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001810
1811 // Records and classes and unions can all be recursive. To handle them, we
1812 // first generate a debug descriptor for the struct as a forward declaration.
1813 // Then (if it is a definition) we go through and get debug info for all of
1814 // its members. Finally, we create a descriptor for the complete type (which
1815 // may refer to the forward decl if the struct is recursive) and replace all
1816 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001817 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001818
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001819 const RecordDecl *D = RD->getDefinition();
1820 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 return FwdDecl;
1822
David Majnemer58ed0f32016-07-17 00:39:12 +00001823 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001824 CollectContainingType(CXXDecl, FwdDecl);
1825
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001827 LexicalBlockStack.emplace_back(&*FwdDecl);
1828 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001829
Guy Benyei11169dd2012-12-18 14:30:41 +00001830 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001831 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001832 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001833
1834 // Note: The split of CXXDecl information here is intentional, the
1835 // gdb tests will depend on a certain ordering at printout. The debug
1836 // information offsets are still correct if we merge them all together
1837 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001838 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 if (CXXDecl) {
1840 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001841 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 }
1843
Eric Christopher91a31902013-01-16 01:22:32 +00001844 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001845 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001846 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001847 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001848
1849 LexicalBlockStack.pop_back();
1850 RegionMap.erase(Ty->getDecl());
1851
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001852 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001853 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001854
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001855 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001856 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001857 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001858
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001859 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001860 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001861}
1862
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001863llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1864 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 // Ignore protocols.
1866 return getOrCreateType(Ty->getBaseType(), Unit);
1867}
1868
Manman Rene6be26c2016-09-13 17:25:08 +00001869llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1870 llvm::DIFile *Unit) {
1871 // Ignore protocols.
1872 SourceLocation Loc = Ty->getDecl()->getLocation();
1873
1874 // Use Typedefs to represent ObjCTypeParamType.
1875 return DBuilder.createTypedef(
1876 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1877 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1878 getDeclContextDescriptor(Ty->getDecl()));
1879}
1880
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001881/// \return true if Getter has the default name for the property PD.
1882static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1883 const ObjCMethodDecl *Getter) {
1884 assert(PD);
1885 if (!Getter)
1886 return true;
1887
1888 assert(Getter->getDeclName().isObjCZeroArgSelector());
1889 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001890 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001891}
1892
1893/// \return true if Setter has the default name for the property PD.
1894static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1895 const ObjCMethodDecl *Setter) {
1896 assert(PD);
1897 if (!Setter)
1898 return true;
1899
1900 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001901 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001902 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001903}
1904
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001905llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1906 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 ObjCInterfaceDecl *ID = Ty->getDecl();
1908 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001909 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001910
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001911 // Return a forward declaration if this type was imported from a clang module,
1912 // and this is not the compile unit with the implementation of the type (which
1913 // may contain hidden ivars).
1914 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1915 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001916 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1917 ID->getName(),
1918 getDeclContextDescriptor(ID), Unit, 0);
1919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001921 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001923 auto RuntimeLang =
1924 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001925
1926 // If this is just a forward declaration return a special forward-declaration
1927 // debug type since we won't be able to lay out the entire type.
1928 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001929 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001930 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001932 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1933 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001934 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 return FwdDecl;
1936 }
1937
David Blaikieef8a9512014-05-05 23:23:53 +00001938 return CreateTypeDefinition(Ty, Unit);
1939}
1940
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001941llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001942CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1943 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001944 // Use the Module pointer as the key into the cache. This is a
1945 // nullptr if the "Module" is a PCH, which is safe because we don't
1946 // support chained PCH debug info, so there can only be a single PCH.
1947 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001948 auto ModRef = ModuleCache.find(M);
1949 if (ModRef != ModuleCache.end())
1950 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001951
1952 // Macro definitions that were defined with "-D" on the command line.
1953 SmallString<128> ConfigMacros;
1954 {
1955 llvm::raw_svector_ostream OS(ConfigMacros);
1956 const auto &PPOpts = CGM.getPreprocessorOpts();
1957 unsigned I = 0;
1958 // Translate the macro definitions back into a commmand line.
1959 for (auto &M : PPOpts.Macros) {
1960 if (++I > 1)
1961 OS << " ";
1962 const std::string &Macro = M.first;
1963 bool Undef = M.second;
1964 OS << "\"-" << (Undef ? 'U' : 'D');
1965 for (char c : Macro)
1966 switch (c) {
1967 case '\\' : OS << "\\\\"; break;
1968 case '"' : OS << "\\\""; break;
1969 default: OS << c;
1970 }
1971 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001972 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001973 }
Adrian Prantl66689202015-09-18 23:01:45 +00001974
Adrian Prantl835e6632015-09-24 16:10:10 +00001975 bool IsRootModule = M ? !M->Parent : true;
1976 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001977 // PCH files don't have a signature field in the control block,
1978 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001979 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001980 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00001981 DIB.createCompileUnit(TheCU->getSourceLanguage(),
1982 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
1983 TheCU->getProducer(), true, StringRef(), 0,
1984 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
1985 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001986 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001987 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001988 llvm::DIModule *Parent =
1989 IsRootModule ? nullptr
1990 : getOrCreateModuleRef(
1991 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1992 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001993 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001994 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1995 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001996 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001997 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001998}
1999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002000llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2001 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002002 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002003 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002004 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002005 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002006
2007 // Bit size, align and offset of the type.
2008 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002009 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002010
Leny Kholodov80c047d2016-09-06 10:48:04 +00002011 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002013 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002014
Adrian Prantlfd696112015-10-01 00:48:51 +00002015 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002016 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002017 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2018 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002019
David Blaikieef8a9512014-05-05 23:23:53 +00002020 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002021 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002022
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002023 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002024 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002025 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002026
2027 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002028 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002029
2030 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2031 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002032 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002033 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002034 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002035 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002036
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002037 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2038 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002039 EltTys.push_back(InhTag);
2040 }
2041
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002042 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002043 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002045 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 unsigned PLine = getLineNumber(Loc);
2047 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2048 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002049 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2050 PD->getName(), PUnit, PLine,
2051 hasDefaultGetterName(PD, Getter) ? ""
2052 : getSelectorName(PD->getGetterName()),
2053 hasDefaultSetterName(PD, Setter) ? ""
2054 : getSelectorName(PD->getSetterName()),
2055 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002056 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002057 };
2058 {
2059 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002060 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002061 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002062 PropertySet.insert(PD->getIdentifier());
2063 AddProperty(PD);
2064 }
Manman Renefe1bac2016-01-27 20:00:32 +00002065 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002066 // Don't emit duplicate metadata for properties that were already in a
2067 // class extension.
2068 if (!PropertySet.insert(PD->getIdentifier()).second)
2069 continue;
2070 AddProperty(PD);
2071 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002072 }
2073
2074 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2075 unsigned FieldNo = 0;
2076 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2077 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002078 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002079 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002080 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002081
Guy Benyei11169dd2012-12-18 14:30:41 +00002082 StringRef FieldName = Field->getName();
2083
2084 // Ignore unnamed fields.
2085 if (FieldName.empty())
2086 continue;
2087
2088 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002089 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002090 unsigned FieldLine = getLineNumber(Field->getLocation());
2091 QualType FType = Field->getType();
2092 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002093 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002094
2095 if (!FType->isIncompleteArrayType()) {
2096
2097 // Bit size, align and offset of the type.
2098 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002099 ? Field->getBitWidthValue(CGM.getContext())
2100 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002101 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 }
2103
2104 uint64_t FieldOffset;
2105 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2106 // We don't know the runtime offset of an ivar if we're using the
2107 // non-fragile ABI. For bitfields, use the bit offset into the first
2108 // byte of storage of the bitfield. For other fields, use zero.
2109 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002110 FieldOffset =
2111 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 FieldOffset %= CGM.getContext().getCharWidth();
2113 } else {
2114 FieldOffset = 0;
2115 }
2116 } else {
2117 FieldOffset = RL.getFieldOffset(FieldNo);
2118 }
2119
Leny Kholodov80c047d2016-09-06 10:48:04 +00002120 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002124 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002125 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002126 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002127
Craig Topper8a13c412014-05-21 05:09:00 +00002128 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002129 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002130 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002131 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002133 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002134 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002135 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2137 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002138 PropertyNode = DBuilder.createObjCProperty(
2139 PD->getName(), PUnit, PLine,
2140 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2141 PD->getGetterName()),
2142 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2143 PD->getSetterName()),
2144 PD->getPropertyAttributes(),
2145 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 }
2147 }
2148 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002149 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2150 FieldSize, FieldAlign, FieldOffset, Flags,
2151 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002152 EltTys.push_back(FieldTy);
2153 }
2154
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002155 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002156 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002157
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002159 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002160}
2161
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002162llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2163 llvm::DIFile *Unit) {
2164 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 int64_t Count = Ty->getNumElements();
2166 if (Count == 0)
2167 // If number of elements are not known then this is an unbounded array.
2168 // Use Count == -1 to express such arrays.
2169 Count = -1;
2170
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002171 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002172 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002173
2174 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002175 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002176
2177 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2178}
2179
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002180llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002182 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002183
2184 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002185 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002187 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2188 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002189 } else if (Ty->isIncompleteArrayType()) {
2190 Size = 0;
2191 if (Ty->getElementType()->isIncompleteType())
2192 Align = 0;
2193 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002194 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002195 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 Size = 0;
2197 Align = 0;
2198 } else {
2199 // Size and align of the whole array, not the element type.
2200 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002201 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 }
2203
2204 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2205 // interior arrays, do we care? Why aren't nested arrays represented the
2206 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002207 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002208 QualType EltTy(Ty, 0);
2209 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2210 // If the number of elements is known, then count is that number. Otherwise,
2211 // it's -1. This allows us to represent a subrange with an array of 0
2212 // elements, like this:
2213 //
2214 // struct foo {
2215 // int x[0];
2216 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002217 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002218 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002219 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002220 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002221 if (Expr *Size = VAT->getSizeExpr()) {
2222 llvm::APSInt V;
2223 if (Size->EvaluateAsInt(V, CGM.getContext()))
2224 Count = V.getExtValue();
2225 }
David Blaikie87173f12016-08-22 17:49:56 +00002226 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002227
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 // FIXME: Verify this is right for VLAs.
2229 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2230 EltTy = Ty->getElementType();
2231 }
2232
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002233 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002234
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002235 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2236 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002237}
2238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002239llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2240 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002241 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2242 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002243}
2244
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002245llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2246 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002247 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2248 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002249}
2250
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002251llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2252 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002253 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002254 uint64_t Size = 0;
2255
2256 if (!Ty->isIncompleteType()) {
2257 Size = CGM.getContext().getTypeSize(Ty);
2258
2259 // Set the MS inheritance model. There is no flag for the unspecified model.
2260 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2261 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2262 case MSInheritanceAttr::Keyword_single_inheritance:
2263 Flags |= llvm::DINode::FlagSingleInheritance;
2264 break;
2265 case MSInheritanceAttr::Keyword_multiple_inheritance:
2266 Flags |= llvm::DINode::FlagMultipleInheritance;
2267 break;
2268 case MSInheritanceAttr::Keyword_virtual_inheritance:
2269 Flags |= llvm::DINode::FlagVirtualInheritance;
2270 break;
2271 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2272 break;
2273 }
2274 }
2275 }
2276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002277 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002278 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002279 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002280 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2281 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002282
2283 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002284 Ty->getPointeeType()->getAs<FunctionProtoType>();
2285 return DBuilder.createMemberPointerType(
2286 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2287 Ty->getClass(), FPT->getTypeQuals())),
2288 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002289 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002290}
2291
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002292llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002293 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2294 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002295}
2296
Xiuli Pan9c14e282016-01-09 12:53:17 +00002297llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2298 llvm::DIFile *U) {
2299 return getOrCreateType(Ty->getElementType(), U);
2300}
2301
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002302llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002303 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002304
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002306 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 if (!ED->getTypeForDecl()->isIncompleteType()) {
2308 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002309 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002310 }
2311
Manman Rene0064d82013-08-29 23:19:58 +00002312 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2313
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002314 bool isImportedFromModule =
2315 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2316
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 // If this is just a forward declaration, construct an appropriately
2318 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002319 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002320 // Note that it is possible for enums to be created as part of
2321 // their own declcontext. In this case a FwdDecl will be created
2322 // twice. This doesn't cause a problem because both FwdDecls are
2323 // entered into the ReplaceMap: finalize() will replace the first
2324 // FwdDecl with the second and then replace the second with
2325 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002326 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002327 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002328 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2329 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002330
Guy Benyei11169dd2012-12-18 14:30:41 +00002331 unsigned Line = getLineNumber(ED->getLocation());
2332 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002333 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002334 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2335 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002336
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002337 ReplaceMap.emplace_back(
2338 std::piecewise_construct, std::make_tuple(Ty),
2339 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002340 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002341 }
2342
David Blaikie483a9da2014-05-06 18:35:21 +00002343 return CreateTypeDefinition(Ty);
2344}
2345
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002346llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002347 const EnumDecl *ED = Ty->getDecl();
2348 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002349 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002350 if (!ED->getTypeForDecl()->isIncompleteType()) {
2351 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002352 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002353 }
2354
2355 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2356
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002357 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002358 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002359 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002360 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002361 Enumerators.push_back(DBuilder.createEnumerator(
2362 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002363 }
2364
2365 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002367
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002368 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002370 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002371 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002372 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2373 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2374 Line, Size, Align, EltArray, ClassTy,
2375 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002376}
2377
David Blaikie05491062013-01-21 04:37:12 +00002378static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2379 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002381 Qualifiers InnerQuals = T.getLocalQualifiers();
2382 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2383 // that is already there.
2384 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2385 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 QualType LastT = T;
2387 switch (T->getTypeClass()) {
2388 default:
David Blaikie05491062013-01-21 04:37:12 +00002389 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002390 case Type::TemplateSpecialization: {
2391 const auto *Spec = cast<TemplateSpecializationType>(T);
2392 if (Spec->isTypeAlias())
2393 return C.getQualifiedType(T.getTypePtr(), Quals);
2394 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002395 break;
2396 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 case Type::TypeOfExpr:
2398 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2399 break;
2400 case Type::TypeOf:
2401 T = cast<TypeOfType>(T)->getUnderlyingType();
2402 break;
2403 case Type::Decltype:
2404 T = cast<DecltypeType>(T)->getUnderlyingType();
2405 break;
2406 case Type::UnaryTransform:
2407 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2408 break;
2409 case Type::Attributed:
2410 T = cast<AttributedType>(T)->getEquivalentType();
2411 break;
2412 case Type::Elaborated:
2413 T = cast<ElaboratedType>(T)->getNamedType();
2414 break;
2415 case Type::Paren:
2416 T = cast<ParenType>(T)->getInnerType();
2417 break;
David Blaikie05491062013-01-21 04:37:12 +00002418 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002420 break;
Jordan Rose303e2f12016-11-10 23:28:17 +00002421 case Type::Auto: {
David Blaikie22c460a02013-05-24 21:24:35 +00002422 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002423 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002424 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002425 break;
2426 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002427 case Type::Adjusted:
2428 case Type::Decayed:
2429 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2430 T = cast<AdjustedType>(T)->getAdjustedType();
2431 break;
2432 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002433
Guy Benyei11169dd2012-12-18 14:30:41 +00002434 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002435 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002436 } while (true);
2437}
2438
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002439llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002440
2441 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002442 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002443
David Blaikief427b002014-05-06 03:42:01 +00002444 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002445 if (it != TypeCache.end()) {
2446 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002447 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002448 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002449 }
2450
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002451 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002452}
2453
David Blaikie0e716b42014-03-03 23:48:23 +00002454void CGDebugInfo::completeTemplateDefinition(
2455 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002456 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002457 return;
2458
David Blaikie0e716b42014-03-03 23:48:23 +00002459 completeClassData(&SD);
2460 // In case this type has no member function definitions being emitted, ensure
2461 // it is retained
2462 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2463}
2464
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002465llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002466 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002467 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002468
2469 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002470 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002471
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002472 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002473 return T;
2474
Adrian Prantlca844182015-09-11 17:23:03 +00002475 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002476 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002477
2478 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002479 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002480
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 return Res;
2482}
2483
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002484llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002485 // A forward declaration inside a module header does not belong to the module.
2486 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2487 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002488 if (DebugTypeExtRefs && D->isFromASTFile()) {
2489 // Record a reference to an imported clang module or precompiled header.
2490 auto *Reader = CGM.getContext().getExternalSource();
2491 auto Idx = D->getOwningModuleID();
2492 auto Info = Reader->getSourceDescriptor(Idx);
2493 if (Info)
2494 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2495 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002496 // We are building a clang module or a precompiled header.
2497 //
2498 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2499 // and it wouldn't be necessary to specify the parent scope
2500 // because the type is already unique by definition (it would look
2501 // like the output of -fno-standalone-debug). On the other hand,
2502 // the parent scope helps a consumer to quickly locate the object
2503 // file where the type's definition is located, so it might be
2504 // best to make this behavior a command line or debugger tuning
2505 // option.
2506 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2507 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002508 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002509 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2510 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002511 } else {
2512 // This the precompiled header being built.
2513 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002514 }
2515 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002516
Adrian Prantl9402cef2015-09-20 16:51:35 +00002517 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002518}
2519
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002520llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 // Handle qualifiers, which recursively handles what they refer to.
2522 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002523 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002524
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 // Work out details of type.
2526 switch (Ty->getTypeClass()) {
2527#define TYPE(Class, Base)
2528#define ABSTRACT_TYPE(Class, Base)
2529#define NON_CANONICAL_TYPE(Class, Base)
2530#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2531#include "clang/AST/TypeNodes.def"
2532 llvm_unreachable("Dependent types cannot show up in debug information");
2533
2534 case Type::ExtVector:
2535 case Type::Vector:
2536 return CreateType(cast<VectorType>(Ty), Unit);
2537 case Type::ObjCObjectPointer:
2538 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2539 case Type::ObjCObject:
2540 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002541 case Type::ObjCTypeParam:
2542 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 case Type::ObjCInterface:
2544 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2545 case Type::Builtin:
2546 return CreateType(cast<BuiltinType>(Ty));
2547 case Type::Complex:
2548 return CreateType(cast<ComplexType>(Ty));
2549 case Type::Pointer:
2550 return CreateType(cast<PointerType>(Ty), Unit);
2551 case Type::BlockPointer:
2552 return CreateType(cast<BlockPointerType>(Ty), Unit);
2553 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002554 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002556 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002557 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002558 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 case Type::FunctionProto:
2560 case Type::FunctionNoProto:
2561 return CreateType(cast<FunctionType>(Ty), Unit);
2562 case Type::ConstantArray:
2563 case Type::VariableArray:
2564 case Type::IncompleteArray:
2565 return CreateType(cast<ArrayType>(Ty), Unit);
2566
2567 case Type::LValueReference:
2568 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2569 case Type::RValueReference:
2570 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2571
2572 case Type::MemberPointer:
2573 return CreateType(cast<MemberPointerType>(Ty), Unit);
2574
2575 case Type::Atomic:
2576 return CreateType(cast<AtomicType>(Ty), Unit);
2577
Xiuli Pan9c14e282016-01-09 12:53:17 +00002578 case Type::Pipe:
2579 return CreateType(cast<PipeType>(Ty), Unit);
2580
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002582 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2583
David Blaikie42edade2014-11-11 20:44:45 +00002584 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002585 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002586 case Type::Adjusted:
2587 case Type::Decayed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 case Type::Elaborated:
2589 case Type::Paren:
2590 case Type::SubstTemplateTypeParm:
2591 case Type::TypeOfExpr:
2592 case Type::TypeOf:
2593 case Type::Decltype:
2594 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002595 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002596 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002597 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002598
David Blaikie42edade2014-11-11 20:44:45 +00002599 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002600}
2601
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002602llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2603 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002604 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002605
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002606 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002607
2608 // We may have cached a forward decl when we could have created
2609 // a non-forward decl. Go ahead and create a non-forward decl
2610 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002611 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002612 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002613
2614 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002615 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002616
2617 // Propagate members from the declaration to the definition
2618 // CreateType(const RecordType*) will overwrite this with the members in the
2619 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002620 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002621
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002623 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 return Res;
2625}
2626
2627// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002628llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002630
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002632 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 unsigned Line = getLineNumber(RD->getLocation());
2634 StringRef RDName = getClassName(RD);
2635
Amjad Abouddc4531e2016-04-30 01:44:38 +00002636 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002637
David Blaikied2785892013-08-18 17:36:19 +00002638 // If we ended up creating the type during the context chain construction,
2639 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002640 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002641 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002642 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002643 return T;
David Blaikied2785892013-08-18 17:36:19 +00002644
Adrian Prantl381e7552014-02-04 21:29:50 +00002645 // If this is just a forward or incomplete declaration, construct an
2646 // appropriately marked node and just return it.
2647 const RecordDecl *D = RD->getDefinition();
2648 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002649 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002650
2651 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002652 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002653
Manman Rene0064d82013-08-29 23:19:58 +00002654 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2655
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002656 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002657 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2658 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002659
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002660 // Elements of composite types usually have back to the type, creating
2661 // uniquing cycles. Distinct nodes are more efficient.
2662 switch (RealDecl->getTag()) {
2663 default:
2664 llvm_unreachable("invalid composite type tag");
2665
2666 case llvm::dwarf::DW_TAG_array_type:
2667 case llvm::dwarf::DW_TAG_enumeration_type:
2668 // Array elements and most enumeration elements don't have back references,
2669 // so they don't tend to be involved in uniquing cycles and there is some
2670 // chance of merging them when linking together two modules. Only make
2671 // them distinct if they are ODR-uniqued.
2672 if (FullName.empty())
2673 break;
2674
2675 case llvm::dwarf::DW_TAG_structure_type:
2676 case llvm::dwarf::DW_TAG_union_type:
2677 case llvm::dwarf::DW_TAG_class_type:
2678 // Immediatley resolve to a distinct node.
2679 RealDecl =
2680 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2681 break;
2682 }
2683
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002684 RegionMap[Ty->getDecl()].reset(RealDecl);
2685 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002686
David Majnemer58ed0f32016-07-17 00:39:12 +00002687 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002688 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002689 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002690 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002691}
2692
David Blaikieadfbf992013-08-18 16:55:33 +00002693void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002694 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002695 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002696 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002697 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2698 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002699 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002700 while (1) {
2701 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2702 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2703 if (PBT && !BRL.isPrimaryBaseVirtual())
2704 PBase = PBT;
2705 else
2706 break;
2707 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002708 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002709 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2710 getOrCreateFile(RD->getLocation())));
2711 } else if (RD->isDynamicClass())
2712 ContainingType = RealDecl;
2713
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002714 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002715}
2716
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002717llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002718 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002719 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002720 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002721 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002722 llvm::DIType *Ty =
2723 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2724 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 *Offset += FieldSize;
2726 return Ty;
2727}
2728
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002729void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002730 StringRef &Name,
2731 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002732 llvm::DIScope *&FDContext,
2733 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002734 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002735 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002736 Name = getFunctionName(FD);
2737 // Use mangled name as linkage name for C/C++ functions.
2738 if (FD->hasPrototype()) {
2739 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002740 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002741 }
2742 // No need to replicate the linkage name if it isn't different from the
2743 // subprogram name, no need to have it at all unless coverage is enabled or
2744 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002745 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2746 !CGM.getCodeGenOpts().EmitGcovNotes &&
2747 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002748 LinkageName = StringRef();
2749
Benjamin Kramer8c305922016-02-02 11:06:51 +00002750 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002751 if (const NamespaceDecl *NSDecl =
2752 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2753 FDContext = getOrCreateNameSpace(NSDecl);
2754 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002755 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2756 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2757 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2758 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002759 // Check if it is a noreturn-marked function
2760 if (FD->isNoReturn())
2761 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002762 // Collect template parameters.
2763 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2764 }
2765}
2766
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002767void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002768 unsigned &LineNo, QualType &T,
2769 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002770 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002771 Unit = getOrCreateFile(VD->getLocation());
2772 LineNo = getLineNumber(VD->getLocation());
2773
2774 setLocation(VD->getLocation());
2775
2776 T = VD->getType();
2777 if (T->isIncompleteArrayType()) {
2778 // CodeGen turns int[] into int[1] so we'll do the same here.
2779 llvm::APInt ConstVal(32, 1);
2780 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2781
2782 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2783 ArrayType::Normal, 0);
2784 }
2785
2786 Name = VD->getName();
2787 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2788 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2789 LinkageName = CGM.getMangledName(VD);
2790 if (LinkageName == Name)
2791 LinkageName = StringRef();
2792
2793 // Since we emit declarations (DW_AT_members) for static members, place the
2794 // definition of those static members in the namespace they were declared in
2795 // in the source code (the lexical decl context).
2796 // FIXME: Generalize this for even non-member global variables where the
2797 // declaration and definition may have different lexical decl contexts, once
2798 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002799 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2800 : VD->getDeclContext();
2801 // When a record type contains an in-line initialization of a static data
2802 // member, and the record type is marked as __declspec(dllexport), an implicit
2803 // definition of the member will be created in the record context. DWARF
2804 // doesn't seem to have a nice way to describe this in a form that consumers
2805 // are likely to understand, so fake the "normal" situation of a definition
2806 // outside the class by putting it in the global scope.
2807 if (DC->isRecord())
2808 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002809
Amjad Abouddc4531e2016-04-30 01:44:38 +00002810 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2811 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002812}
2813
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002814llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002815CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002816 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002817 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002818 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Frederic Rissd253ed62014-11-18 03:40:51 +00002819 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002820 llvm::DIFile *Unit = getOrCreateFile(Loc);
2821 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002822 unsigned Line = getLineNumber(Loc);
2823
2824 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2825 TParamsArray, Flags);
2826 // Build function type.
2827 SmallVector<QualType, 16> ArgTypes;
2828 for (const ParmVarDecl *Parm: FD->parameters())
2829 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002830 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2831 QualType FnType = CGM.getContext().getFunctionType(
2832 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002833 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002834 DContext, Name, LinkageName, Unit, Line,
2835 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002836 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002837 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002838 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002839 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2840 std::make_tuple(CanonDecl),
2841 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002842 return SP;
2843}
2844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002845llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002846CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2847 QualType T;
2848 StringRef Name, LinkageName;
2849 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002850 llvm::DIFile *Unit = getOrCreateFile(Loc);
2851 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002852 unsigned Line = getLineNumber(Loc);
2853
2854 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002855 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002856 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2857 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Victor Leschuka7ece032016-10-20 00:13:19 +00002858 !VD->isExternallyVisible(), nullptr, nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002859 FwdDeclReplaceMap.emplace_back(
2860 std::piecewise_construct,
2861 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2862 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002863 return GV;
2864}
2865
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002866llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002867 // We only need a declaration (not a definition) of the type - so use whatever
2868 // we would otherwise do to get a type for a pointee. (forward declarations in
2869 // limited debug info, full definitions (if the type definition is available)
2870 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002871 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002872 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002873 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002874 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002875
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002876 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002877 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002878
2879 // No definition for now. Emit a forward definition that might be
2880 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002881 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002882 return getFunctionForwardDeclaration(FD);
2883 else if (const auto *VD = dyn_cast<VarDecl>(D))
2884 return getGlobalVariableForwardDeclaration(VD);
2885
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002886 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002887}
2888
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002889llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002890 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002891 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002892
David Majnemer58ed0f32016-07-17 00:39:12 +00002893 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002894 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002895 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002896
2897 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002898 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002899
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002900 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002901 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002902 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002903 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002904 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002905 }
2906 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002908 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002909 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 return SP;
2911 }
2912
Aaron Ballman86c93902014-03-06 23:45:36 +00002913 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002914 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002915 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002916 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002917 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 return SP;
2919 }
2920 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002921 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002922}
2923
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002924// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002925// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002926llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002927 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002928 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002929 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002930 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2931 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002932 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002933
David Majnemer58ed0f32016-07-17 00:39:12 +00002934 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00002935 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002936
2937 const auto *FTy = FnType->getAs<FunctionType>();
2938 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2939
David Majnemer58ed0f32016-07-17 00:39:12 +00002940 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002942 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002943
2944 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002945 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002946
2947 // Replace the instancetype keyword with the actual type.
2948 if (ResultTy == CGM.getContext().getObjCInstanceType())
2949 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002950 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002951
Adrian Prantl7bec9032013-05-10 21:08:31 +00002952 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002953 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002954 QualType SelfDeclTy;
2955 if (auto *SelfDecl = OMethod->getSelfDecl())
2956 SelfDeclTy = SelfDecl->getType();
2957 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2958 if (FPT->getNumParams() > 1)
2959 SelfDeclTy = FPT->getParamType(0);
2960 if (!SelfDeclTy.isNull())
2961 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002962 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002963 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002964 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00002966 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00002967 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002968 // Variadic methods need a special marker at the end of the type list.
2969 if (OMethod->isVariadic())
2970 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002972 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002973 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2974 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002976
Adrian Prantl800faef2014-02-25 23:42:18 +00002977 // Handle variadic function types; they need an additional
2978 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00002979 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00002980 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002981 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002982 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00002983 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2984 for (QualType ParamType : FPT->param_types())
2985 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00002986 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002987 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002988 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2989 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002990 }
2991
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002992 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002993}
2994
Eric Christophere7b87e52014-10-26 23:40:33 +00002995void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2996 SourceLocation ScopeLoc, QualType FnType,
2997 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002998
2999 StringRef Name;
3000 StringRef LinkageName;
3001
3002 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3003
3004 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003005 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003006
Leny Kholodov80c047d2016-09-06 10:48:04 +00003007 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003008 llvm::DIFile *Unit = getOrCreateFile(Loc);
3009 llvm::DIScope *FDContext = Unit;
3010 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 if (!HasDecl) {
3012 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003013 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003014 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003015 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003016 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003018 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003019 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003020 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003021 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 return;
3023 }
3024 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003025 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3026 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003027 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003029 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 } else {
3031 // Use llvm function name.
3032 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003033 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003034 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003035 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 Name = Name.substr(1);
3037
Adrian Prantl42d71b92014-04-10 23:21:53 +00003038 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003039 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003040 // Artificial functions should not silently reuse CurLoc.
3041 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003042 }
3043 unsigned LineNo = getLineNumber(Loc);
3044 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003045
Eric Christopher8018e412014-03-27 18:50:35 +00003046 // FIXME: The function declaration we're constructing here is mostly reusing
3047 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3048 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3049 // all subprograms instead of the actual context since subprogram definitions
3050 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003051 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003052 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003053 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003054 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003055 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003056 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003057 // We might get here with a VarDecl in the case we're generating
3058 // code for the initialization of globals. Do not record these decls
3059 // as they will overwrite the actual VarDecl Decl in the cache.
3060 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003061 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003062
Adrian Prantlbebb8932014-03-21 21:01:58 +00003063 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003064 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003065
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003067 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003068}
3069
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003070void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3071 QualType FnType) {
3072 StringRef Name;
3073 StringRef LinkageName;
3074
3075 const Decl *D = GD.getDecl();
3076 if (!D)
3077 return;
3078
Leny Kholodov80c047d2016-09-06 10:48:04 +00003079 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003080 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003081 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003082 llvm::DINodeArray TParamsArray;
3083 if (isa<FunctionDecl>(D)) {
3084 // If there is a DISubprogram for this function available then use it.
3085 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3086 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003087 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003088 Name = getObjCMethodName(OMD);
3089 Flags |= llvm::DINode::FlagPrototyped;
3090 } else {
3091 llvm_unreachable("not a function or ObjC method");
3092 }
3093 if (!Name.empty() && Name[0] == '\01')
3094 Name = Name.substr(1);
3095
3096 if (D->isImplicit()) {
3097 Flags |= llvm::DINode::FlagArtificial;
3098 // Artificial functions without a location should not silently reuse CurLoc.
3099 if (Loc.isInvalid())
3100 CurLoc = SourceLocation();
3101 }
3102 unsigned LineNo = getLineNumber(Loc);
3103 unsigned ScopeLine = 0;
3104
Adrian Prantle76bda52016-04-15 15:55:45 +00003105 DBuilder.retainType(DBuilder.createFunction(
3106 FDContext, Name, LinkageName, Unit, LineNo,
3107 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3108 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3109 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003110}
3111
David Blaikie835afb22015-01-21 23:08:17 +00003112void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003113 // Update our current location
3114 setLocation(Loc);
3115
Eric Christophere7b87e52014-10-26 23:40:33 +00003116 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3117 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003118
Adrian Prantle83b1302014-01-07 22:05:52 +00003119 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003120 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00003121 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003122}
3123
Guy Benyei11169dd2012-12-18 14:30:41 +00003124void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003125 llvm::MDNode *Back = nullptr;
3126 if (!LexicalBlockStack.empty())
3127 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003128 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003129 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003130 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003131}
3132
Eric Christopher0fdcb312013-05-16 00:52:20 +00003133void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3134 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 // Set our current location.
3136 setLocation(Loc);
3137
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00003139 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3140 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00003141
Benjamin Kramer8c305922016-02-02 11:06:51 +00003142 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003143 return;
3144
3145 // Create a new lexical block and push it on the stack.
3146 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003147}
3148
Eric Christopher0fdcb312013-05-16 00:52:20 +00003149void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3150 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3152
3153 // Provide an entry in the line table for the end of the block.
3154 EmitLocation(Builder, Loc);
3155
Benjamin Kramer8c305922016-02-02 11:06:51 +00003156 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003157 return;
3158
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 LexicalBlockStack.pop_back();
3160}
3161
Guy Benyei11169dd2012-12-18 14:30:41 +00003162void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3163 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3164 unsigned RCount = FnBeginRegionCount.back();
3165 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3166
3167 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003168 while (LexicalBlockStack.size() != RCount) {
3169 // Provide an entry in the line table for the end of the block.
3170 EmitLocation(Builder, CurLoc);
3171 LexicalBlockStack.pop_back();
3172 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003173 FnBeginRegionCount.pop_back();
3174}
3175
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003176llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003177 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003178
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003179 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003180 QualType FType;
3181 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003182 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003183
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003184 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003185 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003186
3187 FieldOffset = 0;
3188 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3189 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3190 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3191 FType = CGM.getContext().IntTy;
3192 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3193 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3194
3195 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3196 if (HasCopyAndDispose) {
3197 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003198 EltTys.push_back(
3199 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3200 EltTys.push_back(
3201 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003202 }
3203 bool HasByrefExtendedLayout;
3204 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003205 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3206 HasByrefExtendedLayout) &&
3207 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003208 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003209 EltTys.push_back(
3210 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003211 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003212
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3214 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003215 CGM.getTarget().getPointerAlign(0))) {
3216 CharUnits FieldOffsetInBytes =
3217 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003218 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003219 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003220
Guy Benyei11169dd2012-12-18 14:30:41 +00003221 if (NumPaddingBytes.isPositive()) {
3222 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3223 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3224 pad, ArrayType::Normal, 0);
3225 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3226 }
3227 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003228
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003230 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003231 FieldSize = CGM.getContext().getTypeSize(FType);
3232 FieldAlign = CGM.getContext().toBits(Align);
3233
Eric Christopherb2a008c2013-05-16 00:45:12 +00003234 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003235 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003236 FieldAlign, FieldOffset,
3237 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003238 EltTys.push_back(FieldTy);
3239 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003240
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003241 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003242
Leny Kholodov80c047d2016-09-06 10:48:04 +00003243 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003244
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003246 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003247}
3248
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003249void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3250 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003251 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003252 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003253 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003254 if (VD->hasAttr<NoDebugAttr>())
3255 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003256
David Blaikie7fceebf2013-08-19 03:37:48 +00003257 bool Unwritten =
3258 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3259 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003260 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003261 if (!Unwritten)
3262 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003263 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 uint64_t XOffset = 0;
3265 if (VD->hasAttr<BlocksAttr>())
3266 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003267 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003268 Ty = getOrCreateType(VD->getType(), Unit);
3269
3270 // If there is no debug info for this type then do not emit debug info
3271 // for this variable.
3272 if (!Ty)
3273 return;
3274
Guy Benyei11169dd2012-12-18 14:30:41 +00003275 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003276 unsigned Line = 0;
3277 unsigned Column = 0;
3278 if (!Unwritten) {
3279 Line = getLineNumber(VD->getLocation());
3280 Column = getColumnNumber(VD->getLocation());
3281 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003282 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003283 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003284 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003285 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003286
3287 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3288
Guy Benyei11169dd2012-12-18 14:30:41 +00003289 // If this is the first argument and it is implicit then
3290 // give it an object pointer flag.
3291 // FIXME: There has to be a better way to do this, but for static
3292 // functions there won't be an implicit param at arg1 and
3293 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003294 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003295 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003296 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003297 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3298 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003299 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003300
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003301 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003302
3303 StringRef Name = VD->getName();
3304 if (!Name.empty()) {
3305 if (VD->hasAttr<BlocksAttr>()) {
3306 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003307 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003308 // offset of __forwarding field
3309 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003310 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003311 Expr.push_back(offset.getQuantity());
3312 Expr.push_back(llvm::dwarf::DW_OP_deref);
3313 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 // offset of x field
3315 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003316 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003317
3318 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003319 auto *D = ArgNo
3320 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3321 *ArgNo, Unit, Line, Ty)
3322 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003323 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003324
Guy Benyei11169dd2012-12-18 14:30:41 +00003325 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003326 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3327 llvm::DebugLoc::get(Line, Column, Scope),
3328 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003329 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003330 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003331 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003332 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003333 // If VD is an anonymous union then Storage represents value for
3334 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003335 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003336 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003337 // GDB has trouble finding local variables in anonymous unions, so we emit
3338 // artifical local variables for each of the members.
3339 //
3340 // FIXME: Remove this code as soon as GDB supports this.
3341 // The debug info verifier in LLVM operates based on the assumption that a
3342 // variable has the same size as its storage and we had to disable the check
3343 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003344 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003345 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003346 StringRef FieldName = Field->getName();
3347
3348 // Ignore unnamed fields. Do not ignore unnamed records.
3349 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3350 continue;
3351
3352 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003353 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003354 auto *D = DBuilder.createAutoVariable(
3355 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003356 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003357
3358 // Insert an llvm.dbg.declare into the current block.
3359 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3360 llvm::DebugLoc::get(Line, Column, Scope),
3361 Builder.GetInsertBlock());
3362 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003363 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003365
3366 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003367 auto *D = ArgNo
3368 ? DBuilder.createParameterVariable(
3369 Scope, Name, *ArgNo, Unit, Line, Ty,
3370 CGM.getLangOpts().Optimize, Flags)
3371 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3372 CGM.getLangOpts().Optimize, Flags,
3373 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003374
3375 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003376 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3377 llvm::DebugLoc::get(Line, Column, Scope),
3378 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003379}
3380
3381void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3382 llvm::Value *Storage,
3383 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003384 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003385 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003386}
3387
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003388llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3389 llvm::DIType *Ty) {
3390 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003391 if (CachedTy)
3392 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003393 return DBuilder.createObjectPointerType(Ty);
3394}
3395
Eric Christophere7b87e52014-10-26 23:40:33 +00003396void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3397 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003398 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003399 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003401
Craig Topper8a13c412014-05-21 05:09:00 +00003402 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003403 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003404 if (VD->hasAttr<NoDebugAttr>())
3405 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003406
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003408
Guy Benyei11169dd2012-12-18 14:30:41 +00003409 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003410 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3411 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 if (isByRef)
3413 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003414 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003415 Ty = getOrCreateType(VD->getType(), Unit);
3416
3417 // Self is passed along as an implicit non-arg variable in a
3418 // block. Mark it as the object pointer.
3419 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003420 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003421
3422 // Get location information.
3423 unsigned Line = getLineNumber(VD->getLocation());
3424 unsigned Column = getColumnNumber(VD->getLocation());
3425
3426 const llvm::DataLayout &target = CGM.getDataLayout();
3427
3428 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003429 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003430 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3431
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003432 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003433 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003434 addr.push_back(llvm::dwarf::DW_OP_deref);
3435 addr.push_back(llvm::dwarf::DW_OP_plus);
3436 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003437 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003438 addr.push_back(llvm::dwarf::DW_OP_deref);
3439 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003440 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003441 offset =
3442 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003443 addr.push_back(offset.getQuantity());
3444 addr.push_back(llvm::dwarf::DW_OP_deref);
3445 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003446 // offset of x field
3447 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003448 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003449 }
3450
3451 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003452 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003453 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003454 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003455 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003456
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003458 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3459 if (InsertPoint)
3460 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3461 InsertPoint);
3462 else
3463 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3464 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003465}
3466
Guy Benyei11169dd2012-12-18 14:30:41 +00003467void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3468 unsigned ArgNo,
3469 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003470 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003471 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003472}
3473
3474namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003475struct BlockLayoutChunk {
3476 uint64_t OffsetInBits;
3477 const BlockDecl::Capture *Capture;
3478};
3479bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3480 return l.OffsetInBits < r.OffsetInBits;
3481}
Guy Benyei11169dd2012-12-18 14:30:41 +00003482}
3483
3484void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003485 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003486 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003487 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003489 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 ASTContext &C = CGM.getContext();
3491 const BlockDecl *blockDecl = block.getBlockDecl();
3492
3493 // Collect some general information about the block's location.
3494 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003495 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 unsigned line = getLineNumber(loc);
3497 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003498
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003500 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003501
3502 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003503 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003504
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003505 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003506 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003507 blockLayout->getElementOffsetInBits(0),
3508 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003509 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 blockLayout->getElementOffsetInBits(1),
3511 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003512 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003513 blockLayout->getElementOffsetInBits(2),
3514 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003515 auto *FnTy = block.getBlockExpr()->getFunctionType();
3516 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003517 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003518 blockLayout->getElementOffsetInBits(3),
3519 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003520 fields.push_back(createFieldType(
3521 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3522 ? C.getBlockDescriptorExtendedType()
3523 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003524 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003525
3526 // We want to sort the captures by offset, not because DWARF
3527 // requires this, but because we're paranoid about debuggers.
3528 SmallVector<BlockLayoutChunk, 8> chunks;
3529
3530 // 'this' capture.
3531 if (blockDecl->capturesCXXThis()) {
3532 BlockLayoutChunk chunk;
3533 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003534 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003535 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003536 chunks.push_back(chunk);
3537 }
3538
3539 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003540 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003541 const VarDecl *variable = capture.getVariable();
3542 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3543
3544 // Ignore constant captures.
3545 if (captureInfo.isConstant())
3546 continue;
3547
3548 BlockLayoutChunk chunk;
3549 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003550 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003551 chunk.Capture = &capture;
3552 chunks.push_back(chunk);
3553 }
3554
3555 // Sort by offset.
3556 llvm::array_pod_sort(chunks.begin(), chunks.end());
3557
David Majnemer58ed0f32016-07-17 00:39:12 +00003558 for (const BlockLayoutChunk &Chunk : chunks) {
3559 uint64_t offsetInBits = Chunk.OffsetInBits;
3560 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003561
3562 // If we have a null capture, this must be the C++ 'this' capture.
3563 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003564 QualType type;
3565 if (auto *Method =
3566 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3567 type = Method->getThisType(C);
3568 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3569 type = QualType(RDecl->getTypeForDecl(), 0);
3570 else
3571 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003572
David Majnemerb4b671e2016-06-30 03:01:59 +00003573 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 offsetInBits, tunit, tunit));
3575 continue;
3576 }
3577
3578 const VarDecl *variable = capture->getVariable();
3579 StringRef name = variable->getName();
3580
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003581 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003582 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003583 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003584 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003585
3586 // FIXME: this creates a second copy of this type!
3587 uint64_t xoffset;
3588 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003589 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003590 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3591 PtrInfo.Width, Align, offsetInBits,
3592 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003593 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003594 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003595 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003596 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003597 }
3598 fields.push_back(fieldType);
3599 }
3600
3601 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003602 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3603 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003605 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003606
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003607 llvm::DIType *type =
3608 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003609 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003610 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003611 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3612
3613 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003614 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003615 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003616
3617 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003618 auto *debugVar = DBuilder.createParameterVariable(
3619 scope, Arg->getName(), ArgNo, tunit, line, type,
3620 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003621
Adrian Prantl616bef42013-03-14 21:52:59 +00003622 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003623 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003624 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003625 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003626 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003627 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003628
Adrian Prantl616bef42013-03-14 21:52:59 +00003629 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003630 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3631 llvm::DebugLoc::get(line, column, scope),
3632 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003633}
3634
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003635llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003636CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3637 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003638 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003639
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003640 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003641 if (MI != StaticDataMemberCache.end()) {
3642 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003643 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003644 }
David Blaikiece763042013-08-20 21:49:21 +00003645
3646 // If the member wasn't found in the cache, lazily construct and add it to the
3647 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003648 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003649 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003650 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003651}
3652
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003653llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3654 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3655 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3656 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003657
3658 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003659 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003660 StringRef FieldName = Field->getName();
3661
3662 // Ignore unnamed fields, but recurse into anonymous records.
3663 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003664 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Eric Christophercab9fae2014-04-10 05:20:00 +00003665 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3666 Var, DContext);
3667 continue;
3668 }
3669 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003670 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003671 LineNo, FieldTy, Var->hasLocalLinkage());
3672 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003673 }
3674 return GV;
3675}
3676
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003677void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003678 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003679 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003680 if (D->hasAttr<NoDebugAttr>())
3681 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003682
3683 // If we already created a DIGlobalVariable for this declaration, just attach
3684 // it to the llvm::GlobalVariable.
3685 auto Cached = DeclCache.find(D->getCanonicalDecl());
3686 if (Cached != DeclCache.end())
3687 return Var->addDebugInfo(cast<llvm::DIGlobalVariable>(Cached->second));
3688
Guy Benyei11169dd2012-12-18 14:30:41 +00003689 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003690 llvm::DIFile *Unit = nullptr;
3691 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003692 unsigned LineNo;
3693 StringRef DeclName, LinkageName;
3694 QualType T;
3695 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003696
3697 // Attempt to store one global variable for the declaration - even if we
3698 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003699 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003700
3701 // If this is an anonymous union then we'll want to emit a global
3702 // variable for each member of the anonymous union so that it's possible
3703 // to find the name of any field in the union.
3704 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003705 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003706 assert(RD->isAnonymousStructOrUnion() &&
3707 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003708 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3709 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003710 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
David Blaikie7550b112014-10-20 17:42:23 +00003711 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003712 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003713 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003714 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003715 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003716 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003717 DeclCache[D->getCanonicalDecl()].reset(GV);
Guy Benyei11169dd2012-12-18 14:30:41 +00003718}
3719
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003720void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003721 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003722 if (VD->hasAttr<NoDebugAttr>())
3723 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003724 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003725 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003726 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003727 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003728 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003729 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3730 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003731 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3732 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3733 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003734 // Do not use global variables for enums.
3735 //
3736 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003737 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003738 return;
David Blaikiea15565562014-04-04 20:56:17 +00003739 // Do not emit separate definitions for function local const/statics.
3740 if (isa<FunctionDecl>(VD->getDeclContext()))
3741 return;
David Blaikiebb113912014-04-05 07:23:17 +00003742 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003743 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003744 if (VarD->isStaticDataMember()) {
3745 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003746 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003747 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003748 //
3749 // FIXME: This is probably unnecessary, since Ty should reference RD
3750 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003751 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003752 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003753 return;
3754 }
3755
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003756 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003757
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003758 auto &GV = DeclCache[VD];
3759 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003760 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003761 llvm::DIExpression *InitExpr = nullptr;
3762 if (Init.isInt())
3763 InitExpr =
3764 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
David Gross1118d592016-12-08 20:02:46 +00003765 else if (Init.isFloat() && CGM.getContext().getTypeSize(VD->getType()) <= 64)
3766 InitExpr = DBuilder.createConstantValueExpression(
3767 Init.getFloat().bitcastToAPInt().getZExtValue());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003768 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003769 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003770 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3771 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003772}
3773
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003774llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003775 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003776 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003777 llvm::DIScope *Mod = getParentModuleOrNull(D);
3778 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003779}
3780
David Blaikie9f88fe82013-04-22 06:13:21 +00003781void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003782 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003783 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003784 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003785 if (!NSDecl->isAnonymousNamespace() ||
3786 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003787 DBuilder.createImportedModule(
3788 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3789 getOrCreateNameSpace(NSDecl),
3790 getLineNumber(UD.getLocation()));
3791 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003792}
3793
David Blaikiebd483762013-05-20 04:58:53 +00003794void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003795 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003796 return;
3797 assert(UD.shadow_size() &&
3798 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3799 // Emitting one decl is sufficient - debuggers can detect that this is an
3800 // overloaded name & provide lookup for all the overloads.
3801 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003802
3803 // FIXME: Skip functions with undeduced auto return type for now since we
3804 // don't currently have the plumbing for separate declarations & definitions
3805 // of free functions and mismatched types (auto in the declaration, concrete
3806 // return type in the definition)
3807 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3808 if (const auto *AT =
3809 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3810 if (AT->getDeducedType().isNull())
3811 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003812 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003813 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003814 DBuilder.createImportedDeclaration(
3815 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3816 getLineNumber(USD.getLocation()));
3817}
3818
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003819void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003820 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3821 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003822 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003823 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003824 DBuilder.createImportedDeclaration(
3825 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3826 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3827 getLineNumber(ID.getLocation()));
3828 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003829}
3830
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003831llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003832CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003833 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003834 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003835 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003836 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003837 return cast<llvm::DIImportedEntity>(VH);
3838 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003839 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003840 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3841 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003842 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003843 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3844 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3845 NA.getName());
3846 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003847 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003848 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3849 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3850 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003851 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003852 return R;
3853}
3854
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003855llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003856CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003857 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003858 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003859 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003860 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003861
Guy Benyei11169dd2012-12-18 14:30:41 +00003862 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003863 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003864 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00003865 llvm::DINamespace *NS = DBuilder.createNameSpace(
3866 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003867 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003868 return NS;
3869}
3870
Adrian Prantla5206ce2015-09-22 23:26:43 +00003871void CGDebugInfo::setDwoId(uint64_t Signature) {
3872 assert(TheCU && "no main compile unit");
3873 TheCU->setDWOId(Signature);
3874}
3875
3876
Guy Benyei11169dd2012-12-18 14:30:41 +00003877void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003878 // Creating types might create further types - invalidating the current
3879 // element and the size(), so don't cache/reference them.
3880 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3881 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003882 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003883 ? CreateTypeDefinition(E.Type, E.Unit)
3884 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003885 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003886 }
3887
David Blaikief427b002014-05-06 03:42:01 +00003888 for (auto p : ReplaceMap) {
3889 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003890 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003891 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003892
David Blaikief427b002014-05-06 03:42:01 +00003893 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003894 assert(it != TypeCache.end());
3895 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003896
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003897 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3898 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003899 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003900
Frederic Rissd253ed62014-11-18 03:40:51 +00003901 for (const auto &p : FwdDeclReplaceMap) {
3902 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003903 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003904 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003905
3906 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003907 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003908 // with ourselves, that will destroy the temporary MDNode and
3909 // replace it with a standard one, avoiding leaking memory.
3910 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003911 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003912 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003913 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003914
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003915 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003916 }
3917
Adrian Prantl73409ce2013-03-11 18:33:46 +00003918 // We keep our own list of retained types, because we need to look
3919 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003920 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003921 if (auto MD = TypeCache[RT])
3922 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003923
Guy Benyei11169dd2012-12-18 14:30:41 +00003924 DBuilder.finalize();
3925}
David Blaikie66088d52014-09-24 17:01:27 +00003926
3927void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003928 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003929 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003930
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003931 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003932 // Don't ignore in case of explicit cast where it is referenced indirectly.
3933 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003934}
Amara Emerson652795d2016-11-10 14:44:30 +00003935
3936llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
3937 if (LexicalBlockStack.empty())
3938 return llvm::DebugLoc();
3939
3940 llvm::MDNode *Scope = LexicalBlockStack.back();
3941 return llvm::DebugLoc::get(
3942 getLineNumber(Loc), getColumnNumber(Loc), Scope);
3943}