blob: 3e3e50b85c74c38de153c7652ecb4392a7bbdc2b [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000030#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000031#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000032#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000034#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000035#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000037#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000044#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000045#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046using namespace clang;
47using namespace clang::CodeGen;
48
Victor Leschuka7ece032016-10-20 00:13:19 +000049static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
50 auto TI = Ctx.getTypeInfo(Ty);
51 return TI.AlignIsRequired ? TI.Align : 0;
52}
53
54static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
55 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
56}
57
58static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
59 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
60}
61
Guy Benyei11169dd2012-12-18 14:30:41 +000062CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000063 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000064 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000066 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
67 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000068 CreateCompileUnit();
69}
70
71CGDebugInfo::~CGDebugInfo() {
72 assert(LexicalBlockStack.empty() &&
73 "Region stack mismatch, stack not empty!");
74}
75
David Blaikie66e41972015-01-14 07:38:27 +000076ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000077 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000078 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000079 init(TemporaryLocation);
80}
81
Adrian Prantl39428e72015-02-03 18:40:42 +000082ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000083 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000084 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000085 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000086 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000087}
88
89void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000090 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000091 auto *DI = CGF->getDebugInfo();
92 if (!DI) {
93 CGF = nullptr;
94 return;
David Blaikie66e41972015-01-14 07:38:27 +000095 }
David Blaikied7057d92015-08-12 23:49:57 +000096
97 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
98 if (TemporaryLocation.isValid()) {
99 DI->EmitLocation(CGF->Builder, TemporaryLocation);
100 return;
101 }
102
103 if (DefaultToEmpty) {
104 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
105 return;
106 }
107
108 // Construct a location that has a valid scope, but no line info.
109 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000110 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
111 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000112}
113
David Blaikie9b479662015-01-25 01:19:10 +0000114ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000115 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000116 init(E->getExprLoc());
117}
118
David Blaikie66e41972015-01-14 07:38:27 +0000119ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000120 : CGF(&CGF) {
121 if (!CGF.getDebugInfo()) {
122 this->CGF = nullptr;
123 return;
David Blaikie66e41972015-01-14 07:38:27 +0000124 }
David Blaikied7057d92015-08-12 23:49:57 +0000125 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
126 if (Loc)
127 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000128}
129
130ApplyDebugLocation::~ApplyDebugLocation() {
131 // Query CGF so the location isn't overwritten when location updates are
132 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000133 if (CGF)
134 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000135}
136
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000137ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
138 GlobalDecl InlinedFn)
139 : CGF(&CGF) {
140 if (!CGF.getDebugInfo()) {
141 this->CGF = nullptr;
142 return;
143 }
144 auto &DI = *CGF.getDebugInfo();
145 SavedLocation = DI.getLocation();
146 assert((DI.getInlinedAt() ==
147 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
148 "CGDebugInfo and IRBuilder are out of sync");
149
150 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
151}
152
153ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
154 if (!CGF)
155 return;
156 auto &DI = *CGF->getDebugInfo();
157 DI.EmitInlineFunctionEnd(CGF->Builder);
158 DI.EmitLocation(CGF->Builder, SavedLocation);
159}
160
Guy Benyei11169dd2012-12-18 14:30:41 +0000161void CGDebugInfo::setLocation(SourceLocation Loc) {
162 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000163 if (Loc.isInvalid())
164 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000165
166 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
167
168 // If we've changed files in the middle of a lexical scope go ahead
169 // and create a new lexical scope with file node if it's different
170 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000171 if (LexicalBlockStack.empty())
172 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000173
174 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000175 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000176 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000177
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000178 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000179 return;
180
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000181 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000182 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000183 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
184 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000185 } else if (isa<llvm::DILexicalBlock>(Scope) ||
186 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000187 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000188 LexicalBlockStack.emplace_back(
189 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000190 }
191}
192
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000193llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000194 llvm::DIScope *Mod = getParentModuleOrNull(D);
195 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
196 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000197}
198
199llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
200 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000201 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000202 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000203
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000204 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000205 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000206 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000207 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000208 }
209
210 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000211 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000212 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000213
David Majnemer58ed0f32016-07-17 00:39:12 +0000214 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000215 if (!RDecl->isDependentType())
216 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000218 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000219}
220
Guy Benyei11169dd2012-12-18 14:30:41 +0000221StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000222 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000223 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 FunctionTemplateSpecializationInfo *Info =
225 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000226
Reid Kleckner31abd802016-06-30 17:41:31 +0000227 // Emit the unqualified name in normal operation. LLVM and the debugger can
228 // compute the fully qualified name from the scope chain. If we're only
229 // emitting line table info, there won't be any scope chains, so emit the
230 // fully qualified name here so that stack traces are more accurate.
231 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
232 // evaluating the size impact.
233 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
234 CGM.getCodeGenOpts().EmitCodeView;
235
236 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000237 return FII->getName();
238
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 SmallString<128> NS;
240 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000241 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000242 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000243 if (!UseQualifiedName)
244 FD->printName(OS);
245 else
246 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000247
Reid Kleckner829398e2016-06-17 16:11:20 +0000248 // Add any template specialization args.
249 if (Info) {
250 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000251 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000252 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000253 }
254
255 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000256 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000257}
258
259StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
260 SmallString<256> MethodName;
261 llvm::raw_svector_ostream OS(MethodName);
262 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
263 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000264 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000265 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000266 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000267 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000268 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000269 if (OC->IsClassExtension()) {
270 OS << OC->getClassInterface()->getName();
271 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000272 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000273 << OC->getIdentifier()->getNameStart() << ')';
274 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000275 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000276 OS << OCD->getClassInterface()->getName() << '('
277 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000278 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000279 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000280 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000281 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000282 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000283 ClassTy.print(OS, PrintingPolicy(LangOptions()));
284 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000285 }
286 OS << ' ' << OMD->getSelector().getAsString() << ']';
287
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000288 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000289}
290
Guy Benyei11169dd2012-12-18 14:30:41 +0000291StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000292 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000293}
294
Eric Christophere7b87e52014-10-26 23:40:33 +0000295StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000296 if (isa<ClassTemplateSpecializationDecl>(RD)) {
297 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000298 llvm::raw_svector_ostream OS(Name);
299 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
300 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000301
302 // Copy this name on the side and use its reference.
303 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000304 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000305
David Majnemerbc5976a2016-07-01 23:12:54 +0000306 // quick optimization to avoid having to intern strings that are already
307 // stored reliably elsewhere
308 if (const IdentifierInfo *II = RD->getIdentifier())
309 return II->getName();
310
311 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
312 // used to reconstruct the fully qualified type names.
313 if (CGM.getCodeGenOpts().EmitCodeView) {
314 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
315 assert(RD->getDeclContext() == D->getDeclContext() &&
316 "Typedef should not be in another decl context!");
317 assert(D->getDeclName().getAsIdentifierInfo() &&
318 "Typedef was not named!");
319 return D->getDeclName().getAsIdentifierInfo()->getName();
320 }
321
322 if (CGM.getLangOpts().CPlusPlus) {
323 StringRef Name;
324
325 ASTContext &Context = CGM.getContext();
326 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
327 // Anonymous types without a name for linkage purposes have their
328 // declarator mangled in if they have one.
329 Name = DD->getName();
330 else if (const TypedefNameDecl *TND =
331 Context.getTypedefNameForUnnamedTagDecl(RD))
332 // Anonymous types without a name for linkage purposes have their
333 // associate typedef mangled in if they have one.
334 Name = TND->getName();
335
336 if (!Name.empty()) {
337 SmallString<256> UnnamedType("<unnamed-type-");
338 UnnamedType += Name;
339 UnnamedType += '>';
340 return internString(UnnamedType);
341 }
342 }
343 }
344
345 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000346}
347
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000348llvm::DIFile::ChecksumKind
349CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
350 Checksum.clear();
351
352 if (!CGM.getCodeGenOpts().EmitCodeView)
353 return llvm::DIFile::CSK_None;
354
355 SourceManager &SM = CGM.getContext().getSourceManager();
356 bool Invalid;
357 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
358 if (Invalid)
359 return llvm::DIFile::CSK_None;
360
361 llvm::MD5 Hash;
362 llvm::MD5::MD5Result Result;
363
364 Hash.update(MemBuffer->getBuffer());
365 Hash.final(Result);
366
367 Hash.stringifyResult(Result, Checksum);
368 return llvm::DIFile::CSK_MD5;
369}
370
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000371llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000372 if (!Loc.isValid())
373 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000374 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000375 remapDIPath(TheCU->getDirectory()),
376 TheCU->getFile()->getChecksumKind(),
377 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000378
379 SourceManager &SM = CGM.getContext().getSourceManager();
380 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
381
382 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
383 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000384 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000385 remapDIPath(TheCU->getDirectory()),
386 TheCU->getFile()->getChecksumKind(),
387 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000388
389 // Cache the results.
390 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000391 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000392
393 if (it != DIFileCache.end()) {
394 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000395 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000396 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000397 }
398
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000399 SmallString<32> Checksum;
400 llvm::DIFile::ChecksumKind CSKind =
401 computeChecksum(SM.getFileID(Loc), Checksum);
402
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000403 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000404 remapDIPath(getCurrentDirname()),
405 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000406
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000407 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return F;
409}
410
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000411llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000412 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000413 remapDIPath(TheCU->getDirectory()),
414 TheCU->getFile()->getChecksumKind(),
415 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000416}
417
418std::string CGDebugInfo::remapDIPath(StringRef Path) const {
419 for (const auto &Entry : DebugPrefixMap)
420 if (Path.startswith(Entry.first))
421 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
422 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000423}
424
Guy Benyei11169dd2012-12-18 14:30:41 +0000425unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
426 if (Loc.isInvalid() && CurLoc.isInvalid())
427 return 0;
428 SourceManager &SM = CGM.getContext().getSourceManager();
429 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000430 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000431}
432
Adrian Prantlc7822422013-03-12 20:43:25 +0000433unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000435 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 return 0;
437
438 // If the location is invalid then use the current column.
439 if (Loc.isInvalid() && CurLoc.isInvalid())
440 return 0;
441 SourceManager &SM = CGM.getContext().getSourceManager();
442 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000444}
445
446StringRef CGDebugInfo::getCurrentDirname() {
447 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
448 return CGM.getCodeGenOpts().DebugCompilationDir;
449
450 if (!CWDName.empty())
451 return CWDName;
452 SmallString<256> CWD;
453 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000454 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000455}
456
Guy Benyei11169dd2012-12-18 14:30:41 +0000457void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000458 SmallString<32> Checksum;
459 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000460
David Blaikieaabde052014-05-14 00:29:00 +0000461 // Should we be asking the SourceManager for the main file name, instead of
462 // accepting it as an argument? This just causes the main file name to
463 // mismatch with source locations and create extra lexical scopes or
464 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
465 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
466 // because that's what the SourceManager says)
467
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 // Get absolute path name.
469 SourceManager &SM = CGM.getContext().getSourceManager();
470 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
471 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000472 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000473
474 // The main file name provided via the "-main-file-name" option contains just
475 // the file name itself with no path information. This file name may have had
476 // a relative path, so we look into the actual file entry for the main
477 // file to determine the real absolute path for the file.
478 std::string MainFileDir;
479 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000480 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000481 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000482 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
483 llvm::sys::path::append(MainFileDirSS, MainFileName);
484 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000485 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000486 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 }
488
Ed Masteda706022014-05-07 12:49:30 +0000489 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 const LangOptions &LO = CGM.getLangOpts();
491 if (LO.CPlusPlus) {
492 if (LO.ObjC1)
493 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
494 else
495 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
496 } else if (LO.ObjC1) {
497 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000498 } else if (LO.RenderScript) {
499 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 } else if (LO.C99) {
501 LangTag = llvm::dwarf::DW_LANG_C99;
502 } else {
503 LangTag = llvm::dwarf::DW_LANG_C89;
504 }
505
506 std::string Producer = getClangFullVersion();
507
508 // Figure out which version of the ObjC runtime we have.
509 unsigned RuntimeVers = 0;
510 if (LO.ObjC1)
511 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
512
Adrian Prantl826824e2016-04-08 22:43:06 +0000513 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
514 switch (DebugKind) {
515 case codegenoptions::NoDebugInfo:
516 case codegenoptions::LocTrackingOnly:
517 EmissionKind = llvm::DICompileUnit::NoDebug;
518 break;
519 case codegenoptions::DebugLineTablesOnly:
520 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
521 break;
522 case codegenoptions::LimitedDebugInfo:
523 case codegenoptions::FullDebugInfo:
524 EmissionKind = llvm::DICompileUnit::FullDebug;
525 break;
526 }
527
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000530 TheCU = DBuilder.createCompileUnit(
Amjad Aboudfa9a17e2016-12-14 20:24:40 +0000531 LangTag, DBuilder.createFile(remapDIPath(MainFileName),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000532 remapDIPath(getCurrentDirname()), CSKind,
533 Checksum),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000534 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000535 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
Dehao Chen5a3f8902017-02-01 22:45:21 +0000536 CGM.getCodeGenOpts().SplitDwarfInlining,
537 CGM.getCodeGenOpts().DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000538}
539
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000540llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000541 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 StringRef BTName;
543 switch (BT->getKind()) {
544#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000545#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000546#include "clang/AST/BuiltinTypes.def"
547 case BuiltinType::Dependent:
548 llvm_unreachable("Unexpected builtin type");
549 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000550 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000552 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000554 if (!ClassTy)
555 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
556 "objc_class", TheCU,
557 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000558 return ClassTy;
559 case BuiltinType::ObjCId: {
560 // typedef struct objc_class *Class;
561 // typedef struct objc_object {
562 // Class isa;
563 // } *id;
564
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000565 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 return ObjTy;
567
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000568 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000569 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
570 "objc_class", TheCU,
571 getOrCreateMainFile(), 0);
572
573 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000574
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000575 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000576
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000577 ObjTy = DBuilder.createStructType(
578 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
579 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000580
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000581 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000582 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
583 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
584 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000585 return ObjTy;
586 }
587 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000588 if (!SelTy)
589 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
590 "objc_selector", TheCU,
591 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 return SelTy;
593 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000594
Alexey Bader954ba212016-04-08 13:40:33 +0000595#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
596 case BuiltinType::Id: \
597 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
598 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000599#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000600 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000601 return getOrCreateStructPtrType("opencl_sampler_t",
602 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000603 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000604 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000605 case BuiltinType::OCLClkEvent:
606 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
607 case BuiltinType::OCLQueue:
608 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000609 case BuiltinType::OCLReserveID:
610 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000611
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000613 case BuiltinType::Char_U:
614 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
615 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000617 case BuiltinType::SChar:
618 Encoding = llvm::dwarf::DW_ATE_signed_char;
619 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000621 case BuiltinType::Char32:
622 Encoding = llvm::dwarf::DW_ATE_UTF;
623 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000624 case BuiltinType::UShort:
625 case BuiltinType::UInt:
626 case BuiltinType::UInt128:
627 case BuiltinType::ULong:
628 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000629 case BuiltinType::ULongLong:
630 Encoding = llvm::dwarf::DW_ATE_unsigned;
631 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000632 case BuiltinType::Short:
633 case BuiltinType::Int:
634 case BuiltinType::Int128:
635 case BuiltinType::Long:
636 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000637 case BuiltinType::LongLong:
638 Encoding = llvm::dwarf::DW_ATE_signed;
639 break;
640 case BuiltinType::Bool:
641 Encoding = llvm::dwarf::DW_ATE_boolean;
642 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 case BuiltinType::Half:
644 case BuiltinType::Float:
645 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000646 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000647 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000648 // FIXME: For targets where long double and __float128 have the same size,
649 // they are currently indistinguishable in the debugger without some
650 // special treatment. However, there is currently no consensus on encoding
651 // and this should be updated once a DWARF encoding exists for distinct
652 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000653 Encoding = llvm::dwarf::DW_ATE_float;
654 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000655 }
656
657 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000658 case BuiltinType::Long:
659 BTName = "long int";
660 break;
661 case BuiltinType::LongLong:
662 BTName = "long long int";
663 break;
664 case BuiltinType::ULong:
665 BTName = "long unsigned int";
666 break;
667 case BuiltinType::ULongLong:
668 BTName = "long long unsigned int";
669 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 default:
671 BTName = BT->getName(CGM.getLangOpts());
672 break;
673 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000674 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000676 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000677}
678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000679llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000680 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000681 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 if (Ty->isComplexIntegerType())
683 Encoding = llvm::dwarf::DW_ATE_lo_user;
684
685 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000686 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687}
688
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000689llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
690 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000691 QualifierCollector Qc;
692 const Type *T = Qc.strip(Ty);
693
694 // Ignore these qualifiers for now.
695 Qc.removeObjCGCAttr();
696 Qc.removeAddressSpace();
697 Qc.removeObjCLifetime();
698
699 // We will create one Derived type for one qualifier and recurse to handle any
700 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000701 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702 if (Qc.hasConst()) {
703 Tag = llvm::dwarf::DW_TAG_const_type;
704 Qc.removeConst();
705 } else if (Qc.hasVolatile()) {
706 Tag = llvm::dwarf::DW_TAG_volatile_type;
707 Qc.removeVolatile();
708 } else if (Qc.hasRestrict()) {
709 Tag = llvm::dwarf::DW_TAG_restrict_type;
710 Qc.removeRestrict();
711 } else {
712 assert(Qc.empty() && "Unknown type qualifier for debug info");
713 return getOrCreateType(QualType(T, 0), Unit);
714 }
715
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000716 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
718 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
719 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000720 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721}
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
724 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000725
726 // The frontend treats 'id' as a typedef to an ObjCObjectType,
727 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
728 // debug info, we want to emit 'id' in both cases.
729 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000730 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000731
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000732 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
733 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734}
735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000736llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
737 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000738 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000739 Ty->getPointeeType(), Unit);
740}
741
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000742/// \return whether a C++ mangling exists for the type defined by TD.
743static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
744 switch (TheCU->getSourceLanguage()) {
745 case llvm::dwarf::DW_LANG_C_plus_plus:
746 return true;
747 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
748 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
749 default:
750 return false;
751 }
752}
753
Manman Rene0064d82013-08-29 23:19:58 +0000754/// In C++ mode, types have linkage, so we can rely on the ODR and
755/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000756static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
757 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000758 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000759 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000760 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000761
762 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000763 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000764
Manman Rene0064d82013-08-29 23:19:58 +0000765 // TODO: This is using the RTTI name. Is there a better way to get
766 // a unique string for a type?
767 llvm::raw_svector_ostream Out(FullName);
768 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000769 return FullName;
770}
771
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000772/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000773static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
774 llvm::dwarf::Tag Tag;
775 if (RD->isStruct() || RD->isInterface())
776 Tag = llvm::dwarf::DW_TAG_structure_type;
777 else if (RD->isUnion())
778 Tag = llvm::dwarf::DW_TAG_union_type;
779 else {
780 // FIXME: This could be a struct type giving a default visibility different
781 // than C++ class type, but needs llvm metadata changes first.
782 assert(RD->isClass());
783 Tag = llvm::dwarf::DW_TAG_class_type;
784 }
785 return Tag;
786}
787
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000788llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000789CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000790 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000791 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
793 return cast<llvm::DICompositeType>(T);
794 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 unsigned Line = getLineNumber(RD->getLocation());
796 StringRef RDName = getClassName(RD);
797
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000798 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000799 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000800
Guy Benyei11169dd2012-12-18 14:30:41 +0000801 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000802 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000804 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000805 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000806 ReplaceMap.emplace_back(
807 std::piecewise_construct, std::make_tuple(Ty),
808 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000809 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000810}
811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000813 const Type *Ty,
814 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 // Bit size, align and offset of the type.
817 // Size is always the size of a pointer. We can't use getTypeSize here
818 // because that does not return the correct value for references.
819 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000820 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +0000821 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +0000822
Keno Fischer87842f32015-11-16 09:04:13 +0000823 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
824 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
825 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
826 Size, Align);
827 else
828 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
829 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000830}
831
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000832llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
833 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000834 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000835 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000836 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
837 TheCU, getOrCreateMainFile(), 0);
838 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
839 Cache = DBuilder.createPointerType(Cache, Size);
840 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000841}
842
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000843llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
844 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000845 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 QualType FType;
847 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000848 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000849 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000850
851 FieldOffset = 0;
852 FType = CGM.getContext().UnsignedLongTy;
853 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
854 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
855
856 Elements = DBuilder.getOrCreateArray(EltTys);
857 EltTys.clear();
858
Leny Kholodov80c047d2016-09-06 10:48:04 +0000859 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000860 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000861
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000862 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000863 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000864 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000865
866 // Bit size, align and offset of the type.
867 uint64_t Size = CGM.getContext().getTypeSize(Ty);
868
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000869 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000870
871 FieldOffset = 0;
872 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
873 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
874 FType = CGM.getContext().IntTy;
875 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
876 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000877 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000878 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
879
880 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 FieldSize = CGM.getContext().getTypeSize(Ty);
882 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000883 EltTys.push_back(DBuilder.createMemberType(
884 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
885 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000886
887 FieldOffset += FieldSize;
888 Elements = DBuilder.getOrCreateArray(EltTys);
889
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000890 // The __block_literal_generic structs are marked with a special
891 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
892 // the debugger needs to know about. To allow type uniquing, emit
893 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000894 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000895 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000896 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000897
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000898 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000899}
900
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000901llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
902 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000903 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000904 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000905
906 SmallString<128> NS;
907 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000908 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
909 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000910
911 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000912 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000913 CGM.getContext().getPrintingPolicy());
914
David Majnemer58ed0f32016-07-17 00:39:12 +0000915 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000916 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000917
918 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000919 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
920 getLineNumber(Loc),
921 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000922}
923
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000924llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
925 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000926 // We don't set size information, but do specify where the typedef was
927 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000928 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000929
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000930 // Typedefs are derived from some other type.
931 return DBuilder.createTypedef(
932 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
933 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000934 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000935}
936
Reid Klecknerf00f8032016-06-08 20:41:54 +0000937static unsigned getDwarfCC(CallingConv CC) {
938 switch (CC) {
939 case CC_C:
940 // Avoid emitting DW_AT_calling_convention if the C convention was used.
941 return 0;
942
943 case CC_X86StdCall:
944 return llvm::dwarf::DW_CC_BORLAND_stdcall;
945 case CC_X86FastCall:
946 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
947 case CC_X86ThisCall:
948 return llvm::dwarf::DW_CC_BORLAND_thiscall;
949 case CC_X86VectorCall:
950 return llvm::dwarf::DW_CC_LLVM_vectorcall;
951 case CC_X86Pascal:
952 return llvm::dwarf::DW_CC_BORLAND_pascal;
953
954 // FIXME: Create new DW_CC_ codes for these calling conventions.
955 case CC_X86_64Win64:
956 case CC_X86_64SysV:
957 case CC_AAPCS:
958 case CC_AAPCS_VFP:
959 case CC_IntelOclBicc:
960 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000961 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000962 case CC_Swift:
963 case CC_PreserveMost:
964 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000965 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000966 return 0;
967 }
968 return 0;
969}
970
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000971llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
972 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000973 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000974
975 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000976 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000977
978 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000979 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000980 if (isa<FunctionNoProtoType>(Ty))
981 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000982 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
983 for (const QualType &ParamType : FPT->param_types())
984 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000985 if (FPT->isVariadic())
986 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000987 }
988
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000989 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000990 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000991 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000992}
993
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000994/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000995/// As an optimization, return 0 if the access specifier equals the
996/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000997static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
998 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +0000999 AccessSpecifier Default = clang::AS_none;
1000 if (RD && RD->isClass())
1001 Default = clang::AS_private;
1002 else if (RD && (RD->isStruct() || RD->isUnion()))
1003 Default = clang::AS_public;
1004
1005 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001006 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001007
Eric Christophere7b87e52014-10-26 23:40:33 +00001008 switch (Access) {
1009 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001010 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001011 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001012 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001013 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001014 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001015 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001016 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001017 }
1018 llvm_unreachable("unexpected access enumerator");
1019}
Guy Benyei11169dd2012-12-18 14:30:41 +00001020
David Majnemerb4b671e2016-06-30 03:01:59 +00001021llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1022 llvm::DIScope *RecordTy,
1023 const RecordDecl *RD) {
1024 StringRef Name = BitFieldDecl->getName();
1025 QualType Ty = BitFieldDecl->getType();
1026 SourceLocation Loc = BitFieldDecl->getLocation();
1027 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1028 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1029
1030 // Get the location for the field.
1031 llvm::DIFile *File = getOrCreateFile(Loc);
1032 unsigned Line = getLineNumber(Loc);
1033
1034 const CGBitFieldInfo &BitFieldInfo =
1035 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1036 uint64_t SizeInBits = BitFieldInfo.Size;
1037 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001038 uint64_t StorageOffsetInBits =
1039 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1040 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001041 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001042 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001043 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1044 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001045}
1046
1047llvm::DIType *
1048CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1049 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001050 uint32_t AlignInBits, llvm::DIFile *tunit,
1051 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001052 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001053
1054 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001055 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001056 unsigned line = getLineNumber(loc);
1057
David Majnemer34b57492014-07-30 01:30:47 +00001058 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001059 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001060 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001061 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1062 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001063 if (!Align)
1064 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001065 }
1066
Leny Kholodov80c047d2016-09-06 10:48:04 +00001067 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001068 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001069 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001070}
1071
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001072void CGDebugInfo::CollectRecordLambdaFields(
1073 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001074 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001075 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1076 // has the name and the location of the variable so we should iterate over
1077 // both concurrently.
1078 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1079 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1080 unsigned fieldno = 0;
1081 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001082 E = CXXDecl->captures_end();
1083 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001084 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001085 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001086 SourceLocation Loc = C.getLocation();
1087 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001088 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001089 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001090 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001091 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001092 llvm::DIType *FieldType = createFieldType(
1093 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001094 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001095 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001096 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001097 // TODO: Need to handle 'this' in some way by probably renaming the
1098 // this of the lambda class and having a field member of 'this' or
1099 // by using AT_object_pointer for the function and having that be
1100 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001101 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001103 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001105 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001106 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001107
1108 elements.push_back(fieldType);
1109 }
1110 }
1111}
1112
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001113llvm::DIDerivedType *
1114CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001115 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001116 // Create the descriptor for the static variable, with or without
1117 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001118 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1120 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001121
Eric Christopher91a31902013-01-16 01:22:32 +00001122 unsigned LineNumber = getLineNumber(Var->getLocation());
1123 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001124 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001125 if (Var->getInit()) {
1126 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001127 if (Value) {
1128 if (Value->isInt())
1129 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1130 if (Value->isFloat())
1131 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1132 }
Eric Christopher91a31902013-01-16 01:22:32 +00001133 }
1134
Leny Kholodov80c047d2016-09-06 10:48:04 +00001135 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001136 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001137 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001138 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001139 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001140 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001141}
1142
Eric Christophere7b87e52014-10-26 23:40:33 +00001143void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001144 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1145 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001146 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001147 StringRef name = field->getName();
1148 QualType type = field->getType();
1149
1150 // Ignore unnamed fields unless they're anonymous structs/unions.
1151 if (name.empty() && !type->isRecordType())
1152 return;
1153
David Majnemerb4b671e2016-06-30 03:01:59 +00001154 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001155 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001156 FieldType = createBitFieldType(field, RecordTy, RD);
1157 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001158 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001159 FieldType =
1160 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001161 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001162 }
1163
David Majnemerb4b671e2016-06-30 03:01:59 +00001164 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001165}
1166
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001167void CGDebugInfo::CollectRecordNestedRecord(
1168 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1169 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001170 // Injected class names are not considered nested records.
1171 if (isa<InjectedClassNameType>(Ty))
1172 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001173 SourceLocation Loc = RD->getLocation();
1174 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1175 elements.push_back(nestedType);
1176}
1177
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001178void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001179 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001180 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001181 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001182 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001183
Eric Christopher91a31902013-01-16 01:22:32 +00001184 if (CXXDecl && CXXDecl->isLambda())
1185 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1186 else {
1187 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001188
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001189 // Debug info for nested records is included in the member list only for
1190 // CodeView.
1191 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1192
Eric Christopher91a31902013-01-16 01:22:32 +00001193 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001194 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001195
Eric Christopher91a31902013-01-16 01:22:32 +00001196 // Static and non-static members should appear in the same order as
1197 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001198 for (const auto *I : record->decls())
1199 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001200 if (V->hasAttr<NoDebugAttr>())
1201 continue;
David Blaikiece763042013-08-20 21:49:21 +00001202 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001203 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001204 if (MI != StaticDataMemberCache.end()) {
1205 assert(MI->second &&
1206 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001207 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001208 } else {
1209 auto Field = CreateRecordStaticField(V, RecordTy, record);
1210 elements.push_back(Field);
1211 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001212 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001213 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1214 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001215
1216 // Bump field number for next field.
1217 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001218 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1219 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1220 nestedRec->getDeclContext() == record)
1221 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001222 }
1223}
1224
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001225llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001226CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001227 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001228 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001229 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001230 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001231 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001232 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1233 Func, Unit);
1234}
David Blaikie2aaf0652013-01-07 22:24:59 +00001235
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1237 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DITypeRefArray Args(
1240 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001241 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001242 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001243
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001244 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001245
1246 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001247 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001248
David Blaikie2aaf0652013-01-07 22:24:59 +00001249 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001250 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001251 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1252 // Create pointer type directly in this case.
1253 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1254 QualType PointeeTy = ThisPtrTy->getPointeeType();
1255 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001256 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001257 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1259 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001260 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001261 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001262 // TODO: This and the artificial type below are misleading, the
1263 // types aren't artificial the argument is, but the current
1264 // metadata doesn't represent that.
1265 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1266 Elts.push_back(ThisPtrType);
1267 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001268 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001269 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001270 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1271 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001272 }
1273
1274 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001275 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1276 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001279
Leny Kholodov80c047d2016-09-06 10:48:04 +00001280 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001281 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001282 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001283 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001284 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001285
Reid Klecknerf00f8032016-06-08 20:41:54 +00001286 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1287 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001288}
1289
Eric Christopherb2a008c2013-05-16 00:45:12 +00001290/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001291/// inside a function.
1292static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001293 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 return isFunctionLocalClass(NRD);
1295 if (isa<FunctionDecl>(RD->getDeclContext()))
1296 return true;
1297 return false;
1298}
1299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001300llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1301 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001302 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001303 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001304
Guy Benyei11169dd2012-12-18 14:30:41 +00001305 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001306 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001307
1308 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1309 // make sense to give a single ctor/dtor a linkage name.
1310 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001311 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1312 // property to use here. It may've been intended to model "is non-external
1313 // type" but misses cases of non-function-local but non-external classes such
1314 // as those in anonymous namespaces as well as the reverse - external types
1315 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1317 MethodLinkageName = CGM.getMangledName(Method);
1318
1319 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001320 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001321 unsigned MethodLine = 0;
1322 if (!Method->isImplicit()) {
1323 MethodDefUnit = getOrCreateFile(Method->getLocation());
1324 MethodLine = getLineNumber(Method->getLocation());
1325 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001326
1327 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001329 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001330 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001331 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001332 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001333
Guy Benyei11169dd2012-12-18 14:30:41 +00001334 if (Method->isVirtual()) {
1335 if (Method->isPure())
1336 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1337 else
1338 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001339
Reid Kleckner216d0a12016-06-16 20:08:51 +00001340 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1341 // It doesn't make sense to give a virtual destructor a vtable index,
1342 // since a single destructor has two entries in the vtable.
1343 if (!isa<CXXDestructorDecl>(Method))
1344 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1345 } else {
1346 // Emit MS ABI vftable information. There is only one entry for the
1347 // deleting dtor.
1348 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1349 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1350 MicrosoftVTableContext::MethodVFTableLocation ML =
1351 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1352 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001353
1354 // CodeView only records the vftable offset in the class that introduces
1355 // the virtual method. This is possible because, unlike Itanium, the MS
1356 // C++ ABI does not include all virtual methods from non-primary bases in
1357 // the vtable for the most derived class. For example, if C inherits from
1358 // A and B, C's primary vftable will not include B's virtual methods.
1359 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1360 Flags |= llvm::DINode::FlagIntroducedVirtual;
1361
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001362 // The 'this' adjustment accounts for both the virtual and non-virtual
1363 // portions of the adjustment. Presumably the debugger only uses it when
1364 // it knows the dynamic type of an object.
1365 ThisAdjustment = CGM.getCXXABI()
1366 .getVirtualFunctionPrologueThisAdjustment(GD)
1367 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001368 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 ContainingType = RecordTy;
1370 }
1371
Guy Benyei11169dd2012-12-18 14:30:41 +00001372 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001373 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001374 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001375 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001376 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001377 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001378 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001380 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001381 }
1382 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001383 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001384 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001386 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001388
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1390 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001391 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001392 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1393 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1394 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001395
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001396 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001397
1398 return SP;
1399}
1400
Eric Christophere7b87e52014-10-26 23:40:33 +00001401void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001402 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1403 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001404
1405 // Since we want more than just the individual member decls if we
1406 // have templated functions iterate over every declaration to gather
1407 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001408 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001409 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1410 // If the member is implicit, don't add it to the member list. This avoids
1411 // the member being added to type units by LLVM, while still allowing it
1412 // to be emitted into the type declaration/reference inside the compile
1413 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001414 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001415 // FIXME: Handle Using(Shadow?)Decls here to create
1416 // DW_TAG_imported_declarations inside the class for base decls brought into
1417 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1418 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1419 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001420 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001421 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001422
1423 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1424 continue;
1425
David Blaikiefd580722014-10-06 05:18:55 +00001426 // Reuse the existing member function declaration if it exists.
1427 // It may be associated with the declaration of the type & should be
1428 // reused as we're building the definition.
1429 //
1430 // This situation can arise in the vtable-based debug info reduction where
1431 // implicit members are emitted in a non-vtable TU.
1432 auto MI = SPCache.find(Method->getCanonicalDecl());
1433 EltTys.push_back(MI == SPCache.end()
1434 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001435 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001437}
Guy Benyei11169dd2012-12-18 14:30:41 +00001438
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001439void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001440 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001442 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1443 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1444 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001445
Bob Haarmandff36732016-10-25 22:19:32 +00001446 // If we are generating CodeView debug info, we also need to emit records for
1447 // indirect virtual base classes.
1448 if (CGM.getCodeGenOpts().EmitCodeView) {
1449 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1450 llvm::DINode::FlagIndirectVirtualBase);
1451 }
1452}
1453
1454void CGDebugInfo::CollectCXXBasesAux(
1455 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1456 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1457 const CXXRecordDecl::base_class_const_range &Bases,
1458 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1459 llvm::DINode::DIFlags StartingFlags) {
1460 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1461 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001462 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001463 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001464 if (!SeenTypes.insert(Base).second)
1465 continue;
1466 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1467 llvm::DINode::DIFlags BFlags = StartingFlags;
1468 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001469
Aaron Ballman574705e2014-03-13 15:41:46 +00001470 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001471 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1472 // virtual base offset offset is -ve. The code generator emits dwarf
1473 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001474 BaseOffset = 0 - CGM.getItaniumVTableContext()
1475 .getVirtualBaseOffsetOffset(RD, Base)
1476 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001477 } else {
1478 // In the MS ABI, store the vbtable offset, which is analogous to the
1479 // vbase offset offset in Itanium.
1480 BaseOffset =
1481 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1482 }
Bob Haarmandff36732016-10-25 22:19:32 +00001483 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 } else
1485 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1486 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1487 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001488
Adrian Prantl21361fb2014-08-29 22:44:27 +00001489 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001490 llvm::DIType *DTy =
1491 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 EltTys.push_back(DTy);
1493 }
1494}
1495
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001496llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001497CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1498 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001499 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001500 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001501 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1502 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001503 StringRef Name;
1504 if (TPList)
1505 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001506 switch (TA.getKind()) {
1507 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001509 TemplateParams.push_back(
1510 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001511 } break;
1512 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001513 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001514 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1515 TheCU, Name, TTy,
1516 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001517 } break;
1518 case TemplateArgument::Declaration: {
1519 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001520 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001521 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001522 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001523 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001524 // Variable pointer template parameters have a value that is the address
1525 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001526 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001527 V = CGM.GetAddrOfGlobalVar(VD);
1528 // Member function pointers have special support for building them, though
1529 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001530 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001531 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001532 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001533 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001534 // Member data pointers have special handling too to compute the fixed
1535 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001536 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001537 // These five lines (& possibly the above member function pointer
1538 // handling) might be able to be refactored to use similar code in
1539 // CodeGenModule::getMemberPointerConstant
1540 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1541 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001542 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001543 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001544 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001545 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1546 TheCU, Name, TTy,
1547 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001548 } break;
1549 case TemplateArgument::NullPtr: {
1550 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001551 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001552 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001553 // Special case member data pointer null values since they're actually -1
1554 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001555 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001556 // But treat member function pointers as simple zero integers because
1557 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1558 // CodeGen grows handling for values of non-null member function
1559 // pointers then perhaps we could remove this special case and rely on
1560 // EmitNullMemberPointer for member function pointers.
1561 if (MPT->isMemberDataPointer())
1562 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1563 if (!V)
1564 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001565 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001566 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001567 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001568 case TemplateArgument::Template:
1569 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001570 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001571 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1572 break;
1573 case TemplateArgument::Pack:
1574 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1575 TheCU, Name, nullptr,
1576 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1577 break;
David Majnemer5559d472013-08-24 08:21:10 +00001578 case TemplateArgument::Expression: {
1579 const Expr *E = TA.getAsExpr();
1580 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001581 if (E->isGLValue())
1582 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001583 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001584 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001585 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001586 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001587 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001588 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001589 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001590 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001591 case TemplateArgument::Null:
1592 llvm_unreachable(
1593 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001594 }
1595 }
1596 return DBuilder.getOrCreateArray(TemplateParams);
1597}
1598
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001599llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001600CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001601 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001602 if (FD->getTemplatedKind() ==
1603 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001604 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1605 ->getTemplate()
1606 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001607 return CollectTemplateParams(
1608 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001610 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001611}
1612
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001613llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1614 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001615 // Always get the full list of parameters, not just the ones from
1616 // the specialization.
1617 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001618 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001619 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001620 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001621}
1622
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001623llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001624 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001625 return VTablePtrType;
1626
1627 ASTContext &Context = CGM.getContext();
1628
1629 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001630 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001631 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001632 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001633 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001634 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001635 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001636 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1637 return VTablePtrType;
1638}
1639
Guy Benyei11169dd2012-12-18 14:30:41 +00001640StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001641 // Copy the gdb compatible name on the side and use its reference.
1642 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001643}
1644
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001645void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001646 SmallVectorImpl<llvm::Metadata *> &EltTys,
1647 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001648 // If this class is not dynamic then there is not any vtable info to collect.
1649 if (!RD->isDynamicClass())
1650 return;
1651
Reid Kleckner59812422016-08-31 20:35:01 +00001652 // Don't emit any vtable shape or vptr info if this class doesn't have an
1653 // extendable vfptr. This can happen if the class doesn't have virtual
1654 // methods, or in the MS ABI if those virtual methods only come from virtually
1655 // inherited bases.
1656 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1657 if (!RL.hasExtendableVFPtr())
1658 return;
1659
Reid Klecknerdc124992016-08-31 16:11:43 +00001660 // CodeView needs to know how large the vtable of every dynamic class is, so
1661 // emit a special named pointer type into the element list. The vptr type
1662 // points to this type as well.
1663 llvm::DIType *VPtrTy = nullptr;
1664 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1665 CGM.getTarget().getCXXABI().isMicrosoft();
1666 if (NeedVTableShape) {
1667 uint64_t PtrWidth =
1668 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1669 const VTableLayout &VFTLayout =
1670 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1671 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001672 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001673 unsigned VTableWidth = PtrWidth * VSlotCount;
1674
1675 // Create a very wide void* type and insert it directly in the element list.
1676 llvm::DIType *VTableType =
1677 DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type");
1678 EltTys.push_back(VTableType);
1679
1680 // The vptr is a pointer to this special vtable type.
1681 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1682 }
1683
1684 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001685 if (RL.getPrimaryBase())
1686 return;
1687
1688 if (!VPtrTy)
1689 VPtrTy = getOrCreateVTablePtrType(Unit);
1690
Guy Benyei11169dd2012-12-18 14:30:41 +00001691 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001692 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001693 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001694 llvm::DINode::FlagArtificial, VPtrTy);
1695 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001696}
1697
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001698llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001699 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001700 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001701 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001702 return T;
1703}
1704
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001705llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001706 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001707 return getOrCreateStandaloneType(D, Loc);
1708}
1709
1710llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1711 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001712 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001713 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001714 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001715 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001716
Adrian Prantl73409ce2013-03-11 18:33:46 +00001717 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 return T;
1719}
1720
David Blaikie483a9da2014-05-06 18:35:21 +00001721void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001722 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001723 return;
1724 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001725 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001726 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001727 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001728 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001730 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001731 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001732}
1733
David Blaikieb2e86eb2013-08-15 20:49:17 +00001734void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001735 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001736 !CGM.getLangOpts().CPlusPlus)
1737 completeRequiredType(RD);
1738}
1739
David Blaikieb11c8732017-01-30 06:36:08 +00001740/// Return true if the class or any of its methods are marked dllimport.
1741static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1742 if (RD->hasAttr<DLLImportAttr>())
1743 return true;
1744 for (const CXXMethodDecl *MD : RD->methods())
1745 if (MD->hasAttr<DLLImportAttr>())
1746 return true;
1747 return false;
1748}
1749
David Blaikie6943dea2013-08-20 01:28:15 +00001750void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001751 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1752 if (CXXRD->isDynamicClass() &&
1753 CGM.getVTableLinkage(CXXRD) ==
1754 llvm::GlobalValue::AvailableExternallyLinkage &&
1755 !isClassOrMethodDLLImport(CXXRD))
1756 return;
1757 completeClass(RD);
1758}
1759
1760void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001761 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001762 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001763 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001764 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001765 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001766 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001767 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001768 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001769 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001770 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001771}
1772
David Blaikie0e716b42014-03-03 23:48:23 +00001773static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1774 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001775 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1776 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001777 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001778 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001779 return true;
1780 return false;
1781}
1782
Adrian Prantl05fefa42016-04-25 20:52:40 +00001783/// Does a type definition exist in an imported clang module?
1784static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001785 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001786 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001787 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001788 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001789 if (!RD->isExternallyVisible() && RD->getName().empty())
1790 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001791 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001792 if (!CXXDecl->isCompleteDefinition())
1793 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001794 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1795 if (TemplateKind != TSK_Undeclared) {
1796 // This is a template, check the origin of the first member.
1797 if (CXXDecl->field_begin() == CXXDecl->field_end())
1798 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1799 if (!CXXDecl->field_begin()->isFromASTFile())
1800 return false;
1801 }
Adrian Prantl94913712016-04-26 23:42:43 +00001802 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001803 return true;
1804}
1805
Benjamin Kramer8c305922016-02-02 11:06:51 +00001806static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1807 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001808 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001809 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001810 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001811
Benjamin Kramer8c305922016-02-02 11:06:51 +00001812 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001813 return false;
1814
1815 if (!LangOpts.CPlusPlus)
1816 return false;
1817
1818 if (!RD->isCompleteDefinitionRequired())
1819 return true;
1820
David Majnemer58ed0f32016-07-17 00:39:12 +00001821 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001822
1823 if (!CXXDecl)
1824 return false;
1825
Adrian McCarthy99242982016-08-16 22:11:18 +00001826 // Only emit complete debug info for a dynamic class when its vtable is
1827 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001828 // across DLL boundaries, so skip this optimization if the class or any of its
1829 // methods are marked dllimport. This isn't a complete solution, since objects
1830 // without any dllimport methods can be used in one DLL and constructed in
1831 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001832 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001833 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001834 return true;
1835
1836 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001837 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001838 Spec = SD->getSpecializationKind();
1839
1840 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1841 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1842 CXXDecl->method_end()))
1843 return true;
1844
1845 return false;
1846}
1847
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001848void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1849 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1850 return;
1851
1852 QualType Ty = CGM.getContext().getRecordType(RD);
1853 llvm::DIType *T = getTypeOrNull(Ty);
1854 if (T && T->isForwardDecl())
1855 completeClassData(RD);
1856}
1857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001858llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001859 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001860 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001861 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1862 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001863 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001864 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001865 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001866 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001867
David Blaikieb2e86eb2013-08-15 20:49:17 +00001868 return CreateTypeDefinition(Ty);
1869}
1870
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001872 RecordDecl *RD = Ty->getDecl();
1873
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001875 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001876
1877 // Records and classes and unions can all be recursive. To handle them, we
1878 // first generate a debug descriptor for the struct as a forward declaration.
1879 // Then (if it is a definition) we go through and get debug info for all of
1880 // its members. Finally, we create a descriptor for the complete type (which
1881 // may refer to the forward decl if the struct is recursive) and replace all
1882 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001883 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001884
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001885 const RecordDecl *D = RD->getDefinition();
1886 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 return FwdDecl;
1888
David Majnemer58ed0f32016-07-17 00:39:12 +00001889 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001890 CollectContainingType(CXXDecl, FwdDecl);
1891
Guy Benyei11169dd2012-12-18 14:30:41 +00001892 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001893 LexicalBlockStack.emplace_back(&*FwdDecl);
1894 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001895
Guy Benyei11169dd2012-12-18 14:30:41 +00001896 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001897 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001898 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001899
1900 // Note: The split of CXXDecl information here is intentional, the
1901 // gdb tests will depend on a certain ordering at printout. The debug
1902 // information offsets are still correct if we merge them all together
1903 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001904 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001905 if (CXXDecl) {
1906 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001907 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001908 }
1909
Eric Christopher91a31902013-01-16 01:22:32 +00001910 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001912 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001913 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001914
1915 LexicalBlockStack.pop_back();
1916 RegionMap.erase(Ty->getDecl());
1917
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001918 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001919 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001920
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001921 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001922 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001923 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001924
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001925 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001926 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001927}
1928
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1930 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 // Ignore protocols.
1932 return getOrCreateType(Ty->getBaseType(), Unit);
1933}
1934
Manman Rene6be26c2016-09-13 17:25:08 +00001935llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1936 llvm::DIFile *Unit) {
1937 // Ignore protocols.
1938 SourceLocation Loc = Ty->getDecl()->getLocation();
1939
1940 // Use Typedefs to represent ObjCTypeParamType.
1941 return DBuilder.createTypedef(
1942 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1943 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1944 getDeclContextDescriptor(Ty->getDecl()));
1945}
1946
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001947/// \return true if Getter has the default name for the property PD.
1948static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1949 const ObjCMethodDecl *Getter) {
1950 assert(PD);
1951 if (!Getter)
1952 return true;
1953
1954 assert(Getter->getDeclName().isObjCZeroArgSelector());
1955 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001956 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001957}
1958
1959/// \return true if Setter has the default name for the property PD.
1960static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1961 const ObjCMethodDecl *Setter) {
1962 assert(PD);
1963 if (!Setter)
1964 return true;
1965
1966 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001967 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001968 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001969}
1970
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001971llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1972 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001973 ObjCInterfaceDecl *ID = Ty->getDecl();
1974 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001975 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001976
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001977 // Return a forward declaration if this type was imported from a clang module,
1978 // and this is not the compile unit with the implementation of the type (which
1979 // may contain hidden ivars).
1980 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1981 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001982 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1983 ID->getName(),
1984 getDeclContextDescriptor(ID), Unit, 0);
1985
Guy Benyei11169dd2012-12-18 14:30:41 +00001986 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001987 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001988 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001989 auto RuntimeLang =
1990 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001991
1992 // If this is just a forward declaration return a special forward-declaration
1993 // debug type since we won't be able to lay out the entire type.
1994 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001995 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001996 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001997 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001998 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1999 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002000 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002001 return FwdDecl;
2002 }
2003
David Blaikieef8a9512014-05-05 23:23:53 +00002004 return CreateTypeDefinition(Ty, Unit);
2005}
2006
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002007llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002008CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2009 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002010 // Use the Module pointer as the key into the cache. This is a
2011 // nullptr if the "Module" is a PCH, which is safe because we don't
2012 // support chained PCH debug info, so there can only be a single PCH.
2013 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002014 auto ModRef = ModuleCache.find(M);
2015 if (ModRef != ModuleCache.end())
2016 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002017
2018 // Macro definitions that were defined with "-D" on the command line.
2019 SmallString<128> ConfigMacros;
2020 {
2021 llvm::raw_svector_ostream OS(ConfigMacros);
2022 const auto &PPOpts = CGM.getPreprocessorOpts();
2023 unsigned I = 0;
2024 // Translate the macro definitions back into a commmand line.
2025 for (auto &M : PPOpts.Macros) {
2026 if (++I > 1)
2027 OS << " ";
2028 const std::string &Macro = M.first;
2029 bool Undef = M.second;
2030 OS << "\"-" << (Undef ? 'U' : 'D');
2031 for (char c : Macro)
2032 switch (c) {
2033 case '\\' : OS << "\\\\"; break;
2034 case '"' : OS << "\\\""; break;
2035 default: OS << c;
2036 }
2037 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002038 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002039 }
Adrian Prantl66689202015-09-18 23:01:45 +00002040
Adrian Prantl835e6632015-09-24 16:10:10 +00002041 bool IsRootModule = M ? !M->Parent : true;
2042 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002043 // PCH files don't have a signature field in the control block,
2044 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00002045 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002046 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002047 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2048 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2049 TheCU->getProducer(), true, StringRef(), 0,
2050 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2051 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002052 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002053 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002054 llvm::DIModule *Parent =
2055 IsRootModule ? nullptr
2056 : getOrCreateModuleRef(
2057 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2058 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002059 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002060 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2061 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002062 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002063 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002064}
2065
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002066llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2067 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002068 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002069 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002070 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002071 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002072
2073 // Bit size, align and offset of the type.
2074 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002075 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002076
Leny Kholodov80c047d2016-09-06 10:48:04 +00002077 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002079 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002080
Adrian Prantlfd696112015-10-01 00:48:51 +00002081 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002082 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002083 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2084 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002085
David Blaikieef8a9512014-05-05 23:23:53 +00002086 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002087 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002088
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002089 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002090 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002091 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002092
2093 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002094 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002095
2096 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2097 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002098 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002099 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002100 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002101 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002102
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002103 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2104 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002105 EltTys.push_back(InhTag);
2106 }
2107
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002108 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002109 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002111 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 unsigned PLine = getLineNumber(Loc);
2113 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2114 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002115 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2116 PD->getName(), PUnit, PLine,
2117 hasDefaultGetterName(PD, Getter) ? ""
2118 : getSelectorName(PD->getGetterName()),
2119 hasDefaultSetterName(PD, Setter) ? ""
2120 : getSelectorName(PD->getSetterName()),
2121 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002122 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002123 };
2124 {
2125 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002126 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002127 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002128 PropertySet.insert(PD->getIdentifier());
2129 AddProperty(PD);
2130 }
Manman Renefe1bac2016-01-27 20:00:32 +00002131 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002132 // Don't emit duplicate metadata for properties that were already in a
2133 // class extension.
2134 if (!PropertySet.insert(PD->getIdentifier()).second)
2135 continue;
2136 AddProperty(PD);
2137 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 }
2139
2140 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2141 unsigned FieldNo = 0;
2142 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2143 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002144 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002145 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002146 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002147
Guy Benyei11169dd2012-12-18 14:30:41 +00002148 StringRef FieldName = Field->getName();
2149
2150 // Ignore unnamed fields.
2151 if (FieldName.empty())
2152 continue;
2153
2154 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002155 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 unsigned FieldLine = getLineNumber(Field->getLocation());
2157 QualType FType = Field->getType();
2158 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002159 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002160
2161 if (!FType->isIncompleteArrayType()) {
2162
2163 // Bit size, align and offset of the type.
2164 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002165 ? Field->getBitWidthValue(CGM.getContext())
2166 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002167 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 }
2169
2170 uint64_t FieldOffset;
2171 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2172 // We don't know the runtime offset of an ivar if we're using the
2173 // non-fragile ABI. For bitfields, use the bit offset into the first
2174 // byte of storage of the bitfield. For other fields, use zero.
2175 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002176 FieldOffset =
2177 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002178 FieldOffset %= CGM.getContext().getCharWidth();
2179 } else {
2180 FieldOffset = 0;
2181 }
2182 } else {
2183 FieldOffset = RL.getFieldOffset(FieldNo);
2184 }
2185
Leny Kholodov80c047d2016-09-06 10:48:04 +00002186 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002187 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002188 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002189 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002190 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002191 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002192 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002193
Craig Topper8a13c412014-05-21 05:09:00 +00002194 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002196 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002197 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002199 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002200 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002201 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2203 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002204 PropertyNode = DBuilder.createObjCProperty(
2205 PD->getName(), PUnit, PLine,
2206 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2207 PD->getGetterName()),
2208 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2209 PD->getSetterName()),
2210 PD->getPropertyAttributes(),
2211 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002212 }
2213 }
2214 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002215 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2216 FieldSize, FieldAlign, FieldOffset, Flags,
2217 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 EltTys.push_back(FieldTy);
2219 }
2220
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002221 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002222 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002223
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002225 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002226}
2227
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002228llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2229 llvm::DIFile *Unit) {
2230 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 int64_t Count = Ty->getNumElements();
2232 if (Count == 0)
2233 // If number of elements are not known then this is an unbounded array.
2234 // Use Count == -1 to express such arrays.
2235 Count = -1;
2236
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002237 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002238 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002239
2240 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002241 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002242
2243 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2244}
2245
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002246llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002248 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249
2250 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002251 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002253 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2254 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 } else if (Ty->isIncompleteArrayType()) {
2256 Size = 0;
2257 if (Ty->getElementType()->isIncompleteType())
2258 Align = 0;
2259 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002260 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002261 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 Size = 0;
2263 Align = 0;
2264 } else {
2265 // Size and align of the whole array, not the element type.
2266 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002267 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002268 }
2269
2270 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2271 // interior arrays, do we care? Why aren't nested arrays represented the
2272 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002273 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002274 QualType EltTy(Ty, 0);
2275 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2276 // If the number of elements is known, then count is that number. Otherwise,
2277 // it's -1. This allows us to represent a subrange with an array of 0
2278 // elements, like this:
2279 //
2280 // struct foo {
2281 // int x[0];
2282 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002283 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002284 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002286 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002287 if (Expr *Size = VAT->getSizeExpr()) {
2288 llvm::APSInt V;
2289 if (Size->EvaluateAsInt(V, CGM.getContext()))
2290 Count = V.getExtValue();
2291 }
David Blaikie87173f12016-08-22 17:49:56 +00002292 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002293
Guy Benyei11169dd2012-12-18 14:30:41 +00002294 // FIXME: Verify this is right for VLAs.
2295 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2296 EltTy = Ty->getElementType();
2297 }
2298
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002299 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002300
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002301 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2302 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002303}
2304
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002305llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2306 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002307 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2308 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002309}
2310
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002311llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2312 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002313 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2314 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002315}
2316
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002317llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2318 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002319 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002320 uint64_t Size = 0;
2321
2322 if (!Ty->isIncompleteType()) {
2323 Size = CGM.getContext().getTypeSize(Ty);
2324
2325 // Set the MS inheritance model. There is no flag for the unspecified model.
2326 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2327 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2328 case MSInheritanceAttr::Keyword_single_inheritance:
2329 Flags |= llvm::DINode::FlagSingleInheritance;
2330 break;
2331 case MSInheritanceAttr::Keyword_multiple_inheritance:
2332 Flags |= llvm::DINode::FlagMultipleInheritance;
2333 break;
2334 case MSInheritanceAttr::Keyword_virtual_inheritance:
2335 Flags |= llvm::DINode::FlagVirtualInheritance;
2336 break;
2337 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2338 break;
2339 }
2340 }
2341 }
2342
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002343 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002344 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002345 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002346 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2347 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002348
2349 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002350 Ty->getPointeeType()->getAs<FunctionProtoType>();
2351 return DBuilder.createMemberPointerType(
2352 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2353 Ty->getClass(), FPT->getTypeQuals())),
2354 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002355 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002356}
2357
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002358llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002359 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2360 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002361}
2362
Xiuli Pan9c14e282016-01-09 12:53:17 +00002363llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2364 llvm::DIFile *U) {
2365 return getOrCreateType(Ty->getElementType(), U);
2366}
2367
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002368llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002369 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002370
Guy Benyei11169dd2012-12-18 14:30:41 +00002371 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002372 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002373 if (!ED->getTypeForDecl()->isIncompleteType()) {
2374 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002375 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002376 }
2377
Manman Rene0064d82013-08-29 23:19:58 +00002378 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2379
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002380 bool isImportedFromModule =
2381 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2382
Guy Benyei11169dd2012-12-18 14:30:41 +00002383 // If this is just a forward declaration, construct an appropriately
2384 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002385 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002386 // Note that it is possible for enums to be created as part of
2387 // their own declcontext. In this case a FwdDecl will be created
2388 // twice. This doesn't cause a problem because both FwdDecls are
2389 // entered into the ReplaceMap: finalize() will replace the first
2390 // FwdDecl with the second and then replace the second with
2391 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002392 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002393 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002394 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2395 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002396
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 unsigned Line = getLineNumber(ED->getLocation());
2398 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002399 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002400 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2401 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002402
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002403 ReplaceMap.emplace_back(
2404 std::piecewise_construct, std::make_tuple(Ty),
2405 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002406 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 }
2408
David Blaikie483a9da2014-05-06 18:35:21 +00002409 return CreateTypeDefinition(Ty);
2410}
2411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002413 const EnumDecl *ED = Ty->getDecl();
2414 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002415 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +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());
David Blaikie483a9da2014-05-06 18:35:21 +00002419 }
2420
2421 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2422
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002423 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002424 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002425 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002426 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002427 Enumerators.push_back(DBuilder.createEnumerator(
2428 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002429 }
2430
2431 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002432 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002433
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002434 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002435 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002436 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002438 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2439 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2440 Line, Size, Align, EltArray, ClassTy,
2441 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002442}
2443
Amjad Aboud546bc112017-02-09 22:07:24 +00002444llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2445 unsigned MType, SourceLocation LineLoc,
2446 StringRef Name, StringRef Value) {
2447 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2448 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2449}
2450
2451llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2452 SourceLocation LineLoc,
2453 SourceLocation FileLoc) {
2454 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2455 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2456 return DBuilder.createTempMacroFile(Parent, Line, FName);
2457}
2458
David Blaikie05491062013-01-21 04:37:12 +00002459static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2460 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002461 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002462 Qualifiers InnerQuals = T.getLocalQualifiers();
2463 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2464 // that is already there.
2465 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2466 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002467 QualType LastT = T;
2468 switch (T->getTypeClass()) {
2469 default:
David Blaikie05491062013-01-21 04:37:12 +00002470 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002471 case Type::TemplateSpecialization: {
2472 const auto *Spec = cast<TemplateSpecializationType>(T);
2473 if (Spec->isTypeAlias())
2474 return C.getQualifiedType(T.getTypePtr(), Quals);
2475 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002476 break;
2477 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 case Type::TypeOfExpr:
2479 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2480 break;
2481 case Type::TypeOf:
2482 T = cast<TypeOfType>(T)->getUnderlyingType();
2483 break;
2484 case Type::Decltype:
2485 T = cast<DecltypeType>(T)->getUnderlyingType();
2486 break;
2487 case Type::UnaryTransform:
2488 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2489 break;
2490 case Type::Attributed:
2491 T = cast<AttributedType>(T)->getEquivalentType();
2492 break;
2493 case Type::Elaborated:
2494 T = cast<ElaboratedType>(T)->getNamedType();
2495 break;
2496 case Type::Paren:
2497 T = cast<ParenType>(T)->getInnerType();
2498 break;
David Blaikie05491062013-01-21 04:37:12 +00002499 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002500 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002502 case Type::Auto:
2503 case Type::DeducedTemplateSpecialization: {
2504 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002505 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002506 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 break;
2508 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002509 case Type::Adjusted:
2510 case Type::Decayed:
2511 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2512 T = cast<AdjustedType>(T)->getAdjustedType();
2513 break;
2514 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002515
Guy Benyei11169dd2012-12-18 14:30:41 +00002516 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002517 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 } while (true);
2519}
2520
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002521llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002522
2523 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002524 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002525
David Blaikief427b002014-05-06 03:42:01 +00002526 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 if (it != TypeCache.end()) {
2528 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002529 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 }
2532
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002533 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002534}
2535
David Blaikie0e716b42014-03-03 23:48:23 +00002536void CGDebugInfo::completeTemplateDefinition(
2537 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002538 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002539 return;
2540
David Blaikie0e716b42014-03-03 23:48:23 +00002541 completeClassData(&SD);
2542 // In case this type has no member function definitions being emitted, ensure
2543 // it is retained
2544 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2545}
2546
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002547llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002549 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002550
2551 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002552 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002553
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002554 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 return T;
2556
Adrian Prantlca844182015-09-11 17:23:03 +00002557 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002558 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002559
2560 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002561 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 return Res;
2564}
2565
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002566llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002567 // A forward declaration inside a module header does not belong to the module.
2568 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2569 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002570 if (DebugTypeExtRefs && D->isFromASTFile()) {
2571 // Record a reference to an imported clang module or precompiled header.
2572 auto *Reader = CGM.getContext().getExternalSource();
2573 auto Idx = D->getOwningModuleID();
2574 auto Info = Reader->getSourceDescriptor(Idx);
2575 if (Info)
2576 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2577 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002578 // We are building a clang module or a precompiled header.
2579 //
2580 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2581 // and it wouldn't be necessary to specify the parent scope
2582 // because the type is already unique by definition (it would look
2583 // like the output of -fno-standalone-debug). On the other hand,
2584 // the parent scope helps a consumer to quickly locate the object
2585 // file where the type's definition is located, so it might be
2586 // best to make this behavior a command line or debugger tuning
2587 // option.
2588 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2589 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002590 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002591 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2592 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002593 } else {
2594 // This the precompiled header being built.
2595 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002596 }
2597 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002598
Adrian Prantl9402cef2015-09-20 16:51:35 +00002599 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002600}
2601
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002602llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002603 // Handle qualifiers, which recursively handles what they refer to.
2604 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002605 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002606
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 // Work out details of type.
2608 switch (Ty->getTypeClass()) {
2609#define TYPE(Class, Base)
2610#define ABSTRACT_TYPE(Class, Base)
2611#define NON_CANONICAL_TYPE(Class, Base)
2612#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2613#include "clang/AST/TypeNodes.def"
2614 llvm_unreachable("Dependent types cannot show up in debug information");
2615
2616 case Type::ExtVector:
2617 case Type::Vector:
2618 return CreateType(cast<VectorType>(Ty), Unit);
2619 case Type::ObjCObjectPointer:
2620 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2621 case Type::ObjCObject:
2622 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002623 case Type::ObjCTypeParam:
2624 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 case Type::ObjCInterface:
2626 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2627 case Type::Builtin:
2628 return CreateType(cast<BuiltinType>(Ty));
2629 case Type::Complex:
2630 return CreateType(cast<ComplexType>(Ty));
2631 case Type::Pointer:
2632 return CreateType(cast<PointerType>(Ty), Unit);
2633 case Type::BlockPointer:
2634 return CreateType(cast<BlockPointerType>(Ty), Unit);
2635 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002636 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002637 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002638 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002640 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 case Type::FunctionProto:
2642 case Type::FunctionNoProto:
2643 return CreateType(cast<FunctionType>(Ty), Unit);
2644 case Type::ConstantArray:
2645 case Type::VariableArray:
2646 case Type::IncompleteArray:
2647 return CreateType(cast<ArrayType>(Ty), Unit);
2648
2649 case Type::LValueReference:
2650 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2651 case Type::RValueReference:
2652 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2653
2654 case Type::MemberPointer:
2655 return CreateType(cast<MemberPointerType>(Ty), Unit);
2656
2657 case Type::Atomic:
2658 return CreateType(cast<AtomicType>(Ty), Unit);
2659
Xiuli Pan9c14e282016-01-09 12:53:17 +00002660 case Type::Pipe:
2661 return CreateType(cast<PipeType>(Ty), Unit);
2662
Guy Benyei11169dd2012-12-18 14:30:41 +00002663 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002664 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2665
David Blaikie42edade2014-11-11 20:44:45 +00002666 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002667 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002668 case Type::Adjusted:
2669 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002670 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002671 case Type::Elaborated:
2672 case Type::Paren:
2673 case Type::SubstTemplateTypeParm:
2674 case Type::TypeOfExpr:
2675 case Type::TypeOf:
2676 case Type::Decltype:
2677 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002678 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002679 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002681
David Blaikie42edade2014-11-11 20:44:45 +00002682 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002683}
2684
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002685llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2686 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002687 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002688
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002689 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002690
2691 // We may have cached a forward decl when we could have created
2692 // a non-forward decl. Go ahead and create a non-forward decl
2693 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002694 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002695 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002696
2697 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002698 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002699
2700 // Propagate members from the declaration to the definition
2701 // CreateType(const RecordType*) will overwrite this with the members in the
2702 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002703 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002704
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002706 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 return Res;
2708}
2709
2710// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002711llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002712 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002713
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002715 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 unsigned Line = getLineNumber(RD->getLocation());
2717 StringRef RDName = getClassName(RD);
2718
Amjad Abouddc4531e2016-04-30 01:44:38 +00002719 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002720
David Blaikied2785892013-08-18 17:36:19 +00002721 // If we ended up creating the type during the context chain construction,
2722 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002723 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002724 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002725 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002726 return T;
David Blaikied2785892013-08-18 17:36:19 +00002727
Adrian Prantl381e7552014-02-04 21:29:50 +00002728 // If this is just a forward or incomplete declaration, construct an
2729 // appropriately marked node and just return it.
2730 const RecordDecl *D = RD->getDefinition();
2731 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002732 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002733
2734 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002735 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002736
Manman Rene0064d82013-08-29 23:19:58 +00002737 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2738
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002739 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002740 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2741 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002742
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002743 // Elements of composite types usually have back to the type, creating
2744 // uniquing cycles. Distinct nodes are more efficient.
2745 switch (RealDecl->getTag()) {
2746 default:
2747 llvm_unreachable("invalid composite type tag");
2748
2749 case llvm::dwarf::DW_TAG_array_type:
2750 case llvm::dwarf::DW_TAG_enumeration_type:
2751 // Array elements and most enumeration elements don't have back references,
2752 // so they don't tend to be involved in uniquing cycles and there is some
2753 // chance of merging them when linking together two modules. Only make
2754 // them distinct if they are ODR-uniqued.
2755 if (FullName.empty())
2756 break;
2757
2758 case llvm::dwarf::DW_TAG_structure_type:
2759 case llvm::dwarf::DW_TAG_union_type:
2760 case llvm::dwarf::DW_TAG_class_type:
2761 // Immediatley resolve to a distinct node.
2762 RealDecl =
2763 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2764 break;
2765 }
2766
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002767 RegionMap[Ty->getDecl()].reset(RealDecl);
2768 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002769
David Majnemer58ed0f32016-07-17 00:39:12 +00002770 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002771 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002772 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002773 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002774}
2775
David Blaikieadfbf992013-08-18 16:55:33 +00002776void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002777 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002778 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002779 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002780 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2781 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002782 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002783 while (1) {
2784 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2785 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2786 if (PBT && !BRL.isPrimaryBaseVirtual())
2787 PBase = PBT;
2788 else
2789 break;
2790 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002791 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002792 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2793 getOrCreateFile(RD->getLocation())));
2794 } else if (RD->isDynamicClass())
2795 ContainingType = RealDecl;
2796
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002797 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002798}
2799
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002800llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002801 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002802 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002804 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002805 llvm::DIType *Ty =
2806 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2807 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 *Offset += FieldSize;
2809 return Ty;
2810}
2811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002812void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002813 StringRef &Name,
2814 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002815 llvm::DIScope *&FDContext,
2816 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002817 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002818 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002819 Name = getFunctionName(FD);
2820 // Use mangled name as linkage name for C/C++ functions.
2821 if (FD->hasPrototype()) {
2822 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002823 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002824 }
2825 // No need to replicate the linkage name if it isn't different from the
2826 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002827 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002828 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2829 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002830 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002831 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002832 LinkageName = StringRef();
2833
Benjamin Kramer8c305922016-02-02 11:06:51 +00002834 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002835 if (const NamespaceDecl *NSDecl =
2836 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2837 FDContext = getOrCreateNameSpace(NSDecl);
2838 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002839 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2840 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2841 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2842 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002843 // Check if it is a noreturn-marked function
2844 if (FD->isNoReturn())
2845 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002846 // Collect template parameters.
2847 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2848 }
2849}
2850
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002851void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002852 unsigned &LineNo, QualType &T,
2853 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002854 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002855 Unit = getOrCreateFile(VD->getLocation());
2856 LineNo = getLineNumber(VD->getLocation());
2857
2858 setLocation(VD->getLocation());
2859
2860 T = VD->getType();
2861 if (T->isIncompleteArrayType()) {
2862 // CodeGen turns int[] into int[1] so we'll do the same here.
2863 llvm::APInt ConstVal(32, 1);
2864 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2865
2866 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2867 ArrayType::Normal, 0);
2868 }
2869
2870 Name = VD->getName();
2871 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2872 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2873 LinkageName = CGM.getMangledName(VD);
2874 if (LinkageName == Name)
2875 LinkageName = StringRef();
2876
2877 // Since we emit declarations (DW_AT_members) for static members, place the
2878 // definition of those static members in the namespace they were declared in
2879 // in the source code (the lexical decl context).
2880 // FIXME: Generalize this for even non-member global variables where the
2881 // declaration and definition may have different lexical decl contexts, once
2882 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002883 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2884 : VD->getDeclContext();
2885 // When a record type contains an in-line initialization of a static data
2886 // member, and the record type is marked as __declspec(dllexport), an implicit
2887 // definition of the member will be created in the record context. DWARF
2888 // doesn't seem to have a nice way to describe this in a form that consumers
2889 // are likely to understand, so fake the "normal" situation of a definition
2890 // outside the class by putting it in the global scope.
2891 if (DC->isRecord())
2892 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002893
Amjad Abouddc4531e2016-04-30 01:44:38 +00002894 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2895 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002896}
2897
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002898llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2899 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002900 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002901 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002902 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002903 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002904 llvm::DIFile *Unit = getOrCreateFile(Loc);
2905 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002906 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002907 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002908 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002909 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2910
Frederic Rissd253ed62014-11-18 03:40:51 +00002911 // Build function type.
2912 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002913 if (FD)
2914 for (const ParmVarDecl *Parm : FD->parameters())
2915 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002916 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2917 QualType FnType = CGM.getContext().getFunctionType(
2918 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002919 if (Stub) {
2920 return DBuilder.createFunction(
2921 DContext, Name, LinkageName, Unit, Line,
2922 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2923 !FD->isExternallyVisible(),
2924 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2925 TParamsArray.get(), getFunctionDeclaration(FD));
2926 }
2927
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002928 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002929 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002930 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2931 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002932 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002933 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002934 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002935 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2936 std::make_tuple(CanonDecl),
2937 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002938 return SP;
2939}
2940
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002941llvm::DISubprogram *
2942CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
2943 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
2944}
2945
2946llvm::DISubprogram *
2947CGDebugInfo::getFunctionStub(GlobalDecl GD) {
2948 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
2949}
2950
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002951llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002952CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2953 QualType T;
2954 StringRef Name, LinkageName;
2955 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002956 llvm::DIFile *Unit = getOrCreateFile(Loc);
2957 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002958 unsigned Line = getLineNumber(Loc);
2959
2960 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002961 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002962 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2963 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002964 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002965 FwdDeclReplaceMap.emplace_back(
2966 std::piecewise_construct,
2967 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2968 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002969 return GV;
2970}
2971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002972llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002973 // We only need a declaration (not a definition) of the type - so use whatever
2974 // we would otherwise do to get a type for a pointee. (forward declarations in
2975 // limited debug info, full definitions (if the type definition is available)
2976 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002977 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002978 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002979 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002980 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002981
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002982 if (I != DeclCache.end()) {
2983 auto N = I->second;
2984 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
2985 return GVE->getVariable();
2986 return dyn_cast_or_null<llvm::DINode>(N);
2987 }
Frederic Rissd253ed62014-11-18 03:40:51 +00002988
2989 // No definition for now. Emit a forward definition that might be
2990 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002991 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002992 return getFunctionForwardDeclaration(FD);
2993 else if (const auto *VD = dyn_cast<VarDecl>(D))
2994 return getGlobalVariableForwardDeclaration(VD);
2995
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002996 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002997}
2998
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002999llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003000 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003001 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003002
David Majnemer58ed0f32016-07-17 00:39:12 +00003003 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003004 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003005 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003006
3007 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003008 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003009
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003010 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003011 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003012 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003013 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003014 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003015 }
3016 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003018 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003019 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 return SP;
3021 }
3022
Aaron Ballman86c93902014-03-06 23:45:36 +00003023 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003024 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003026 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003027 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 return SP;
3029 }
3030 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003031 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003032}
3033
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003034// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003035// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003036llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003037 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003038 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003039 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003040 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3041 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003042 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003043
David Majnemer58ed0f32016-07-17 00:39:12 +00003044 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003046
3047 const auto *FTy = FnType->getAs<FunctionType>();
3048 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3049
David Majnemer58ed0f32016-07-17 00:39:12 +00003050 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003051 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003052 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003053
3054 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003055 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003056
3057 // Replace the instancetype keyword with the actual type.
3058 if (ResultTy == CGM.getContext().getObjCInstanceType())
3059 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003060 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003061
Adrian Prantl7bec9032013-05-10 21:08:31 +00003062 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003063 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003064 QualType SelfDeclTy;
3065 if (auto *SelfDecl = OMethod->getSelfDecl())
3066 SelfDeclTy = SelfDecl->getType();
3067 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3068 if (FPT->getNumParams() > 1)
3069 SelfDeclTy = FPT->getParamType(0);
3070 if (!SelfDeclTy.isNull())
3071 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003073 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003074 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003076 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003077 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003078 // Variadic methods need a special marker at the end of the type list.
3079 if (OMethod->isVariadic())
3080 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003081
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003082 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003083 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3084 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003085 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003086
Adrian Prantl800faef2014-02-25 23:42:18 +00003087 // Handle variadic function types; they need an additional
3088 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003089 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003090 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003091 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003092 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003093 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3094 for (QualType ParamType : FPT->param_types())
3095 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003096 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003097 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003098 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3099 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003100 }
3101
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003102 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003103}
3104
Eric Christophere7b87e52014-10-26 23:40:33 +00003105void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3106 SourceLocation ScopeLoc, QualType FnType,
3107 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003108
3109 StringRef Name;
3110 StringRef LinkageName;
3111
3112 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3113
3114 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003115 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003116
Leny Kholodov80c047d2016-09-06 10:48:04 +00003117 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003118 llvm::DIFile *Unit = getOrCreateFile(Loc);
3119 llvm::DIScope *FDContext = Unit;
3120 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003121 if (!HasDecl) {
3122 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003123 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003124 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003125 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003126 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003128 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003129 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003130 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003131 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 return;
3133 }
3134 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003135 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3136 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003137 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003139 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003140 } else {
3141 // Use llvm function name.
3142 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003143 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003145 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 Name = Name.substr(1);
3147
Adrian Prantl42d71b92014-04-10 23:21:53 +00003148 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003149 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003150 // Artificial functions should not silently reuse CurLoc.
3151 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003152 }
3153 unsigned LineNo = getLineNumber(Loc);
3154 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003155
Eric Christopher8018e412014-03-27 18:50:35 +00003156 // FIXME: The function declaration we're constructing here is mostly reusing
3157 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3158 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3159 // all subprograms instead of the actual context since subprogram definitions
3160 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003161 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003163 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003164 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003165 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003166 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003167 // We might get here with a VarDecl in the case we're generating
3168 // code for the initialization of globals. Do not record these decls
3169 // as they will overwrite the actual VarDecl Decl in the cache.
3170 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003171 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003172
Adrian Prantlbebb8932014-03-21 21:01:58 +00003173 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003174 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003175
Guy Benyei11169dd2012-12-18 14:30:41 +00003176 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003177 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003178}
3179
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003180void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3181 QualType FnType) {
3182 StringRef Name;
3183 StringRef LinkageName;
3184
3185 const Decl *D = GD.getDecl();
3186 if (!D)
3187 return;
3188
Leny Kholodov80c047d2016-09-06 10:48:04 +00003189 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003190 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003191 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003192 llvm::DINodeArray TParamsArray;
3193 if (isa<FunctionDecl>(D)) {
3194 // If there is a DISubprogram for this function available then use it.
3195 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3196 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003197 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003198 Name = getObjCMethodName(OMD);
3199 Flags |= llvm::DINode::FlagPrototyped;
3200 } else {
3201 llvm_unreachable("not a function or ObjC method");
3202 }
3203 if (!Name.empty() && Name[0] == '\01')
3204 Name = Name.substr(1);
3205
3206 if (D->isImplicit()) {
3207 Flags |= llvm::DINode::FlagArtificial;
3208 // Artificial functions without a location should not silently reuse CurLoc.
3209 if (Loc.isInvalid())
3210 CurLoc = SourceLocation();
3211 }
3212 unsigned LineNo = getLineNumber(Loc);
3213 unsigned ScopeLine = 0;
3214
Adrian Prantle76bda52016-04-15 15:55:45 +00003215 DBuilder.retainType(DBuilder.createFunction(
3216 FDContext, Name, LinkageName, Unit, LineNo,
3217 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3218 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3219 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003220}
3221
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003222void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3223 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3224 // If there is a subprogram for this function available then use it.
3225 auto FI = SPCache.find(FD->getCanonicalDecl());
3226 llvm::DISubprogram *SP = nullptr;
3227 if (FI != SPCache.end())
3228 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
3229 if (!SP)
3230 SP = getFunctionStub(GD);
3231 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3232 LexicalBlockStack.emplace_back(SP);
3233 setInlinedAt(Builder.getCurrentDebugLocation());
3234 EmitLocation(Builder, FD->getLocation());
3235}
3236
3237void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3238 assert(CurInlinedAt && "unbalanced inline scope stack");
3239 EmitFunctionEnd(Builder);
3240 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3241}
3242
David Blaikie835afb22015-01-21 23:08:17 +00003243void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003244 // Update our current location
3245 setLocation(Loc);
3246
Eric Christophere7b87e52014-10-26 23:40:33 +00003247 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3248 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003249
Adrian Prantle83b1302014-01-07 22:05:52 +00003250 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003251 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003252 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003253}
3254
Guy Benyei11169dd2012-12-18 14:30:41 +00003255void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003256 llvm::MDNode *Back = nullptr;
3257 if (!LexicalBlockStack.empty())
3258 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003259 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003260 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003261 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003262}
3263
Eric Christopher0fdcb312013-05-16 00:52:20 +00003264void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3265 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003266 // Set our current location.
3267 setLocation(Loc);
3268
Guy Benyei11169dd2012-12-18 14:30:41 +00003269 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003270 Builder.SetCurrentDebugLocation(
3271 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3272 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003273
Benjamin Kramer8c305922016-02-02 11:06:51 +00003274 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003275 return;
3276
3277 // Create a new lexical block and push it on the stack.
3278 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003279}
3280
Eric Christopher0fdcb312013-05-16 00:52:20 +00003281void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3282 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003283 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3284
3285 // Provide an entry in the line table for the end of the block.
3286 EmitLocation(Builder, Loc);
3287
Benjamin Kramer8c305922016-02-02 11:06:51 +00003288 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003289 return;
3290
Guy Benyei11169dd2012-12-18 14:30:41 +00003291 LexicalBlockStack.pop_back();
3292}
3293
Guy Benyei11169dd2012-12-18 14:30:41 +00003294void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3295 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3296 unsigned RCount = FnBeginRegionCount.back();
3297 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3298
3299 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003300 while (LexicalBlockStack.size() != RCount) {
3301 // Provide an entry in the line table for the end of the block.
3302 EmitLocation(Builder, CurLoc);
3303 LexicalBlockStack.pop_back();
3304 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 FnBeginRegionCount.pop_back();
3306}
3307
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003308llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003309 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003310
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003311 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003312 QualType FType;
3313 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003314 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003315
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003316 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003317 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003318
3319 FieldOffset = 0;
3320 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3321 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3322 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3323 FType = CGM.getContext().IntTy;
3324 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3325 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3326
3327 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3328 if (HasCopyAndDispose) {
3329 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003330 EltTys.push_back(
3331 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3332 EltTys.push_back(
3333 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003334 }
3335 bool HasByrefExtendedLayout;
3336 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003337 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3338 HasByrefExtendedLayout) &&
3339 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003340 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003341 EltTys.push_back(
3342 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003343 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003344
Guy Benyei11169dd2012-12-18 14:30:41 +00003345 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3346 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003347 CGM.getTarget().getPointerAlign(0))) {
3348 CharUnits FieldOffsetInBytes =
3349 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003350 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003351 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003352
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 if (NumPaddingBytes.isPositive()) {
3354 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3355 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3356 pad, ArrayType::Normal, 0);
3357 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3358 }
3359 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003360
Guy Benyei11169dd2012-12-18 14:30:41 +00003361 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003362 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 FieldSize = CGM.getContext().getTypeSize(FType);
3364 FieldAlign = CGM.getContext().toBits(Align);
3365
Eric Christopherb2a008c2013-05-16 00:45:12 +00003366 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003367 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003368 FieldAlign, FieldOffset,
3369 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 EltTys.push_back(FieldTy);
3371 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003373 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003374
Leny Kholodov80c047d2016-09-06 10:48:04 +00003375 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003376
Guy Benyei11169dd2012-12-18 14:30:41 +00003377 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003378 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003379}
3380
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003381void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3382 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003383 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003384 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003385 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003386 if (VD->hasAttr<NoDebugAttr>())
3387 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003388
David Blaikie7fceebf2013-08-19 03:37:48 +00003389 bool Unwritten =
3390 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3391 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003392 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003393 if (!Unwritten)
3394 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003395 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 uint64_t XOffset = 0;
3397 if (VD->hasAttr<BlocksAttr>())
3398 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003399 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 Ty = getOrCreateType(VD->getType(), Unit);
3401
3402 // If there is no debug info for this type then do not emit debug info
3403 // for this variable.
3404 if (!Ty)
3405 return;
3406
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003408 unsigned Line = 0;
3409 unsigned Column = 0;
3410 if (!Unwritten) {
3411 Line = getLineNumber(VD->getLocation());
3412 Column = getColumnNumber(VD->getLocation());
3413 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003414 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003415 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003416 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003417 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003418
3419 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3420
Guy Benyei11169dd2012-12-18 14:30:41 +00003421 // If this is the first argument and it is implicit then
3422 // give it an object pointer flag.
3423 // FIXME: There has to be a better way to do this, but for static
3424 // functions there won't be an implicit param at arg1 and
3425 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003426 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003427 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003428 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003429 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3430 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003431 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003432
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003433 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003434
3435 StringRef Name = VD->getName();
3436 if (!Name.empty()) {
3437 if (VD->hasAttr<BlocksAttr>()) {
3438 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003439 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003440 // offset of __forwarding field
3441 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003442 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003443 Expr.push_back(offset.getQuantity());
3444 Expr.push_back(llvm::dwarf::DW_OP_deref);
3445 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003446 // offset of x field
3447 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003448 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003449
3450 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003451 auto *D = ArgNo
3452 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3453 *ArgNo, Unit, Line, Ty)
3454 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003455 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003456
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003458 DBuilder.insertDeclare(
3459 Storage, D, DBuilder.createExpression(Expr),
3460 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3461 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003462 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003463 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003464 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003465 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003466 // If VD is an anonymous union then Storage represents value for
3467 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003468 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003469 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003470 // GDB has trouble finding local variables in anonymous unions, so we emit
3471 // artifical local variables for each of the members.
3472 //
3473 // FIXME: Remove this code as soon as GDB supports this.
3474 // The debug info verifier in LLVM operates based on the assumption that a
3475 // variable has the same size as its storage and we had to disable the check
3476 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003477 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003478 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003479 StringRef FieldName = Field->getName();
3480
3481 // Ignore unnamed fields. Do not ignore unnamed records.
3482 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3483 continue;
3484
3485 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003486 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003487 auto *D = DBuilder.createAutoVariable(
3488 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003489 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003490
3491 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003492 DBuilder.insertDeclare(
3493 Storage, D, DBuilder.createExpression(Expr),
3494 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3495 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003496 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003497 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003498 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003499
3500 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003501 auto *D = ArgNo
3502 ? DBuilder.createParameterVariable(
3503 Scope, Name, *ArgNo, Unit, Line, Ty,
3504 CGM.getLangOpts().Optimize, Flags)
3505 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3506 CGM.getLangOpts().Optimize, Flags,
3507 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003508
3509 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003510 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003511 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003512 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003513}
3514
3515void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3516 llvm::Value *Storage,
3517 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003518 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003519 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003520}
3521
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003522llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3523 llvm::DIType *Ty) {
3524 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003525 if (CachedTy)
3526 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003527 return DBuilder.createObjectPointerType(Ty);
3528}
3529
Eric Christophere7b87e52014-10-26 23:40:33 +00003530void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3531 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003532 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003533 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003534 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003535
Craig Topper8a13c412014-05-21 05:09:00 +00003536 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003537 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003538 if (VD->hasAttr<NoDebugAttr>())
3539 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003540
Guy Benyei11169dd2012-12-18 14:30:41 +00003541 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003542
Guy Benyei11169dd2012-12-18 14:30:41 +00003543 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003544 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3545 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003546 if (isByRef)
3547 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003548 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003549 Ty = getOrCreateType(VD->getType(), Unit);
3550
3551 // Self is passed along as an implicit non-arg variable in a
3552 // block. Mark it as the object pointer.
3553 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003554 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003555
3556 // Get location information.
3557 unsigned Line = getLineNumber(VD->getLocation());
3558 unsigned Column = getColumnNumber(VD->getLocation());
3559
3560 const llvm::DataLayout &target = CGM.getDataLayout();
3561
3562 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003563 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003564 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3565
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003566 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003567 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003568 addr.push_back(llvm::dwarf::DW_OP_deref);
3569 addr.push_back(llvm::dwarf::DW_OP_plus);
3570 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003571 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003572 addr.push_back(llvm::dwarf::DW_OP_deref);
3573 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003575 offset =
3576 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003577 addr.push_back(offset.getQuantity());
3578 addr.push_back(llvm::dwarf::DW_OP_deref);
3579 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003580 // offset of x field
3581 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003582 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003583 }
3584
3585 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003586 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003587 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003588 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003589 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003590
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003592 auto DL =
3593 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003594 if (InsertPoint)
3595 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3596 InsertPoint);
3597 else
3598 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3599 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003600}
3601
Guy Benyei11169dd2012-12-18 14:30:41 +00003602void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3603 unsigned ArgNo,
3604 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003605 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003606 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003607}
3608
3609namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003610struct BlockLayoutChunk {
3611 uint64_t OffsetInBits;
3612 const BlockDecl::Capture *Capture;
3613};
3614bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3615 return l.OffsetInBits < r.OffsetInBits;
3616}
Guy Benyei11169dd2012-12-18 14:30:41 +00003617}
3618
3619void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003620 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003621 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003622 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003624 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003625 ASTContext &C = CGM.getContext();
3626 const BlockDecl *blockDecl = block.getBlockDecl();
3627
3628 // Collect some general information about the block's location.
3629 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003630 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003631 unsigned line = getLineNumber(loc);
3632 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003633
Guy Benyei11169dd2012-12-18 14:30:41 +00003634 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003635 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003636
3637 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003638 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003639
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003640 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003641 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003642 blockLayout->getElementOffsetInBits(0),
3643 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003644 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003645 blockLayout->getElementOffsetInBits(1),
3646 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003647 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003648 blockLayout->getElementOffsetInBits(2),
3649 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003650 auto *FnTy = block.getBlockExpr()->getFunctionType();
3651 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003652 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003653 blockLayout->getElementOffsetInBits(3),
3654 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003655 fields.push_back(createFieldType(
3656 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3657 ? C.getBlockDescriptorExtendedType()
3658 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003659 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003660
3661 // We want to sort the captures by offset, not because DWARF
3662 // requires this, but because we're paranoid about debuggers.
3663 SmallVector<BlockLayoutChunk, 8> chunks;
3664
3665 // 'this' capture.
3666 if (blockDecl->capturesCXXThis()) {
3667 BlockLayoutChunk chunk;
3668 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003669 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003670 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003671 chunks.push_back(chunk);
3672 }
3673
3674 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003675 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003676 const VarDecl *variable = capture.getVariable();
3677 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3678
3679 // Ignore constant captures.
3680 if (captureInfo.isConstant())
3681 continue;
3682
3683 BlockLayoutChunk chunk;
3684 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003685 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003686 chunk.Capture = &capture;
3687 chunks.push_back(chunk);
3688 }
3689
3690 // Sort by offset.
3691 llvm::array_pod_sort(chunks.begin(), chunks.end());
3692
David Majnemer58ed0f32016-07-17 00:39:12 +00003693 for (const BlockLayoutChunk &Chunk : chunks) {
3694 uint64_t offsetInBits = Chunk.OffsetInBits;
3695 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003696
3697 // If we have a null capture, this must be the C++ 'this' capture.
3698 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003699 QualType type;
3700 if (auto *Method =
3701 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3702 type = Method->getThisType(C);
3703 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3704 type = QualType(RDecl->getTypeForDecl(), 0);
3705 else
3706 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003707
David Majnemerb4b671e2016-06-30 03:01:59 +00003708 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003709 offsetInBits, tunit, tunit));
3710 continue;
3711 }
3712
3713 const VarDecl *variable = capture->getVariable();
3714 StringRef name = variable->getName();
3715
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003716 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003717 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003718 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003719 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003720
3721 // FIXME: this creates a second copy of this type!
3722 uint64_t xoffset;
3723 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003724 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003725 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3726 PtrInfo.Width, Align, offsetInBits,
3727 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003728 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003729 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003730 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003731 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003732 }
3733 fields.push_back(fieldType);
3734 }
3735
3736 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003737 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3738 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003739
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003740 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003741
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003742 llvm::DIType *type =
3743 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003744 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003745 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003746 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3747
3748 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003749 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003750 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003751
3752 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003753 auto *debugVar = DBuilder.createParameterVariable(
3754 scope, Arg->getName(), ArgNo, tunit, line, type,
3755 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003756
Adrian Prantl616bef42013-03-14 21:52:59 +00003757 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003758 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003759 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003760 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003761 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3762 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003763 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003764
Adrian Prantl616bef42013-03-14 21:52:59 +00003765 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003766 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003767 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003768 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003769}
3770
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003771llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003772CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3773 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003774 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003775
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003776 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003777 if (MI != StaticDataMemberCache.end()) {
3778 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003779 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003780 }
David Blaikiece763042013-08-20 21:49:21 +00003781
3782 // If the member wasn't found in the cache, lazily construct and add it to the
3783 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003784 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003785 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003786 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003787}
3788
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003789llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003790 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3791 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003792 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003793
3794 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003795 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003796 StringRef FieldName = Field->getName();
3797
3798 // Ignore unnamed fields, but recurse into anonymous records.
3799 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003800 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003801 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003802 Var, DContext);
3803 continue;
3804 }
3805 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003806 GVE = DBuilder.createGlobalVariableExpression(
3807 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3808 Var->hasLocalLinkage());
3809 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003810 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003811 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003812}
3813
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003814void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003815 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003816 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003817 if (D->hasAttr<NoDebugAttr>())
3818 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003819
3820 // If we already created a DIGlobalVariable for this declaration, just attach
3821 // it to the llvm::GlobalVariable.
3822 auto Cached = DeclCache.find(D->getCanonicalDecl());
3823 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003824 return Var->addDebugInfo(
3825 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003826
Guy Benyei11169dd2012-12-18 14:30:41 +00003827 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003828 llvm::DIFile *Unit = nullptr;
3829 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003830 unsigned LineNo;
3831 StringRef DeclName, LinkageName;
3832 QualType T;
3833 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003834
3835 // Attempt to store one global variable for the declaration - even if we
3836 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003837 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003838
3839 // If this is an anonymous union then we'll want to emit a global
3840 // variable for each member of the anonymous union so that it's possible
3841 // to find the name of any field in the union.
3842 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003843 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003844 assert(RD->isAnonymousStructOrUnion() &&
3845 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003846 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003847 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003848 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003849 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003850 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003851 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003852 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003853 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003854 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003855 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003856}
3857
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003858void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003859 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003860 if (VD->hasAttr<NoDebugAttr>())
3861 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003862 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003863 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003864 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003865 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003866 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003867 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3868 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003869 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3870 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3871 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003872 // Do not use global variables for enums.
3873 //
3874 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003875 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003876 return;
David Blaikiea15565562014-04-04 20:56:17 +00003877 // Do not emit separate definitions for function local const/statics.
3878 if (isa<FunctionDecl>(VD->getDeclContext()))
3879 return;
David Blaikiebb113912014-04-05 07:23:17 +00003880 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003881 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003882 if (VarD->isStaticDataMember()) {
3883 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003884 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003885 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003886 //
3887 // FIXME: This is probably unnecessary, since Ty should reference RD
3888 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003889 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003890 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003891 return;
3892 }
3893
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003894 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003895
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003896 auto &GV = DeclCache[VD];
3897 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003898 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003899 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003900 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3901 // FIXME: Add a representation for integer constants wider than 64 bits.
3902 if (Init.isInt())
3903 InitExpr =
3904 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3905 else if (Init.isFloat())
3906 InitExpr = DBuilder.createConstantValueExpression(
3907 Init.getFloat().bitcastToAPInt().getZExtValue());
3908 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003909 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003910 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003911 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3912 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003913}
3914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003915llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003916 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003917 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003918 llvm::DIScope *Mod = getParentModuleOrNull(D);
3919 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003920}
3921
David Blaikie9f88fe82013-04-22 06:13:21 +00003922void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003923 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003924 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003925 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003926 if (!NSDecl->isAnonymousNamespace() ||
3927 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003928 DBuilder.createImportedModule(
3929 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3930 getOrCreateNameSpace(NSDecl),
3931 getLineNumber(UD.getLocation()));
3932 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003933}
3934
David Blaikiebd483762013-05-20 04:58:53 +00003935void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003936 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003937 return;
3938 assert(UD.shadow_size() &&
3939 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3940 // Emitting one decl is sufficient - debuggers can detect that this is an
3941 // overloaded name & provide lookup for all the overloads.
3942 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003943
3944 // FIXME: Skip functions with undeduced auto return type for now since we
3945 // don't currently have the plumbing for separate declarations & definitions
3946 // of free functions and mismatched types (auto in the declaration, concrete
3947 // return type in the definition)
3948 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3949 if (const auto *AT =
3950 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3951 if (AT->getDeducedType().isNull())
3952 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003953 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003954 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003955 DBuilder.createImportedDeclaration(
3956 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3957 getLineNumber(USD.getLocation()));
3958}
3959
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003960void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003961 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3962 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003963 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003964 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003965 DBuilder.createImportedDeclaration(
3966 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3967 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3968 getLineNumber(ID.getLocation()));
3969 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003970}
3971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003972llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003973CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003974 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003975 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003976 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003977 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003978 return cast<llvm::DIImportedEntity>(VH);
3979 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003980 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003981 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3982 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003983 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003984 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3985 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3986 NA.getName());
3987 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003988 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003989 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3990 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3991 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003992 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003993 return R;
3994}
3995
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003996llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003997CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003998 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003999 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00004000 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004001 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004002
Guy Benyei11169dd2012-12-18 14:30:41 +00004003 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004004 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004005 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00004006 llvm::DINamespace *NS = DBuilder.createNameSpace(
4007 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004008 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004009 return NS;
4010}
4011
Adrian Prantla5206ce2015-09-22 23:26:43 +00004012void CGDebugInfo::setDwoId(uint64_t Signature) {
4013 assert(TheCU && "no main compile unit");
4014 TheCU->setDWOId(Signature);
4015}
4016
4017
Guy Benyei11169dd2012-12-18 14:30:41 +00004018void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004019 // Creating types might create further types - invalidating the current
4020 // element and the size(), so don't cache/reference them.
4021 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4022 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004023 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004024 ? CreateTypeDefinition(E.Type, E.Unit)
4025 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004026 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004027 }
4028
David Blaikief427b002014-05-06 03:42:01 +00004029 for (auto p : ReplaceMap) {
4030 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004031 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004032 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004033
David Blaikief427b002014-05-06 03:42:01 +00004034 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004035 assert(it != TypeCache.end());
4036 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004037
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004038 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4039 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004040 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004041
Frederic Rissd253ed62014-11-18 03:40:51 +00004042 for (const auto &p : FwdDeclReplaceMap) {
4043 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004044 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004045 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004046
4047 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004048 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004049 // with ourselves, that will destroy the temporary MDNode and
4050 // replace it with a standard one, avoiding leaking memory.
4051 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004052 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004053 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004054 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004055
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004056 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4057 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004058 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004059 }
4060
Adrian Prantl73409ce2013-03-11 18:33:46 +00004061 // We keep our own list of retained types, because we need to look
4062 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004063 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004064 if (auto MD = TypeCache[RT])
4065 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004066
Guy Benyei11169dd2012-12-18 14:30:41 +00004067 DBuilder.finalize();
4068}
David Blaikie66088d52014-09-24 17:01:27 +00004069
4070void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004071 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004072 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004073
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004074 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004075 // Don't ignore in case of explicit cast where it is referenced indirectly.
4076 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004077}
Amara Emerson652795d2016-11-10 14:44:30 +00004078
4079llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4080 if (LexicalBlockStack.empty())
4081 return llvm::DebugLoc();
4082
4083 llvm::MDNode *Scope = LexicalBlockStack.back();
4084 return llvm::DebugLoc::get(
4085 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4086}