blob: 7eb0af2d371a8f532666ba80747f3a69be2286ec [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/Basic/FileManager.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000030#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000031#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000032#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000034#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000035#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000037#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000044#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000045#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046using namespace clang;
47using namespace clang::CodeGen;
48
Victor Leschuka7ece032016-10-20 00:13:19 +000049static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
50 auto TI = Ctx.getTypeInfo(Ty);
51 return TI.AlignIsRequired ? TI.Align : 0;
52}
53
54static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
55 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
56}
57
58static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
59 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
60}
61
Guy Benyei11169dd2012-12-18 14:30:41 +000062CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000063 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000064 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000066 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
67 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000068 CreateCompileUnit();
69}
70
71CGDebugInfo::~CGDebugInfo() {
72 assert(LexicalBlockStack.empty() &&
73 "Region stack mismatch, stack not empty!");
74}
75
David Blaikie66e41972015-01-14 07:38:27 +000076ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000077 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000078 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000079 init(TemporaryLocation);
80}
81
Adrian Prantl39428e72015-02-03 18:40:42 +000082ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000083 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000084 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000085 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000086 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000087}
88
89void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000090 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000091 auto *DI = CGF->getDebugInfo();
92 if (!DI) {
93 CGF = nullptr;
94 return;
David Blaikie66e41972015-01-14 07:38:27 +000095 }
David Blaikied7057d92015-08-12 23:49:57 +000096
97 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
98 if (TemporaryLocation.isValid()) {
99 DI->EmitLocation(CGF->Builder, TemporaryLocation);
100 return;
101 }
102
103 if (DefaultToEmpty) {
104 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
105 return;
106 }
107
108 // Construct a location that has a valid scope, but no line info.
109 assert(!DI->LexicalBlockStack.empty());
110 CGF->Builder.SetCurrentDebugLocation(
111 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000112}
113
David Blaikie9b479662015-01-25 01:19:10 +0000114ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000115 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000116 init(E->getExprLoc());
117}
118
David Blaikie66e41972015-01-14 07:38:27 +0000119ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000120 : CGF(&CGF) {
121 if (!CGF.getDebugInfo()) {
122 this->CGF = nullptr;
123 return;
David Blaikie66e41972015-01-14 07:38:27 +0000124 }
David Blaikied7057d92015-08-12 23:49:57 +0000125 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
126 if (Loc)
127 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000128}
129
130ApplyDebugLocation::~ApplyDebugLocation() {
131 // Query CGF so the location isn't overwritten when location updates are
132 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000133 if (CGF)
134 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000135}
136
Guy Benyei11169dd2012-12-18 14:30:41 +0000137void CGDebugInfo::setLocation(SourceLocation Loc) {
138 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000139 if (Loc.isInvalid())
140 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000141
142 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
143
144 // If we've changed files in the middle of a lexical scope go ahead
145 // and create a new lexical scope with file node if it's different
146 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000147 if (LexicalBlockStack.empty())
148 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000149
150 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000151 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000152 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000153
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000154 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000155 return;
156
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000157 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000159 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
160 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000161 } else if (isa<llvm::DILexicalBlock>(Scope) ||
162 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000163 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000164 LexicalBlockStack.emplace_back(
165 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000166 }
167}
168
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000169llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000170 llvm::DIScope *Mod = getParentModuleOrNull(D);
171 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
172 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000173}
174
175llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
176 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000177 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000178 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000180 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000181 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000182 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000183 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 }
185
186 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000187 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000188 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000189
David Majnemer58ed0f32016-07-17 00:39:12 +0000190 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000191 if (!RDecl->isDependentType())
192 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000193 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000194 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000195}
196
Guy Benyei11169dd2012-12-18 14:30:41 +0000197StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000198 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000200 FunctionTemplateSpecializationInfo *Info =
201 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000202
Reid Kleckner31abd802016-06-30 17:41:31 +0000203 // Emit the unqualified name in normal operation. LLVM and the debugger can
204 // compute the fully qualified name from the scope chain. If we're only
205 // emitting line table info, there won't be any scope chains, so emit the
206 // fully qualified name here so that stack traces are more accurate.
207 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
208 // evaluating the size impact.
209 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
210 CGM.getCodeGenOpts().EmitCodeView;
211
212 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 return FII->getName();
214
Benjamin Kramer9170e912013-02-22 15:46:01 +0000215 SmallString<128> NS;
216 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000217 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000218 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000219 if (!UseQualifiedName)
220 FD->printName(OS);
221 else
222 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000223
Reid Kleckner829398e2016-06-17 16:11:20 +0000224 // Add any template specialization args.
225 if (Info) {
226 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000227 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000228 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000229 }
230
231 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000232 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000233}
234
235StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
236 SmallString<256> MethodName;
237 llvm::raw_svector_ostream OS(MethodName);
238 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
239 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000240 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000242 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000243 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000244 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000245 if (OC->IsClassExtension()) {
246 OS << OC->getClassInterface()->getName();
247 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000248 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000249 << OC->getIdentifier()->getNameStart() << ')';
250 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000251 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000252 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
253 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000254 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000255 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000256 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000257 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000258 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000259 ClassTy.print(OS, PrintingPolicy(LangOptions()));
260 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000261 }
262 OS << ' ' << OMD->getSelector().getAsString() << ']';
263
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000264 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000265}
266
Guy Benyei11169dd2012-12-18 14:30:41 +0000267StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000268 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269}
270
Eric Christophere7b87e52014-10-26 23:40:33 +0000271StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000272 if (isa<ClassTemplateSpecializationDecl>(RD)) {
273 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000274 llvm::raw_svector_ostream OS(Name);
275 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
276 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000277
278 // Copy this name on the side and use its reference.
279 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000280 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000281
David Majnemerbc5976a2016-07-01 23:12:54 +0000282 // quick optimization to avoid having to intern strings that are already
283 // stored reliably elsewhere
284 if (const IdentifierInfo *II = RD->getIdentifier())
285 return II->getName();
286
287 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
288 // used to reconstruct the fully qualified type names.
289 if (CGM.getCodeGenOpts().EmitCodeView) {
290 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
291 assert(RD->getDeclContext() == D->getDeclContext() &&
292 "Typedef should not be in another decl context!");
293 assert(D->getDeclName().getAsIdentifierInfo() &&
294 "Typedef was not named!");
295 return D->getDeclName().getAsIdentifierInfo()->getName();
296 }
297
298 if (CGM.getLangOpts().CPlusPlus) {
299 StringRef Name;
300
301 ASTContext &Context = CGM.getContext();
302 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
303 // Anonymous types without a name for linkage purposes have their
304 // declarator mangled in if they have one.
305 Name = DD->getName();
306 else if (const TypedefNameDecl *TND =
307 Context.getTypedefNameForUnnamedTagDecl(RD))
308 // Anonymous types without a name for linkage purposes have their
309 // associate typedef mangled in if they have one.
310 Name = TND->getName();
311
312 if (!Name.empty()) {
313 SmallString<256> UnnamedType("<unnamed-type-");
314 UnnamedType += Name;
315 UnnamedType += '>';
316 return internString(UnnamedType);
317 }
318 }
319 }
320
321 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000322}
323
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000324llvm::DIFile::ChecksumKind
325CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
326 Checksum.clear();
327
328 if (!CGM.getCodeGenOpts().EmitCodeView)
329 return llvm::DIFile::CSK_None;
330
331 SourceManager &SM = CGM.getContext().getSourceManager();
332 bool Invalid;
333 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
334 if (Invalid)
335 return llvm::DIFile::CSK_None;
336
337 llvm::MD5 Hash;
338 llvm::MD5::MD5Result Result;
339
340 Hash.update(MemBuffer->getBuffer());
341 Hash.final(Result);
342
343 Hash.stringifyResult(Result, Checksum);
344 return llvm::DIFile::CSK_MD5;
345}
346
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000347llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000348 if (!Loc.isValid())
349 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000350 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000351 remapDIPath(TheCU->getDirectory()),
352 TheCU->getFile()->getChecksumKind(),
353 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000354
355 SourceManager &SM = CGM.getContext().getSourceManager();
356 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
357
358 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
359 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000360 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000361 remapDIPath(TheCU->getDirectory()),
362 TheCU->getFile()->getChecksumKind(),
363 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000364
365 // Cache the results.
366 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000367 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000368
369 if (it != DIFileCache.end()) {
370 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000371 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000372 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000373 }
374
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000375 SmallString<32> Checksum;
376 llvm::DIFile::ChecksumKind CSKind =
377 computeChecksum(SM.getFileID(Loc), Checksum);
378
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000379 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000380 remapDIPath(getCurrentDirname()),
381 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000382
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000383 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 return F;
385}
386
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000387llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000388 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000389 remapDIPath(TheCU->getDirectory()),
390 TheCU->getFile()->getChecksumKind(),
391 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000392}
393
394std::string CGDebugInfo::remapDIPath(StringRef Path) const {
395 for (const auto &Entry : DebugPrefixMap)
396 if (Path.startswith(Entry.first))
397 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
398 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000399}
400
Guy Benyei11169dd2012-12-18 14:30:41 +0000401unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
402 if (Loc.isInvalid() && CurLoc.isInvalid())
403 return 0;
404 SourceManager &SM = CGM.getContext().getSourceManager();
405 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000406 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000407}
408
Adrian Prantlc7822422013-03-12 20:43:25 +0000409unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000411 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000412 return 0;
413
414 // If the location is invalid then use the current column.
415 if (Loc.isInvalid() && CurLoc.isInvalid())
416 return 0;
417 SourceManager &SM = CGM.getContext().getSourceManager();
418 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000419 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000420}
421
422StringRef CGDebugInfo::getCurrentDirname() {
423 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
424 return CGM.getCodeGenOpts().DebugCompilationDir;
425
426 if (!CWDName.empty())
427 return CWDName;
428 SmallString<256> CWD;
429 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000430 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000431}
432
Guy Benyei11169dd2012-12-18 14:30:41 +0000433void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000434 SmallString<32> Checksum;
435 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000436
David Blaikieaabde052014-05-14 00:29:00 +0000437 // Should we be asking the SourceManager for the main file name, instead of
438 // accepting it as an argument? This just causes the main file name to
439 // mismatch with source locations and create extra lexical scopes or
440 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
441 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
442 // because that's what the SourceManager says)
443
Guy Benyei11169dd2012-12-18 14:30:41 +0000444 // Get absolute path name.
445 SourceManager &SM = CGM.getContext().getSourceManager();
446 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
447 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000448 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000449
450 // The main file name provided via the "-main-file-name" option contains just
451 // the file name itself with no path information. This file name may have had
452 // a relative path, so we look into the actual file entry for the main
453 // file to determine the real absolute path for the file.
454 std::string MainFileDir;
455 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000456 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000457 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000458 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
459 llvm::sys::path::append(MainFileDirSS, MainFileName);
460 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000461 }
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000462 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 }
464
Ed Masteda706022014-05-07 12:49:30 +0000465 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 const LangOptions &LO = CGM.getLangOpts();
467 if (LO.CPlusPlus) {
468 if (LO.ObjC1)
469 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
470 else
471 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
472 } else if (LO.ObjC1) {
473 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000474 } else if (LO.RenderScript) {
475 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000476 } else if (LO.C99) {
477 LangTag = llvm::dwarf::DW_LANG_C99;
478 } else {
479 LangTag = llvm::dwarf::DW_LANG_C89;
480 }
481
482 std::string Producer = getClangFullVersion();
483
484 // Figure out which version of the ObjC runtime we have.
485 unsigned RuntimeVers = 0;
486 if (LO.ObjC1)
487 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
488
Adrian Prantl826824e2016-04-08 22:43:06 +0000489 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
490 switch (DebugKind) {
491 case codegenoptions::NoDebugInfo:
492 case codegenoptions::LocTrackingOnly:
493 EmissionKind = llvm::DICompileUnit::NoDebug;
494 break;
495 case codegenoptions::DebugLineTablesOnly:
496 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
497 break;
498 case codegenoptions::LimitedDebugInfo:
499 case codegenoptions::FullDebugInfo:
500 EmissionKind = llvm::DICompileUnit::FullDebug;
501 break;
502 }
503
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000506 TheCU = DBuilder.createCompileUnit(
Amjad Aboudfa9a17e2016-12-14 20:24:40 +0000507 LangTag, DBuilder.createFile(remapDIPath(MainFileName),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000508 remapDIPath(getCurrentDirname()), CSKind,
509 Checksum),
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000510 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000511 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
Dehao Chen5a3f8902017-02-01 22:45:21 +0000512 CGM.getCodeGenOpts().SplitDwarfInlining,
513 CGM.getCodeGenOpts().DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000514}
515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000516llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000517 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000518 StringRef BTName;
519 switch (BT->getKind()) {
520#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000521#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000522#include "clang/AST/BuiltinTypes.def"
523 case BuiltinType::Dependent:
524 llvm_unreachable("Unexpected builtin type");
525 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000526 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000528 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000530 if (!ClassTy)
531 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
532 "objc_class", TheCU,
533 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 return ClassTy;
535 case BuiltinType::ObjCId: {
536 // typedef struct objc_class *Class;
537 // typedef struct objc_object {
538 // Class isa;
539 // } *id;
540
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000541 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 return ObjTy;
543
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000544 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
546 "objc_class", TheCU,
547 getOrCreateMainFile(), 0);
548
549 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000550
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000551 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000552
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000553 ObjTy = DBuilder.createStructType(
554 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
555 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000556
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000557 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000558 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
559 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
560 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 return ObjTy;
562 }
563 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000564 if (!SelTy)
565 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
566 "objc_selector", TheCU,
567 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 return SelTy;
569 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000570
Alexey Bader954ba212016-04-08 13:40:33 +0000571#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
572 case BuiltinType::Id: \
573 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
574 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000575#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000576 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000577 return getOrCreateStructPtrType("opencl_sampler_t",
578 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000579 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000580 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000581 case BuiltinType::OCLClkEvent:
582 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
583 case BuiltinType::OCLQueue:
584 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
585 case BuiltinType::OCLNDRange:
586 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
587 case BuiltinType::OCLReserveID:
588 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000589
Guy Benyei11169dd2012-12-18 14:30:41 +0000590 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000591 case BuiltinType::Char_U:
592 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
593 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000594 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000595 case BuiltinType::SChar:
596 Encoding = llvm::dwarf::DW_ATE_signed_char;
597 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000598 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000599 case BuiltinType::Char32:
600 Encoding = llvm::dwarf::DW_ATE_UTF;
601 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000602 case BuiltinType::UShort:
603 case BuiltinType::UInt:
604 case BuiltinType::UInt128:
605 case BuiltinType::ULong:
606 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000607 case BuiltinType::ULongLong:
608 Encoding = llvm::dwarf::DW_ATE_unsigned;
609 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000610 case BuiltinType::Short:
611 case BuiltinType::Int:
612 case BuiltinType::Int128:
613 case BuiltinType::Long:
614 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000615 case BuiltinType::LongLong:
616 Encoding = llvm::dwarf::DW_ATE_signed;
617 break;
618 case BuiltinType::Bool:
619 Encoding = llvm::dwarf::DW_ATE_boolean;
620 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000621 case BuiltinType::Half:
622 case BuiltinType::Float:
623 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000624 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000625 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000626 // FIXME: For targets where long double and __float128 have the same size,
627 // they are currently indistinguishable in the debugger without some
628 // special treatment. However, there is currently no consensus on encoding
629 // and this should be updated once a DWARF encoding exists for distinct
630 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000631 Encoding = llvm::dwarf::DW_ATE_float;
632 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 }
634
635 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000636 case BuiltinType::Long:
637 BTName = "long int";
638 break;
639 case BuiltinType::LongLong:
640 BTName = "long long int";
641 break;
642 case BuiltinType::ULong:
643 BTName = "long unsigned int";
644 break;
645 case BuiltinType::ULongLong:
646 BTName = "long long unsigned int";
647 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000648 default:
649 BTName = BT->getName(CGM.getLangOpts());
650 break;
651 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000652 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000653 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000654 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000655}
656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000657llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000658 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000659 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000660 if (Ty->isComplexIntegerType())
661 Encoding = llvm::dwarf::DW_ATE_lo_user;
662
663 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000664 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000665}
666
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000667llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
668 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 QualifierCollector Qc;
670 const Type *T = Qc.strip(Ty);
671
672 // Ignore these qualifiers for now.
673 Qc.removeObjCGCAttr();
674 Qc.removeAddressSpace();
675 Qc.removeObjCLifetime();
676
677 // We will create one Derived type for one qualifier and recurse to handle any
678 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000679 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000680 if (Qc.hasConst()) {
681 Tag = llvm::dwarf::DW_TAG_const_type;
682 Qc.removeConst();
683 } else if (Qc.hasVolatile()) {
684 Tag = llvm::dwarf::DW_TAG_volatile_type;
685 Qc.removeVolatile();
686 } else if (Qc.hasRestrict()) {
687 Tag = llvm::dwarf::DW_TAG_restrict_type;
688 Qc.removeRestrict();
689 } else {
690 assert(Qc.empty() && "Unknown type qualifier for debug info");
691 return getOrCreateType(QualType(T, 0), Unit);
692 }
693
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000694 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000695
696 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
697 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000698 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000699}
700
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000701llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
702 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000703
704 // The frontend treats 'id' as a typedef to an ObjCObjectType,
705 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
706 // debug info, we want to emit 'id' in both cases.
707 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000708 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000709
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000710 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
711 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000712}
713
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000714llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
715 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000716 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000717 Ty->getPointeeType(), Unit);
718}
719
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000720/// \return whether a C++ mangling exists for the type defined by TD.
721static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
722 switch (TheCU->getSourceLanguage()) {
723 case llvm::dwarf::DW_LANG_C_plus_plus:
724 return true;
725 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
726 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
727 default:
728 return false;
729 }
730}
731
Manman Rene0064d82013-08-29 23:19:58 +0000732/// In C++ mode, types have linkage, so we can rely on the ODR and
733/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000734static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
735 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000736 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000737 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000738 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000739
740 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000741 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000742
Manman Rene0064d82013-08-29 23:19:58 +0000743 // TODO: This is using the RTTI name. Is there a better way to get
744 // a unique string for a type?
745 llvm::raw_svector_ostream Out(FullName);
746 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000747 return FullName;
748}
749
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000750/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000751static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
752 llvm::dwarf::Tag Tag;
753 if (RD->isStruct() || RD->isInterface())
754 Tag = llvm::dwarf::DW_TAG_structure_type;
755 else if (RD->isUnion())
756 Tag = llvm::dwarf::DW_TAG_union_type;
757 else {
758 // FIXME: This could be a struct type giving a default visibility different
759 // than C++ class type, but needs llvm metadata changes first.
760 assert(RD->isClass());
761 Tag = llvm::dwarf::DW_TAG_class_type;
762 }
763 return Tag;
764}
765
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000766llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000767CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000768 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000769 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000770 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
771 return cast<llvm::DICompositeType>(T);
772 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000773 unsigned Line = getLineNumber(RD->getLocation());
774 StringRef RDName = getClassName(RD);
775
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000776 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000777 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000778
Guy Benyei11169dd2012-12-18 14:30:41 +0000779 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000780 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000782 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000783 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000784 ReplaceMap.emplace_back(
785 std::piecewise_construct, std::make_tuple(Ty),
786 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000787 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000788}
789
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000790llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000791 const Type *Ty,
792 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000793 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000794 // Bit size, align and offset of the type.
795 // Size is always the size of a pointer. We can't use getTypeSize here
796 // because that does not return the correct value for references.
797 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000798 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +0000799 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +0000800
Keno Fischer87842f32015-11-16 09:04:13 +0000801 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
802 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
803 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
804 Size, Align);
805 else
806 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
807 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000808}
809
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000810llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
811 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000812 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000813 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000814 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
815 TheCU, getOrCreateMainFile(), 0);
816 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
817 Cache = DBuilder.createPointerType(Cache, Size);
818 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000819}
820
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000821llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
822 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000823 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 QualType FType;
825 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000826 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000827 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000828
829 FieldOffset = 0;
830 FType = CGM.getContext().UnsignedLongTy;
831 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
832 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
833
834 Elements = DBuilder.getOrCreateArray(EltTys);
835 EltTys.clear();
836
Leny Kholodov80c047d2016-09-06 10:48:04 +0000837 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000838 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000839
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000840 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000841 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000842 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000843
844 // Bit size, align and offset of the type.
845 uint64_t Size = CGM.getContext().getTypeSize(Ty);
846
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000847 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000848
849 FieldOffset = 0;
850 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
851 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
852 FType = CGM.getContext().IntTy;
853 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
854 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000855 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
857
858 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 FieldSize = CGM.getContext().getTypeSize(Ty);
860 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000861 EltTys.push_back(DBuilder.createMemberType(
862 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
863 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000864
865 FieldOffset += FieldSize;
866 Elements = DBuilder.getOrCreateArray(EltTys);
867
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000868 // The __block_literal_generic structs are marked with a special
869 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
870 // the debugger needs to know about. To allow type uniquing, emit
871 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000872 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000873 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000874 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000875
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000876 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000877}
878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000879llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
880 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000881 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000882 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000883
884 SmallString<128> NS;
885 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000886 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
887 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000888
889 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000890 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000891 CGM.getContext().getPrintingPolicy());
892
David Majnemer58ed0f32016-07-17 00:39:12 +0000893 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000894 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000895
896 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000897 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
898 getLineNumber(Loc),
899 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000900}
901
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000902llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
903 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000904 // We don't set size information, but do specify where the typedef was
905 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000906 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000907
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000908 // Typedefs are derived from some other type.
909 return DBuilder.createTypedef(
910 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
911 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000912 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000913}
914
Reid Klecknerf00f8032016-06-08 20:41:54 +0000915static unsigned getDwarfCC(CallingConv CC) {
916 switch (CC) {
917 case CC_C:
918 // Avoid emitting DW_AT_calling_convention if the C convention was used.
919 return 0;
920
921 case CC_X86StdCall:
922 return llvm::dwarf::DW_CC_BORLAND_stdcall;
923 case CC_X86FastCall:
924 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
925 case CC_X86ThisCall:
926 return llvm::dwarf::DW_CC_BORLAND_thiscall;
927 case CC_X86VectorCall:
928 return llvm::dwarf::DW_CC_LLVM_vectorcall;
929 case CC_X86Pascal:
930 return llvm::dwarf::DW_CC_BORLAND_pascal;
931
932 // FIXME: Create new DW_CC_ codes for these calling conventions.
933 case CC_X86_64Win64:
934 case CC_X86_64SysV:
935 case CC_AAPCS:
936 case CC_AAPCS_VFP:
937 case CC_IntelOclBicc:
938 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000939 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000940 case CC_Swift:
941 case CC_PreserveMost:
942 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000943 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000944 return 0;
945 }
946 return 0;
947}
948
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000949llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
950 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000951 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000952
953 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000954 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000955
956 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000957 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000958 if (isa<FunctionNoProtoType>(Ty))
959 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000960 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
961 for (const QualType &ParamType : FPT->param_types())
962 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000963 if (FPT->isVariadic())
964 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 }
966
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000967 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000968 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000969 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000970}
971
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000972/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000973/// As an optimization, return 0 if the access specifier equals the
974/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000975static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
976 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +0000977 AccessSpecifier Default = clang::AS_none;
978 if (RD && RD->isClass())
979 Default = clang::AS_private;
980 else if (RD && (RD->isStruct() || RD->isUnion()))
981 Default = clang::AS_public;
982
983 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +0000984 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000985
Eric Christophere7b87e52014-10-26 23:40:33 +0000986 switch (Access) {
987 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000988 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000989 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000990 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000991 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000992 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000993 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +0000994 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000995 }
996 llvm_unreachable("unexpected access enumerator");
997}
Guy Benyei11169dd2012-12-18 14:30:41 +0000998
David Majnemerb4b671e2016-06-30 03:01:59 +0000999llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1000 llvm::DIScope *RecordTy,
1001 const RecordDecl *RD) {
1002 StringRef Name = BitFieldDecl->getName();
1003 QualType Ty = BitFieldDecl->getType();
1004 SourceLocation Loc = BitFieldDecl->getLocation();
1005 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1006 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1007
1008 // Get the location for the field.
1009 llvm::DIFile *File = getOrCreateFile(Loc);
1010 unsigned Line = getLineNumber(Loc);
1011
1012 const CGBitFieldInfo &BitFieldInfo =
1013 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1014 uint64_t SizeInBits = BitFieldInfo.Size;
1015 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001016 uint64_t StorageOffsetInBits =
1017 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
1018 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001019 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001020 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001021 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1022 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001023}
1024
1025llvm::DIType *
1026CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1027 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001028 uint32_t AlignInBits, llvm::DIFile *tunit,
1029 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001030 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001031
1032 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001033 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001034 unsigned line = getLineNumber(loc);
1035
David Majnemer34b57492014-07-30 01:30:47 +00001036 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001037 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001038 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001039 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1040 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001041 if (!Align)
1042 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001043 }
1044
Leny Kholodov80c047d2016-09-06 10:48:04 +00001045 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001046 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001047 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001048}
1049
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001050void CGDebugInfo::CollectRecordLambdaFields(
1051 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001052 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001053 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1054 // has the name and the location of the variable so we should iterate over
1055 // both concurrently.
1056 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1057 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1058 unsigned fieldno = 0;
1059 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001060 E = CXXDecl->captures_end();
1061 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001062 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001063 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001064 SourceLocation Loc = C.getLocation();
1065 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001066 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001067 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001068 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001069 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001070 llvm::DIType *FieldType = createFieldType(
1071 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001072 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001073 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001074 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001075 // TODO: Need to handle 'this' in some way by probably renaming the
1076 // this of the lambda class and having a field member of 'this' or
1077 // by using AT_object_pointer for the function and having that be
1078 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001079 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001081 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001082 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001083 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001084 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001085
1086 elements.push_back(fieldType);
1087 }
1088 }
1089}
1090
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001091llvm::DIDerivedType *
1092CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001093 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001094 // Create the descriptor for the static variable, with or without
1095 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001096 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001097 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1098 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001099
Eric Christopher91a31902013-01-16 01:22:32 +00001100 unsigned LineNumber = getLineNumber(Var->getLocation());
1101 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001102 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001103 if (Var->getInit()) {
1104 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001105 if (Value) {
1106 if (Value->isInt())
1107 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1108 if (Value->isFloat())
1109 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1110 }
Eric Christopher91a31902013-01-16 01:22:32 +00001111 }
1112
Leny Kholodov80c047d2016-09-06 10:48:04 +00001113 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001114 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001115 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001116 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001117 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001118 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001119}
1120
Eric Christophere7b87e52014-10-26 23:40:33 +00001121void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1123 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001124 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001125 StringRef name = field->getName();
1126 QualType type = field->getType();
1127
1128 // Ignore unnamed fields unless they're anonymous structs/unions.
1129 if (name.empty() && !type->isRecordType())
1130 return;
1131
David Majnemerb4b671e2016-06-30 03:01:59 +00001132 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001133 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001134 FieldType = createBitFieldType(field, RecordTy, RD);
1135 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001136 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001137 FieldType =
1138 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001139 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001140 }
1141
David Majnemerb4b671e2016-06-30 03:01:59 +00001142 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001143}
1144
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001145void CGDebugInfo::CollectRecordNestedRecord(
1146 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1147 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001148 // Injected class names are not considered nested records.
1149 if (isa<InjectedClassNameType>(Ty))
1150 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001151 SourceLocation Loc = RD->getLocation();
1152 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1153 elements.push_back(nestedType);
1154}
1155
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001156void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001158 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001159 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001160 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001161
Eric Christopher91a31902013-01-16 01:22:32 +00001162 if (CXXDecl && CXXDecl->isLambda())
1163 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1164 else {
1165 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001166
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001167 // Debug info for nested records is included in the member list only for
1168 // CodeView.
1169 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1170
Eric Christopher91a31902013-01-16 01:22:32 +00001171 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001172 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001173
Eric Christopher91a31902013-01-16 01:22:32 +00001174 // Static and non-static members should appear in the same order as
1175 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001176 for (const auto *I : record->decls())
1177 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001178 if (V->hasAttr<NoDebugAttr>())
1179 continue;
David Blaikiece763042013-08-20 21:49:21 +00001180 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001181 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001182 if (MI != StaticDataMemberCache.end()) {
1183 assert(MI->second &&
1184 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001185 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001186 } else {
1187 auto Field = CreateRecordStaticField(V, RecordTy, record);
1188 elements.push_back(Field);
1189 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001190 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001191 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1192 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001193
1194 // Bump field number for next field.
1195 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001196 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1197 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1198 nestedRec->getDeclContext() == record)
1199 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001200 }
1201}
1202
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001203llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001204CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001205 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001206 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001207 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001208 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001209 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001210 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1211 Func, Unit);
1212}
David Blaikie2aaf0652013-01-07 22:24:59 +00001213
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1215 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 llvm::DITypeRefArray Args(
1218 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001219 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001220 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001221
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001222 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001223
1224 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001225 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001226
David Blaikie2aaf0652013-01-07 22:24:59 +00001227 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001228 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001229 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1230 // Create pointer type directly in this case.
1231 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1232 QualType PointeeTy = ThisPtrTy->getPointeeType();
1233 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001234 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001235 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1237 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001238 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001239 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001240 // TODO: This and the artificial type below are misleading, the
1241 // types aren't artificial the argument is, but the current
1242 // metadata doesn't represent that.
1243 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1244 Elts.push_back(ThisPtrType);
1245 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001246 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001247 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001248 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1249 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001250 }
1251
1252 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001253 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1254 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001255
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001256 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001257
Leny Kholodov80c047d2016-09-06 10:48:04 +00001258 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001259 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001261 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001262 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001263
Reid Klecknerf00f8032016-06-08 20:41:54 +00001264 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1265 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001266}
1267
Eric Christopherb2a008c2013-05-16 00:45:12 +00001268/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001269/// inside a function.
1270static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001271 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001272 return isFunctionLocalClass(NRD);
1273 if (isa<FunctionDecl>(RD->getDeclContext()))
1274 return true;
1275 return false;
1276}
1277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1279 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001280 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001281 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001282
Guy Benyei11169dd2012-12-18 14:30:41 +00001283 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001284 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001285
1286 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1287 // make sense to give a single ctor/dtor a linkage name.
1288 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001289 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1290 // property to use here. It may've been intended to model "is non-external
1291 // type" but misses cases of non-function-local but non-external classes such
1292 // as those in anonymous namespaces as well as the reverse - external types
1293 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1295 MethodLinkageName = CGM.getMangledName(Method);
1296
1297 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001298 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001299 unsigned MethodLine = 0;
1300 if (!Method->isImplicit()) {
1301 MethodDefUnit = getOrCreateFile(Method->getLocation());
1302 MethodLine = getLineNumber(Method->getLocation());
1303 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001304
1305 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001306 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001307 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001308 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001309 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001310 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001311
Guy Benyei11169dd2012-12-18 14:30:41 +00001312 if (Method->isVirtual()) {
1313 if (Method->isPure())
1314 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1315 else
1316 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001317
Reid Kleckner216d0a12016-06-16 20:08:51 +00001318 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1319 // It doesn't make sense to give a virtual destructor a vtable index,
1320 // since a single destructor has two entries in the vtable.
1321 if (!isa<CXXDestructorDecl>(Method))
1322 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1323 } else {
1324 // Emit MS ABI vftable information. There is only one entry for the
1325 // deleting dtor.
1326 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1327 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1328 MicrosoftVTableContext::MethodVFTableLocation ML =
1329 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1330 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001331
1332 // CodeView only records the vftable offset in the class that introduces
1333 // the virtual method. This is possible because, unlike Itanium, the MS
1334 // C++ ABI does not include all virtual methods from non-primary bases in
1335 // the vtable for the most derived class. For example, if C inherits from
1336 // A and B, C's primary vftable will not include B's virtual methods.
1337 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1338 Flags |= llvm::DINode::FlagIntroducedVirtual;
1339
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001340 // The 'this' adjustment accounts for both the virtual and non-virtual
1341 // portions of the adjustment. Presumably the debugger only uses it when
1342 // it knows the dynamic type of an object.
1343 ThisAdjustment = CGM.getCXXABI()
1344 .getVirtualFunctionPrologueThisAdjustment(GD)
1345 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001346 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 ContainingType = RecordTy;
1348 }
1349
Guy Benyei11169dd2012-12-18 14:30:41 +00001350 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001351 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001352 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001353 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001354 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001355 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001356 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001358 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001359 }
1360 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001362 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001363 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001364 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001366
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001367 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1368 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001369 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001370 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1371 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1372 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001373
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001374 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001375
1376 return SP;
1377}
1378
Eric Christophere7b87e52014-10-26 23:40:33 +00001379void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001380 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1381 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001382
1383 // Since we want more than just the individual member decls if we
1384 // have templated functions iterate over every declaration to gather
1385 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001386 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001387 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1388 // If the member is implicit, don't add it to the member list. This avoids
1389 // the member being added to type units by LLVM, while still allowing it
1390 // to be emitted into the type declaration/reference inside the compile
1391 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001392 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001393 // FIXME: Handle Using(Shadow?)Decls here to create
1394 // DW_TAG_imported_declarations inside the class for base decls brought into
1395 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1396 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1397 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001398 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001399 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001400
1401 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1402 continue;
1403
David Blaikiefd580722014-10-06 05:18:55 +00001404 // Reuse the existing member function declaration if it exists.
1405 // It may be associated with the declaration of the type & should be
1406 // reused as we're building the definition.
1407 //
1408 // This situation can arise in the vtable-based debug info reduction where
1409 // implicit members are emitted in a non-vtable TU.
1410 auto MI = SPCache.find(Method->getCanonicalDecl());
1411 EltTys.push_back(MI == SPCache.end()
1412 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001413 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001414 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001415}
Guy Benyei11169dd2012-12-18 14:30:41 +00001416
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001417void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001418 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001419 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001420 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1421 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1422 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001423
Bob Haarmandff36732016-10-25 22:19:32 +00001424 // If we are generating CodeView debug info, we also need to emit records for
1425 // indirect virtual base classes.
1426 if (CGM.getCodeGenOpts().EmitCodeView) {
1427 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1428 llvm::DINode::FlagIndirectVirtualBase);
1429 }
1430}
1431
1432void CGDebugInfo::CollectCXXBasesAux(
1433 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1434 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1435 const CXXRecordDecl::base_class_const_range &Bases,
1436 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1437 llvm::DINode::DIFlags StartingFlags) {
1438 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1439 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001440 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001441 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001442 if (!SeenTypes.insert(Base).second)
1443 continue;
1444 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1445 llvm::DINode::DIFlags BFlags = StartingFlags;
1446 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001447
Aaron Ballman574705e2014-03-13 15:41:46 +00001448 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001449 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1450 // virtual base offset offset is -ve. The code generator emits dwarf
1451 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001452 BaseOffset = 0 - CGM.getItaniumVTableContext()
1453 .getVirtualBaseOffsetOffset(RD, Base)
1454 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001455 } else {
1456 // In the MS ABI, store the vbtable offset, which is analogous to the
1457 // vbase offset offset in Itanium.
1458 BaseOffset =
1459 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1460 }
Bob Haarmandff36732016-10-25 22:19:32 +00001461 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001462 } else
1463 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1464 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1465 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001466
Adrian Prantl21361fb2014-08-29 22:44:27 +00001467 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001468 llvm::DIType *DTy =
1469 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 EltTys.push_back(DTy);
1471 }
1472}
1473
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001474llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001475CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1476 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001477 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001478 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001479 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1480 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001481 StringRef Name;
1482 if (TPList)
1483 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001484 switch (TA.getKind()) {
1485 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001486 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001487 TemplateParams.push_back(
1488 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001489 } break;
1490 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001491 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001492 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1493 TheCU, Name, TTy,
1494 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001495 } break;
1496 case TemplateArgument::Declaration: {
1497 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001498 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001499 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001500 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001501 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001502 // Variable pointer template parameters have a value that is the address
1503 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001504 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001505 V = CGM.GetAddrOfGlobalVar(VD);
1506 // Member function pointers have special support for building them, though
1507 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001508 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001509 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001510 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001511 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001512 // Member data pointers have special handling too to compute the fixed
1513 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001514 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001515 // These five lines (& possibly the above member function pointer
1516 // handling) might be able to be refactored to use similar code in
1517 // CodeGenModule::getMemberPointerConstant
1518 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1519 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001520 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001521 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001522 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001523 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1524 TheCU, Name, TTy,
1525 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001526 } break;
1527 case TemplateArgument::NullPtr: {
1528 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001529 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001530 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001531 // Special case member data pointer null values since they're actually -1
1532 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001533 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001534 // But treat member function pointers as simple zero integers because
1535 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1536 // CodeGen grows handling for values of non-null member function
1537 // pointers then perhaps we could remove this special case and rely on
1538 // EmitNullMemberPointer for member function pointers.
1539 if (MPT->isMemberDataPointer())
1540 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1541 if (!V)
1542 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001543 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001544 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001545 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001546 case TemplateArgument::Template:
1547 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001548 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001549 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1550 break;
1551 case TemplateArgument::Pack:
1552 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1553 TheCU, Name, nullptr,
1554 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1555 break;
David Majnemer5559d472013-08-24 08:21:10 +00001556 case TemplateArgument::Expression: {
1557 const Expr *E = TA.getAsExpr();
1558 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001559 if (E->isGLValue())
1560 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001561 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001562 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001563 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001564 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001565 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001566 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001567 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001568 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001569 case TemplateArgument::Null:
1570 llvm_unreachable(
1571 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 }
1573 }
1574 return DBuilder.getOrCreateArray(TemplateParams);
1575}
1576
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001578CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001579 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 if (FD->getTemplatedKind() ==
1581 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001582 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1583 ->getTemplate()
1584 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001585 return CollectTemplateParams(
1586 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001587 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001588 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001589}
1590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001591llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1592 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001593 // Always get the full list of parameters, not just the ones from
1594 // the specialization.
1595 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001596 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001597 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001598 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001599}
1600
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001601llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001602 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 return VTablePtrType;
1604
1605 ASTContext &Context = CGM.getContext();
1606
1607 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001608 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001609 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001610 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001612 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001613 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001614 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1615 return VTablePtrType;
1616}
1617
Guy Benyei11169dd2012-12-18 14:30:41 +00001618StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001619 // Copy the gdb compatible name on the side and use its reference.
1620 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001621}
1622
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001623void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001624 SmallVectorImpl<llvm::Metadata *> &EltTys,
1625 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001626 // If this class is not dynamic then there is not any vtable info to collect.
1627 if (!RD->isDynamicClass())
1628 return;
1629
Reid Kleckner59812422016-08-31 20:35:01 +00001630 // Don't emit any vtable shape or vptr info if this class doesn't have an
1631 // extendable vfptr. This can happen if the class doesn't have virtual
1632 // methods, or in the MS ABI if those virtual methods only come from virtually
1633 // inherited bases.
1634 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1635 if (!RL.hasExtendableVFPtr())
1636 return;
1637
Reid Klecknerdc124992016-08-31 16:11:43 +00001638 // CodeView needs to know how large the vtable of every dynamic class is, so
1639 // emit a special named pointer type into the element list. The vptr type
1640 // points to this type as well.
1641 llvm::DIType *VPtrTy = nullptr;
1642 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1643 CGM.getTarget().getCXXABI().isMicrosoft();
1644 if (NeedVTableShape) {
1645 uint64_t PtrWidth =
1646 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1647 const VTableLayout &VFTLayout =
1648 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1649 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001650 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001651 unsigned VTableWidth = PtrWidth * VSlotCount;
1652
1653 // Create a very wide void* type and insert it directly in the element list.
1654 llvm::DIType *VTableType =
1655 DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type");
1656 EltTys.push_back(VTableType);
1657
1658 // The vptr is a pointer to this special vtable type.
1659 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1660 }
1661
1662 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001663 if (RL.getPrimaryBase())
1664 return;
1665
1666 if (!VPtrTy)
1667 VPtrTy = getOrCreateVTablePtrType(Unit);
1668
Guy Benyei11169dd2012-12-18 14:30:41 +00001669 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001670 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001671 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001672 llvm::DINode::FlagArtificial, VPtrTy);
1673 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674}
1675
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001676llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001677 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001678 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001680 return T;
1681}
1682
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001683llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001684 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001685 return getOrCreateStandaloneType(D, Loc);
1686}
1687
1688llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1689 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001690 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001691 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001692 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001693 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001694
Adrian Prantl73409ce2013-03-11 18:33:46 +00001695 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001696 return T;
1697}
1698
David Blaikie483a9da2014-05-06 18:35:21 +00001699void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001700 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001701 return;
1702 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001703 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001704 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001705 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001706 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001707 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001708 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001709 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001710}
1711
David Blaikieb2e86eb2013-08-15 20:49:17 +00001712void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001713 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001714 !CGM.getLangOpts().CPlusPlus)
1715 completeRequiredType(RD);
1716}
1717
David Blaikieb11c8732017-01-30 06:36:08 +00001718/// Return true if the class or any of its methods are marked dllimport.
1719static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1720 if (RD->hasAttr<DLLImportAttr>())
1721 return true;
1722 for (const CXXMethodDecl *MD : RD->methods())
1723 if (MD->hasAttr<DLLImportAttr>())
1724 return true;
1725 return false;
1726}
1727
David Blaikie6943dea2013-08-20 01:28:15 +00001728void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001729 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1730 if (CXXRD->isDynamicClass() &&
1731 CGM.getVTableLinkage(CXXRD) ==
1732 llvm::GlobalValue::AvailableExternallyLinkage &&
1733 !isClassOrMethodDLLImport(CXXRD))
1734 return;
1735 completeClass(RD);
1736}
1737
1738void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001739 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001740 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001741 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001742 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001743 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001744 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001745 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001746 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001747 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001748 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001749}
1750
David Blaikie0e716b42014-03-03 23:48:23 +00001751static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1752 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001753 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1754 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001755 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001756 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001757 return true;
1758 return false;
1759}
1760
Adrian Prantl05fefa42016-04-25 20:52:40 +00001761/// Does a type definition exist in an imported clang module?
1762static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001763 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001764 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001765 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001766 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001767 if (!RD->isExternallyVisible() && RD->getName().empty())
1768 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001769 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001770 if (!CXXDecl->isCompleteDefinition())
1771 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001772 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1773 if (TemplateKind != TSK_Undeclared) {
1774 // This is a template, check the origin of the first member.
1775 if (CXXDecl->field_begin() == CXXDecl->field_end())
1776 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1777 if (!CXXDecl->field_begin()->isFromASTFile())
1778 return false;
1779 }
Adrian Prantl94913712016-04-26 23:42:43 +00001780 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001781 return true;
1782}
1783
Benjamin Kramer8c305922016-02-02 11:06:51 +00001784static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1785 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001786 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001787 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001788 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001789
Benjamin Kramer8c305922016-02-02 11:06:51 +00001790 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001791 return false;
1792
1793 if (!LangOpts.CPlusPlus)
1794 return false;
1795
1796 if (!RD->isCompleteDefinitionRequired())
1797 return true;
1798
David Majnemer58ed0f32016-07-17 00:39:12 +00001799 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001800
1801 if (!CXXDecl)
1802 return false;
1803
Adrian McCarthy99242982016-08-16 22:11:18 +00001804 // Only emit complete debug info for a dynamic class when its vtable is
1805 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001806 // across DLL boundaries, so skip this optimization if the class or any of its
1807 // methods are marked dllimport. This isn't a complete solution, since objects
1808 // without any dllimport methods can be used in one DLL and constructed in
1809 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001810 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001811 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001812 return true;
1813
1814 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001815 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001816 Spec = SD->getSpecializationKind();
1817
1818 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1819 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1820 CXXDecl->method_end()))
1821 return true;
1822
1823 return false;
1824}
1825
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001826void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1827 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1828 return;
1829
1830 QualType Ty = CGM.getContext().getRecordType(RD);
1831 llvm::DIType *T = getTypeOrNull(Ty);
1832 if (T && T->isForwardDecl())
1833 completeClassData(RD);
1834}
1835
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001836llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001837 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001838 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001839 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1840 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001841 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001842 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001843 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001844 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001845
David Blaikieb2e86eb2013-08-15 20:49:17 +00001846 return CreateTypeDefinition(Ty);
1847}
1848
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001849llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001850 RecordDecl *RD = Ty->getDecl();
1851
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001853 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001854
1855 // Records and classes and unions can all be recursive. To handle them, we
1856 // first generate a debug descriptor for the struct as a forward declaration.
1857 // Then (if it is a definition) we go through and get debug info for all of
1858 // its members. Finally, we create a descriptor for the complete type (which
1859 // may refer to the forward decl if the struct is recursive) and replace all
1860 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001861 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001862
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001863 const RecordDecl *D = RD->getDefinition();
1864 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 return FwdDecl;
1866
David Majnemer58ed0f32016-07-17 00:39:12 +00001867 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001868 CollectContainingType(CXXDecl, FwdDecl);
1869
Guy Benyei11169dd2012-12-18 14:30:41 +00001870 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001871 LexicalBlockStack.emplace_back(&*FwdDecl);
1872 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001873
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001875 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001876 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001877
1878 // Note: The split of CXXDecl information here is intentional, the
1879 // gdb tests will depend on a certain ordering at printout. The debug
1880 // information offsets are still correct if we merge them all together
1881 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001882 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 if (CXXDecl) {
1884 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001885 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 }
1887
Eric Christopher91a31902013-01-16 01:22:32 +00001888 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001890 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001892
1893 LexicalBlockStack.pop_back();
1894 RegionMap.erase(Ty->getDecl());
1895
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001896 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001897 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001898
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001899 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001900 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001901 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001902
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001903 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001904 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001905}
1906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001907llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1908 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 // Ignore protocols.
1910 return getOrCreateType(Ty->getBaseType(), Unit);
1911}
1912
Manman Rene6be26c2016-09-13 17:25:08 +00001913llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1914 llvm::DIFile *Unit) {
1915 // Ignore protocols.
1916 SourceLocation Loc = Ty->getDecl()->getLocation();
1917
1918 // Use Typedefs to represent ObjCTypeParamType.
1919 return DBuilder.createTypedef(
1920 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1921 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1922 getDeclContextDescriptor(Ty->getDecl()));
1923}
1924
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001925/// \return true if Getter has the default name for the property PD.
1926static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1927 const ObjCMethodDecl *Getter) {
1928 assert(PD);
1929 if (!Getter)
1930 return true;
1931
1932 assert(Getter->getDeclName().isObjCZeroArgSelector());
1933 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001934 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001935}
1936
1937/// \return true if Setter has the default name for the property PD.
1938static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1939 const ObjCMethodDecl *Setter) {
1940 assert(PD);
1941 if (!Setter)
1942 return true;
1943
1944 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001945 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001946 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001947}
1948
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001949llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1950 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001951 ObjCInterfaceDecl *ID = Ty->getDecl();
1952 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001953 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001954
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001955 // Return a forward declaration if this type was imported from a clang module,
1956 // and this is not the compile unit with the implementation of the type (which
1957 // may contain hidden ivars).
1958 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1959 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001960 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1961 ID->getName(),
1962 getDeclContextDescriptor(ID), Unit, 0);
1963
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001965 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001967 auto RuntimeLang =
1968 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001969
1970 // If this is just a forward declaration return a special forward-declaration
1971 // debug type since we won't be able to lay out the entire type.
1972 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001973 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001974 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001975 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001976 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1977 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001978 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 return FwdDecl;
1980 }
1981
David Blaikieef8a9512014-05-05 23:23:53 +00001982 return CreateTypeDefinition(Ty, Unit);
1983}
1984
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001985llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001986CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1987 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001988 // Use the Module pointer as the key into the cache. This is a
1989 // nullptr if the "Module" is a PCH, which is safe because we don't
1990 // support chained PCH debug info, so there can only be a single PCH.
1991 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001992 auto ModRef = ModuleCache.find(M);
1993 if (ModRef != ModuleCache.end())
1994 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001995
1996 // Macro definitions that were defined with "-D" on the command line.
1997 SmallString<128> ConfigMacros;
1998 {
1999 llvm::raw_svector_ostream OS(ConfigMacros);
2000 const auto &PPOpts = CGM.getPreprocessorOpts();
2001 unsigned I = 0;
2002 // Translate the macro definitions back into a commmand line.
2003 for (auto &M : PPOpts.Macros) {
2004 if (++I > 1)
2005 OS << " ";
2006 const std::string &Macro = M.first;
2007 bool Undef = M.second;
2008 OS << "\"-" << (Undef ? 'U' : 'D');
2009 for (char c : Macro)
2010 switch (c) {
2011 case '\\' : OS << "\\\\"; break;
2012 case '"' : OS << "\\\""; break;
2013 default: OS << c;
2014 }
2015 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002016 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002017 }
Adrian Prantl66689202015-09-18 23:01:45 +00002018
Adrian Prantl835e6632015-09-24 16:10:10 +00002019 bool IsRootModule = M ? !M->Parent : true;
2020 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002021 // PCH files don't have a signature field in the control block,
2022 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00002023 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002024 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002025 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2026 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2027 TheCU->getProducer(), true, StringRef(), 0,
2028 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2029 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002030 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002031 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002032 llvm::DIModule *Parent =
2033 IsRootModule ? nullptr
2034 : getOrCreateModuleRef(
2035 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2036 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002037 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002038 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2039 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002040 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002041 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002042}
2043
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002044llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2045 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002046 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002047 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002048 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002049 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002050
2051 // Bit size, align and offset of the type.
2052 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002053 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002054
Leny Kholodov80c047d2016-09-06 10:48:04 +00002055 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002056 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002057 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002058
Adrian Prantlfd696112015-10-01 00:48:51 +00002059 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002060 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002061 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2062 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002063
David Blaikieef8a9512014-05-05 23:23:53 +00002064 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002065 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002066
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002067 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002068 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002069 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002070
2071 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002072 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002073
2074 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2075 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002076 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002077 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002078 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002079 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002080
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002081 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2082 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 EltTys.push_back(InhTag);
2084 }
2085
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002086 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002087 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002089 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002090 unsigned PLine = getLineNumber(Loc);
2091 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2092 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002093 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2094 PD->getName(), PUnit, PLine,
2095 hasDefaultGetterName(PD, Getter) ? ""
2096 : getSelectorName(PD->getGetterName()),
2097 hasDefaultSetterName(PD, Setter) ? ""
2098 : getSelectorName(PD->getSetterName()),
2099 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002100 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002101 };
2102 {
2103 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002104 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002105 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002106 PropertySet.insert(PD->getIdentifier());
2107 AddProperty(PD);
2108 }
Manman Renefe1bac2016-01-27 20:00:32 +00002109 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002110 // Don't emit duplicate metadata for properties that were already in a
2111 // class extension.
2112 if (!PropertySet.insert(PD->getIdentifier()).second)
2113 continue;
2114 AddProperty(PD);
2115 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 }
2117
2118 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2119 unsigned FieldNo = 0;
2120 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2121 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002123 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002124 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002125
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 StringRef FieldName = Field->getName();
2127
2128 // Ignore unnamed fields.
2129 if (FieldName.empty())
2130 continue;
2131
2132 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002133 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002134 unsigned FieldLine = getLineNumber(Field->getLocation());
2135 QualType FType = Field->getType();
2136 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002137 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002138
2139 if (!FType->isIncompleteArrayType()) {
2140
2141 // Bit size, align and offset of the type.
2142 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002143 ? Field->getBitWidthValue(CGM.getContext())
2144 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002145 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 }
2147
2148 uint64_t FieldOffset;
2149 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2150 // We don't know the runtime offset of an ivar if we're using the
2151 // non-fragile ABI. For bitfields, use the bit offset into the first
2152 // byte of storage of the bitfield. For other fields, use zero.
2153 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002154 FieldOffset =
2155 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 FieldOffset %= CGM.getContext().getCharWidth();
2157 } else {
2158 FieldOffset = 0;
2159 }
2160 } else {
2161 FieldOffset = RL.getFieldOffset(FieldNo);
2162 }
2163
Leny Kholodov80c047d2016-09-06 10:48:04 +00002164 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002166 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002168 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002169 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002170 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
Craig Topper8a13c412014-05-21 05:09:00 +00002172 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002174 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002175 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002177 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002179 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2181 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002182 PropertyNode = DBuilder.createObjCProperty(
2183 PD->getName(), PUnit, PLine,
2184 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2185 PD->getGetterName()),
2186 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2187 PD->getSetterName()),
2188 PD->getPropertyAttributes(),
2189 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002190 }
2191 }
2192 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002193 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2194 FieldSize, FieldAlign, FieldOffset, Flags,
2195 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 EltTys.push_back(FieldTy);
2197 }
2198
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002199 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002200 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002201
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002203 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002204}
2205
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002206llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2207 llvm::DIFile *Unit) {
2208 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 int64_t Count = Ty->getNumElements();
2210 if (Count == 0)
2211 // If number of elements are not known then this is an unbounded array.
2212 // Use Count == -1 to express such arrays.
2213 Count = -1;
2214
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002215 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002216 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002217
2218 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002219 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
2221 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2222}
2223
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002224llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002226 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
2228 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002229 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002231 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2232 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002233 } else if (Ty->isIncompleteArrayType()) {
2234 Size = 0;
2235 if (Ty->getElementType()->isIncompleteType())
2236 Align = 0;
2237 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002238 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002239 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002240 Size = 0;
2241 Align = 0;
2242 } else {
2243 // Size and align of the whole array, not the element type.
2244 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002245 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 }
2247
2248 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2249 // interior arrays, do we care? Why aren't nested arrays represented the
2250 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002251 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 QualType EltTy(Ty, 0);
2253 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2254 // If the number of elements is known, then count is that number. Otherwise,
2255 // it's -1. This allows us to represent a subrange with an array of 0
2256 // elements, like this:
2257 //
2258 // struct foo {
2259 // int x[0];
2260 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002261 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002262 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002263 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002264 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002265 if (Expr *Size = VAT->getSizeExpr()) {
2266 llvm::APSInt V;
2267 if (Size->EvaluateAsInt(V, CGM.getContext()))
2268 Count = V.getExtValue();
2269 }
David Blaikie87173f12016-08-22 17:49:56 +00002270 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002271
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 // FIXME: Verify this is right for VLAs.
2273 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2274 EltTy = Ty->getElementType();
2275 }
2276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002277 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002278
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002279 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2280 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002281}
2282
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002283llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2284 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002285 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2286 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002287}
2288
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002289llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2290 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002291 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2292 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002293}
2294
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002295llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2296 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002297 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002298 uint64_t Size = 0;
2299
2300 if (!Ty->isIncompleteType()) {
2301 Size = CGM.getContext().getTypeSize(Ty);
2302
2303 // Set the MS inheritance model. There is no flag for the unspecified model.
2304 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2305 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2306 case MSInheritanceAttr::Keyword_single_inheritance:
2307 Flags |= llvm::DINode::FlagSingleInheritance;
2308 break;
2309 case MSInheritanceAttr::Keyword_multiple_inheritance:
2310 Flags |= llvm::DINode::FlagMultipleInheritance;
2311 break;
2312 case MSInheritanceAttr::Keyword_virtual_inheritance:
2313 Flags |= llvm::DINode::FlagVirtualInheritance;
2314 break;
2315 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2316 break;
2317 }
2318 }
2319 }
2320
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002321 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002322 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002323 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002324 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2325 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002326
2327 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002328 Ty->getPointeeType()->getAs<FunctionProtoType>();
2329 return DBuilder.createMemberPointerType(
2330 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2331 Ty->getClass(), FPT->getTypeQuals())),
2332 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002333 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002334}
2335
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002336llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002337 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2338 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002339}
2340
Xiuli Pan9c14e282016-01-09 12:53:17 +00002341llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2342 llvm::DIFile *U) {
2343 return getOrCreateType(Ty->getElementType(), U);
2344}
2345
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002346llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002347 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002348
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002350 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 if (!ED->getTypeForDecl()->isIncompleteType()) {
2352 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002353 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 }
2355
Manman Rene0064d82013-08-29 23:19:58 +00002356 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2357
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002358 bool isImportedFromModule =
2359 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2360
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 // If this is just a forward declaration, construct an appropriately
2362 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002363 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002364 // Note that it is possible for enums to be created as part of
2365 // their own declcontext. In this case a FwdDecl will be created
2366 // twice. This doesn't cause a problem because both FwdDecls are
2367 // entered into the ReplaceMap: finalize() will replace the first
2368 // FwdDecl with the second and then replace the second with
2369 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002370 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002371 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002372 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2373 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002374
Guy Benyei11169dd2012-12-18 14:30:41 +00002375 unsigned Line = getLineNumber(ED->getLocation());
2376 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002377 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002378 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2379 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002380
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002381 ReplaceMap.emplace_back(
2382 std::piecewise_construct, std::make_tuple(Ty),
2383 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002384 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 }
2386
David Blaikie483a9da2014-05-06 18:35:21 +00002387 return CreateTypeDefinition(Ty);
2388}
2389
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002390llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002391 const EnumDecl *ED = Ty->getDecl();
2392 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002393 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002394 if (!ED->getTypeForDecl()->isIncompleteType()) {
2395 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002396 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002397 }
2398
2399 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2400
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002401 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002402 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002403 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002404 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002405 Enumerators.push_back(DBuilder.createEnumerator(
2406 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 }
2408
2409 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002410 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002413 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002414 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002415 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002416 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2417 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2418 Line, Size, Align, EltArray, ClassTy,
2419 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002420}
2421
David Blaikie05491062013-01-21 04:37:12 +00002422static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2423 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002425 Qualifiers InnerQuals = T.getLocalQualifiers();
2426 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2427 // that is already there.
2428 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2429 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 QualType LastT = T;
2431 switch (T->getTypeClass()) {
2432 default:
David Blaikie05491062013-01-21 04:37:12 +00002433 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002434 case Type::TemplateSpecialization: {
2435 const auto *Spec = cast<TemplateSpecializationType>(T);
2436 if (Spec->isTypeAlias())
2437 return C.getQualifiedType(T.getTypePtr(), Quals);
2438 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002439 break;
2440 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002441 case Type::TypeOfExpr:
2442 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2443 break;
2444 case Type::TypeOf:
2445 T = cast<TypeOfType>(T)->getUnderlyingType();
2446 break;
2447 case Type::Decltype:
2448 T = cast<DecltypeType>(T)->getUnderlyingType();
2449 break;
2450 case Type::UnaryTransform:
2451 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2452 break;
2453 case Type::Attributed:
2454 T = cast<AttributedType>(T)->getEquivalentType();
2455 break;
2456 case Type::Elaborated:
2457 T = cast<ElaboratedType>(T)->getNamedType();
2458 break;
2459 case Type::Paren:
2460 T = cast<ParenType>(T)->getInnerType();
2461 break;
David Blaikie05491062013-01-21 04:37:12 +00002462 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002464 break;
Jordan Rose303e2f12016-11-10 23:28:17 +00002465 case Type::Auto: {
David Blaikie22c460a02013-05-24 21:24:35 +00002466 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002467 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002468 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002469 break;
2470 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002471 case Type::Adjusted:
2472 case Type::Decayed:
2473 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2474 T = cast<AdjustedType>(T)->getAdjustedType();
2475 break;
2476 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002477
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002479 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002480 } while (true);
2481}
2482
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002483llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002484
2485 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002486 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002487
David Blaikief427b002014-05-06 03:42:01 +00002488 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 if (it != TypeCache.end()) {
2490 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002491 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 }
2494
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002495 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002496}
2497
David Blaikie0e716b42014-03-03 23:48:23 +00002498void CGDebugInfo::completeTemplateDefinition(
2499 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002500 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002501 return;
2502
David Blaikie0e716b42014-03-03 23:48:23 +00002503 completeClassData(&SD);
2504 // In case this type has no member function definitions being emitted, ensure
2505 // it is retained
2506 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2507}
2508
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002509llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002511 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002512
2513 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002514 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002515
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002516 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 return T;
2518
Adrian Prantlca844182015-09-11 17:23:03 +00002519 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002520 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002521
2522 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002523 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002524
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 return Res;
2526}
2527
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002528llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002529 // A forward declaration inside a module header does not belong to the module.
2530 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2531 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002532 if (DebugTypeExtRefs && D->isFromASTFile()) {
2533 // Record a reference to an imported clang module or precompiled header.
2534 auto *Reader = CGM.getContext().getExternalSource();
2535 auto Idx = D->getOwningModuleID();
2536 auto Info = Reader->getSourceDescriptor(Idx);
2537 if (Info)
2538 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2539 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002540 // We are building a clang module or a precompiled header.
2541 //
2542 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2543 // and it wouldn't be necessary to specify the parent scope
2544 // because the type is already unique by definition (it would look
2545 // like the output of -fno-standalone-debug). On the other hand,
2546 // the parent scope helps a consumer to quickly locate the object
2547 // file where the type's definition is located, so it might be
2548 // best to make this behavior a command line or debugger tuning
2549 // option.
2550 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2551 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002552 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002553 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2554 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002555 } else {
2556 // This the precompiled header being built.
2557 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002558 }
2559 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002560
Adrian Prantl9402cef2015-09-20 16:51:35 +00002561 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002562}
2563
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002564llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 // Handle qualifiers, which recursively handles what they refer to.
2566 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002567 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 // Work out details of type.
2570 switch (Ty->getTypeClass()) {
2571#define TYPE(Class, Base)
2572#define ABSTRACT_TYPE(Class, Base)
2573#define NON_CANONICAL_TYPE(Class, Base)
2574#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2575#include "clang/AST/TypeNodes.def"
2576 llvm_unreachable("Dependent types cannot show up in debug information");
2577
2578 case Type::ExtVector:
2579 case Type::Vector:
2580 return CreateType(cast<VectorType>(Ty), Unit);
2581 case Type::ObjCObjectPointer:
2582 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2583 case Type::ObjCObject:
2584 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002585 case Type::ObjCTypeParam:
2586 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 case Type::ObjCInterface:
2588 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2589 case Type::Builtin:
2590 return CreateType(cast<BuiltinType>(Ty));
2591 case Type::Complex:
2592 return CreateType(cast<ComplexType>(Ty));
2593 case Type::Pointer:
2594 return CreateType(cast<PointerType>(Ty), Unit);
2595 case Type::BlockPointer:
2596 return CreateType(cast<BlockPointerType>(Ty), Unit);
2597 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002598 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002599 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002600 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002601 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002602 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002603 case Type::FunctionProto:
2604 case Type::FunctionNoProto:
2605 return CreateType(cast<FunctionType>(Ty), Unit);
2606 case Type::ConstantArray:
2607 case Type::VariableArray:
2608 case Type::IncompleteArray:
2609 return CreateType(cast<ArrayType>(Ty), Unit);
2610
2611 case Type::LValueReference:
2612 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2613 case Type::RValueReference:
2614 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2615
2616 case Type::MemberPointer:
2617 return CreateType(cast<MemberPointerType>(Ty), Unit);
2618
2619 case Type::Atomic:
2620 return CreateType(cast<AtomicType>(Ty), Unit);
2621
Xiuli Pan9c14e282016-01-09 12:53:17 +00002622 case Type::Pipe:
2623 return CreateType(cast<PipeType>(Ty), Unit);
2624
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002626 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2627
David Blaikie42edade2014-11-11 20:44:45 +00002628 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002629 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002630 case Type::Adjusted:
2631 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002632 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 case Type::Elaborated:
2634 case Type::Paren:
2635 case Type::SubstTemplateTypeParm:
2636 case Type::TypeOfExpr:
2637 case Type::TypeOf:
2638 case Type::Decltype:
2639 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002640 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002641 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002642 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002643
David Blaikie42edade2014-11-11 20:44:45 +00002644 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002645}
2646
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002647llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2648 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002649 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002650
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002651 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002652
2653 // We may have cached a forward decl when we could have created
2654 // a non-forward decl. Go ahead and create a non-forward decl
2655 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002656 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002657 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002658
2659 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002660 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002661
2662 // Propagate members from the declaration to the definition
2663 // CreateType(const RecordType*) will overwrite this with the members in the
2664 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002665 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002666
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002668 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 return Res;
2670}
2671
2672// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002673llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002675
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002677 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002678 unsigned Line = getLineNumber(RD->getLocation());
2679 StringRef RDName = getClassName(RD);
2680
Amjad Abouddc4531e2016-04-30 01:44:38 +00002681 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002682
David Blaikied2785892013-08-18 17:36:19 +00002683 // If we ended up creating the type during the context chain construction,
2684 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002685 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002686 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002687 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002688 return T;
David Blaikied2785892013-08-18 17:36:19 +00002689
Adrian Prantl381e7552014-02-04 21:29:50 +00002690 // If this is just a forward or incomplete declaration, construct an
2691 // appropriately marked node and just return it.
2692 const RecordDecl *D = RD->getDefinition();
2693 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002694 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002695
2696 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002697 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002698
Manman Rene0064d82013-08-29 23:19:58 +00002699 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2700
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002701 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002702 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2703 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002704
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002705 // Elements of composite types usually have back to the type, creating
2706 // uniquing cycles. Distinct nodes are more efficient.
2707 switch (RealDecl->getTag()) {
2708 default:
2709 llvm_unreachable("invalid composite type tag");
2710
2711 case llvm::dwarf::DW_TAG_array_type:
2712 case llvm::dwarf::DW_TAG_enumeration_type:
2713 // Array elements and most enumeration elements don't have back references,
2714 // so they don't tend to be involved in uniquing cycles and there is some
2715 // chance of merging them when linking together two modules. Only make
2716 // them distinct if they are ODR-uniqued.
2717 if (FullName.empty())
2718 break;
2719
2720 case llvm::dwarf::DW_TAG_structure_type:
2721 case llvm::dwarf::DW_TAG_union_type:
2722 case llvm::dwarf::DW_TAG_class_type:
2723 // Immediatley resolve to a distinct node.
2724 RealDecl =
2725 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2726 break;
2727 }
2728
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002729 RegionMap[Ty->getDecl()].reset(RealDecl);
2730 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002731
David Majnemer58ed0f32016-07-17 00:39:12 +00002732 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002733 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002734 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002735 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002736}
2737
David Blaikieadfbf992013-08-18 16:55:33 +00002738void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002739 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002740 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002741 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002742 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2743 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002744 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002745 while (1) {
2746 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2747 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2748 if (PBT && !BRL.isPrimaryBaseVirtual())
2749 PBase = PBT;
2750 else
2751 break;
2752 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002753 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002754 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2755 getOrCreateFile(RD->getLocation())));
2756 } else if (RD->isDynamicClass())
2757 ContainingType = RealDecl;
2758
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002759 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002760}
2761
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002762llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002763 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002764 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002766 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002767 llvm::DIType *Ty =
2768 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2769 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 *Offset += FieldSize;
2771 return Ty;
2772}
2773
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002774void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002775 StringRef &Name,
2776 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002777 llvm::DIScope *&FDContext,
2778 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002779 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002780 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002781 Name = getFunctionName(FD);
2782 // Use mangled name as linkage name for C/C++ functions.
2783 if (FD->hasPrototype()) {
2784 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002785 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002786 }
2787 // No need to replicate the linkage name if it isn't different from the
2788 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002789 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002790 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2791 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002792 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002793 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002794 LinkageName = StringRef();
2795
Benjamin Kramer8c305922016-02-02 11:06:51 +00002796 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002797 if (const NamespaceDecl *NSDecl =
2798 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2799 FDContext = getOrCreateNameSpace(NSDecl);
2800 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002801 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2802 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2803 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2804 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002805 // Check if it is a noreturn-marked function
2806 if (FD->isNoReturn())
2807 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002808 // Collect template parameters.
2809 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2810 }
2811}
2812
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002813void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002814 unsigned &LineNo, QualType &T,
2815 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002816 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002817 Unit = getOrCreateFile(VD->getLocation());
2818 LineNo = getLineNumber(VD->getLocation());
2819
2820 setLocation(VD->getLocation());
2821
2822 T = VD->getType();
2823 if (T->isIncompleteArrayType()) {
2824 // CodeGen turns int[] into int[1] so we'll do the same here.
2825 llvm::APInt ConstVal(32, 1);
2826 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2827
2828 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2829 ArrayType::Normal, 0);
2830 }
2831
2832 Name = VD->getName();
2833 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2834 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2835 LinkageName = CGM.getMangledName(VD);
2836 if (LinkageName == Name)
2837 LinkageName = StringRef();
2838
2839 // Since we emit declarations (DW_AT_members) for static members, place the
2840 // definition of those static members in the namespace they were declared in
2841 // in the source code (the lexical decl context).
2842 // FIXME: Generalize this for even non-member global variables where the
2843 // declaration and definition may have different lexical decl contexts, once
2844 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002845 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2846 : VD->getDeclContext();
2847 // When a record type contains an in-line initialization of a static data
2848 // member, and the record type is marked as __declspec(dllexport), an implicit
2849 // definition of the member will be created in the record context. DWARF
2850 // doesn't seem to have a nice way to describe this in a form that consumers
2851 // are likely to understand, so fake the "normal" situation of a definition
2852 // outside the class by putting it in the global scope.
2853 if (DC->isRecord())
2854 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002855
Amjad Abouddc4531e2016-04-30 01:44:38 +00002856 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2857 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002858}
2859
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002860llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002861CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002862 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002863 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002864 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Frederic Rissd253ed62014-11-18 03:40:51 +00002865 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002866 llvm::DIFile *Unit = getOrCreateFile(Loc);
2867 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002868 unsigned Line = getLineNumber(Loc);
2869
2870 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2871 TParamsArray, Flags);
2872 // Build function type.
2873 SmallVector<QualType, 16> ArgTypes;
2874 for (const ParmVarDecl *Parm: FD->parameters())
2875 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002876 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2877 QualType FnType = CGM.getContext().getFunctionType(
2878 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002879 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002880 DContext, Name, LinkageName, Unit, Line,
2881 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002882 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002883 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002884 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002885 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2886 std::make_tuple(CanonDecl),
2887 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002888 return SP;
2889}
2890
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002891llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002892CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2893 QualType T;
2894 StringRef Name, LinkageName;
2895 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002896 llvm::DIFile *Unit = getOrCreateFile(Loc);
2897 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002898 unsigned Line = getLineNumber(Loc);
2899
2900 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002901 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002902 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2903 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002904 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002905 FwdDeclReplaceMap.emplace_back(
2906 std::piecewise_construct,
2907 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2908 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002909 return GV;
2910}
2911
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002912llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002913 // We only need a declaration (not a definition) of the type - so use whatever
2914 // we would otherwise do to get a type for a pointee. (forward declarations in
2915 // limited debug info, full definitions (if the type definition is available)
2916 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002917 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002918 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002919 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002920 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002921
Adrian Prantl5f4740d2016-12-20 02:10:02 +00002922 if (I != DeclCache.end()) {
2923 auto N = I->second;
2924 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
2925 return GVE->getVariable();
2926 return dyn_cast_or_null<llvm::DINode>(N);
2927 }
Frederic Rissd253ed62014-11-18 03:40:51 +00002928
2929 // No definition for now. Emit a forward definition that might be
2930 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002931 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002932 return getFunctionForwardDeclaration(FD);
2933 else if (const auto *VD = dyn_cast<VarDecl>(D))
2934 return getGlobalVariableForwardDeclaration(VD);
2935
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002936 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002937}
2938
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002939llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002940 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002941 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002942
David Majnemer58ed0f32016-07-17 00:39:12 +00002943 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002944 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002945 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002946
2947 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002948 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002949
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002950 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002951 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002952 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002953 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002954 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002955 }
2956 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002958 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002959 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 return SP;
2961 }
2962
Aaron Ballman86c93902014-03-06 23:45:36 +00002963 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002964 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002966 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002967 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 return SP;
2969 }
2970 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002971 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002972}
2973
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002974// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002975// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002976llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002977 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002978 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002979 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002980 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2981 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002982 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002983
David Majnemer58ed0f32016-07-17 00:39:12 +00002984 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002986
2987 const auto *FTy = FnType->getAs<FunctionType>();
2988 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2989
David Majnemer58ed0f32016-07-17 00:39:12 +00002990 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002992 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002993
2994 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002995 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002996
2997 // Replace the instancetype keyword with the actual type.
2998 if (ResultTy == CGM.getContext().getObjCInstanceType())
2999 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003000 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003001
Adrian Prantl7bec9032013-05-10 21:08:31 +00003002 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003004 QualType SelfDeclTy;
3005 if (auto *SelfDecl = OMethod->getSelfDecl())
3006 SelfDeclTy = SelfDecl->getType();
3007 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3008 if (FPT->getNumParams() > 1)
3009 SelfDeclTy = FPT->getParamType(0);
3010 if (!SelfDeclTy.isNull())
3011 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003013 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003014 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003015 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003016 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003017 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003018 // Variadic methods need a special marker at the end of the type list.
3019 if (OMethod->isVariadic())
3020 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003021
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003022 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003023 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3024 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003026
Adrian Prantl800faef2014-02-25 23:42:18 +00003027 // Handle variadic function types; they need an additional
3028 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003029 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003030 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003031 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003032 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003033 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3034 for (QualType ParamType : FPT->param_types())
3035 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003036 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003037 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003038 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3039 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003040 }
3041
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003042 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003043}
3044
Eric Christophere7b87e52014-10-26 23:40:33 +00003045void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3046 SourceLocation ScopeLoc, QualType FnType,
3047 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003048
3049 StringRef Name;
3050 StringRef LinkageName;
3051
3052 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3053
3054 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003055 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003056
Leny Kholodov80c047d2016-09-06 10:48:04 +00003057 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003058 llvm::DIFile *Unit = getOrCreateFile(Loc);
3059 llvm::DIScope *FDContext = Unit;
3060 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 if (!HasDecl) {
3062 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003063 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003064 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003065 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003066 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003067 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003068 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003069 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003070 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003071 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 return;
3073 }
3074 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003075 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3076 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003077 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003079 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003080 } else {
3081 // Use llvm function name.
3082 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003083 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003085 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 Name = Name.substr(1);
3087
Adrian Prantl42d71b92014-04-10 23:21:53 +00003088 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003089 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003090 // Artificial functions should not silently reuse CurLoc.
3091 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003092 }
3093 unsigned LineNo = getLineNumber(Loc);
3094 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003095
Eric Christopher8018e412014-03-27 18:50:35 +00003096 // FIXME: The function declaration we're constructing here is mostly reusing
3097 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3098 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3099 // all subprograms instead of the actual context since subprogram definitions
3100 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003101 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003102 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003103 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003104 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003105 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003106 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003107 // We might get here with a VarDecl in the case we're generating
3108 // code for the initialization of globals. Do not record these decls
3109 // as they will overwrite the actual VarDecl Decl in the cache.
3110 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003111 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003112
Adrian Prantlbebb8932014-03-21 21:01:58 +00003113 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003114 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003115
Guy Benyei11169dd2012-12-18 14:30:41 +00003116 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003117 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003118}
3119
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003120void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3121 QualType FnType) {
3122 StringRef Name;
3123 StringRef LinkageName;
3124
3125 const Decl *D = GD.getDecl();
3126 if (!D)
3127 return;
3128
Leny Kholodov80c047d2016-09-06 10:48:04 +00003129 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003130 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003131 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003132 llvm::DINodeArray TParamsArray;
3133 if (isa<FunctionDecl>(D)) {
3134 // If there is a DISubprogram for this function available then use it.
3135 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)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003138 Name = getObjCMethodName(OMD);
3139 Flags |= llvm::DINode::FlagPrototyped;
3140 } else {
3141 llvm_unreachable("not a function or ObjC method");
3142 }
3143 if (!Name.empty() && Name[0] == '\01')
3144 Name = Name.substr(1);
3145
3146 if (D->isImplicit()) {
3147 Flags |= llvm::DINode::FlagArtificial;
3148 // Artificial functions without a location should not silently reuse CurLoc.
3149 if (Loc.isInvalid())
3150 CurLoc = SourceLocation();
3151 }
3152 unsigned LineNo = getLineNumber(Loc);
3153 unsigned ScopeLine = 0;
3154
Adrian Prantle76bda52016-04-15 15:55:45 +00003155 DBuilder.retainType(DBuilder.createFunction(
3156 FDContext, Name, LinkageName, Unit, LineNo,
3157 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3158 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3159 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003160}
3161
David Blaikie835afb22015-01-21 23:08:17 +00003162void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 // Update our current location
3164 setLocation(Loc);
3165
Eric Christophere7b87e52014-10-26 23:40:33 +00003166 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3167 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003168
Adrian Prantle83b1302014-01-07 22:05:52 +00003169 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003170 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00003171 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003172}
3173
Guy Benyei11169dd2012-12-18 14:30:41 +00003174void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003175 llvm::MDNode *Back = nullptr;
3176 if (!LexicalBlockStack.empty())
3177 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003178 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003179 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003180 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003181}
3182
Eric Christopher0fdcb312013-05-16 00:52:20 +00003183void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3184 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003185 // Set our current location.
3186 setLocation(Loc);
3187
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00003189 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3190 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00003191
Benjamin Kramer8c305922016-02-02 11:06:51 +00003192 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003193 return;
3194
3195 // Create a new lexical block and push it on the stack.
3196 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003197}
3198
Eric Christopher0fdcb312013-05-16 00:52:20 +00003199void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3200 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3202
3203 // Provide an entry in the line table for the end of the block.
3204 EmitLocation(Builder, Loc);
3205
Benjamin Kramer8c305922016-02-02 11:06:51 +00003206 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003207 return;
3208
Guy Benyei11169dd2012-12-18 14:30:41 +00003209 LexicalBlockStack.pop_back();
3210}
3211
Guy Benyei11169dd2012-12-18 14:30:41 +00003212void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3213 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3214 unsigned RCount = FnBeginRegionCount.back();
3215 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3216
3217 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003218 while (LexicalBlockStack.size() != RCount) {
3219 // Provide an entry in the line table for the end of the block.
3220 EmitLocation(Builder, CurLoc);
3221 LexicalBlockStack.pop_back();
3222 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003223 FnBeginRegionCount.pop_back();
3224}
3225
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003226llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003227 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003228
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003229 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003230 QualType FType;
3231 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003232 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003233
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003234 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003235 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003236
3237 FieldOffset = 0;
3238 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3239 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3240 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3241 FType = CGM.getContext().IntTy;
3242 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3243 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3244
3245 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3246 if (HasCopyAndDispose) {
3247 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003248 EltTys.push_back(
3249 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3250 EltTys.push_back(
3251 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003252 }
3253 bool HasByrefExtendedLayout;
3254 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003255 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3256 HasByrefExtendedLayout) &&
3257 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003258 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003259 EltTys.push_back(
3260 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003261 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003262
Guy Benyei11169dd2012-12-18 14:30:41 +00003263 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3264 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003265 CGM.getTarget().getPointerAlign(0))) {
3266 CharUnits FieldOffsetInBytes =
3267 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003268 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003269 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003270
Guy Benyei11169dd2012-12-18 14:30:41 +00003271 if (NumPaddingBytes.isPositive()) {
3272 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3273 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3274 pad, ArrayType::Normal, 0);
3275 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3276 }
3277 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003278
Guy Benyei11169dd2012-12-18 14:30:41 +00003279 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003280 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003281 FieldSize = CGM.getContext().getTypeSize(FType);
3282 FieldAlign = CGM.getContext().toBits(Align);
3283
Eric Christopherb2a008c2013-05-16 00:45:12 +00003284 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003285 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003286 FieldAlign, FieldOffset,
3287 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 EltTys.push_back(FieldTy);
3289 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003290
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003291 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003292
Leny Kholodov80c047d2016-09-06 10:48:04 +00003293 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003294
Guy Benyei11169dd2012-12-18 14:30:41 +00003295 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003296 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003297}
3298
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003299void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3300 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003301 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003302 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003304 if (VD->hasAttr<NoDebugAttr>())
3305 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003306
David Blaikie7fceebf2013-08-19 03:37:48 +00003307 bool Unwritten =
3308 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3309 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003310 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003311 if (!Unwritten)
3312 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 uint64_t XOffset = 0;
3315 if (VD->hasAttr<BlocksAttr>())
3316 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003317 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003318 Ty = getOrCreateType(VD->getType(), Unit);
3319
3320 // If there is no debug info for this type then do not emit debug info
3321 // for this variable.
3322 if (!Ty)
3323 return;
3324
Guy Benyei11169dd2012-12-18 14:30:41 +00003325 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003326 unsigned Line = 0;
3327 unsigned Column = 0;
3328 if (!Unwritten) {
3329 Line = getLineNumber(VD->getLocation());
3330 Column = getColumnNumber(VD->getLocation());
3331 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003332 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003333 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003334 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003335 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003336
3337 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3338
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 // If this is the first argument and it is implicit then
3340 // give it an object pointer flag.
3341 // FIXME: There has to be a better way to do this, but for static
3342 // functions there won't be an implicit param at arg1 and
3343 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003344 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003345 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003346 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003347 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3348 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003349 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003350
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003351 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003352
3353 StringRef Name = VD->getName();
3354 if (!Name.empty()) {
3355 if (VD->hasAttr<BlocksAttr>()) {
3356 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003357 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003358 // offset of __forwarding field
3359 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003360 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003361 Expr.push_back(offset.getQuantity());
3362 Expr.push_back(llvm::dwarf::DW_OP_deref);
3363 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 // offset of x field
3365 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003366 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003367
3368 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003369 auto *D = ArgNo
3370 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3371 *ArgNo, Unit, Line, Ty)
3372 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003373 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003374
Guy Benyei11169dd2012-12-18 14:30:41 +00003375 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003376 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3377 llvm::DebugLoc::get(Line, Column, Scope),
3378 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003380 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003381 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003382 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003383 // If VD is an anonymous union then Storage represents value for
3384 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003385 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003386 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003387 // GDB has trouble finding local variables in anonymous unions, so we emit
3388 // artifical local variables for each of the members.
3389 //
3390 // FIXME: Remove this code as soon as GDB supports this.
3391 // The debug info verifier in LLVM operates based on the assumption that a
3392 // variable has the same size as its storage and we had to disable the check
3393 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003394 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003395 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003396 StringRef FieldName = Field->getName();
3397
3398 // Ignore unnamed fields. Do not ignore unnamed records.
3399 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3400 continue;
3401
3402 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003403 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003404 auto *D = DBuilder.createAutoVariable(
3405 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003406 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003407
3408 // Insert an llvm.dbg.declare into the current block.
3409 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3410 llvm::DebugLoc::get(Line, Column, Scope),
3411 Builder.GetInsertBlock());
3412 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003413 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003415
3416 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003417 auto *D = ArgNo
3418 ? DBuilder.createParameterVariable(
3419 Scope, Name, *ArgNo, Unit, Line, Ty,
3420 CGM.getLangOpts().Optimize, Flags)
3421 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3422 CGM.getLangOpts().Optimize, Flags,
3423 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003424
3425 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003426 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3427 llvm::DebugLoc::get(Line, Column, Scope),
3428 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003429}
3430
3431void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3432 llvm::Value *Storage,
3433 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003434 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003435 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003436}
3437
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003438llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3439 llvm::DIType *Ty) {
3440 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003441 if (CachedTy)
3442 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003443 return DBuilder.createObjectPointerType(Ty);
3444}
3445
Eric Christophere7b87e52014-10-26 23:40:33 +00003446void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3447 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003448 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003449 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003450 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003451
Craig Topper8a13c412014-05-21 05:09:00 +00003452 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003453 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003454 if (VD->hasAttr<NoDebugAttr>())
3455 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003456
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003458
Guy Benyei11169dd2012-12-18 14:30:41 +00003459 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003460 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3461 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003462 if (isByRef)
3463 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003464 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003465 Ty = getOrCreateType(VD->getType(), Unit);
3466
3467 // Self is passed along as an implicit non-arg variable in a
3468 // block. Mark it as the object pointer.
3469 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003470 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003471
3472 // Get location information.
3473 unsigned Line = getLineNumber(VD->getLocation());
3474 unsigned Column = getColumnNumber(VD->getLocation());
3475
3476 const llvm::DataLayout &target = CGM.getDataLayout();
3477
3478 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003479 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003480 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3481
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003482 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003483 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003484 addr.push_back(llvm::dwarf::DW_OP_deref);
3485 addr.push_back(llvm::dwarf::DW_OP_plus);
3486 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003487 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003488 addr.push_back(llvm::dwarf::DW_OP_deref);
3489 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003491 offset =
3492 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003493 addr.push_back(offset.getQuantity());
3494 addr.push_back(llvm::dwarf::DW_OP_deref);
3495 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 // offset of x field
3497 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003498 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 }
3500
3501 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003502 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003503 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003504 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003505 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003506
Guy Benyei11169dd2012-12-18 14:30:41 +00003507 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003508 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3509 if (InsertPoint)
3510 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3511 InsertPoint);
3512 else
3513 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3514 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003515}
3516
Guy Benyei11169dd2012-12-18 14:30:41 +00003517void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3518 unsigned ArgNo,
3519 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003520 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003521 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003522}
3523
3524namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003525struct BlockLayoutChunk {
3526 uint64_t OffsetInBits;
3527 const BlockDecl::Capture *Capture;
3528};
3529bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3530 return l.OffsetInBits < r.OffsetInBits;
3531}
Guy Benyei11169dd2012-12-18 14:30:41 +00003532}
3533
3534void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003535 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003536 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003537 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003538 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003539 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003540 ASTContext &C = CGM.getContext();
3541 const BlockDecl *blockDecl = block.getBlockDecl();
3542
3543 // Collect some general information about the block's location.
3544 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003545 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003546 unsigned line = getLineNumber(loc);
3547 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003548
Guy Benyei11169dd2012-12-18 14:30:41 +00003549 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003550 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003551
3552 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003553 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003554
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003555 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003556 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003557 blockLayout->getElementOffsetInBits(0),
3558 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003559 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 blockLayout->getElementOffsetInBits(1),
3561 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003562 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003563 blockLayout->getElementOffsetInBits(2),
3564 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003565 auto *FnTy = block.getBlockExpr()->getFunctionType();
3566 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003567 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003568 blockLayout->getElementOffsetInBits(3),
3569 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003570 fields.push_back(createFieldType(
3571 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3572 ? C.getBlockDescriptorExtendedType()
3573 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003574 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003575
3576 // We want to sort the captures by offset, not because DWARF
3577 // requires this, but because we're paranoid about debuggers.
3578 SmallVector<BlockLayoutChunk, 8> chunks;
3579
3580 // 'this' capture.
3581 if (blockDecl->capturesCXXThis()) {
3582 BlockLayoutChunk chunk;
3583 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003584 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003585 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003586 chunks.push_back(chunk);
3587 }
3588
3589 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003590 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 const VarDecl *variable = capture.getVariable();
3592 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3593
3594 // Ignore constant captures.
3595 if (captureInfo.isConstant())
3596 continue;
3597
3598 BlockLayoutChunk chunk;
3599 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003600 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003601 chunk.Capture = &capture;
3602 chunks.push_back(chunk);
3603 }
3604
3605 // Sort by offset.
3606 llvm::array_pod_sort(chunks.begin(), chunks.end());
3607
David Majnemer58ed0f32016-07-17 00:39:12 +00003608 for (const BlockLayoutChunk &Chunk : chunks) {
3609 uint64_t offsetInBits = Chunk.OffsetInBits;
3610 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003611
3612 // If we have a null capture, this must be the C++ 'this' capture.
3613 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003614 QualType type;
3615 if (auto *Method =
3616 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3617 type = Method->getThisType(C);
3618 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3619 type = QualType(RDecl->getTypeForDecl(), 0);
3620 else
3621 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003622
David Majnemerb4b671e2016-06-30 03:01:59 +00003623 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003624 offsetInBits, tunit, tunit));
3625 continue;
3626 }
3627
3628 const VarDecl *variable = capture->getVariable();
3629 StringRef name = variable->getName();
3630
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003631 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003632 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003633 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003634 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003635
3636 // FIXME: this creates a second copy of this type!
3637 uint64_t xoffset;
3638 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003639 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003640 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3641 PtrInfo.Width, Align, offsetInBits,
3642 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003643 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003644 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003645 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003646 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003647 }
3648 fields.push_back(fieldType);
3649 }
3650
3651 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003652 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3653 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003654
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003655 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003656
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003657 llvm::DIType *type =
3658 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003659 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003660 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003661 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3662
3663 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003664 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003665 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003666
3667 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003668 auto *debugVar = DBuilder.createParameterVariable(
3669 scope, Arg->getName(), ArgNo, tunit, line, type,
3670 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003671
Adrian Prantl616bef42013-03-14 21:52:59 +00003672 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003673 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003674 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003675 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003676 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003677 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003678
Adrian Prantl616bef42013-03-14 21:52:59 +00003679 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003680 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3681 llvm::DebugLoc::get(line, column, scope),
3682 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003683}
3684
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003685llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003686CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3687 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003688 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003689
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003690 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003691 if (MI != StaticDataMemberCache.end()) {
3692 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003693 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003694 }
David Blaikiece763042013-08-20 21:49:21 +00003695
3696 // If the member wasn't found in the cache, lazily construct and add it to the
3697 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003698 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003699 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003700 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003701}
3702
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003703llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003704 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3705 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003706 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003707
3708 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003709 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003710 StringRef FieldName = Field->getName();
3711
3712 // Ignore unnamed fields, but recurse into anonymous records.
3713 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003714 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003715 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003716 Var, DContext);
3717 continue;
3718 }
3719 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003720 GVE = DBuilder.createGlobalVariableExpression(
3721 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3722 Var->hasLocalLinkage());
3723 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003724 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003725 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003726}
3727
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003728void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003729 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003730 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003731 if (D->hasAttr<NoDebugAttr>())
3732 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003733
3734 // If we already created a DIGlobalVariable for this declaration, just attach
3735 // it to the llvm::GlobalVariable.
3736 auto Cached = DeclCache.find(D->getCanonicalDecl());
3737 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003738 return Var->addDebugInfo(
3739 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003740
Guy Benyei11169dd2012-12-18 14:30:41 +00003741 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003742 llvm::DIFile *Unit = nullptr;
3743 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003744 unsigned LineNo;
3745 StringRef DeclName, LinkageName;
3746 QualType T;
3747 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003748
3749 // Attempt to store one global variable for the declaration - even if we
3750 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003751 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003752
3753 // If this is an anonymous union then we'll want to emit a global
3754 // variable for each member of the anonymous union so that it's possible
3755 // to find the name of any field in the union.
3756 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003757 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003758 assert(RD->isAnonymousStructOrUnion() &&
3759 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003760 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003761 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003762 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003763 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003764 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003765 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003766 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003767 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003768 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003769 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003770}
3771
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003772void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003773 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003774 if (VD->hasAttr<NoDebugAttr>())
3775 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003776 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003777 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003778 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003779 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003780 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003781 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3782 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003783 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3784 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3785 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003786 // Do not use global variables for enums.
3787 //
3788 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003789 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003790 return;
David Blaikiea15565562014-04-04 20:56:17 +00003791 // Do not emit separate definitions for function local const/statics.
3792 if (isa<FunctionDecl>(VD->getDeclContext()))
3793 return;
David Blaikiebb113912014-04-05 07:23:17 +00003794 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003795 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003796 if (VarD->isStaticDataMember()) {
3797 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003798 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003799 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003800 //
3801 // FIXME: This is probably unnecessary, since Ty should reference RD
3802 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003803 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003804 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003805 return;
3806 }
3807
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003808 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003809
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003810 auto &GV = DeclCache[VD];
3811 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003812 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003813 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003814 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3815 // FIXME: Add a representation for integer constants wider than 64 bits.
3816 if (Init.isInt())
3817 InitExpr =
3818 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3819 else if (Init.isFloat())
3820 InitExpr = DBuilder.createConstantValueExpression(
3821 Init.getFloat().bitcastToAPInt().getZExtValue());
3822 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003823 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003824 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003825 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3826 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003827}
3828
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003829llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003830 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003831 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003832 llvm::DIScope *Mod = getParentModuleOrNull(D);
3833 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003834}
3835
David Blaikie9f88fe82013-04-22 06:13:21 +00003836void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003837 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003838 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003839 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003840 if (!NSDecl->isAnonymousNamespace() ||
3841 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003842 DBuilder.createImportedModule(
3843 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3844 getOrCreateNameSpace(NSDecl),
3845 getLineNumber(UD.getLocation()));
3846 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003847}
3848
David Blaikiebd483762013-05-20 04:58:53 +00003849void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003850 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003851 return;
3852 assert(UD.shadow_size() &&
3853 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3854 // Emitting one decl is sufficient - debuggers can detect that this is an
3855 // overloaded name & provide lookup for all the overloads.
3856 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003857
3858 // FIXME: Skip functions with undeduced auto return type for now since we
3859 // don't currently have the plumbing for separate declarations & definitions
3860 // of free functions and mismatched types (auto in the declaration, concrete
3861 // return type in the definition)
3862 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3863 if (const auto *AT =
3864 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3865 if (AT->getDeducedType().isNull())
3866 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003867 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003868 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003869 DBuilder.createImportedDeclaration(
3870 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3871 getLineNumber(USD.getLocation()));
3872}
3873
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003874void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003875 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3876 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003877 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003878 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003879 DBuilder.createImportedDeclaration(
3880 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3881 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3882 getLineNumber(ID.getLocation()));
3883 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003884}
3885
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003886llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003887CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003888 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003889 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003890 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003891 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003892 return cast<llvm::DIImportedEntity>(VH);
3893 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003894 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003895 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3896 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003897 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003898 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3899 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3900 NA.getName());
3901 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003902 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003903 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3904 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3905 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003906 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003907 return R;
3908}
3909
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003910llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003911CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003912 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003913 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003914 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003915 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003916
Guy Benyei11169dd2012-12-18 14:30:41 +00003917 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003918 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003919 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00003920 llvm::DINamespace *NS = DBuilder.createNameSpace(
3921 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003922 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003923 return NS;
3924}
3925
Adrian Prantla5206ce2015-09-22 23:26:43 +00003926void CGDebugInfo::setDwoId(uint64_t Signature) {
3927 assert(TheCU && "no main compile unit");
3928 TheCU->setDWOId(Signature);
3929}
3930
3931
Guy Benyei11169dd2012-12-18 14:30:41 +00003932void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003933 // Creating types might create further types - invalidating the current
3934 // element and the size(), so don't cache/reference them.
3935 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3936 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003937 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003938 ? CreateTypeDefinition(E.Type, E.Unit)
3939 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003940 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003941 }
3942
David Blaikief427b002014-05-06 03:42:01 +00003943 for (auto p : ReplaceMap) {
3944 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003945 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003946 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003947
David Blaikief427b002014-05-06 03:42:01 +00003948 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003949 assert(it != TypeCache.end());
3950 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003951
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003952 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3953 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003954 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003955
Frederic Rissd253ed62014-11-18 03:40:51 +00003956 for (const auto &p : FwdDeclReplaceMap) {
3957 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003958 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003959 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003960
3961 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003962 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003963 // with ourselves, that will destroy the temporary MDNode and
3964 // replace it with a standard one, avoiding leaking memory.
3965 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003966 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003967 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003968 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003969
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003970 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
3971 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003972 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003973 }
3974
Adrian Prantl73409ce2013-03-11 18:33:46 +00003975 // We keep our own list of retained types, because we need to look
3976 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003977 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003978 if (auto MD = TypeCache[RT])
3979 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003980
Guy Benyei11169dd2012-12-18 14:30:41 +00003981 DBuilder.finalize();
3982}
David Blaikie66088d52014-09-24 17:01:27 +00003983
3984void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003985 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003986 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003987
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003988 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003989 // Don't ignore in case of explicit cast where it is referenced indirectly.
3990 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003991}
Amara Emerson652795d2016-11-10 14:44:30 +00003992
3993llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
3994 if (LexicalBlockStack.empty())
3995 return llvm::DebugLoc();
3996
3997 llvm::MDNode *Scope = LexicalBlockStack.back();
3998 return llvm::DebugLoc::get(
3999 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4000}