blob: d15ae06b455beceeb9259011ba4d9e08171704fa [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.
Adrian Prantld8870552017-05-11 22:59:19 +0000211 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context)) {
212 auto *ParentModule = dyn_cast<llvm::DIModule>(Default);
213 return getOrCreateNamespace(NSDecl, ParentModule);
214 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000215
David Majnemer58ed0f32016-07-17 00:39:12 +0000216 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000217 if (!RDecl->isDependentType())
218 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000219 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000220 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000221}
222
Guy Benyei11169dd2012-12-18 14:30:41 +0000223StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000225 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000226 FunctionTemplateSpecializationInfo *Info =
227 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000228
Reid Kleckner31abd802016-06-30 17:41:31 +0000229 // Emit the unqualified name in normal operation. LLVM and the debugger can
230 // compute the fully qualified name from the scope chain. If we're only
231 // emitting line table info, there won't be any scope chains, so emit the
232 // fully qualified name here so that stack traces are more accurate.
233 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
234 // evaluating the size impact.
235 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
236 CGM.getCodeGenOpts().EmitCodeView;
237
238 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000239 return FII->getName();
240
Benjamin Kramer9170e912013-02-22 15:46:01 +0000241 SmallString<128> NS;
242 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000243 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000244 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000245 if (!UseQualifiedName)
246 FD->printName(OS);
247 else
248 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000249
Reid Kleckner829398e2016-06-17 16:11:20 +0000250 // Add any template specialization args.
251 if (Info) {
252 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000253 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000254 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000255 }
256
257 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000258 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259}
260
261StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
262 SmallString<256> MethodName;
263 llvm::raw_svector_ostream OS(MethodName);
264 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
265 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000266 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000267 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000268 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000269 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000270 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000271 if (OC->IsClassExtension()) {
272 OS << OC->getClassInterface()->getName();
273 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000274 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000275 << OC->getIdentifier()->getNameStart() << ')';
276 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000277 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000278 OS << OCD->getClassInterface()->getName() << '('
279 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000280 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000281 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000282 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000283 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000284 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000285 ClassTy.print(OS, PrintingPolicy(LangOptions()));
286 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000287 }
288 OS << ' ' << OMD->getSelector().getAsString() << ']';
289
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000290 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000291}
292
Guy Benyei11169dd2012-12-18 14:30:41 +0000293StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000294 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000295}
296
Eric Christophere7b87e52014-10-26 23:40:33 +0000297StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000298 if (isa<ClassTemplateSpecializationDecl>(RD)) {
299 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000300 llvm::raw_svector_ostream OS(Name);
301 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
302 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000303
304 // Copy this name on the side and use its reference.
305 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000306 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000307
David Majnemerbc5976a2016-07-01 23:12:54 +0000308 // quick optimization to avoid having to intern strings that are already
309 // stored reliably elsewhere
310 if (const IdentifierInfo *II = RD->getIdentifier())
311 return II->getName();
312
313 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
314 // used to reconstruct the fully qualified type names.
315 if (CGM.getCodeGenOpts().EmitCodeView) {
316 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
317 assert(RD->getDeclContext() == D->getDeclContext() &&
318 "Typedef should not be in another decl context!");
319 assert(D->getDeclName().getAsIdentifierInfo() &&
320 "Typedef was not named!");
321 return D->getDeclName().getAsIdentifierInfo()->getName();
322 }
323
324 if (CGM.getLangOpts().CPlusPlus) {
325 StringRef Name;
326
327 ASTContext &Context = CGM.getContext();
328 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
329 // Anonymous types without a name for linkage purposes have their
330 // declarator mangled in if they have one.
331 Name = DD->getName();
332 else if (const TypedefNameDecl *TND =
333 Context.getTypedefNameForUnnamedTagDecl(RD))
334 // Anonymous types without a name for linkage purposes have their
335 // associate typedef mangled in if they have one.
336 Name = TND->getName();
337
338 if (!Name.empty()) {
339 SmallString<256> UnnamedType("<unnamed-type-");
340 UnnamedType += Name;
341 UnnamedType += '>';
342 return internString(UnnamedType);
343 }
344 }
345 }
346
347 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000348}
349
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000350llvm::DIFile::ChecksumKind
351CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
352 Checksum.clear();
353
354 if (!CGM.getCodeGenOpts().EmitCodeView)
355 return llvm::DIFile::CSK_None;
356
357 SourceManager &SM = CGM.getContext().getSourceManager();
358 bool Invalid;
359 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
360 if (Invalid)
361 return llvm::DIFile::CSK_None;
362
363 llvm::MD5 Hash;
364 llvm::MD5::MD5Result Result;
365
366 Hash.update(MemBuffer->getBuffer());
367 Hash.final(Result);
368
369 Hash.stringifyResult(Result, Checksum);
370 return llvm::DIFile::CSK_MD5;
371}
372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000373llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000374 if (!Loc.isValid())
375 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000376 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000377 remapDIPath(TheCU->getDirectory()),
378 TheCU->getFile()->getChecksumKind(),
379 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000380
381 SourceManager &SM = CGM.getContext().getSourceManager();
382 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
383
384 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
385 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000386 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000387 remapDIPath(TheCU->getDirectory()),
388 TheCU->getFile()->getChecksumKind(),
389 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000390
391 // Cache the results.
392 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000393 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000394
395 if (it != DIFileCache.end()) {
396 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000397 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000398 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000399 }
400
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000401 SmallString<32> Checksum;
402 llvm::DIFile::ChecksumKind CSKind =
403 computeChecksum(SM.getFileID(Loc), Checksum);
404
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000405 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000406 remapDIPath(getCurrentDirname()),
407 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000409 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 return F;
411}
412
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000413llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000414 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000415 remapDIPath(TheCU->getDirectory()),
416 TheCU->getFile()->getChecksumKind(),
417 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000418}
419
420std::string CGDebugInfo::remapDIPath(StringRef Path) const {
421 for (const auto &Entry : DebugPrefixMap)
422 if (Path.startswith(Entry.first))
423 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
424 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000425}
426
Guy Benyei11169dd2012-12-18 14:30:41 +0000427unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
428 if (Loc.isInvalid() && CurLoc.isInvalid())
429 return 0;
430 SourceManager &SM = CGM.getContext().getSourceManager();
431 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000432 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000433}
434
Adrian Prantlc7822422013-03-12 20:43:25 +0000435unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000437 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 return 0;
439
440 // If the location is invalid then use the current column.
441 if (Loc.isInvalid() && CurLoc.isInvalid())
442 return 0;
443 SourceManager &SM = CGM.getContext().getSourceManager();
444 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000445 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000446}
447
448StringRef CGDebugInfo::getCurrentDirname() {
449 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
450 return CGM.getCodeGenOpts().DebugCompilationDir;
451
452 if (!CWDName.empty())
453 return CWDName;
454 SmallString<256> CWD;
455 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000456 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000457}
458
Guy Benyei11169dd2012-12-18 14:30:41 +0000459void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000460 SmallString<32> Checksum;
461 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462
David Blaikieaabde052014-05-14 00:29:00 +0000463 // Should we be asking the SourceManager for the main file name, instead of
464 // accepting it as an argument? This just causes the main file name to
465 // mismatch with source locations and create extra lexical scopes or
466 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
467 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
468 // because that's what the SourceManager says)
469
Guy Benyei11169dd2012-12-18 14:30:41 +0000470 // Get absolute path name.
471 SourceManager &SM = CGM.getContext().getSourceManager();
472 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
473 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000474 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000475
476 // The main file name provided via the "-main-file-name" option contains just
477 // the file name itself with no path information. This file name may have had
478 // a relative path, so we look into the actual file entry for the main
479 // file to determine the real absolute path for the file.
480 std::string MainFileDir;
481 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000482 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000483 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000484 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
485 llvm::sys::path::append(MainFileDirSS, MainFileName);
486 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000487 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000488 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000489 }
490
Ed Masteda706022014-05-07 12:49:30 +0000491 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000492 const LangOptions &LO = CGM.getLangOpts();
493 if (LO.CPlusPlus) {
494 if (LO.ObjC1)
495 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
496 else
497 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
498 } else if (LO.ObjC1) {
499 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000500 } else if (LO.RenderScript) {
501 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 } else if (LO.C99) {
503 LangTag = llvm::dwarf::DW_LANG_C99;
504 } else {
505 LangTag = llvm::dwarf::DW_LANG_C89;
506 }
507
508 std::string Producer = getClangFullVersion();
509
510 // Figure out which version of the ObjC runtime we have.
511 unsigned RuntimeVers = 0;
512 if (LO.ObjC1)
513 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
514
Adrian Prantl826824e2016-04-08 22:43:06 +0000515 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
516 switch (DebugKind) {
517 case codegenoptions::NoDebugInfo:
518 case codegenoptions::LocTrackingOnly:
519 EmissionKind = llvm::DICompileUnit::NoDebug;
520 break;
521 case codegenoptions::DebugLineTablesOnly:
522 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
523 break;
524 case codegenoptions::LimitedDebugInfo:
525 case codegenoptions::FullDebugInfo:
526 EmissionKind = llvm::DICompileUnit::FullDebug;
527 break;
528 }
529
Guy Benyei11169dd2012-12-18 14:30:41 +0000530 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000531 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000532 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000533 LangTag,
534 DBuilder.createFile(remapDIPath(MainFileName),
535 remapDIPath(getCurrentDirname()), CSKind, Checksum),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000536 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikie81503552017-04-21 23:35:36 +0000537 CGM.getCodeGenOpts().EnableSplitDwarf
538 ? ""
539 : CGM.getCodeGenOpts().SplitDwarfFile,
540 EmissionKind, 0 /* DWOid */, CGM.getCodeGenOpts().SplitDwarfInlining,
Dehao Chen5a3f8902017-02-01 22:45:21 +0000541 CGM.getCodeGenOpts().DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000542}
543
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000544llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000545 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000546 StringRef BTName;
547 switch (BT->getKind()) {
548#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000549#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000550#include "clang/AST/BuiltinTypes.def"
551 case BuiltinType::Dependent:
552 llvm_unreachable("Unexpected builtin type");
553 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000554 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000556 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000557 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000558 if (!ClassTy)
559 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
560 "objc_class", TheCU,
561 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 return ClassTy;
563 case BuiltinType::ObjCId: {
564 // typedef struct objc_class *Class;
565 // typedef struct objc_object {
566 // Class isa;
567 // } *id;
568
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000569 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000570 return ObjTy;
571
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000572 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
574 "objc_class", TheCU,
575 getOrCreateMainFile(), 0);
576
577 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000578
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000579 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000580
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000581 ObjTy = DBuilder.createStructType(
582 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
583 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000584
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000585 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000586 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
587 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
588 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 return ObjTy;
590 }
591 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000592 if (!SelTy)
593 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
594 "objc_selector", TheCU,
595 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000596 return SelTy;
597 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000598
Alexey Bader954ba212016-04-08 13:40:33 +0000599#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
600 case BuiltinType::Id: \
601 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
602 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000603#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000604 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000605 return getOrCreateStructPtrType("opencl_sampler_t",
606 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000607 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000608 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000609 case BuiltinType::OCLClkEvent:
610 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
611 case BuiltinType::OCLQueue:
612 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000613 case BuiltinType::OCLReserveID:
614 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000615
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000617 case BuiltinType::Char_U:
618 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
619 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000621 case BuiltinType::SChar:
622 Encoding = llvm::dwarf::DW_ATE_signed_char;
623 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000624 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000625 case BuiltinType::Char32:
626 Encoding = llvm::dwarf::DW_ATE_UTF;
627 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000628 case BuiltinType::UShort:
629 case BuiltinType::UInt:
630 case BuiltinType::UInt128:
631 case BuiltinType::ULong:
632 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000633 case BuiltinType::ULongLong:
634 Encoding = llvm::dwarf::DW_ATE_unsigned;
635 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 case BuiltinType::Short:
637 case BuiltinType::Int:
638 case BuiltinType::Int128:
639 case BuiltinType::Long:
640 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000641 case BuiltinType::LongLong:
642 Encoding = llvm::dwarf::DW_ATE_signed;
643 break;
644 case BuiltinType::Bool:
645 Encoding = llvm::dwarf::DW_ATE_boolean;
646 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000647 case BuiltinType::Half:
648 case BuiltinType::Float:
649 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000650 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000651 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000652 // FIXME: For targets where long double and __float128 have the same size,
653 // they are currently indistinguishable in the debugger without some
654 // special treatment. However, there is currently no consensus on encoding
655 // and this should be updated once a DWARF encoding exists for distinct
656 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000657 Encoding = llvm::dwarf::DW_ATE_float;
658 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000659 }
660
661 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000662 case BuiltinType::Long:
663 BTName = "long int";
664 break;
665 case BuiltinType::LongLong:
666 BTName = "long long int";
667 break;
668 case BuiltinType::ULong:
669 BTName = "long unsigned int";
670 break;
671 case BuiltinType::ULongLong:
672 BTName = "long long unsigned int";
673 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 default:
675 BTName = BT->getName(CGM.getLangOpts());
676 break;
677 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000678 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000680 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000681}
682
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000683llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000684 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000685 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000686 if (Ty->isComplexIntegerType())
687 Encoding = llvm::dwarf::DW_ATE_lo_user;
688
689 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000690 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000691}
692
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000693llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
694 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 QualifierCollector Qc;
696 const Type *T = Qc.strip(Ty);
697
698 // Ignore these qualifiers for now.
699 Qc.removeObjCGCAttr();
700 Qc.removeAddressSpace();
701 Qc.removeObjCLifetime();
702
703 // We will create one Derived type for one qualifier and recurse to handle any
704 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000705 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 if (Qc.hasConst()) {
707 Tag = llvm::dwarf::DW_TAG_const_type;
708 Qc.removeConst();
709 } else if (Qc.hasVolatile()) {
710 Tag = llvm::dwarf::DW_TAG_volatile_type;
711 Qc.removeVolatile();
712 } else if (Qc.hasRestrict()) {
713 Tag = llvm::dwarf::DW_TAG_restrict_type;
714 Qc.removeRestrict();
715 } else {
716 assert(Qc.empty() && "Unknown type qualifier for debug info");
717 return getOrCreateType(QualType(T, 0), Unit);
718 }
719
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000720 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721
722 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
723 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000724 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000725}
726
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000727llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
728 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000729
730 // The frontend treats 'id' as a typedef to an ObjCObjectType,
731 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
732 // debug info, we want to emit 'id' in both cases.
733 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000734 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000735
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000736 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
737 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000738}
739
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000740llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
741 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000742 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000743 Ty->getPointeeType(), Unit);
744}
745
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000746/// \return whether a C++ mangling exists for the type defined by TD.
747static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
748 switch (TheCU->getSourceLanguage()) {
749 case llvm::dwarf::DW_LANG_C_plus_plus:
750 return true;
751 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
752 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
753 default:
754 return false;
755 }
756}
757
Manman Rene0064d82013-08-29 23:19:58 +0000758/// In C++ mode, types have linkage, so we can rely on the ODR and
759/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000760static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
761 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000762 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000763 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000764 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000765
766 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000767 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000768
Manman Rene0064d82013-08-29 23:19:58 +0000769 // TODO: This is using the RTTI name. Is there a better way to get
770 // a unique string for a type?
771 llvm::raw_svector_ostream Out(FullName);
772 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000773 return FullName;
774}
775
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000776/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000777static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
778 llvm::dwarf::Tag Tag;
779 if (RD->isStruct() || RD->isInterface())
780 Tag = llvm::dwarf::DW_TAG_structure_type;
781 else if (RD->isUnion())
782 Tag = llvm::dwarf::DW_TAG_union_type;
783 else {
784 // FIXME: This could be a struct type giving a default visibility different
785 // than C++ class type, but needs llvm metadata changes first.
786 assert(RD->isClass());
787 Tag = llvm::dwarf::DW_TAG_class_type;
788 }
789 return Tag;
790}
791
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000793CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000794 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000795 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000796 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
797 return cast<llvm::DICompositeType>(T);
798 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000799 unsigned Line = getLineNumber(RD->getLocation());
800 StringRef RDName = getClassName(RD);
801
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000802 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000803 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000804
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000806 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000807 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000808 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000809 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000810 ReplaceMap.emplace_back(
811 std::piecewise_construct, std::make_tuple(Ty),
812 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000813 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000814}
815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000817 const Type *Ty,
818 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000819 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000820 // Bit size, align and offset of the type.
821 // Size is always the size of a pointer. We can't use getTypeSize here
822 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000823 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
824 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000825 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000826 Optional<unsigned> DWARFAddressSpace =
827 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000828
Keno Fischer87842f32015-11-16 09:04:13 +0000829 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
830 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
831 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000832 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000833 else
834 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000835 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000836}
837
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000838llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
839 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000840 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000841 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000842 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
843 TheCU, getOrCreateMainFile(), 0);
844 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
845 Cache = DBuilder.createPointerType(Cache, Size);
846 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000847}
848
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000849llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
850 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000851 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 QualType FType;
853 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000854 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000855 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856
857 FieldOffset = 0;
858 FType = CGM.getContext().UnsignedLongTy;
859 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
860 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
861
862 Elements = DBuilder.getOrCreateArray(EltTys);
863 EltTys.clear();
864
Leny Kholodov80c047d2016-09-06 10:48:04 +0000865 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000866 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000867
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000868 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000869 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000870 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000871
872 // Bit size, align and offset of the type.
873 uint64_t Size = CGM.getContext().getTypeSize(Ty);
874
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000875 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000876
877 FieldOffset = 0;
878 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
879 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
880 FType = CGM.getContext().IntTy;
881 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
882 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000883 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000884 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
885
886 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000887 FieldSize = CGM.getContext().getTypeSize(Ty);
888 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000889 EltTys.push_back(DBuilder.createMemberType(
890 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
891 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000892
893 FieldOffset += FieldSize;
894 Elements = DBuilder.getOrCreateArray(EltTys);
895
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000896 // The __block_literal_generic structs are marked with a special
897 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
898 // the debugger needs to know about. To allow type uniquing, emit
899 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000900 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000901 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000902 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000903
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000904 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000905}
906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000907llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
908 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000909 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000910 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000911
912 SmallString<128> NS;
913 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000914 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
915 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000916
917 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000918 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000919 CGM.getContext().getPrintingPolicy());
920
David Majnemer58ed0f32016-07-17 00:39:12 +0000921 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000922 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000923
924 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000925 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
926 getLineNumber(Loc),
927 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000928}
929
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000930llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
931 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000932 // We don't set size information, but do specify where the typedef was
933 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000934 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000935
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000936 // Typedefs are derived from some other type.
937 return DBuilder.createTypedef(
938 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
939 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000940 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000941}
942
Reid Klecknerf00f8032016-06-08 20:41:54 +0000943static unsigned getDwarfCC(CallingConv CC) {
944 switch (CC) {
945 case CC_C:
946 // Avoid emitting DW_AT_calling_convention if the C convention was used.
947 return 0;
948
949 case CC_X86StdCall:
950 return llvm::dwarf::DW_CC_BORLAND_stdcall;
951 case CC_X86FastCall:
952 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
953 case CC_X86ThisCall:
954 return llvm::dwarf::DW_CC_BORLAND_thiscall;
955 case CC_X86VectorCall:
956 return llvm::dwarf::DW_CC_LLVM_vectorcall;
957 case CC_X86Pascal:
958 return llvm::dwarf::DW_CC_BORLAND_pascal;
959
960 // FIXME: Create new DW_CC_ codes for these calling conventions.
961 case CC_X86_64Win64:
962 case CC_X86_64SysV:
963 case CC_AAPCS:
964 case CC_AAPCS_VFP:
965 case CC_IntelOclBicc:
966 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000967 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000968 case CC_Swift:
969 case CC_PreserveMost:
970 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000971 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000972 return 0;
973 }
974 return 0;
975}
976
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000977llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
978 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000979 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000980
981 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000982 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000983
984 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000985 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000986 if (isa<FunctionNoProtoType>(Ty))
987 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000988 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
989 for (const QualType &ParamType : FPT->param_types())
990 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000991 if (FPT->isVariadic())
992 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000993 }
994
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000995 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000996 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000997 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000998}
999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001001/// As an optimization, return 0 if the access specifier equals the
1002/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001003static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1004 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001005 AccessSpecifier Default = clang::AS_none;
1006 if (RD && RD->isClass())
1007 Default = clang::AS_private;
1008 else if (RD && (RD->isStruct() || RD->isUnion()))
1009 Default = clang::AS_public;
1010
1011 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001012 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001013
Eric Christophere7b87e52014-10-26 23:40:33 +00001014 switch (Access) {
1015 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001016 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001017 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001018 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001019 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001020 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001021 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001022 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001023 }
1024 llvm_unreachable("unexpected access enumerator");
1025}
Guy Benyei11169dd2012-12-18 14:30:41 +00001026
David Majnemerb4b671e2016-06-30 03:01:59 +00001027llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1028 llvm::DIScope *RecordTy,
1029 const RecordDecl *RD) {
1030 StringRef Name = BitFieldDecl->getName();
1031 QualType Ty = BitFieldDecl->getType();
1032 SourceLocation Loc = BitFieldDecl->getLocation();
1033 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1034 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1035
1036 // Get the location for the field.
1037 llvm::DIFile *File = getOrCreateFile(Loc);
1038 unsigned Line = getLineNumber(Loc);
1039
1040 const CGBitFieldInfo &BitFieldInfo =
1041 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1042 uint64_t SizeInBits = BitFieldInfo.Size;
1043 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001044 uint64_t StorageOffsetInBits =
1045 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1046 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001047 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001048 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001049 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1050 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001051}
1052
1053llvm::DIType *
1054CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1055 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001056 uint32_t AlignInBits, llvm::DIFile *tunit,
1057 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001058 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001059
1060 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001061 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 unsigned line = getLineNumber(loc);
1063
David Majnemer34b57492014-07-30 01:30:47 +00001064 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001065 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001066 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001067 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1068 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001069 if (!Align)
1070 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001071 }
1072
Leny Kholodov80c047d2016-09-06 10:48:04 +00001073 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001074 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001075 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076}
1077
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001078void CGDebugInfo::CollectRecordLambdaFields(
1079 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001081 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1082 // has the name and the location of the variable so we should iterate over
1083 // both concurrently.
1084 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1085 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1086 unsigned fieldno = 0;
1087 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001088 E = CXXDecl->captures_end();
1089 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001090 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001091 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001092 SourceLocation Loc = C.getLocation();
1093 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001094 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001095 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001096 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001097 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001098 llvm::DIType *FieldType = createFieldType(
1099 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001100 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001101 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001102 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001103 // TODO: Need to handle 'this' in some way by probably renaming the
1104 // this of the lambda class and having a field member of 'this' or
1105 // by using AT_object_pointer for the function and having that be
1106 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001107 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001108 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001109 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001110 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001111 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001112 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001113
1114 elements.push_back(fieldType);
1115 }
1116 }
1117}
1118
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119llvm::DIDerivedType *
1120CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001121 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001122 // Create the descriptor for the static variable, with or without
1123 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001124 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1126 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001127
Eric Christopher91a31902013-01-16 01:22:32 +00001128 unsigned LineNumber = getLineNumber(Var->getLocation());
1129 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001130 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001131 if (Var->getInit()) {
1132 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001133 if (Value) {
1134 if (Value->isInt())
1135 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1136 if (Value->isFloat())
1137 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1138 }
Eric Christopher91a31902013-01-16 01:22:32 +00001139 }
1140
Leny Kholodov80c047d2016-09-06 10:48:04 +00001141 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001142 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001143 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001144 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001145 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001146 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001147}
1148
Eric Christophere7b87e52014-10-26 23:40:33 +00001149void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1151 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001152 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001153 StringRef name = field->getName();
1154 QualType type = field->getType();
1155
1156 // Ignore unnamed fields unless they're anonymous structs/unions.
1157 if (name.empty() && !type->isRecordType())
1158 return;
1159
David Majnemerb4b671e2016-06-30 03:01:59 +00001160 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001161 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001162 FieldType = createBitFieldType(field, RecordTy, RD);
1163 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001164 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001165 FieldType =
1166 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001167 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001168 }
1169
David Majnemerb4b671e2016-06-30 03:01:59 +00001170 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001171}
1172
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001173void CGDebugInfo::CollectRecordNestedRecord(
1174 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1175 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001176 // Injected class names are not considered nested records.
1177 if (isa<InjectedClassNameType>(Ty))
1178 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001179 SourceLocation Loc = RD->getLocation();
1180 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1181 elements.push_back(nestedType);
1182}
1183
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001184void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001185 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001186 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001188 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001189
Eric Christopher91a31902013-01-16 01:22:32 +00001190 if (CXXDecl && CXXDecl->isLambda())
1191 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1192 else {
1193 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001194
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001195 // Debug info for nested records is included in the member list only for
1196 // CodeView.
1197 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1198
Eric Christopher91a31902013-01-16 01:22:32 +00001199 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001200 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001201
Eric Christopher91a31902013-01-16 01:22:32 +00001202 // Static and non-static members should appear in the same order as
1203 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001204 for (const auto *I : record->decls())
1205 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001206 if (V->hasAttr<NoDebugAttr>())
1207 continue;
David Blaikiece763042013-08-20 21:49:21 +00001208 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001209 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001210 if (MI != StaticDataMemberCache.end()) {
1211 assert(MI->second &&
1212 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001213 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001214 } else {
1215 auto Field = CreateRecordStaticField(V, RecordTy, record);
1216 elements.push_back(Field);
1217 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001218 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001219 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1220 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001221
1222 // Bump field number for next field.
1223 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001224 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1225 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1226 nestedRec->getDeclContext() == record)
1227 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001228 }
1229}
1230
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001231llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001232CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001233 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001234 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001235 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001237 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001238 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1239 Func, Unit);
1240}
David Blaikie2aaf0652013-01-07 22:24:59 +00001241
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001242llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1243 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001245 llvm::DITypeRefArray Args(
1246 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001247 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001248 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001249
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001250 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001251
1252 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001253 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001254
David Blaikie2aaf0652013-01-07 22:24:59 +00001255 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001256 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001257 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1258 // Create pointer type directly in this case.
1259 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1260 QualType PointeeTy = ThisPtrTy->getPointeeType();
1261 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001262 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001263 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001264 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1265 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001266 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001267 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001268 // TODO: This and the artificial type below are misleading, the
1269 // types aren't artificial the argument is, but the current
1270 // metadata doesn't represent that.
1271 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1272 Elts.push_back(ThisPtrType);
1273 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001274 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001275 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001276 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1277 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001278 }
1279
1280 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001281 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1282 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001283
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001284 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001285
Leny Kholodov80c047d2016-09-06 10:48:04 +00001286 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001287 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001288 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001289 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001290 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001291
Reid Klecknerf00f8032016-06-08 20:41:54 +00001292 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1293 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001294}
1295
Eric Christopherb2a008c2013-05-16 00:45:12 +00001296/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001297/// inside a function.
1298static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001299 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001300 return isFunctionLocalClass(NRD);
1301 if (isa<FunctionDecl>(RD->getDeclContext()))
1302 return true;
1303 return false;
1304}
1305
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001306llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1307 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001308 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001309 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001310
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001312 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001313
1314 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1315 // make sense to give a single ctor/dtor a linkage name.
1316 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001317 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1318 // property to use here. It may've been intended to model "is non-external
1319 // type" but misses cases of non-function-local but non-external classes such
1320 // as those in anonymous namespaces as well as the reverse - external types
1321 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1323 MethodLinkageName = CGM.getMangledName(Method);
1324
1325 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001326 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001327 unsigned MethodLine = 0;
1328 if (!Method->isImplicit()) {
1329 MethodDefUnit = getOrCreateFile(Method->getLocation());
1330 MethodLine = getLineNumber(Method->getLocation());
1331 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001332
1333 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001334 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001335 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001337 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001338 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001339
Guy Benyei11169dd2012-12-18 14:30:41 +00001340 if (Method->isVirtual()) {
1341 if (Method->isPure())
1342 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1343 else
1344 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001345
Reid Kleckner216d0a12016-06-16 20:08:51 +00001346 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1347 // It doesn't make sense to give a virtual destructor a vtable index,
1348 // since a single destructor has two entries in the vtable.
1349 if (!isa<CXXDestructorDecl>(Method))
1350 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1351 } else {
1352 // Emit MS ABI vftable information. There is only one entry for the
1353 // deleting dtor.
1354 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1355 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1356 MicrosoftVTableContext::MethodVFTableLocation ML =
1357 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1358 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001359
1360 // CodeView only records the vftable offset in the class that introduces
1361 // the virtual method. This is possible because, unlike Itanium, the MS
1362 // C++ ABI does not include all virtual methods from non-primary bases in
1363 // the vtable for the most derived class. For example, if C inherits from
1364 // A and B, C's primary vftable will not include B's virtual methods.
1365 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1366 Flags |= llvm::DINode::FlagIntroducedVirtual;
1367
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001368 // The 'this' adjustment accounts for both the virtual and non-virtual
1369 // portions of the adjustment. Presumably the debugger only uses it when
1370 // it knows the dynamic type of an object.
1371 ThisAdjustment = CGM.getCXXABI()
1372 .getVirtualFunctionPrologueThisAdjustment(GD)
1373 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001374 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 ContainingType = RecordTy;
1376 }
1377
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001379 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001380 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001381 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001382 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001383 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001384 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 }
1388 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001390 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001391 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001392 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001393 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001394
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1396 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001397 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001398 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1399 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1400 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001401
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001402 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001403
1404 return SP;
1405}
1406
Eric Christophere7b87e52014-10-26 23:40:33 +00001407void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001408 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1409 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001410
1411 // Since we want more than just the individual member decls if we
1412 // have templated functions iterate over every declaration to gather
1413 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001414 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001415 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1416 // If the member is implicit, don't add it to the member list. This avoids
1417 // the member being added to type units by LLVM, while still allowing it
1418 // to be emitted into the type declaration/reference inside the compile
1419 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001420 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001421 // FIXME: Handle Using(Shadow?)Decls here to create
1422 // DW_TAG_imported_declarations inside the class for base decls brought into
1423 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1424 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1425 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001426 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001427 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001428
1429 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1430 continue;
1431
David Blaikiefd580722014-10-06 05:18:55 +00001432 // Reuse the existing member function declaration if it exists.
1433 // It may be associated with the declaration of the type & should be
1434 // reused as we're building the definition.
1435 //
1436 // This situation can arise in the vtable-based debug info reduction where
1437 // implicit members are emitted in a non-vtable TU.
1438 auto MI = SPCache.find(Method->getCanonicalDecl());
1439 EltTys.push_back(MI == SPCache.end()
1440 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001441 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001443}
Guy Benyei11169dd2012-12-18 14:30:41 +00001444
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001445void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001446 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001448 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1449 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1450 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001451
Bob Haarmandff36732016-10-25 22:19:32 +00001452 // If we are generating CodeView debug info, we also need to emit records for
1453 // indirect virtual base classes.
1454 if (CGM.getCodeGenOpts().EmitCodeView) {
1455 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1456 llvm::DINode::FlagIndirectVirtualBase);
1457 }
1458}
1459
1460void CGDebugInfo::CollectCXXBasesAux(
1461 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1462 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1463 const CXXRecordDecl::base_class_const_range &Bases,
1464 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1465 llvm::DINode::DIFlags StartingFlags) {
1466 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1467 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001468 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001469 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001470 if (!SeenTypes.insert(Base).second)
1471 continue;
1472 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1473 llvm::DINode::DIFlags BFlags = StartingFlags;
1474 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001475
Aaron Ballman574705e2014-03-13 15:41:46 +00001476 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001477 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1478 // virtual base offset offset is -ve. The code generator emits dwarf
1479 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001480 BaseOffset = 0 - CGM.getItaniumVTableContext()
1481 .getVirtualBaseOffsetOffset(RD, Base)
1482 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001483 } else {
1484 // In the MS ABI, store the vbtable offset, which is analogous to the
1485 // vbase offset offset in Itanium.
1486 BaseOffset =
1487 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1488 }
Bob Haarmandff36732016-10-25 22:19:32 +00001489 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001490 } else
1491 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1492 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1493 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001494
Adrian Prantl21361fb2014-08-29 22:44:27 +00001495 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001496 llvm::DIType *DTy =
1497 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001498 EltTys.push_back(DTy);
1499 }
1500}
1501
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001502llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001503CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1504 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001505 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001506 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001507 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1508 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001509 StringRef Name;
1510 if (TPList)
1511 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001512 switch (TA.getKind()) {
1513 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001514 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001515 TemplateParams.push_back(
1516 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001517 } break;
1518 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001519 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001520 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1521 TheCU, Name, TTy,
1522 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001523 } break;
1524 case TemplateArgument::Declaration: {
1525 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001526 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001527 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001528 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001529 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001530 // Variable pointer template parameters have a value that is the address
1531 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001532 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001533 V = CGM.GetAddrOfGlobalVar(VD);
1534 // Member function pointers have special support for building them, though
1535 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001536 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001537 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001538 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001539 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001540 // Member data pointers have special handling too to compute the fixed
1541 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001542 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001543 // These five lines (& possibly the above member function pointer
1544 // handling) might be able to be refactored to use similar code in
1545 // CodeGenModule::getMemberPointerConstant
1546 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1547 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001548 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001549 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001550 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001551 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1552 TheCU, Name, TTy,
1553 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001554 } break;
1555 case TemplateArgument::NullPtr: {
1556 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001558 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001559 // Special case member data pointer null values since they're actually -1
1560 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001561 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001562 // But treat member function pointers as simple zero integers because
1563 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1564 // CodeGen grows handling for values of non-null member function
1565 // pointers then perhaps we could remove this special case and rely on
1566 // EmitNullMemberPointer for member function pointers.
1567 if (MPT->isMemberDataPointer())
1568 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1569 if (!V)
1570 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001571 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001572 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001573 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001574 case TemplateArgument::Template:
1575 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001576 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001577 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1578 break;
1579 case TemplateArgument::Pack:
1580 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1581 TheCU, Name, nullptr,
1582 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1583 break;
David Majnemer5559d472013-08-24 08:21:10 +00001584 case TemplateArgument::Expression: {
1585 const Expr *E = TA.getAsExpr();
1586 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001587 if (E->isGLValue())
1588 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001589 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001590 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001591 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001592 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001593 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001594 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001595 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001596 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001597 case TemplateArgument::Null:
1598 llvm_unreachable(
1599 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001600 }
1601 }
1602 return DBuilder.getOrCreateArray(TemplateParams);
1603}
1604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001605llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001606CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001607 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001608 if (FD->getTemplatedKind() ==
1609 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001610 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1611 ->getTemplate()
1612 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001613 return CollectTemplateParams(
1614 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001616 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001617}
1618
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001619llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1620 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001621 // Always get the full list of parameters, not just the ones from
1622 // the specialization.
1623 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001624 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001625 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001626 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001627}
1628
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001629llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001630 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 return VTablePtrType;
1632
1633 ASTContext &Context = CGM.getContext();
1634
1635 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001636 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001637 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001638 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001639 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001640 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1641 Optional<unsigned> DWARFAddressSpace =
1642 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1643
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001644 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001645 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1646 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1648 return VTablePtrType;
1649}
1650
Guy Benyei11169dd2012-12-18 14:30:41 +00001651StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001652 // Copy the gdb compatible name on the side and use its reference.
1653 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001654}
1655
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001656void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001657 SmallVectorImpl<llvm::Metadata *> &EltTys,
1658 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001659 // If this class is not dynamic then there is not any vtable info to collect.
1660 if (!RD->isDynamicClass())
1661 return;
1662
Reid Kleckner59812422016-08-31 20:35:01 +00001663 // Don't emit any vtable shape or vptr info if this class doesn't have an
1664 // extendable vfptr. This can happen if the class doesn't have virtual
1665 // methods, or in the MS ABI if those virtual methods only come from virtually
1666 // inherited bases.
1667 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1668 if (!RL.hasExtendableVFPtr())
1669 return;
1670
Reid Klecknerdc124992016-08-31 16:11:43 +00001671 // CodeView needs to know how large the vtable of every dynamic class is, so
1672 // emit a special named pointer type into the element list. The vptr type
1673 // points to this type as well.
1674 llvm::DIType *VPtrTy = nullptr;
1675 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1676 CGM.getTarget().getCXXABI().isMicrosoft();
1677 if (NeedVTableShape) {
1678 uint64_t PtrWidth =
1679 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1680 const VTableLayout &VFTLayout =
1681 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1682 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001683 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001684 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001685 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1686 Optional<unsigned> DWARFAddressSpace =
1687 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001688
1689 // Create a very wide void* type and insert it directly in the element list.
1690 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001691 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1692 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001693 EltTys.push_back(VTableType);
1694
1695 // The vptr is a pointer to this special vtable type.
1696 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1697 }
1698
1699 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001700 if (RL.getPrimaryBase())
1701 return;
1702
1703 if (!VPtrTy)
1704 VPtrTy = getOrCreateVTablePtrType(Unit);
1705
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001707 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001708 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001709 llvm::DINode::FlagArtificial, VPtrTy);
1710 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001711}
1712
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001713llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001714 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001715 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001716 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 return T;
1718}
1719
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001720llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001721 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001722 return getOrCreateStandaloneType(D, Loc);
1723}
1724
1725llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1726 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001727 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001728 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001730 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001731
Adrian Prantl73409ce2013-03-11 18:33:46 +00001732 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001733 return T;
1734}
1735
David Blaikie483a9da2014-05-06 18:35:21 +00001736void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001737 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001738 return;
1739 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001740 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001741 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001742 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001743 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001744 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001745 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001746 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001747}
1748
David Blaikieb2e86eb2013-08-15 20:49:17 +00001749void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001750 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001751 !CGM.getLangOpts().CPlusPlus)
1752 completeRequiredType(RD);
1753}
1754
David Blaikieb11c8732017-01-30 06:36:08 +00001755/// Return true if the class or any of its methods are marked dllimport.
1756static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1757 if (RD->hasAttr<DLLImportAttr>())
1758 return true;
1759 for (const CXXMethodDecl *MD : RD->methods())
1760 if (MD->hasAttr<DLLImportAttr>())
1761 return true;
1762 return false;
1763}
1764
David Blaikie6943dea2013-08-20 01:28:15 +00001765void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001766 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1767 if (CXXRD->isDynamicClass() &&
1768 CGM.getVTableLinkage(CXXRD) ==
1769 llvm::GlobalValue::AvailableExternallyLinkage &&
1770 !isClassOrMethodDLLImport(CXXRD))
1771 return;
1772 completeClass(RD);
1773}
1774
1775void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001776 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001777 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001778 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001779 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001780 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001782 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001783 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001784 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001785 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001786}
1787
David Blaikie0e716b42014-03-03 23:48:23 +00001788static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1789 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001790 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1791 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001792 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001793 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001794 return true;
1795 return false;
1796}
1797
Adrian Prantl05fefa42016-04-25 20:52:40 +00001798/// Does a type definition exist in an imported clang module?
1799static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001800 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001801 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001802 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001803 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001804 if (!RD->isExternallyVisible() && RD->getName().empty())
1805 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001806 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001807 if (!CXXDecl->isCompleteDefinition())
1808 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001809 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1810 if (TemplateKind != TSK_Undeclared) {
1811 // This is a template, check the origin of the first member.
1812 if (CXXDecl->field_begin() == CXXDecl->field_end())
1813 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1814 if (!CXXDecl->field_begin()->isFromASTFile())
1815 return false;
1816 }
Adrian Prantl94913712016-04-26 23:42:43 +00001817 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001818 return true;
1819}
1820
Benjamin Kramer8c305922016-02-02 11:06:51 +00001821static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1822 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001823 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001824 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001825 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001826
David Blaikie1ac9c982017-04-11 21:13:37 +00001827 if (auto *ES = RD->getASTContext().getExternalSource())
1828 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1829 return true;
1830
Benjamin Kramer8c305922016-02-02 11:06:51 +00001831 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001832 return false;
1833
1834 if (!LangOpts.CPlusPlus)
1835 return false;
1836
1837 if (!RD->isCompleteDefinitionRequired())
1838 return true;
1839
David Majnemer58ed0f32016-07-17 00:39:12 +00001840 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001841
1842 if (!CXXDecl)
1843 return false;
1844
Adrian McCarthy99242982016-08-16 22:11:18 +00001845 // Only emit complete debug info for a dynamic class when its vtable is
1846 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001847 // across DLL boundaries, so skip this optimization if the class or any of its
1848 // methods are marked dllimport. This isn't a complete solution, since objects
1849 // without any dllimport methods can be used in one DLL and constructed in
1850 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001851 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001852 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001853 return true;
1854
1855 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001856 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001857 Spec = SD->getSpecializationKind();
1858
1859 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1860 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1861 CXXDecl->method_end()))
1862 return true;
1863
1864 return false;
1865}
1866
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001867void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1868 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1869 return;
1870
1871 QualType Ty = CGM.getContext().getRecordType(RD);
1872 llvm::DIType *T = getTypeOrNull(Ty);
1873 if (T && T->isForwardDecl())
1874 completeClassData(RD);
1875}
1876
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001877llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001879 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001880 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1881 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001882 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001883 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001884 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001885 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001886
David Blaikieb2e86eb2013-08-15 20:49:17 +00001887 return CreateTypeDefinition(Ty);
1888}
1889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001891 RecordDecl *RD = Ty->getDecl();
1892
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001894 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001895
1896 // Records and classes and unions can all be recursive. To handle them, we
1897 // first generate a debug descriptor for the struct as a forward declaration.
1898 // Then (if it is a definition) we go through and get debug info for all of
1899 // its members. Finally, we create a descriptor for the complete type (which
1900 // may refer to the forward decl if the struct is recursive) and replace all
1901 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001902 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001903
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001904 const RecordDecl *D = RD->getDefinition();
1905 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 return FwdDecl;
1907
David Majnemer58ed0f32016-07-17 00:39:12 +00001908 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001909 CollectContainingType(CXXDecl, FwdDecl);
1910
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001912 LexicalBlockStack.emplace_back(&*FwdDecl);
1913 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001914
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001916 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001917 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001918
1919 // Note: The split of CXXDecl information here is intentional, the
1920 // gdb tests will depend on a certain ordering at printout. The debug
1921 // information offsets are still correct if we merge them all together
1922 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001923 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 if (CXXDecl) {
1925 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001926 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 }
1928
Eric Christopher91a31902013-01-16 01:22:32 +00001929 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001931 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001933
1934 LexicalBlockStack.pop_back();
1935 RegionMap.erase(Ty->getDecl());
1936
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001937 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001938 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001939
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001940 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001941 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001942 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001943
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001944 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001945 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001946}
1947
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001948llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1949 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001950 // Ignore protocols.
1951 return getOrCreateType(Ty->getBaseType(), Unit);
1952}
1953
Manman Rene6be26c2016-09-13 17:25:08 +00001954llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1955 llvm::DIFile *Unit) {
1956 // Ignore protocols.
1957 SourceLocation Loc = Ty->getDecl()->getLocation();
1958
1959 // Use Typedefs to represent ObjCTypeParamType.
1960 return DBuilder.createTypedef(
1961 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1962 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1963 getDeclContextDescriptor(Ty->getDecl()));
1964}
1965
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001966/// \return true if Getter has the default name for the property PD.
1967static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1968 const ObjCMethodDecl *Getter) {
1969 assert(PD);
1970 if (!Getter)
1971 return true;
1972
1973 assert(Getter->getDeclName().isObjCZeroArgSelector());
1974 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001975 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001976}
1977
1978/// \return true if Setter has the default name for the property PD.
1979static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1980 const ObjCMethodDecl *Setter) {
1981 assert(PD);
1982 if (!Setter)
1983 return true;
1984
1985 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001986 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001987 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001988}
1989
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001990llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1991 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 ObjCInterfaceDecl *ID = Ty->getDecl();
1993 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001994 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001995
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001996 // Return a forward declaration if this type was imported from a clang module,
1997 // and this is not the compile unit with the implementation of the type (which
1998 // may contain hidden ivars).
1999 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2000 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002001 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2002 ID->getName(),
2003 getDeclContextDescriptor(ID), Unit, 0);
2004
Guy Benyei11169dd2012-12-18 14:30:41 +00002005 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002006 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002008 auto RuntimeLang =
2009 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002010
2011 // If this is just a forward declaration return a special forward-declaration
2012 // debug type since we won't be able to lay out the entire type.
2013 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002014 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002015 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002016 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002017 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2018 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002019 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 return FwdDecl;
2021 }
2022
David Blaikieef8a9512014-05-05 23:23:53 +00002023 return CreateTypeDefinition(Ty, Unit);
2024}
2025
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002026llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002027CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2028 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002029 // Use the Module pointer as the key into the cache. This is a
2030 // nullptr if the "Module" is a PCH, which is safe because we don't
2031 // support chained PCH debug info, so there can only be a single PCH.
2032 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002033 auto ModRef = ModuleCache.find(M);
2034 if (ModRef != ModuleCache.end())
2035 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002036
2037 // Macro definitions that were defined with "-D" on the command line.
2038 SmallString<128> ConfigMacros;
2039 {
2040 llvm::raw_svector_ostream OS(ConfigMacros);
2041 const auto &PPOpts = CGM.getPreprocessorOpts();
2042 unsigned I = 0;
2043 // Translate the macro definitions back into a commmand line.
2044 for (auto &M : PPOpts.Macros) {
2045 if (++I > 1)
2046 OS << " ";
2047 const std::string &Macro = M.first;
2048 bool Undef = M.second;
2049 OS << "\"-" << (Undef ? 'U' : 'D');
2050 for (char c : Macro)
2051 switch (c) {
2052 case '\\' : OS << "\\\\"; break;
2053 case '"' : OS << "\\\""; break;
2054 default: OS << c;
2055 }
2056 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002057 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002058 }
Adrian Prantl66689202015-09-18 23:01:45 +00002059
Adrian Prantl835e6632015-09-24 16:10:10 +00002060 bool IsRootModule = M ? !M->Parent : true;
2061 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002062 // PCH files don't have a signature field in the control block,
2063 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002064 // We use the lower 64 bits for debug info.
2065 uint64_t Signature =
2066 Mod.getSignature()
2067 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2068 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002069 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002070 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2071 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2072 TheCU->getProducer(), true, StringRef(), 0,
2073 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2074 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002075 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002076 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002077 llvm::DIModule *Parent =
2078 IsRootModule ? nullptr
2079 : getOrCreateModuleRef(
2080 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2081 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002082 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002083 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2084 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002085 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002086 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002087}
2088
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002089llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2090 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002091 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002092 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002093 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002094 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002095
2096 // Bit size, align and offset of the type.
2097 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002098 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002099
Leny Kholodov80c047d2016-09-06 10:48:04 +00002100 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002101 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002102 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
Adrian Prantlfd696112015-10-01 00:48:51 +00002104 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002105 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002106 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2107 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002108
David Blaikieef8a9512014-05-05 23:23:53 +00002109 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002110 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002111
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002112 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002113 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002114 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002115
2116 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002117 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002118
2119 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2120 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002121 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002122 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002123 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002124 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002125
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002126 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2127 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002128 EltTys.push_back(InhTag);
2129 }
2130
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002131 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002132 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002133 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002134 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 unsigned PLine = getLineNumber(Loc);
2136 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2137 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002138 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2139 PD->getName(), PUnit, PLine,
2140 hasDefaultGetterName(PD, Getter) ? ""
2141 : getSelectorName(PD->getGetterName()),
2142 hasDefaultSetterName(PD, Setter) ? ""
2143 : getSelectorName(PD->getSetterName()),
2144 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002146 };
2147 {
2148 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002149 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002150 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002151 PropertySet.insert(PD->getIdentifier());
2152 AddProperty(PD);
2153 }
Manman Renefe1bac2016-01-27 20:00:32 +00002154 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002155 // Don't emit duplicate metadata for properties that were already in a
2156 // class extension.
2157 if (!PropertySet.insert(PD->getIdentifier()).second)
2158 continue;
2159 AddProperty(PD);
2160 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002161 }
2162
2163 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2164 unsigned FieldNo = 0;
2165 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2166 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002167 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002168 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002169 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002170
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 StringRef FieldName = Field->getName();
2172
2173 // Ignore unnamed fields.
2174 if (FieldName.empty())
2175 continue;
2176
2177 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 unsigned FieldLine = getLineNumber(Field->getLocation());
2180 QualType FType = Field->getType();
2181 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002182 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002183
2184 if (!FType->isIncompleteArrayType()) {
2185
2186 // Bit size, align and offset of the type.
2187 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002188 ? Field->getBitWidthValue(CGM.getContext())
2189 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002190 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 }
2192
2193 uint64_t FieldOffset;
2194 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2195 // We don't know the runtime offset of an ivar if we're using the
2196 // non-fragile ABI. For bitfields, use the bit offset into the first
2197 // byte of storage of the bitfield. For other fields, use zero.
2198 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002199 FieldOffset =
2200 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 FieldOffset %= CGM.getContext().getCharWidth();
2202 } else {
2203 FieldOffset = 0;
2204 }
2205 } else {
2206 FieldOffset = RL.getFieldOffset(FieldNo);
2207 }
2208
Leny Kholodov80c047d2016-09-06 10:48:04 +00002209 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002211 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002212 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002213 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002214 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002215 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002216
Craig Topper8a13c412014-05-21 05:09:00 +00002217 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002219 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002220 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002222 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002223 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002224 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2226 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002227 PropertyNode = DBuilder.createObjCProperty(
2228 PD->getName(), PUnit, PLine,
2229 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2230 PD->getGetterName()),
2231 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2232 PD->getSetterName()),
2233 PD->getPropertyAttributes(),
2234 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002235 }
2236 }
2237 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002238 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2239 FieldSize, FieldAlign, FieldOffset, Flags,
2240 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 EltTys.push_back(FieldTy);
2242 }
2243
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002244 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002245 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002246
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002248 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249}
2250
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002251llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2252 llvm::DIFile *Unit) {
2253 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002254 int64_t Count = Ty->getNumElements();
2255 if (Count == 0)
2256 // If number of elements are not known then this is an unbounded array.
2257 // Use Count == -1 to express such arrays.
2258 Count = -1;
2259
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002260 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002261 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002262
2263 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002264 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002265
2266 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2267}
2268
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002269llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002270 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002271 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002272
2273 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002274 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002275 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002276 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2277 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 } else if (Ty->isIncompleteArrayType()) {
2279 Size = 0;
2280 if (Ty->getElementType()->isIncompleteType())
2281 Align = 0;
2282 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002283 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002284 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 Size = 0;
2286 Align = 0;
2287 } else {
2288 // Size and align of the whole array, not the element type.
2289 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002290 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002291 }
2292
2293 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2294 // interior arrays, do we care? Why aren't nested arrays represented the
2295 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002296 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 QualType EltTy(Ty, 0);
2298 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2299 // If the number of elements is known, then count is that number. Otherwise,
2300 // it's -1. This allows us to represent a subrange with an array of 0
2301 // elements, like this:
2302 //
2303 // struct foo {
2304 // int x[0];
2305 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002306 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002307 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002309 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002310 if (Expr *Size = VAT->getSizeExpr()) {
2311 llvm::APSInt V;
2312 if (Size->EvaluateAsInt(V, CGM.getContext()))
2313 Count = V.getExtValue();
2314 }
David Blaikie87173f12016-08-22 17:49:56 +00002315 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002316
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 // FIXME: Verify this is right for VLAs.
2318 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2319 EltTy = Ty->getElementType();
2320 }
2321
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002322 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002323
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002324 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2325 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002326}
2327
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002328llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2329 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002330 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2331 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002332}
2333
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002334llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2335 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002336 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2337 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002338}
2339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002340llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2341 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002342 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002343 uint64_t Size = 0;
2344
2345 if (!Ty->isIncompleteType()) {
2346 Size = CGM.getContext().getTypeSize(Ty);
2347
2348 // Set the MS inheritance model. There is no flag for the unspecified model.
2349 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2350 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2351 case MSInheritanceAttr::Keyword_single_inheritance:
2352 Flags |= llvm::DINode::FlagSingleInheritance;
2353 break;
2354 case MSInheritanceAttr::Keyword_multiple_inheritance:
2355 Flags |= llvm::DINode::FlagMultipleInheritance;
2356 break;
2357 case MSInheritanceAttr::Keyword_virtual_inheritance:
2358 Flags |= llvm::DINode::FlagVirtualInheritance;
2359 break;
2360 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2361 break;
2362 }
2363 }
2364 }
2365
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002367 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002368 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002369 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2370 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002371
2372 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002373 Ty->getPointeeType()->getAs<FunctionProtoType>();
2374 return DBuilder.createMemberPointerType(
2375 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2376 Ty->getClass(), FPT->getTypeQuals())),
2377 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002378 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002379}
2380
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002382 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2383 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002384}
2385
Xiuli Pan9c14e282016-01-09 12:53:17 +00002386llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2387 llvm::DIFile *U) {
2388 return getOrCreateType(Ty->getElementType(), U);
2389}
2390
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002391llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002392 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002393
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002395 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002396 if (!ED->getTypeForDecl()->isIncompleteType()) {
2397 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002398 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002399 }
2400
Manman Rene0064d82013-08-29 23:19:58 +00002401 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2402
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002403 bool isImportedFromModule =
2404 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2405
Guy Benyei11169dd2012-12-18 14:30:41 +00002406 // If this is just a forward declaration, construct an appropriately
2407 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002408 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002409 // Note that it is possible for enums to be created as part of
2410 // their own declcontext. In this case a FwdDecl will be created
2411 // twice. This doesn't cause a problem because both FwdDecls are
2412 // entered into the ReplaceMap: finalize() will replace the first
2413 // FwdDecl with the second and then replace the second with
2414 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002415 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002416 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002417 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2418 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002419
Guy Benyei11169dd2012-12-18 14:30:41 +00002420 unsigned Line = getLineNumber(ED->getLocation());
2421 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002422 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002423 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2424 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002425
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002426 ReplaceMap.emplace_back(
2427 std::piecewise_construct, std::make_tuple(Ty),
2428 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002429 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 }
2431
David Blaikie483a9da2014-05-06 18:35:21 +00002432 return CreateTypeDefinition(Ty);
2433}
2434
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002435llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002436 const EnumDecl *ED = Ty->getDecl();
2437 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002438 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002439 if (!ED->getTypeForDecl()->isIncompleteType()) {
2440 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002441 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002442 }
2443
2444 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2445
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002446 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002447 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002448 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002449 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002450 Enumerators.push_back(DBuilder.createEnumerator(
2451 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002452 }
2453
2454 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002456
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002457 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002459 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002460 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002461 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2462 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2463 Line, Size, Align, EltArray, ClassTy,
2464 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002465}
2466
Amjad Aboud546bc112017-02-09 22:07:24 +00002467llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2468 unsigned MType, SourceLocation LineLoc,
2469 StringRef Name, StringRef Value) {
2470 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2471 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2472}
2473
2474llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2475 SourceLocation LineLoc,
2476 SourceLocation FileLoc) {
2477 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2478 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2479 return DBuilder.createTempMacroFile(Parent, Line, FName);
2480}
2481
David Blaikie05491062013-01-21 04:37:12 +00002482static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2483 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002485 Qualifiers InnerQuals = T.getLocalQualifiers();
2486 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2487 // that is already there.
2488 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2489 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002490 QualType LastT = T;
2491 switch (T->getTypeClass()) {
2492 default:
David Blaikie05491062013-01-21 04:37:12 +00002493 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002494 case Type::TemplateSpecialization: {
2495 const auto *Spec = cast<TemplateSpecializationType>(T);
2496 if (Spec->isTypeAlias())
2497 return C.getQualifiedType(T.getTypePtr(), Quals);
2498 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002499 break;
2500 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 case Type::TypeOfExpr:
2502 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2503 break;
2504 case Type::TypeOf:
2505 T = cast<TypeOfType>(T)->getUnderlyingType();
2506 break;
2507 case Type::Decltype:
2508 T = cast<DecltypeType>(T)->getUnderlyingType();
2509 break;
2510 case Type::UnaryTransform:
2511 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2512 break;
2513 case Type::Attributed:
2514 T = cast<AttributedType>(T)->getEquivalentType();
2515 break;
2516 case Type::Elaborated:
2517 T = cast<ElaboratedType>(T)->getNamedType();
2518 break;
2519 case Type::Paren:
2520 T = cast<ParenType>(T)->getInnerType();
2521 break;
David Blaikie05491062013-01-21 04:37:12 +00002522 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002524 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002525 case Type::Auto:
2526 case Type::DeducedTemplateSpecialization: {
2527 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002528 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002529 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002530 break;
2531 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002532 case Type::Adjusted:
2533 case Type::Decayed:
2534 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2535 T = cast<AdjustedType>(T)->getAdjustedType();
2536 break;
2537 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002538
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002540 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002541 } while (true);
2542}
2543
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002544llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002545
2546 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002547 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002548
David Blaikief427b002014-05-06 03:42:01 +00002549 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 if (it != TypeCache.end()) {
2551 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002552 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002553 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 }
2555
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002556 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002557}
2558
David Blaikie0e716b42014-03-03 23:48:23 +00002559void CGDebugInfo::completeTemplateDefinition(
2560 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002561 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002562 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002563 completeUnusedClass(SD);
2564}
David Blaikie0856f662014-03-04 22:01:08 +00002565
David Blaikie1ac9c982017-04-11 21:13:37 +00002566void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2567 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2568 return;
2569
2570 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002571 // In case this type has no member function definitions being emitted, ensure
2572 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002573 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002574}
2575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002576llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002578 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002579
2580 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002581 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002582
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002583 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 return T;
2585
Adrian Prantlca844182015-09-11 17:23:03 +00002586 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002587 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002588
2589 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002590 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002591
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 return Res;
2593}
2594
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002595llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002596 // A forward declaration inside a module header does not belong to the module.
2597 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2598 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002599 if (DebugTypeExtRefs && D->isFromASTFile()) {
2600 // Record a reference to an imported clang module or precompiled header.
2601 auto *Reader = CGM.getContext().getExternalSource();
2602 auto Idx = D->getOwningModuleID();
2603 auto Info = Reader->getSourceDescriptor(Idx);
2604 if (Info)
2605 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2606 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002607 // We are building a clang module or a precompiled header.
2608 //
2609 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2610 // and it wouldn't be necessary to specify the parent scope
2611 // because the type is already unique by definition (it would look
2612 // like the output of -fno-standalone-debug). On the other hand,
2613 // the parent scope helps a consumer to quickly locate the object
2614 // file where the type's definition is located, so it might be
2615 // best to make this behavior a command line or debugger tuning
2616 // option.
2617 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2618 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002619 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002620 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2621 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002622 } else {
2623 // This the precompiled header being built.
2624 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002625 }
2626 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002627
Adrian Prantl9402cef2015-09-20 16:51:35 +00002628 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002629}
2630
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002631llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002632 // Handle qualifiers, which recursively handles what they refer to.
2633 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002634 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002635
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 // Work out details of type.
2637 switch (Ty->getTypeClass()) {
2638#define TYPE(Class, Base)
2639#define ABSTRACT_TYPE(Class, Base)
2640#define NON_CANONICAL_TYPE(Class, Base)
2641#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2642#include "clang/AST/TypeNodes.def"
2643 llvm_unreachable("Dependent types cannot show up in debug information");
2644
2645 case Type::ExtVector:
2646 case Type::Vector:
2647 return CreateType(cast<VectorType>(Ty), Unit);
2648 case Type::ObjCObjectPointer:
2649 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2650 case Type::ObjCObject:
2651 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002652 case Type::ObjCTypeParam:
2653 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002654 case Type::ObjCInterface:
2655 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2656 case Type::Builtin:
2657 return CreateType(cast<BuiltinType>(Ty));
2658 case Type::Complex:
2659 return CreateType(cast<ComplexType>(Ty));
2660 case Type::Pointer:
2661 return CreateType(cast<PointerType>(Ty), Unit);
2662 case Type::BlockPointer:
2663 return CreateType(cast<BlockPointerType>(Ty), Unit);
2664 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002665 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002666 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002667 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002668 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002669 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 case Type::FunctionProto:
2671 case Type::FunctionNoProto:
2672 return CreateType(cast<FunctionType>(Ty), Unit);
2673 case Type::ConstantArray:
2674 case Type::VariableArray:
2675 case Type::IncompleteArray:
2676 return CreateType(cast<ArrayType>(Ty), Unit);
2677
2678 case Type::LValueReference:
2679 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2680 case Type::RValueReference:
2681 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2682
2683 case Type::MemberPointer:
2684 return CreateType(cast<MemberPointerType>(Ty), Unit);
2685
2686 case Type::Atomic:
2687 return CreateType(cast<AtomicType>(Ty), Unit);
2688
Xiuli Pan9c14e282016-01-09 12:53:17 +00002689 case Type::Pipe:
2690 return CreateType(cast<PipeType>(Ty), Unit);
2691
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002693 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2694
David Blaikie42edade2014-11-11 20:44:45 +00002695 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002696 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002697 case Type::Adjusted:
2698 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002699 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 case Type::Elaborated:
2701 case Type::Paren:
2702 case Type::SubstTemplateTypeParm:
2703 case Type::TypeOfExpr:
2704 case Type::TypeOf:
2705 case Type::Decltype:
2706 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002707 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002708 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002710
David Blaikie42edade2014-11-11 20:44:45 +00002711 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002712}
2713
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002714llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2715 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002716 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002717
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002718 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002719
2720 // We may have cached a forward decl when we could have created
2721 // a non-forward decl. Go ahead and create a non-forward decl
2722 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002723 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002724 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002725
2726 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002727 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002728
2729 // Propagate members from the declaration to the definition
2730 // CreateType(const RecordType*) will overwrite this with the members in the
2731 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002732 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002733
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002735 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 return Res;
2737}
2738
2739// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002740llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002741 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002742
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002744 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 unsigned Line = getLineNumber(RD->getLocation());
2746 StringRef RDName = getClassName(RD);
2747
Amjad Abouddc4531e2016-04-30 01:44:38 +00002748 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002749
David Blaikied2785892013-08-18 17:36:19 +00002750 // If we ended up creating the type during the context chain construction,
2751 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002752 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002753 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002754 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002755 return T;
David Blaikied2785892013-08-18 17:36:19 +00002756
Adrian Prantl381e7552014-02-04 21:29:50 +00002757 // If this is just a forward or incomplete declaration, construct an
2758 // appropriately marked node and just return it.
2759 const RecordDecl *D = RD->getDefinition();
2760 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002761 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002762
2763 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002764 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002765
Manman Rene0064d82013-08-29 23:19:58 +00002766 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2767
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002769 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2770 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002771
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002772 // Elements of composite types usually have back to the type, creating
2773 // uniquing cycles. Distinct nodes are more efficient.
2774 switch (RealDecl->getTag()) {
2775 default:
2776 llvm_unreachable("invalid composite type tag");
2777
2778 case llvm::dwarf::DW_TAG_array_type:
2779 case llvm::dwarf::DW_TAG_enumeration_type:
2780 // Array elements and most enumeration elements don't have back references,
2781 // so they don't tend to be involved in uniquing cycles and there is some
2782 // chance of merging them when linking together two modules. Only make
2783 // them distinct if they are ODR-uniqued.
2784 if (FullName.empty())
2785 break;
2786
2787 case llvm::dwarf::DW_TAG_structure_type:
2788 case llvm::dwarf::DW_TAG_union_type:
2789 case llvm::dwarf::DW_TAG_class_type:
2790 // Immediatley resolve to a distinct node.
2791 RealDecl =
2792 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2793 break;
2794 }
2795
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002796 RegionMap[Ty->getDecl()].reset(RealDecl);
2797 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002798
David Majnemer58ed0f32016-07-17 00:39:12 +00002799 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002800 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002801 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002802 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002803}
2804
David Blaikieadfbf992013-08-18 16:55:33 +00002805void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002806 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002807 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002808 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002809 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2810 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002811 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002812 while (1) {
2813 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2814 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2815 if (PBT && !BRL.isPrimaryBaseVirtual())
2816 PBase = PBT;
2817 else
2818 break;
2819 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002820 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002821 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2822 getOrCreateFile(RD->getLocation())));
2823 } else if (RD->isDynamicClass())
2824 ContainingType = RealDecl;
2825
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002826 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002827}
2828
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002829llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002830 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002831 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002832 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002833 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002834 llvm::DIType *Ty =
2835 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2836 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 *Offset += FieldSize;
2838 return Ty;
2839}
2840
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002841void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002842 StringRef &Name,
2843 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002844 llvm::DIScope *&FDContext,
2845 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002846 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002847 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002848 Name = getFunctionName(FD);
2849 // Use mangled name as linkage name for C/C++ functions.
2850 if (FD->hasPrototype()) {
2851 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002852 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002853 }
2854 // No need to replicate the linkage name if it isn't different from the
2855 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002856 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002857 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2858 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002859 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002860 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002861 LinkageName = StringRef();
2862
Benjamin Kramer8c305922016-02-02 11:06:51 +00002863 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002864 if (const NamespaceDecl *NSDecl =
2865 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantld8870552017-05-11 22:59:19 +00002866 FDContext = getOrCreateNamespace(NSDecl, getParentModuleOrNull(FD));
Frederic Riss9db79f12014-11-18 03:40:46 +00002867 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002868 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2869 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2870 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2871 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002872 // Check if it is a noreturn-marked function
2873 if (FD->isNoReturn())
2874 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002875 // Collect template parameters.
2876 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2877 }
2878}
2879
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002880void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002881 unsigned &LineNo, QualType &T,
2882 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002883 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002884 Unit = getOrCreateFile(VD->getLocation());
2885 LineNo = getLineNumber(VD->getLocation());
2886
2887 setLocation(VD->getLocation());
2888
2889 T = VD->getType();
2890 if (T->isIncompleteArrayType()) {
2891 // CodeGen turns int[] into int[1] so we'll do the same here.
2892 llvm::APInt ConstVal(32, 1);
2893 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2894
2895 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2896 ArrayType::Normal, 0);
2897 }
2898
2899 Name = VD->getName();
2900 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2901 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2902 LinkageName = CGM.getMangledName(VD);
2903 if (LinkageName == Name)
2904 LinkageName = StringRef();
2905
2906 // Since we emit declarations (DW_AT_members) for static members, place the
2907 // definition of those static members in the namespace they were declared in
2908 // in the source code (the lexical decl context).
2909 // FIXME: Generalize this for even non-member global variables where the
2910 // declaration and definition may have different lexical decl contexts, once
2911 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002912 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2913 : VD->getDeclContext();
2914 // When a record type contains an in-line initialization of a static data
2915 // member, and the record type is marked as __declspec(dllexport), an implicit
2916 // definition of the member will be created in the record context. DWARF
2917 // doesn't seem to have a nice way to describe this in a form that consumers
2918 // are likely to understand, so fake the "normal" situation of a definition
2919 // outside the class by putting it in the global scope.
2920 if (DC->isRecord())
2921 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002922
Amjad Abouddc4531e2016-04-30 01:44:38 +00002923 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2924 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002925}
2926
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002927llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2928 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002929 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002930 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002931 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002932 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002933 llvm::DIFile *Unit = getOrCreateFile(Loc);
2934 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002935 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002936 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002937 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002938 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2939
Frederic Rissd253ed62014-11-18 03:40:51 +00002940 // Build function type.
2941 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002942 if (FD)
2943 for (const ParmVarDecl *Parm : FD->parameters())
2944 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002945 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2946 QualType FnType = CGM.getContext().getFunctionType(
2947 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002948 if (Stub) {
2949 return DBuilder.createFunction(
2950 DContext, Name, LinkageName, Unit, Line,
2951 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2952 !FD->isExternallyVisible(),
2953 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2954 TParamsArray.get(), getFunctionDeclaration(FD));
2955 }
2956
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002957 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002958 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002959 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2960 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002961 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002962 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002963 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002964 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2965 std::make_tuple(CanonDecl),
2966 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002967 return SP;
2968}
2969
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002970llvm::DISubprogram *
2971CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
2972 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
2973}
2974
2975llvm::DISubprogram *
2976CGDebugInfo::getFunctionStub(GlobalDecl GD) {
2977 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
2978}
2979
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002980llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002981CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2982 QualType T;
2983 StringRef Name, LinkageName;
2984 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002985 llvm::DIFile *Unit = getOrCreateFile(Loc);
2986 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002987 unsigned Line = getLineNumber(Loc);
2988
2989 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002990 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002991 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2992 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002993 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002994 FwdDeclReplaceMap.emplace_back(
2995 std::piecewise_construct,
2996 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2997 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002998 return GV;
2999}
3000
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003001llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003002 // We only need a declaration (not a definition) of the type - so use whatever
3003 // we would otherwise do to get a type for a pointee. (forward declarations in
3004 // limited debug info, full definitions (if the type definition is available)
3005 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003006 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003007 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003008 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003009 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003010
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003011 if (I != DeclCache.end()) {
3012 auto N = I->second;
3013 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3014 return GVE->getVariable();
3015 return dyn_cast_or_null<llvm::DINode>(N);
3016 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003017
3018 // No definition for now. Emit a forward definition that might be
3019 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003020 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003021 return getFunctionForwardDeclaration(FD);
3022 else if (const auto *VD = dyn_cast<VarDecl>(D))
3023 return getGlobalVariableForwardDeclaration(VD);
3024
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003025 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003026}
3027
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003028llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003029 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003030 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003031
David Majnemer58ed0f32016-07-17 00:39:12 +00003032 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003033 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003034 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003035
3036 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003037 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003038
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003039 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003040 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003041 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003042 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003043 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003044 }
3045 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003046 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003047 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003048 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003049 return SP;
3050 }
3051
Aaron Ballman86c93902014-03-06 23:45:36 +00003052 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003053 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003054 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003055 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003056 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003057 return SP;
3058 }
3059 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003060 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003061}
3062
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003063// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003064// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003065llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003066 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003067 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003068 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003069 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3070 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003071 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003072
David Majnemer58ed0f32016-07-17 00:39:12 +00003073 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003075
3076 const auto *FTy = FnType->getAs<FunctionType>();
3077 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3078
David Majnemer58ed0f32016-07-17 00:39:12 +00003079 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003080 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003081 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003082
3083 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003084 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003085
3086 // Replace the instancetype keyword with the actual type.
3087 if (ResultTy == CGM.getContext().getObjCInstanceType())
3088 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003089 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003090
Adrian Prantl7bec9032013-05-10 21:08:31 +00003091 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003092 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003093 QualType SelfDeclTy;
3094 if (auto *SelfDecl = OMethod->getSelfDecl())
3095 SelfDeclTy = SelfDecl->getType();
3096 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3097 if (FPT->getNumParams() > 1)
3098 SelfDeclTy = FPT->getParamType(0);
3099 if (!SelfDeclTy.isNull())
3100 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003102 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003103 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003105 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003106 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003107 // Variadic methods need a special marker at the end of the type list.
3108 if (OMethod->isVariadic())
3109 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003110
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003111 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003112 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3113 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003115
Adrian Prantl800faef2014-02-25 23:42:18 +00003116 // Handle variadic function types; they need an additional
3117 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003118 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003119 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003120 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003121 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003122 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3123 for (QualType ParamType : FPT->param_types())
3124 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003125 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003126 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003127 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3128 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003129 }
3130
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003131 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003132}
3133
Eric Christophere7b87e52014-10-26 23:40:33 +00003134void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3135 SourceLocation ScopeLoc, QualType FnType,
3136 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003137
3138 StringRef Name;
3139 StringRef LinkageName;
3140
3141 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3142
3143 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003144 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003145
Leny Kholodov80c047d2016-09-06 10:48:04 +00003146 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003147 llvm::DIFile *Unit = getOrCreateFile(Loc);
3148 llvm::DIScope *FDContext = Unit;
3149 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 if (!HasDecl) {
3151 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003152 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003153 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003154 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003155 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003156 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003157 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003158 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003159 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003160 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003161 return;
3162 }
3163 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003164 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3165 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003166 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003167 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003168 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 } else {
3170 // Use llvm function name.
3171 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003172 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003173 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003174 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 Name = Name.substr(1);
3176
Adrian Prantl42d71b92014-04-10 23:21:53 +00003177 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003178 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003179 // Artificial functions should not silently reuse CurLoc.
3180 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003181 }
3182 unsigned LineNo = getLineNumber(Loc);
3183 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
Eric Christopher8018e412014-03-27 18:50:35 +00003185 // FIXME: The function declaration we're constructing here is mostly reusing
3186 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3187 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3188 // all subprograms instead of the actual context since subprogram definitions
3189 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003190 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003191 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003192 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003193 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003194 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003195 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003196 // We might get here with a VarDecl in the case we're generating
3197 // code for the initialization of globals. Do not record these decls
3198 // as they will overwrite the actual VarDecl Decl in the cache.
3199 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003200 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003201
Adrian Prantlbebb8932014-03-21 21:01:58 +00003202 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003203 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003204
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003206 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003207}
3208
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003209void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3210 QualType FnType) {
3211 StringRef Name;
3212 StringRef LinkageName;
3213
3214 const Decl *D = GD.getDecl();
3215 if (!D)
3216 return;
3217
Leny Kholodov80c047d2016-09-06 10:48:04 +00003218 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003219 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003220 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003221 llvm::DINodeArray TParamsArray;
3222 if (isa<FunctionDecl>(D)) {
3223 // If there is a DISubprogram for this function available then use it.
3224 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3225 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003226 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003227 Name = getObjCMethodName(OMD);
3228 Flags |= llvm::DINode::FlagPrototyped;
3229 } else {
3230 llvm_unreachable("not a function or ObjC method");
3231 }
3232 if (!Name.empty() && Name[0] == '\01')
3233 Name = Name.substr(1);
3234
3235 if (D->isImplicit()) {
3236 Flags |= llvm::DINode::FlagArtificial;
3237 // Artificial functions without a location should not silently reuse CurLoc.
3238 if (Loc.isInvalid())
3239 CurLoc = SourceLocation();
3240 }
3241 unsigned LineNo = getLineNumber(Loc);
3242 unsigned ScopeLine = 0;
3243
Adrian Prantle76bda52016-04-15 15:55:45 +00003244 DBuilder.retainType(DBuilder.createFunction(
3245 FDContext, Name, LinkageName, Unit, LineNo,
3246 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3247 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3248 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003249}
3250
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003251void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3252 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3253 // If there is a subprogram for this function available then use it.
3254 auto FI = SPCache.find(FD->getCanonicalDecl());
3255 llvm::DISubprogram *SP = nullptr;
3256 if (FI != SPCache.end())
3257 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
3258 if (!SP)
3259 SP = getFunctionStub(GD);
3260 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3261 LexicalBlockStack.emplace_back(SP);
3262 setInlinedAt(Builder.getCurrentDebugLocation());
3263 EmitLocation(Builder, FD->getLocation());
3264}
3265
3266void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3267 assert(CurInlinedAt && "unbalanced inline scope stack");
3268 EmitFunctionEnd(Builder);
3269 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3270}
3271
David Blaikie835afb22015-01-21 23:08:17 +00003272void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003273 // Update our current location
3274 setLocation(Loc);
3275
Eric Christophere7b87e52014-10-26 23:40:33 +00003276 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3277 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003278
Adrian Prantle83b1302014-01-07 22:05:52 +00003279 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003280 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003281 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003282}
3283
Guy Benyei11169dd2012-12-18 14:30:41 +00003284void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003285 llvm::MDNode *Back = nullptr;
3286 if (!LexicalBlockStack.empty())
3287 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003288 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003289 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003290 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003291}
3292
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003293void CGDebugInfo::AppendAddressSpaceXDeref(
3294 unsigned AddressSpace,
3295 SmallVectorImpl<int64_t> &Expr) const {
3296 Optional<unsigned> DWARFAddressSpace =
3297 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3298 if (!DWARFAddressSpace)
3299 return;
3300
3301 Expr.push_back(llvm::dwarf::DW_OP_constu);
3302 Expr.push_back(DWARFAddressSpace.getValue());
3303 Expr.push_back(llvm::dwarf::DW_OP_swap);
3304 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3305}
3306
Eric Christopher0fdcb312013-05-16 00:52:20 +00003307void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3308 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 // Set our current location.
3310 setLocation(Loc);
3311
Guy Benyei11169dd2012-12-18 14:30:41 +00003312 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003313 Builder.SetCurrentDebugLocation(
3314 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3315 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003316
Benjamin Kramer8c305922016-02-02 11:06:51 +00003317 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003318 return;
3319
3320 // Create a new lexical block and push it on the stack.
3321 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003322}
3323
Eric Christopher0fdcb312013-05-16 00:52:20 +00003324void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3325 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003326 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3327
3328 // Provide an entry in the line table for the end of the block.
3329 EmitLocation(Builder, Loc);
3330
Benjamin Kramer8c305922016-02-02 11:06:51 +00003331 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003332 return;
3333
Guy Benyei11169dd2012-12-18 14:30:41 +00003334 LexicalBlockStack.pop_back();
3335}
3336
Guy Benyei11169dd2012-12-18 14:30:41 +00003337void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3338 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3339 unsigned RCount = FnBeginRegionCount.back();
3340 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3341
3342 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003343 while (LexicalBlockStack.size() != RCount) {
3344 // Provide an entry in the line table for the end of the block.
3345 EmitLocation(Builder, CurLoc);
3346 LexicalBlockStack.pop_back();
3347 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003348 FnBeginRegionCount.pop_back();
3349}
3350
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003351llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003352 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003353
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003354 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003355 QualType FType;
3356 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003357 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003358
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003359 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003360 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003361
3362 FieldOffset = 0;
3363 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3364 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3365 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3366 FType = CGM.getContext().IntTy;
3367 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3368 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3369
3370 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3371 if (HasCopyAndDispose) {
3372 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003373 EltTys.push_back(
3374 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3375 EltTys.push_back(
3376 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003377 }
3378 bool HasByrefExtendedLayout;
3379 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003380 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3381 HasByrefExtendedLayout) &&
3382 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003383 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003384 EltTys.push_back(
3385 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003386 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003387
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3389 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003390 CGM.getTarget().getPointerAlign(0))) {
3391 CharUnits FieldOffsetInBytes =
3392 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003393 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003394 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003395
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 if (NumPaddingBytes.isPositive()) {
3397 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3398 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3399 pad, ArrayType::Normal, 0);
3400 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3401 }
3402 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003403
Guy Benyei11169dd2012-12-18 14:30:41 +00003404 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003405 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003406 FieldSize = CGM.getContext().getTypeSize(FType);
3407 FieldAlign = CGM.getContext().toBits(Align);
3408
Eric Christopherb2a008c2013-05-16 00:45:12 +00003409 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003410 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003411 FieldAlign, FieldOffset,
3412 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003413 EltTys.push_back(FieldTy);
3414 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003415
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003416 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003417
Leny Kholodov80c047d2016-09-06 10:48:04 +00003418 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003419
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003421 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003422}
3423
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003424void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3425 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003426 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003427 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003429 if (VD->hasAttr<NoDebugAttr>())
3430 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003431
David Blaikie7fceebf2013-08-19 03:37:48 +00003432 bool Unwritten =
3433 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3434 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003435 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003436 if (!Unwritten)
3437 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003439 uint64_t XOffset = 0;
3440 if (VD->hasAttr<BlocksAttr>())
3441 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003442 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003443 Ty = getOrCreateType(VD->getType(), Unit);
3444
3445 // If there is no debug info for this type then do not emit debug info
3446 // for this variable.
3447 if (!Ty)
3448 return;
3449
Guy Benyei11169dd2012-12-18 14:30:41 +00003450 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003451 unsigned Line = 0;
3452 unsigned Column = 0;
3453 if (!Unwritten) {
3454 Line = getLineNumber(VD->getLocation());
3455 Column = getColumnNumber(VD->getLocation());
3456 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003457 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003458 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003459 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003460 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003461
3462 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3463
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003464 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3465 AppendAddressSpaceXDeref(AddressSpace, Expr);
3466
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 // If this is the first argument and it is implicit then
3468 // give it an object pointer flag.
3469 // FIXME: There has to be a better way to do this, but for static
3470 // functions there won't be an implicit param at arg1 and
3471 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003472 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003473 Flags |= llvm::DINode::FlagObjectPointer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003474
Adrian Prantlc3782a12017-04-18 01:22:01 +00003475 // Note: Older versions of clang used to emit byval references with an extra
3476 // DW_OP_deref, because they referenced the IR arg directly instead of
3477 // referencing an alloca. Newer versions of LLVM don't treat allocas
3478 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003479 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003480 StringRef Name = VD->getName();
3481 if (!Name.empty()) {
3482 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003483 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003484 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003485 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 // offset of __forwarding field
3487 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003488 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003489 Expr.push_back(offset.getQuantity());
3490 Expr.push_back(llvm::dwarf::DW_OP_deref);
3491 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003492 // offset of x field
3493 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003494 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003495 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003496 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003497 // If VD is an anonymous union then Storage represents value for
3498 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003499 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003500 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003501 // GDB has trouble finding local variables in anonymous unions, so we emit
3502 // artifical local variables for each of the members.
3503 //
3504 // FIXME: Remove this code as soon as GDB supports this.
3505 // The debug info verifier in LLVM operates based on the assumption that a
3506 // variable has the same size as its storage and we had to disable the check
3507 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003508 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003509 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003510 StringRef FieldName = Field->getName();
3511
3512 // Ignore unnamed fields. Do not ignore unnamed records.
3513 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3514 continue;
3515
3516 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003517 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003518 auto *D = DBuilder.createAutoVariable(
3519 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003520 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003521
3522 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003523 DBuilder.insertDeclare(
3524 Storage, D, DBuilder.createExpression(Expr),
3525 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3526 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003527 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003528 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003529 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003530
3531 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003532 auto *D = ArgNo
3533 ? DBuilder.createParameterVariable(
3534 Scope, Name, *ArgNo, Unit, Line, Ty,
3535 CGM.getLangOpts().Optimize, Flags)
3536 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3537 CGM.getLangOpts().Optimize, Flags,
3538 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003539
3540 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003541 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003542 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003543 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003544}
3545
3546void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3547 llvm::Value *Storage,
3548 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003549 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003550 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003551}
3552
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003553llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3554 llvm::DIType *Ty) {
3555 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003556 if (CachedTy)
3557 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003558 return DBuilder.createObjectPointerType(Ty);
3559}
3560
Eric Christophere7b87e52014-10-26 23:40:33 +00003561void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3562 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003563 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003564 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003565 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003566
Craig Topper8a13c412014-05-21 05:09:00 +00003567 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003568 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003569 if (VD->hasAttr<NoDebugAttr>())
3570 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003571
Guy Benyei11169dd2012-12-18 14:30:41 +00003572 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003573
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003575 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3576 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 if (isByRef)
3578 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003579 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003580 Ty = getOrCreateType(VD->getType(), Unit);
3581
3582 // Self is passed along as an implicit non-arg variable in a
3583 // block. Mark it as the object pointer.
3584 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003585 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003586
3587 // Get location information.
3588 unsigned Line = getLineNumber(VD->getLocation());
3589 unsigned Column = getColumnNumber(VD->getLocation());
3590
3591 const llvm::DataLayout &target = CGM.getDataLayout();
3592
3593 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003594 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3596
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003597 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003598 addr.push_back(llvm::dwarf::DW_OP_deref);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003599 addr.push_back(llvm::dwarf::DW_OP_plus);
3600 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003601 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003602 addr.push_back(llvm::dwarf::DW_OP_deref);
3603 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003604 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003605 offset =
3606 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003607 addr.push_back(offset.getQuantity());
3608 addr.push_back(llvm::dwarf::DW_OP_deref);
3609 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003610 // offset of x field
3611 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003612 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003613 }
3614
3615 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003616 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003617 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003618 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003619 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003620
Guy Benyei11169dd2012-12-18 14:30:41 +00003621 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003622 auto DL =
3623 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003624 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003625 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003626 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003627 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003628 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003629}
3630
Guy Benyei11169dd2012-12-18 14:30:41 +00003631void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3632 unsigned ArgNo,
3633 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003634 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003635 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003636}
3637
3638namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003639struct BlockLayoutChunk {
3640 uint64_t OffsetInBits;
3641 const BlockDecl::Capture *Capture;
3642};
3643bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3644 return l.OffsetInBits < r.OffsetInBits;
3645}
Guy Benyei11169dd2012-12-18 14:30:41 +00003646}
3647
3648void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003649 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003650 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003651 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003652 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003653 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003654 ASTContext &C = CGM.getContext();
3655 const BlockDecl *blockDecl = block.getBlockDecl();
3656
3657 // Collect some general information about the block's location.
3658 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003659 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003660 unsigned line = getLineNumber(loc);
3661 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003662
Guy Benyei11169dd2012-12-18 14:30:41 +00003663 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003664 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003665
3666 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003667 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003668
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003669 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003670 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003671 blockLayout->getElementOffsetInBits(0),
3672 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003673 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003674 blockLayout->getElementOffsetInBits(1),
3675 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003676 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003677 blockLayout->getElementOffsetInBits(2),
3678 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003679 auto *FnTy = block.getBlockExpr()->getFunctionType();
3680 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003681 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003682 blockLayout->getElementOffsetInBits(3),
3683 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003684 fields.push_back(createFieldType(
3685 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3686 ? C.getBlockDescriptorExtendedType()
3687 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003688 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003689
3690 // We want to sort the captures by offset, not because DWARF
3691 // requires this, but because we're paranoid about debuggers.
3692 SmallVector<BlockLayoutChunk, 8> chunks;
3693
3694 // 'this' capture.
3695 if (blockDecl->capturesCXXThis()) {
3696 BlockLayoutChunk chunk;
3697 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003698 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003699 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003700 chunks.push_back(chunk);
3701 }
3702
3703 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003704 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003705 const VarDecl *variable = capture.getVariable();
3706 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3707
3708 // Ignore constant captures.
3709 if (captureInfo.isConstant())
3710 continue;
3711
3712 BlockLayoutChunk chunk;
3713 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003714 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003715 chunk.Capture = &capture;
3716 chunks.push_back(chunk);
3717 }
3718
3719 // Sort by offset.
3720 llvm::array_pod_sort(chunks.begin(), chunks.end());
3721
David Majnemer58ed0f32016-07-17 00:39:12 +00003722 for (const BlockLayoutChunk &Chunk : chunks) {
3723 uint64_t offsetInBits = Chunk.OffsetInBits;
3724 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003725
3726 // If we have a null capture, this must be the C++ 'this' capture.
3727 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003728 QualType type;
3729 if (auto *Method =
3730 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3731 type = Method->getThisType(C);
3732 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3733 type = QualType(RDecl->getTypeForDecl(), 0);
3734 else
3735 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003736
David Majnemerb4b671e2016-06-30 03:01:59 +00003737 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003738 offsetInBits, tunit, tunit));
3739 continue;
3740 }
3741
3742 const VarDecl *variable = capture->getVariable();
3743 StringRef name = variable->getName();
3744
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003745 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003746 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003747 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003748 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003749
3750 // FIXME: this creates a second copy of this type!
3751 uint64_t xoffset;
3752 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003753 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003754 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3755 PtrInfo.Width, Align, offsetInBits,
3756 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003757 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003758 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003759 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003760 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003761 }
3762 fields.push_back(fieldType);
3763 }
3764
3765 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003766 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3767 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003768
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003769 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003770
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003771 llvm::DIType *type =
3772 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003773 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003774 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003775 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3776
3777 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003778 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003779 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003780
3781 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003782 auto *debugVar = DBuilder.createParameterVariable(
3783 scope, Arg->getName(), ArgNo, tunit, line, type,
3784 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003785
Adrian Prantl616bef42013-03-14 21:52:59 +00003786 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003787 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003788 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003789 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003790 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3791 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003792 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003793
Adrian Prantl616bef42013-03-14 21:52:59 +00003794 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003795 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003796 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003797 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003798}
3799
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003800llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003801CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3802 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003803 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003804
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003805 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003806 if (MI != StaticDataMemberCache.end()) {
3807 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003808 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003809 }
David Blaikiece763042013-08-20 21:49:21 +00003810
3811 // If the member wasn't found in the cache, lazily construct and add it to the
3812 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003813 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003814 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003815 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003816}
3817
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003818llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003819 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3820 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003821 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003822
3823 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003824 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003825 StringRef FieldName = Field->getName();
3826
3827 // Ignore unnamed fields, but recurse into anonymous records.
3828 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003829 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003830 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003831 Var, DContext);
3832 continue;
3833 }
3834 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003835 GVE = DBuilder.createGlobalVariableExpression(
3836 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3837 Var->hasLocalLinkage());
3838 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003839 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003840 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003841}
3842
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003843void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003844 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003845 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003846 if (D->hasAttr<NoDebugAttr>())
3847 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003848
3849 // If we already created a DIGlobalVariable for this declaration, just attach
3850 // it to the llvm::GlobalVariable.
3851 auto Cached = DeclCache.find(D->getCanonicalDecl());
3852 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003853 return Var->addDebugInfo(
3854 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003855
Guy Benyei11169dd2012-12-18 14:30:41 +00003856 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003857 llvm::DIFile *Unit = nullptr;
3858 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003859 unsigned LineNo;
3860 StringRef DeclName, LinkageName;
3861 QualType T;
3862 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003863
3864 // Attempt to store one global variable for the declaration - even if we
3865 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003866 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003867
3868 // If this is an anonymous union then we'll want to emit a global
3869 // variable for each member of the anonymous union so that it's possible
3870 // to find the name of any field in the union.
3871 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003872 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003873 assert(RD->isAnonymousStructOrUnion() &&
3874 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003875 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003876 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003877 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003878
3879 SmallVector<int64_t, 4> Expr;
3880 unsigned AddressSpace =
3881 CGM.getContext().getTargetAddressSpace(D->getType());
3882 AppendAddressSpaceXDeref(AddressSpace, Expr);
3883
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003884 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003885 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003886 Var->hasLocalLinkage(),
3887 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00003888 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003889 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003890 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003891 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003892}
3893
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003894void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003895 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003896 if (VD->hasAttr<NoDebugAttr>())
3897 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003898 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003899 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003900 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003901 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003902 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003903 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3904 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003905 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3906 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3907 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003908 // Do not use global variables for enums.
3909 //
3910 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003911 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003912 return;
David Blaikiea15565562014-04-04 20:56:17 +00003913 // Do not emit separate definitions for function local const/statics.
3914 if (isa<FunctionDecl>(VD->getDeclContext()))
3915 return;
David Blaikiebb113912014-04-05 07:23:17 +00003916 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003917 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003918 if (VarD->isStaticDataMember()) {
3919 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003920 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003921 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003922 //
3923 // FIXME: This is probably unnecessary, since Ty should reference RD
3924 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003925 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003926 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003927 return;
3928 }
3929
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003930 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003931
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003932 auto &GV = DeclCache[VD];
3933 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003934 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003935 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003936 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3937 // FIXME: Add a representation for integer constants wider than 64 bits.
3938 if (Init.isInt())
3939 InitExpr =
3940 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3941 else if (Init.isFloat())
3942 InitExpr = DBuilder.createConstantValueExpression(
3943 Init.getFloat().bitcastToAPInt().getZExtValue());
3944 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003945 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003946 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003947 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3948 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003949}
3950
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003951llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003952 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003953 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003954 llvm::DIScope *Mod = getParentModuleOrNull(D);
3955 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003956}
3957
David Blaikie9f88fe82013-04-22 06:13:21 +00003958void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003959 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003960 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003961 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003962 if (!NSDecl->isAnonymousNamespace() ||
3963 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003964 DBuilder.createImportedModule(
3965 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantld8870552017-05-11 22:59:19 +00003966 getOrCreateNamespace(NSDecl, getParentModuleOrNull(&UD)),
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003967 getLineNumber(UD.getLocation()));
3968 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003969}
3970
David Blaikiebd483762013-05-20 04:58:53 +00003971void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003972 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003973 return;
3974 assert(UD.shadow_size() &&
3975 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3976 // Emitting one decl is sufficient - debuggers can detect that this is an
3977 // overloaded name & provide lookup for all the overloads.
3978 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003979
3980 // FIXME: Skip functions with undeduced auto return type for now since we
3981 // don't currently have the plumbing for separate declarations & definitions
3982 // of free functions and mismatched types (auto in the declaration, concrete
3983 // return type in the definition)
3984 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3985 if (const auto *AT =
3986 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3987 if (AT->getDeducedType().isNull())
3988 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003989 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003990 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003991 DBuilder.createImportedDeclaration(
3992 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3993 getLineNumber(USD.getLocation()));
3994}
3995
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003996void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003997 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3998 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003999 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004000 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00004001 DBuilder.createImportedDeclaration(
4002 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
4003 getOrCreateModuleRef(Info, DebugTypeExtRefs),
4004 getLineNumber(ID.getLocation()));
4005 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004006}
4007
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004008llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004009CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004010 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004011 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004012 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004013 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004014 return cast<llvm::DIImportedEntity>(VH);
4015 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00004016 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004017 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4018 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004019 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004020 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
4021 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
4022 NA.getName());
4023 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004024 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004025 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantld8870552017-05-11 22:59:19 +00004026 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace()),
4027 getParentModuleOrNull(&NA)),
David Blaikief121b932013-05-20 22:50:41 +00004028 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004029 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004030 return R;
4031}
4032
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004033llvm::DINamespace *
Adrian Prantld8870552017-05-11 22:59:19 +00004034CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl,
4035 llvm::DIModule *ParentModule) {
David Blaikie9fdedec2013-08-16 22:52:07 +00004036 NSDecl = NSDecl->getCanonicalDecl();
Adrian Prantld8870552017-05-11 22:59:19 +00004037 // The AST merges NamespaceDecls, but for module debug info it is important to
4038 // put a namespace decl (or rather its children) into the correct
4039 // (sub-)module, so use the parent module of the decl that triggered this
4040 // namespace to be serialized as a second key.
4041 NamespaceKey Key = {NSDecl, ParentModule};
4042 auto I = NamespaceCache.find(Key);
4043 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004044 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004045
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004046 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004047 // Don't trust the context if it is a DIModule (see comment above).
4048 llvm::DINamespace *NS = DBuilder.createNameSpace(
4049 isa<llvm::DIModule>(Context) ? ParentModule : Context, NSDecl->getName(),
4050 NSDecl->isInline());
4051 NamespaceCache[Key].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004052 return NS;
4053}
4054
Adrian Prantla5206ce2015-09-22 23:26:43 +00004055void CGDebugInfo::setDwoId(uint64_t Signature) {
4056 assert(TheCU && "no main compile unit");
4057 TheCU->setDWOId(Signature);
4058}
4059
4060
Guy Benyei11169dd2012-12-18 14:30:41 +00004061void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004062 // Creating types might create further types - invalidating the current
4063 // element and the size(), so don't cache/reference them.
4064 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4065 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004066 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004067 ? CreateTypeDefinition(E.Type, E.Unit)
4068 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004069 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004070 }
4071
David Blaikief427b002014-05-06 03:42:01 +00004072 for (auto p : ReplaceMap) {
4073 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004074 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004075 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004076
David Blaikief427b002014-05-06 03:42:01 +00004077 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004078 assert(it != TypeCache.end());
4079 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004080
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004081 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4082 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004083 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004084
Frederic Rissd253ed62014-11-18 03:40:51 +00004085 for (const auto &p : FwdDeclReplaceMap) {
4086 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004087 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004088 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004089
4090 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004091 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004092 // with ourselves, that will destroy the temporary MDNode and
4093 // replace it with a standard one, avoiding leaking memory.
4094 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004095 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004096 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004097 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004098
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004099 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4100 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004101 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004102 }
4103
Adrian Prantl73409ce2013-03-11 18:33:46 +00004104 // We keep our own list of retained types, because we need to look
4105 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004106 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004107 if (auto MD = TypeCache[RT])
4108 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004109
Guy Benyei11169dd2012-12-18 14:30:41 +00004110 DBuilder.finalize();
4111}
David Blaikie66088d52014-09-24 17:01:27 +00004112
4113void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004114 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004115 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004116
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004117 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004118 // Don't ignore in case of explicit cast where it is referenced indirectly.
4119 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004120}
Amara Emerson652795d2016-11-10 14:44:30 +00004121
4122llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4123 if (LexicalBlockStack.empty())
4124 return llvm::DebugLoc();
4125
4126 llvm::MDNode *Scope = LexicalBlockStack.back();
4127 return llvm::DebugLoc::get(
4128 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4129}