blob: 003b6c1872eea72eda89deac2502a7245788bd0b [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());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000110 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
111 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
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
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000137ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
138 GlobalDecl InlinedFn)
139 : CGF(&CGF) {
140 if (!CGF.getDebugInfo()) {
141 this->CGF = nullptr;
142 return;
143 }
144 auto &DI = *CGF.getDebugInfo();
145 SavedLocation = DI.getLocation();
146 assert((DI.getInlinedAt() ==
147 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
148 "CGDebugInfo and IRBuilder are out of sync");
149
150 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
151}
152
153ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
154 if (!CGF)
155 return;
156 auto &DI = *CGF->getDebugInfo();
157 DI.EmitInlineFunctionEnd(CGF->Builder);
158 DI.EmitLocation(CGF->Builder, SavedLocation);
159}
160
Guy Benyei11169dd2012-12-18 14:30:41 +0000161void CGDebugInfo::setLocation(SourceLocation Loc) {
162 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000163 if (Loc.isInvalid())
164 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000165
166 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
167
168 // If we've changed files in the middle of a lexical scope go ahead
169 // and create a new lexical scope with file node if it's different
170 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000171 if (LexicalBlockStack.empty())
172 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000173
174 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000175 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000176 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000177
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000178 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000179 return;
180
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000181 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000182 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000183 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
184 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000185 } else if (isa<llvm::DILexicalBlock>(Scope) ||
186 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000187 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000188 LexicalBlockStack.emplace_back(
189 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000190 }
191}
192
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000193llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000194 llvm::DIScope *Mod = getParentModuleOrNull(D);
195 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
196 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000197}
198
199llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
200 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000201 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000202 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000203
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000204 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000205 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000206 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000207 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000208 }
209
210 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000211 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000212 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000213
David Majnemer58ed0f32016-07-17 00:39:12 +0000214 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000215 if (!RDecl->isDependentType())
216 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000218 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000219}
220
Guy Benyei11169dd2012-12-18 14:30:41 +0000221StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000222 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000223 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 FunctionTemplateSpecializationInfo *Info =
225 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000226
Reid Kleckner31abd802016-06-30 17:41:31 +0000227 // Emit the unqualified name in normal operation. LLVM and the debugger can
228 // compute the fully qualified name from the scope chain. If we're only
229 // emitting line table info, there won't be any scope chains, so emit the
230 // fully qualified name here so that stack traces are more accurate.
231 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
232 // evaluating the size impact.
233 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
234 CGM.getCodeGenOpts().EmitCodeView;
235
236 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000237 return FII->getName();
238
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 SmallString<128> NS;
240 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000241 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000242 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000243 if (!UseQualifiedName)
244 FD->printName(OS);
245 else
246 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000247
Reid Kleckner829398e2016-06-17 16:11:20 +0000248 // Add any template specialization args.
249 if (Info) {
250 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000251 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000252 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000253 }
254
255 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000256 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000257}
258
259StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
260 SmallString<256> MethodName;
261 llvm::raw_svector_ostream OS(MethodName);
262 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
263 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000264 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000265 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000266 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000267 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000268 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000269 if (OC->IsClassExtension()) {
270 OS << OC->getClassInterface()->getName();
271 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000272 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000273 << OC->getIdentifier()->getNameStart() << ')';
274 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000275 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000276 OS << OCD->getClassInterface()->getName() << '('
277 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000278 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000279 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000280 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000281 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000282 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000283 ClassTy.print(OS, PrintingPolicy(LangOptions()));
284 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000285 }
286 OS << ' ' << OMD->getSelector().getAsString() << ']';
287
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000288 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000289}
290
Guy Benyei11169dd2012-12-18 14:30:41 +0000291StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000292 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000293}
294
Eric Christophere7b87e52014-10-26 23:40:33 +0000295StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000296 if (isa<ClassTemplateSpecializationDecl>(RD)) {
297 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000298 llvm::raw_svector_ostream OS(Name);
299 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
300 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000301
302 // Copy this name on the side and use its reference.
303 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000304 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000305
David Majnemerbc5976a2016-07-01 23:12:54 +0000306 // quick optimization to avoid having to intern strings that are already
307 // stored reliably elsewhere
308 if (const IdentifierInfo *II = RD->getIdentifier())
309 return II->getName();
310
311 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
312 // used to reconstruct the fully qualified type names.
313 if (CGM.getCodeGenOpts().EmitCodeView) {
314 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
315 assert(RD->getDeclContext() == D->getDeclContext() &&
316 "Typedef should not be in another decl context!");
317 assert(D->getDeclName().getAsIdentifierInfo() &&
318 "Typedef was not named!");
319 return D->getDeclName().getAsIdentifierInfo()->getName();
320 }
321
322 if (CGM.getLangOpts().CPlusPlus) {
323 StringRef Name;
324
325 ASTContext &Context = CGM.getContext();
326 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
327 // Anonymous types without a name for linkage purposes have their
328 // declarator mangled in if they have one.
329 Name = DD->getName();
330 else if (const TypedefNameDecl *TND =
331 Context.getTypedefNameForUnnamedTagDecl(RD))
332 // Anonymous types without a name for linkage purposes have their
333 // associate typedef mangled in if they have one.
334 Name = TND->getName();
335
336 if (!Name.empty()) {
337 SmallString<256> UnnamedType("<unnamed-type-");
338 UnnamedType += Name;
339 UnnamedType += '>';
340 return internString(UnnamedType);
341 }
342 }
343 }
344
345 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000346}
347
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000348llvm::DIFile::ChecksumKind
349CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
350 Checksum.clear();
351
352 if (!CGM.getCodeGenOpts().EmitCodeView)
353 return llvm::DIFile::CSK_None;
354
355 SourceManager &SM = CGM.getContext().getSourceManager();
356 bool Invalid;
357 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
358 if (Invalid)
359 return llvm::DIFile::CSK_None;
360
361 llvm::MD5 Hash;
362 llvm::MD5::MD5Result Result;
363
364 Hash.update(MemBuffer->getBuffer());
365 Hash.final(Result);
366
367 Hash.stringifyResult(Result, Checksum);
368 return llvm::DIFile::CSK_MD5;
369}
370
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000371llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 if (!Loc.isValid())
373 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000374 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000375 remapDIPath(TheCU->getDirectory()),
376 TheCU->getFile()->getChecksumKind(),
377 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000378
379 SourceManager &SM = CGM.getContext().getSourceManager();
380 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
381
382 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
383 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000384 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000385 remapDIPath(TheCU->getDirectory()),
386 TheCU->getFile()->getChecksumKind(),
387 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000388
389 // Cache the results.
390 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000391 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000392
393 if (it != DIFileCache.end()) {
394 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000395 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000396 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000397 }
398
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000399 SmallString<32> Checksum;
400 llvm::DIFile::ChecksumKind CSKind =
401 computeChecksum(SM.getFileID(Loc), Checksum);
402
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000403 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000404 remapDIPath(getCurrentDirname()),
405 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000406
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000407 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return F;
409}
410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000411llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000412 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000413 remapDIPath(TheCU->getDirectory()),
414 TheCU->getFile()->getChecksumKind(),
415 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000416}
417
418std::string CGDebugInfo::remapDIPath(StringRef Path) const {
419 for (const auto &Entry : DebugPrefixMap)
420 if (Path.startswith(Entry.first))
421 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
422 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000423}
424
Guy Benyei11169dd2012-12-18 14:30:41 +0000425unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
426 if (Loc.isInvalid() && CurLoc.isInvalid())
427 return 0;
428 SourceManager &SM = CGM.getContext().getSourceManager();
429 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000430 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000431}
432
Adrian Prantlc7822422013-03-12 20:43:25 +0000433unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000435 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 return 0;
437
438 // If the location is invalid then use the current column.
439 if (Loc.isInvalid() && CurLoc.isInvalid())
440 return 0;
441 SourceManager &SM = CGM.getContext().getSourceManager();
442 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000444}
445
446StringRef CGDebugInfo::getCurrentDirname() {
447 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
448 return CGM.getCodeGenOpts().DebugCompilationDir;
449
450 if (!CWDName.empty())
451 return CWDName;
452 SmallString<256> CWD;
453 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000454 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000455}
456
Guy Benyei11169dd2012-12-18 14:30:41 +0000457void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000458 SmallString<32> Checksum;
459 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000460
David Blaikieaabde052014-05-14 00:29:00 +0000461 // Should we be asking the SourceManager for the main file name, instead of
462 // accepting it as an argument? This just causes the main file name to
463 // mismatch with source locations and create extra lexical scopes or
464 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
465 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
466 // because that's what the SourceManager says)
467
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 // Get absolute path name.
469 SourceManager &SM = CGM.getContext().getSourceManager();
470 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
471 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000472 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000473
474 // The main file name provided via the "-main-file-name" option contains just
475 // the file name itself with no path information. This file name may have had
476 // a relative path, so we look into the actual file entry for the main
477 // file to determine the real absolute path for the file.
478 std::string MainFileDir;
479 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000480 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000481 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000482 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
483 llvm::sys::path::append(MainFileDirSS, MainFileName);
484 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000485 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000486 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 }
488
Ed Masteda706022014-05-07 12:49:30 +0000489 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 const LangOptions &LO = CGM.getLangOpts();
491 if (LO.CPlusPlus) {
492 if (LO.ObjC1)
493 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
494 else
495 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
496 } else if (LO.ObjC1) {
497 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000498 } else if (LO.RenderScript) {
499 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 } else if (LO.C99) {
501 LangTag = llvm::dwarf::DW_LANG_C99;
502 } else {
503 LangTag = llvm::dwarf::DW_LANG_C89;
504 }
505
506 std::string Producer = getClangFullVersion();
507
508 // Figure out which version of the ObjC runtime we have.
509 unsigned RuntimeVers = 0;
510 if (LO.ObjC1)
511 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
512
Adrian Prantl826824e2016-04-08 22:43:06 +0000513 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
514 switch (DebugKind) {
515 case codegenoptions::NoDebugInfo:
516 case codegenoptions::LocTrackingOnly:
517 EmissionKind = llvm::DICompileUnit::NoDebug;
518 break;
519 case codegenoptions::DebugLineTablesOnly:
520 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
521 break;
522 case codegenoptions::LimitedDebugInfo:
523 case codegenoptions::FullDebugInfo:
524 EmissionKind = llvm::DICompileUnit::FullDebug;
525 break;
526 }
527
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000530 TheCU = DBuilder.createCompileUnit(
Amjad Aboudfa9a17e2016-12-14 20:24:40 +0000531 LangTag, DBuilder.createFile(remapDIPath(MainFileName),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000532 remapDIPath(getCurrentDirname()), CSKind,
533 Checksum),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000534 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000535 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
Dehao Chen5a3f8902017-02-01 22:45:21 +0000536 CGM.getCodeGenOpts().SplitDwarfInlining,
537 CGM.getCodeGenOpts().DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000538}
539
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000540llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000541 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 StringRef BTName;
543 switch (BT->getKind()) {
544#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000545#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000546#include "clang/AST/BuiltinTypes.def"
547 case BuiltinType::Dependent:
548 llvm_unreachable("Unexpected builtin type");
549 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000550 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000552 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000554 if (!ClassTy)
555 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
556 "objc_class", TheCU,
557 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000558 return ClassTy;
559 case BuiltinType::ObjCId: {
560 // typedef struct objc_class *Class;
561 // typedef struct objc_object {
562 // Class isa;
563 // } *id;
564
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000565 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 return ObjTy;
567
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000568 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000569 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
570 "objc_class", TheCU,
571 getOrCreateMainFile(), 0);
572
573 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000574
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000575 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000576
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000577 ObjTy = DBuilder.createStructType(
578 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
579 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000580
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000581 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000582 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
583 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
584 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 return ObjTy;
586 }
587 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000588 if (!SelTy)
589 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
590 "objc_selector", TheCU,
591 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 return SelTy;
593 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000594
Alexey Bader954ba212016-04-08 13:40:33 +0000595#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
596 case BuiltinType::Id: \
597 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
598 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000599#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000600 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000601 return getOrCreateStructPtrType("opencl_sampler_t",
602 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000603 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000604 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000605 case BuiltinType::OCLClkEvent:
606 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
607 case BuiltinType::OCLQueue:
608 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000609 case BuiltinType::OCLReserveID:
610 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000611
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000613 case BuiltinType::Char_U:
614 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
615 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000617 case BuiltinType::SChar:
618 Encoding = llvm::dwarf::DW_ATE_signed_char;
619 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000621 case BuiltinType::Char32:
622 Encoding = llvm::dwarf::DW_ATE_UTF;
623 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000624 case BuiltinType::UShort:
625 case BuiltinType::UInt:
626 case BuiltinType::UInt128:
627 case BuiltinType::ULong:
628 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000629 case BuiltinType::ULongLong:
630 Encoding = llvm::dwarf::DW_ATE_unsigned;
631 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000632 case BuiltinType::Short:
633 case BuiltinType::Int:
634 case BuiltinType::Int128:
635 case BuiltinType::Long:
636 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000637 case BuiltinType::LongLong:
638 Encoding = llvm::dwarf::DW_ATE_signed;
639 break;
640 case BuiltinType::Bool:
641 Encoding = llvm::dwarf::DW_ATE_boolean;
642 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 case BuiltinType::Half:
644 case BuiltinType::Float:
645 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000646 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000647 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000648 // FIXME: For targets where long double and __float128 have the same size,
649 // they are currently indistinguishable in the debugger without some
650 // special treatment. However, there is currently no consensus on encoding
651 // and this should be updated once a DWARF encoding exists for distinct
652 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000653 Encoding = llvm::dwarf::DW_ATE_float;
654 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000655 }
656
657 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000658 case BuiltinType::Long:
659 BTName = "long int";
660 break;
661 case BuiltinType::LongLong:
662 BTName = "long long int";
663 break;
664 case BuiltinType::ULong:
665 BTName = "long unsigned int";
666 break;
667 case BuiltinType::ULongLong:
668 BTName = "long long unsigned int";
669 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 default:
671 BTName = BT->getName(CGM.getLangOpts());
672 break;
673 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000674 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000676 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000677}
678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000679llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000680 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000681 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 if (Ty->isComplexIntegerType())
683 Encoding = llvm::dwarf::DW_ATE_lo_user;
684
685 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000686 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687}
688
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000689llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
690 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000691 QualifierCollector Qc;
692 const Type *T = Qc.strip(Ty);
693
694 // Ignore these qualifiers for now.
695 Qc.removeObjCGCAttr();
696 Qc.removeAddressSpace();
697 Qc.removeObjCLifetime();
698
699 // We will create one Derived type for one qualifier and recurse to handle any
700 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000701 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702 if (Qc.hasConst()) {
703 Tag = llvm::dwarf::DW_TAG_const_type;
704 Qc.removeConst();
705 } else if (Qc.hasVolatile()) {
706 Tag = llvm::dwarf::DW_TAG_volatile_type;
707 Qc.removeVolatile();
708 } else if (Qc.hasRestrict()) {
709 Tag = llvm::dwarf::DW_TAG_restrict_type;
710 Qc.removeRestrict();
711 } else {
712 assert(Qc.empty() && "Unknown type qualifier for debug info");
713 return getOrCreateType(QualType(T, 0), Unit);
714 }
715
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000716 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
718 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
719 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000720 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721}
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
724 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000725
726 // The frontend treats 'id' as a typedef to an ObjCObjectType,
727 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
728 // debug info, we want to emit 'id' in both cases.
729 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000730 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000731
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000732 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
733 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734}
735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000736llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
737 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000738 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000739 Ty->getPointeeType(), Unit);
740}
741
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000742/// \return whether a C++ mangling exists for the type defined by TD.
743static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
744 switch (TheCU->getSourceLanguage()) {
745 case llvm::dwarf::DW_LANG_C_plus_plus:
746 return true;
747 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
748 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
749 default:
750 return false;
751 }
752}
753
Manman Rene0064d82013-08-29 23:19:58 +0000754/// In C++ mode, types have linkage, so we can rely on the ODR and
755/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000756static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
757 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000758 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000759 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000760 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000761
762 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000763 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000764
Manman Rene0064d82013-08-29 23:19:58 +0000765 // TODO: This is using the RTTI name. Is there a better way to get
766 // a unique string for a type?
767 llvm::raw_svector_ostream Out(FullName);
768 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000769 return FullName;
770}
771
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000772/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000773static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
774 llvm::dwarf::Tag Tag;
775 if (RD->isStruct() || RD->isInterface())
776 Tag = llvm::dwarf::DW_TAG_structure_type;
777 else if (RD->isUnion())
778 Tag = llvm::dwarf::DW_TAG_union_type;
779 else {
780 // FIXME: This could be a struct type giving a default visibility different
781 // than C++ class type, but needs llvm metadata changes first.
782 assert(RD->isClass());
783 Tag = llvm::dwarf::DW_TAG_class_type;
784 }
785 return Tag;
786}
787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000788llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000789CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000790 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000791 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
793 return cast<llvm::DICompositeType>(T);
794 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 unsigned Line = getLineNumber(RD->getLocation());
796 StringRef RDName = getClassName(RD);
797
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000798 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000799 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000800
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000802 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000804 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000805 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000806 ReplaceMap.emplace_back(
807 std::piecewise_construct, std::make_tuple(Ty),
808 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000809 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000810}
811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000813 const Type *Ty,
814 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 // Bit size, align and offset of the type.
817 // Size is always the size of a pointer. We can't use getTypeSize here
818 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000819 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
820 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000821 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000822 Optional<unsigned> DWARFAddressSpace =
823 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000824
Keno Fischer87842f32015-11-16 09:04:13 +0000825 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
826 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
827 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000828 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000829 else
830 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000831 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000832}
833
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000834llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
835 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000836 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000837 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000838 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
839 TheCU, getOrCreateMainFile(), 0);
840 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
841 Cache = DBuilder.createPointerType(Cache, Size);
842 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000843}
844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
846 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000847 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000848 QualType FType;
849 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000850 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000851 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000852
853 FieldOffset = 0;
854 FType = CGM.getContext().UnsignedLongTy;
855 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
856 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
857
858 Elements = DBuilder.getOrCreateArray(EltTys);
859 EltTys.clear();
860
Leny Kholodov80c047d2016-09-06 10:48:04 +0000861 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000862 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000863
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000864 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000865 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000866 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000867
868 // Bit size, align and offset of the type.
869 uint64_t Size = CGM.getContext().getTypeSize(Ty);
870
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000871 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000872
873 FieldOffset = 0;
874 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
875 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
876 FType = CGM.getContext().IntTy;
877 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
878 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000879 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000880 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
881
882 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000883 FieldSize = CGM.getContext().getTypeSize(Ty);
884 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000885 EltTys.push_back(DBuilder.createMemberType(
886 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
887 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000888
889 FieldOffset += FieldSize;
890 Elements = DBuilder.getOrCreateArray(EltTys);
891
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000892 // The __block_literal_generic structs are marked with a special
893 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
894 // the debugger needs to know about. To allow type uniquing, emit
895 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000896 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000897 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000898 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000899
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000900 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000901}
902
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000903llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
904 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000905 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000906 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000907
908 SmallString<128> NS;
909 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000910 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
911 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000912
913 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000914 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000915 CGM.getContext().getPrintingPolicy());
916
David Majnemer58ed0f32016-07-17 00:39:12 +0000917 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000918 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000919
920 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000921 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
922 getLineNumber(Loc),
923 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000924}
925
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000926llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
927 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000928 // We don't set size information, but do specify where the typedef was
929 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000930 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000931
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000932 // Typedefs are derived from some other type.
933 return DBuilder.createTypedef(
934 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
935 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000936 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000937}
938
Reid Klecknerf00f8032016-06-08 20:41:54 +0000939static unsigned getDwarfCC(CallingConv CC) {
940 switch (CC) {
941 case CC_C:
942 // Avoid emitting DW_AT_calling_convention if the C convention was used.
943 return 0;
944
945 case CC_X86StdCall:
946 return llvm::dwarf::DW_CC_BORLAND_stdcall;
947 case CC_X86FastCall:
948 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
949 case CC_X86ThisCall:
950 return llvm::dwarf::DW_CC_BORLAND_thiscall;
951 case CC_X86VectorCall:
952 return llvm::dwarf::DW_CC_LLVM_vectorcall;
953 case CC_X86Pascal:
954 return llvm::dwarf::DW_CC_BORLAND_pascal;
955
956 // FIXME: Create new DW_CC_ codes for these calling conventions.
957 case CC_X86_64Win64:
958 case CC_X86_64SysV:
959 case CC_AAPCS:
960 case CC_AAPCS_VFP:
961 case CC_IntelOclBicc:
962 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000963 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000964 case CC_Swift:
965 case CC_PreserveMost:
966 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000967 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000968 return 0;
969 }
970 return 0;
971}
972
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000973llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
974 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000975 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000976
977 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000978 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000979
980 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000981 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000982 if (isa<FunctionNoProtoType>(Ty))
983 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000984 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
985 for (const QualType &ParamType : FPT->param_types())
986 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000987 if (FPT->isVariadic())
988 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000989 }
990
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000991 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000992 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000993 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000994}
995
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000996/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000997/// As an optimization, return 0 if the access specifier equals the
998/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000999static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1000 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001001 AccessSpecifier Default = clang::AS_none;
1002 if (RD && RD->isClass())
1003 Default = clang::AS_private;
1004 else if (RD && (RD->isStruct() || RD->isUnion()))
1005 Default = clang::AS_public;
1006
1007 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001008 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001009
Eric Christophere7b87e52014-10-26 23:40:33 +00001010 switch (Access) {
1011 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001012 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001013 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001014 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001015 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001016 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001017 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001018 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001019 }
1020 llvm_unreachable("unexpected access enumerator");
1021}
Guy Benyei11169dd2012-12-18 14:30:41 +00001022
David Majnemerb4b671e2016-06-30 03:01:59 +00001023llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1024 llvm::DIScope *RecordTy,
1025 const RecordDecl *RD) {
1026 StringRef Name = BitFieldDecl->getName();
1027 QualType Ty = BitFieldDecl->getType();
1028 SourceLocation Loc = BitFieldDecl->getLocation();
1029 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1030 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1031
1032 // Get the location for the field.
1033 llvm::DIFile *File = getOrCreateFile(Loc);
1034 unsigned Line = getLineNumber(Loc);
1035
1036 const CGBitFieldInfo &BitFieldInfo =
1037 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1038 uint64_t SizeInBits = BitFieldInfo.Size;
1039 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001040 uint64_t StorageOffsetInBits =
1041 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1042 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001043 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001044 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001045 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1046 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001047}
1048
1049llvm::DIType *
1050CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1051 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001052 uint32_t AlignInBits, llvm::DIFile *tunit,
1053 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001054 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055
1056 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 unsigned line = getLineNumber(loc);
1059
David Majnemer34b57492014-07-30 01:30:47 +00001060 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001061 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001063 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1064 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001065 if (!Align)
1066 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001067 }
1068
Leny Kholodov80c047d2016-09-06 10:48:04 +00001069 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001070 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001071 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072}
1073
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001074void CGDebugInfo::CollectRecordLambdaFields(
1075 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001076 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001077 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1078 // has the name and the location of the variable so we should iterate over
1079 // both concurrently.
1080 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1081 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1082 unsigned fieldno = 0;
1083 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001084 E = CXXDecl->captures_end();
1085 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001086 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001087 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001088 SourceLocation Loc = C.getLocation();
1089 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001090 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001091 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001092 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001093 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001094 llvm::DIType *FieldType = createFieldType(
1095 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001096 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001097 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001098 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001099 // TODO: Need to handle 'this' in some way by probably renaming the
1100 // this of the lambda class and having a field member of 'this' or
1101 // by using AT_object_pointer for the function and having that be
1102 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001103 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001105 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001106 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001107 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001108 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001109
1110 elements.push_back(fieldType);
1111 }
1112 }
1113}
1114
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001115llvm::DIDerivedType *
1116CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001117 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001118 // Create the descriptor for the static variable, with or without
1119 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001120 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001121 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1122 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001123
Eric Christopher91a31902013-01-16 01:22:32 +00001124 unsigned LineNumber = getLineNumber(Var->getLocation());
1125 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001126 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001127 if (Var->getInit()) {
1128 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001129 if (Value) {
1130 if (Value->isInt())
1131 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1132 if (Value->isFloat())
1133 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1134 }
Eric Christopher91a31902013-01-16 01:22:32 +00001135 }
1136
Leny Kholodov80c047d2016-09-06 10:48:04 +00001137 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001138 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001139 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001140 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001141 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001142 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001143}
1144
Eric Christophere7b87e52014-10-26 23:40:33 +00001145void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001146 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1147 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001148 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001149 StringRef name = field->getName();
1150 QualType type = field->getType();
1151
1152 // Ignore unnamed fields unless they're anonymous structs/unions.
1153 if (name.empty() && !type->isRecordType())
1154 return;
1155
David Majnemerb4b671e2016-06-30 03:01:59 +00001156 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001157 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001158 FieldType = createBitFieldType(field, RecordTy, RD);
1159 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001160 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001161 FieldType =
1162 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001163 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001164 }
1165
David Majnemerb4b671e2016-06-30 03:01:59 +00001166 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001167}
1168
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001169void CGDebugInfo::CollectRecordNestedRecord(
1170 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1171 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001172 // Injected class names are not considered nested records.
1173 if (isa<InjectedClassNameType>(Ty))
1174 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001175 SourceLocation Loc = RD->getLocation();
1176 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1177 elements.push_back(nestedType);
1178}
1179
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001180void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001181 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001182 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001183 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001184 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001185
Eric Christopher91a31902013-01-16 01:22:32 +00001186 if (CXXDecl && CXXDecl->isLambda())
1187 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1188 else {
1189 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001190
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001191 // Debug info for nested records is included in the member list only for
1192 // CodeView.
1193 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1194
Eric Christopher91a31902013-01-16 01:22:32 +00001195 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001196 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001197
Eric Christopher91a31902013-01-16 01:22:32 +00001198 // Static and non-static members should appear in the same order as
1199 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001200 for (const auto *I : record->decls())
1201 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001202 if (V->hasAttr<NoDebugAttr>())
1203 continue;
David Blaikiece763042013-08-20 21:49:21 +00001204 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001205 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001206 if (MI != StaticDataMemberCache.end()) {
1207 assert(MI->second &&
1208 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001209 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001210 } else {
1211 auto Field = CreateRecordStaticField(V, RecordTy, record);
1212 elements.push_back(Field);
1213 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001214 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001215 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1216 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001217
1218 // Bump field number for next field.
1219 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001220 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1221 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1222 nestedRec->getDeclContext() == record)
1223 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001224 }
1225}
1226
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001227llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001228CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001229 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001230 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001231 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001232 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001233 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001234 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1235 Func, Unit);
1236}
David Blaikie2aaf0652013-01-07 22:24:59 +00001237
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001238llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1239 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001241 llvm::DITypeRefArray Args(
1242 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001243 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001244 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001245
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001246 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001247
1248 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001249 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001250
David Blaikie2aaf0652013-01-07 22:24:59 +00001251 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001252 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001253 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1254 // Create pointer type directly in this case.
1255 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1256 QualType PointeeTy = ThisPtrTy->getPointeeType();
1257 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001258 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001259 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1261 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001262 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001263 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001264 // TODO: This and the artificial type below are misleading, the
1265 // types aren't artificial the argument is, but the current
1266 // metadata doesn't represent that.
1267 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1268 Elts.push_back(ThisPtrType);
1269 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001270 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001271 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001272 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1273 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001274 }
1275
1276 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001277 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1278 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001279
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001280 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001281
Leny Kholodov80c047d2016-09-06 10:48:04 +00001282 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001283 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001284 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001285 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001286 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001287
Reid Klecknerf00f8032016-06-08 20:41:54 +00001288 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1289 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001290}
1291
Eric Christopherb2a008c2013-05-16 00:45:12 +00001292/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001293/// inside a function.
1294static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001295 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001296 return isFunctionLocalClass(NRD);
1297 if (isa<FunctionDecl>(RD->getDeclContext()))
1298 return true;
1299 return false;
1300}
1301
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001302llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1303 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001304 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001305 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001306
Guy Benyei11169dd2012-12-18 14:30:41 +00001307 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001308 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001309
1310 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1311 // make sense to give a single ctor/dtor a linkage name.
1312 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001313 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1314 // property to use here. It may've been intended to model "is non-external
1315 // type" but misses cases of non-function-local but non-external classes such
1316 // as those in anonymous namespaces as well as the reverse - external types
1317 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001318 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1319 MethodLinkageName = CGM.getMangledName(Method);
1320
1321 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001322 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001323 unsigned MethodLine = 0;
1324 if (!Method->isImplicit()) {
1325 MethodDefUnit = getOrCreateFile(Method->getLocation());
1326 MethodLine = getLineNumber(Method->getLocation());
1327 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001328
1329 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001330 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001331 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001332 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001333 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001334 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001335
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 if (Method->isVirtual()) {
1337 if (Method->isPure())
1338 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1339 else
1340 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001341
Reid Kleckner216d0a12016-06-16 20:08:51 +00001342 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1343 // It doesn't make sense to give a virtual destructor a vtable index,
1344 // since a single destructor has two entries in the vtable.
1345 if (!isa<CXXDestructorDecl>(Method))
1346 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1347 } else {
1348 // Emit MS ABI vftable information. There is only one entry for the
1349 // deleting dtor.
1350 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1351 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1352 MicrosoftVTableContext::MethodVFTableLocation ML =
1353 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1354 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001355
1356 // CodeView only records the vftable offset in the class that introduces
1357 // the virtual method. This is possible because, unlike Itanium, the MS
1358 // C++ ABI does not include all virtual methods from non-primary bases in
1359 // the vtable for the most derived class. For example, if C inherits from
1360 // A and B, C's primary vftable will not include B's virtual methods.
1361 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1362 Flags |= llvm::DINode::FlagIntroducedVirtual;
1363
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001364 // The 'this' adjustment accounts for both the virtual and non-virtual
1365 // portions of the adjustment. Presumably the debugger only uses it when
1366 // it knows the dynamic type of an object.
1367 ThisAdjustment = CGM.getCXXABI()
1368 .getVirtualFunctionPrologueThisAdjustment(GD)
1369 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001370 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001371 ContainingType = RecordTy;
1372 }
1373
Guy Benyei11169dd2012-12-18 14:30:41 +00001374 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001375 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001376 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001377 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001379 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001380 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001381 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001382 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 }
1384 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001386 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001388 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001390
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001391 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1392 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001393 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001394 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1395 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1396 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001397
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001398 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001399
1400 return SP;
1401}
1402
Eric Christophere7b87e52014-10-26 23:40:33 +00001403void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001404 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1405 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001406
1407 // Since we want more than just the individual member decls if we
1408 // have templated functions iterate over every declaration to gather
1409 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001410 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001411 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1412 // If the member is implicit, don't add it to the member list. This avoids
1413 // the member being added to type units by LLVM, while still allowing it
1414 // to be emitted into the type declaration/reference inside the compile
1415 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001416 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001417 // FIXME: Handle Using(Shadow?)Decls here to create
1418 // DW_TAG_imported_declarations inside the class for base decls brought into
1419 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1420 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1421 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001422 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001423 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001424
1425 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1426 continue;
1427
David Blaikiefd580722014-10-06 05:18:55 +00001428 // Reuse the existing member function declaration if it exists.
1429 // It may be associated with the declaration of the type & should be
1430 // reused as we're building the definition.
1431 //
1432 // This situation can arise in the vtable-based debug info reduction where
1433 // implicit members are emitted in a non-vtable TU.
1434 auto MI = SPCache.find(Method->getCanonicalDecl());
1435 EltTys.push_back(MI == SPCache.end()
1436 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001437 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001439}
Guy Benyei11169dd2012-12-18 14:30:41 +00001440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001442 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001444 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1445 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1446 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001447
Bob Haarmandff36732016-10-25 22:19:32 +00001448 // If we are generating CodeView debug info, we also need to emit records for
1449 // indirect virtual base classes.
1450 if (CGM.getCodeGenOpts().EmitCodeView) {
1451 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1452 llvm::DINode::FlagIndirectVirtualBase);
1453 }
1454}
1455
1456void CGDebugInfo::CollectCXXBasesAux(
1457 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1458 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1459 const CXXRecordDecl::base_class_const_range &Bases,
1460 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1461 llvm::DINode::DIFlags StartingFlags) {
1462 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1463 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001464 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001465 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001466 if (!SeenTypes.insert(Base).second)
1467 continue;
1468 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1469 llvm::DINode::DIFlags BFlags = StartingFlags;
1470 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001471
Aaron Ballman574705e2014-03-13 15:41:46 +00001472 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001473 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1474 // virtual base offset offset is -ve. The code generator emits dwarf
1475 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001476 BaseOffset = 0 - CGM.getItaniumVTableContext()
1477 .getVirtualBaseOffsetOffset(RD, Base)
1478 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001479 } else {
1480 // In the MS ABI, store the vbtable offset, which is analogous to the
1481 // vbase offset offset in Itanium.
1482 BaseOffset =
1483 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1484 }
Bob Haarmandff36732016-10-25 22:19:32 +00001485 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 } else
1487 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1488 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1489 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001490
Adrian Prantl21361fb2014-08-29 22:44:27 +00001491 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001492 llvm::DIType *DTy =
1493 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001494 EltTys.push_back(DTy);
1495 }
1496}
1497
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001498llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001499CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1500 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001501 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001502 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001503 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1504 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001505 StringRef Name;
1506 if (TPList)
1507 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001508 switch (TA.getKind()) {
1509 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001510 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001511 TemplateParams.push_back(
1512 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001513 } break;
1514 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001515 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001516 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1517 TheCU, Name, TTy,
1518 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001519 } break;
1520 case TemplateArgument::Declaration: {
1521 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001522 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001523 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001524 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001525 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001526 // Variable pointer template parameters have a value that is the address
1527 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001528 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001529 V = CGM.GetAddrOfGlobalVar(VD);
1530 // Member function pointers have special support for building them, though
1531 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001532 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001533 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001534 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001535 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001536 // Member data pointers have special handling too to compute the fixed
1537 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001538 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001539 // These five lines (& possibly the above member function pointer
1540 // handling) might be able to be refactored to use similar code in
1541 // CodeGenModule::getMemberPointerConstant
1542 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1543 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001544 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001545 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001546 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001547 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1548 TheCU, Name, TTy,
1549 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001550 } break;
1551 case TemplateArgument::NullPtr: {
1552 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001553 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001554 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001555 // Special case member data pointer null values since they're actually -1
1556 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001557 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001558 // But treat member function pointers as simple zero integers because
1559 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1560 // CodeGen grows handling for values of non-null member function
1561 // pointers then perhaps we could remove this special case and rely on
1562 // EmitNullMemberPointer for member function pointers.
1563 if (MPT->isMemberDataPointer())
1564 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1565 if (!V)
1566 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001567 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001568 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001569 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001570 case TemplateArgument::Template:
1571 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001572 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001573 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1574 break;
1575 case TemplateArgument::Pack:
1576 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1577 TheCU, Name, nullptr,
1578 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1579 break;
David Majnemer5559d472013-08-24 08:21:10 +00001580 case TemplateArgument::Expression: {
1581 const Expr *E = TA.getAsExpr();
1582 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001583 if (E->isGLValue())
1584 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001585 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001586 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001587 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001588 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001589 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001590 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001591 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001592 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001593 case TemplateArgument::Null:
1594 llvm_unreachable(
1595 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 }
1597 }
1598 return DBuilder.getOrCreateArray(TemplateParams);
1599}
1600
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001601llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001602CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001603 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001604 if (FD->getTemplatedKind() ==
1605 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001606 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1607 ->getTemplate()
1608 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001609 return CollectTemplateParams(
1610 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001612 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001613}
1614
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001615llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1616 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001617 // Always get the full list of parameters, not just the ones from
1618 // the specialization.
1619 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001620 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001621 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001622 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001623}
1624
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001625llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001626 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001627 return VTablePtrType;
1628
1629 ASTContext &Context = CGM.getContext();
1630
1631 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001632 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001633 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001634 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001636 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1637 Optional<unsigned> DWARFAddressSpace =
1638 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1639
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001640 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001641 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1642 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1644 return VTablePtrType;
1645}
1646
Guy Benyei11169dd2012-12-18 14:30:41 +00001647StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001648 // Copy the gdb compatible name on the side and use its reference.
1649 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001650}
1651
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001652void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001653 SmallVectorImpl<llvm::Metadata *> &EltTys,
1654 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 // If this class is not dynamic then there is not any vtable info to collect.
1656 if (!RD->isDynamicClass())
1657 return;
1658
Reid Kleckner59812422016-08-31 20:35:01 +00001659 // Don't emit any vtable shape or vptr info if this class doesn't have an
1660 // extendable vfptr. This can happen if the class doesn't have virtual
1661 // methods, or in the MS ABI if those virtual methods only come from virtually
1662 // inherited bases.
1663 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1664 if (!RL.hasExtendableVFPtr())
1665 return;
1666
Reid Klecknerdc124992016-08-31 16:11:43 +00001667 // CodeView needs to know how large the vtable of every dynamic class is, so
1668 // emit a special named pointer type into the element list. The vptr type
1669 // points to this type as well.
1670 llvm::DIType *VPtrTy = nullptr;
1671 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1672 CGM.getTarget().getCXXABI().isMicrosoft();
1673 if (NeedVTableShape) {
1674 uint64_t PtrWidth =
1675 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1676 const VTableLayout &VFTLayout =
1677 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1678 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001679 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001680 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001681 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1682 Optional<unsigned> DWARFAddressSpace =
1683 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001684
1685 // Create a very wide void* type and insert it directly in the element list.
1686 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001687 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1688 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001689 EltTys.push_back(VTableType);
1690
1691 // The vptr is a pointer to this special vtable type.
1692 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1693 }
1694
1695 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001696 if (RL.getPrimaryBase())
1697 return;
1698
1699 if (!VPtrTy)
1700 VPtrTy = getOrCreateVTablePtrType(Unit);
1701
Guy Benyei11169dd2012-12-18 14:30:41 +00001702 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001703 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001704 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001705 llvm::DINode::FlagArtificial, VPtrTy);
1706 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001707}
1708
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001709llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001710 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001711 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001712 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001713 return T;
1714}
1715
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001716llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001717 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001718 return getOrCreateStandaloneType(D, Loc);
1719}
1720
1721llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1722 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001723 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001724 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001725 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001726 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001727
Adrian Prantl73409ce2013-03-11 18:33:46 +00001728 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 return T;
1730}
1731
David Blaikie483a9da2014-05-06 18:35:21 +00001732void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001733 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001734 return;
1735 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001736 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001737 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001738 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001739 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001740 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001741 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001742 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001743}
1744
David Blaikieb2e86eb2013-08-15 20:49:17 +00001745void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001746 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001747 !CGM.getLangOpts().CPlusPlus)
1748 completeRequiredType(RD);
1749}
1750
David Blaikieb11c8732017-01-30 06:36:08 +00001751/// Return true if the class or any of its methods are marked dllimport.
1752static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1753 if (RD->hasAttr<DLLImportAttr>())
1754 return true;
1755 for (const CXXMethodDecl *MD : RD->methods())
1756 if (MD->hasAttr<DLLImportAttr>())
1757 return true;
1758 return false;
1759}
1760
David Blaikie6943dea2013-08-20 01:28:15 +00001761void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001762 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1763 if (CXXRD->isDynamicClass() &&
1764 CGM.getVTableLinkage(CXXRD) ==
1765 llvm::GlobalValue::AvailableExternallyLinkage &&
1766 !isClassOrMethodDLLImport(CXXRD))
1767 return;
1768 completeClass(RD);
1769}
1770
1771void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001772 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001773 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001774 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001775 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001776 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001777 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001778 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001779 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001780 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001781 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001782}
1783
David Blaikie0e716b42014-03-03 23:48:23 +00001784static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1785 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001786 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1787 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001788 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001789 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001790 return true;
1791 return false;
1792}
1793
Adrian Prantl05fefa42016-04-25 20:52:40 +00001794/// Does a type definition exist in an imported clang module?
1795static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001796 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001797 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001798 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001799 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001800 if (!RD->isExternallyVisible() && RD->getName().empty())
1801 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001802 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001803 if (!CXXDecl->isCompleteDefinition())
1804 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001805 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1806 if (TemplateKind != TSK_Undeclared) {
1807 // This is a template, check the origin of the first member.
1808 if (CXXDecl->field_begin() == CXXDecl->field_end())
1809 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1810 if (!CXXDecl->field_begin()->isFromASTFile())
1811 return false;
1812 }
Adrian Prantl94913712016-04-26 23:42:43 +00001813 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001814 return true;
1815}
1816
Benjamin Kramer8c305922016-02-02 11:06:51 +00001817static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1818 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001819 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001820 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001821 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001822
Benjamin Kramer8c305922016-02-02 11:06:51 +00001823 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001824 return false;
1825
1826 if (!LangOpts.CPlusPlus)
1827 return false;
1828
1829 if (!RD->isCompleteDefinitionRequired())
1830 return true;
1831
David Majnemer58ed0f32016-07-17 00:39:12 +00001832 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001833
1834 if (!CXXDecl)
1835 return false;
1836
Adrian McCarthy99242982016-08-16 22:11:18 +00001837 // Only emit complete debug info for a dynamic class when its vtable is
1838 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001839 // across DLL boundaries, so skip this optimization if the class or any of its
1840 // methods are marked dllimport. This isn't a complete solution, since objects
1841 // without any dllimport methods can be used in one DLL and constructed in
1842 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001843 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001844 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001845 return true;
1846
1847 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001848 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001849 Spec = SD->getSpecializationKind();
1850
1851 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1852 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1853 CXXDecl->method_end()))
1854 return true;
1855
1856 return false;
1857}
1858
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001859void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1860 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1861 return;
1862
1863 QualType Ty = CGM.getContext().getRecordType(RD);
1864 llvm::DIType *T = getTypeOrNull(Ty);
1865 if (T && T->isForwardDecl())
1866 completeClassData(RD);
1867}
1868
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001869llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001870 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001872 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1873 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001874 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001875 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001876 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001877 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001878
David Blaikieb2e86eb2013-08-15 20:49:17 +00001879 return CreateTypeDefinition(Ty);
1880}
1881
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001882llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001883 RecordDecl *RD = Ty->getDecl();
1884
Guy Benyei11169dd2012-12-18 14:30:41 +00001885 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001886 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001887
1888 // Records and classes and unions can all be recursive. To handle them, we
1889 // first generate a debug descriptor for the struct as a forward declaration.
1890 // Then (if it is a definition) we go through and get debug info for all of
1891 // its members. Finally, we create a descriptor for the complete type (which
1892 // may refer to the forward decl if the struct is recursive) and replace all
1893 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001894 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001895
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001896 const RecordDecl *D = RD->getDefinition();
1897 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 return FwdDecl;
1899
David Majnemer58ed0f32016-07-17 00:39:12 +00001900 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001901 CollectContainingType(CXXDecl, FwdDecl);
1902
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001904 LexicalBlockStack.emplace_back(&*FwdDecl);
1905 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001906
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001908 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001909 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001910
1911 // Note: The split of CXXDecl information here is intentional, the
1912 // gdb tests will depend on a certain ordering at printout. The debug
1913 // information offsets are still correct if we merge them all together
1914 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001915 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001916 if (CXXDecl) {
1917 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001918 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001919 }
1920
Eric Christopher91a31902013-01-16 01:22:32 +00001921 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001923 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001925
1926 LexicalBlockStack.pop_back();
1927 RegionMap.erase(Ty->getDecl());
1928
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001930 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001931
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001932 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001933 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001934 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001935
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001936 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001937 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001938}
1939
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001940llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1941 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 // Ignore protocols.
1943 return getOrCreateType(Ty->getBaseType(), Unit);
1944}
1945
Manman Rene6be26c2016-09-13 17:25:08 +00001946llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1947 llvm::DIFile *Unit) {
1948 // Ignore protocols.
1949 SourceLocation Loc = Ty->getDecl()->getLocation();
1950
1951 // Use Typedefs to represent ObjCTypeParamType.
1952 return DBuilder.createTypedef(
1953 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1954 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1955 getDeclContextDescriptor(Ty->getDecl()));
1956}
1957
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001958/// \return true if Getter has the default name for the property PD.
1959static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1960 const ObjCMethodDecl *Getter) {
1961 assert(PD);
1962 if (!Getter)
1963 return true;
1964
1965 assert(Getter->getDeclName().isObjCZeroArgSelector());
1966 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001967 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001968}
1969
1970/// \return true if Setter has the default name for the property PD.
1971static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1972 const ObjCMethodDecl *Setter) {
1973 assert(PD);
1974 if (!Setter)
1975 return true;
1976
1977 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001978 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001979 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001980}
1981
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001982llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1983 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 ObjCInterfaceDecl *ID = Ty->getDecl();
1985 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001986 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001987
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001988 // Return a forward declaration if this type was imported from a clang module,
1989 // and this is not the compile unit with the implementation of the type (which
1990 // may contain hidden ivars).
1991 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1992 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001993 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1994 ID->getName(),
1995 getDeclContextDescriptor(ID), Unit, 0);
1996
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001998 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002000 auto RuntimeLang =
2001 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002002
2003 // If this is just a forward declaration return a special forward-declaration
2004 // debug type since we won't be able to lay out the entire type.
2005 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002006 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002007 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002008 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002009 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2010 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002011 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 return FwdDecl;
2013 }
2014
David Blaikieef8a9512014-05-05 23:23:53 +00002015 return CreateTypeDefinition(Ty, Unit);
2016}
2017
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002018llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002019CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2020 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002021 // Use the Module pointer as the key into the cache. This is a
2022 // nullptr if the "Module" is a PCH, which is safe because we don't
2023 // support chained PCH debug info, so there can only be a single PCH.
2024 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002025 auto ModRef = ModuleCache.find(M);
2026 if (ModRef != ModuleCache.end())
2027 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002028
2029 // Macro definitions that were defined with "-D" on the command line.
2030 SmallString<128> ConfigMacros;
2031 {
2032 llvm::raw_svector_ostream OS(ConfigMacros);
2033 const auto &PPOpts = CGM.getPreprocessorOpts();
2034 unsigned I = 0;
2035 // Translate the macro definitions back into a commmand line.
2036 for (auto &M : PPOpts.Macros) {
2037 if (++I > 1)
2038 OS << " ";
2039 const std::string &Macro = M.first;
2040 bool Undef = M.second;
2041 OS << "\"-" << (Undef ? 'U' : 'D');
2042 for (char c : Macro)
2043 switch (c) {
2044 case '\\' : OS << "\\\\"; break;
2045 case '"' : OS << "\\\""; break;
2046 default: OS << c;
2047 }
2048 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002049 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002050 }
Adrian Prantl66689202015-09-18 23:01:45 +00002051
Adrian Prantl835e6632015-09-24 16:10:10 +00002052 bool IsRootModule = M ? !M->Parent : true;
2053 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002054 // PCH files don't have a signature field in the control block,
2055 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00002056 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002057 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002058 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2059 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2060 TheCU->getProducer(), true, StringRef(), 0,
2061 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2062 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002063 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002064 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002065 llvm::DIModule *Parent =
2066 IsRootModule ? nullptr
2067 : getOrCreateModuleRef(
2068 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2069 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002070 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002071 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2072 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002073 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002074 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002075}
2076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002077llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2078 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002079 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002080 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002081 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002082 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002083
2084 // Bit size, align and offset of the type.
2085 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002086 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002087
Leny Kholodov80c047d2016-09-06 10:48:04 +00002088 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002090 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002091
Adrian Prantlfd696112015-10-01 00:48:51 +00002092 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002093 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002094 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2095 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002096
David Blaikieef8a9512014-05-05 23:23:53 +00002097 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002098 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002099
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002100 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002101 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002102 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
2104 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002105 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002106
2107 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2108 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002109 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002110 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002111 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002112 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002113
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002114 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2115 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 EltTys.push_back(InhTag);
2117 }
2118
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002119 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002120 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 unsigned PLine = getLineNumber(Loc);
2124 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2125 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002126 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2127 PD->getName(), PUnit, PLine,
2128 hasDefaultGetterName(PD, Getter) ? ""
2129 : getSelectorName(PD->getGetterName()),
2130 hasDefaultSetterName(PD, Setter) ? ""
2131 : getSelectorName(PD->getSetterName()),
2132 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002133 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002134 };
2135 {
2136 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002137 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002138 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002139 PropertySet.insert(PD->getIdentifier());
2140 AddProperty(PD);
2141 }
Manman Renefe1bac2016-01-27 20:00:32 +00002142 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002143 // Don't emit duplicate metadata for properties that were already in a
2144 // class extension.
2145 if (!PropertySet.insert(PD->getIdentifier()).second)
2146 continue;
2147 AddProperty(PD);
2148 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 }
2150
2151 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2152 unsigned FieldNo = 0;
2153 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2154 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002155 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002156 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002157 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002158
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 StringRef FieldName = Field->getName();
2160
2161 // Ignore unnamed fields.
2162 if (FieldName.empty())
2163 continue;
2164
2165 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002166 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 unsigned FieldLine = getLineNumber(Field->getLocation());
2168 QualType FType = Field->getType();
2169 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002170 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
2172 if (!FType->isIncompleteArrayType()) {
2173
2174 // Bit size, align and offset of the type.
2175 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002176 ? Field->getBitWidthValue(CGM.getContext())
2177 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002178 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 }
2180
2181 uint64_t FieldOffset;
2182 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2183 // We don't know the runtime offset of an ivar if we're using the
2184 // non-fragile ABI. For bitfields, use the bit offset into the first
2185 // byte of storage of the bitfield. For other fields, use zero.
2186 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002187 FieldOffset =
2188 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002189 FieldOffset %= CGM.getContext().getCharWidth();
2190 } else {
2191 FieldOffset = 0;
2192 }
2193 } else {
2194 FieldOffset = RL.getFieldOffset(FieldNo);
2195 }
2196
Leny Kholodov80c047d2016-09-06 10:48:04 +00002197 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002199 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002201 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002202 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002203 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002204
Craig Topper8a13c412014-05-21 05:09:00 +00002205 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002207 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002208 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002210 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002211 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002212 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2214 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002215 PropertyNode = DBuilder.createObjCProperty(
2216 PD->getName(), PUnit, PLine,
2217 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2218 PD->getGetterName()),
2219 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2220 PD->getSetterName()),
2221 PD->getPropertyAttributes(),
2222 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002223 }
2224 }
2225 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002226 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2227 FieldSize, FieldAlign, FieldOffset, Flags,
2228 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 EltTys.push_back(FieldTy);
2230 }
2231
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002232 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002233 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002234
Guy Benyei11169dd2012-12-18 14:30:41 +00002235 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002236 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002237}
2238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002239llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2240 llvm::DIFile *Unit) {
2241 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002242 int64_t Count = Ty->getNumElements();
2243 if (Count == 0)
2244 // If number of elements are not known then this is an unbounded array.
2245 // Use Count == -1 to express such arrays.
2246 Count = -1;
2247
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002248 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002249 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002250
2251 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002252 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002253
2254 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2255}
2256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002257llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002259 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
2261 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002262 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002263 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002264 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2265 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002266 } else if (Ty->isIncompleteArrayType()) {
2267 Size = 0;
2268 if (Ty->getElementType()->isIncompleteType())
2269 Align = 0;
2270 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002271 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002272 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002273 Size = 0;
2274 Align = 0;
2275 } else {
2276 // Size and align of the whole array, not the element type.
2277 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002278 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002279 }
2280
2281 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2282 // interior arrays, do we care? Why aren't nested arrays represented the
2283 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002284 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 QualType EltTy(Ty, 0);
2286 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2287 // If the number of elements is known, then count is that number. Otherwise,
2288 // it's -1. This allows us to represent a subrange with an array of 0
2289 // elements, like this:
2290 //
2291 // struct foo {
2292 // int x[0];
2293 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002294 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002295 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002296 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002297 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002298 if (Expr *Size = VAT->getSizeExpr()) {
2299 llvm::APSInt V;
2300 if (Size->EvaluateAsInt(V, CGM.getContext()))
2301 Count = V.getExtValue();
2302 }
David Blaikie87173f12016-08-22 17:49:56 +00002303 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002304
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 // FIXME: Verify this is right for VLAs.
2306 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2307 EltTy = Ty->getElementType();
2308 }
2309
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002310 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002311
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002312 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2313 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002314}
2315
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2317 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002318 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2319 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002320}
2321
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002322llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2323 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002324 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2325 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002326}
2327
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002328llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2329 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002330 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002331 uint64_t Size = 0;
2332
2333 if (!Ty->isIncompleteType()) {
2334 Size = CGM.getContext().getTypeSize(Ty);
2335
2336 // Set the MS inheritance model. There is no flag for the unspecified model.
2337 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2338 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2339 case MSInheritanceAttr::Keyword_single_inheritance:
2340 Flags |= llvm::DINode::FlagSingleInheritance;
2341 break;
2342 case MSInheritanceAttr::Keyword_multiple_inheritance:
2343 Flags |= llvm::DINode::FlagMultipleInheritance;
2344 break;
2345 case MSInheritanceAttr::Keyword_virtual_inheritance:
2346 Flags |= llvm::DINode::FlagVirtualInheritance;
2347 break;
2348 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2349 break;
2350 }
2351 }
2352 }
2353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002354 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002355 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002356 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002357 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2358 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002359
2360 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002361 Ty->getPointeeType()->getAs<FunctionProtoType>();
2362 return DBuilder.createMemberPointerType(
2363 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2364 Ty->getClass(), FPT->getTypeQuals())),
2365 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002366 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002367}
2368
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002370 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2371 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002372}
2373
Xiuli Pan9c14e282016-01-09 12:53:17 +00002374llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2375 llvm::DIFile *U) {
2376 return getOrCreateType(Ty->getElementType(), U);
2377}
2378
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002379llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002380 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002381
Guy Benyei11169dd2012-12-18 14:30:41 +00002382 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002383 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002384 if (!ED->getTypeForDecl()->isIncompleteType()) {
2385 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002386 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002387 }
2388
Manman Rene0064d82013-08-29 23:19:58 +00002389 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2390
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002391 bool isImportedFromModule =
2392 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2393
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 // If this is just a forward declaration, construct an appropriately
2395 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002396 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002397 // Note that it is possible for enums to be created as part of
2398 // their own declcontext. In this case a FwdDecl will be created
2399 // twice. This doesn't cause a problem because both FwdDecls are
2400 // entered into the ReplaceMap: finalize() will replace the first
2401 // FwdDecl with the second and then replace the second with
2402 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002403 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002404 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002405 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2406 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002407
Guy Benyei11169dd2012-12-18 14:30:41 +00002408 unsigned Line = getLineNumber(ED->getLocation());
2409 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002410 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002411 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2412 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002413
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002414 ReplaceMap.emplace_back(
2415 std::piecewise_construct, std::make_tuple(Ty),
2416 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002417 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002418 }
2419
David Blaikie483a9da2014-05-06 18:35:21 +00002420 return CreateTypeDefinition(Ty);
2421}
2422
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002423llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002424 const EnumDecl *ED = Ty->getDecl();
2425 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002426 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002427 if (!ED->getTypeForDecl()->isIncompleteType()) {
2428 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002429 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002430 }
2431
2432 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2433
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002434 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002435 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002436 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002437 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002438 Enumerators.push_back(DBuilder.createEnumerator(
2439 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 }
2441
2442 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002443 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002444
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002445 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002446 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002447 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002448 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002449 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2450 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2451 Line, Size, Align, EltArray, ClassTy,
2452 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002453}
2454
Amjad Aboud546bc112017-02-09 22:07:24 +00002455llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2456 unsigned MType, SourceLocation LineLoc,
2457 StringRef Name, StringRef Value) {
2458 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2459 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2460}
2461
2462llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2463 SourceLocation LineLoc,
2464 SourceLocation FileLoc) {
2465 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2466 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2467 return DBuilder.createTempMacroFile(Parent, Line, FName);
2468}
2469
David Blaikie05491062013-01-21 04:37:12 +00002470static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2471 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002473 Qualifiers InnerQuals = T.getLocalQualifiers();
2474 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2475 // that is already there.
2476 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2477 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 QualType LastT = T;
2479 switch (T->getTypeClass()) {
2480 default:
David Blaikie05491062013-01-21 04:37:12 +00002481 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002482 case Type::TemplateSpecialization: {
2483 const auto *Spec = cast<TemplateSpecializationType>(T);
2484 if (Spec->isTypeAlias())
2485 return C.getQualifiedType(T.getTypePtr(), Quals);
2486 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002487 break;
2488 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 case Type::TypeOfExpr:
2490 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2491 break;
2492 case Type::TypeOf:
2493 T = cast<TypeOfType>(T)->getUnderlyingType();
2494 break;
2495 case Type::Decltype:
2496 T = cast<DecltypeType>(T)->getUnderlyingType();
2497 break;
2498 case Type::UnaryTransform:
2499 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2500 break;
2501 case Type::Attributed:
2502 T = cast<AttributedType>(T)->getEquivalentType();
2503 break;
2504 case Type::Elaborated:
2505 T = cast<ElaboratedType>(T)->getNamedType();
2506 break;
2507 case Type::Paren:
2508 T = cast<ParenType>(T)->getInnerType();
2509 break;
David Blaikie05491062013-01-21 04:37:12 +00002510 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002511 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002512 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002513 case Type::Auto:
2514 case Type::DeducedTemplateSpecialization: {
2515 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002516 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002517 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 break;
2519 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002520 case Type::Adjusted:
2521 case Type::Decayed:
2522 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2523 T = cast<AdjustedType>(T)->getAdjustedType();
2524 break;
2525 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002526
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002528 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 } while (true);
2530}
2531
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002532llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002533
2534 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002535 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002536
David Blaikief427b002014-05-06 03:42:01 +00002537 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002538 if (it != TypeCache.end()) {
2539 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002540 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002541 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002542 }
2543
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002544 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002545}
2546
David Blaikie0e716b42014-03-03 23:48:23 +00002547void CGDebugInfo::completeTemplateDefinition(
2548 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002549 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002550 return;
2551
David Blaikie0e716b42014-03-03 23:48:23 +00002552 completeClassData(&SD);
2553 // In case this type has no member function definitions being emitted, ensure
2554 // it is retained
2555 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2556}
2557
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002558llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002560 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002561
2562 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002563 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002564
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002565 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 return T;
2567
Adrian Prantlca844182015-09-11 17:23:03 +00002568 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002569 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002570
2571 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002572 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002573
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 return Res;
2575}
2576
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002577llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002578 // A forward declaration inside a module header does not belong to the module.
2579 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2580 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002581 if (DebugTypeExtRefs && D->isFromASTFile()) {
2582 // Record a reference to an imported clang module or precompiled header.
2583 auto *Reader = CGM.getContext().getExternalSource();
2584 auto Idx = D->getOwningModuleID();
2585 auto Info = Reader->getSourceDescriptor(Idx);
2586 if (Info)
2587 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2588 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002589 // We are building a clang module or a precompiled header.
2590 //
2591 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2592 // and it wouldn't be necessary to specify the parent scope
2593 // because the type is already unique by definition (it would look
2594 // like the output of -fno-standalone-debug). On the other hand,
2595 // the parent scope helps a consumer to quickly locate the object
2596 // file where the type's definition is located, so it might be
2597 // best to make this behavior a command line or debugger tuning
2598 // option.
2599 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2600 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002601 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002602 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2603 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002604 } else {
2605 // This the precompiled header being built.
2606 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002607 }
2608 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002609
Adrian Prantl9402cef2015-09-20 16:51:35 +00002610 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002611}
2612
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002613llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 // Handle qualifiers, which recursively handles what they refer to.
2615 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002616 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002617
Guy Benyei11169dd2012-12-18 14:30:41 +00002618 // Work out details of type.
2619 switch (Ty->getTypeClass()) {
2620#define TYPE(Class, Base)
2621#define ABSTRACT_TYPE(Class, Base)
2622#define NON_CANONICAL_TYPE(Class, Base)
2623#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2624#include "clang/AST/TypeNodes.def"
2625 llvm_unreachable("Dependent types cannot show up in debug information");
2626
2627 case Type::ExtVector:
2628 case Type::Vector:
2629 return CreateType(cast<VectorType>(Ty), Unit);
2630 case Type::ObjCObjectPointer:
2631 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2632 case Type::ObjCObject:
2633 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002634 case Type::ObjCTypeParam:
2635 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 case Type::ObjCInterface:
2637 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2638 case Type::Builtin:
2639 return CreateType(cast<BuiltinType>(Ty));
2640 case Type::Complex:
2641 return CreateType(cast<ComplexType>(Ty));
2642 case Type::Pointer:
2643 return CreateType(cast<PointerType>(Ty), Unit);
2644 case Type::BlockPointer:
2645 return CreateType(cast<BlockPointerType>(Ty), Unit);
2646 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002647 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002649 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002650 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002651 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 case Type::FunctionProto:
2653 case Type::FunctionNoProto:
2654 return CreateType(cast<FunctionType>(Ty), Unit);
2655 case Type::ConstantArray:
2656 case Type::VariableArray:
2657 case Type::IncompleteArray:
2658 return CreateType(cast<ArrayType>(Ty), Unit);
2659
2660 case Type::LValueReference:
2661 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2662 case Type::RValueReference:
2663 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2664
2665 case Type::MemberPointer:
2666 return CreateType(cast<MemberPointerType>(Ty), Unit);
2667
2668 case Type::Atomic:
2669 return CreateType(cast<AtomicType>(Ty), Unit);
2670
Xiuli Pan9c14e282016-01-09 12:53:17 +00002671 case Type::Pipe:
2672 return CreateType(cast<PipeType>(Ty), Unit);
2673
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002675 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2676
David Blaikie42edade2014-11-11 20:44:45 +00002677 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002678 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002679 case Type::Adjusted:
2680 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002681 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 case Type::Elaborated:
2683 case Type::Paren:
2684 case Type::SubstTemplateTypeParm:
2685 case Type::TypeOfExpr:
2686 case Type::TypeOf:
2687 case Type::Decltype:
2688 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002689 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002690 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002691 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002692
David Blaikie42edade2014-11-11 20:44:45 +00002693 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002694}
2695
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002696llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2697 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002698 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002699
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002700 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002701
2702 // We may have cached a forward decl when we could have created
2703 // a non-forward decl. Go ahead and create a non-forward decl
2704 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002705 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002706 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002707
2708 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002709 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002710
2711 // Propagate members from the declaration to the definition
2712 // CreateType(const RecordType*) will overwrite this with the members in the
2713 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002714 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002717 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 return Res;
2719}
2720
2721// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002722llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002724
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002726 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002727 unsigned Line = getLineNumber(RD->getLocation());
2728 StringRef RDName = getClassName(RD);
2729
Amjad Abouddc4531e2016-04-30 01:44:38 +00002730 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002731
David Blaikied2785892013-08-18 17:36:19 +00002732 // If we ended up creating the type during the context chain construction,
2733 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002734 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002735 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002736 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002737 return T;
David Blaikied2785892013-08-18 17:36:19 +00002738
Adrian Prantl381e7552014-02-04 21:29:50 +00002739 // If this is just a forward or incomplete declaration, construct an
2740 // appropriately marked node and just return it.
2741 const RecordDecl *D = RD->getDefinition();
2742 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002743 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002744
2745 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002746 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002747
Manman Rene0064d82013-08-29 23:19:58 +00002748 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2749
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002750 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002751 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2752 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002753
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002754 // Elements of composite types usually have back to the type, creating
2755 // uniquing cycles. Distinct nodes are more efficient.
2756 switch (RealDecl->getTag()) {
2757 default:
2758 llvm_unreachable("invalid composite type tag");
2759
2760 case llvm::dwarf::DW_TAG_array_type:
2761 case llvm::dwarf::DW_TAG_enumeration_type:
2762 // Array elements and most enumeration elements don't have back references,
2763 // so they don't tend to be involved in uniquing cycles and there is some
2764 // chance of merging them when linking together two modules. Only make
2765 // them distinct if they are ODR-uniqued.
2766 if (FullName.empty())
2767 break;
2768
2769 case llvm::dwarf::DW_TAG_structure_type:
2770 case llvm::dwarf::DW_TAG_union_type:
2771 case llvm::dwarf::DW_TAG_class_type:
2772 // Immediatley resolve to a distinct node.
2773 RealDecl =
2774 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2775 break;
2776 }
2777
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002778 RegionMap[Ty->getDecl()].reset(RealDecl);
2779 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002780
David Majnemer58ed0f32016-07-17 00:39:12 +00002781 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002782 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002783 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002784 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002785}
2786
David Blaikieadfbf992013-08-18 16:55:33 +00002787void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002788 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002789 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002790 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002791 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2792 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002793 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002794 while (1) {
2795 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2796 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2797 if (PBT && !BRL.isPrimaryBaseVirtual())
2798 PBase = PBT;
2799 else
2800 break;
2801 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002802 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002803 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2804 getOrCreateFile(RD->getLocation())));
2805 } else if (RD->isDynamicClass())
2806 ContainingType = RealDecl;
2807
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002808 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002809}
2810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002811llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002812 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002813 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002815 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002816 llvm::DIType *Ty =
2817 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2818 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 *Offset += FieldSize;
2820 return Ty;
2821}
2822
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002823void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002824 StringRef &Name,
2825 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002826 llvm::DIScope *&FDContext,
2827 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002828 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002829 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002830 Name = getFunctionName(FD);
2831 // Use mangled name as linkage name for C/C++ functions.
2832 if (FD->hasPrototype()) {
2833 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002834 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002835 }
2836 // No need to replicate the linkage name if it isn't different from the
2837 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002838 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002839 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2840 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002841 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002842 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002843 LinkageName = StringRef();
2844
Benjamin Kramer8c305922016-02-02 11:06:51 +00002845 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002846 if (const NamespaceDecl *NSDecl =
2847 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2848 FDContext = getOrCreateNameSpace(NSDecl);
2849 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002850 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2851 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2852 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2853 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002854 // Check if it is a noreturn-marked function
2855 if (FD->isNoReturn())
2856 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002857 // Collect template parameters.
2858 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2859 }
2860}
2861
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002862void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002863 unsigned &LineNo, QualType &T,
2864 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002865 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002866 Unit = getOrCreateFile(VD->getLocation());
2867 LineNo = getLineNumber(VD->getLocation());
2868
2869 setLocation(VD->getLocation());
2870
2871 T = VD->getType();
2872 if (T->isIncompleteArrayType()) {
2873 // CodeGen turns int[] into int[1] so we'll do the same here.
2874 llvm::APInt ConstVal(32, 1);
2875 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2876
2877 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2878 ArrayType::Normal, 0);
2879 }
2880
2881 Name = VD->getName();
2882 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2883 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2884 LinkageName = CGM.getMangledName(VD);
2885 if (LinkageName == Name)
2886 LinkageName = StringRef();
2887
2888 // Since we emit declarations (DW_AT_members) for static members, place the
2889 // definition of those static members in the namespace they were declared in
2890 // in the source code (the lexical decl context).
2891 // FIXME: Generalize this for even non-member global variables where the
2892 // declaration and definition may have different lexical decl contexts, once
2893 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002894 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2895 : VD->getDeclContext();
2896 // When a record type contains an in-line initialization of a static data
2897 // member, and the record type is marked as __declspec(dllexport), an implicit
2898 // definition of the member will be created in the record context. DWARF
2899 // doesn't seem to have a nice way to describe this in a form that consumers
2900 // are likely to understand, so fake the "normal" situation of a definition
2901 // outside the class by putting it in the global scope.
2902 if (DC->isRecord())
2903 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002904
Amjad Abouddc4531e2016-04-30 01:44:38 +00002905 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2906 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002907}
2908
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002909llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2910 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002911 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002912 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002913 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002914 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002915 llvm::DIFile *Unit = getOrCreateFile(Loc);
2916 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002917 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002918 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002919 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002920 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2921
Frederic Rissd253ed62014-11-18 03:40:51 +00002922 // Build function type.
2923 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002924 if (FD)
2925 for (const ParmVarDecl *Parm : FD->parameters())
2926 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002927 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2928 QualType FnType = CGM.getContext().getFunctionType(
2929 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002930 if (Stub) {
2931 return DBuilder.createFunction(
2932 DContext, Name, LinkageName, Unit, Line,
2933 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2934 !FD->isExternallyVisible(),
2935 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2936 TParamsArray.get(), getFunctionDeclaration(FD));
2937 }
2938
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002939 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002940 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002941 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2942 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002943 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002944 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002945 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002946 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2947 std::make_tuple(CanonDecl),
2948 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002949 return SP;
2950}
2951
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002952llvm::DISubprogram *
2953CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
2954 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
2955}
2956
2957llvm::DISubprogram *
2958CGDebugInfo::getFunctionStub(GlobalDecl GD) {
2959 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
2960}
2961
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002962llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002963CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2964 QualType T;
2965 StringRef Name, LinkageName;
2966 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002967 llvm::DIFile *Unit = getOrCreateFile(Loc);
2968 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002969 unsigned Line = getLineNumber(Loc);
2970
2971 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002972 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002973 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2974 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002975 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002976 FwdDeclReplaceMap.emplace_back(
2977 std::piecewise_construct,
2978 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2979 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002980 return GV;
2981}
2982
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002983llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002984 // We only need a declaration (not a definition) of the type - so use whatever
2985 // we would otherwise do to get a type for a pointee. (forward declarations in
2986 // limited debug info, full definitions (if the type definition is available)
2987 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002988 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002989 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002990 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002991 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002992
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002993 if (I != DeclCache.end()) {
2994 auto N = I->second;
2995 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
2996 return GVE->getVariable();
2997 return dyn_cast_or_null<llvm::DINode>(N);
2998 }
Frederic Rissd253ed62014-11-18 03:40:51 +00002999
3000 // No definition for now. Emit a forward definition that might be
3001 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003002 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003003 return getFunctionForwardDeclaration(FD);
3004 else if (const auto *VD = dyn_cast<VarDecl>(D))
3005 return getGlobalVariableForwardDeclaration(VD);
3006
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003007 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003008}
3009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003010llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003011 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003012 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003013
David Majnemer58ed0f32016-07-17 00:39:12 +00003014 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003015 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003016 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003017
3018 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003019 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003020
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003021 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003022 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003023 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003024 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003025 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003026 }
3027 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003029 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003030 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 return SP;
3032 }
3033
Aaron Ballman86c93902014-03-06 23:45:36 +00003034 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003035 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003037 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003038 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003039 return SP;
3040 }
3041 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003042 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003043}
3044
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003045// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003046// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003047llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003048 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003049 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003050 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003051 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3052 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003053 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003054
David Majnemer58ed0f32016-07-17 00:39:12 +00003055 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003056 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003057
3058 const auto *FTy = FnType->getAs<FunctionType>();
3059 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3060
David Majnemer58ed0f32016-07-17 00:39:12 +00003061 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003063 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003064
3065 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003066 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003067
3068 // Replace the instancetype keyword with the actual type.
3069 if (ResultTy == CGM.getContext().getObjCInstanceType())
3070 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003071 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003072
Adrian Prantl7bec9032013-05-10 21:08:31 +00003073 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003075 QualType SelfDeclTy;
3076 if (auto *SelfDecl = OMethod->getSelfDecl())
3077 SelfDeclTy = SelfDecl->getType();
3078 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3079 if (FPT->getNumParams() > 1)
3080 SelfDeclTy = FPT->getParamType(0);
3081 if (!SelfDeclTy.isNull())
3082 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003084 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003085 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003087 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003088 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003089 // Variadic methods need a special marker at the end of the type list.
3090 if (OMethod->isVariadic())
3091 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003092
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003093 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003094 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3095 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003096 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003097
Adrian Prantl800faef2014-02-25 23:42:18 +00003098 // Handle variadic function types; they need an additional
3099 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003100 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003101 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003102 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003103 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003104 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3105 for (QualType ParamType : FPT->param_types())
3106 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003107 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003108 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003109 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3110 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003111 }
3112
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003113 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003114}
3115
Eric Christophere7b87e52014-10-26 23:40:33 +00003116void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3117 SourceLocation ScopeLoc, QualType FnType,
3118 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003119
3120 StringRef Name;
3121 StringRef LinkageName;
3122
3123 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3124
3125 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003126 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003127
Leny Kholodov80c047d2016-09-06 10:48:04 +00003128 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003129 llvm::DIFile *Unit = getOrCreateFile(Loc);
3130 llvm::DIScope *FDContext = Unit;
3131 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 if (!HasDecl) {
3133 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003134 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003135 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003136 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003137 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003139 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003140 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003141 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003142 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003143 return;
3144 }
3145 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003146 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3147 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003148 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003150 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 } else {
3152 // Use llvm function name.
3153 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003154 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003156 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 Name = Name.substr(1);
3158
Adrian Prantl42d71b92014-04-10 23:21:53 +00003159 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003160 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003161 // Artificial functions should not silently reuse CurLoc.
3162 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003163 }
3164 unsigned LineNo = getLineNumber(Loc);
3165 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003166
Eric Christopher8018e412014-03-27 18:50:35 +00003167 // FIXME: The function declaration we're constructing here is mostly reusing
3168 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3169 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3170 // all subprograms instead of the actual context since subprogram definitions
3171 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003172 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003173 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003174 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003175 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003176 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003177 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003178 // We might get here with a VarDecl in the case we're generating
3179 // code for the initialization of globals. Do not record these decls
3180 // as they will overwrite the actual VarDecl Decl in the cache.
3181 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003182 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003183
Adrian Prantlbebb8932014-03-21 21:01:58 +00003184 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003185 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003186
Guy Benyei11169dd2012-12-18 14:30:41 +00003187 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003188 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003189}
3190
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003191void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3192 QualType FnType) {
3193 StringRef Name;
3194 StringRef LinkageName;
3195
3196 const Decl *D = GD.getDecl();
3197 if (!D)
3198 return;
3199
Leny Kholodov80c047d2016-09-06 10:48:04 +00003200 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003201 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003202 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003203 llvm::DINodeArray TParamsArray;
3204 if (isa<FunctionDecl>(D)) {
3205 // If there is a DISubprogram for this function available then use it.
3206 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3207 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003208 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003209 Name = getObjCMethodName(OMD);
3210 Flags |= llvm::DINode::FlagPrototyped;
3211 } else {
3212 llvm_unreachable("not a function or ObjC method");
3213 }
3214 if (!Name.empty() && Name[0] == '\01')
3215 Name = Name.substr(1);
3216
3217 if (D->isImplicit()) {
3218 Flags |= llvm::DINode::FlagArtificial;
3219 // Artificial functions without a location should not silently reuse CurLoc.
3220 if (Loc.isInvalid())
3221 CurLoc = SourceLocation();
3222 }
3223 unsigned LineNo = getLineNumber(Loc);
3224 unsigned ScopeLine = 0;
3225
Adrian Prantle76bda52016-04-15 15:55:45 +00003226 DBuilder.retainType(DBuilder.createFunction(
3227 FDContext, Name, LinkageName, Unit, LineNo,
3228 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3229 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3230 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003231}
3232
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003233void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3234 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3235 // If there is a subprogram for this function available then use it.
3236 auto FI = SPCache.find(FD->getCanonicalDecl());
3237 llvm::DISubprogram *SP = nullptr;
3238 if (FI != SPCache.end())
3239 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
3240 if (!SP)
3241 SP = getFunctionStub(GD);
3242 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3243 LexicalBlockStack.emplace_back(SP);
3244 setInlinedAt(Builder.getCurrentDebugLocation());
3245 EmitLocation(Builder, FD->getLocation());
3246}
3247
3248void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3249 assert(CurInlinedAt && "unbalanced inline scope stack");
3250 EmitFunctionEnd(Builder);
3251 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3252}
3253
David Blaikie835afb22015-01-21 23:08:17 +00003254void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003255 // Update our current location
3256 setLocation(Loc);
3257
Eric Christophere7b87e52014-10-26 23:40:33 +00003258 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3259 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003260
Adrian Prantle83b1302014-01-07 22:05:52 +00003261 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003262 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003263 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003264}
3265
Guy Benyei11169dd2012-12-18 14:30:41 +00003266void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003267 llvm::MDNode *Back = nullptr;
3268 if (!LexicalBlockStack.empty())
3269 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003270 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003271 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003272 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003273}
3274
Eric Christopher0fdcb312013-05-16 00:52:20 +00003275void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3276 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 // Set our current location.
3278 setLocation(Loc);
3279
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003281 Builder.SetCurrentDebugLocation(
3282 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3283 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003284
Benjamin Kramer8c305922016-02-02 11:06:51 +00003285 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003286 return;
3287
3288 // Create a new lexical block and push it on the stack.
3289 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003290}
3291
Eric Christopher0fdcb312013-05-16 00:52:20 +00003292void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3293 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3295
3296 // Provide an entry in the line table for the end of the block.
3297 EmitLocation(Builder, Loc);
3298
Benjamin Kramer8c305922016-02-02 11:06:51 +00003299 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003300 return;
3301
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 LexicalBlockStack.pop_back();
3303}
3304
Guy Benyei11169dd2012-12-18 14:30:41 +00003305void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3306 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3307 unsigned RCount = FnBeginRegionCount.back();
3308 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3309
3310 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003311 while (LexicalBlockStack.size() != RCount) {
3312 // Provide an entry in the line table for the end of the block.
3313 EmitLocation(Builder, CurLoc);
3314 LexicalBlockStack.pop_back();
3315 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003316 FnBeginRegionCount.pop_back();
3317}
3318
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003319llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003320 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003321
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003322 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003323 QualType FType;
3324 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003325 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003326
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003327 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003328 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003329
3330 FieldOffset = 0;
3331 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3332 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3333 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3334 FType = CGM.getContext().IntTy;
3335 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3336 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3337
3338 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3339 if (HasCopyAndDispose) {
3340 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003341 EltTys.push_back(
3342 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3343 EltTys.push_back(
3344 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003345 }
3346 bool HasByrefExtendedLayout;
3347 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003348 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3349 HasByrefExtendedLayout) &&
3350 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003351 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003352 EltTys.push_back(
3353 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003354 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003355
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3357 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003358 CGM.getTarget().getPointerAlign(0))) {
3359 CharUnits FieldOffsetInBytes =
3360 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003361 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003362 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003363
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 if (NumPaddingBytes.isPositive()) {
3365 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3366 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3367 pad, ArrayType::Normal, 0);
3368 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3369 }
3370 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003371
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003373 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003374 FieldSize = CGM.getContext().getTypeSize(FType);
3375 FieldAlign = CGM.getContext().toBits(Align);
3376
Eric Christopherb2a008c2013-05-16 00:45:12 +00003377 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003378 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003379 FieldAlign, FieldOffset,
3380 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003381 EltTys.push_back(FieldTy);
3382 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003383
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003384 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003385
Leny Kholodov80c047d2016-09-06 10:48:04 +00003386 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003387
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003389 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003390}
3391
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003392void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3393 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003394 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003395 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003397 if (VD->hasAttr<NoDebugAttr>())
3398 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003399
David Blaikie7fceebf2013-08-19 03:37:48 +00003400 bool Unwritten =
3401 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3402 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003403 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003404 if (!Unwritten)
3405 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003406 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 uint64_t XOffset = 0;
3408 if (VD->hasAttr<BlocksAttr>())
3409 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003410 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003411 Ty = getOrCreateType(VD->getType(), Unit);
3412
3413 // If there is no debug info for this type then do not emit debug info
3414 // for this variable.
3415 if (!Ty)
3416 return;
3417
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003419 unsigned Line = 0;
3420 unsigned Column = 0;
3421 if (!Unwritten) {
3422 Line = getLineNumber(VD->getLocation());
3423 Column = getColumnNumber(VD->getLocation());
3424 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003425 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003426 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003427 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003428 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003429
3430 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3431
Guy Benyei11169dd2012-12-18 14:30:41 +00003432 // If this is the first argument and it is implicit then
3433 // give it an object pointer flag.
3434 // FIXME: There has to be a better way to do this, but for static
3435 // functions there won't be an implicit param at arg1 and
3436 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003437 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003439 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003440 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3441 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003442 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003443
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003444 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003445
3446 StringRef Name = VD->getName();
3447 if (!Name.empty()) {
3448 if (VD->hasAttr<BlocksAttr>()) {
3449 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003450 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003451 // offset of __forwarding field
3452 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003453 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003454 Expr.push_back(offset.getQuantity());
3455 Expr.push_back(llvm::dwarf::DW_OP_deref);
3456 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 // offset of x field
3458 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003459 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003460
3461 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003462 auto *D = ArgNo
3463 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3464 *ArgNo, Unit, Line, Ty)
3465 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003466 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003467
Guy Benyei11169dd2012-12-18 14:30:41 +00003468 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003469 DBuilder.insertDeclare(
3470 Storage, D, DBuilder.createExpression(Expr),
3471 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3472 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003473 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003474 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003475 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003476 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003477 // If VD is an anonymous union then Storage represents value for
3478 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003479 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003480 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003481 // GDB has trouble finding local variables in anonymous unions, so we emit
3482 // artifical local variables for each of the members.
3483 //
3484 // FIXME: Remove this code as soon as GDB supports this.
3485 // The debug info verifier in LLVM operates based on the assumption that a
3486 // variable has the same size as its storage and we had to disable the check
3487 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003488 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003489 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003490 StringRef FieldName = Field->getName();
3491
3492 // Ignore unnamed fields. Do not ignore unnamed records.
3493 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3494 continue;
3495
3496 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003497 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003498 auto *D = DBuilder.createAutoVariable(
3499 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003500 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003501
3502 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003503 DBuilder.insertDeclare(
3504 Storage, D, DBuilder.createExpression(Expr),
3505 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3506 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003507 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003508 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003509 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003510
3511 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003512 auto *D = ArgNo
3513 ? DBuilder.createParameterVariable(
3514 Scope, Name, *ArgNo, Unit, Line, Ty,
3515 CGM.getLangOpts().Optimize, Flags)
3516 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3517 CGM.getLangOpts().Optimize, Flags,
3518 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003519
3520 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003521 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003522 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003523 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003524}
3525
3526void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3527 llvm::Value *Storage,
3528 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003529 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003530 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003531}
3532
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003533llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3534 llvm::DIType *Ty) {
3535 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003536 if (CachedTy)
3537 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003538 return DBuilder.createObjectPointerType(Ty);
3539}
3540
Eric Christophere7b87e52014-10-26 23:40:33 +00003541void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3542 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003543 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003544 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003545 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003546
Craig Topper8a13c412014-05-21 05:09:00 +00003547 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003548 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003549 if (VD->hasAttr<NoDebugAttr>())
3550 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003551
Guy Benyei11169dd2012-12-18 14:30:41 +00003552 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003553
Guy Benyei11169dd2012-12-18 14:30:41 +00003554 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003555 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3556 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003557 if (isByRef)
3558 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003559 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 Ty = getOrCreateType(VD->getType(), Unit);
3561
3562 // Self is passed along as an implicit non-arg variable in a
3563 // block. Mark it as the object pointer.
3564 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003565 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003566
3567 // Get location information.
3568 unsigned Line = getLineNumber(VD->getLocation());
3569 unsigned Column = getColumnNumber(VD->getLocation());
3570
3571 const llvm::DataLayout &target = CGM.getDataLayout();
3572
3573 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003574 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003575 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3576
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003577 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003578 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003579 addr.push_back(llvm::dwarf::DW_OP_deref);
3580 addr.push_back(llvm::dwarf::DW_OP_plus);
3581 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003582 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003583 addr.push_back(llvm::dwarf::DW_OP_deref);
3584 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003585 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003586 offset =
3587 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003588 addr.push_back(offset.getQuantity());
3589 addr.push_back(llvm::dwarf::DW_OP_deref);
3590 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 // offset of x field
3592 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003593 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003594 }
3595
3596 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003597 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003598 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003599 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003600 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003601
Guy Benyei11169dd2012-12-18 14:30:41 +00003602 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003603 auto DL =
3604 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003605 if (InsertPoint)
3606 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3607 InsertPoint);
3608 else
3609 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3610 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003611}
3612
Guy Benyei11169dd2012-12-18 14:30:41 +00003613void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3614 unsigned ArgNo,
3615 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003616 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003617 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003618}
3619
3620namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003621struct BlockLayoutChunk {
3622 uint64_t OffsetInBits;
3623 const BlockDecl::Capture *Capture;
3624};
3625bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3626 return l.OffsetInBits < r.OffsetInBits;
3627}
Guy Benyei11169dd2012-12-18 14:30:41 +00003628}
3629
3630void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003631 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003632 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003633 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003634 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003635 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003636 ASTContext &C = CGM.getContext();
3637 const BlockDecl *blockDecl = block.getBlockDecl();
3638
3639 // Collect some general information about the block's location.
3640 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003641 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003642 unsigned line = getLineNumber(loc);
3643 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003644
Guy Benyei11169dd2012-12-18 14:30:41 +00003645 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003646 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003647
3648 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003649 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003650
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003651 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003652 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003653 blockLayout->getElementOffsetInBits(0),
3654 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003655 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003656 blockLayout->getElementOffsetInBits(1),
3657 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003658 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003659 blockLayout->getElementOffsetInBits(2),
3660 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003661 auto *FnTy = block.getBlockExpr()->getFunctionType();
3662 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003663 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003664 blockLayout->getElementOffsetInBits(3),
3665 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003666 fields.push_back(createFieldType(
3667 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3668 ? C.getBlockDescriptorExtendedType()
3669 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003670 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003671
3672 // We want to sort the captures by offset, not because DWARF
3673 // requires this, but because we're paranoid about debuggers.
3674 SmallVector<BlockLayoutChunk, 8> chunks;
3675
3676 // 'this' capture.
3677 if (blockDecl->capturesCXXThis()) {
3678 BlockLayoutChunk chunk;
3679 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003680 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003681 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003682 chunks.push_back(chunk);
3683 }
3684
3685 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003686 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003687 const VarDecl *variable = capture.getVariable();
3688 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3689
3690 // Ignore constant captures.
3691 if (captureInfo.isConstant())
3692 continue;
3693
3694 BlockLayoutChunk chunk;
3695 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003696 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003697 chunk.Capture = &capture;
3698 chunks.push_back(chunk);
3699 }
3700
3701 // Sort by offset.
3702 llvm::array_pod_sort(chunks.begin(), chunks.end());
3703
David Majnemer58ed0f32016-07-17 00:39:12 +00003704 for (const BlockLayoutChunk &Chunk : chunks) {
3705 uint64_t offsetInBits = Chunk.OffsetInBits;
3706 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003707
3708 // If we have a null capture, this must be the C++ 'this' capture.
3709 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003710 QualType type;
3711 if (auto *Method =
3712 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3713 type = Method->getThisType(C);
3714 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3715 type = QualType(RDecl->getTypeForDecl(), 0);
3716 else
3717 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003718
David Majnemerb4b671e2016-06-30 03:01:59 +00003719 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003720 offsetInBits, tunit, tunit));
3721 continue;
3722 }
3723
3724 const VarDecl *variable = capture->getVariable();
3725 StringRef name = variable->getName();
3726
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003727 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003728 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003729 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003730 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003731
3732 // FIXME: this creates a second copy of this type!
3733 uint64_t xoffset;
3734 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003735 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003736 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3737 PtrInfo.Width, Align, offsetInBits,
3738 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003739 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003740 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003741 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003742 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003743 }
3744 fields.push_back(fieldType);
3745 }
3746
3747 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003748 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3749 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003750
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003751 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003752
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003753 llvm::DIType *type =
3754 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003755 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003756 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003757 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3758
3759 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003760 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003761 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003762
3763 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003764 auto *debugVar = DBuilder.createParameterVariable(
3765 scope, Arg->getName(), ArgNo, tunit, line, type,
3766 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003767
Adrian Prantl616bef42013-03-14 21:52:59 +00003768 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003769 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003770 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003771 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003772 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3773 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003774 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003775
Adrian Prantl616bef42013-03-14 21:52:59 +00003776 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003777 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003778 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003779 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003780}
3781
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003782llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003783CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3784 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003785 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003786
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003787 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003788 if (MI != StaticDataMemberCache.end()) {
3789 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003790 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003791 }
David Blaikiece763042013-08-20 21:49:21 +00003792
3793 // If the member wasn't found in the cache, lazily construct and add it to the
3794 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003795 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003796 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003797 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003798}
3799
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003800llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003801 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3802 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003803 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003804
3805 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003806 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003807 StringRef FieldName = Field->getName();
3808
3809 // Ignore unnamed fields, but recurse into anonymous records.
3810 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003811 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003812 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003813 Var, DContext);
3814 continue;
3815 }
3816 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003817 GVE = DBuilder.createGlobalVariableExpression(
3818 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3819 Var->hasLocalLinkage());
3820 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003821 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003822 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003823}
3824
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003825void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003826 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003827 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003828 if (D->hasAttr<NoDebugAttr>())
3829 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003830
3831 // If we already created a DIGlobalVariable for this declaration, just attach
3832 // it to the llvm::GlobalVariable.
3833 auto Cached = DeclCache.find(D->getCanonicalDecl());
3834 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003835 return Var->addDebugInfo(
3836 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003837
Guy Benyei11169dd2012-12-18 14:30:41 +00003838 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003839 llvm::DIFile *Unit = nullptr;
3840 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003841 unsigned LineNo;
3842 StringRef DeclName, LinkageName;
3843 QualType T;
3844 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003845
3846 // Attempt to store one global variable for the declaration - even if we
3847 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003848 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003849
3850 // If this is an anonymous union then we'll want to emit a global
3851 // variable for each member of the anonymous union so that it's possible
3852 // to find the name of any field in the union.
3853 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003854 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003855 assert(RD->isAnonymousStructOrUnion() &&
3856 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003857 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003858 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003859 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003860 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003861 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003862 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003863 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003864 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003865 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003866 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003867}
3868
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003869void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003870 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003871 if (VD->hasAttr<NoDebugAttr>())
3872 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003873 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003874 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003875 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003876 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003877 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003878 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3879 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003880 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3881 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3882 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003883 // Do not use global variables for enums.
3884 //
3885 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003886 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003887 return;
David Blaikiea15565562014-04-04 20:56:17 +00003888 // Do not emit separate definitions for function local const/statics.
3889 if (isa<FunctionDecl>(VD->getDeclContext()))
3890 return;
David Blaikiebb113912014-04-05 07:23:17 +00003891 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003892 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003893 if (VarD->isStaticDataMember()) {
3894 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003895 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003896 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003897 //
3898 // FIXME: This is probably unnecessary, since Ty should reference RD
3899 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003900 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003901 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003902 return;
3903 }
3904
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003905 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003906
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003907 auto &GV = DeclCache[VD];
3908 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003909 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003910 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003911 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3912 // FIXME: Add a representation for integer constants wider than 64 bits.
3913 if (Init.isInt())
3914 InitExpr =
3915 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3916 else if (Init.isFloat())
3917 InitExpr = DBuilder.createConstantValueExpression(
3918 Init.getFloat().bitcastToAPInt().getZExtValue());
3919 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003920 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003921 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003922 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3923 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003924}
3925
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003926llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003927 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003928 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003929 llvm::DIScope *Mod = getParentModuleOrNull(D);
3930 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003931}
3932
David Blaikie9f88fe82013-04-22 06:13:21 +00003933void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003934 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003935 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003936 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003937 if (!NSDecl->isAnonymousNamespace() ||
3938 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003939 DBuilder.createImportedModule(
3940 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3941 getOrCreateNameSpace(NSDecl),
3942 getLineNumber(UD.getLocation()));
3943 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003944}
3945
David Blaikiebd483762013-05-20 04:58:53 +00003946void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003947 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003948 return;
3949 assert(UD.shadow_size() &&
3950 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3951 // Emitting one decl is sufficient - debuggers can detect that this is an
3952 // overloaded name & provide lookup for all the overloads.
3953 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003954
3955 // FIXME: Skip functions with undeduced auto return type for now since we
3956 // don't currently have the plumbing for separate declarations & definitions
3957 // of free functions and mismatched types (auto in the declaration, concrete
3958 // return type in the definition)
3959 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3960 if (const auto *AT =
3961 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3962 if (AT->getDeducedType().isNull())
3963 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003964 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003965 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003966 DBuilder.createImportedDeclaration(
3967 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3968 getLineNumber(USD.getLocation()));
3969}
3970
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003971void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003972 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3973 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003974 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003975 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003976 DBuilder.createImportedDeclaration(
3977 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3978 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3979 getLineNumber(ID.getLocation()));
3980 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003981}
3982
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003983llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003984CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003985 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003986 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003987 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003988 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003989 return cast<llvm::DIImportedEntity>(VH);
3990 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003991 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003992 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3993 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003994 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003995 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3996 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3997 NA.getName());
3998 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003999 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004000 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
4001 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
4002 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004003 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004004 return R;
4005}
4006
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004007llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00004008CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00004009 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004010 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00004011 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004012 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004013
Guy Benyei11169dd2012-12-18 14:30:41 +00004014 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004015 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004016 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00004017 llvm::DINamespace *NS = DBuilder.createNameSpace(
4018 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004019 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004020 return NS;
4021}
4022
Adrian Prantla5206ce2015-09-22 23:26:43 +00004023void CGDebugInfo::setDwoId(uint64_t Signature) {
4024 assert(TheCU && "no main compile unit");
4025 TheCU->setDWOId(Signature);
4026}
4027
4028
Guy Benyei11169dd2012-12-18 14:30:41 +00004029void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004030 // Creating types might create further types - invalidating the current
4031 // element and the size(), so don't cache/reference them.
4032 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4033 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004034 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004035 ? CreateTypeDefinition(E.Type, E.Unit)
4036 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004037 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004038 }
4039
David Blaikief427b002014-05-06 03:42:01 +00004040 for (auto p : ReplaceMap) {
4041 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004042 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004043 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004044
David Blaikief427b002014-05-06 03:42:01 +00004045 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004046 assert(it != TypeCache.end());
4047 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004048
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004049 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4050 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004051 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004052
Frederic Rissd253ed62014-11-18 03:40:51 +00004053 for (const auto &p : FwdDeclReplaceMap) {
4054 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004055 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004056 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004057
4058 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004059 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004060 // with ourselves, that will destroy the temporary MDNode and
4061 // replace it with a standard one, avoiding leaking memory.
4062 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004063 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004064 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004065 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004066
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004067 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4068 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004069 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004070 }
4071
Adrian Prantl73409ce2013-03-11 18:33:46 +00004072 // We keep our own list of retained types, because we need to look
4073 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004074 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004075 if (auto MD = TypeCache[RT])
4076 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004077
Guy Benyei11169dd2012-12-18 14:30:41 +00004078 DBuilder.finalize();
4079}
David Blaikie66088d52014-09-24 17:01:27 +00004080
4081void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004082 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004083 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004084
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004085 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004086 // Don't ignore in case of explicit cast where it is referenced indirectly.
4087 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004088}
Amara Emerson652795d2016-11-10 14:44:30 +00004089
4090llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4091 if (LexicalBlockStack.empty())
4092 return llvm::DebugLoc();
4093
4094 llvm::MDNode *Scope = LexicalBlockStack.back();
4095 return llvm::DebugLoc::get(
4096 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4097}