blob: cca0b51a0ffd50c64c42c521ef036c3c93bb28f0 [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"
John McCallde0fe072017-08-15 21:42:52 +000021#include "ConstantEmitter.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/RecordLayout.h"
28#include "clang/Basic/FileManager.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000031#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000032#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000033#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000034#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000035#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000036#include "llvm/ADT/SmallVector.h"
37#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000038#include "llvm/IR/Constants.h"
39#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/DerivedTypes.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000044#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000045#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000046#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000047using namespace clang;
48using namespace clang::CodeGen;
49
Victor Leschuka7ece032016-10-20 00:13:19 +000050static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
51 auto TI = Ctx.getTypeInfo(Ty);
52 return TI.AlignIsRequired ? TI.Align : 0;
53}
54
55static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
56 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
57}
58
59static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
60 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
61}
62
Guy Benyei11169dd2012-12-18 14:30:41 +000063CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000064 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000065 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000066 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000067 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
68 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000069 CreateCompileUnit();
70}
71
72CGDebugInfo::~CGDebugInfo() {
73 assert(LexicalBlockStack.empty() &&
74 "Region stack mismatch, stack not empty!");
75}
76
David Blaikie66e41972015-01-14 07:38:27 +000077ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000078 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000079 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000080 init(TemporaryLocation);
81}
82
Adrian Prantl39428e72015-02-03 18:40:42 +000083ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000084 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000085 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000086 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000087 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000088}
89
90void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000091 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000092 auto *DI = CGF->getDebugInfo();
93 if (!DI) {
94 CGF = nullptr;
95 return;
David Blaikie66e41972015-01-14 07:38:27 +000096 }
David Blaikied7057d92015-08-12 23:49:57 +000097
98 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
99 if (TemporaryLocation.isValid()) {
100 DI->EmitLocation(CGF->Builder, TemporaryLocation);
101 return;
102 }
103
104 if (DefaultToEmpty) {
105 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
106 return;
107 }
108
109 // Construct a location that has a valid scope, but no line info.
110 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000111 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
112 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000113}
114
David Blaikie9b479662015-01-25 01:19:10 +0000115ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000116 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000117 init(E->getExprLoc());
118}
119
David Blaikie66e41972015-01-14 07:38:27 +0000120ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000121 : CGF(&CGF) {
122 if (!CGF.getDebugInfo()) {
123 this->CGF = nullptr;
124 return;
David Blaikie66e41972015-01-14 07:38:27 +0000125 }
David Blaikied7057d92015-08-12 23:49:57 +0000126 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
127 if (Loc)
128 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000129}
130
131ApplyDebugLocation::~ApplyDebugLocation() {
132 // Query CGF so the location isn't overwritten when location updates are
133 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000134 if (CGF)
135 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000136}
137
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000138ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
139 GlobalDecl InlinedFn)
140 : CGF(&CGF) {
141 if (!CGF.getDebugInfo()) {
142 this->CGF = nullptr;
143 return;
144 }
145 auto &DI = *CGF.getDebugInfo();
146 SavedLocation = DI.getLocation();
147 assert((DI.getInlinedAt() ==
148 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
149 "CGDebugInfo and IRBuilder are out of sync");
150
151 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
152}
153
154ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
155 if (!CGF)
156 return;
157 auto &DI = *CGF->getDebugInfo();
158 DI.EmitInlineFunctionEnd(CGF->Builder);
159 DI.EmitLocation(CGF->Builder, SavedLocation);
160}
161
Guy Benyei11169dd2012-12-18 14:30:41 +0000162void CGDebugInfo::setLocation(SourceLocation Loc) {
163 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000164 if (Loc.isInvalid())
165 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000166
167 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
168
169 // If we've changed files in the middle of a lexical scope go ahead
170 // and create a new lexical scope with file node if it's different
171 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000172 if (LexicalBlockStack.empty())
173 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000174
175 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000176 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000177 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000178
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000179 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000180 return;
181
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000182 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000184 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
185 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000186 } else if (isa<llvm::DILexicalBlock>(Scope) ||
187 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000188 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000189 LexicalBlockStack.emplace_back(
190 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000191 }
192}
193
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000194llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000195 llvm::DIScope *Mod = getParentModuleOrNull(D);
196 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
197 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000198}
199
200llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
201 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000202 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000203 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000204
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000205 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000206 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000207 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000208 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000209 }
210
211 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000212 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
213 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000214
David Majnemer58ed0f32016-07-17 00:39:12 +0000215 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000216 if (!RDecl->isDependentType())
217 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000218 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000219 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000220}
221
Reid Kleckner59d12202017-08-08 01:33:53 +0000222PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
223 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
224
225 // If we're emitting codeview, it's important to try to match MSVC's naming so
226 // that visualizers written for MSVC will trigger for our class names. In
227 // particular, we can't have spaces between arguments of standard templates
228 // like basic_string and vector.
229 if (CGM.getCodeGenOpts().EmitCodeView)
230 PP.MSVCFormatting = true;
231
232 return PP;
233}
234
Guy Benyei11169dd2012-12-18 14:30:41 +0000235StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000236 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000237 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000238 FunctionTemplateSpecializationInfo *Info =
239 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000240
Reid Kleckner31abd802016-06-30 17:41:31 +0000241 // Emit the unqualified name in normal operation. LLVM and the debugger can
242 // compute the fully qualified name from the scope chain. If we're only
243 // emitting line table info, there won't be any scope chains, so emit the
244 // fully qualified name here so that stack traces are more accurate.
245 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
246 // evaluating the size impact.
247 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
248 CGM.getCodeGenOpts().EmitCodeView;
249
250 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000251 return FII->getName();
252
Benjamin Kramer9170e912013-02-22 15:46:01 +0000253 SmallString<128> NS;
254 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000255 if (!UseQualifiedName)
256 FD->printName(OS);
257 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000258 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
Reid Kleckner829398e2016-06-17 16:11:20 +0000260 // Add any template specialization args.
261 if (Info) {
262 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000263 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner59d12202017-08-08 01:33:53 +0000264 getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000265 }
266
267 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000268 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269}
270
271StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
272 SmallString<256> MethodName;
273 llvm::raw_svector_ostream OS(MethodName);
274 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
275 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000276 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000277 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000278 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000279 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000280 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000281 if (OC->IsClassExtension()) {
282 OS << OC->getClassInterface()->getName();
283 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000284 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000285 << OC->getIdentifier()->getNameStart() << ')';
286 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000287 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000288 OS << OCD->getClassInterface()->getName() << '('
289 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000290 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000291 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000292 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000293 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000294 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000295 ClassTy.print(OS, PrintingPolicy(LangOptions()));
296 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000297 }
298 OS << ' ' << OMD->getSelector().getAsString() << ']';
299
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000300 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000301}
302
Guy Benyei11169dd2012-12-18 14:30:41 +0000303StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000304 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
Eric Christophere7b87e52014-10-26 23:40:33 +0000307StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000308 if (isa<ClassTemplateSpecializationDecl>(RD)) {
309 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000310 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000311 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000312 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000313
314 // Copy this name on the side and use its reference.
315 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000316 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000317
David Majnemerbc5976a2016-07-01 23:12:54 +0000318 // quick optimization to avoid having to intern strings that are already
319 // stored reliably elsewhere
320 if (const IdentifierInfo *II = RD->getIdentifier())
321 return II->getName();
322
323 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
324 // used to reconstruct the fully qualified type names.
325 if (CGM.getCodeGenOpts().EmitCodeView) {
326 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
327 assert(RD->getDeclContext() == D->getDeclContext() &&
328 "Typedef should not be in another decl context!");
329 assert(D->getDeclName().getAsIdentifierInfo() &&
330 "Typedef was not named!");
331 return D->getDeclName().getAsIdentifierInfo()->getName();
332 }
333
334 if (CGM.getLangOpts().CPlusPlus) {
335 StringRef Name;
336
337 ASTContext &Context = CGM.getContext();
338 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
339 // Anonymous types without a name for linkage purposes have their
340 // declarator mangled in if they have one.
341 Name = DD->getName();
342 else if (const TypedefNameDecl *TND =
343 Context.getTypedefNameForUnnamedTagDecl(RD))
344 // Anonymous types without a name for linkage purposes have their
345 // associate typedef mangled in if they have one.
346 Name = TND->getName();
347
348 if (!Name.empty()) {
349 SmallString<256> UnnamedType("<unnamed-type-");
350 UnnamedType += Name;
351 UnnamedType += '>';
352 return internString(UnnamedType);
353 }
354 }
355 }
356
357 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000358}
359
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000360llvm::DIFile::ChecksumKind
361CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
362 Checksum.clear();
363
364 if (!CGM.getCodeGenOpts().EmitCodeView)
365 return llvm::DIFile::CSK_None;
366
367 SourceManager &SM = CGM.getContext().getSourceManager();
368 bool Invalid;
369 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
370 if (Invalid)
371 return llvm::DIFile::CSK_None;
372
373 llvm::MD5 Hash;
374 llvm::MD5::MD5Result Result;
375
376 Hash.update(MemBuffer->getBuffer());
377 Hash.final(Result);
378
379 Hash.stringifyResult(Result, Checksum);
380 return llvm::DIFile::CSK_MD5;
381}
382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000383llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 if (!Loc.isValid())
385 // If 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 SourceManager &SM = CGM.getContext().getSourceManager();
392 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
393
394 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
395 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000396 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000397 remapDIPath(TheCU->getDirectory()),
398 TheCU->getFile()->getChecksumKind(),
399 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000400
401 // Cache the results.
402 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000403 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000404
405 if (it != DIFileCache.end()) {
406 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000407 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000408 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000409 }
410
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000411 SmallString<32> Checksum;
412 llvm::DIFile::ChecksumKind CSKind =
413 computeChecksum(SM.getFileID(Loc), Checksum);
414
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000415 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000416 remapDIPath(getCurrentDirname()),
417 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000418
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000419 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000420 return F;
421}
422
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000423llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000424 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000425 remapDIPath(TheCU->getDirectory()),
426 TheCU->getFile()->getChecksumKind(),
427 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000428}
429
430std::string CGDebugInfo::remapDIPath(StringRef Path) const {
431 for (const auto &Entry : DebugPrefixMap)
432 if (Path.startswith(Entry.first))
433 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
434 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000435}
436
Guy Benyei11169dd2012-12-18 14:30:41 +0000437unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
438 if (Loc.isInvalid() && CurLoc.isInvalid())
439 return 0;
440 SourceManager &SM = CGM.getContext().getSourceManager();
441 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000442 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000443}
444
Adrian Prantlc7822422013-03-12 20:43:25 +0000445unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000446 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000447 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000448 return 0;
449
450 // If the location is invalid then use the current column.
451 if (Loc.isInvalid() && CurLoc.isInvalid())
452 return 0;
453 SourceManager &SM = CGM.getContext().getSourceManager();
454 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000455 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000456}
457
458StringRef CGDebugInfo::getCurrentDirname() {
459 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
460 return CGM.getCodeGenOpts().DebugCompilationDir;
461
462 if (!CWDName.empty())
463 return CWDName;
464 SmallString<256> CWD;
465 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000466 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000467}
468
Guy Benyei11169dd2012-12-18 14:30:41 +0000469void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000470 SmallString<32> Checksum;
471 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472
David Blaikieaabde052014-05-14 00:29:00 +0000473 // Should we be asking the SourceManager for the main file name, instead of
474 // accepting it as an argument? This just causes the main file name to
475 // mismatch with source locations and create extra lexical scopes or
476 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
477 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
478 // because that's what the SourceManager says)
479
Guy Benyei11169dd2012-12-18 14:30:41 +0000480 // Get absolute path name.
481 SourceManager &SM = CGM.getContext().getSourceManager();
482 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
483 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000484 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000485
486 // The main file name provided via the "-main-file-name" option contains just
487 // the file name itself with no path information. This file name may have had
488 // a relative path, so we look into the actual file entry for the main
489 // file to determine the real absolute path for the file.
490 std::string MainFileDir;
491 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000492 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000493 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000494 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
495 llvm::sys::path::append(MainFileDirSS, MainFileName);
496 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000497 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000498 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000499 }
500
Ed Masteda706022014-05-07 12:49:30 +0000501 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 const LangOptions &LO = CGM.getLangOpts();
503 if (LO.CPlusPlus) {
504 if (LO.ObjC1)
505 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
506 else
507 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
508 } else if (LO.ObjC1) {
509 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000510 } else if (LO.RenderScript) {
511 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000512 } else if (LO.C99) {
513 LangTag = llvm::dwarf::DW_LANG_C99;
514 } else {
515 LangTag = llvm::dwarf::DW_LANG_C89;
516 }
517
518 std::string Producer = getClangFullVersion();
519
520 // Figure out which version of the ObjC runtime we have.
521 unsigned RuntimeVers = 0;
522 if (LO.ObjC1)
523 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
524
Adrian Prantl826824e2016-04-08 22:43:06 +0000525 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
526 switch (DebugKind) {
527 case codegenoptions::NoDebugInfo:
528 case codegenoptions::LocTrackingOnly:
529 EmissionKind = llvm::DICompileUnit::NoDebug;
530 break;
531 case codegenoptions::DebugLineTablesOnly:
532 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
533 break;
534 case codegenoptions::LimitedDebugInfo:
535 case codegenoptions::FullDebugInfo:
536 EmissionKind = llvm::DICompileUnit::FullDebug;
537 break;
538 }
539
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000541 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000542 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000543 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000544 LangTag,
545 DBuilder.createFile(remapDIPath(MainFileName),
546 remapDIPath(getCurrentDirname()), CSKind, Checksum),
Adrian Prantlb4423022017-08-04 23:08:57 +0000547 Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
548 CGOpts.DwarfDebugFlags, RuntimeVers,
549 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
550 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000551}
552
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000553llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000554 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 StringRef BTName;
556 switch (BT->getKind()) {
557#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000558#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000559#include "clang/AST/BuiltinTypes.def"
560 case BuiltinType::Dependent:
561 llvm_unreachable("Unexpected builtin type");
562 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000563 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000564 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000565 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000567 if (!ClassTy)
568 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
569 "objc_class", TheCU,
570 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 return ClassTy;
572 case BuiltinType::ObjCId: {
573 // typedef struct objc_class *Class;
574 // typedef struct objc_object {
575 // Class isa;
576 // } *id;
577
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000578 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 return ObjTy;
580
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000581 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
583 "objc_class", TheCU,
584 getOrCreateMainFile(), 0);
585
586 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000587
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000588 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000589
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000590 ObjTy = DBuilder.createStructType(
591 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
592 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000593
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000594 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000595 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
596 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
597 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000598 return ObjTy;
599 }
600 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000601 if (!SelTy)
602 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
603 "objc_selector", TheCU,
604 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000605 return SelTy;
606 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000607
Alexey Bader954ba212016-04-08 13:40:33 +0000608#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
609 case BuiltinType::Id: \
610 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
611 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000612#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000613 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000614 return getOrCreateStructPtrType("opencl_sampler_t",
615 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000616 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000617 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000618 case BuiltinType::OCLClkEvent:
619 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
620 case BuiltinType::OCLQueue:
621 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000622 case BuiltinType::OCLReserveID:
623 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000624
Guy Benyei11169dd2012-12-18 14:30:41 +0000625 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000626 case BuiltinType::Char_U:
627 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
628 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000629 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000630 case BuiltinType::SChar:
631 Encoding = llvm::dwarf::DW_ATE_signed_char;
632 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000634 case BuiltinType::Char32:
635 Encoding = llvm::dwarf::DW_ATE_UTF;
636 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000637 case BuiltinType::UShort:
638 case BuiltinType::UInt:
639 case BuiltinType::UInt128:
640 case BuiltinType::ULong:
641 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000642 case BuiltinType::ULongLong:
643 Encoding = llvm::dwarf::DW_ATE_unsigned;
644 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000645 case BuiltinType::Short:
646 case BuiltinType::Int:
647 case BuiltinType::Int128:
648 case BuiltinType::Long:
649 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000650 case BuiltinType::LongLong:
651 Encoding = llvm::dwarf::DW_ATE_signed;
652 break;
653 case BuiltinType::Bool:
654 Encoding = llvm::dwarf::DW_ATE_boolean;
655 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000656 case BuiltinType::Half:
657 case BuiltinType::Float:
658 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000659 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000660 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000661 // FIXME: For targets where long double and __float128 have the same size,
662 // they are currently indistinguishable in the debugger without some
663 // special treatment. However, there is currently no consensus on encoding
664 // and this should be updated once a DWARF encoding exists for distinct
665 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000666 Encoding = llvm::dwarf::DW_ATE_float;
667 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000668 }
669
670 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000671 case BuiltinType::Long:
672 BTName = "long int";
673 break;
674 case BuiltinType::LongLong:
675 BTName = "long long int";
676 break;
677 case BuiltinType::ULong:
678 BTName = "long unsigned int";
679 break;
680 case BuiltinType::ULongLong:
681 BTName = "long long unsigned int";
682 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 default:
684 BTName = BT->getName(CGM.getLangOpts());
685 break;
686 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000687 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000688 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000689 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000690}
691
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000692llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000693 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000694 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 if (Ty->isComplexIntegerType())
696 Encoding = llvm::dwarf::DW_ATE_lo_user;
697
698 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000699 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000700}
701
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
703 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 QualifierCollector Qc;
705 const Type *T = Qc.strip(Ty);
706
707 // Ignore these qualifiers for now.
708 Qc.removeObjCGCAttr();
709 Qc.removeAddressSpace();
710 Qc.removeObjCLifetime();
711
712 // We will create one Derived type for one qualifier and recurse to handle any
713 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000714 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000715 if (Qc.hasConst()) {
716 Tag = llvm::dwarf::DW_TAG_const_type;
717 Qc.removeConst();
718 } else if (Qc.hasVolatile()) {
719 Tag = llvm::dwarf::DW_TAG_volatile_type;
720 Qc.removeVolatile();
721 } else if (Qc.hasRestrict()) {
722 Tag = llvm::dwarf::DW_TAG_restrict_type;
723 Qc.removeRestrict();
724 } else {
725 assert(Qc.empty() && "Unknown type qualifier for debug info");
726 return getOrCreateType(QualType(T, 0), Unit);
727 }
728
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000729 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000730
731 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
732 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000733 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734}
735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000736llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
737 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000738
739 // The frontend treats 'id' as a typedef to an ObjCObjectType,
740 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
741 // debug info, we want to emit 'id' in both cases.
742 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000743 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000744
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000745 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
746 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000747}
748
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000749llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
750 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000751 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000752 Ty->getPointeeType(), Unit);
753}
754
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000755/// \return whether a C++ mangling exists for the type defined by TD.
756static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
757 switch (TheCU->getSourceLanguage()) {
758 case llvm::dwarf::DW_LANG_C_plus_plus:
759 return true;
760 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
761 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
762 default:
763 return false;
764 }
765}
766
Manman Rene0064d82013-08-29 23:19:58 +0000767/// In C++ mode, types have linkage, so we can rely on the ODR and
768/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000769static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
770 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000771 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000772 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000773 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000774
775 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000776 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000777
Manman Rene0064d82013-08-29 23:19:58 +0000778 // TODO: This is using the RTTI name. Is there a better way to get
779 // a unique string for a type?
780 llvm::raw_svector_ostream Out(FullName);
781 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000782 return FullName;
783}
784
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000785/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000786static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
787 llvm::dwarf::Tag Tag;
788 if (RD->isStruct() || RD->isInterface())
789 Tag = llvm::dwarf::DW_TAG_structure_type;
790 else if (RD->isUnion())
791 Tag = llvm::dwarf::DW_TAG_union_type;
792 else {
793 // FIXME: This could be a struct type giving a default visibility different
794 // than C++ class type, but needs llvm metadata changes first.
795 assert(RD->isClass());
796 Tag = llvm::dwarf::DW_TAG_class_type;
797 }
798 return Tag;
799}
800
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000801llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000802CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000804 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000805 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
806 return cast<llvm::DICompositeType>(T);
807 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 unsigned Line = getLineNumber(RD->getLocation());
809 StringRef RDName = getClassName(RD);
810
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000811 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000812 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000813
Guy Benyei11169dd2012-12-18 14:30:41 +0000814 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000815 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000817 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000818 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000819 ReplaceMap.emplace_back(
820 std::piecewise_construct, std::make_tuple(Ty),
821 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000822 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000823}
824
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000825llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000826 const Type *Ty,
827 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000828 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000829 // Bit size, align and offset of the type.
830 // Size is always the size of a pointer. We can't use getTypeSize here
831 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000832 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
833 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000834 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000835 Optional<unsigned> DWARFAddressSpace =
836 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000837
Keno Fischer87842f32015-11-16 09:04:13 +0000838 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
839 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
840 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000841 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000842 else
843 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000844 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000845}
846
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000847llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
848 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000849 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000850 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000851 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
852 TheCU, getOrCreateMainFile(), 0);
853 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
854 Cache = DBuilder.createPointerType(Cache, Size);
855 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000856}
857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000858llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
859 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000860 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000861 QualType FType;
862 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000863 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000864 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000865
866 FieldOffset = 0;
867 FType = CGM.getContext().UnsignedLongTy;
868 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
869 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
870
871 Elements = DBuilder.getOrCreateArray(EltTys);
872 EltTys.clear();
873
Leny Kholodov80c047d2016-09-06 10:48:04 +0000874 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000875 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000876
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000877 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000878 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000879 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000880
881 // Bit size, align and offset of the type.
882 uint64_t Size = CGM.getContext().getTypeSize(Ty);
883
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000884 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000885
886 FieldOffset = 0;
887 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
888 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
889 FType = CGM.getContext().IntTy;
890 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
891 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000892 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000893 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
894
895 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000896 FieldSize = CGM.getContext().getTypeSize(Ty);
897 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000898 EltTys.push_back(DBuilder.createMemberType(
899 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
900 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000901
902 FieldOffset += FieldSize;
903 Elements = DBuilder.getOrCreateArray(EltTys);
904
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000905 // The __block_literal_generic structs are marked with a special
906 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
907 // the debugger needs to know about. To allow type uniquing, emit
908 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000909 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000910 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000911 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000912
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000913 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000914}
915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000916llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
917 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000918 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000919 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000920
921 SmallString<128> NS;
922 llvm::raw_svector_ostream OS(NS);
Reid Kleckner59d12202017-08-08 01:33:53 +0000923 Ty->getTemplateName().print(OS, getPrintingPolicy(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000924 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000925
926 TemplateSpecializationType::PrintTemplateArgumentList(
Reid Kleckner59d12202017-08-08 01:33:53 +0000927 OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +0000928
David Majnemer58ed0f32016-07-17 00:39:12 +0000929 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000930 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000931
932 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000933 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
934 getLineNumber(Loc),
935 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000936}
937
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000938llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
939 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000940 // We don't set size information, but do specify where the typedef was
941 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000942 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000943
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000944 // Typedefs are derived from some other type.
945 return DBuilder.createTypedef(
946 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
947 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000948 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000949}
950
Reid Klecknerf00f8032016-06-08 20:41:54 +0000951static unsigned getDwarfCC(CallingConv CC) {
952 switch (CC) {
953 case CC_C:
954 // Avoid emitting DW_AT_calling_convention if the C convention was used.
955 return 0;
956
957 case CC_X86StdCall:
958 return llvm::dwarf::DW_CC_BORLAND_stdcall;
959 case CC_X86FastCall:
960 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
961 case CC_X86ThisCall:
962 return llvm::dwarf::DW_CC_BORLAND_thiscall;
963 case CC_X86VectorCall:
964 return llvm::dwarf::DW_CC_LLVM_vectorcall;
965 case CC_X86Pascal:
966 return llvm::dwarf::DW_CC_BORLAND_pascal;
967
968 // FIXME: Create new DW_CC_ codes for these calling conventions.
Martin Storsjo022e7822017-07-17 20:49:45 +0000969 case CC_Win64:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000970 case CC_X86_64SysV:
971 case CC_AAPCS:
972 case CC_AAPCS_VFP:
973 case CC_IntelOclBicc:
974 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000975 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000976 case CC_Swift:
977 case CC_PreserveMost:
978 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000979 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000980 return 0;
981 }
982 return 0;
983}
984
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000985llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
986 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000987 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000988
989 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000990 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000991
992 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000993 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000994 if (isa<FunctionNoProtoType>(Ty))
995 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000996 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
997 for (const QualType &ParamType : FPT->param_types())
998 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000999 if (FPT->isVariadic())
1000 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001001 }
1002
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001003 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001004 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001005 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001006}
1007
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001008/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001009/// As an optimization, return 0 if the access specifier equals the
1010/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001011static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1012 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001013 AccessSpecifier Default = clang::AS_none;
1014 if (RD && RD->isClass())
1015 Default = clang::AS_private;
1016 else if (RD && (RD->isStruct() || RD->isUnion()))
1017 Default = clang::AS_public;
1018
1019 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001020 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001021
Eric Christophere7b87e52014-10-26 23:40:33 +00001022 switch (Access) {
1023 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001024 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001025 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001026 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001027 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001028 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001029 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001030 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001031 }
1032 llvm_unreachable("unexpected access enumerator");
1033}
Guy Benyei11169dd2012-12-18 14:30:41 +00001034
David Majnemerb4b671e2016-06-30 03:01:59 +00001035llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1036 llvm::DIScope *RecordTy,
1037 const RecordDecl *RD) {
1038 StringRef Name = BitFieldDecl->getName();
1039 QualType Ty = BitFieldDecl->getType();
1040 SourceLocation Loc = BitFieldDecl->getLocation();
1041 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1042 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1043
1044 // Get the location for the field.
1045 llvm::DIFile *File = getOrCreateFile(Loc);
1046 unsigned Line = getLineNumber(Loc);
1047
1048 const CGBitFieldInfo &BitFieldInfo =
1049 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1050 uint64_t SizeInBits = BitFieldInfo.Size;
1051 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001052 uint64_t StorageOffsetInBits =
1053 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001054 uint64_t Offset = BitFieldInfo.Offset;
1055 // The bit offsets for big endian machines are reversed for big
1056 // endian target, compensate for that as the DIDerivedType requires
1057 // un-reversed offsets.
1058 if (CGM.getDataLayout().isBigEndian())
1059 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1060 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001061 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001062 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001063 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1064 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001065}
1066
1067llvm::DIType *
1068CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1069 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001070 uint32_t AlignInBits, llvm::DIFile *tunit,
1071 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001072 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001073
1074 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001075 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076 unsigned line = getLineNumber(loc);
1077
David Majnemer34b57492014-07-30 01:30:47 +00001078 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001079 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001080 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001081 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1082 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001083 if (!Align)
1084 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001085 }
1086
Leny Kholodov80c047d2016-09-06 10:48:04 +00001087 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001088 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001089 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001090}
1091
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001092void CGDebugInfo::CollectRecordLambdaFields(
1093 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001094 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001095 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1096 // has the name and the location of the variable so we should iterate over
1097 // both concurrently.
1098 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1099 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1100 unsigned fieldno = 0;
1101 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001102 E = CXXDecl->captures_end();
1103 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001104 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001105 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001106 SourceLocation Loc = C.getLocation();
1107 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001108 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001109 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001110 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001111 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001112 llvm::DIType *FieldType = createFieldType(
1113 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001114 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001115 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001116 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001117 // TODO: Need to handle 'this' in some way by probably renaming the
1118 // this of the lambda class and having a field member of 'this' or
1119 // by using AT_object_pointer for the function and having that be
1120 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001121 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001123 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001124 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001125 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001126 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001127
1128 elements.push_back(fieldType);
1129 }
1130 }
1131}
1132
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001133llvm::DIDerivedType *
1134CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001135 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001136 // Create the descriptor for the static variable, with or without
1137 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001138 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001139 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1140 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001141
Eric Christopher91a31902013-01-16 01:22:32 +00001142 unsigned LineNumber = getLineNumber(Var->getLocation());
1143 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001144 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001145 if (Var->getInit()) {
1146 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001147 if (Value) {
1148 if (Value->isInt())
1149 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1150 if (Value->isFloat())
1151 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1152 }
Eric Christopher91a31902013-01-16 01:22:32 +00001153 }
1154
Leny Kholodov80c047d2016-09-06 10:48:04 +00001155 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001156 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001158 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001159 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001160 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001161}
1162
Eric Christophere7b87e52014-10-26 23:40:33 +00001163void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001164 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1165 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001166 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001167 StringRef name = field->getName();
1168 QualType type = field->getType();
1169
1170 // Ignore unnamed fields unless they're anonymous structs/unions.
1171 if (name.empty() && !type->isRecordType())
1172 return;
1173
David Majnemerb4b671e2016-06-30 03:01:59 +00001174 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001175 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001176 FieldType = createBitFieldType(field, RecordTy, RD);
1177 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001178 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001179 FieldType =
1180 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001181 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001182 }
1183
David Majnemerb4b671e2016-06-30 03:01:59 +00001184 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001185}
1186
Reid Klecknere2e82062017-08-08 20:30:14 +00001187void CGDebugInfo::CollectRecordNestedType(
1188 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1189 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001190 // Injected class names are not considered nested records.
1191 if (isa<InjectedClassNameType>(Ty))
1192 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001193 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001194 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1195 elements.push_back(nestedType);
1196}
1197
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001198void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001199 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001200 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001201 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001202 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001203
Eric Christopher91a31902013-01-16 01:22:32 +00001204 if (CXXDecl && CXXDecl->isLambda())
1205 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1206 else {
1207 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001208
Reid Klecknere2e82062017-08-08 20:30:14 +00001209 // Debug info for nested types is included in the member list only for
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001210 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001211 bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001212
Eric Christopher91a31902013-01-16 01:22:32 +00001213 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001214 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001215
Eric Christopher91a31902013-01-16 01:22:32 +00001216 // Static and non-static members should appear in the same order as
1217 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001218 for (const auto *I : record->decls())
1219 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001220 if (V->hasAttr<NoDebugAttr>())
1221 continue;
David Blaikiece763042013-08-20 21:49:21 +00001222 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001223 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001224 if (MI != StaticDataMemberCache.end()) {
1225 assert(MI->second &&
1226 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001227 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001228 } else {
1229 auto Field = CreateRecordStaticField(V, RecordTy, record);
1230 elements.push_back(Field);
1231 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001232 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001233 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1234 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001235
1236 // Bump field number for next field.
1237 ++fieldNo;
Reid Klecknere2e82062017-08-08 20:30:14 +00001238 } else if (IncludeNestedTypes) {
1239 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1240 if (!nestedType->isImplicit() &&
1241 nestedType->getDeclContext() == record)
1242 CollectRecordNestedType(nestedType, elements);
1243 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 }
1245}
1246
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001247llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001248CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001249 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001250 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001251 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001252 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001253 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001254 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1255 Func, Unit);
1256}
David Blaikie2aaf0652013-01-07 22:24:59 +00001257
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1259 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001261 llvm::DITypeRefArray Args(
1262 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001263 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001264 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001265
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001266 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001267
1268 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001269 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001270
David Blaikie2aaf0652013-01-07 22:24:59 +00001271 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001272 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001273 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1274 // Create pointer type directly in this case.
1275 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1276 QualType PointeeTy = ThisPtrTy->getPointeeType();
1277 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001278 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001279 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001280 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1281 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001282 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001283 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001284 // TODO: This and the artificial type below are misleading, the
1285 // types aren't artificial the argument is, but the current
1286 // metadata doesn't represent that.
1287 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1288 Elts.push_back(ThisPtrType);
1289 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001290 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001291 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001292 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1293 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 }
1295
1296 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001297 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1298 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001300 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001301
Leny Kholodov80c047d2016-09-06 10:48:04 +00001302 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001303 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001304 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001305 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001306 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001307
Reid Klecknerf00f8032016-06-08 20:41:54 +00001308 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1309 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001310}
1311
Eric Christopherb2a008c2013-05-16 00:45:12 +00001312/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001313/// inside a function.
1314static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001315 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 return isFunctionLocalClass(NRD);
1317 if (isa<FunctionDecl>(RD->getDeclContext()))
1318 return true;
1319 return false;
1320}
1321
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001322llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1323 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001324 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001325 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001326
Guy Benyei11169dd2012-12-18 14:30:41 +00001327 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001329
1330 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1331 // make sense to give a single ctor/dtor a linkage name.
1332 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001333 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1334 // property to use here. It may've been intended to model "is non-external
1335 // type" but misses cases of non-function-local but non-external classes such
1336 // as those in anonymous namespaces as well as the reverse - external types
1337 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001338 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1339 MethodLinkageName = CGM.getMangledName(Method);
1340
1341 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001342 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001343 unsigned MethodLine = 0;
1344 if (!Method->isImplicit()) {
1345 MethodDefUnit = getOrCreateFile(Method->getLocation());
1346 MethodLine = getLineNumber(Method->getLocation());
1347 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001348
1349 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001350 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001351 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001353 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001354 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001355
Guy Benyei11169dd2012-12-18 14:30:41 +00001356 if (Method->isVirtual()) {
1357 if (Method->isPure())
1358 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1359 else
1360 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001361
Reid Kleckner216d0a12016-06-16 20:08:51 +00001362 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1363 // It doesn't make sense to give a virtual destructor a vtable index,
1364 // since a single destructor has two entries in the vtable.
1365 if (!isa<CXXDestructorDecl>(Method))
1366 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1367 } else {
1368 // Emit MS ABI vftable information. There is only one entry for the
1369 // deleting dtor.
1370 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1371 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1372 MicrosoftVTableContext::MethodVFTableLocation ML =
1373 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1374 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001375
1376 // CodeView only records the vftable offset in the class that introduces
1377 // the virtual method. This is possible because, unlike Itanium, the MS
1378 // C++ ABI does not include all virtual methods from non-primary bases in
1379 // the vtable for the most derived class. For example, if C inherits from
1380 // A and B, C's primary vftable will not include B's virtual methods.
1381 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1382 Flags |= llvm::DINode::FlagIntroducedVirtual;
1383
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001384 // The 'this' adjustment accounts for both the virtual and non-virtual
1385 // portions of the adjustment. Presumably the debugger only uses it when
1386 // it knows the dynamic type of an object.
1387 ThisAdjustment = CGM.getCXXABI()
1388 .getVirtualFunctionPrologueThisAdjustment(GD)
1389 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001390 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001391 ContainingType = RecordTy;
1392 }
1393
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001396 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001397 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001398 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001400 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001402 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 }
1404 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001405 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001406 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001407 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001408 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001409 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001411 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1412 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001413 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001414 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1415 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1416 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001417
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001418 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001419
1420 return SP;
1421}
1422
Eric Christophere7b87e52014-10-26 23:40:33 +00001423void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1425 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001426
1427 // Since we want more than just the individual member decls if we
1428 // have templated functions iterate over every declaration to gather
1429 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001430 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001431 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1432 // If the member is implicit, don't add it to the member list. This avoids
1433 // the member being added to type units by LLVM, while still allowing it
1434 // to be emitted into the type declaration/reference inside the compile
1435 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001436 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001437 // FIXME: Handle Using(Shadow?)Decls here to create
1438 // DW_TAG_imported_declarations inside the class for base decls brought into
1439 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1440 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1441 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001442 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001443 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001444
1445 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1446 continue;
1447
David Blaikiefd580722014-10-06 05:18:55 +00001448 // Reuse the existing member function declaration if it exists.
1449 // It may be associated with the declaration of the type & should be
1450 // reused as we're building the definition.
1451 //
1452 // This situation can arise in the vtable-based debug info reduction where
1453 // implicit members are emitted in a non-vtable TU.
1454 auto MI = SPCache.find(Method->getCanonicalDecl());
1455 EltTys.push_back(MI == SPCache.end()
1456 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001457 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001458 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001459}
Guy Benyei11169dd2012-12-18 14:30:41 +00001460
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001461void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001462 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001463 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001464 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1465 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1466 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001467
Bob Haarmandff36732016-10-25 22:19:32 +00001468 // If we are generating CodeView debug info, we also need to emit records for
1469 // indirect virtual base classes.
1470 if (CGM.getCodeGenOpts().EmitCodeView) {
1471 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1472 llvm::DINode::FlagIndirectVirtualBase);
1473 }
1474}
1475
1476void CGDebugInfo::CollectCXXBasesAux(
1477 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1478 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1479 const CXXRecordDecl::base_class_const_range &Bases,
1480 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1481 llvm::DINode::DIFlags StartingFlags) {
1482 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1483 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001484 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001485 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001486 if (!SeenTypes.insert(Base).second)
1487 continue;
1488 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1489 llvm::DINode::DIFlags BFlags = StartingFlags;
1490 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001491
Aaron Ballman574705e2014-03-13 15:41:46 +00001492 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001493 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1494 // virtual base offset offset is -ve. The code generator emits dwarf
1495 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001496 BaseOffset = 0 - CGM.getItaniumVTableContext()
1497 .getVirtualBaseOffsetOffset(RD, Base)
1498 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001499 } else {
1500 // In the MS ABI, store the vbtable offset, which is analogous to the
1501 // vbase offset offset in Itanium.
1502 BaseOffset =
1503 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1504 }
Bob Haarmandff36732016-10-25 22:19:32 +00001505 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 } else
1507 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1508 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1509 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001510
Adrian Prantl21361fb2014-08-29 22:44:27 +00001511 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001512 llvm::DIType *DTy =
1513 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001514 EltTys.push_back(DTy);
1515 }
1516}
1517
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001518llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001519CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1520 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001521 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001522 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001523 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1524 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001525 StringRef Name;
1526 if (TPList)
1527 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001528 switch (TA.getKind()) {
1529 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001530 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001531 TemplateParams.push_back(
1532 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001533 } break;
1534 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001535 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001536 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1537 TheCU, Name, TTy,
1538 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001539 } break;
1540 case TemplateArgument::Declaration: {
1541 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001542 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001543 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001544 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001545 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001546 // Variable pointer template parameters have a value that is the address
1547 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001548 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001549 V = CGM.GetAddrOfGlobalVar(VD);
1550 // Member function pointers have special support for building them, though
1551 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001552 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001553 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001554 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001555 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001556 // Member data pointers have special handling too to compute the fixed
1557 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001558 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001559 // These five lines (& possibly the above member function pointer
1560 // handling) might be able to be refactored to use similar code in
1561 // CodeGenModule::getMemberPointerConstant
1562 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1563 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001564 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001565 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001566 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001567 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1568 TheCU, Name, TTy,
1569 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001570 } break;
1571 case TemplateArgument::NullPtr: {
1572 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001573 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001574 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001575 // Special case member data pointer null values since they're actually -1
1576 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001577 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001578 // But treat member function pointers as simple zero integers because
1579 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1580 // CodeGen grows handling for values of non-null member function
1581 // pointers then perhaps we could remove this special case and rely on
1582 // EmitNullMemberPointer for member function pointers.
1583 if (MPT->isMemberDataPointer())
1584 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1585 if (!V)
1586 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001587 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001588 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001589 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001590 case TemplateArgument::Template:
1591 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001592 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001593 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1594 break;
1595 case TemplateArgument::Pack:
1596 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1597 TheCU, Name, nullptr,
1598 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1599 break;
David Majnemer5559d472013-08-24 08:21:10 +00001600 case TemplateArgument::Expression: {
1601 const Expr *E = TA.getAsExpr();
1602 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001603 if (E->isGLValue())
1604 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001605 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001606 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001607 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001608 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001609 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001610 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001611 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001612 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001613 case TemplateArgument::Null:
1614 llvm_unreachable(
1615 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001616 }
1617 }
1618 return DBuilder.getOrCreateArray(TemplateParams);
1619}
1620
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001621llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001622CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001623 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 if (FD->getTemplatedKind() ==
1625 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001626 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1627 ->getTemplate()
1628 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001629 return CollectTemplateParams(
1630 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001632 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001633}
1634
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001635llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1636 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001637 // Always get the full list of parameters, not just the ones from
1638 // the specialization.
1639 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001640 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001641 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001642 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001643}
1644
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001645llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001646 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001647 return VTablePtrType;
1648
1649 ASTContext &Context = CGM.getContext();
1650
1651 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001652 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001653 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001654 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001656 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1657 Optional<unsigned> DWARFAddressSpace =
1658 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1659
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001660 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001661 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1662 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001663 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1664 return VTablePtrType;
1665}
1666
Guy Benyei11169dd2012-12-18 14:30:41 +00001667StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001668 // Copy the gdb compatible name on the side and use its reference.
1669 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001670}
1671
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001672void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001673 SmallVectorImpl<llvm::Metadata *> &EltTys,
1674 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001675 // If this class is not dynamic then there is not any vtable info to collect.
1676 if (!RD->isDynamicClass())
1677 return;
1678
Reid Kleckner59812422016-08-31 20:35:01 +00001679 // Don't emit any vtable shape or vptr info if this class doesn't have an
1680 // extendable vfptr. This can happen if the class doesn't have virtual
1681 // methods, or in the MS ABI if those virtual methods only come from virtually
1682 // inherited bases.
1683 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1684 if (!RL.hasExtendableVFPtr())
1685 return;
1686
Reid Klecknerdc124992016-08-31 16:11:43 +00001687 // CodeView needs to know how large the vtable of every dynamic class is, so
1688 // emit a special named pointer type into the element list. The vptr type
1689 // points to this type as well.
1690 llvm::DIType *VPtrTy = nullptr;
1691 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1692 CGM.getTarget().getCXXABI().isMicrosoft();
1693 if (NeedVTableShape) {
1694 uint64_t PtrWidth =
1695 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1696 const VTableLayout &VFTLayout =
1697 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1698 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001699 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001700 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001701 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1702 Optional<unsigned> DWARFAddressSpace =
1703 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001704
1705 // Create a very wide void* type and insert it directly in the element list.
1706 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001707 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1708 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001709 EltTys.push_back(VTableType);
1710
1711 // The vptr is a pointer to this special vtable type.
1712 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1713 }
1714
1715 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001716 if (RL.getPrimaryBase())
1717 return;
1718
1719 if (!VPtrTy)
1720 VPtrTy = getOrCreateVTablePtrType(Unit);
1721
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001723 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001724 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001725 llvm::DINode::FlagArtificial, VPtrTy);
1726 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001727}
1728
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001730 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001731 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001732 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001733 return T;
1734}
1735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001736llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001737 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001738 return getOrCreateStandaloneType(D, Loc);
1739}
1740
1741llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1742 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001743 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001744 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001745 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001746 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001747
Adrian Prantl73409ce2013-03-11 18:33:46 +00001748 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001749 return T;
1750}
1751
David Blaikie483a9da2014-05-06 18:35:21 +00001752void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001753 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001754 return;
1755 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001756 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001757 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001758 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001759 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001760 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001761 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001762 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001763}
1764
David Blaikieb2e86eb2013-08-15 20:49:17 +00001765void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001766 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001767 !CGM.getLangOpts().CPlusPlus)
1768 completeRequiredType(RD);
1769}
1770
David Blaikieb11c8732017-01-30 06:36:08 +00001771/// Return true if the class or any of its methods are marked dllimport.
1772static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1773 if (RD->hasAttr<DLLImportAttr>())
1774 return true;
1775 for (const CXXMethodDecl *MD : RD->methods())
1776 if (MD->hasAttr<DLLImportAttr>())
1777 return true;
1778 return false;
1779}
1780
Adrian Prantla43acdc2017-07-24 23:48:51 +00001781/// Does a type definition exist in an imported clang module?
1782static bool isDefinedInClangModule(const RecordDecl *RD) {
1783 // Only definitions that where imported from an AST file come from a module.
1784 if (!RD || !RD->isFromASTFile())
1785 return false;
1786 // Anonymous entities cannot be addressed. Treat them as not from module.
1787 if (!RD->isExternallyVisible() && RD->getName().empty())
1788 return false;
1789 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1790 if (!CXXDecl->isCompleteDefinition())
1791 return false;
1792 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1793 if (TemplateKind != TSK_Undeclared) {
1794 // This is a template, check the origin of the first member.
1795 if (CXXDecl->field_begin() == CXXDecl->field_end())
1796 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1797 if (!CXXDecl->field_begin()->isFromASTFile())
1798 return false;
1799 }
1800 }
1801 return true;
1802}
1803
David Blaikie6943dea2013-08-20 01:28:15 +00001804void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001805 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1806 if (CXXRD->isDynamicClass() &&
1807 CGM.getVTableLinkage(CXXRD) ==
1808 llvm::GlobalValue::AvailableExternallyLinkage &&
1809 !isClassOrMethodDLLImport(CXXRD))
1810 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001811
1812 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1813 return;
1814
David Blaikieb11c8732017-01-30 06:36:08 +00001815 completeClass(RD);
1816}
1817
1818void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001819 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001820 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001821 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001822 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001823 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001824 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001825 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001826 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001827 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001828 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001829}
1830
David Blaikie0e716b42014-03-03 23:48:23 +00001831static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1832 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001833 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1834 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001835 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001836 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001837 return true;
1838 return false;
1839}
1840
Benjamin Kramer8c305922016-02-02 11:06:51 +00001841static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1842 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001843 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001844 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001845 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001846
David Blaikie1ac9c982017-04-11 21:13:37 +00001847 if (auto *ES = RD->getASTContext().getExternalSource())
1848 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1849 return true;
1850
Benjamin Kramer8c305922016-02-02 11:06:51 +00001851 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001852 return false;
1853
1854 if (!LangOpts.CPlusPlus)
1855 return false;
1856
1857 if (!RD->isCompleteDefinitionRequired())
1858 return true;
1859
David Majnemer58ed0f32016-07-17 00:39:12 +00001860 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001861
1862 if (!CXXDecl)
1863 return false;
1864
Adrian McCarthy99242982016-08-16 22:11:18 +00001865 // Only emit complete debug info for a dynamic class when its vtable is
1866 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001867 // across DLL boundaries, so skip this optimization if the class or any of its
1868 // methods are marked dllimport. This isn't a complete solution, since objects
1869 // without any dllimport methods can be used in one DLL and constructed in
1870 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001871 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001872 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001873 return true;
1874
1875 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001876 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001877 Spec = SD->getSpecializationKind();
1878
1879 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1880 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1881 CXXDecl->method_end()))
1882 return true;
1883
1884 return false;
1885}
1886
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001887void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1888 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1889 return;
1890
1891 QualType Ty = CGM.getContext().getRecordType(RD);
1892 llvm::DIType *T = getTypeOrNull(Ty);
1893 if (T && T->isForwardDecl())
1894 completeClassData(RD);
1895}
1896
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001897llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001899 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001900 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1901 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001902 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001903 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001904 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001905 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001906
David Blaikieb2e86eb2013-08-15 20:49:17 +00001907 return CreateTypeDefinition(Ty);
1908}
1909
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001910llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001911 RecordDecl *RD = Ty->getDecl();
1912
Guy Benyei11169dd2012-12-18 14:30:41 +00001913 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001914 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001915
1916 // Records and classes and unions can all be recursive. To handle them, we
1917 // first generate a debug descriptor for the struct as a forward declaration.
1918 // Then (if it is a definition) we go through and get debug info for all of
1919 // its members. Finally, we create a descriptor for the complete type (which
1920 // may refer to the forward decl if the struct is recursive) and replace all
1921 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001922 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001923
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001924 const RecordDecl *D = RD->getDefinition();
1925 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 return FwdDecl;
1927
David Majnemer58ed0f32016-07-17 00:39:12 +00001928 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001929 CollectContainingType(CXXDecl, FwdDecl);
1930
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001932 LexicalBlockStack.emplace_back(&*FwdDecl);
1933 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001934
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001936 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001937 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001938
1939 // Note: The split of CXXDecl information here is intentional, the
1940 // gdb tests will depend on a certain ordering at printout. The debug
1941 // information offsets are still correct if we merge them all together
1942 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001943 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001944 if (CXXDecl) {
1945 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001946 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001947 }
1948
Eric Christopher91a31902013-01-16 01:22:32 +00001949 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001950 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001951 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001953
1954 LexicalBlockStack.pop_back();
1955 RegionMap.erase(Ty->getDecl());
1956
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001957 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001958 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001959
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001960 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001961 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001962 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001963
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001964 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001965 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001966}
1967
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1969 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 // Ignore protocols.
1971 return getOrCreateType(Ty->getBaseType(), Unit);
1972}
1973
Manman Rene6be26c2016-09-13 17:25:08 +00001974llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1975 llvm::DIFile *Unit) {
1976 // Ignore protocols.
1977 SourceLocation Loc = Ty->getDecl()->getLocation();
1978
1979 // Use Typedefs to represent ObjCTypeParamType.
1980 return DBuilder.createTypedef(
1981 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1982 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1983 getDeclContextDescriptor(Ty->getDecl()));
1984}
1985
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001986/// \return true if Getter has the default name for the property PD.
1987static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1988 const ObjCMethodDecl *Getter) {
1989 assert(PD);
1990 if (!Getter)
1991 return true;
1992
1993 assert(Getter->getDeclName().isObjCZeroArgSelector());
1994 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001995 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001996}
1997
1998/// \return true if Setter has the default name for the property PD.
1999static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2000 const ObjCMethodDecl *Setter) {
2001 assert(PD);
2002 if (!Setter)
2003 return true;
2004
2005 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002006 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002007 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002008}
2009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002010llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2011 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 ObjCInterfaceDecl *ID = Ty->getDecl();
2013 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002014 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002015
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002016 // Return a forward declaration if this type was imported from a clang module,
2017 // and this is not the compile unit with the implementation of the type (which
2018 // may contain hidden ivars).
2019 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2020 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002021 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2022 ID->getName(),
2023 getDeclContextDescriptor(ID), Unit, 0);
2024
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002026 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002027 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002028 auto RuntimeLang =
2029 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002030
2031 // If this is just a forward declaration return a special forward-declaration
2032 // debug type since we won't be able to lay out the entire type.
2033 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002034 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002035 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002036 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002037 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2038 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002039 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 return FwdDecl;
2041 }
2042
David Blaikieef8a9512014-05-05 23:23:53 +00002043 return CreateTypeDefinition(Ty, Unit);
2044}
2045
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002046llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002047CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2048 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002049 // Use the Module pointer as the key into the cache. This is a
2050 // nullptr if the "Module" is a PCH, which is safe because we don't
2051 // support chained PCH debug info, so there can only be a single PCH.
2052 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002053 auto ModRef = ModuleCache.find(M);
2054 if (ModRef != ModuleCache.end())
2055 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002056
2057 // Macro definitions that were defined with "-D" on the command line.
2058 SmallString<128> ConfigMacros;
2059 {
2060 llvm::raw_svector_ostream OS(ConfigMacros);
2061 const auto &PPOpts = CGM.getPreprocessorOpts();
2062 unsigned I = 0;
2063 // Translate the macro definitions back into a commmand line.
2064 for (auto &M : PPOpts.Macros) {
2065 if (++I > 1)
2066 OS << " ";
2067 const std::string &Macro = M.first;
2068 bool Undef = M.second;
2069 OS << "\"-" << (Undef ? 'U' : 'D');
2070 for (char c : Macro)
2071 switch (c) {
2072 case '\\' : OS << "\\\\"; break;
2073 case '"' : OS << "\\\""; break;
2074 default: OS << c;
2075 }
2076 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002077 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002078 }
Adrian Prantl66689202015-09-18 23:01:45 +00002079
Adrian Prantl835e6632015-09-24 16:10:10 +00002080 bool IsRootModule = M ? !M->Parent : true;
2081 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002082 // PCH files don't have a signature field in the control block,
2083 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002084 // We use the lower 64 bits for debug info.
2085 uint64_t Signature =
2086 Mod.getSignature()
2087 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2088 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002089 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002090 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2091 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2092 TheCU->getProducer(), true, StringRef(), 0,
2093 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2094 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002095 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002096 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002097 llvm::DIModule *Parent =
2098 IsRootModule ? nullptr
2099 : getOrCreateModuleRef(
2100 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2101 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002102 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002103 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2104 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002105 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002106 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002107}
2108
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002109llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2110 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002111 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002112 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002113 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002114 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002115
2116 // Bit size, align and offset of the type.
2117 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002118 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002119
Leny Kholodov80c047d2016-09-06 10:48:04 +00002120 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002123
Adrian Prantlfd696112015-10-01 00:48:51 +00002124 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002125 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002126 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2127 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002128
David Blaikieef8a9512014-05-05 23:23:53 +00002129 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002130 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002131
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002132 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002133 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002134 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135
2136 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002137 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002138
2139 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2140 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002141 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002142 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002143 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002144 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002145
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002146 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2147 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002148 EltTys.push_back(InhTag);
2149 }
2150
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002151 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002152 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002154 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002155 unsigned PLine = getLineNumber(Loc);
2156 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2157 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002158 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2159 PD->getName(), PUnit, PLine,
2160 hasDefaultGetterName(PD, Getter) ? ""
2161 : getSelectorName(PD->getGetterName()),
2162 hasDefaultSetterName(PD, Setter) ? ""
2163 : getSelectorName(PD->getSetterName()),
2164 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002166 };
2167 {
2168 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002169 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002170 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002171 PropertySet.insert(PD->getIdentifier());
2172 AddProperty(PD);
2173 }
Manman Renefe1bac2016-01-27 20:00:32 +00002174 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002175 // Don't emit duplicate metadata for properties that were already in a
2176 // class extension.
2177 if (!PropertySet.insert(PD->getIdentifier()).second)
2178 continue;
2179 AddProperty(PD);
2180 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 }
2182
2183 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2184 unsigned FieldNo = 0;
2185 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2186 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002187 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002188 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002189 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002190
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 StringRef FieldName = Field->getName();
2192
2193 // Ignore unnamed fields.
2194 if (FieldName.empty())
2195 continue;
2196
2197 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002198 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 unsigned FieldLine = getLineNumber(Field->getLocation());
2200 QualType FType = Field->getType();
2201 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002202 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002203
2204 if (!FType->isIncompleteArrayType()) {
2205
2206 // Bit size, align and offset of the type.
2207 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002208 ? Field->getBitWidthValue(CGM.getContext())
2209 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002210 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 }
2212
2213 uint64_t FieldOffset;
2214 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2215 // We don't know the runtime offset of an ivar if we're using the
2216 // non-fragile ABI. For bitfields, use the bit offset into the first
2217 // byte of storage of the bitfield. For other fields, use zero.
2218 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002219 FieldOffset =
2220 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 FieldOffset %= CGM.getContext().getCharWidth();
2222 } else {
2223 FieldOffset = 0;
2224 }
2225 } else {
2226 FieldOffset = RL.getFieldOffset(FieldNo);
2227 }
2228
Leny Kholodov80c047d2016-09-06 10:48:04 +00002229 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002231 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002233 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002234 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002235 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002236
Craig Topper8a13c412014-05-21 05:09:00 +00002237 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002239 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002240 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002242 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002243 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002244 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2246 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002247 PropertyNode = DBuilder.createObjCProperty(
2248 PD->getName(), PUnit, PLine,
2249 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2250 PD->getGetterName()),
2251 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2252 PD->getSetterName()),
2253 PD->getPropertyAttributes(),
2254 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 }
2256 }
2257 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002258 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2259 FieldSize, FieldAlign, FieldOffset, Flags,
2260 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002261 EltTys.push_back(FieldTy);
2262 }
2263
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002264 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002265 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002266
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002268 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002269}
2270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002271llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2272 llvm::DIFile *Unit) {
2273 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002274 int64_t Count = Ty->getNumElements();
2275 if (Count == 0)
2276 // If number of elements are not known then this is an unbounded array.
2277 // Use Count == -1 to express such arrays.
2278 Count = -1;
2279
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002280 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002281 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002282
2283 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002284 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002285
2286 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2287}
2288
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002289llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002290 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002291 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002292
2293 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002294 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002296 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2297 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 } else if (Ty->isIncompleteArrayType()) {
2299 Size = 0;
2300 if (Ty->getElementType()->isIncompleteType())
2301 Align = 0;
2302 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002303 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002304 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 Size = 0;
2306 Align = 0;
2307 } else {
2308 // Size and align of the whole array, not the element type.
2309 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002310 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002311 }
2312
2313 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2314 // interior arrays, do we care? Why aren't nested arrays represented the
2315 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002316 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 QualType EltTy(Ty, 0);
2318 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2319 // If the number of elements is known, then count is that number. Otherwise,
2320 // it's -1. This allows us to represent a subrange with an array of 0
2321 // elements, like this:
2322 //
2323 // struct foo {
2324 // int x[0];
2325 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002326 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002327 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002328 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002329 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002330 if (Expr *Size = VAT->getSizeExpr()) {
2331 llvm::APSInt V;
2332 if (Size->EvaluateAsInt(V, CGM.getContext()))
2333 Count = V.getExtValue();
2334 }
David Blaikie87173f12016-08-22 17:49:56 +00002335 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002336
Guy Benyei11169dd2012-12-18 14:30:41 +00002337 // FIXME: Verify this is right for VLAs.
2338 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2339 EltTy = Ty->getElementType();
2340 }
2341
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002342 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002343
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002344 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2345 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002346}
2347
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002348llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2349 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002350 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2351 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002352}
2353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002354llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2355 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002356 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2357 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002358}
2359
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002360llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2361 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002362 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002363 uint64_t Size = 0;
2364
2365 if (!Ty->isIncompleteType()) {
2366 Size = CGM.getContext().getTypeSize(Ty);
2367
2368 // Set the MS inheritance model. There is no flag for the unspecified model.
2369 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2370 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2371 case MSInheritanceAttr::Keyword_single_inheritance:
2372 Flags |= llvm::DINode::FlagSingleInheritance;
2373 break;
2374 case MSInheritanceAttr::Keyword_multiple_inheritance:
2375 Flags |= llvm::DINode::FlagMultipleInheritance;
2376 break;
2377 case MSInheritanceAttr::Keyword_virtual_inheritance:
2378 Flags |= llvm::DINode::FlagVirtualInheritance;
2379 break;
2380 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2381 break;
2382 }
2383 }
2384 }
2385
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002386 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002387 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002388 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002389 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2390 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002391
2392 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002393 Ty->getPointeeType()->getAs<FunctionProtoType>();
2394 return DBuilder.createMemberPointerType(
2395 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2396 Ty->getClass(), FPT->getTypeQuals())),
2397 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002398 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002399}
2400
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002401llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002402 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2403 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002404}
2405
Xiuli Pan9c14e282016-01-09 12:53:17 +00002406llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2407 llvm::DIFile *U) {
2408 return getOrCreateType(Ty->getElementType(), U);
2409}
2410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002411llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002412 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002413
Guy Benyei11169dd2012-12-18 14:30:41 +00002414 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002415 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002416 if (!ED->getTypeForDecl()->isIncompleteType()) {
2417 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002418 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 }
2420
Manman Rene0064d82013-08-29 23:19:58 +00002421 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2422
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002423 bool isImportedFromModule =
2424 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2425
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 // If this is just a forward declaration, construct an appropriately
2427 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002428 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002429 // Note that it is possible for enums to be created as part of
2430 // their own declcontext. In this case a FwdDecl will be created
2431 // twice. This doesn't cause a problem because both FwdDecls are
2432 // entered into the ReplaceMap: finalize() will replace the first
2433 // FwdDecl with the second and then replace the second with
2434 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002435 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002436 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002437 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2438 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002439
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 unsigned Line = getLineNumber(ED->getLocation());
2441 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002442 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002443 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2444 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002445
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002446 ReplaceMap.emplace_back(
2447 std::piecewise_construct, std::make_tuple(Ty),
2448 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002449 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002450 }
2451
David Blaikie483a9da2014-05-06 18:35:21 +00002452 return CreateTypeDefinition(Ty);
2453}
2454
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002456 const EnumDecl *ED = Ty->getDecl();
2457 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002458 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002459 if (!ED->getTypeForDecl()->isIncompleteType()) {
2460 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002461 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002462 }
2463
2464 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2465
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002466 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002467 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002468 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002469 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002470 Enumerators.push_back(DBuilder.createEnumerator(
2471 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002472 }
2473
2474 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002475 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002476
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002477 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002479 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002480 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002481 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2482 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2483 Line, Size, Align, EltArray, ClassTy,
2484 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002485}
2486
Amjad Aboud546bc112017-02-09 22:07:24 +00002487llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2488 unsigned MType, SourceLocation LineLoc,
2489 StringRef Name, StringRef Value) {
2490 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2491 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2492}
2493
2494llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2495 SourceLocation LineLoc,
2496 SourceLocation FileLoc) {
2497 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2498 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2499 return DBuilder.createTempMacroFile(Parent, Line, FName);
2500}
2501
David Blaikie05491062013-01-21 04:37:12 +00002502static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2503 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002504 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002505 Qualifiers InnerQuals = T.getLocalQualifiers();
2506 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2507 // that is already there.
2508 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2509 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 QualType LastT = T;
2511 switch (T->getTypeClass()) {
2512 default:
David Blaikie05491062013-01-21 04:37:12 +00002513 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002514 case Type::TemplateSpecialization: {
2515 const auto *Spec = cast<TemplateSpecializationType>(T);
2516 if (Spec->isTypeAlias())
2517 return C.getQualifiedType(T.getTypePtr(), Quals);
2518 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002519 break;
2520 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 case Type::TypeOfExpr:
2522 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2523 break;
2524 case Type::TypeOf:
2525 T = cast<TypeOfType>(T)->getUnderlyingType();
2526 break;
2527 case Type::Decltype:
2528 T = cast<DecltypeType>(T)->getUnderlyingType();
2529 break;
2530 case Type::UnaryTransform:
2531 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2532 break;
2533 case Type::Attributed:
2534 T = cast<AttributedType>(T)->getEquivalentType();
2535 break;
2536 case Type::Elaborated:
2537 T = cast<ElaboratedType>(T)->getNamedType();
2538 break;
2539 case Type::Paren:
2540 T = cast<ParenType>(T)->getInnerType();
2541 break;
David Blaikie05491062013-01-21 04:37:12 +00002542 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002545 case Type::Auto:
2546 case Type::DeducedTemplateSpecialization: {
2547 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002548 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002549 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 break;
2551 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002552 case Type::Adjusted:
2553 case Type::Decayed:
2554 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2555 T = cast<AdjustedType>(T)->getAdjustedType();
2556 break;
2557 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002558
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002560 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002561 } while (true);
2562}
2563
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002564llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002565
2566 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002567 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002568
David Blaikief427b002014-05-06 03:42:01 +00002569 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 if (it != TypeCache.end()) {
2571 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002572 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002573 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 }
2575
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002576 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002577}
2578
David Blaikie0e716b42014-03-03 23:48:23 +00002579void CGDebugInfo::completeTemplateDefinition(
2580 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002581 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002582 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002583 completeUnusedClass(SD);
2584}
David Blaikie0856f662014-03-04 22:01:08 +00002585
David Blaikie1ac9c982017-04-11 21:13:37 +00002586void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2587 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2588 return;
2589
2590 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002591 // In case this type has no member function definitions being emitted, ensure
2592 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002593 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002594}
2595
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002596llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002597 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002598 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002599
2600 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002601 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002602
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002603 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 return T;
2605
Adrian Prantlca844182015-09-11 17:23:03 +00002606 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002607 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002608
2609 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002610 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002611
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 return Res;
2613}
2614
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002615llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002616 // A forward declaration inside a module header does not belong to the module.
2617 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2618 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002619 if (DebugTypeExtRefs && D->isFromASTFile()) {
2620 // Record a reference to an imported clang module or precompiled header.
2621 auto *Reader = CGM.getContext().getExternalSource();
2622 auto Idx = D->getOwningModuleID();
2623 auto Info = Reader->getSourceDescriptor(Idx);
2624 if (Info)
2625 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2626 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002627 // We are building a clang module or a precompiled header.
2628 //
2629 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2630 // and it wouldn't be necessary to specify the parent scope
2631 // because the type is already unique by definition (it would look
2632 // like the output of -fno-standalone-debug). On the other hand,
2633 // the parent scope helps a consumer to quickly locate the object
2634 // file where the type's definition is located, so it might be
2635 // best to make this behavior a command line or debugger tuning
2636 // option.
2637 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
Richard Smith54f04402017-05-18 02:29:20 +00002638 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002639 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002640 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2641 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002642 } else {
2643 // This the precompiled header being built.
2644 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002645 }
2646 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002647
Adrian Prantl9402cef2015-09-20 16:51:35 +00002648 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002649}
2650
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002651llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 // Handle qualifiers, which recursively handles what they refer to.
2653 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002654 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002655
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 // Work out details of type.
2657 switch (Ty->getTypeClass()) {
2658#define TYPE(Class, Base)
2659#define ABSTRACT_TYPE(Class, Base)
2660#define NON_CANONICAL_TYPE(Class, Base)
2661#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2662#include "clang/AST/TypeNodes.def"
2663 llvm_unreachable("Dependent types cannot show up in debug information");
2664
2665 case Type::ExtVector:
2666 case Type::Vector:
2667 return CreateType(cast<VectorType>(Ty), Unit);
2668 case Type::ObjCObjectPointer:
2669 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2670 case Type::ObjCObject:
2671 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002672 case Type::ObjCTypeParam:
2673 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 case Type::ObjCInterface:
2675 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2676 case Type::Builtin:
2677 return CreateType(cast<BuiltinType>(Ty));
2678 case Type::Complex:
2679 return CreateType(cast<ComplexType>(Ty));
2680 case Type::Pointer:
2681 return CreateType(cast<PointerType>(Ty), Unit);
2682 case Type::BlockPointer:
2683 return CreateType(cast<BlockPointerType>(Ty), Unit);
2684 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002685 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002687 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002689 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 case Type::FunctionProto:
2691 case Type::FunctionNoProto:
2692 return CreateType(cast<FunctionType>(Ty), Unit);
2693 case Type::ConstantArray:
2694 case Type::VariableArray:
2695 case Type::IncompleteArray:
2696 return CreateType(cast<ArrayType>(Ty), Unit);
2697
2698 case Type::LValueReference:
2699 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2700 case Type::RValueReference:
2701 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2702
2703 case Type::MemberPointer:
2704 return CreateType(cast<MemberPointerType>(Ty), Unit);
2705
2706 case Type::Atomic:
2707 return CreateType(cast<AtomicType>(Ty), Unit);
2708
Xiuli Pan9c14e282016-01-09 12:53:17 +00002709 case Type::Pipe:
2710 return CreateType(cast<PipeType>(Ty), Unit);
2711
Guy Benyei11169dd2012-12-18 14:30:41 +00002712 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002713 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2714
David Blaikie42edade2014-11-11 20:44:45 +00002715 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002716 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002717 case Type::Adjusted:
2718 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002719 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002720 case Type::Elaborated:
2721 case Type::Paren:
2722 case Type::SubstTemplateTypeParm:
2723 case Type::TypeOfExpr:
2724 case Type::TypeOf:
2725 case Type::Decltype:
2726 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002727 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002728 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002729 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002730
David Blaikie42edade2014-11-11 20:44:45 +00002731 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002732}
2733
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002734llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2735 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002736 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002737
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002738 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002739
2740 // We may have cached a forward decl when we could have created
2741 // a non-forward decl. Go ahead and create a non-forward decl
2742 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002743 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002744 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002745
2746 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002747 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002748
2749 // Propagate members from the declaration to the definition
2750 // CreateType(const RecordType*) will overwrite this with the members in the
2751 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002752 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002753
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002755 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 return Res;
2757}
2758
2759// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002760llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002762
Guy Benyei11169dd2012-12-18 14:30:41 +00002763 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002764 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 unsigned Line = getLineNumber(RD->getLocation());
2766 StringRef RDName = getClassName(RD);
2767
Amjad Abouddc4531e2016-04-30 01:44:38 +00002768 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002769
David Blaikied2785892013-08-18 17:36:19 +00002770 // If we ended up creating the type during the context chain construction,
2771 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002772 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002773 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002774 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002775 return T;
David Blaikied2785892013-08-18 17:36:19 +00002776
Adrian Prantl381e7552014-02-04 21:29:50 +00002777 // If this is just a forward or incomplete declaration, construct an
2778 // appropriately marked node and just return it.
2779 const RecordDecl *D = RD->getDefinition();
2780 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002781 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002782
2783 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002784 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002785
Manman Rene0064d82013-08-29 23:19:58 +00002786 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002788 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002789 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2790 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002791
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002792 // Elements of composite types usually have back to the type, creating
2793 // uniquing cycles. Distinct nodes are more efficient.
2794 switch (RealDecl->getTag()) {
2795 default:
2796 llvm_unreachable("invalid composite type tag");
2797
2798 case llvm::dwarf::DW_TAG_array_type:
2799 case llvm::dwarf::DW_TAG_enumeration_type:
2800 // Array elements and most enumeration elements don't have back references,
2801 // so they don't tend to be involved in uniquing cycles and there is some
2802 // chance of merging them when linking together two modules. Only make
2803 // them distinct if they are ODR-uniqued.
2804 if (FullName.empty())
2805 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002806 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002807
2808 case llvm::dwarf::DW_TAG_structure_type:
2809 case llvm::dwarf::DW_TAG_union_type:
2810 case llvm::dwarf::DW_TAG_class_type:
2811 // Immediatley resolve to a distinct node.
2812 RealDecl =
2813 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2814 break;
2815 }
2816
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002817 RegionMap[Ty->getDecl()].reset(RealDecl);
2818 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002819
David Majnemer58ed0f32016-07-17 00:39:12 +00002820 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002821 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002822 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002823 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002824}
2825
David Blaikieadfbf992013-08-18 16:55:33 +00002826void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002827 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002828 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002829 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002830 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2831 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002832 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002833 while (1) {
2834 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2835 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2836 if (PBT && !BRL.isPrimaryBaseVirtual())
2837 PBase = PBT;
2838 else
2839 break;
2840 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002841 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002842 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2843 getOrCreateFile(RD->getLocation())));
2844 } else if (RD->isDynamicClass())
2845 ContainingType = RealDecl;
2846
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002847 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002848}
2849
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002850llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002851 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002852 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002854 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002855 llvm::DIType *Ty =
2856 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2857 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 *Offset += FieldSize;
2859 return Ty;
2860}
2861
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002862void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002863 StringRef &Name,
2864 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002865 llvm::DIScope *&FDContext,
2866 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002867 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002868 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002869 Name = getFunctionName(FD);
2870 // Use mangled name as linkage name for C/C++ functions.
2871 if (FD->hasPrototype()) {
2872 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002873 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002874 }
2875 // No need to replicate the linkage name if it isn't different from the
2876 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002877 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002878 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2879 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002880 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002881 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002882 LinkageName = StringRef();
2883
Benjamin Kramer8c305922016-02-02 11:06:51 +00002884 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002885 if (const NamespaceDecl *NSDecl =
Adrian Prantl6fc88752017-05-16 23:46:10 +00002886 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00002887 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00002888 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002889 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2890 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2891 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2892 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002893 // Check if it is a noreturn-marked function
2894 if (FD->isNoReturn())
2895 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002896 // Collect template parameters.
2897 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2898 }
2899}
2900
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002901void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002902 unsigned &LineNo, QualType &T,
2903 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002904 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002905 Unit = getOrCreateFile(VD->getLocation());
2906 LineNo = getLineNumber(VD->getLocation());
2907
2908 setLocation(VD->getLocation());
2909
2910 T = VD->getType();
2911 if (T->isIncompleteArrayType()) {
2912 // CodeGen turns int[] into int[1] so we'll do the same here.
2913 llvm::APInt ConstVal(32, 1);
2914 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2915
2916 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2917 ArrayType::Normal, 0);
2918 }
2919
2920 Name = VD->getName();
2921 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2922 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2923 LinkageName = CGM.getMangledName(VD);
2924 if (LinkageName == Name)
2925 LinkageName = StringRef();
2926
2927 // Since we emit declarations (DW_AT_members) for static members, place the
2928 // definition of those static members in the namespace they were declared in
2929 // in the source code (the lexical decl context).
2930 // FIXME: Generalize this for even non-member global variables where the
2931 // declaration and definition may have different lexical decl contexts, once
2932 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002933 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2934 : VD->getDeclContext();
2935 // When a record type contains an in-line initialization of a static data
2936 // member, and the record type is marked as __declspec(dllexport), an implicit
2937 // definition of the member will be created in the record context. DWARF
2938 // doesn't seem to have a nice way to describe this in a form that consumers
2939 // are likely to understand, so fake the "normal" situation of a definition
2940 // outside the class by putting it in the global scope.
2941 if (DC->isRecord())
2942 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002943
Amjad Abouddc4531e2016-04-30 01:44:38 +00002944 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2945 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002946}
2947
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002948llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2949 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002950 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002951 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002952 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002953 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002954 llvm::DIFile *Unit = getOrCreateFile(Loc);
2955 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002956 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002957 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002958 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002959 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2960
Frederic Rissd253ed62014-11-18 03:40:51 +00002961 // Build function type.
2962 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002963 if (FD)
2964 for (const ParmVarDecl *Parm : FD->parameters())
2965 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002966 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2967 QualType FnType = CGM.getContext().getFunctionType(
2968 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002969 if (Stub) {
2970 return DBuilder.createFunction(
2971 DContext, Name, LinkageName, Unit, Line,
2972 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2973 !FD->isExternallyVisible(),
2974 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2975 TParamsArray.get(), getFunctionDeclaration(FD));
2976 }
2977
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002978 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002979 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002980 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2981 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002982 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002983 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002984 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002985 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2986 std::make_tuple(CanonDecl),
2987 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002988 return SP;
2989}
2990
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002991llvm::DISubprogram *
2992CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
2993 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
2994}
2995
2996llvm::DISubprogram *
2997CGDebugInfo::getFunctionStub(GlobalDecl GD) {
2998 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
2999}
3000
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003001llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003002CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3003 QualType T;
3004 StringRef Name, LinkageName;
3005 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003006 llvm::DIFile *Unit = getOrCreateFile(Loc);
3007 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003008 unsigned Line = getLineNumber(Loc);
3009
3010 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003011 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003012 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3013 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003014 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003015 FwdDeclReplaceMap.emplace_back(
3016 std::piecewise_construct,
3017 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3018 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003019 return GV;
3020}
3021
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003022llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003023 // We only need a declaration (not a definition) of the type - so use whatever
3024 // we would otherwise do to get a type for a pointee. (forward declarations in
3025 // limited debug info, full definitions (if the type definition is available)
3026 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003027 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003028 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003029 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003030 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003031
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003032 if (I != DeclCache.end()) {
3033 auto N = I->second;
3034 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3035 return GVE->getVariable();
3036 return dyn_cast_or_null<llvm::DINode>(N);
3037 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003038
3039 // No definition for now. Emit a forward definition that might be
3040 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003041 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003042 return getFunctionForwardDeclaration(FD);
3043 else if (const auto *VD = dyn_cast<VarDecl>(D))
3044 return getGlobalVariableForwardDeclaration(VD);
3045
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003046 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003047}
3048
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003049llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003050 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003051 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003052
David Majnemer58ed0f32016-07-17 00:39:12 +00003053 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003054 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003055 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003056
3057 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003058 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003059
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003060 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003061 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003062 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003063 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003064 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003065 }
3066 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003068 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003069 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 return SP;
3071 }
3072
Aaron Ballman86c93902014-03-06 23:45:36 +00003073 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003074 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003076 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003077 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 return SP;
3079 }
3080 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003081 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003082}
3083
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003084// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003085// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003086llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003087 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003088 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003089 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003090 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3091 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003092 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003093
David Majnemer58ed0f32016-07-17 00:39:12 +00003094 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003096
3097 const auto *FTy = FnType->getAs<FunctionType>();
3098 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3099
David Majnemer58ed0f32016-07-17 00:39:12 +00003100 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003102 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003103
3104 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003105 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003106
3107 // Replace the instancetype keyword with the actual type.
3108 if (ResultTy == CGM.getContext().getObjCInstanceType())
3109 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003110 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003111
Adrian Prantl7bec9032013-05-10 21:08:31 +00003112 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003113 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003114 QualType SelfDeclTy;
3115 if (auto *SelfDecl = OMethod->getSelfDecl())
3116 SelfDeclTy = SelfDecl->getType();
3117 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3118 if (FPT->getNumParams() > 1)
3119 SelfDeclTy = FPT->getParamType(0);
3120 if (!SelfDeclTy.isNull())
3121 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003122 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003123 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003124 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003126 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003127 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003128 // Variadic methods need a special marker at the end of the type list.
3129 if (OMethod->isVariadic())
3130 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003131
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003132 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003133 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3134 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003136
Adrian Prantl800faef2014-02-25 23:42:18 +00003137 // Handle variadic function types; they need an additional
3138 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003139 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003140 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003141 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003142 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003143 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3144 for (QualType ParamType : FPT->param_types())
3145 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003146 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003147 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003148 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3149 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003150 }
3151
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003152 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003153}
3154
Eric Christophere7b87e52014-10-26 23:40:33 +00003155void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3156 SourceLocation ScopeLoc, QualType FnType,
3157 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003158
3159 StringRef Name;
3160 StringRef LinkageName;
3161
3162 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3163
3164 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003165 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003166
Leny Kholodov80c047d2016-09-06 10:48:04 +00003167 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003168 llvm::DIFile *Unit = getOrCreateFile(Loc);
3169 llvm::DIScope *FDContext = Unit;
3170 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003171 if (!HasDecl) {
3172 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003173 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003174 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003175 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003176 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003178 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003179 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003180 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003181 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 return;
3183 }
3184 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003185 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3186 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003187 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003189 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003190 } else {
3191 // Use llvm function name.
3192 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003193 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003194 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003195 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003196 Name = Name.substr(1);
3197
Adrian Prantl42d71b92014-04-10 23:21:53 +00003198 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003199 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003200 // Artificial functions should not silently reuse CurLoc.
3201 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003202 }
3203 unsigned LineNo = getLineNumber(Loc);
3204 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003205
Eric Christopher8018e412014-03-27 18:50:35 +00003206 // FIXME: The function declaration we're constructing here is mostly reusing
3207 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3208 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3209 // all subprograms instead of the actual context since subprogram definitions
3210 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003211 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003212 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003213 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003214 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003215 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003216 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003217 // We might get here with a VarDecl in the case we're generating
3218 // code for the initialization of globals. Do not record these decls
3219 // as they will overwrite the actual VarDecl Decl in the cache.
3220 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003221 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003222
Adrian Prantlbebb8932014-03-21 21:01:58 +00003223 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003224 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003225
Guy Benyei11169dd2012-12-18 14:30:41 +00003226 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003227 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003228}
3229
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003230void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3231 QualType FnType) {
3232 StringRef Name;
3233 StringRef LinkageName;
3234
3235 const Decl *D = GD.getDecl();
3236 if (!D)
3237 return;
3238
Leny Kholodov80c047d2016-09-06 10:48:04 +00003239 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003240 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003241 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003242 llvm::DINodeArray TParamsArray;
3243 if (isa<FunctionDecl>(D)) {
3244 // If there is a DISubprogram for this function available then use it.
3245 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3246 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003247 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003248 Name = getObjCMethodName(OMD);
3249 Flags |= llvm::DINode::FlagPrototyped;
3250 } else {
3251 llvm_unreachable("not a function or ObjC method");
3252 }
3253 if (!Name.empty() && Name[0] == '\01')
3254 Name = Name.substr(1);
3255
3256 if (D->isImplicit()) {
3257 Flags |= llvm::DINode::FlagArtificial;
3258 // Artificial functions without a location should not silently reuse CurLoc.
3259 if (Loc.isInvalid())
3260 CurLoc = SourceLocation();
3261 }
3262 unsigned LineNo = getLineNumber(Loc);
3263 unsigned ScopeLine = 0;
3264
Adrian Prantle76bda52016-04-15 15:55:45 +00003265 DBuilder.retainType(DBuilder.createFunction(
3266 FDContext, Name, LinkageName, Unit, LineNo,
3267 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3268 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3269 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003270}
3271
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003272void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3273 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3274 // If there is a subprogram for this function available then use it.
3275 auto FI = SPCache.find(FD->getCanonicalDecl());
3276 llvm::DISubprogram *SP = nullptr;
3277 if (FI != SPCache.end())
3278 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
3279 if (!SP)
3280 SP = getFunctionStub(GD);
3281 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3282 LexicalBlockStack.emplace_back(SP);
3283 setInlinedAt(Builder.getCurrentDebugLocation());
3284 EmitLocation(Builder, FD->getLocation());
3285}
3286
3287void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3288 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003289 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003290 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3291}
3292
David Blaikie835afb22015-01-21 23:08:17 +00003293void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 // Update our current location
3295 setLocation(Loc);
3296
Eric Christophere7b87e52014-10-26 23:40:33 +00003297 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3298 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003299
Adrian Prantle83b1302014-01-07 22:05:52 +00003300 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003301 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003302 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003303}
3304
Guy Benyei11169dd2012-12-18 14:30:41 +00003305void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003306 llvm::MDNode *Back = nullptr;
3307 if (!LexicalBlockStack.empty())
3308 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003309 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003310 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003311 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003312}
3313
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003314void CGDebugInfo::AppendAddressSpaceXDeref(
3315 unsigned AddressSpace,
3316 SmallVectorImpl<int64_t> &Expr) const {
3317 Optional<unsigned> DWARFAddressSpace =
3318 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3319 if (!DWARFAddressSpace)
3320 return;
3321
3322 Expr.push_back(llvm::dwarf::DW_OP_constu);
3323 Expr.push_back(DWARFAddressSpace.getValue());
3324 Expr.push_back(llvm::dwarf::DW_OP_swap);
3325 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3326}
3327
Eric Christopher0fdcb312013-05-16 00:52:20 +00003328void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3329 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003330 // Set our current location.
3331 setLocation(Loc);
3332
Guy Benyei11169dd2012-12-18 14:30:41 +00003333 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003334 Builder.SetCurrentDebugLocation(
3335 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3336 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003337
Benjamin Kramer8c305922016-02-02 11:06:51 +00003338 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003339 return;
3340
3341 // Create a new lexical block and push it on the stack.
3342 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003343}
3344
Eric Christopher0fdcb312013-05-16 00:52:20 +00003345void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3346 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003347 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3348
3349 // Provide an entry in the line table for the end of the block.
3350 EmitLocation(Builder, Loc);
3351
Benjamin Kramer8c305922016-02-02 11:06:51 +00003352 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003353 return;
3354
Guy Benyei11169dd2012-12-18 14:30:41 +00003355 LexicalBlockStack.pop_back();
3356}
3357
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003358void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003359 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3360 unsigned RCount = FnBeginRegionCount.back();
3361 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3362
3363 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003364 while (LexicalBlockStack.size() != RCount) {
3365 // Provide an entry in the line table for the end of the block.
3366 EmitLocation(Builder, CurLoc);
3367 LexicalBlockStack.pop_back();
3368 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003369 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003370
3371 if (Fn && Fn->getSubprogram())
3372 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003373}
3374
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003375llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003376 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003377
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003378 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 QualType FType;
3380 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003381 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003383 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003384 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003385
3386 FieldOffset = 0;
3387 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3388 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3389 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3390 FType = CGM.getContext().IntTy;
3391 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3392 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3393
3394 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3395 if (HasCopyAndDispose) {
3396 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003397 EltTys.push_back(
3398 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3399 EltTys.push_back(
3400 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 }
3402 bool HasByrefExtendedLayout;
3403 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003404 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3405 HasByrefExtendedLayout) &&
3406 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003407 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003408 EltTys.push_back(
3409 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003410 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003411
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3413 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003414 CGM.getTarget().getPointerAlign(0))) {
3415 CharUnits FieldOffsetInBytes =
3416 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003417 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003418 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003419
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 if (NumPaddingBytes.isPositive()) {
3421 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3422 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3423 pad, ArrayType::Normal, 0);
3424 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3425 }
3426 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003427
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003429 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003430 FieldSize = CGM.getContext().getTypeSize(FType);
3431 FieldAlign = CGM.getContext().toBits(Align);
3432
Eric Christopherb2a008c2013-05-16 00:45:12 +00003433 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003434 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003435 FieldAlign, FieldOffset,
3436 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003437 EltTys.push_back(FieldTy);
3438 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003439
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003440 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003441
Leny Kholodov80c047d2016-09-06 10:48:04 +00003442 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003443
Guy Benyei11169dd2012-12-18 14:30:41 +00003444 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003445 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003446}
3447
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003448void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3449 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003450 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003451 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003452 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003453 if (VD->hasAttr<NoDebugAttr>())
3454 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003455
David Blaikie7fceebf2013-08-19 03:37:48 +00003456 bool Unwritten =
3457 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3458 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003460 if (!Unwritten)
3461 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003462 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003463 uint64_t XOffset = 0;
3464 if (VD->hasAttr<BlocksAttr>())
3465 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003466 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 Ty = getOrCreateType(VD->getType(), Unit);
3468
3469 // If there is no debug info for this type then do not emit debug info
3470 // for this variable.
3471 if (!Ty)
3472 return;
3473
Guy Benyei11169dd2012-12-18 14:30:41 +00003474 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003475 unsigned Line = 0;
3476 unsigned Column = 0;
3477 if (!Unwritten) {
3478 Line = getLineNumber(VD->getLocation());
3479 Column = getColumnNumber(VD->getLocation());
3480 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003481 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003482 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003485
3486 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3487
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003488 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3489 AppendAddressSpaceXDeref(AddressSpace, Expr);
3490
Alexey Bataev24f710182017-06-09 13:55:08 +00003491 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3492 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003493 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3494 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3495 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3496 Flags |= llvm::DINode::FlagObjectPointer;
3497 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003498
Adrian Prantlc3782a12017-04-18 01:22:01 +00003499 // Note: Older versions of clang used to emit byval references with an extra
3500 // DW_OP_deref, because they referenced the IR arg directly instead of
3501 // referencing an alloca. Newer versions of LLVM don't treat allocas
3502 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003503 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003504 StringRef Name = VD->getName();
3505 if (!Name.empty()) {
3506 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003507 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003508 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003509 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 // offset of __forwarding field
3511 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003512 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003513 Expr.push_back(offset.getQuantity());
3514 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003515 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 // offset of x field
3517 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003518 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003519 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003520 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003521 // If VD is an anonymous union then Storage represents value for
3522 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003523 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003524 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003525 // GDB has trouble finding local variables in anonymous unions, so we emit
3526 // artifical local variables for each of the members.
3527 //
3528 // FIXME: Remove this code as soon as GDB supports this.
3529 // The debug info verifier in LLVM operates based on the assumption that a
3530 // variable has the same size as its storage and we had to disable the check
3531 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003532 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003533 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003534 StringRef FieldName = Field->getName();
3535
3536 // Ignore unnamed fields. Do not ignore unnamed records.
3537 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3538 continue;
3539
3540 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003541 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003542 auto *D = DBuilder.createAutoVariable(
3543 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003544 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003545
3546 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003547 DBuilder.insertDeclare(
3548 Storage, D, DBuilder.createExpression(Expr),
3549 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3550 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003551 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003552 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003553 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003554
3555 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003556 auto *D = ArgNo
3557 ? DBuilder.createParameterVariable(
3558 Scope, Name, *ArgNo, Unit, Line, Ty,
3559 CGM.getLangOpts().Optimize, Flags)
3560 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3561 CGM.getLangOpts().Optimize, Flags,
3562 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003563
3564 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003565 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003566 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003567 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003568}
3569
3570void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3571 llvm::Value *Storage,
3572 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003573 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003574 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003575}
3576
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003577llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3578 llvm::DIType *Ty) {
3579 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003580 if (CachedTy)
3581 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003582 return DBuilder.createObjectPointerType(Ty);
3583}
3584
Eric Christophere7b87e52014-10-26 23:40:33 +00003585void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3586 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003587 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003588 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003589 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003590
Craig Topper8a13c412014-05-21 05:09:00 +00003591 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003592 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003593 if (VD->hasAttr<NoDebugAttr>())
3594 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003595
Guy Benyei11169dd2012-12-18 14:30:41 +00003596 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003597
Guy Benyei11169dd2012-12-18 14:30:41 +00003598 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003599 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3600 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003601 if (isByRef)
3602 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003603 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003604 Ty = getOrCreateType(VD->getType(), Unit);
3605
3606 // Self is passed along as an implicit non-arg variable in a
3607 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003608 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3609 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3610 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003611
3612 // Get location information.
3613 unsigned Line = getLineNumber(VD->getLocation());
3614 unsigned Column = getColumnNumber(VD->getLocation());
3615
3616 const llvm::DataLayout &target = CGM.getDataLayout();
3617
3618 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003619 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003620 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3621
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003622 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003623 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003624 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003625 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003626 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003627 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003628 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003629 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003630 offset =
3631 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003632 addr.push_back(offset.getQuantity());
3633 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003634 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 // offset of x field
3636 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003637 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003638 }
3639
3640 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003641 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003642 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003643 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003644 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003645
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003647 auto DL =
3648 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003649 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003650 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003651 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003652 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003653 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003654}
3655
Guy Benyei11169dd2012-12-18 14:30:41 +00003656void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3657 unsigned ArgNo,
3658 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003659 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003660 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003661}
3662
3663namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003664struct BlockLayoutChunk {
3665 uint64_t OffsetInBits;
3666 const BlockDecl::Capture *Capture;
3667};
3668bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3669 return l.OffsetInBits < r.OffsetInBits;
3670}
Guy Benyei11169dd2012-12-18 14:30:41 +00003671}
3672
3673void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003674 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003675 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003676 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003677 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003678 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003679 ASTContext &C = CGM.getContext();
3680 const BlockDecl *blockDecl = block.getBlockDecl();
3681
3682 // Collect some general information about the block's location.
3683 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003684 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003685 unsigned line = getLineNumber(loc);
3686 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003687
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003689 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003690
3691 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003692 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003693
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003694 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003695 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003696 blockLayout->getElementOffsetInBits(0),
3697 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003698 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003699 blockLayout->getElementOffsetInBits(1),
3700 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003701 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 blockLayout->getElementOffsetInBits(2),
3703 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003704 auto *FnTy = block.getBlockExpr()->getFunctionType();
3705 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003706 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003707 blockLayout->getElementOffsetInBits(3),
3708 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003709 fields.push_back(createFieldType(
3710 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3711 ? C.getBlockDescriptorExtendedType()
3712 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003713 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003714
3715 // We want to sort the captures by offset, not because DWARF
3716 // requires this, but because we're paranoid about debuggers.
3717 SmallVector<BlockLayoutChunk, 8> chunks;
3718
3719 // 'this' capture.
3720 if (blockDecl->capturesCXXThis()) {
3721 BlockLayoutChunk chunk;
3722 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003723 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003724 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003725 chunks.push_back(chunk);
3726 }
3727
3728 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003729 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003730 const VarDecl *variable = capture.getVariable();
3731 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3732
3733 // Ignore constant captures.
3734 if (captureInfo.isConstant())
3735 continue;
3736
3737 BlockLayoutChunk chunk;
3738 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003739 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003740 chunk.Capture = &capture;
3741 chunks.push_back(chunk);
3742 }
3743
3744 // Sort by offset.
3745 llvm::array_pod_sort(chunks.begin(), chunks.end());
3746
David Majnemer58ed0f32016-07-17 00:39:12 +00003747 for (const BlockLayoutChunk &Chunk : chunks) {
3748 uint64_t offsetInBits = Chunk.OffsetInBits;
3749 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003750
3751 // If we have a null capture, this must be the C++ 'this' capture.
3752 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003753 QualType type;
3754 if (auto *Method =
3755 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3756 type = Method->getThisType(C);
3757 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3758 type = QualType(RDecl->getTypeForDecl(), 0);
3759 else
3760 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003761
David Majnemerb4b671e2016-06-30 03:01:59 +00003762 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003763 offsetInBits, tunit, tunit));
3764 continue;
3765 }
3766
3767 const VarDecl *variable = capture->getVariable();
3768 StringRef name = variable->getName();
3769
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003770 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003771 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003772 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003773 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003774
3775 // FIXME: this creates a second copy of this type!
3776 uint64_t xoffset;
3777 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003778 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003779 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3780 PtrInfo.Width, Align, offsetInBits,
3781 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003782 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003783 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003784 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003785 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003786 }
3787 fields.push_back(fieldType);
3788 }
3789
3790 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003791 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3792 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003793
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003794 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003795
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003796 llvm::DIType *type =
3797 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003798 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003799 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003800 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3801
3802 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003803 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003804 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003805
3806 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003807 auto *debugVar = DBuilder.createParameterVariable(
3808 scope, Arg->getName(), ArgNo, tunit, line, type,
3809 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003810
Adrian Prantl616bef42013-03-14 21:52:59 +00003811 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003812 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003813 DBuilder.insertDbgValueIntrinsic(
Adrian Prantl1fa18852017-07-28 20:21:08 +00003814 LocalAddr, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003815 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3816 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003817 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003818
Adrian Prantl616bef42013-03-14 21:52:59 +00003819 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003820 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003821 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003822 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003823}
3824
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003825llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003826CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3827 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003828 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003829
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003830 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003831 if (MI != StaticDataMemberCache.end()) {
3832 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003833 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003834 }
David Blaikiece763042013-08-20 21:49:21 +00003835
3836 // If the member wasn't found in the cache, lazily construct and add it to the
3837 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003838 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003839 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003840 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003841}
3842
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003843llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003844 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3845 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003846 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003847
3848 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003849 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003850 StringRef FieldName = Field->getName();
3851
3852 // Ignore unnamed fields, but recurse into anonymous records.
3853 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003854 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003855 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003856 Var, DContext);
3857 continue;
3858 }
3859 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003860 GVE = DBuilder.createGlobalVariableExpression(
3861 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3862 Var->hasLocalLinkage());
3863 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003864 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003865 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003866}
3867
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003868void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003869 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003870 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003871 if (D->hasAttr<NoDebugAttr>())
3872 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003873
3874 // If we already created a DIGlobalVariable for this declaration, just attach
3875 // it to the llvm::GlobalVariable.
3876 auto Cached = DeclCache.find(D->getCanonicalDecl());
3877 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003878 return Var->addDebugInfo(
3879 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003880
Guy Benyei11169dd2012-12-18 14:30:41 +00003881 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003882 llvm::DIFile *Unit = nullptr;
3883 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003884 unsigned LineNo;
3885 StringRef DeclName, LinkageName;
3886 QualType T;
3887 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003888
3889 // Attempt to store one global variable for the declaration - even if we
3890 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003891 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003892
3893 // If this is an anonymous union then we'll want to emit a global
3894 // variable for each member of the anonymous union so that it's possible
3895 // to find the name of any field in the union.
3896 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003897 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003898 assert(RD->isAnonymousStructOrUnion() &&
3899 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003900 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003901 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003902 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003903
3904 SmallVector<int64_t, 4> Expr;
3905 unsigned AddressSpace =
3906 CGM.getContext().getTargetAddressSpace(D->getType());
3907 AppendAddressSpaceXDeref(AddressSpace, Expr);
3908
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003909 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003910 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003911 Var->hasLocalLinkage(),
3912 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00003913 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003914 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003915 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003916 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003917}
3918
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003919void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003920 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003921 if (VD->hasAttr<NoDebugAttr>())
3922 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003923 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003924 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003925 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003926 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003927 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003928 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3929 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003930 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3931 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3932 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003933 // Do not use global variables for enums.
3934 //
3935 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003936 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003937 return;
David Blaikiea15565562014-04-04 20:56:17 +00003938 // Do not emit separate definitions for function local const/statics.
3939 if (isa<FunctionDecl>(VD->getDeclContext()))
3940 return;
David Blaikiebb113912014-04-05 07:23:17 +00003941 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003942 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003943 if (VarD->isStaticDataMember()) {
3944 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003945 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003946 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003947 //
3948 // FIXME: This is probably unnecessary, since Ty should reference RD
3949 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003950 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003951 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003952 return;
3953 }
3954
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003955 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003956
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003957 auto &GV = DeclCache[VD];
3958 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003959 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003960 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003961 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3962 // FIXME: Add a representation for integer constants wider than 64 bits.
3963 if (Init.isInt())
3964 InitExpr =
3965 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3966 else if (Init.isFloat())
3967 InitExpr = DBuilder.createConstantValueExpression(
3968 Init.getFloat().bitcastToAPInt().getZExtValue());
3969 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003970 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003971 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003972 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3973 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003974}
3975
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003976llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003977 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003978 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003979 llvm::DIScope *Mod = getParentModuleOrNull(D);
3980 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003981}
3982
David Blaikie9f88fe82013-04-22 06:13:21 +00003983void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003984 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003985 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003986 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003987 if (!NSDecl->isAnonymousNamespace() ||
3988 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00003989 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003990 DBuilder.createImportedModule(
3991 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00003992 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003993 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003994}
3995
David Blaikiebd483762013-05-20 04:58:53 +00003996void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003997 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003998 return;
3999 assert(UD.shadow_size() &&
4000 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4001 // Emitting one decl is sufficient - debuggers can detect that this is an
4002 // overloaded name & provide lookup for all the overloads.
4003 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004004
4005 // FIXME: Skip functions with undeduced auto return type for now since we
4006 // don't currently have the plumbing for separate declarations & definitions
4007 // of free functions and mismatched types (auto in the declaration, concrete
4008 // return type in the definition)
4009 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4010 if (const auto *AT =
4011 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4012 if (AT->getDeducedType().isNull())
4013 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004014 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004015 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4016 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004017 DBuilder.createImportedDeclaration(
4018 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004019 getOrCreateFile(Loc), getLineNumber(Loc));
4020 }
David Blaikiebd483762013-05-20 04:58:53 +00004021}
4022
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004023void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004024 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4025 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004026 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004027 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004028 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004029 DBuilder.createImportedDeclaration(
4030 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004031 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4032 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004033 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004034}
4035
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004036llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004037CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004038 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004039 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004040 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004041 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004042 return cast<llvm::DIImportedEntity>(VH);
4043 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004044 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004045 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004046 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4047 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004048 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004049 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004050 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4051 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004052 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004053 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004054 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004055 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004056 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004057 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004058 return R;
4059}
4060
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004061llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004062CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4063 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4064 // if necessary, and this way multiple declarations of the same namespace in
4065 // different parent modules stay distinct.
4066 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004067 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004068 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004069
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004070 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004071 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004072 llvm::DINamespace *NS =
4073 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4074 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004075 return NS;
4076}
4077
Adrian Prantla5206ce2015-09-22 23:26:43 +00004078void CGDebugInfo::setDwoId(uint64_t Signature) {
4079 assert(TheCU && "no main compile unit");
4080 TheCU->setDWOId(Signature);
4081}
4082
4083
Guy Benyei11169dd2012-12-18 14:30:41 +00004084void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004085 // Creating types might create further types - invalidating the current
4086 // element and the size(), so don't cache/reference them.
4087 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4088 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004089 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004090 ? CreateTypeDefinition(E.Type, E.Unit)
4091 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004092 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004093 }
4094
David Blaikief427b002014-05-06 03:42:01 +00004095 for (auto p : ReplaceMap) {
4096 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004097 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004098 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004099
David Blaikief427b002014-05-06 03:42:01 +00004100 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004101 assert(it != TypeCache.end());
4102 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004103
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004104 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4105 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004106 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004107
Frederic Rissd253ed62014-11-18 03:40:51 +00004108 for (const auto &p : FwdDeclReplaceMap) {
4109 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004110 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004111 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004112
4113 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004114 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004115 // with ourselves, that will destroy the temporary MDNode and
4116 // replace it with a standard one, avoiding leaking memory.
4117 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004118 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004119 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004120 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004121
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004122 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4123 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004124 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004125 }
4126
Adrian Prantl73409ce2013-03-11 18:33:46 +00004127 // We keep our own list of retained types, because we need to look
4128 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004129 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004130 if (auto MD = TypeCache[RT])
4131 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004132
Guy Benyei11169dd2012-12-18 14:30:41 +00004133 DBuilder.finalize();
4134}
David Blaikie66088d52014-09-24 17:01:27 +00004135
4136void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004137 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004138 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004139
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004140 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004141 // Don't ignore in case of explicit cast where it is referenced indirectly.
4142 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004143}
Amara Emerson652795d2016-11-10 14:44:30 +00004144
4145llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4146 if (LexicalBlockStack.empty())
4147 return llvm::DebugLoc();
4148
4149 llvm::MDNode *Scope = LexicalBlockStack.back();
4150 return llvm::DebugLoc::get(
4151 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4152}