blob: acfe0a4d7d0bff371fda314fad9cc9b81c5c4bcc [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"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000044#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000045#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046using namespace clang;
47using namespace clang::CodeGen;
48
Victor Leschuka7ece032016-10-20 00:13:19 +000049static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
50 auto TI = Ctx.getTypeInfo(Ty);
51 return TI.AlignIsRequired ? TI.Align : 0;
52}
53
54static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
55 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
56}
57
58static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
59 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
60}
61
Guy Benyei11169dd2012-12-18 14:30:41 +000062CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000063 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000064 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000066 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
67 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000068 CreateCompileUnit();
69}
70
71CGDebugInfo::~CGDebugInfo() {
72 assert(LexicalBlockStack.empty() &&
73 "Region stack mismatch, stack not empty!");
74}
75
David Blaikie66e41972015-01-14 07:38:27 +000076ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000077 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000078 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000079 init(TemporaryLocation);
80}
81
Adrian Prantl39428e72015-02-03 18:40:42 +000082ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000083 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000084 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000085 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000086 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000087}
88
89void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000090 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000091 auto *DI = CGF->getDebugInfo();
92 if (!DI) {
93 CGF = nullptr;
94 return;
David Blaikie66e41972015-01-14 07:38:27 +000095 }
David Blaikied7057d92015-08-12 23:49:57 +000096
97 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
98 if (TemporaryLocation.isValid()) {
99 DI->EmitLocation(CGF->Builder, TemporaryLocation);
100 return;
101 }
102
103 if (DefaultToEmpty) {
104 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
105 return;
106 }
107
108 // Construct a location that has a valid scope, but no line info.
109 assert(!DI->LexicalBlockStack.empty());
110 CGF->Builder.SetCurrentDebugLocation(
111 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000112}
113
David Blaikie9b479662015-01-25 01:19:10 +0000114ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000115 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000116 init(E->getExprLoc());
117}
118
David Blaikie66e41972015-01-14 07:38:27 +0000119ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000120 : CGF(&CGF) {
121 if (!CGF.getDebugInfo()) {
122 this->CGF = nullptr;
123 return;
David Blaikie66e41972015-01-14 07:38:27 +0000124 }
David Blaikied7057d92015-08-12 23:49:57 +0000125 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
126 if (Loc)
127 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000128}
129
130ApplyDebugLocation::~ApplyDebugLocation() {
131 // Query CGF so the location isn't overwritten when location updates are
132 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000133 if (CGF)
134 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000135}
136
Guy Benyei11169dd2012-12-18 14:30:41 +0000137void CGDebugInfo::setLocation(SourceLocation Loc) {
138 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000139 if (Loc.isInvalid())
140 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000141
142 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
143
144 // If we've changed files in the middle of a lexical scope go ahead
145 // and create a new lexical scope with file node if it's different
146 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000147 if (LexicalBlockStack.empty())
148 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000149
150 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000151 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000152 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000154 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000155 return;
156
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000157 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000159 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
160 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000161 } else if (isa<llvm::DILexicalBlock>(Scope) ||
162 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000163 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000164 LexicalBlockStack.emplace_back(
165 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 }
167}
168
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000169llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000170 llvm::DIScope *Mod = getParentModuleOrNull(D);
171 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
172 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000173}
174
175llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
176 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000177 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000178 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000180 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000181 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000182 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000183 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 }
185
186 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000187 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000188 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000189
David Majnemer58ed0f32016-07-17 00:39:12 +0000190 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000191 if (!RDecl->isDependentType())
192 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000193 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000194 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000195}
196
Guy Benyei11169dd2012-12-18 14:30:41 +0000197StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000198 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000200 FunctionTemplateSpecializationInfo *Info =
201 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000202
Reid Kleckner31abd802016-06-30 17:41:31 +0000203 // Emit the unqualified name in normal operation. LLVM and the debugger can
204 // compute the fully qualified name from the scope chain. If we're only
205 // emitting line table info, there won't be any scope chains, so emit the
206 // fully qualified name here so that stack traces are more accurate.
207 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
208 // evaluating the size impact.
209 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
210 CGM.getCodeGenOpts().EmitCodeView;
211
212 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 return FII->getName();
214
Benjamin Kramer9170e912013-02-22 15:46:01 +0000215 SmallString<128> NS;
216 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000217 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000218 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000219 if (!UseQualifiedName)
220 FD->printName(OS);
221 else
222 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000223
Reid Kleckner829398e2016-06-17 16:11:20 +0000224 // Add any template specialization args.
225 if (Info) {
226 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000227 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000228 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000229 }
230
231 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000232 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000233}
234
235StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
236 SmallString<256> MethodName;
237 llvm::raw_svector_ostream OS(MethodName);
238 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
239 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000240 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000242 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000243 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000244 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000245 if (OC->IsClassExtension()) {
246 OS << OC->getClassInterface()->getName();
247 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000248 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000249 << OC->getIdentifier()->getNameStart() << ')';
250 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000251 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000252 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
253 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000254 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000255 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000256 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000257 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000258 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000259 ClassTy.print(OS, PrintingPolicy(LangOptions()));
260 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000261 }
262 OS << ' ' << OMD->getSelector().getAsString() << ']';
263
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000264 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000265}
266
Guy Benyei11169dd2012-12-18 14:30:41 +0000267StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000268 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269}
270
Eric Christophere7b87e52014-10-26 23:40:33 +0000271StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000272 if (isa<ClassTemplateSpecializationDecl>(RD)) {
273 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000274 llvm::raw_svector_ostream OS(Name);
275 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
276 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000277
278 // Copy this name on the side and use its reference.
279 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000280 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000281
David Majnemerbc5976a2016-07-01 23:12:54 +0000282 // quick optimization to avoid having to intern strings that are already
283 // stored reliably elsewhere
284 if (const IdentifierInfo *II = RD->getIdentifier())
285 return II->getName();
286
287 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
288 // used to reconstruct the fully qualified type names.
289 if (CGM.getCodeGenOpts().EmitCodeView) {
290 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
291 assert(RD->getDeclContext() == D->getDeclContext() &&
292 "Typedef should not be in another decl context!");
293 assert(D->getDeclName().getAsIdentifierInfo() &&
294 "Typedef was not named!");
295 return D->getDeclName().getAsIdentifierInfo()->getName();
296 }
297
298 if (CGM.getLangOpts().CPlusPlus) {
299 StringRef Name;
300
301 ASTContext &Context = CGM.getContext();
302 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
303 // Anonymous types without a name for linkage purposes have their
304 // declarator mangled in if they have one.
305 Name = DD->getName();
306 else if (const TypedefNameDecl *TND =
307 Context.getTypedefNameForUnnamedTagDecl(RD))
308 // Anonymous types without a name for linkage purposes have their
309 // associate typedef mangled in if they have one.
310 Name = TND->getName();
311
312 if (!Name.empty()) {
313 SmallString<256> UnnamedType("<unnamed-type-");
314 UnnamedType += Name;
315 UnnamedType += '>';
316 return internString(UnnamedType);
317 }
318 }
319 }
320
321 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000322}
323
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000324llvm::DIFile::ChecksumKind
325CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
326 Checksum.clear();
327
328 if (!CGM.getCodeGenOpts().EmitCodeView)
329 return llvm::DIFile::CSK_None;
330
331 SourceManager &SM = CGM.getContext().getSourceManager();
332 bool Invalid;
333 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
334 if (Invalid)
335 return llvm::DIFile::CSK_None;
336
337 llvm::MD5 Hash;
338 llvm::MD5::MD5Result Result;
339
340 Hash.update(MemBuffer->getBuffer());
341 Hash.final(Result);
342
343 Hash.stringifyResult(Result, Checksum);
344 return llvm::DIFile::CSK_MD5;
345}
346
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000347llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000348 if (!Loc.isValid())
349 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000350 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000351 remapDIPath(TheCU->getDirectory()),
352 TheCU->getFile()->getChecksumKind(),
353 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000354
355 SourceManager &SM = CGM.getContext().getSourceManager();
356 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
357
358 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
359 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000360 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000361 remapDIPath(TheCU->getDirectory()),
362 TheCU->getFile()->getChecksumKind(),
363 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000364
365 // Cache the results.
366 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000367 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000368
369 if (it != DIFileCache.end()) {
370 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000371 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000372 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000373 }
374
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000375 SmallString<32> Checksum;
376 llvm::DIFile::ChecksumKind CSKind =
377 computeChecksum(SM.getFileID(Loc), Checksum);
378
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000379 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000380 remapDIPath(getCurrentDirname()),
381 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000382
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000383 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 return F;
385}
386
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000387llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000388 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000389 remapDIPath(TheCU->getDirectory()),
390 TheCU->getFile()->getChecksumKind(),
391 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000392}
393
394std::string CGDebugInfo::remapDIPath(StringRef Path) const {
395 for (const auto &Entry : DebugPrefixMap)
396 if (Path.startswith(Entry.first))
397 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
398 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000399}
400
Guy Benyei11169dd2012-12-18 14:30:41 +0000401unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
402 if (Loc.isInvalid() && CurLoc.isInvalid())
403 return 0;
404 SourceManager &SM = CGM.getContext().getSourceManager();
405 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000406 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000407}
408
Adrian Prantlc7822422013-03-12 20:43:25 +0000409unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000411 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 return 0;
413
414 // If the location is invalid then use the current column.
415 if (Loc.isInvalid() && CurLoc.isInvalid())
416 return 0;
417 SourceManager &SM = CGM.getContext().getSourceManager();
418 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000419 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000420}
421
422StringRef CGDebugInfo::getCurrentDirname() {
423 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
424 return CGM.getCodeGenOpts().DebugCompilationDir;
425
426 if (!CWDName.empty())
427 return CWDName;
428 SmallString<256> CWD;
429 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000430 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000431}
432
Guy Benyei11169dd2012-12-18 14:30:41 +0000433void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000434 SmallString<32> Checksum;
435 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000436
David Blaikieaabde052014-05-14 00:29:00 +0000437 // Should we be asking the SourceManager for the main file name, instead of
438 // accepting it as an argument? This just causes the main file name to
439 // mismatch with source locations and create extra lexical scopes or
440 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
441 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
442 // because that's what the SourceManager says)
443
Guy Benyei11169dd2012-12-18 14:30:41 +0000444 // Get absolute path name.
445 SourceManager &SM = CGM.getContext().getSourceManager();
446 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
447 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000448 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000449
450 // The main file name provided via the "-main-file-name" option contains just
451 // the file name itself with no path information. This file name may have had
452 // a relative path, so we look into the actual file entry for the main
453 // file to determine the real absolute path for the file.
454 std::string MainFileDir;
455 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000456 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000457 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000458 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
459 llvm::sys::path::append(MainFileDirSS, MainFileName);
460 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000461 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000462 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 }
464
Ed Masteda706022014-05-07 12:49:30 +0000465 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 const LangOptions &LO = CGM.getLangOpts();
467 if (LO.CPlusPlus) {
468 if (LO.ObjC1)
469 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
470 else
471 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
472 } else if (LO.ObjC1) {
473 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000474 } else if (LO.RenderScript) {
475 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000476 } else if (LO.C99) {
477 LangTag = llvm::dwarf::DW_LANG_C99;
478 } else {
479 LangTag = llvm::dwarf::DW_LANG_C89;
480 }
481
482 std::string Producer = getClangFullVersion();
483
484 // Figure out which version of the ObjC runtime we have.
485 unsigned RuntimeVers = 0;
486 if (LO.ObjC1)
487 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
488
Adrian Prantl826824e2016-04-08 22:43:06 +0000489 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
490 switch (DebugKind) {
491 case codegenoptions::NoDebugInfo:
492 case codegenoptions::LocTrackingOnly:
493 EmissionKind = llvm::DICompileUnit::NoDebug;
494 break;
495 case codegenoptions::DebugLineTablesOnly:
496 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
497 break;
498 case codegenoptions::LimitedDebugInfo:
499 case codegenoptions::FullDebugInfo:
500 EmissionKind = llvm::DICompileUnit::FullDebug;
501 break;
502 }
503
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000506 TheCU = DBuilder.createCompileUnit(
Amjad Aboudfa9a17e2016-12-14 20:24:40 +0000507 LangTag, DBuilder.createFile(remapDIPath(MainFileName),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000508 remapDIPath(getCurrentDirname()), CSKind,
509 Checksum),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000510 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000511 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
Dehao Chen5a3f8902017-02-01 22:45:21 +0000512 CGM.getCodeGenOpts().SplitDwarfInlining,
513 CGM.getCodeGenOpts().DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000514}
515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000516llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000517 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 StringRef BTName;
519 switch (BT->getKind()) {
520#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000521#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000522#include "clang/AST/BuiltinTypes.def"
523 case BuiltinType::Dependent:
524 llvm_unreachable("Unexpected builtin type");
525 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000526 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000528 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000530 if (!ClassTy)
531 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
532 "objc_class", TheCU,
533 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 return ClassTy;
535 case BuiltinType::ObjCId: {
536 // typedef struct objc_class *Class;
537 // typedef struct objc_object {
538 // Class isa;
539 // } *id;
540
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000541 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 return ObjTy;
543
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000544 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
546 "objc_class", TheCU,
547 getOrCreateMainFile(), 0);
548
549 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000550
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000551 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000552
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000553 ObjTy = DBuilder.createStructType(
554 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
555 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000556
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000557 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000558 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
559 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
560 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 return ObjTy;
562 }
563 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000564 if (!SelTy)
565 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
566 "objc_selector", TheCU,
567 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 return SelTy;
569 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000570
Alexey Bader954ba212016-04-08 13:40:33 +0000571#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
572 case BuiltinType::Id: \
573 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
574 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000575#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000576 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000577 return getOrCreateStructPtrType("opencl_sampler_t",
578 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000579 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000580 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000581 case BuiltinType::OCLClkEvent:
582 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
583 case BuiltinType::OCLQueue:
584 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000585 case BuiltinType::OCLReserveID:
586 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000587
Guy Benyei11169dd2012-12-18 14:30:41 +0000588 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000589 case BuiltinType::Char_U:
590 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
591 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000593 case BuiltinType::SChar:
594 Encoding = llvm::dwarf::DW_ATE_signed_char;
595 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000596 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000597 case BuiltinType::Char32:
598 Encoding = llvm::dwarf::DW_ATE_UTF;
599 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000600 case BuiltinType::UShort:
601 case BuiltinType::UInt:
602 case BuiltinType::UInt128:
603 case BuiltinType::ULong:
604 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000605 case BuiltinType::ULongLong:
606 Encoding = llvm::dwarf::DW_ATE_unsigned;
607 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000608 case BuiltinType::Short:
609 case BuiltinType::Int:
610 case BuiltinType::Int128:
611 case BuiltinType::Long:
612 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000613 case BuiltinType::LongLong:
614 Encoding = llvm::dwarf::DW_ATE_signed;
615 break;
616 case BuiltinType::Bool:
617 Encoding = llvm::dwarf::DW_ATE_boolean;
618 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000619 case BuiltinType::Half:
620 case BuiltinType::Float:
621 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000622 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000623 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000624 // FIXME: For targets where long double and __float128 have the same size,
625 // they are currently indistinguishable in the debugger without some
626 // special treatment. However, there is currently no consensus on encoding
627 // and this should be updated once a DWARF encoding exists for distinct
628 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000629 Encoding = llvm::dwarf::DW_ATE_float;
630 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000631 }
632
633 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000634 case BuiltinType::Long:
635 BTName = "long int";
636 break;
637 case BuiltinType::LongLong:
638 BTName = "long long int";
639 break;
640 case BuiltinType::ULong:
641 BTName = "long unsigned int";
642 break;
643 case BuiltinType::ULongLong:
644 BTName = "long long unsigned int";
645 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000646 default:
647 BTName = BT->getName(CGM.getLangOpts());
648 break;
649 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000650 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000652 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000653}
654
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000655llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000656 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000657 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000658 if (Ty->isComplexIntegerType())
659 Encoding = llvm::dwarf::DW_ATE_lo_user;
660
661 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000662 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000663}
664
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000665llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
666 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000667 QualifierCollector Qc;
668 const Type *T = Qc.strip(Ty);
669
670 // Ignore these qualifiers for now.
671 Qc.removeObjCGCAttr();
672 Qc.removeAddressSpace();
673 Qc.removeObjCLifetime();
674
675 // We will create one Derived type for one qualifier and recurse to handle any
676 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000677 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 if (Qc.hasConst()) {
679 Tag = llvm::dwarf::DW_TAG_const_type;
680 Qc.removeConst();
681 } else if (Qc.hasVolatile()) {
682 Tag = llvm::dwarf::DW_TAG_volatile_type;
683 Qc.removeVolatile();
684 } else if (Qc.hasRestrict()) {
685 Tag = llvm::dwarf::DW_TAG_restrict_type;
686 Qc.removeRestrict();
687 } else {
688 assert(Qc.empty() && "Unknown type qualifier for debug info");
689 return getOrCreateType(QualType(T, 0), Unit);
690 }
691
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000692 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000693
694 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
695 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000696 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000697}
698
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000699llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
700 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000701
702 // The frontend treats 'id' as a typedef to an ObjCObjectType,
703 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
704 // debug info, we want to emit 'id' in both cases.
705 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000706 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000707
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000708 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
709 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000710}
711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000712llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
713 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000714 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000715 Ty->getPointeeType(), Unit);
716}
717
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000718/// \return whether a C++ mangling exists for the type defined by TD.
719static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
720 switch (TheCU->getSourceLanguage()) {
721 case llvm::dwarf::DW_LANG_C_plus_plus:
722 return true;
723 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
724 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
725 default:
726 return false;
727 }
728}
729
Manman Rene0064d82013-08-29 23:19:58 +0000730/// In C++ mode, types have linkage, so we can rely on the ODR and
731/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000732static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
733 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000734 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000735 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000736 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000737
738 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000739 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000740
Manman Rene0064d82013-08-29 23:19:58 +0000741 // TODO: This is using the RTTI name. Is there a better way to get
742 // a unique string for a type?
743 llvm::raw_svector_ostream Out(FullName);
744 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000745 return FullName;
746}
747
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000748/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000749static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
750 llvm::dwarf::Tag Tag;
751 if (RD->isStruct() || RD->isInterface())
752 Tag = llvm::dwarf::DW_TAG_structure_type;
753 else if (RD->isUnion())
754 Tag = llvm::dwarf::DW_TAG_union_type;
755 else {
756 // FIXME: This could be a struct type giving a default visibility different
757 // than C++ class type, but needs llvm metadata changes first.
758 assert(RD->isClass());
759 Tag = llvm::dwarf::DW_TAG_class_type;
760 }
761 return Tag;
762}
763
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000764llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000765CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000766 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000767 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000768 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
769 return cast<llvm::DICompositeType>(T);
770 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 unsigned Line = getLineNumber(RD->getLocation());
772 StringRef RDName = getClassName(RD);
773
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000774 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000775 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000776
Guy Benyei11169dd2012-12-18 14:30:41 +0000777 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000778 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000779 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000780 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000782 ReplaceMap.emplace_back(
783 std::piecewise_construct, std::make_tuple(Ty),
784 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000785 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000786}
787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000788llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000789 const Type *Ty,
790 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000791 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000792 // Bit size, align and offset of the type.
793 // Size is always the size of a pointer. We can't use getTypeSize here
794 // because that does not return the correct value for references.
795 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000796 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +0000797 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +0000798
Keno Fischer87842f32015-11-16 09:04:13 +0000799 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
800 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
801 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
802 Size, Align);
803 else
804 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
805 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000806}
807
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000808llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
809 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000810 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000811 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000812 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
813 TheCU, getOrCreateMainFile(), 0);
814 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
815 Cache = DBuilder.createPointerType(Cache, Size);
816 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000817}
818
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000819llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
820 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000821 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000822 QualType FType;
823 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000824 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000825 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000826
827 FieldOffset = 0;
828 FType = CGM.getContext().UnsignedLongTy;
829 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
830 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
831
832 Elements = DBuilder.getOrCreateArray(EltTys);
833 EltTys.clear();
834
Leny Kholodov80c047d2016-09-06 10:48:04 +0000835 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000836 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000837
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000838 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000839 DBuilder.createStructType(Unit, "__block_descriptor", 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
842 // Bit size, align and offset of the type.
843 uint64_t Size = CGM.getContext().getTypeSize(Ty);
844
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000845 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000846
847 FieldOffset = 0;
848 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
849 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
850 FType = CGM.getContext().IntTy;
851 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
852 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000853 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
855
856 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000857 FieldSize = CGM.getContext().getTypeSize(Ty);
858 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000859 EltTys.push_back(DBuilder.createMemberType(
860 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
861 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000862
863 FieldOffset += FieldSize;
864 Elements = DBuilder.getOrCreateArray(EltTys);
865
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000866 // The __block_literal_generic structs are marked with a special
867 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
868 // the debugger needs to know about. To allow type uniquing, emit
869 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000870 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000871 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000872 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000873
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000874 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000875}
876
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
878 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000879 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000880 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000881
882 SmallString<128> NS;
883 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000884 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
885 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000886
887 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000888 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000889 CGM.getContext().getPrintingPolicy());
890
David Majnemer58ed0f32016-07-17 00:39:12 +0000891 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000892 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000893
894 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000895 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
896 getLineNumber(Loc),
897 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000898}
899
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000900llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
901 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000902 // We don't set size information, but do specify where the typedef was
903 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000904 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000905
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000906 // Typedefs are derived from some other type.
907 return DBuilder.createTypedef(
908 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
909 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000910 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000911}
912
Reid Klecknerf00f8032016-06-08 20:41:54 +0000913static unsigned getDwarfCC(CallingConv CC) {
914 switch (CC) {
915 case CC_C:
916 // Avoid emitting DW_AT_calling_convention if the C convention was used.
917 return 0;
918
919 case CC_X86StdCall:
920 return llvm::dwarf::DW_CC_BORLAND_stdcall;
921 case CC_X86FastCall:
922 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
923 case CC_X86ThisCall:
924 return llvm::dwarf::DW_CC_BORLAND_thiscall;
925 case CC_X86VectorCall:
926 return llvm::dwarf::DW_CC_LLVM_vectorcall;
927 case CC_X86Pascal:
928 return llvm::dwarf::DW_CC_BORLAND_pascal;
929
930 // FIXME: Create new DW_CC_ codes for these calling conventions.
931 case CC_X86_64Win64:
932 case CC_X86_64SysV:
933 case CC_AAPCS:
934 case CC_AAPCS_VFP:
935 case CC_IntelOclBicc:
936 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000937 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000938 case CC_Swift:
939 case CC_PreserveMost:
940 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000941 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000942 return 0;
943 }
944 return 0;
945}
946
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000947llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
948 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000949 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000950
951 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000952 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000953
954 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000955 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 if (isa<FunctionNoProtoType>(Ty))
957 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000958 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
959 for (const QualType &ParamType : FPT->param_types())
960 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000961 if (FPT->isVariadic())
962 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000963 }
964
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000965 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000966 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000967 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000968}
969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000970/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000971/// As an optimization, return 0 if the access specifier equals the
972/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000973static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
974 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +0000975 AccessSpecifier Default = clang::AS_none;
976 if (RD && RD->isClass())
977 Default = clang::AS_private;
978 else if (RD && (RD->isStruct() || RD->isUnion()))
979 Default = clang::AS_public;
980
981 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +0000982 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000983
Eric Christophere7b87e52014-10-26 23:40:33 +0000984 switch (Access) {
985 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000986 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000987 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000988 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000989 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000990 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000991 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +0000992 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000993 }
994 llvm_unreachable("unexpected access enumerator");
995}
Guy Benyei11169dd2012-12-18 14:30:41 +0000996
David Majnemerb4b671e2016-06-30 03:01:59 +0000997llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
998 llvm::DIScope *RecordTy,
999 const RecordDecl *RD) {
1000 StringRef Name = BitFieldDecl->getName();
1001 QualType Ty = BitFieldDecl->getType();
1002 SourceLocation Loc = BitFieldDecl->getLocation();
1003 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1004 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1005
1006 // Get the location for the field.
1007 llvm::DIFile *File = getOrCreateFile(Loc);
1008 unsigned Line = getLineNumber(Loc);
1009
1010 const CGBitFieldInfo &BitFieldInfo =
1011 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1012 uint64_t SizeInBits = BitFieldInfo.Size;
1013 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001014 uint64_t StorageOffsetInBits =
1015 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1016 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001017 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001018 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001019 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1020 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001021}
1022
1023llvm::DIType *
1024CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1025 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001026 uint32_t AlignInBits, llvm::DIFile *tunit,
1027 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001028 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001029
1030 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001032 unsigned line = getLineNumber(loc);
1033
David Majnemer34b57492014-07-30 01:30:47 +00001034 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001035 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001036 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001037 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1038 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001039 if (!Align)
1040 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001041 }
1042
Leny Kholodov80c047d2016-09-06 10:48:04 +00001043 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001044 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001045 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001046}
1047
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001048void CGDebugInfo::CollectRecordLambdaFields(
1049 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001050 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001051 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1052 // has the name and the location of the variable so we should iterate over
1053 // both concurrently.
1054 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1055 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1056 unsigned fieldno = 0;
1057 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001058 E = CXXDecl->captures_end();
1059 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001060 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001061 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001062 SourceLocation Loc = C.getLocation();
1063 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001064 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001065 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001066 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001067 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001068 llvm::DIType *FieldType = createFieldType(
1069 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001070 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001071 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001072 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001073 // TODO: Need to handle 'this' in some way by probably renaming the
1074 // this of the lambda class and having a field member of 'this' or
1075 // by using AT_object_pointer for the function and having that be
1076 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001077 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001079 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001081 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001082 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001083
1084 elements.push_back(fieldType);
1085 }
1086 }
1087}
1088
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001089llvm::DIDerivedType *
1090CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001091 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001092 // Create the descriptor for the static variable, with or without
1093 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001094 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001095 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1096 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001097
Eric Christopher91a31902013-01-16 01:22:32 +00001098 unsigned LineNumber = getLineNumber(Var->getLocation());
1099 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001100 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001101 if (Var->getInit()) {
1102 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001103 if (Value) {
1104 if (Value->isInt())
1105 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1106 if (Value->isFloat())
1107 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1108 }
Eric Christopher91a31902013-01-16 01:22:32 +00001109 }
1110
Leny Kholodov80c047d2016-09-06 10:48:04 +00001111 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001112 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001113 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001114 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001115 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001116 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001117}
1118
Eric Christophere7b87e52014-10-26 23:40:33 +00001119void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001120 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1121 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001122 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001123 StringRef name = field->getName();
1124 QualType type = field->getType();
1125
1126 // Ignore unnamed fields unless they're anonymous structs/unions.
1127 if (name.empty() && !type->isRecordType())
1128 return;
1129
David Majnemerb4b671e2016-06-30 03:01:59 +00001130 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001131 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001132 FieldType = createBitFieldType(field, RecordTy, RD);
1133 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001134 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001135 FieldType =
1136 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001137 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001138 }
1139
David Majnemerb4b671e2016-06-30 03:01:59 +00001140 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001141}
1142
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001143void CGDebugInfo::CollectRecordNestedRecord(
1144 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1145 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001146 // Injected class names are not considered nested records.
1147 if (isa<InjectedClassNameType>(Ty))
1148 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001149 SourceLocation Loc = RD->getLocation();
1150 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1151 elements.push_back(nestedType);
1152}
1153
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001154void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001155 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001156 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001158 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001159
Eric Christopher91a31902013-01-16 01:22:32 +00001160 if (CXXDecl && CXXDecl->isLambda())
1161 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1162 else {
1163 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001164
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001165 // Debug info for nested records is included in the member list only for
1166 // CodeView.
1167 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1168
Eric Christopher91a31902013-01-16 01:22:32 +00001169 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001170 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001171
Eric Christopher91a31902013-01-16 01:22:32 +00001172 // Static and non-static members should appear in the same order as
1173 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001174 for (const auto *I : record->decls())
1175 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001176 if (V->hasAttr<NoDebugAttr>())
1177 continue;
David Blaikiece763042013-08-20 21:49:21 +00001178 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001179 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001180 if (MI != StaticDataMemberCache.end()) {
1181 assert(MI->second &&
1182 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001183 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001184 } else {
1185 auto Field = CreateRecordStaticField(V, RecordTy, record);
1186 elements.push_back(Field);
1187 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001188 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001189 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1190 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001191
1192 // Bump field number for next field.
1193 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001194 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1195 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1196 nestedRec->getDeclContext() == record)
1197 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001198 }
1199}
1200
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001201llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001202CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001203 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001204 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001205 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001206 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001207 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001208 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1209 Func, Unit);
1210}
David Blaikie2aaf0652013-01-07 22:24:59 +00001211
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1213 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001215 llvm::DITypeRefArray Args(
1216 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001217 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001218 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001219
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001220 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001221
1222 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001223 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001224
David Blaikie2aaf0652013-01-07 22:24:59 +00001225 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001226 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001227 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1228 // Create pointer type directly in this case.
1229 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1230 QualType PointeeTy = ThisPtrTy->getPointeeType();
1231 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001232 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001233 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1235 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001236 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001237 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001238 // TODO: This and the artificial type below are misleading, the
1239 // types aren't artificial the argument is, but the current
1240 // metadata doesn't represent that.
1241 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1242 Elts.push_back(ThisPtrType);
1243 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001244 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001245 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001246 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1247 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 }
1249
1250 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001251 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1252 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001253
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001254 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001255
Leny Kholodov80c047d2016-09-06 10:48:04 +00001256 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001257 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001259 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001261
Reid Klecknerf00f8032016-06-08 20:41:54 +00001262 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1263 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001264}
1265
Eric Christopherb2a008c2013-05-16 00:45:12 +00001266/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001267/// inside a function.
1268static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001269 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001270 return isFunctionLocalClass(NRD);
1271 if (isa<FunctionDecl>(RD->getDeclContext()))
1272 return true;
1273 return false;
1274}
1275
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001276llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1277 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001278 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001279 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001280
Guy Benyei11169dd2012-12-18 14:30:41 +00001281 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001282 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001283
1284 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1285 // make sense to give a single ctor/dtor a linkage name.
1286 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001287 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1288 // property to use here. It may've been intended to model "is non-external
1289 // type" but misses cases of non-function-local but non-external classes such
1290 // as those in anonymous namespaces as well as the reverse - external types
1291 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001292 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1293 MethodLinkageName = CGM.getMangledName(Method);
1294
1295 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001296 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001297 unsigned MethodLine = 0;
1298 if (!Method->isImplicit()) {
1299 MethodDefUnit = getOrCreateFile(Method->getLocation());
1300 MethodLine = getLineNumber(Method->getLocation());
1301 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001302
1303 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001304 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001305 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001307 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001308 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001309
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 if (Method->isVirtual()) {
1311 if (Method->isPure())
1312 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1313 else
1314 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001315
Reid Kleckner216d0a12016-06-16 20:08:51 +00001316 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1317 // It doesn't make sense to give a virtual destructor a vtable index,
1318 // since a single destructor has two entries in the vtable.
1319 if (!isa<CXXDestructorDecl>(Method))
1320 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1321 } else {
1322 // Emit MS ABI vftable information. There is only one entry for the
1323 // deleting dtor.
1324 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1325 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1326 MicrosoftVTableContext::MethodVFTableLocation ML =
1327 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1328 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001329
1330 // CodeView only records the vftable offset in the class that introduces
1331 // the virtual method. This is possible because, unlike Itanium, the MS
1332 // C++ ABI does not include all virtual methods from non-primary bases in
1333 // the vtable for the most derived class. For example, if C inherits from
1334 // A and B, C's primary vftable will not include B's virtual methods.
1335 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1336 Flags |= llvm::DINode::FlagIntroducedVirtual;
1337
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001338 // The 'this' adjustment accounts for both the virtual and non-virtual
1339 // portions of the adjustment. Presumably the debugger only uses it when
1340 // it knows the dynamic type of an object.
1341 ThisAdjustment = CGM.getCXXABI()
1342 .getVirtualFunctionPrologueThisAdjustment(GD)
1343 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001344 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001345 ContainingType = RecordTy;
1346 }
1347
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001349 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001350 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001351 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001353 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001354 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001356 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 }
1358 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001359 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001360 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001362 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001363 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001364
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1366 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001367 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001368 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1369 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1370 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001371
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001372 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001373
1374 return SP;
1375}
1376
Eric Christophere7b87e52014-10-26 23:40:33 +00001377void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001378 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1379 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001380
1381 // Since we want more than just the individual member decls if we
1382 // have templated functions iterate over every declaration to gather
1383 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001384 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001385 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1386 // If the member is implicit, don't add it to the member list. This avoids
1387 // the member being added to type units by LLVM, while still allowing it
1388 // to be emitted into the type declaration/reference inside the compile
1389 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001390 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001391 // FIXME: Handle Using(Shadow?)Decls here to create
1392 // DW_TAG_imported_declarations inside the class for base decls brought into
1393 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1394 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1395 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001396 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001397 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001398
1399 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1400 continue;
1401
David Blaikiefd580722014-10-06 05:18:55 +00001402 // Reuse the existing member function declaration if it exists.
1403 // It may be associated with the declaration of the type & should be
1404 // reused as we're building the definition.
1405 //
1406 // This situation can arise in the vtable-based debug info reduction where
1407 // implicit members are emitted in a non-vtable TU.
1408 auto MI = SPCache.find(Method->getCanonicalDecl());
1409 EltTys.push_back(MI == SPCache.end()
1410 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001411 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001412 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001413}
Guy Benyei11169dd2012-12-18 14:30:41 +00001414
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001415void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001416 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001417 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001418 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1419 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1420 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001421
Bob Haarmandff36732016-10-25 22:19:32 +00001422 // If we are generating CodeView debug info, we also need to emit records for
1423 // indirect virtual base classes.
1424 if (CGM.getCodeGenOpts().EmitCodeView) {
1425 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1426 llvm::DINode::FlagIndirectVirtualBase);
1427 }
1428}
1429
1430void CGDebugInfo::CollectCXXBasesAux(
1431 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1432 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1433 const CXXRecordDecl::base_class_const_range &Bases,
1434 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1435 llvm::DINode::DIFlags StartingFlags) {
1436 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1437 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001438 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001439 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001440 if (!SeenTypes.insert(Base).second)
1441 continue;
1442 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1443 llvm::DINode::DIFlags BFlags = StartingFlags;
1444 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001445
Aaron Ballman574705e2014-03-13 15:41:46 +00001446 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001447 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1448 // virtual base offset offset is -ve. The code generator emits dwarf
1449 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001450 BaseOffset = 0 - CGM.getItaniumVTableContext()
1451 .getVirtualBaseOffsetOffset(RD, Base)
1452 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001453 } else {
1454 // In the MS ABI, store the vbtable offset, which is analogous to the
1455 // vbase offset offset in Itanium.
1456 BaseOffset =
1457 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1458 }
Bob Haarmandff36732016-10-25 22:19:32 +00001459 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001460 } else
1461 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1462 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1463 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001464
Adrian Prantl21361fb2014-08-29 22:44:27 +00001465 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001466 llvm::DIType *DTy =
1467 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001468 EltTys.push_back(DTy);
1469 }
1470}
1471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001473CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1474 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001475 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001476 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1478 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001479 StringRef Name;
1480 if (TPList)
1481 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001482 switch (TA.getKind()) {
1483 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001484 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001485 TemplateParams.push_back(
1486 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001487 } break;
1488 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001489 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001490 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1491 TheCU, Name, TTy,
1492 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001493 } break;
1494 case TemplateArgument::Declaration: {
1495 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001496 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001497 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001498 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001499 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001500 // Variable pointer template parameters have a value that is the address
1501 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001502 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001503 V = CGM.GetAddrOfGlobalVar(VD);
1504 // Member function pointers have special support for building them, though
1505 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001506 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001507 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001508 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001509 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001510 // Member data pointers have special handling too to compute the fixed
1511 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001512 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001513 // These five lines (& possibly the above member function pointer
1514 // handling) might be able to be refactored to use similar code in
1515 // CodeGenModule::getMemberPointerConstant
1516 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1517 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001518 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001519 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001520 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001521 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1522 TheCU, Name, TTy,
1523 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001524 } break;
1525 case TemplateArgument::NullPtr: {
1526 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001527 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001528 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001529 // Special case member data pointer null values since they're actually -1
1530 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001531 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001532 // But treat member function pointers as simple zero integers because
1533 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1534 // CodeGen grows handling for values of non-null member function
1535 // pointers then perhaps we could remove this special case and rely on
1536 // EmitNullMemberPointer for member function pointers.
1537 if (MPT->isMemberDataPointer())
1538 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1539 if (!V)
1540 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001541 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001542 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001543 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001544 case TemplateArgument::Template:
1545 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001546 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001547 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1548 break;
1549 case TemplateArgument::Pack:
1550 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1551 TheCU, Name, nullptr,
1552 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1553 break;
David Majnemer5559d472013-08-24 08:21:10 +00001554 case TemplateArgument::Expression: {
1555 const Expr *E = TA.getAsExpr();
1556 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001557 if (E->isGLValue())
1558 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001559 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001560 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001561 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001562 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001563 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001564 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001565 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001566 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001567 case TemplateArgument::Null:
1568 llvm_unreachable(
1569 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001570 }
1571 }
1572 return DBuilder.getOrCreateArray(TemplateParams);
1573}
1574
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001575llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001576CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001578 if (FD->getTemplatedKind() ==
1579 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001580 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1581 ->getTemplate()
1582 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001583 return CollectTemplateParams(
1584 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001586 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001587}
1588
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001589llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1590 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001591 // Always get the full list of parameters, not just the ones from
1592 // the specialization.
1593 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001594 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001595 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001596 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001597}
1598
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001599llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001600 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 return VTablePtrType;
1602
1603 ASTContext &Context = CGM.getContext();
1604
1605 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001606 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001607 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001608 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001610 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001611 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001612 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1613 return VTablePtrType;
1614}
1615
Guy Benyei11169dd2012-12-18 14:30:41 +00001616StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001617 // Copy the gdb compatible name on the side and use its reference.
1618 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001619}
1620
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001621void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001622 SmallVectorImpl<llvm::Metadata *> &EltTys,
1623 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 // If this class is not dynamic then there is not any vtable info to collect.
1625 if (!RD->isDynamicClass())
1626 return;
1627
Reid Kleckner59812422016-08-31 20:35:01 +00001628 // Don't emit any vtable shape or vptr info if this class doesn't have an
1629 // extendable vfptr. This can happen if the class doesn't have virtual
1630 // methods, or in the MS ABI if those virtual methods only come from virtually
1631 // inherited bases.
1632 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1633 if (!RL.hasExtendableVFPtr())
1634 return;
1635
Reid Klecknerdc124992016-08-31 16:11:43 +00001636 // CodeView needs to know how large the vtable of every dynamic class is, so
1637 // emit a special named pointer type into the element list. The vptr type
1638 // points to this type as well.
1639 llvm::DIType *VPtrTy = nullptr;
1640 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1641 CGM.getTarget().getCXXABI().isMicrosoft();
1642 if (NeedVTableShape) {
1643 uint64_t PtrWidth =
1644 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1645 const VTableLayout &VFTLayout =
1646 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1647 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001648 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001649 unsigned VTableWidth = PtrWidth * VSlotCount;
1650
1651 // Create a very wide void* type and insert it directly in the element list.
1652 llvm::DIType *VTableType =
1653 DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type");
1654 EltTys.push_back(VTableType);
1655
1656 // The vptr is a pointer to this special vtable type.
1657 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1658 }
1659
1660 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001661 if (RL.getPrimaryBase())
1662 return;
1663
1664 if (!VPtrTy)
1665 VPtrTy = getOrCreateVTablePtrType(Unit);
1666
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001668 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001669 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001670 llvm::DINode::FlagArtificial, VPtrTy);
1671 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001672}
1673
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001674llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001675 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001676 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001678 return T;
1679}
1680
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001681llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001682 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001683 return getOrCreateStandaloneType(D, Loc);
1684}
1685
1686llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1687 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001688 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001689 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001690 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001691 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001692
Adrian Prantl73409ce2013-03-11 18:33:46 +00001693 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 return T;
1695}
1696
David Blaikie483a9da2014-05-06 18:35:21 +00001697void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001698 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001699 return;
1700 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001701 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001702 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001703 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001704 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001705 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001706 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001707 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001708}
1709
David Blaikieb2e86eb2013-08-15 20:49:17 +00001710void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001711 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001712 !CGM.getLangOpts().CPlusPlus)
1713 completeRequiredType(RD);
1714}
1715
David Blaikieb11c8732017-01-30 06:36:08 +00001716/// Return true if the class or any of its methods are marked dllimport.
1717static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1718 if (RD->hasAttr<DLLImportAttr>())
1719 return true;
1720 for (const CXXMethodDecl *MD : RD->methods())
1721 if (MD->hasAttr<DLLImportAttr>())
1722 return true;
1723 return false;
1724}
1725
David Blaikie6943dea2013-08-20 01:28:15 +00001726void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001727 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1728 if (CXXRD->isDynamicClass() &&
1729 CGM.getVTableLinkage(CXXRD) ==
1730 llvm::GlobalValue::AvailableExternallyLinkage &&
1731 !isClassOrMethodDLLImport(CXXRD))
1732 return;
1733 completeClass(RD);
1734}
1735
1736void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001737 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001738 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001739 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001740 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001741 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001742 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001743 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001744 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001745 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001746 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001747}
1748
David Blaikie0e716b42014-03-03 23:48:23 +00001749static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1750 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001751 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1752 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001753 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001754 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001755 return true;
1756 return false;
1757}
1758
Adrian Prantl05fefa42016-04-25 20:52:40 +00001759/// Does a type definition exist in an imported clang module?
1760static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001761 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001762 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001763 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001764 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001765 if (!RD->isExternallyVisible() && RD->getName().empty())
1766 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001767 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001768 if (!CXXDecl->isCompleteDefinition())
1769 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001770 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1771 if (TemplateKind != TSK_Undeclared) {
1772 // This is a template, check the origin of the first member.
1773 if (CXXDecl->field_begin() == CXXDecl->field_end())
1774 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1775 if (!CXXDecl->field_begin()->isFromASTFile())
1776 return false;
1777 }
Adrian Prantl94913712016-04-26 23:42:43 +00001778 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001779 return true;
1780}
1781
Benjamin Kramer8c305922016-02-02 11:06:51 +00001782static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1783 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001784 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001785 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001786 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001787
Benjamin Kramer8c305922016-02-02 11:06:51 +00001788 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001789 return false;
1790
1791 if (!LangOpts.CPlusPlus)
1792 return false;
1793
1794 if (!RD->isCompleteDefinitionRequired())
1795 return true;
1796
David Majnemer58ed0f32016-07-17 00:39:12 +00001797 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001798
1799 if (!CXXDecl)
1800 return false;
1801
Adrian McCarthy99242982016-08-16 22:11:18 +00001802 // Only emit complete debug info for a dynamic class when its vtable is
1803 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001804 // across DLL boundaries, so skip this optimization if the class or any of its
1805 // methods are marked dllimport. This isn't a complete solution, since objects
1806 // without any dllimport methods can be used in one DLL and constructed in
1807 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001808 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001809 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001810 return true;
1811
1812 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001813 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001814 Spec = SD->getSpecializationKind();
1815
1816 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1817 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1818 CXXDecl->method_end()))
1819 return true;
1820
1821 return false;
1822}
1823
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001824void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1825 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1826 return;
1827
1828 QualType Ty = CGM.getContext().getRecordType(RD);
1829 llvm::DIType *T = getTypeOrNull(Ty);
1830 if (T && T->isForwardDecl())
1831 completeClassData(RD);
1832}
1833
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001834llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001836 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001837 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1838 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001839 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001840 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001841 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001842 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001843
David Blaikieb2e86eb2013-08-15 20:49:17 +00001844 return CreateTypeDefinition(Ty);
1845}
1846
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001847llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001848 RecordDecl *RD = Ty->getDecl();
1849
Guy Benyei11169dd2012-12-18 14:30:41 +00001850 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001851 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001852
1853 // Records and classes and unions can all be recursive. To handle them, we
1854 // first generate a debug descriptor for the struct as a forward declaration.
1855 // Then (if it is a definition) we go through and get debug info for all of
1856 // its members. Finally, we create a descriptor for the complete type (which
1857 // may refer to the forward decl if the struct is recursive) and replace all
1858 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001859 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001860
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001861 const RecordDecl *D = RD->getDefinition();
1862 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001863 return FwdDecl;
1864
David Majnemer58ed0f32016-07-17 00:39:12 +00001865 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001866 CollectContainingType(CXXDecl, FwdDecl);
1867
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001869 LexicalBlockStack.emplace_back(&*FwdDecl);
1870 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001871
Guy Benyei11169dd2012-12-18 14:30:41 +00001872 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001873 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001874 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001875
1876 // Note: The split of CXXDecl information here is intentional, the
1877 // gdb tests will depend on a certain ordering at printout. The debug
1878 // information offsets are still correct if we merge them all together
1879 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001880 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 if (CXXDecl) {
1882 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001883 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 }
1885
Eric Christopher91a31902013-01-16 01:22:32 +00001886 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001888 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001890
1891 LexicalBlockStack.pop_back();
1892 RegionMap.erase(Ty->getDecl());
1893
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001894 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001895 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001896
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001897 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001898 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001899 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001900
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001901 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001902 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001903}
1904
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001905llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1906 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 // Ignore protocols.
1908 return getOrCreateType(Ty->getBaseType(), Unit);
1909}
1910
Manman Rene6be26c2016-09-13 17:25:08 +00001911llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1912 llvm::DIFile *Unit) {
1913 // Ignore protocols.
1914 SourceLocation Loc = Ty->getDecl()->getLocation();
1915
1916 // Use Typedefs to represent ObjCTypeParamType.
1917 return DBuilder.createTypedef(
1918 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1919 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1920 getDeclContextDescriptor(Ty->getDecl()));
1921}
1922
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001923/// \return true if Getter has the default name for the property PD.
1924static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1925 const ObjCMethodDecl *Getter) {
1926 assert(PD);
1927 if (!Getter)
1928 return true;
1929
1930 assert(Getter->getDeclName().isObjCZeroArgSelector());
1931 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001932 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001933}
1934
1935/// \return true if Setter has the default name for the property PD.
1936static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1937 const ObjCMethodDecl *Setter) {
1938 assert(PD);
1939 if (!Setter)
1940 return true;
1941
1942 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001943 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001944 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001945}
1946
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001947llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1948 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 ObjCInterfaceDecl *ID = Ty->getDecl();
1950 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001951 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001952
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001953 // Return a forward declaration if this type was imported from a clang module,
1954 // and this is not the compile unit with the implementation of the type (which
1955 // may contain hidden ivars).
1956 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1957 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001958 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1959 ID->getName(),
1960 getDeclContextDescriptor(ID), Unit, 0);
1961
Guy Benyei11169dd2012-12-18 14:30:41 +00001962 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001963 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001965 auto RuntimeLang =
1966 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001967
1968 // If this is just a forward declaration return a special forward-declaration
1969 // debug type since we won't be able to lay out the entire type.
1970 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001971 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001972 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001974 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1975 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001976 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 return FwdDecl;
1978 }
1979
David Blaikieef8a9512014-05-05 23:23:53 +00001980 return CreateTypeDefinition(Ty, Unit);
1981}
1982
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001983llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001984CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1985 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001986 // Use the Module pointer as the key into the cache. This is a
1987 // nullptr if the "Module" is a PCH, which is safe because we don't
1988 // support chained PCH debug info, so there can only be a single PCH.
1989 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001990 auto ModRef = ModuleCache.find(M);
1991 if (ModRef != ModuleCache.end())
1992 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001993
1994 // Macro definitions that were defined with "-D" on the command line.
1995 SmallString<128> ConfigMacros;
1996 {
1997 llvm::raw_svector_ostream OS(ConfigMacros);
1998 const auto &PPOpts = CGM.getPreprocessorOpts();
1999 unsigned I = 0;
2000 // Translate the macro definitions back into a commmand line.
2001 for (auto &M : PPOpts.Macros) {
2002 if (++I > 1)
2003 OS << " ";
2004 const std::string &Macro = M.first;
2005 bool Undef = M.second;
2006 OS << "\"-" << (Undef ? 'U' : 'D');
2007 for (char c : Macro)
2008 switch (c) {
2009 case '\\' : OS << "\\\\"; break;
2010 case '"' : OS << "\\\""; break;
2011 default: OS << c;
2012 }
2013 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002014 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002015 }
Adrian Prantl66689202015-09-18 23:01:45 +00002016
Adrian Prantl835e6632015-09-24 16:10:10 +00002017 bool IsRootModule = M ? !M->Parent : true;
2018 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002019 // PCH files don't have a signature field in the control block,
2020 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00002021 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002022 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002023 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2024 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2025 TheCU->getProducer(), true, StringRef(), 0,
2026 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2027 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002028 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002029 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002030 llvm::DIModule *Parent =
2031 IsRootModule ? nullptr
2032 : getOrCreateModuleRef(
2033 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2034 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002035 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002036 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2037 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002038 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002039 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002040}
2041
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002042llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2043 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002044 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002045 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002046 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002047 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002048
2049 // Bit size, align and offset of the type.
2050 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002051 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002052
Leny Kholodov80c047d2016-09-06 10:48:04 +00002053 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002055 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002056
Adrian Prantlfd696112015-10-01 00:48:51 +00002057 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002058 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002059 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2060 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002061
David Blaikieef8a9512014-05-05 23:23:53 +00002062 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002063 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002064
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002065 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002066 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002067 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002068
2069 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002070 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002071
2072 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2073 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002074 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002075 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002076 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002077 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002078
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002079 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2080 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002081 EltTys.push_back(InhTag);
2082 }
2083
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002084 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002085 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002086 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002087 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 unsigned PLine = getLineNumber(Loc);
2089 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2090 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002091 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2092 PD->getName(), PUnit, PLine,
2093 hasDefaultGetterName(PD, Getter) ? ""
2094 : getSelectorName(PD->getGetterName()),
2095 hasDefaultSetterName(PD, Setter) ? ""
2096 : getSelectorName(PD->getSetterName()),
2097 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002099 };
2100 {
2101 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002102 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002103 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002104 PropertySet.insert(PD->getIdentifier());
2105 AddProperty(PD);
2106 }
Manman Renefe1bac2016-01-27 20:00:32 +00002107 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002108 // Don't emit duplicate metadata for properties that were already in a
2109 // class extension.
2110 if (!PropertySet.insert(PD->getIdentifier()).second)
2111 continue;
2112 AddProperty(PD);
2113 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 }
2115
2116 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2117 unsigned FieldNo = 0;
2118 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2119 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002120 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002121 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002122 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002123
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 StringRef FieldName = Field->getName();
2125
2126 // Ignore unnamed fields.
2127 if (FieldName.empty())
2128 continue;
2129
2130 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002131 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 unsigned FieldLine = getLineNumber(Field->getLocation());
2133 QualType FType = Field->getType();
2134 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002135 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002136
2137 if (!FType->isIncompleteArrayType()) {
2138
2139 // Bit size, align and offset of the type.
2140 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002141 ? Field->getBitWidthValue(CGM.getContext())
2142 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002143 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 }
2145
2146 uint64_t FieldOffset;
2147 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2148 // We don't know the runtime offset of an ivar if we're using the
2149 // non-fragile ABI. For bitfields, use the bit offset into the first
2150 // byte of storage of the bitfield. For other fields, use zero.
2151 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002152 FieldOffset =
2153 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002154 FieldOffset %= CGM.getContext().getCharWidth();
2155 } else {
2156 FieldOffset = 0;
2157 }
2158 } else {
2159 FieldOffset = RL.getFieldOffset(FieldNo);
2160 }
2161
Leny Kholodov80c047d2016-09-06 10:48:04 +00002162 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002164 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002166 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002167 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002168 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002169
Craig Topper8a13c412014-05-21 05:09:00 +00002170 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002172 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002173 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002175 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002176 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002177 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002178 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2179 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002180 PropertyNode = DBuilder.createObjCProperty(
2181 PD->getName(), PUnit, PLine,
2182 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2183 PD->getGetterName()),
2184 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2185 PD->getSetterName()),
2186 PD->getPropertyAttributes(),
2187 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 }
2189 }
2190 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002191 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2192 FieldSize, FieldAlign, FieldOffset, Flags,
2193 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 EltTys.push_back(FieldTy);
2195 }
2196
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002197 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002198 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002199
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002201 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202}
2203
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002204llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2205 llvm::DIFile *Unit) {
2206 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 int64_t Count = Ty->getNumElements();
2208 if (Count == 0)
2209 // If number of elements are not known then this is an unbounded array.
2210 // Use Count == -1 to express such arrays.
2211 Count = -1;
2212
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002213 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002214 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002215
2216 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002217 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002218
2219 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2220}
2221
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002222llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002223 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002224 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002225
2226 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002227 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002229 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2230 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 } else if (Ty->isIncompleteArrayType()) {
2232 Size = 0;
2233 if (Ty->getElementType()->isIncompleteType())
2234 Align = 0;
2235 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002236 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002237 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 Size = 0;
2239 Align = 0;
2240 } else {
2241 // Size and align of the whole array, not the element type.
2242 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002243 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 }
2245
2246 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2247 // interior arrays, do we care? Why aren't nested arrays represented the
2248 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002249 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002250 QualType EltTy(Ty, 0);
2251 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2252 // If the number of elements is known, then count is that number. Otherwise,
2253 // it's -1. This allows us to represent a subrange with an array of 0
2254 // elements, like this:
2255 //
2256 // struct foo {
2257 // int x[0];
2258 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002259 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002260 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002261 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002262 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002263 if (Expr *Size = VAT->getSizeExpr()) {
2264 llvm::APSInt V;
2265 if (Size->EvaluateAsInt(V, CGM.getContext()))
2266 Count = V.getExtValue();
2267 }
David Blaikie87173f12016-08-22 17:49:56 +00002268 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002269
Guy Benyei11169dd2012-12-18 14:30:41 +00002270 // FIXME: Verify this is right for VLAs.
2271 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2272 EltTy = Ty->getElementType();
2273 }
2274
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002275 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002276
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002277 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2278 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002279}
2280
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002281llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2282 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002283 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2284 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002285}
2286
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002287llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2288 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002289 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2290 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002291}
2292
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2294 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002295 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002296 uint64_t Size = 0;
2297
2298 if (!Ty->isIncompleteType()) {
2299 Size = CGM.getContext().getTypeSize(Ty);
2300
2301 // Set the MS inheritance model. There is no flag for the unspecified model.
2302 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2303 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2304 case MSInheritanceAttr::Keyword_single_inheritance:
2305 Flags |= llvm::DINode::FlagSingleInheritance;
2306 break;
2307 case MSInheritanceAttr::Keyword_multiple_inheritance:
2308 Flags |= llvm::DINode::FlagMultipleInheritance;
2309 break;
2310 case MSInheritanceAttr::Keyword_virtual_inheritance:
2311 Flags |= llvm::DINode::FlagVirtualInheritance;
2312 break;
2313 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2314 break;
2315 }
2316 }
2317 }
2318
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002319 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002320 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002321 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002322 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2323 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002324
2325 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002326 Ty->getPointeeType()->getAs<FunctionProtoType>();
2327 return DBuilder.createMemberPointerType(
2328 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2329 Ty->getClass(), FPT->getTypeQuals())),
2330 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002331 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002332}
2333
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002334llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002335 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2336 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002337}
2338
Xiuli Pan9c14e282016-01-09 12:53:17 +00002339llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2340 llvm::DIFile *U) {
2341 return getOrCreateType(Ty->getElementType(), U);
2342}
2343
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002344llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002345 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002346
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002348 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 if (!ED->getTypeForDecl()->isIncompleteType()) {
2350 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002351 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002352 }
2353
Manman Rene0064d82013-08-29 23:19:58 +00002354 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2355
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002356 bool isImportedFromModule =
2357 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2358
Guy Benyei11169dd2012-12-18 14:30:41 +00002359 // If this is just a forward declaration, construct an appropriately
2360 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002361 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002362 // Note that it is possible for enums to be created as part of
2363 // their own declcontext. In this case a FwdDecl will be created
2364 // twice. This doesn't cause a problem because both FwdDecls are
2365 // entered into the ReplaceMap: finalize() will replace the first
2366 // FwdDecl with the second and then replace the second with
2367 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002368 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002370 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2371 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002372
Guy Benyei11169dd2012-12-18 14:30:41 +00002373 unsigned Line = getLineNumber(ED->getLocation());
2374 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002375 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002376 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2377 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002378
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002379 ReplaceMap.emplace_back(
2380 std::piecewise_construct, std::make_tuple(Ty),
2381 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002382 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002383 }
2384
David Blaikie483a9da2014-05-06 18:35:21 +00002385 return CreateTypeDefinition(Ty);
2386}
2387
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002388llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002389 const EnumDecl *ED = Ty->getDecl();
2390 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002391 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002392 if (!ED->getTypeForDecl()->isIncompleteType()) {
2393 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002394 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002395 }
2396
2397 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2398
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002399 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002400 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002401 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002402 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002403 Enumerators.push_back(DBuilder.createEnumerator(
2404 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002405 }
2406
2407 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002408 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002409
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002410 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002411 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002412 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002413 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002414 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2415 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2416 Line, Size, Align, EltArray, ClassTy,
2417 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002418}
2419
Amjad Aboud546bc112017-02-09 22:07:24 +00002420llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2421 unsigned MType, SourceLocation LineLoc,
2422 StringRef Name, StringRef Value) {
2423 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2424 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2425}
2426
2427llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2428 SourceLocation LineLoc,
2429 SourceLocation FileLoc) {
2430 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2431 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2432 return DBuilder.createTempMacroFile(Parent, Line, FName);
2433}
2434
David Blaikie05491062013-01-21 04:37:12 +00002435static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2436 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002437 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002438 Qualifiers InnerQuals = T.getLocalQualifiers();
2439 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2440 // that is already there.
2441 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2442 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002443 QualType LastT = T;
2444 switch (T->getTypeClass()) {
2445 default:
David Blaikie05491062013-01-21 04:37:12 +00002446 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002447 case Type::TemplateSpecialization: {
2448 const auto *Spec = cast<TemplateSpecializationType>(T);
2449 if (Spec->isTypeAlias())
2450 return C.getQualifiedType(T.getTypePtr(), Quals);
2451 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002452 break;
2453 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 case Type::TypeOfExpr:
2455 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2456 break;
2457 case Type::TypeOf:
2458 T = cast<TypeOfType>(T)->getUnderlyingType();
2459 break;
2460 case Type::Decltype:
2461 T = cast<DecltypeType>(T)->getUnderlyingType();
2462 break;
2463 case Type::UnaryTransform:
2464 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2465 break;
2466 case Type::Attributed:
2467 T = cast<AttributedType>(T)->getEquivalentType();
2468 break;
2469 case Type::Elaborated:
2470 T = cast<ElaboratedType>(T)->getNamedType();
2471 break;
2472 case Type::Paren:
2473 T = cast<ParenType>(T)->getInnerType();
2474 break;
David Blaikie05491062013-01-21 04:37:12 +00002475 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002478 case Type::Auto:
2479 case Type::DeducedTemplateSpecialization: {
2480 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002481 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002482 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002483 break;
2484 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002485 case Type::Adjusted:
2486 case Type::Decayed:
2487 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2488 T = cast<AdjustedType>(T)->getAdjustedType();
2489 break;
2490 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002491
Guy Benyei11169dd2012-12-18 14:30:41 +00002492 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002493 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 } while (true);
2495}
2496
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002497llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002498
2499 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002500 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002501
David Blaikief427b002014-05-06 03:42:01 +00002502 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 if (it != TypeCache.end()) {
2504 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002505 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002506 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 }
2508
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002509 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002510}
2511
David Blaikie0e716b42014-03-03 23:48:23 +00002512void CGDebugInfo::completeTemplateDefinition(
2513 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002514 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002515 return;
2516
David Blaikie0e716b42014-03-03 23:48:23 +00002517 completeClassData(&SD);
2518 // In case this type has no member function definitions being emitted, ensure
2519 // it is retained
2520 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2521}
2522
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002523llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002524 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002525 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002526
2527 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002528 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002529
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002530 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 return T;
2532
Adrian Prantlca844182015-09-11 17:23:03 +00002533 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002534 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002535
2536 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002537 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002538
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 return Res;
2540}
2541
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002542llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002543 // A forward declaration inside a module header does not belong to the module.
2544 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2545 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002546 if (DebugTypeExtRefs && D->isFromASTFile()) {
2547 // Record a reference to an imported clang module or precompiled header.
2548 auto *Reader = CGM.getContext().getExternalSource();
2549 auto Idx = D->getOwningModuleID();
2550 auto Info = Reader->getSourceDescriptor(Idx);
2551 if (Info)
2552 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2553 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002554 // We are building a clang module or a precompiled header.
2555 //
2556 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2557 // and it wouldn't be necessary to specify the parent scope
2558 // because the type is already unique by definition (it would look
2559 // like the output of -fno-standalone-debug). On the other hand,
2560 // the parent scope helps a consumer to quickly locate the object
2561 // file where the type's definition is located, so it might be
2562 // best to make this behavior a command line or debugger tuning
2563 // option.
2564 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2565 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002566 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002567 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2568 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002569 } else {
2570 // This the precompiled header being built.
2571 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002572 }
2573 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002574
Adrian Prantl9402cef2015-09-20 16:51:35 +00002575 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002576}
2577
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002578llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 // Handle qualifiers, which recursively handles what they refer to.
2580 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002581 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002582
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 // Work out details of type.
2584 switch (Ty->getTypeClass()) {
2585#define TYPE(Class, Base)
2586#define ABSTRACT_TYPE(Class, Base)
2587#define NON_CANONICAL_TYPE(Class, Base)
2588#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2589#include "clang/AST/TypeNodes.def"
2590 llvm_unreachable("Dependent types cannot show up in debug information");
2591
2592 case Type::ExtVector:
2593 case Type::Vector:
2594 return CreateType(cast<VectorType>(Ty), Unit);
2595 case Type::ObjCObjectPointer:
2596 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2597 case Type::ObjCObject:
2598 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002599 case Type::ObjCTypeParam:
2600 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002601 case Type::ObjCInterface:
2602 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2603 case Type::Builtin:
2604 return CreateType(cast<BuiltinType>(Ty));
2605 case Type::Complex:
2606 return CreateType(cast<ComplexType>(Ty));
2607 case Type::Pointer:
2608 return CreateType(cast<PointerType>(Ty), Unit);
2609 case Type::BlockPointer:
2610 return CreateType(cast<BlockPointerType>(Ty), Unit);
2611 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002612 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002614 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002615 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002616 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 case Type::FunctionProto:
2618 case Type::FunctionNoProto:
2619 return CreateType(cast<FunctionType>(Ty), Unit);
2620 case Type::ConstantArray:
2621 case Type::VariableArray:
2622 case Type::IncompleteArray:
2623 return CreateType(cast<ArrayType>(Ty), Unit);
2624
2625 case Type::LValueReference:
2626 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2627 case Type::RValueReference:
2628 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2629
2630 case Type::MemberPointer:
2631 return CreateType(cast<MemberPointerType>(Ty), Unit);
2632
2633 case Type::Atomic:
2634 return CreateType(cast<AtomicType>(Ty), Unit);
2635
Xiuli Pan9c14e282016-01-09 12:53:17 +00002636 case Type::Pipe:
2637 return CreateType(cast<PipeType>(Ty), Unit);
2638
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002640 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2641
David Blaikie42edade2014-11-11 20:44:45 +00002642 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002643 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002644 case Type::Adjusted:
2645 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002646 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002647 case Type::Elaborated:
2648 case Type::Paren:
2649 case Type::SubstTemplateTypeParm:
2650 case Type::TypeOfExpr:
2651 case Type::TypeOf:
2652 case Type::Decltype:
2653 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002654 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002655 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002657
David Blaikie42edade2014-11-11 20:44:45 +00002658 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002659}
2660
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002661llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2662 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002663 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002664
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002665 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002666
2667 // We may have cached a forward decl when we could have created
2668 // a non-forward decl. Go ahead and create a non-forward decl
2669 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002670 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002671 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002672
2673 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002674 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002675
2676 // Propagate members from the declaration to the definition
2677 // CreateType(const RecordType*) will overwrite this with the members in the
2678 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002679 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002680
Guy Benyei11169dd2012-12-18 14:30:41 +00002681 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002682 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 return Res;
2684}
2685
2686// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002687llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002689
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002691 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 unsigned Line = getLineNumber(RD->getLocation());
2693 StringRef RDName = getClassName(RD);
2694
Amjad Abouddc4531e2016-04-30 01:44:38 +00002695 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002696
David Blaikied2785892013-08-18 17:36:19 +00002697 // If we ended up creating the type during the context chain construction,
2698 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002699 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002700 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002701 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002702 return T;
David Blaikied2785892013-08-18 17:36:19 +00002703
Adrian Prantl381e7552014-02-04 21:29:50 +00002704 // If this is just a forward or incomplete declaration, construct an
2705 // appropriately marked node and just return it.
2706 const RecordDecl *D = RD->getDefinition();
2707 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002708 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002709
2710 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002711 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002712
Manman Rene0064d82013-08-29 23:19:58 +00002713 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2714
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002715 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002716 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2717 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002718
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002719 // Elements of composite types usually have back to the type, creating
2720 // uniquing cycles. Distinct nodes are more efficient.
2721 switch (RealDecl->getTag()) {
2722 default:
2723 llvm_unreachable("invalid composite type tag");
2724
2725 case llvm::dwarf::DW_TAG_array_type:
2726 case llvm::dwarf::DW_TAG_enumeration_type:
2727 // Array elements and most enumeration elements don't have back references,
2728 // so they don't tend to be involved in uniquing cycles and there is some
2729 // chance of merging them when linking together two modules. Only make
2730 // them distinct if they are ODR-uniqued.
2731 if (FullName.empty())
2732 break;
2733
2734 case llvm::dwarf::DW_TAG_structure_type:
2735 case llvm::dwarf::DW_TAG_union_type:
2736 case llvm::dwarf::DW_TAG_class_type:
2737 // Immediatley resolve to a distinct node.
2738 RealDecl =
2739 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2740 break;
2741 }
2742
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002743 RegionMap[Ty->getDecl()].reset(RealDecl);
2744 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002745
David Majnemer58ed0f32016-07-17 00:39:12 +00002746 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002747 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002748 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002749 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002750}
2751
David Blaikieadfbf992013-08-18 16:55:33 +00002752void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002753 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002754 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002755 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002756 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2757 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002758 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002759 while (1) {
2760 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2761 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2762 if (PBT && !BRL.isPrimaryBaseVirtual())
2763 PBase = PBT;
2764 else
2765 break;
2766 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002767 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002768 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2769 getOrCreateFile(RD->getLocation())));
2770 } else if (RD->isDynamicClass())
2771 ContainingType = RealDecl;
2772
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002773 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002774}
2775
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002776llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002777 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002778 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002779 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002780 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002781 llvm::DIType *Ty =
2782 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2783 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 *Offset += FieldSize;
2785 return Ty;
2786}
2787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002788void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002789 StringRef &Name,
2790 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002791 llvm::DIScope *&FDContext,
2792 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002793 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002794 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002795 Name = getFunctionName(FD);
2796 // Use mangled name as linkage name for C/C++ functions.
2797 if (FD->hasPrototype()) {
2798 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002799 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002800 }
2801 // No need to replicate the linkage name if it isn't different from the
2802 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002803 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002804 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2805 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002806 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002807 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002808 LinkageName = StringRef();
2809
Benjamin Kramer8c305922016-02-02 11:06:51 +00002810 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002811 if (const NamespaceDecl *NSDecl =
2812 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2813 FDContext = getOrCreateNameSpace(NSDecl);
2814 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002815 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2816 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2817 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2818 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002819 // Check if it is a noreturn-marked function
2820 if (FD->isNoReturn())
2821 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002822 // Collect template parameters.
2823 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2824 }
2825}
2826
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002827void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002828 unsigned &LineNo, QualType &T,
2829 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002830 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002831 Unit = getOrCreateFile(VD->getLocation());
2832 LineNo = getLineNumber(VD->getLocation());
2833
2834 setLocation(VD->getLocation());
2835
2836 T = VD->getType();
2837 if (T->isIncompleteArrayType()) {
2838 // CodeGen turns int[] into int[1] so we'll do the same here.
2839 llvm::APInt ConstVal(32, 1);
2840 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2841
2842 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2843 ArrayType::Normal, 0);
2844 }
2845
2846 Name = VD->getName();
2847 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2848 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2849 LinkageName = CGM.getMangledName(VD);
2850 if (LinkageName == Name)
2851 LinkageName = StringRef();
2852
2853 // Since we emit declarations (DW_AT_members) for static members, place the
2854 // definition of those static members in the namespace they were declared in
2855 // in the source code (the lexical decl context).
2856 // FIXME: Generalize this for even non-member global variables where the
2857 // declaration and definition may have different lexical decl contexts, once
2858 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002859 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2860 : VD->getDeclContext();
2861 // When a record type contains an in-line initialization of a static data
2862 // member, and the record type is marked as __declspec(dllexport), an implicit
2863 // definition of the member will be created in the record context. DWARF
2864 // doesn't seem to have a nice way to describe this in a form that consumers
2865 // are likely to understand, so fake the "normal" situation of a definition
2866 // outside the class by putting it in the global scope.
2867 if (DC->isRecord())
2868 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002869
Amjad Abouddc4531e2016-04-30 01:44:38 +00002870 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2871 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002872}
2873
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002874llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002875CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002876 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002877 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002878 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Frederic Rissd253ed62014-11-18 03:40:51 +00002879 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002880 llvm::DIFile *Unit = getOrCreateFile(Loc);
2881 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002882 unsigned Line = getLineNumber(Loc);
2883
2884 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2885 TParamsArray, Flags);
2886 // Build function type.
2887 SmallVector<QualType, 16> ArgTypes;
2888 for (const ParmVarDecl *Parm: FD->parameters())
2889 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002890 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2891 QualType FnType = CGM.getContext().getFunctionType(
2892 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002893 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002894 DContext, Name, LinkageName, Unit, Line,
2895 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002896 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002897 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002898 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002899 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2900 std::make_tuple(CanonDecl),
2901 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002902 return SP;
2903}
2904
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002905llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002906CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2907 QualType T;
2908 StringRef Name, LinkageName;
2909 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002910 llvm::DIFile *Unit = getOrCreateFile(Loc);
2911 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002912 unsigned Line = getLineNumber(Loc);
2913
2914 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002915 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002916 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2917 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002918 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002919 FwdDeclReplaceMap.emplace_back(
2920 std::piecewise_construct,
2921 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2922 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002923 return GV;
2924}
2925
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002926llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002927 // We only need a declaration (not a definition) of the type - so use whatever
2928 // we would otherwise do to get a type for a pointee. (forward declarations in
2929 // limited debug info, full definitions (if the type definition is available)
2930 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002931 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002932 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002933 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002934 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002935
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002936 if (I != DeclCache.end()) {
2937 auto N = I->second;
2938 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
2939 return GVE->getVariable();
2940 return dyn_cast_or_null<llvm::DINode>(N);
2941 }
Frederic Rissd253ed62014-11-18 03:40:51 +00002942
2943 // No definition for now. Emit a forward definition that might be
2944 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002945 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002946 return getFunctionForwardDeclaration(FD);
2947 else if (const auto *VD = dyn_cast<VarDecl>(D))
2948 return getGlobalVariableForwardDeclaration(VD);
2949
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002950 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002951}
2952
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002953llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002954 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002955 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002956
David Majnemer58ed0f32016-07-17 00:39:12 +00002957 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002958 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002959 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002960
2961 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002962 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002963
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002964 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002965 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002966 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002967 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002968 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002969 }
2970 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002971 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002972 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002973 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 return SP;
2975 }
2976
Aaron Ballman86c93902014-03-06 23:45:36 +00002977 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002978 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002980 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002981 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 return SP;
2983 }
2984 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002985 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002986}
2987
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002988// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002989// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002990llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002991 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002992 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002993 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002994 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2995 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002996 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002997
David Majnemer58ed0f32016-07-17 00:39:12 +00002998 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003000
3001 const auto *FTy = FnType->getAs<FunctionType>();
3002 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3003
David Majnemer58ed0f32016-07-17 00:39:12 +00003004 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003006 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003007
3008 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003009 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003010
3011 // Replace the instancetype keyword with the actual type.
3012 if (ResultTy == CGM.getContext().getObjCInstanceType())
3013 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003014 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003015
Adrian Prantl7bec9032013-05-10 21:08:31 +00003016 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003018 QualType SelfDeclTy;
3019 if (auto *SelfDecl = OMethod->getSelfDecl())
3020 SelfDeclTy = SelfDecl->getType();
3021 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3022 if (FPT->getNumParams() > 1)
3023 SelfDeclTy = FPT->getParamType(0);
3024 if (!SelfDeclTy.isNull())
3025 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003027 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003028 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003030 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003031 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003032 // Variadic methods need a special marker at the end of the type list.
3033 if (OMethod->isVariadic())
3034 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003035
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003036 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003037 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3038 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003039 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003040
Adrian Prantl800faef2014-02-25 23:42:18 +00003041 // Handle variadic function types; they need an additional
3042 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003043 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003044 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003045 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003046 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003047 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3048 for (QualType ParamType : FPT->param_types())
3049 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003050 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003051 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003052 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3053 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003054 }
3055
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003056 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003057}
3058
Eric Christophere7b87e52014-10-26 23:40:33 +00003059void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3060 SourceLocation ScopeLoc, QualType FnType,
3061 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003062
3063 StringRef Name;
3064 StringRef LinkageName;
3065
3066 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3067
3068 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003069 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003070
Leny Kholodov80c047d2016-09-06 10:48:04 +00003071 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003072 llvm::DIFile *Unit = getOrCreateFile(Loc);
3073 llvm::DIScope *FDContext = Unit;
3074 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 if (!HasDecl) {
3076 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003077 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003078 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003079 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003080 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003082 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003083 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003084 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003085 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 return;
3087 }
3088 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003089 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3090 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003091 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003092 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003093 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 } else {
3095 // Use llvm function name.
3096 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003097 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003099 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003100 Name = Name.substr(1);
3101
Adrian Prantl42d71b92014-04-10 23:21:53 +00003102 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003103 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003104 // Artificial functions should not silently reuse CurLoc.
3105 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003106 }
3107 unsigned LineNo = getLineNumber(Loc);
3108 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003109
Eric Christopher8018e412014-03-27 18:50:35 +00003110 // FIXME: The function declaration we're constructing here is mostly reusing
3111 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3112 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3113 // all subprograms instead of the actual context since subprogram definitions
3114 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003115 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003116 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003117 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003118 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003119 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003120 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003121 // We might get here with a VarDecl in the case we're generating
3122 // code for the initialization of globals. Do not record these decls
3123 // as they will overwrite the actual VarDecl Decl in the cache.
3124 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003125 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003126
Adrian Prantlbebb8932014-03-21 21:01:58 +00003127 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003128 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003129
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003131 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003132}
3133
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003134void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3135 QualType FnType) {
3136 StringRef Name;
3137 StringRef LinkageName;
3138
3139 const Decl *D = GD.getDecl();
3140 if (!D)
3141 return;
3142
Leny Kholodov80c047d2016-09-06 10:48:04 +00003143 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003144 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003145 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003146 llvm::DINodeArray TParamsArray;
3147 if (isa<FunctionDecl>(D)) {
3148 // If there is a DISubprogram for this function available then use it.
3149 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3150 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003151 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003152 Name = getObjCMethodName(OMD);
3153 Flags |= llvm::DINode::FlagPrototyped;
3154 } else {
3155 llvm_unreachable("not a function or ObjC method");
3156 }
3157 if (!Name.empty() && Name[0] == '\01')
3158 Name = Name.substr(1);
3159
3160 if (D->isImplicit()) {
3161 Flags |= llvm::DINode::FlagArtificial;
3162 // Artificial functions without a location should not silently reuse CurLoc.
3163 if (Loc.isInvalid())
3164 CurLoc = SourceLocation();
3165 }
3166 unsigned LineNo = getLineNumber(Loc);
3167 unsigned ScopeLine = 0;
3168
Adrian Prantle76bda52016-04-15 15:55:45 +00003169 DBuilder.retainType(DBuilder.createFunction(
3170 FDContext, Name, LinkageName, Unit, LineNo,
3171 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3172 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3173 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003174}
3175
David Blaikie835afb22015-01-21 23:08:17 +00003176void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 // Update our current location
3178 setLocation(Loc);
3179
Eric Christophere7b87e52014-10-26 23:40:33 +00003180 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3181 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003182
Adrian Prantle83b1302014-01-07 22:05:52 +00003183 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003184 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00003185 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003186}
3187
Guy Benyei11169dd2012-12-18 14:30:41 +00003188void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003189 llvm::MDNode *Back = nullptr;
3190 if (!LexicalBlockStack.empty())
3191 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003192 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003193 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003194 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003195}
3196
Eric Christopher0fdcb312013-05-16 00:52:20 +00003197void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3198 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003199 // Set our current location.
3200 setLocation(Loc);
3201
Guy Benyei11169dd2012-12-18 14:30:41 +00003202 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00003203 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3204 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00003205
Benjamin Kramer8c305922016-02-02 11:06:51 +00003206 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003207 return;
3208
3209 // Create a new lexical block and push it on the stack.
3210 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003211}
3212
Eric Christopher0fdcb312013-05-16 00:52:20 +00003213void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3214 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003215 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3216
3217 // Provide an entry in the line table for the end of the block.
3218 EmitLocation(Builder, Loc);
3219
Benjamin Kramer8c305922016-02-02 11:06:51 +00003220 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003221 return;
3222
Guy Benyei11169dd2012-12-18 14:30:41 +00003223 LexicalBlockStack.pop_back();
3224}
3225
Guy Benyei11169dd2012-12-18 14:30:41 +00003226void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3227 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3228 unsigned RCount = FnBeginRegionCount.back();
3229 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3230
3231 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003232 while (LexicalBlockStack.size() != RCount) {
3233 // Provide an entry in the line table for the end of the block.
3234 EmitLocation(Builder, CurLoc);
3235 LexicalBlockStack.pop_back();
3236 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 FnBeginRegionCount.pop_back();
3238}
3239
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003240llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003241 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003242
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003243 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003244 QualType FType;
3245 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003246 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003247
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003248 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003249 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003250
3251 FieldOffset = 0;
3252 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3253 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3254 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3255 FType = CGM.getContext().IntTy;
3256 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3257 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3258
3259 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3260 if (HasCopyAndDispose) {
3261 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003262 EltTys.push_back(
3263 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3264 EltTys.push_back(
3265 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003266 }
3267 bool HasByrefExtendedLayout;
3268 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003269 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3270 HasByrefExtendedLayout) &&
3271 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003272 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003273 EltTys.push_back(
3274 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003275 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003276
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3278 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003279 CGM.getTarget().getPointerAlign(0))) {
3280 CharUnits FieldOffsetInBytes =
3281 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003282 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003283 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003284
Guy Benyei11169dd2012-12-18 14:30:41 +00003285 if (NumPaddingBytes.isPositive()) {
3286 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3287 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3288 pad, ArrayType::Normal, 0);
3289 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3290 }
3291 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003292
Guy Benyei11169dd2012-12-18 14:30:41 +00003293 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003294 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003295 FieldSize = CGM.getContext().getTypeSize(FType);
3296 FieldAlign = CGM.getContext().toBits(Align);
3297
Eric Christopherb2a008c2013-05-16 00:45:12 +00003298 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003299 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003300 FieldAlign, FieldOffset,
3301 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 EltTys.push_back(FieldTy);
3303 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003304
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003305 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003306
Leny Kholodov80c047d2016-09-06 10:48:04 +00003307 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003308
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003310 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003311}
3312
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003313void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3314 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003315 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003316 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003318 if (VD->hasAttr<NoDebugAttr>())
3319 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003320
David Blaikie7fceebf2013-08-19 03:37:48 +00003321 bool Unwritten =
3322 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3323 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003324 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003325 if (!Unwritten)
3326 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003327 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003328 uint64_t XOffset = 0;
3329 if (VD->hasAttr<BlocksAttr>())
3330 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003331 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003332 Ty = getOrCreateType(VD->getType(), Unit);
3333
3334 // If there is no debug info for this type then do not emit debug info
3335 // for this variable.
3336 if (!Ty)
3337 return;
3338
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003340 unsigned Line = 0;
3341 unsigned Column = 0;
3342 if (!Unwritten) {
3343 Line = getLineNumber(VD->getLocation());
3344 Column = getColumnNumber(VD->getLocation());
3345 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003346 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003347 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003348 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003349 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003350
3351 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3352
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 // If this is the first argument and it is implicit then
3354 // give it an object pointer flag.
3355 // FIXME: There has to be a better way to do this, but for static
3356 // functions there won't be an implicit param at arg1 and
3357 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003358 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003359 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003360 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003361 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3362 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003363 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003364
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003365 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003366
3367 StringRef Name = VD->getName();
3368 if (!Name.empty()) {
3369 if (VD->hasAttr<BlocksAttr>()) {
3370 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003371 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 // offset of __forwarding field
3373 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003374 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003375 Expr.push_back(offset.getQuantity());
3376 Expr.push_back(llvm::dwarf::DW_OP_deref);
3377 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003378 // offset of x field
3379 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003380 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003381
3382 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003383 auto *D = ArgNo
3384 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3385 *ArgNo, Unit, Line, Ty)
3386 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003387 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003388
Guy Benyei11169dd2012-12-18 14:30:41 +00003389 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003390 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3391 llvm::DebugLoc::get(Line, Column, Scope),
3392 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003393 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003394 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003395 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003396 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003397 // If VD is an anonymous union then Storage represents value for
3398 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003399 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003400 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003401 // GDB has trouble finding local variables in anonymous unions, so we emit
3402 // artifical local variables for each of the members.
3403 //
3404 // FIXME: Remove this code as soon as GDB supports this.
3405 // The debug info verifier in LLVM operates based on the assumption that a
3406 // variable has the same size as its storage and we had to disable the check
3407 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003408 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003409 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003410 StringRef FieldName = Field->getName();
3411
3412 // Ignore unnamed fields. Do not ignore unnamed records.
3413 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3414 continue;
3415
3416 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003417 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003418 auto *D = DBuilder.createAutoVariable(
3419 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003420 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003421
3422 // Insert an llvm.dbg.declare into the current block.
3423 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3424 llvm::DebugLoc::get(Line, Column, Scope),
3425 Builder.GetInsertBlock());
3426 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003427 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003429
3430 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003431 auto *D = ArgNo
3432 ? DBuilder.createParameterVariable(
3433 Scope, Name, *ArgNo, Unit, Line, Ty,
3434 CGM.getLangOpts().Optimize, Flags)
3435 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3436 CGM.getLangOpts().Optimize, Flags,
3437 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003438
3439 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003440 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3441 llvm::DebugLoc::get(Line, Column, Scope),
3442 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003443}
3444
3445void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3446 llvm::Value *Storage,
3447 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003448 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003449 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003450}
3451
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003452llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3453 llvm::DIType *Ty) {
3454 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003455 if (CachedTy)
3456 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003457 return DBuilder.createObjectPointerType(Ty);
3458}
3459
Eric Christophere7b87e52014-10-26 23:40:33 +00003460void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3461 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003462 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003463 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003464 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003465
Craig Topper8a13c412014-05-21 05:09:00 +00003466 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003468 if (VD->hasAttr<NoDebugAttr>())
3469 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003470
Guy Benyei11169dd2012-12-18 14:30:41 +00003471 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003472
Guy Benyei11169dd2012-12-18 14:30:41 +00003473 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003474 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3475 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003476 if (isByRef)
3477 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003478 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003479 Ty = getOrCreateType(VD->getType(), Unit);
3480
3481 // Self is passed along as an implicit non-arg variable in a
3482 // block. Mark it as the object pointer.
3483 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003484 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003485
3486 // Get location information.
3487 unsigned Line = getLineNumber(VD->getLocation());
3488 unsigned Column = getColumnNumber(VD->getLocation());
3489
3490 const llvm::DataLayout &target = CGM.getDataLayout();
3491
3492 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003493 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3495
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003496 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003497 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003498 addr.push_back(llvm::dwarf::DW_OP_deref);
3499 addr.push_back(llvm::dwarf::DW_OP_plus);
3500 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003502 addr.push_back(llvm::dwarf::DW_OP_deref);
3503 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003504 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003505 offset =
3506 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003507 addr.push_back(offset.getQuantity());
3508 addr.push_back(llvm::dwarf::DW_OP_deref);
3509 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 // offset of x field
3511 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003512 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003513 }
3514
3515 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003516 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003517 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003518 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003519 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003520
Guy Benyei11169dd2012-12-18 14:30:41 +00003521 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003522 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3523 if (InsertPoint)
3524 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3525 InsertPoint);
3526 else
3527 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3528 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003529}
3530
Guy Benyei11169dd2012-12-18 14:30:41 +00003531void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3532 unsigned ArgNo,
3533 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003534 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003535 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003536}
3537
3538namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003539struct BlockLayoutChunk {
3540 uint64_t OffsetInBits;
3541 const BlockDecl::Capture *Capture;
3542};
3543bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3544 return l.OffsetInBits < r.OffsetInBits;
3545}
Guy Benyei11169dd2012-12-18 14:30:41 +00003546}
3547
3548void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003549 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003550 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003551 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003552 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003553 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003554 ASTContext &C = CGM.getContext();
3555 const BlockDecl *blockDecl = block.getBlockDecl();
3556
3557 // Collect some general information about the block's location.
3558 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003559 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 unsigned line = getLineNumber(loc);
3561 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003562
Guy Benyei11169dd2012-12-18 14:30:41 +00003563 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003564 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003565
3566 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003567 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003568
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003569 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003570 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003571 blockLayout->getElementOffsetInBits(0),
3572 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003573 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 blockLayout->getElementOffsetInBits(1),
3575 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003576 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 blockLayout->getElementOffsetInBits(2),
3578 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003579 auto *FnTy = block.getBlockExpr()->getFunctionType();
3580 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003581 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003582 blockLayout->getElementOffsetInBits(3),
3583 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003584 fields.push_back(createFieldType(
3585 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3586 ? C.getBlockDescriptorExtendedType()
3587 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003588 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003589
3590 // We want to sort the captures by offset, not because DWARF
3591 // requires this, but because we're paranoid about debuggers.
3592 SmallVector<BlockLayoutChunk, 8> chunks;
3593
3594 // 'this' capture.
3595 if (blockDecl->capturesCXXThis()) {
3596 BlockLayoutChunk chunk;
3597 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003598 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003599 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003600 chunks.push_back(chunk);
3601 }
3602
3603 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003604 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003605 const VarDecl *variable = capture.getVariable();
3606 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3607
3608 // Ignore constant captures.
3609 if (captureInfo.isConstant())
3610 continue;
3611
3612 BlockLayoutChunk chunk;
3613 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003614 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 chunk.Capture = &capture;
3616 chunks.push_back(chunk);
3617 }
3618
3619 // Sort by offset.
3620 llvm::array_pod_sort(chunks.begin(), chunks.end());
3621
David Majnemer58ed0f32016-07-17 00:39:12 +00003622 for (const BlockLayoutChunk &Chunk : chunks) {
3623 uint64_t offsetInBits = Chunk.OffsetInBits;
3624 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003625
3626 // If we have a null capture, this must be the C++ 'this' capture.
3627 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003628 QualType type;
3629 if (auto *Method =
3630 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3631 type = Method->getThisType(C);
3632 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3633 type = QualType(RDecl->getTypeForDecl(), 0);
3634 else
3635 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003636
David Majnemerb4b671e2016-06-30 03:01:59 +00003637 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003638 offsetInBits, tunit, tunit));
3639 continue;
3640 }
3641
3642 const VarDecl *variable = capture->getVariable();
3643 StringRef name = variable->getName();
3644
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003645 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003647 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003648 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003649
3650 // FIXME: this creates a second copy of this type!
3651 uint64_t xoffset;
3652 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003653 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003654 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3655 PtrInfo.Width, Align, offsetInBits,
3656 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003657 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003658 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003659 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003660 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003661 }
3662 fields.push_back(fieldType);
3663 }
3664
3665 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003666 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3667 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003668
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003669 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003670
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003671 llvm::DIType *type =
3672 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003673 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003674 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003675 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3676
3677 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003678 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003679 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003680
3681 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003682 auto *debugVar = DBuilder.createParameterVariable(
3683 scope, Arg->getName(), ArgNo, tunit, line, type,
3684 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003685
Adrian Prantl616bef42013-03-14 21:52:59 +00003686 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003687 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003688 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003689 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003690 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003691 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003692
Adrian Prantl616bef42013-03-14 21:52:59 +00003693 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003694 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3695 llvm::DebugLoc::get(line, column, scope),
3696 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003697}
3698
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003699llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003700CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3701 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003702 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003703
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003704 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003705 if (MI != StaticDataMemberCache.end()) {
3706 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003707 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003708 }
David Blaikiece763042013-08-20 21:49:21 +00003709
3710 // If the member wasn't found in the cache, lazily construct and add it to the
3711 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003712 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003713 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003714 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003715}
3716
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003717llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003718 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3719 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003720 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003721
3722 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003723 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003724 StringRef FieldName = Field->getName();
3725
3726 // Ignore unnamed fields, but recurse into anonymous records.
3727 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003728 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003729 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003730 Var, DContext);
3731 continue;
3732 }
3733 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003734 GVE = DBuilder.createGlobalVariableExpression(
3735 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3736 Var->hasLocalLinkage());
3737 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003738 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003739 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003740}
3741
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003742void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003743 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003744 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003745 if (D->hasAttr<NoDebugAttr>())
3746 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003747
3748 // If we already created a DIGlobalVariable for this declaration, just attach
3749 // it to the llvm::GlobalVariable.
3750 auto Cached = DeclCache.find(D->getCanonicalDecl());
3751 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003752 return Var->addDebugInfo(
3753 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003754
Guy Benyei11169dd2012-12-18 14:30:41 +00003755 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003756 llvm::DIFile *Unit = nullptr;
3757 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003758 unsigned LineNo;
3759 StringRef DeclName, LinkageName;
3760 QualType T;
3761 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003762
3763 // Attempt to store one global variable for the declaration - even if we
3764 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003765 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003766
3767 // If this is an anonymous union then we'll want to emit a global
3768 // variable for each member of the anonymous union so that it's possible
3769 // to find the name of any field in the union.
3770 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003771 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003772 assert(RD->isAnonymousStructOrUnion() &&
3773 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003774 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003775 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003776 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003777 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003778 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003779 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003780 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003781 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003782 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003783 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003784}
3785
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003786void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003787 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003788 if (VD->hasAttr<NoDebugAttr>())
3789 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003790 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003791 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003792 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003793 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003794 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003795 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3796 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003797 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3798 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3799 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003800 // Do not use global variables for enums.
3801 //
3802 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003803 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003804 return;
David Blaikiea15565562014-04-04 20:56:17 +00003805 // Do not emit separate definitions for function local const/statics.
3806 if (isa<FunctionDecl>(VD->getDeclContext()))
3807 return;
David Blaikiebb113912014-04-05 07:23:17 +00003808 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003809 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003810 if (VarD->isStaticDataMember()) {
3811 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003812 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003813 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003814 //
3815 // FIXME: This is probably unnecessary, since Ty should reference RD
3816 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003817 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003818 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003819 return;
3820 }
3821
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003822 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003823
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003824 auto &GV = DeclCache[VD];
3825 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003826 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003827 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003828 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3829 // FIXME: Add a representation for integer constants wider than 64 bits.
3830 if (Init.isInt())
3831 InitExpr =
3832 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3833 else if (Init.isFloat())
3834 InitExpr = DBuilder.createConstantValueExpression(
3835 Init.getFloat().bitcastToAPInt().getZExtValue());
3836 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003837 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003838 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003839 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3840 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003841}
3842
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003843llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003844 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003845 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003846 llvm::DIScope *Mod = getParentModuleOrNull(D);
3847 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003848}
3849
David Blaikie9f88fe82013-04-22 06:13:21 +00003850void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003851 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003852 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003853 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003854 if (!NSDecl->isAnonymousNamespace() ||
3855 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003856 DBuilder.createImportedModule(
3857 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3858 getOrCreateNameSpace(NSDecl),
3859 getLineNumber(UD.getLocation()));
3860 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003861}
3862
David Blaikiebd483762013-05-20 04:58:53 +00003863void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003864 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003865 return;
3866 assert(UD.shadow_size() &&
3867 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3868 // Emitting one decl is sufficient - debuggers can detect that this is an
3869 // overloaded name & provide lookup for all the overloads.
3870 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003871
3872 // FIXME: Skip functions with undeduced auto return type for now since we
3873 // don't currently have the plumbing for separate declarations & definitions
3874 // of free functions and mismatched types (auto in the declaration, concrete
3875 // return type in the definition)
3876 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3877 if (const auto *AT =
3878 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3879 if (AT->getDeducedType().isNull())
3880 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003881 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003882 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003883 DBuilder.createImportedDeclaration(
3884 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3885 getLineNumber(USD.getLocation()));
3886}
3887
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003888void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003889 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3890 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003891 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003892 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003893 DBuilder.createImportedDeclaration(
3894 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3895 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3896 getLineNumber(ID.getLocation()));
3897 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003898}
3899
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003900llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003901CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003902 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003903 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003904 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003905 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003906 return cast<llvm::DIImportedEntity>(VH);
3907 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003908 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003909 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3910 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003911 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003912 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3913 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3914 NA.getName());
3915 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003916 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003917 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3918 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3919 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003920 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003921 return R;
3922}
3923
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003924llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003925CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003926 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003927 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003928 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003929 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003930
Guy Benyei11169dd2012-12-18 14:30:41 +00003931 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003932 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003933 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00003934 llvm::DINamespace *NS = DBuilder.createNameSpace(
3935 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003936 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003937 return NS;
3938}
3939
Adrian Prantla5206ce2015-09-22 23:26:43 +00003940void CGDebugInfo::setDwoId(uint64_t Signature) {
3941 assert(TheCU && "no main compile unit");
3942 TheCU->setDWOId(Signature);
3943}
3944
3945
Guy Benyei11169dd2012-12-18 14:30:41 +00003946void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003947 // Creating types might create further types - invalidating the current
3948 // element and the size(), so don't cache/reference them.
3949 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3950 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003951 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003952 ? CreateTypeDefinition(E.Type, E.Unit)
3953 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003954 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003955 }
3956
David Blaikief427b002014-05-06 03:42:01 +00003957 for (auto p : ReplaceMap) {
3958 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003959 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003960 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003961
David Blaikief427b002014-05-06 03:42:01 +00003962 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003963 assert(it != TypeCache.end());
3964 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003965
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003966 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3967 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003968 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003969
Frederic Rissd253ed62014-11-18 03:40:51 +00003970 for (const auto &p : FwdDeclReplaceMap) {
3971 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003972 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003973 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003974
3975 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003976 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003977 // with ourselves, that will destroy the temporary MDNode and
3978 // replace it with a standard one, avoiding leaking memory.
3979 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003980 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003981 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003982 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003983
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003984 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
3985 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003986 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003987 }
3988
Adrian Prantl73409ce2013-03-11 18:33:46 +00003989 // We keep our own list of retained types, because we need to look
3990 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003991 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003992 if (auto MD = TypeCache[RT])
3993 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003994
Guy Benyei11169dd2012-12-18 14:30:41 +00003995 DBuilder.finalize();
3996}
David Blaikie66088d52014-09-24 17:01:27 +00003997
3998void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003999 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004000 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004001
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004002 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004003 // Don't ignore in case of explicit cast where it is referenced indirectly.
4004 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004005}
Amara Emerson652795d2016-11-10 14:44:30 +00004006
4007llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4008 if (LexicalBlockStack.empty())
4009 return llvm::DebugLoc();
4010
4011 llvm::MDNode *Scope = LexicalBlockStack.back();
4012 return llvm::DebugLoc::get(
4013 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4014}