blob: c51a1d406ff1314bf6e0764c59dde7a3aecbdf17 [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"
Adrian Prantl0630eb72013-12-18 21:48:18 +000044#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045using namespace clang;
46using namespace clang::CodeGen;
47
Victor Leschuka7ece032016-10-20 00:13:19 +000048static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
49 auto TI = Ctx.getTypeInfo(Ty);
50 return TI.AlignIsRequired ? TI.Align : 0;
51}
52
53static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
54 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
55}
56
57static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
58 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
59}
60
Guy Benyei11169dd2012-12-18 14:30:41 +000061CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000062 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000063 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000064 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000065 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
66 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000067 CreateCompileUnit();
68}
69
70CGDebugInfo::~CGDebugInfo() {
71 assert(LexicalBlockStack.empty() &&
72 "Region stack mismatch, stack not empty!");
73}
74
David Blaikie66e41972015-01-14 07:38:27 +000075ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000076 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000077 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000078 init(TemporaryLocation);
79}
80
Adrian Prantl39428e72015-02-03 18:40:42 +000081ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000082 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000083 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000084 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000085 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000086}
87
88void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000089 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000090 auto *DI = CGF->getDebugInfo();
91 if (!DI) {
92 CGF = nullptr;
93 return;
David Blaikie66e41972015-01-14 07:38:27 +000094 }
David Blaikied7057d92015-08-12 23:49:57 +000095
96 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
97 if (TemporaryLocation.isValid()) {
98 DI->EmitLocation(CGF->Builder, TemporaryLocation);
99 return;
100 }
101
102 if (DefaultToEmpty) {
103 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
104 return;
105 }
106
107 // Construct a location that has a valid scope, but no line info.
108 assert(!DI->LexicalBlockStack.empty());
109 CGF->Builder.SetCurrentDebugLocation(
110 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000111}
112
David Blaikie9b479662015-01-25 01:19:10 +0000113ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000114 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000115 init(E->getExprLoc());
116}
117
David Blaikie66e41972015-01-14 07:38:27 +0000118ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000119 : CGF(&CGF) {
120 if (!CGF.getDebugInfo()) {
121 this->CGF = nullptr;
122 return;
David Blaikie66e41972015-01-14 07:38:27 +0000123 }
David Blaikied7057d92015-08-12 23:49:57 +0000124 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
125 if (Loc)
126 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000127}
128
129ApplyDebugLocation::~ApplyDebugLocation() {
130 // Query CGF so the location isn't overwritten when location updates are
131 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000132 if (CGF)
133 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000134}
135
Guy Benyei11169dd2012-12-18 14:30:41 +0000136void CGDebugInfo::setLocation(SourceLocation Loc) {
137 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000138 if (Loc.isInvalid())
139 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000140
141 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
142
143 // If we've changed files in the middle of a lexical scope go ahead
144 // and create a new lexical scope with file node if it's different
145 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000146 if (LexicalBlockStack.empty())
147 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000148
149 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000150 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000152
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000153 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000154 return;
155
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000156 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000157 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000158 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
159 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000160 } else if (isa<llvm::DILexicalBlock>(Scope) ||
161 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000163 LexicalBlockStack.emplace_back(
164 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 }
166}
167
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000168llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000169 llvm::DIScope *Mod = getParentModuleOrNull(D);
170 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
171 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000172}
173
174llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
175 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000176 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000177 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000178
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000179 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000180 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000181 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000182 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 }
184
185 // Check namespace.
David Majnemer58ed0f32016-07-17 00:39:12 +0000186 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000187 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000188
David Majnemer58ed0f32016-07-17 00:39:12 +0000189 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000190 if (!RDecl->isDependentType())
191 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000192 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000193 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000194}
195
Guy Benyei11169dd2012-12-18 14:30:41 +0000196StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000197 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000198 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000199 FunctionTemplateSpecializationInfo *Info =
200 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000201
Reid Kleckner31abd802016-06-30 17:41:31 +0000202 // Emit the unqualified name in normal operation. LLVM and the debugger can
203 // compute the fully qualified name from the scope chain. If we're only
204 // emitting line table info, there won't be any scope chains, so emit the
205 // fully qualified name here so that stack traces are more accurate.
206 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
207 // evaluating the size impact.
208 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
209 CGM.getCodeGenOpts().EmitCodeView;
210
211 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000212 return FII->getName();
213
Benjamin Kramer9170e912013-02-22 15:46:01 +0000214 SmallString<128> NS;
215 llvm::raw_svector_ostream OS(NS);
Reid Kleckner60103382015-12-16 02:04:40 +0000216 PrintingPolicy Policy(CGM.getLangOpts());
Reid Kleckner829398e2016-06-17 16:11:20 +0000217 Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView;
Reid Kleckner31abd802016-06-30 17:41:31 +0000218 if (!UseQualifiedName)
219 FD->printName(OS);
220 else
221 FD->printQualifiedName(OS, Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000222
Reid Kleckner829398e2016-06-17 16:11:20 +0000223 // Add any template specialization args.
224 if (Info) {
225 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000226 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner829398e2016-06-17 16:11:20 +0000227 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000228 }
229
230 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000231 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000232}
233
234StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
235 SmallString<256> MethodName;
236 llvm::raw_svector_ostream OS(MethodName);
237 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
238 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000239 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000240 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000241 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000242 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000243 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000244 if (OC->IsClassExtension()) {
245 OS << OC->getClassInterface()->getName();
246 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000247 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000248 << OC->getIdentifier()->getNameStart() << ')';
249 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000250 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000251 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
252 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000253 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000254 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000255 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000256 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000257 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000258 ClassTy.print(OS, PrintingPolicy(LangOptions()));
259 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000260 }
261 OS << ' ' << OMD->getSelector().getAsString() << ']';
262
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000263 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000264}
265
Guy Benyei11169dd2012-12-18 14:30:41 +0000266StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000267 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000268}
269
Eric Christophere7b87e52014-10-26 23:40:33 +0000270StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000271 if (isa<ClassTemplateSpecializationDecl>(RD)) {
272 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000273 llvm::raw_svector_ostream OS(Name);
274 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
275 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000276
277 // Copy this name on the side and use its reference.
278 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000279 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000280
David Majnemerbc5976a2016-07-01 23:12:54 +0000281 // quick optimization to avoid having to intern strings that are already
282 // stored reliably elsewhere
283 if (const IdentifierInfo *II = RD->getIdentifier())
284 return II->getName();
285
286 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
287 // used to reconstruct the fully qualified type names.
288 if (CGM.getCodeGenOpts().EmitCodeView) {
289 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
290 assert(RD->getDeclContext() == D->getDeclContext() &&
291 "Typedef should not be in another decl context!");
292 assert(D->getDeclName().getAsIdentifierInfo() &&
293 "Typedef was not named!");
294 return D->getDeclName().getAsIdentifierInfo()->getName();
295 }
296
297 if (CGM.getLangOpts().CPlusPlus) {
298 StringRef Name;
299
300 ASTContext &Context = CGM.getContext();
301 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
302 // Anonymous types without a name for linkage purposes have their
303 // declarator mangled in if they have one.
304 Name = DD->getName();
305 else if (const TypedefNameDecl *TND =
306 Context.getTypedefNameForUnnamedTagDecl(RD))
307 // Anonymous types without a name for linkage purposes have their
308 // associate typedef mangled in if they have one.
309 Name = TND->getName();
310
311 if (!Name.empty()) {
312 SmallString<256> UnnamedType("<unnamed-type-");
313 UnnamedType += Name;
314 UnnamedType += '>';
315 return internString(UnnamedType);
316 }
317 }
318 }
319
320 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000321}
322
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000323llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000324 if (!Loc.isValid())
325 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000326 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
327 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000328
329 SourceManager &SM = CGM.getContext().getSourceManager();
330 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
331
332 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
333 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000334 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
335 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000336
337 // Cache the results.
338 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000339 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000340
341 if (it != DIFileCache.end()) {
342 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000343 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000344 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000345 }
346
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000347 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
348 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000349
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000350 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000351 return F;
352}
353
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000354llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000355 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
356 remapDIPath(TheCU->getDirectory()));
357}
358
359std::string CGDebugInfo::remapDIPath(StringRef Path) const {
360 for (const auto &Entry : DebugPrefixMap)
361 if (Path.startswith(Entry.first))
362 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
363 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000364}
365
Guy Benyei11169dd2012-12-18 14:30:41 +0000366unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
367 if (Loc.isInvalid() && CurLoc.isInvalid())
368 return 0;
369 SourceManager &SM = CGM.getContext().getSourceManager();
370 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000371 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000372}
373
Adrian Prantlc7822422013-03-12 20:43:25 +0000374unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000375 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000376 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000377 return 0;
378
379 // If the location is invalid then use the current column.
380 if (Loc.isInvalid() && CurLoc.isInvalid())
381 return 0;
382 SourceManager &SM = CGM.getContext().getSourceManager();
383 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000384 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000385}
386
387StringRef CGDebugInfo::getCurrentDirname() {
388 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
389 return CGM.getCodeGenOpts().DebugCompilationDir;
390
391 if (!CWDName.empty())
392 return CWDName;
393 SmallString<256> CWD;
394 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000395 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000396}
397
Guy Benyei11169dd2012-12-18 14:30:41 +0000398void CGDebugInfo::CreateCompileUnit() {
399
David Blaikieaabde052014-05-14 00:29:00 +0000400 // Should we be asking the SourceManager for the main file name, instead of
401 // accepting it as an argument? This just causes the main file name to
402 // mismatch with source locations and create extra lexical scopes or
403 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
404 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
405 // because that's what the SourceManager says)
406
Guy Benyei11169dd2012-12-18 14:30:41 +0000407 // Get absolute path name.
408 SourceManager &SM = CGM.getContext().getSourceManager();
409 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
410 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000411 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000412
413 // The main file name provided via the "-main-file-name" option contains just
414 // the file name itself with no path information. This file name may have had
415 // a relative path, so we look into the actual file entry for the main
416 // file to determine the real absolute path for the file.
417 std::string MainFileDir;
418 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000419 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000420 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000421 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
422 llvm::sys::path::append(MainFileDirSS, MainFileName);
423 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000424 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 }
426
Ed Masteda706022014-05-07 12:49:30 +0000427 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 const LangOptions &LO = CGM.getLangOpts();
429 if (LO.CPlusPlus) {
430 if (LO.ObjC1)
431 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
432 else
433 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
434 } else if (LO.ObjC1) {
435 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000436 } else if (LO.RenderScript) {
437 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 } else if (LO.C99) {
439 LangTag = llvm::dwarf::DW_LANG_C99;
440 } else {
441 LangTag = llvm::dwarf::DW_LANG_C89;
442 }
443
444 std::string Producer = getClangFullVersion();
445
446 // Figure out which version of the ObjC runtime we have.
447 unsigned RuntimeVers = 0;
448 if (LO.ObjC1)
449 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
450
Adrian Prantl826824e2016-04-08 22:43:06 +0000451 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
452 switch (DebugKind) {
453 case codegenoptions::NoDebugInfo:
454 case codegenoptions::LocTrackingOnly:
455 EmissionKind = llvm::DICompileUnit::NoDebug;
456 break;
457 case codegenoptions::DebugLineTablesOnly:
458 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
459 break;
460 case codegenoptions::LimitedDebugInfo:
461 case codegenoptions::FullDebugInfo:
462 EmissionKind = llvm::DICompileUnit::FullDebug;
463 break;
464 }
465
Guy Benyei11169dd2012-12-18 14:30:41 +0000466 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000468 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000469 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
470 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
David Blaikiea45c31a2016-08-24 18:29:58 +0000471 CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */,
472 CGM.getCodeGenOpts().SplitDwarfInlining);
Guy Benyei11169dd2012-12-18 14:30:41 +0000473}
474
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000475llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000476 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000477 StringRef BTName;
478 switch (BT->getKind()) {
479#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000480#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000481#include "clang/AST/BuiltinTypes.def"
482 case BuiltinType::Dependent:
483 llvm_unreachable("Unexpected builtin type");
484 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000485 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000486 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000487 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000488 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000489 if (!ClassTy)
490 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
491 "objc_class", TheCU,
492 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000493 return ClassTy;
494 case BuiltinType::ObjCId: {
495 // typedef struct objc_class *Class;
496 // typedef struct objc_object {
497 // Class isa;
498 // } *id;
499
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000500 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000501 return ObjTy;
502
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000503 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
505 "objc_class", TheCU,
506 getOrCreateMainFile(), 0);
507
508 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000509
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000510 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000511
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000512 ObjTy = DBuilder.createStructType(
513 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
514 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000515
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000516 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000517 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
518 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
519 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000520 return ObjTy;
521 }
522 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000523 if (!SelTy)
524 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
525 "objc_selector", TheCU,
526 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 return SelTy;
528 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000529
Alexey Bader954ba212016-04-08 13:40:33 +0000530#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
531 case BuiltinType::Id: \
532 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
533 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000534#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000535 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000536 return getOrCreateStructPtrType("opencl_sampler_t",
537 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000538 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000540 case BuiltinType::OCLClkEvent:
541 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
542 case BuiltinType::OCLQueue:
543 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
544 case BuiltinType::OCLNDRange:
545 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
546 case BuiltinType::OCLReserveID:
547 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000548
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000550 case BuiltinType::Char_U:
551 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
552 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000554 case BuiltinType::SChar:
555 Encoding = llvm::dwarf::DW_ATE_signed_char;
556 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000557 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000558 case BuiltinType::Char32:
559 Encoding = llvm::dwarf::DW_ATE_UTF;
560 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000561 case BuiltinType::UShort:
562 case BuiltinType::UInt:
563 case BuiltinType::UInt128:
564 case BuiltinType::ULong:
565 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000566 case BuiltinType::ULongLong:
567 Encoding = llvm::dwarf::DW_ATE_unsigned;
568 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000569 case BuiltinType::Short:
570 case BuiltinType::Int:
571 case BuiltinType::Int128:
572 case BuiltinType::Long:
573 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000574 case BuiltinType::LongLong:
575 Encoding = llvm::dwarf::DW_ATE_signed;
576 break;
577 case BuiltinType::Bool:
578 Encoding = llvm::dwarf::DW_ATE_boolean;
579 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 case BuiltinType::Half:
581 case BuiltinType::Float:
582 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000583 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000584 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000585 // FIXME: For targets where long double and __float128 have the same size,
586 // they are currently indistinguishable in the debugger without some
587 // special treatment. However, there is currently no consensus on encoding
588 // and this should be updated once a DWARF encoding exists for distinct
589 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000590 Encoding = llvm::dwarf::DW_ATE_float;
591 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 }
593
594 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000595 case BuiltinType::Long:
596 BTName = "long int";
597 break;
598 case BuiltinType::LongLong:
599 BTName = "long long int";
600 break;
601 case BuiltinType::ULong:
602 BTName = "long unsigned int";
603 break;
604 case BuiltinType::ULongLong:
605 BTName = "long long unsigned int";
606 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000607 default:
608 BTName = BT->getName(CGM.getLangOpts());
609 break;
610 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000611 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000613 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000614}
615
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000616llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000617 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000618 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000619 if (Ty->isComplexIntegerType())
620 Encoding = llvm::dwarf::DW_ATE_lo_user;
621
622 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000623 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000624}
625
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000626llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
627 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000628 QualifierCollector Qc;
629 const Type *T = Qc.strip(Ty);
630
631 // Ignore these qualifiers for now.
632 Qc.removeObjCGCAttr();
633 Qc.removeAddressSpace();
634 Qc.removeObjCLifetime();
635
636 // We will create one Derived type for one qualifier and recurse to handle any
637 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000638 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000639 if (Qc.hasConst()) {
640 Tag = llvm::dwarf::DW_TAG_const_type;
641 Qc.removeConst();
642 } else if (Qc.hasVolatile()) {
643 Tag = llvm::dwarf::DW_TAG_volatile_type;
644 Qc.removeVolatile();
645 } else if (Qc.hasRestrict()) {
646 Tag = llvm::dwarf::DW_TAG_restrict_type;
647 Qc.removeRestrict();
648 } else {
649 assert(Qc.empty() && "Unknown type qualifier for debug info");
650 return getOrCreateType(QualType(T, 0), Unit);
651 }
652
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000653 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000654
655 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
656 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000657 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000658}
659
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000660llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
661 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000662
663 // The frontend treats 'id' as a typedef to an ObjCObjectType,
664 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
665 // debug info, we want to emit 'id' in both cases.
666 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000667 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000668
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000669 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
670 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000671}
672
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000673llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
674 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000675 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000676 Ty->getPointeeType(), Unit);
677}
678
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000679/// \return whether a C++ mangling exists for the type defined by TD.
680static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
681 switch (TheCU->getSourceLanguage()) {
682 case llvm::dwarf::DW_LANG_C_plus_plus:
683 return true;
684 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
685 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
686 default:
687 return false;
688 }
689}
690
Manman Rene0064d82013-08-29 23:19:58 +0000691/// In C++ mode, types have linkage, so we can rely on the ODR and
692/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000693static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
694 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000696 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000697 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000698
699 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000700 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000701
Manman Rene0064d82013-08-29 23:19:58 +0000702 // TODO: This is using the RTTI name. Is there a better way to get
703 // a unique string for a type?
704 llvm::raw_svector_ostream Out(FullName);
705 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000706 return FullName;
707}
708
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000709/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000710static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
711 llvm::dwarf::Tag Tag;
712 if (RD->isStruct() || RD->isInterface())
713 Tag = llvm::dwarf::DW_TAG_structure_type;
714 else if (RD->isUnion())
715 Tag = llvm::dwarf::DW_TAG_union_type;
716 else {
717 // FIXME: This could be a struct type giving a default visibility different
718 // than C++ class type, but needs llvm metadata changes first.
719 assert(RD->isClass());
720 Tag = llvm::dwarf::DW_TAG_class_type;
721 }
722 return Tag;
723}
724
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000725llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000726CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000727 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000728 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000729 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
730 return cast<llvm::DICompositeType>(T);
731 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 unsigned Line = getLineNumber(RD->getLocation());
733 StringRef RDName = getClassName(RD);
734
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000735 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000736 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000737
738 const RecordDecl *D = RD->getDefinition();
739 if (D && D->isCompleteDefinition()) {
740 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000741 Align = getDeclAlignIfRequired(D, CGM.getContext());
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000742 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000743
744 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000745 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000746 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000747 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000748 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000749 ReplaceMap.emplace_back(
750 std::piecewise_construct, std::make_tuple(Ty),
751 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000752 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000753}
754
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000755llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000756 const Type *Ty,
757 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000758 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000759 // Bit size, align and offset of the type.
760 // Size is always the size of a pointer. We can't use getTypeSize here
761 // because that does not return the correct value for references.
762 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000763 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +0000764 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +0000765
Keno Fischer87842f32015-11-16 09:04:13 +0000766 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
767 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
768 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
769 Size, Align);
770 else
771 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
772 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000773}
774
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000775llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
776 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000777 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000778 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000779 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
780 TheCU, getOrCreateMainFile(), 0);
781 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
782 Cache = DBuilder.createPointerType(Cache, Size);
783 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000784}
785
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000786llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
787 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000788 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000789 QualType FType;
790 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000791 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000792 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000793
794 FieldOffset = 0;
795 FType = CGM.getContext().UnsignedLongTy;
796 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
797 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
798
799 Elements = DBuilder.getOrCreateArray(EltTys);
800 EltTys.clear();
801
Leny Kholodov80c047d2016-09-06 10:48:04 +0000802 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000803 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000804
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000805 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000806 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000807 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000808
809 // Bit size, align and offset of the type.
810 uint64_t Size = CGM.getContext().getTypeSize(Ty);
811
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000812 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000813
814 FieldOffset = 0;
815 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
816 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
817 FType = CGM.getContext().IntTy;
818 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
819 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000820 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000821 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
822
823 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 FieldSize = CGM.getContext().getTypeSize(Ty);
825 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000826 EltTys.push_back(DBuilder.createMemberType(
827 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
828 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000829
830 FieldOffset += FieldSize;
831 Elements = DBuilder.getOrCreateArray(EltTys);
832
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000833 // The __block_literal_generic structs are marked with a special
834 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
835 // the debugger needs to know about. To allow type uniquing, emit
836 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000837 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000838 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000839 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000840
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000841 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000842}
843
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000844llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
845 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000846 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000847 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000848
849 SmallString<128> NS;
850 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000851 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
852 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000853
854 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +0000855 OS, Ty->template_arguments(),
David Blaikief1b382e2014-04-06 17:14:06 +0000856 CGM.getContext().getPrintingPolicy());
857
David Majnemer58ed0f32016-07-17 00:39:12 +0000858 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000859 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000860
861 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000862 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
863 getLineNumber(Loc),
864 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000865}
866
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000867llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
868 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000869 // We don't set size information, but do specify where the typedef was
870 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000871 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000872
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000873 // Typedefs are derived from some other type.
874 return DBuilder.createTypedef(
875 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
876 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000877 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000878}
879
Reid Klecknerf00f8032016-06-08 20:41:54 +0000880static unsigned getDwarfCC(CallingConv CC) {
881 switch (CC) {
882 case CC_C:
883 // Avoid emitting DW_AT_calling_convention if the C convention was used.
884 return 0;
885
886 case CC_X86StdCall:
887 return llvm::dwarf::DW_CC_BORLAND_stdcall;
888 case CC_X86FastCall:
889 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
890 case CC_X86ThisCall:
891 return llvm::dwarf::DW_CC_BORLAND_thiscall;
892 case CC_X86VectorCall:
893 return llvm::dwarf::DW_CC_LLVM_vectorcall;
894 case CC_X86Pascal:
895 return llvm::dwarf::DW_CC_BORLAND_pascal;
896
897 // FIXME: Create new DW_CC_ codes for these calling conventions.
898 case CC_X86_64Win64:
899 case CC_X86_64SysV:
900 case CC_AAPCS:
901 case CC_AAPCS_VFP:
902 case CC_IntelOclBicc:
903 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000904 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000905 case CC_Swift:
906 case CC_PreserveMost:
907 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000908 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000909 return 0;
910 }
911 return 0;
912}
913
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000914llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
915 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000916 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000917
918 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000919 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000920
921 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000922 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000923 if (isa<FunctionNoProtoType>(Ty))
924 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +0000925 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
926 for (const QualType &ParamType : FPT->param_types())
927 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000928 if (FPT->isVariadic())
929 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000930 }
931
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000932 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +0000933 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +0000934 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000935}
936
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000937/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000938/// As an optimization, return 0 if the access specifier equals the
939/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000940static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
941 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +0000942 AccessSpecifier Default = clang::AS_none;
943 if (RD && RD->isClass())
944 Default = clang::AS_private;
945 else if (RD && (RD->isStruct() || RD->isUnion()))
946 Default = clang::AS_public;
947
948 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +0000949 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000950
Eric Christophere7b87e52014-10-26 23:40:33 +0000951 switch (Access) {
952 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000953 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000954 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000955 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000956 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000957 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000958 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +0000959 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000960 }
961 llvm_unreachable("unexpected access enumerator");
962}
Guy Benyei11169dd2012-12-18 14:30:41 +0000963
David Majnemerb4b671e2016-06-30 03:01:59 +0000964llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
965 llvm::DIScope *RecordTy,
966 const RecordDecl *RD) {
967 StringRef Name = BitFieldDecl->getName();
968 QualType Ty = BitFieldDecl->getType();
969 SourceLocation Loc = BitFieldDecl->getLocation();
970 llvm::DIFile *VUnit = getOrCreateFile(Loc);
971 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
972
973 // Get the location for the field.
974 llvm::DIFile *File = getOrCreateFile(Loc);
975 unsigned Line = getLineNumber(Loc);
976
977 const CGBitFieldInfo &BitFieldInfo =
978 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
979 uint64_t SizeInBits = BitFieldInfo.Size;
980 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +0000981 uint64_t StorageOffsetInBits =
982 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
983 uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +0000984 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +0000985 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +0000986 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
987 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +0000988}
989
990llvm::DIType *
991CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
992 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +0000993 uint32_t AlignInBits, llvm::DIFile *tunit,
994 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000995 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000996
997 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000998 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000999 unsigned line = getLineNumber(loc);
1000
David Majnemer34b57492014-07-30 01:30:47 +00001001 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001002 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001003 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001004 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1005 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001006 if (!Align)
1007 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001008 }
1009
Leny Kholodov80c047d2016-09-06 10:48:04 +00001010 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001011 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001012 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001013}
1014
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001015void CGDebugInfo::CollectRecordLambdaFields(
1016 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001017 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001018 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1019 // has the name and the location of the variable so we should iterate over
1020 // both concurrently.
1021 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1022 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1023 unsigned fieldno = 0;
1024 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001025 E = CXXDecl->captures_end();
1026 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001027 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001028 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001029 SourceLocation Loc = C.getLocation();
1030 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001031 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001032 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001033 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001034 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001035 llvm::DIType *FieldType = createFieldType(
1036 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001037 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001038 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001039 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001040 // TODO: Need to handle 'this' in some way by probably renaming the
1041 // this of the lambda class and having a field member of 'this' or
1042 // by using AT_object_pointer for the function and having that be
1043 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001044 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001045 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001046 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001047 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001048 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001049 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001050
1051 elements.push_back(fieldType);
1052 }
1053 }
1054}
1055
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056llvm::DIDerivedType *
1057CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001058 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001059 // Create the descriptor for the static variable, with or without
1060 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001061 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001062 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1063 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001064
Eric Christopher91a31902013-01-16 01:22:32 +00001065 unsigned LineNumber = getLineNumber(Var->getLocation());
1066 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001067 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001068 if (Var->getInit()) {
1069 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001070 if (Value) {
1071 if (Value->isInt())
1072 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1073 if (Value->isFloat())
1074 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1075 }
Eric Christopher91a31902013-01-16 01:22:32 +00001076 }
1077
Leny Kholodov80c047d2016-09-06 10:48:04 +00001078 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001079 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001080 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001081 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001082 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001083 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001084}
1085
Eric Christophere7b87e52014-10-26 23:40:33 +00001086void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001087 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1088 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001089 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001090 StringRef name = field->getName();
1091 QualType type = field->getType();
1092
1093 // Ignore unnamed fields unless they're anonymous structs/unions.
1094 if (name.empty() && !type->isRecordType())
1095 return;
1096
David Majnemerb4b671e2016-06-30 03:01:59 +00001097 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001098 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001099 FieldType = createBitFieldType(field, RecordTy, RD);
1100 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001101 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001102 FieldType =
1103 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001104 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001105 }
1106
David Majnemerb4b671e2016-06-30 03:01:59 +00001107 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001108}
1109
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001110void CGDebugInfo::CollectRecordNestedRecord(
1111 const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) {
1112 QualType Ty = CGM.getContext().getTypeDeclType(RD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001113 // Injected class names are not considered nested records.
1114 if (isa<InjectedClassNameType>(Ty))
1115 return;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001116 SourceLocation Loc = RD->getLocation();
1117 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1118 elements.push_back(nestedType);
1119}
1120
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001121void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001123 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001124 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001125 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
Eric Christopher91a31902013-01-16 01:22:32 +00001127 if (CXXDecl && CXXDecl->isLambda())
1128 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1129 else {
1130 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001131
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001132 // Debug info for nested records is included in the member list only for
1133 // CodeView.
1134 bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView;
1135
Eric Christopher91a31902013-01-16 01:22:32 +00001136 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001137 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001138
Eric Christopher91a31902013-01-16 01:22:32 +00001139 // Static and non-static members should appear in the same order as
1140 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001141 for (const auto *I : record->decls())
1142 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001143 if (V->hasAttr<NoDebugAttr>())
1144 continue;
David Blaikiece763042013-08-20 21:49:21 +00001145 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001146 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001147 if (MI != StaticDataMemberCache.end()) {
1148 assert(MI->second &&
1149 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001150 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001151 } else {
1152 auto Field = CreateRecordStaticField(V, RecordTy, record);
1153 elements.push_back(Field);
1154 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001155 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001156 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1157 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001158
1159 // Bump field number for next field.
1160 ++fieldNo;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001161 } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I))
1162 if (IncludeNestedRecords && !nestedRec->isImplicit() &&
1163 nestedRec->getDeclContext() == record)
1164 CollectRecordNestedRecord(nestedRec, elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 }
1166}
1167
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001168llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001169CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001170 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001171 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001172 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001173 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001174 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001175 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1176 Func, Unit);
1177}
David Blaikie2aaf0652013-01-07 22:24:59 +00001178
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001179llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1180 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001181 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001182 llvm::DITypeRefArray Args(
1183 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001184 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001185 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001186
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001187 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001188
1189 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001190 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001191
David Blaikie2aaf0652013-01-07 22:24:59 +00001192 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001193 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001194 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1195 // Create pointer type directly in this case.
1196 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1197 QualType PointeeTy = ThisPtrTy->getPointeeType();
1198 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001199 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001200 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001201 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1202 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001203 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001204 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001205 // TODO: This and the artificial type below are misleading, the
1206 // types aren't artificial the argument is, but the current
1207 // metadata doesn't represent that.
1208 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1209 Elts.push_back(ThisPtrType);
1210 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001211 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001212 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001213 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1214 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 }
1216
1217 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001218 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1219 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001220
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001221 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001222
Leny Kholodov80c047d2016-09-06 10:48:04 +00001223 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001224 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001225 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001226 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001227 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001228
Reid Klecknerf00f8032016-06-08 20:41:54 +00001229 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1230 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001231}
1232
Eric Christopherb2a008c2013-05-16 00:45:12 +00001233/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001234/// inside a function.
1235static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001236 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 return isFunctionLocalClass(NRD);
1238 if (isa<FunctionDecl>(RD->getDeclContext()))
1239 return true;
1240 return false;
1241}
1242
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001243llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1244 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001245 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001246 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001247
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001249 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001250
1251 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1252 // make sense to give a single ctor/dtor a linkage name.
1253 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001254 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1255 // property to use here. It may've been intended to model "is non-external
1256 // type" but misses cases of non-function-local but non-external classes such
1257 // as those in anonymous namespaces as well as the reverse - external types
1258 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001259 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1260 MethodLinkageName = CGM.getMangledName(Method);
1261
1262 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001263 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001264 unsigned MethodLine = 0;
1265 if (!Method->isImplicit()) {
1266 MethodDefUnit = getOrCreateFile(Method->getLocation());
1267 MethodLine = getLineNumber(Method->getLocation());
1268 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001269
1270 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001272 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001273 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001274 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001275 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001276
Guy Benyei11169dd2012-12-18 14:30:41 +00001277 if (Method->isVirtual()) {
1278 if (Method->isPure())
1279 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1280 else
1281 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001282
Reid Kleckner216d0a12016-06-16 20:08:51 +00001283 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1284 // It doesn't make sense to give a virtual destructor a vtable index,
1285 // since a single destructor has two entries in the vtable.
1286 if (!isa<CXXDestructorDecl>(Method))
1287 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1288 } else {
1289 // Emit MS ABI vftable information. There is only one entry for the
1290 // deleting dtor.
1291 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1292 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1293 MicrosoftVTableContext::MethodVFTableLocation ML =
1294 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1295 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001296
1297 // CodeView only records the vftable offset in the class that introduces
1298 // the virtual method. This is possible because, unlike Itanium, the MS
1299 // C++ ABI does not include all virtual methods from non-primary bases in
1300 // the vtable for the most derived class. For example, if C inherits from
1301 // A and B, C's primary vftable will not include B's virtual methods.
1302 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1303 Flags |= llvm::DINode::FlagIntroducedVirtual;
1304
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001305 // The 'this' adjustment accounts for both the virtual and non-virtual
1306 // portions of the adjustment. Presumably the debugger only uses it when
1307 // it knows the dynamic type of an object.
1308 ThisAdjustment = CGM.getCXXABI()
1309 .getVirtualFunctionPrologueThisAdjustment(GD)
1310 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001311 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001312 ContainingType = RecordTy;
1313 }
1314
Guy Benyei11169dd2012-12-18 14:30:41 +00001315 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001316 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001317 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001318 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001319 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001320 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001321 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001323 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001324 }
1325 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001326 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001327 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001329 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001330 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001331
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001332 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1333 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001334 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001335 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1336 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1337 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001338
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001339 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
1341 return SP;
1342}
1343
Eric Christophere7b87e52014-10-26 23:40:33 +00001344void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001345 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1346 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001347
1348 // Since we want more than just the individual member decls if we
1349 // have templated functions iterate over every declaration to gather
1350 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001351 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001352 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1353 // If the member is implicit, don't add it to the member list. This avoids
1354 // the member being added to type units by LLVM, while still allowing it
1355 // to be emitted into the type declaration/reference inside the compile
1356 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001357 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001358 // FIXME: Handle Using(Shadow?)Decls here to create
1359 // DW_TAG_imported_declarations inside the class for base decls brought into
1360 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1361 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1362 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001363 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001364 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001365
1366 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1367 continue;
1368
David Blaikiefd580722014-10-06 05:18:55 +00001369 // Reuse the existing member function declaration if it exists.
1370 // It may be associated with the declaration of the type & should be
1371 // reused as we're building the definition.
1372 //
1373 // This situation can arise in the vtable-based debug info reduction where
1374 // implicit members are emitted in a non-vtable TU.
1375 auto MI = SPCache.find(Method->getCanonicalDecl());
1376 EltTys.push_back(MI == SPCache.end()
1377 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001378 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001380}
Guy Benyei11169dd2012-12-18 14:30:41 +00001381
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001382void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001383 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001384 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001385 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1386 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1387 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001388
Bob Haarmandff36732016-10-25 22:19:32 +00001389 // If we are generating CodeView debug info, we also need to emit records for
1390 // indirect virtual base classes.
1391 if (CGM.getCodeGenOpts().EmitCodeView) {
1392 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1393 llvm::DINode::FlagIndirectVirtualBase);
1394 }
1395}
1396
1397void CGDebugInfo::CollectCXXBasesAux(
1398 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1399 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1400 const CXXRecordDecl::base_class_const_range &Bases,
1401 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1402 llvm::DINode::DIFlags StartingFlags) {
1403 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1404 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001405 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001406 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001407 if (!SeenTypes.insert(Base).second)
1408 continue;
1409 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1410 llvm::DINode::DIFlags BFlags = StartingFlags;
1411 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001412
Aaron Ballman574705e2014-03-13 15:41:46 +00001413 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001414 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1415 // virtual base offset offset is -ve. The code generator emits dwarf
1416 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001417 BaseOffset = 0 - CGM.getItaniumVTableContext()
1418 .getVirtualBaseOffsetOffset(RD, Base)
1419 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001420 } else {
1421 // In the MS ABI, store the vbtable offset, which is analogous to the
1422 // vbase offset offset in Itanium.
1423 BaseOffset =
1424 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1425 }
Bob Haarmandff36732016-10-25 22:19:32 +00001426 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 } else
1428 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1429 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1430 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001431
Adrian Prantl21361fb2014-08-29 22:44:27 +00001432 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001433 llvm::DIType *DTy =
1434 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001435 EltTys.push_back(DTy);
1436 }
1437}
1438
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001439llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001440CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1441 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001442 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001443 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1445 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001446 StringRef Name;
1447 if (TPList)
1448 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001449 switch (TA.getKind()) {
1450 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001451 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001452 TemplateParams.push_back(
1453 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001454 } break;
1455 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001456 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001457 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1458 TheCU, Name, TTy,
1459 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001460 } break;
1461 case TemplateArgument::Declaration: {
1462 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001463 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001464 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001465 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001466 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001467 // Variable pointer template parameters have a value that is the address
1468 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001469 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001470 V = CGM.GetAddrOfGlobalVar(VD);
1471 // Member function pointers have special support for building them, though
1472 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001473 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001474 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001475 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001476 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001477 // Member data pointers have special handling too to compute the fixed
1478 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001479 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001480 // These five lines (& possibly the above member function pointer
1481 // handling) might be able to be refactored to use similar code in
1482 // CodeGenModule::getMemberPointerConstant
1483 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1484 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001485 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001486 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001487 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001488 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1489 TheCU, Name, TTy,
1490 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001491 } break;
1492 case TemplateArgument::NullPtr: {
1493 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001494 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001495 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001496 // Special case member data pointer null values since they're actually -1
1497 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001498 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001499 // But treat member function pointers as simple zero integers because
1500 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1501 // CodeGen grows handling for values of non-null member function
1502 // pointers then perhaps we could remove this special case and rely on
1503 // EmitNullMemberPointer for member function pointers.
1504 if (MPT->isMemberDataPointer())
1505 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1506 if (!V)
1507 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001508 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001509 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001510 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001511 case TemplateArgument::Template:
1512 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001513 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001514 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1515 break;
1516 case TemplateArgument::Pack:
1517 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1518 TheCU, Name, nullptr,
1519 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1520 break;
David Majnemer5559d472013-08-24 08:21:10 +00001521 case TemplateArgument::Expression: {
1522 const Expr *E = TA.getAsExpr();
1523 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001524 if (E->isGLValue())
1525 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001526 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001527 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001528 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001529 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001530 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001531 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001532 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001533 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001534 case TemplateArgument::Null:
1535 llvm_unreachable(
1536 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001537 }
1538 }
1539 return DBuilder.getOrCreateArray(TemplateParams);
1540}
1541
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001542llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001543CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 if (FD->getTemplatedKind() ==
1546 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001547 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1548 ->getTemplate()
1549 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001550 return CollectTemplateParams(
1551 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001552 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001553 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001554}
1555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001556llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1557 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001558 // Always get the full list of parameters, not just the ones from
1559 // the specialization.
1560 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001561 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001562 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001563 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001564}
1565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001566llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001567 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001568 return VTablePtrType;
1569
1570 ASTContext &Context = CGM.getContext();
1571
1572 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001573 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001574 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001575 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001578 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1580 return VTablePtrType;
1581}
1582
Guy Benyei11169dd2012-12-18 14:30:41 +00001583StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001584 // Copy the gdb compatible name on the side and use its reference.
1585 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001586}
1587
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001588void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001589 SmallVectorImpl<llvm::Metadata *> &EltTys,
1590 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001591 // If this class is not dynamic then there is not any vtable info to collect.
1592 if (!RD->isDynamicClass())
1593 return;
1594
Reid Kleckner59812422016-08-31 20:35:01 +00001595 // Don't emit any vtable shape or vptr info if this class doesn't have an
1596 // extendable vfptr. This can happen if the class doesn't have virtual
1597 // methods, or in the MS ABI if those virtual methods only come from virtually
1598 // inherited bases.
1599 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1600 if (!RL.hasExtendableVFPtr())
1601 return;
1602
Reid Klecknerdc124992016-08-31 16:11:43 +00001603 // CodeView needs to know how large the vtable of every dynamic class is, so
1604 // emit a special named pointer type into the element list. The vptr type
1605 // points to this type as well.
1606 llvm::DIType *VPtrTy = nullptr;
1607 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1608 CGM.getTarget().getCXXABI().isMicrosoft();
1609 if (NeedVTableShape) {
1610 uint64_t PtrWidth =
1611 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1612 const VTableLayout &VFTLayout =
1613 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1614 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001615 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001616 unsigned VTableWidth = PtrWidth * VSlotCount;
1617
1618 // Create a very wide void* type and insert it directly in the element list.
1619 llvm::DIType *VTableType =
1620 DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type");
1621 EltTys.push_back(VTableType);
1622
1623 // The vptr is a pointer to this special vtable type.
1624 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1625 }
1626
1627 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001628 if (RL.getPrimaryBase())
1629 return;
1630
1631 if (!VPtrTy)
1632 VPtrTy = getOrCreateVTablePtrType(Unit);
1633
Guy Benyei11169dd2012-12-18 14:30:41 +00001634 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001635 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001636 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001637 llvm::DINode::FlagArtificial, VPtrTy);
1638 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001639}
1640
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001641llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001642 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001643 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001644 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 return T;
1646}
1647
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001648llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001649 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001650 return getOrCreateStandaloneType(D, Loc);
1651}
1652
1653llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1654 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001655 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001656 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001657 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001658 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001659
Adrian Prantl73409ce2013-03-11 18:33:46 +00001660 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001661 return T;
1662}
1663
David Blaikie483a9da2014-05-06 18:35:21 +00001664void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001665 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001666 return;
1667 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001668 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001669 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001670 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001671 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001672 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001673 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001674 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001675}
1676
David Blaikieb2e86eb2013-08-15 20:49:17 +00001677void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001678 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001679 !CGM.getLangOpts().CPlusPlus)
1680 completeRequiredType(RD);
1681}
1682
David Blaikie6943dea2013-08-20 01:28:15 +00001683void CGDebugInfo::completeClassData(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001684 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001685 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001686 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001687 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001688 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001689 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001690 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001691 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001692 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001693 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001694}
1695
David Blaikie0e716b42014-03-03 23:48:23 +00001696static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1697 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001698 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1699 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001700 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001701 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001702 return true;
1703 return false;
1704}
1705
Adrian Prantl05fefa42016-04-25 20:52:40 +00001706/// Does a type definition exist in an imported clang module?
1707static bool isDefinedInClangModule(const RecordDecl *RD) {
Adrian Prantl09906a62016-08-22 22:38:16 +00001708 // Only definitions that where imported from an AST file come from a module.
Adrian Prantl88d79172016-04-26 21:58:18 +00001709 if (!RD || !RD->isFromASTFile())
Adrian Prantl05fefa42016-04-25 20:52:40 +00001710 return false;
Adrian Prantl09906a62016-08-22 22:38:16 +00001711 // Anonymous entities cannot be addressed. Treat them as not from module.
Adrian Prantl05fefa42016-04-25 20:52:40 +00001712 if (!RD->isExternallyVisible() && RD->getName().empty())
1713 return false;
Adrian Prantl94913712016-04-26 23:42:43 +00001714 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
Adrian Prantla72972b2016-08-22 22:23:58 +00001715 if (!CXXDecl->isCompleteDefinition())
1716 return false;
Adrian Prantl26cb1d22016-08-17 18:27:24 +00001717 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1718 if (TemplateKind != TSK_Undeclared) {
1719 // This is a template, check the origin of the first member.
1720 if (CXXDecl->field_begin() == CXXDecl->field_end())
1721 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1722 if (!CXXDecl->field_begin()->isFromASTFile())
1723 return false;
1724 }
Adrian Prantl94913712016-04-26 23:42:43 +00001725 }
Adrian Prantl05fefa42016-04-25 20:52:40 +00001726 return true;
1727}
1728
Reid Klecknerc9404e12016-09-09 16:27:04 +00001729/// Return true if the class or any of its methods are marked dllimport.
1730static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1731 if (RD->hasAttr<DLLImportAttr>())
1732 return true;
1733 for (const CXXMethodDecl *MD : RD->methods())
1734 if (MD->hasAttr<DLLImportAttr>())
1735 return true;
1736 return false;
1737}
1738
Benjamin Kramer8c305922016-02-02 11:06:51 +00001739static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1740 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001741 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001742 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001743 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001744
Benjamin Kramer8c305922016-02-02 11:06:51 +00001745 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001746 return false;
1747
1748 if (!LangOpts.CPlusPlus)
1749 return false;
1750
1751 if (!RD->isCompleteDefinitionRequired())
1752 return true;
1753
David Majnemer58ed0f32016-07-17 00:39:12 +00001754 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001755
1756 if (!CXXDecl)
1757 return false;
1758
Adrian McCarthy99242982016-08-16 22:11:18 +00001759 // Only emit complete debug info for a dynamic class when its vtable is
1760 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001761 // across DLL boundaries, so skip this optimization if the class or any of its
1762 // methods are marked dllimport. This isn't a complete solution, since objects
1763 // without any dllimport methods can be used in one DLL and constructed in
1764 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001765 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001766 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001767 return true;
1768
1769 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001770 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001771 Spec = SD->getSpecializationKind();
1772
1773 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1774 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1775 CXXDecl->method_end()))
1776 return true;
1777
1778 return false;
1779}
1780
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001781void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1782 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1783 return;
1784
1785 QualType Ty = CGM.getContext().getRecordType(RD);
1786 llvm::DIType *T = getTypeOrNull(Ty);
1787 if (T && T->isForwardDecl())
1788 completeClassData(RD);
1789}
1790
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001791llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001793 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001794 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1795 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001796 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001797 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001798 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001799 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001800
David Blaikieb2e86eb2013-08-15 20:49:17 +00001801 return CreateTypeDefinition(Ty);
1802}
1803
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001804llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001805 RecordDecl *RD = Ty->getDecl();
1806
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001808 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001809
1810 // Records and classes and unions can all be recursive. To handle them, we
1811 // first generate a debug descriptor for the struct as a forward declaration.
1812 // Then (if it is a definition) we go through and get debug info for all of
1813 // its members. Finally, we create a descriptor for the complete type (which
1814 // may refer to the forward decl if the struct is recursive) and replace all
1815 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001816 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001817
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001818 const RecordDecl *D = RD->getDefinition();
1819 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001820 return FwdDecl;
1821
David Majnemer58ed0f32016-07-17 00:39:12 +00001822 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001823 CollectContainingType(CXXDecl, FwdDecl);
1824
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001826 LexicalBlockStack.emplace_back(&*FwdDecl);
1827 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001828
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001830 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001831 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001832
1833 // Note: The split of CXXDecl information here is intentional, the
1834 // gdb tests will depend on a certain ordering at printout. The debug
1835 // information offsets are still correct if we merge them all together
1836 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001837 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001838 if (CXXDecl) {
1839 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001840 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 }
1842
Eric Christopher91a31902013-01-16 01:22:32 +00001843 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001844 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001845 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001846 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001847
1848 LexicalBlockStack.pop_back();
1849 RegionMap.erase(Ty->getDecl());
1850
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001851 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001852 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001853
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001854 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001855 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001856 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001857
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001858 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001859 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001860}
1861
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001862llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1863 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 // Ignore protocols.
1865 return getOrCreateType(Ty->getBaseType(), Unit);
1866}
1867
Manman Rene6be26c2016-09-13 17:25:08 +00001868llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1869 llvm::DIFile *Unit) {
1870 // Ignore protocols.
1871 SourceLocation Loc = Ty->getDecl()->getLocation();
1872
1873 // Use Typedefs to represent ObjCTypeParamType.
1874 return DBuilder.createTypedef(
1875 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1876 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1877 getDeclContextDescriptor(Ty->getDecl()));
1878}
1879
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001880/// \return true if Getter has the default name for the property PD.
1881static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1882 const ObjCMethodDecl *Getter) {
1883 assert(PD);
1884 if (!Getter)
1885 return true;
1886
1887 assert(Getter->getDeclName().isObjCZeroArgSelector());
1888 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001889 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001890}
1891
1892/// \return true if Setter has the default name for the property PD.
1893static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1894 const ObjCMethodDecl *Setter) {
1895 assert(PD);
1896 if (!Setter)
1897 return true;
1898
1899 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001900 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001901 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001902}
1903
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001904llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1905 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 ObjCInterfaceDecl *ID = Ty->getDecl();
1907 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001908 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001909
Adrian Prantl50fd1a82016-04-20 23:59:32 +00001910 // Return a forward declaration if this type was imported from a clang module,
1911 // and this is not the compile unit with the implementation of the type (which
1912 // may contain hidden ivars).
1913 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
1914 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001915 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1916 ID->getName(),
1917 getDeclContextDescriptor(ID), Unit, 0);
1918
Guy Benyei11169dd2012-12-18 14:30:41 +00001919 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001920 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001922 auto RuntimeLang =
1923 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001924
1925 // If this is just a forward declaration return a special forward-declaration
1926 // debug type since we won't be able to lay out the entire type.
1927 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001928 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001929 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001931 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1932 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001933 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 return FwdDecl;
1935 }
1936
David Blaikieef8a9512014-05-05 23:23:53 +00001937 return CreateTypeDefinition(Ty, Unit);
1938}
1939
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001940llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001941CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1942 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001943 // Use the Module pointer as the key into the cache. This is a
1944 // nullptr if the "Module" is a PCH, which is safe because we don't
1945 // support chained PCH debug info, so there can only be a single PCH.
1946 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001947 auto ModRef = ModuleCache.find(M);
1948 if (ModRef != ModuleCache.end())
1949 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001950
1951 // Macro definitions that were defined with "-D" on the command line.
1952 SmallString<128> ConfigMacros;
1953 {
1954 llvm::raw_svector_ostream OS(ConfigMacros);
1955 const auto &PPOpts = CGM.getPreprocessorOpts();
1956 unsigned I = 0;
1957 // Translate the macro definitions back into a commmand line.
1958 for (auto &M : PPOpts.Macros) {
1959 if (++I > 1)
1960 OS << " ";
1961 const std::string &Macro = M.first;
1962 bool Undef = M.second;
1963 OS << "\"-" << (Undef ? 'U' : 'D');
1964 for (char c : Macro)
1965 switch (c) {
1966 case '\\' : OS << "\\\\"; break;
1967 case '"' : OS << "\\\""; break;
1968 default: OS << c;
1969 }
1970 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001971 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001972 }
Adrian Prantl66689202015-09-18 23:01:45 +00001973
Adrian Prantl835e6632015-09-24 16:10:10 +00001974 bool IsRootModule = M ? !M->Parent : true;
1975 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00001976 // PCH files don't have a signature field in the control block,
1977 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Adrian Prantl98bfc822016-01-22 19:29:41 +00001978 uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00001979 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001980 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001981 Mod.getPath(), TheCU->getProducer(), true,
1982 StringRef(), 0, Mod.getASTFile(),
Adrian Prantl3563c552016-03-31 23:57:45 +00001983 llvm::DICompileUnit::FullDebug, Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001984 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001985 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001986 llvm::DIModule *Parent =
1987 IsRootModule ? nullptr
1988 : getOrCreateModuleRef(
1989 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1990 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001991 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001992 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1993 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001994 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001995 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001996}
1997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001998llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1999 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002000 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002001 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002002 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002003 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002004
2005 // Bit size, align and offset of the type.
2006 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002007 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002008
Leny Kholodov80c047d2016-09-06 10:48:04 +00002009 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002010 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002011 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002012
Adrian Prantlfd696112015-10-01 00:48:51 +00002013 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002014 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002015 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2016 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002017
David Blaikieef8a9512014-05-05 23:23:53 +00002018 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002019 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002020
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002021 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002022 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002023 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002024
2025 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002026 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002027
2028 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2029 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002030 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002031 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002032 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002033 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002034
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002035 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2036 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002037 EltTys.push_back(InhTag);
2038 }
2039
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002040 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002041 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002043 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 unsigned PLine = getLineNumber(Loc);
2045 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2046 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002047 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2048 PD->getName(), PUnit, PLine,
2049 hasDefaultGetterName(PD, Getter) ? ""
2050 : getSelectorName(PD->getGetterName()),
2051 hasDefaultSetterName(PD, Setter) ? ""
2052 : getSelectorName(PD->getSetterName()),
2053 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002055 };
2056 {
2057 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002058 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002059 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002060 PropertySet.insert(PD->getIdentifier());
2061 AddProperty(PD);
2062 }
Manman Renefe1bac2016-01-27 20:00:32 +00002063 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002064 // Don't emit duplicate metadata for properties that were already in a
2065 // class extension.
2066 if (!PropertySet.insert(PD->getIdentifier()).second)
2067 continue;
2068 AddProperty(PD);
2069 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 }
2071
2072 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2073 unsigned FieldNo = 0;
2074 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2075 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002076 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002077 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002078 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002079
Guy Benyei11169dd2012-12-18 14:30:41 +00002080 StringRef FieldName = Field->getName();
2081
2082 // Ignore unnamed fields.
2083 if (FieldName.empty())
2084 continue;
2085
2086 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002087 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 unsigned FieldLine = getLineNumber(Field->getLocation());
2089 QualType FType = Field->getType();
2090 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002091 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002092
2093 if (!FType->isIncompleteArrayType()) {
2094
2095 // Bit size, align and offset of the type.
2096 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002097 ? Field->getBitWidthValue(CGM.getContext())
2098 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002099 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002100 }
2101
2102 uint64_t FieldOffset;
2103 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2104 // We don't know the runtime offset of an ivar if we're using the
2105 // non-fragile ABI. For bitfields, use the bit offset into the first
2106 // byte of storage of the bitfield. For other fields, use zero.
2107 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002108 FieldOffset =
2109 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 FieldOffset %= CGM.getContext().getCharWidth();
2111 } else {
2112 FieldOffset = 0;
2113 }
2114 } else {
2115 FieldOffset = RL.getFieldOffset(FieldNo);
2116 }
2117
Leny Kholodov80c047d2016-09-06 10:48:04 +00002118 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002119 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002120 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002122 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002123 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002124 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002125
Craig Topper8a13c412014-05-21 05:09:00 +00002126 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002127 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002128 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002129 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002130 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002131 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002132 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002133 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002134 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2135 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002136 PropertyNode = DBuilder.createObjCProperty(
2137 PD->getName(), PUnit, PLine,
2138 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2139 PD->getGetterName()),
2140 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2141 PD->getSetterName()),
2142 PD->getPropertyAttributes(),
2143 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 }
2145 }
2146 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002147 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2148 FieldSize, FieldAlign, FieldOffset, Flags,
2149 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002150 EltTys.push_back(FieldTy);
2151 }
2152
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002153 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002154 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002155
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002157 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002158}
2159
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002160llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2161 llvm::DIFile *Unit) {
2162 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 int64_t Count = Ty->getNumElements();
2164 if (Count == 0)
2165 // If number of elements are not known then this is an unbounded array.
2166 // Use Count == -1 to express such arrays.
2167 Count = -1;
2168
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002169 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002170 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
2172 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002173 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002174
2175 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2176}
2177
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002180 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002181
2182 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002183 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002185 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2186 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002187 } else if (Ty->isIncompleteArrayType()) {
2188 Size = 0;
2189 if (Ty->getElementType()->isIncompleteType())
2190 Align = 0;
2191 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002192 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002193 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 Size = 0;
2195 Align = 0;
2196 } else {
2197 // Size and align of the whole array, not the element type.
2198 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002199 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 }
2201
2202 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2203 // interior arrays, do we care? Why aren't nested arrays represented the
2204 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002205 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 QualType EltTy(Ty, 0);
2207 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2208 // If the number of elements is known, then count is that number. Otherwise,
2209 // it's -1. This allows us to represent a subrange with an array of 0
2210 // elements, like this:
2211 //
2212 // struct foo {
2213 // int x[0];
2214 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002215 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002216 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002218 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002219 if (Expr *Size = VAT->getSizeExpr()) {
2220 llvm::APSInt V;
2221 if (Size->EvaluateAsInt(V, CGM.getContext()))
2222 Count = V.getExtValue();
2223 }
David Blaikie87173f12016-08-22 17:49:56 +00002224 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002225
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 // FIXME: Verify this is right for VLAs.
2227 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2228 EltTy = Ty->getElementType();
2229 }
2230
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002231 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002232
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002233 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2234 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002235}
2236
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002237llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2238 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002239 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2240 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002241}
2242
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002243llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2244 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002245 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2246 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002247}
2248
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002249llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2250 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002251 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002252 uint64_t Size = 0;
2253
2254 if (!Ty->isIncompleteType()) {
2255 Size = CGM.getContext().getTypeSize(Ty);
2256
2257 // Set the MS inheritance model. There is no flag for the unspecified model.
2258 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2259 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2260 case MSInheritanceAttr::Keyword_single_inheritance:
2261 Flags |= llvm::DINode::FlagSingleInheritance;
2262 break;
2263 case MSInheritanceAttr::Keyword_multiple_inheritance:
2264 Flags |= llvm::DINode::FlagMultipleInheritance;
2265 break;
2266 case MSInheritanceAttr::Keyword_virtual_inheritance:
2267 Flags |= llvm::DINode::FlagVirtualInheritance;
2268 break;
2269 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2270 break;
2271 }
2272 }
2273 }
2274
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002275 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002276 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002277 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002278 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2279 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002280
2281 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002282 Ty->getPointeeType()->getAs<FunctionProtoType>();
2283 return DBuilder.createMemberPointerType(
2284 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2285 Ty->getClass(), FPT->getTypeQuals())),
2286 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002287 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002288}
2289
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002290llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002291 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2292 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002293}
2294
Xiuli Pan9c14e282016-01-09 12:53:17 +00002295llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2296 llvm::DIFile *U) {
2297 return getOrCreateType(Ty->getElementType(), U);
2298}
2299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002301 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002302
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002304 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 if (!ED->getTypeForDecl()->isIncompleteType()) {
2306 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002307 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 }
2309
Manman Rene0064d82013-08-29 23:19:58 +00002310 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2311
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002312 bool isImportedFromModule =
2313 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2314
Guy Benyei11169dd2012-12-18 14:30:41 +00002315 // If this is just a forward declaration, construct an appropriately
2316 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002317 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002318 // Note that it is possible for enums to be created as part of
2319 // their own declcontext. In this case a FwdDecl will be created
2320 // twice. This doesn't cause a problem because both FwdDecls are
2321 // entered into the ReplaceMap: finalize() will replace the first
2322 // FwdDecl with the second and then replace the second with
2323 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002324 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002325 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002326 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2327 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002328
Guy Benyei11169dd2012-12-18 14:30:41 +00002329 unsigned Line = getLineNumber(ED->getLocation());
2330 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002331 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002332 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2333 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002334
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002335 ReplaceMap.emplace_back(
2336 std::piecewise_construct, std::make_tuple(Ty),
2337 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002338 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 }
2340
David Blaikie483a9da2014-05-06 18:35:21 +00002341 return CreateTypeDefinition(Ty);
2342}
2343
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002344llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002345 const EnumDecl *ED = Ty->getDecl();
2346 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002347 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002348 if (!ED->getTypeForDecl()->isIncompleteType()) {
2349 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002350 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002351 }
2352
2353 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2354
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002355 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002356 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002357 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002358 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002359 Enumerators.push_back(DBuilder.createEnumerator(
2360 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 }
2362
2363 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002364 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002365
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002367 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002368 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002370 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2371 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2372 Line, Size, Align, EltArray, ClassTy,
2373 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002374}
2375
David Blaikie05491062013-01-21 04:37:12 +00002376static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2377 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002378 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002379 Qualifiers InnerQuals = T.getLocalQualifiers();
2380 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2381 // that is already there.
2382 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2383 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002384 QualType LastT = T;
2385 switch (T->getTypeClass()) {
2386 default:
David Blaikie05491062013-01-21 04:37:12 +00002387 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002388 case Type::TemplateSpecialization: {
2389 const auto *Spec = cast<TemplateSpecializationType>(T);
2390 if (Spec->isTypeAlias())
2391 return C.getQualifiedType(T.getTypePtr(), Quals);
2392 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002393 break;
2394 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002395 case Type::TypeOfExpr:
2396 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2397 break;
2398 case Type::TypeOf:
2399 T = cast<TypeOfType>(T)->getUnderlyingType();
2400 break;
2401 case Type::Decltype:
2402 T = cast<DecltypeType>(T)->getUnderlyingType();
2403 break;
2404 case Type::UnaryTransform:
2405 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2406 break;
2407 case Type::Attributed:
2408 T = cast<AttributedType>(T)->getEquivalentType();
2409 break;
2410 case Type::Elaborated:
2411 T = cast<ElaboratedType>(T)->getNamedType();
2412 break;
2413 case Type::Paren:
2414 T = cast<ParenType>(T)->getInnerType();
2415 break;
David Blaikie05491062013-01-21 04:37:12 +00002416 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002417 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002418 break;
Jordan Rose303e2f12016-11-10 23:28:17 +00002419 case Type::Auto: {
David Blaikie22c460a02013-05-24 21:24:35 +00002420 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002421 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002422 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002423 break;
2424 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002425 case Type::Adjusted:
2426 case Type::Decayed:
2427 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2428 T = cast<AdjustedType>(T)->getAdjustedType();
2429 break;
2430 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002431
Guy Benyei11169dd2012-12-18 14:30:41 +00002432 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002433 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002434 } while (true);
2435}
2436
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002438
2439 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002440 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002441
David Blaikief427b002014-05-06 03:42:01 +00002442 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002443 if (it != TypeCache.end()) {
2444 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002445 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002446 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002447 }
2448
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002449 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002450}
2451
David Blaikie0e716b42014-03-03 23:48:23 +00002452void CGDebugInfo::completeTemplateDefinition(
2453 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002454 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002455 return;
2456
David Blaikie0e716b42014-03-03 23:48:23 +00002457 completeClassData(&SD);
2458 // In case this type has no member function definitions being emitted, ensure
2459 // it is retained
2460 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2461}
2462
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002463llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002464 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002465 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002466
2467 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002468 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002469
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002470 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002471 return T;
2472
Adrian Prantlca844182015-09-11 17:23:03 +00002473 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002474 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002475
2476 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002477 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002478
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 return Res;
2480}
2481
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002482llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002483 // A forward declaration inside a module header does not belong to the module.
2484 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2485 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002486 if (DebugTypeExtRefs && D->isFromASTFile()) {
2487 // Record a reference to an imported clang module or precompiled header.
2488 auto *Reader = CGM.getContext().getExternalSource();
2489 auto Idx = D->getOwningModuleID();
2490 auto Info = Reader->getSourceDescriptor(Idx);
2491 if (Info)
2492 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2493 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002494 // We are building a clang module or a precompiled header.
2495 //
2496 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2497 // and it wouldn't be necessary to specify the parent scope
2498 // because the type is already unique by definition (it would look
2499 // like the output of -fno-standalone-debug). On the other hand,
2500 // the parent scope helps a consumer to quickly locate the object
2501 // file where the type's definition is located, so it might be
2502 // best to make this behavior a command line or debugger tuning
2503 // option.
2504 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2505 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002506 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002507 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2508 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002509 } else {
2510 // This the precompiled header being built.
2511 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002512 }
2513 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002514
Adrian Prantl9402cef2015-09-20 16:51:35 +00002515 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002516}
2517
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002518llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002519 // Handle qualifiers, which recursively handles what they refer to.
2520 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002521 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002522
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 // Work out details of type.
2524 switch (Ty->getTypeClass()) {
2525#define TYPE(Class, Base)
2526#define ABSTRACT_TYPE(Class, Base)
2527#define NON_CANONICAL_TYPE(Class, Base)
2528#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2529#include "clang/AST/TypeNodes.def"
2530 llvm_unreachable("Dependent types cannot show up in debug information");
2531
2532 case Type::ExtVector:
2533 case Type::Vector:
2534 return CreateType(cast<VectorType>(Ty), Unit);
2535 case Type::ObjCObjectPointer:
2536 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2537 case Type::ObjCObject:
2538 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002539 case Type::ObjCTypeParam:
2540 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002541 case Type::ObjCInterface:
2542 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2543 case Type::Builtin:
2544 return CreateType(cast<BuiltinType>(Ty));
2545 case Type::Complex:
2546 return CreateType(cast<ComplexType>(Ty));
2547 case Type::Pointer:
2548 return CreateType(cast<PointerType>(Ty), Unit);
2549 case Type::BlockPointer:
2550 return CreateType(cast<BlockPointerType>(Ty), Unit);
2551 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002552 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002554 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002556 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002557 case Type::FunctionProto:
2558 case Type::FunctionNoProto:
2559 return CreateType(cast<FunctionType>(Ty), Unit);
2560 case Type::ConstantArray:
2561 case Type::VariableArray:
2562 case Type::IncompleteArray:
2563 return CreateType(cast<ArrayType>(Ty), Unit);
2564
2565 case Type::LValueReference:
2566 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2567 case Type::RValueReference:
2568 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2569
2570 case Type::MemberPointer:
2571 return CreateType(cast<MemberPointerType>(Ty), Unit);
2572
2573 case Type::Atomic:
2574 return CreateType(cast<AtomicType>(Ty), Unit);
2575
Xiuli Pan9c14e282016-01-09 12:53:17 +00002576 case Type::Pipe:
2577 return CreateType(cast<PipeType>(Ty), Unit);
2578
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002580 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2581
David Blaikie42edade2014-11-11 20:44:45 +00002582 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002583 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002584 case Type::Adjusted:
2585 case Type::Decayed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 case Type::Elaborated:
2587 case Type::Paren:
2588 case Type::SubstTemplateTypeParm:
2589 case Type::TypeOfExpr:
2590 case Type::TypeOf:
2591 case Type::Decltype:
2592 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002593 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002594 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002596
David Blaikie42edade2014-11-11 20:44:45 +00002597 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002598}
2599
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002600llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2601 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002602 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002603
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002604 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002605
2606 // We may have cached a forward decl when we could have created
2607 // a non-forward decl. Go ahead and create a non-forward decl
2608 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002609 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002610 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002611
2612 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002613 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002614
2615 // Propagate members from the declaration to the definition
2616 // CreateType(const RecordType*) will overwrite this with the members in the
2617 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002618 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002619
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002621 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 return Res;
2623}
2624
2625// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002626llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002628
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002630 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 unsigned Line = getLineNumber(RD->getLocation());
2632 StringRef RDName = getClassName(RD);
2633
Amjad Abouddc4531e2016-04-30 01:44:38 +00002634 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002635
David Blaikied2785892013-08-18 17:36:19 +00002636 // If we ended up creating the type during the context chain construction,
2637 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002638 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002639 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002640 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002641 return T;
David Blaikied2785892013-08-18 17:36:19 +00002642
Adrian Prantl381e7552014-02-04 21:29:50 +00002643 // If this is just a forward or incomplete declaration, construct an
2644 // appropriately marked node and just return it.
2645 const RecordDecl *D = RD->getDefinition();
2646 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002647 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002648
2649 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002650 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002651
Manman Rene0064d82013-08-29 23:19:58 +00002652 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2653
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002654 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002655 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2656 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002657
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002658 // Elements of composite types usually have back to the type, creating
2659 // uniquing cycles. Distinct nodes are more efficient.
2660 switch (RealDecl->getTag()) {
2661 default:
2662 llvm_unreachable("invalid composite type tag");
2663
2664 case llvm::dwarf::DW_TAG_array_type:
2665 case llvm::dwarf::DW_TAG_enumeration_type:
2666 // Array elements and most enumeration elements don't have back references,
2667 // so they don't tend to be involved in uniquing cycles and there is some
2668 // chance of merging them when linking together two modules. Only make
2669 // them distinct if they are ODR-uniqued.
2670 if (FullName.empty())
2671 break;
2672
2673 case llvm::dwarf::DW_TAG_structure_type:
2674 case llvm::dwarf::DW_TAG_union_type:
2675 case llvm::dwarf::DW_TAG_class_type:
2676 // Immediatley resolve to a distinct node.
2677 RealDecl =
2678 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2679 break;
2680 }
2681
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002682 RegionMap[Ty->getDecl()].reset(RealDecl);
2683 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002684
David Majnemer58ed0f32016-07-17 00:39:12 +00002685 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002686 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002687 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002688 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002689}
2690
David Blaikieadfbf992013-08-18 16:55:33 +00002691void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002692 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002693 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002694 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002695 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2696 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002697 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002698 while (1) {
2699 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2700 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2701 if (PBT && !BRL.isPrimaryBaseVirtual())
2702 PBase = PBT;
2703 else
2704 break;
2705 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002706 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002707 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2708 getOrCreateFile(RD->getLocation())));
2709 } else if (RD->isDynamicClass())
2710 ContainingType = RealDecl;
2711
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002712 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002713}
2714
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002715llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002716 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002717 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002719 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002720 llvm::DIType *Ty =
2721 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2722 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 *Offset += FieldSize;
2724 return Ty;
2725}
2726
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002727void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002728 StringRef &Name,
2729 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002730 llvm::DIScope *&FDContext,
2731 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002732 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002733 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002734 Name = getFunctionName(FD);
2735 // Use mangled name as linkage name for C/C++ functions.
2736 if (FD->hasPrototype()) {
2737 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002738 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002739 }
2740 // No need to replicate the linkage name if it isn't different from the
2741 // subprogram name, no need to have it at all unless coverage is enabled or
2742 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002743 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2744 !CGM.getCodeGenOpts().EmitGcovNotes &&
2745 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002746 LinkageName = StringRef();
2747
Benjamin Kramer8c305922016-02-02 11:06:51 +00002748 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002749 if (const NamespaceDecl *NSDecl =
2750 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2751 FDContext = getOrCreateNameSpace(NSDecl);
2752 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002753 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2754 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2755 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2756 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002757 // Check if it is a noreturn-marked function
2758 if (FD->isNoReturn())
2759 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002760 // Collect template parameters.
2761 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2762 }
2763}
2764
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002765void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002766 unsigned &LineNo, QualType &T,
2767 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002769 Unit = getOrCreateFile(VD->getLocation());
2770 LineNo = getLineNumber(VD->getLocation());
2771
2772 setLocation(VD->getLocation());
2773
2774 T = VD->getType();
2775 if (T->isIncompleteArrayType()) {
2776 // CodeGen turns int[] into int[1] so we'll do the same here.
2777 llvm::APInt ConstVal(32, 1);
2778 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2779
2780 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2781 ArrayType::Normal, 0);
2782 }
2783
2784 Name = VD->getName();
2785 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2786 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2787 LinkageName = CGM.getMangledName(VD);
2788 if (LinkageName == Name)
2789 LinkageName = StringRef();
2790
2791 // Since we emit declarations (DW_AT_members) for static members, place the
2792 // definition of those static members in the namespace they were declared in
2793 // in the source code (the lexical decl context).
2794 // FIXME: Generalize this for even non-member global variables where the
2795 // declaration and definition may have different lexical decl contexts, once
2796 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002797 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2798 : VD->getDeclContext();
2799 // When a record type contains an in-line initialization of a static data
2800 // member, and the record type is marked as __declspec(dllexport), an implicit
2801 // definition of the member will be created in the record context. DWARF
2802 // doesn't seem to have a nice way to describe this in a form that consumers
2803 // are likely to understand, so fake the "normal" situation of a definition
2804 // outside the class by putting it in the global scope.
2805 if (DC->isRecord())
2806 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002807
Amjad Abouddc4531e2016-04-30 01:44:38 +00002808 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2809 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002810}
2811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002812llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002813CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002814 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002815 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002816 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Frederic Rissd253ed62014-11-18 03:40:51 +00002817 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002818 llvm::DIFile *Unit = getOrCreateFile(Loc);
2819 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002820 unsigned Line = getLineNumber(Loc);
2821
2822 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2823 TParamsArray, Flags);
2824 // Build function type.
2825 SmallVector<QualType, 16> ArgTypes;
2826 for (const ParmVarDecl *Parm: FD->parameters())
2827 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002828 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2829 QualType FnType = CGM.getContext().getFunctionType(
2830 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002831 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002832 DContext, Name, LinkageName, Unit, Line,
2833 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002834 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002835 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002836 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002837 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2838 std::make_tuple(CanonDecl),
2839 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002840 return SP;
2841}
2842
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002843llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002844CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2845 QualType T;
2846 StringRef Name, LinkageName;
2847 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002848 llvm::DIFile *Unit = getOrCreateFile(Loc);
2849 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002850 unsigned Line = getLineNumber(Loc);
2851
2852 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002853 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002854 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2855 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Victor Leschuka7ece032016-10-20 00:13:19 +00002856 !VD->isExternallyVisible(), nullptr, nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002857 FwdDeclReplaceMap.emplace_back(
2858 std::piecewise_construct,
2859 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2860 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002861 return GV;
2862}
2863
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002864llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002865 // We only need a declaration (not a definition) of the type - so use whatever
2866 // we would otherwise do to get a type for a pointee. (forward declarations in
2867 // limited debug info, full definitions (if the type definition is available)
2868 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002869 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002870 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002871 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002872 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002873
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002874 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002875 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002876
2877 // No definition for now. Emit a forward definition that might be
2878 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002879 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002880 return getFunctionForwardDeclaration(FD);
2881 else if (const auto *VD = dyn_cast<VarDecl>(D))
2882 return getGlobalVariableForwardDeclaration(VD);
2883
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002884 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002885}
2886
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002887llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002888 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002889 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002890
David Majnemer58ed0f32016-07-17 00:39:12 +00002891 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002892 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002893 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002894
2895 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002896 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002897
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002898 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002899 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002900 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002901 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002902 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002903 }
2904 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002905 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002906 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002907 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 return SP;
2909 }
2910
Aaron Ballman86c93902014-03-06 23:45:36 +00002911 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002912 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002913 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002914 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002915 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002916 return SP;
2917 }
2918 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002919 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002920}
2921
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002922// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002923// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002924llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002925 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002926 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002927 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002928 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2929 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002930 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002931
David Majnemer58ed0f32016-07-17 00:39:12 +00002932 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00002933 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002934
2935 const auto *FTy = FnType->getAs<FunctionType>();
2936 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2937
David Majnemer58ed0f32016-07-17 00:39:12 +00002938 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002940 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002941
2942 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002943 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002944
2945 // Replace the instancetype keyword with the actual type.
2946 if (ResultTy == CGM.getContext().getObjCInstanceType())
2947 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002948 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002949
Adrian Prantl7bec9032013-05-10 21:08:31 +00002950 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002952 QualType SelfDeclTy;
2953 if (auto *SelfDecl = OMethod->getSelfDecl())
2954 SelfDeclTy = SelfDecl->getType();
2955 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2956 if (FPT->getNumParams() > 1)
2957 SelfDeclTy = FPT->getParamType(0);
2958 if (!SelfDeclTy.isNull())
2959 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002961 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002962 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002963 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00002964 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00002965 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002966 // Variadic methods need a special marker at the end of the type list.
2967 if (OMethod->isVariadic())
2968 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002970 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002971 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2972 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002974
Adrian Prantl800faef2014-02-25 23:42:18 +00002975 // Handle variadic function types; they need an additional
2976 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00002977 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00002978 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002979 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002980 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00002981 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2982 for (QualType ParamType : FPT->param_types())
2983 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00002984 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002985 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002986 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2987 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002988 }
2989
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002990 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002991}
2992
Eric Christophere7b87e52014-10-26 23:40:33 +00002993void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2994 SourceLocation ScopeLoc, QualType FnType,
2995 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002996
2997 StringRef Name;
2998 StringRef LinkageName;
2999
3000 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3001
3002 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003003 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003004
Leny Kholodov80c047d2016-09-06 10:48:04 +00003005 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003006 llvm::DIFile *Unit = getOrCreateFile(Loc);
3007 llvm::DIScope *FDContext = Unit;
3008 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003009 if (!HasDecl) {
3010 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003011 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003012 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003013 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003014 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003015 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003016 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003017 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003018 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003019 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 return;
3021 }
3022 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003023 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3024 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003025 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003027 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 } else {
3029 // Use llvm function name.
3030 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003031 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003033 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003034 Name = Name.substr(1);
3035
Adrian Prantl42d71b92014-04-10 23:21:53 +00003036 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003037 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003038 // Artificial functions should not silently reuse CurLoc.
3039 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003040 }
3041 unsigned LineNo = getLineNumber(Loc);
3042 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003043
Eric Christopher8018e412014-03-27 18:50:35 +00003044 // FIXME: The function declaration we're constructing here is mostly reusing
3045 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3046 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3047 // all subprograms instead of the actual context since subprogram definitions
3048 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003049 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003050 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003051 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003052 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003053 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003054 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003055 // We might get here with a VarDecl in the case we're generating
3056 // code for the initialization of globals. Do not record these decls
3057 // as they will overwrite the actual VarDecl Decl in the cache.
3058 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003059 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003060
Adrian Prantlbebb8932014-03-21 21:01:58 +00003061 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003062 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003063
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003065 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003066}
3067
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003068void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3069 QualType FnType) {
3070 StringRef Name;
3071 StringRef LinkageName;
3072
3073 const Decl *D = GD.getDecl();
3074 if (!D)
3075 return;
3076
Leny Kholodov80c047d2016-09-06 10:48:04 +00003077 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003078 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003079 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003080 llvm::DINodeArray TParamsArray;
3081 if (isa<FunctionDecl>(D)) {
3082 // If there is a DISubprogram for this function available then use it.
3083 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3084 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003085 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003086 Name = getObjCMethodName(OMD);
3087 Flags |= llvm::DINode::FlagPrototyped;
3088 } else {
3089 llvm_unreachable("not a function or ObjC method");
3090 }
3091 if (!Name.empty() && Name[0] == '\01')
3092 Name = Name.substr(1);
3093
3094 if (D->isImplicit()) {
3095 Flags |= llvm::DINode::FlagArtificial;
3096 // Artificial functions without a location should not silently reuse CurLoc.
3097 if (Loc.isInvalid())
3098 CurLoc = SourceLocation();
3099 }
3100 unsigned LineNo = getLineNumber(Loc);
3101 unsigned ScopeLine = 0;
3102
Adrian Prantle76bda52016-04-15 15:55:45 +00003103 DBuilder.retainType(DBuilder.createFunction(
3104 FDContext, Name, LinkageName, Unit, LineNo,
3105 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3106 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3107 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003108}
3109
David Blaikie835afb22015-01-21 23:08:17 +00003110void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 // Update our current location
3112 setLocation(Loc);
3113
Eric Christophere7b87e52014-10-26 23:40:33 +00003114 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3115 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003116
Adrian Prantle83b1302014-01-07 22:05:52 +00003117 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003118 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00003119 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003120}
3121
Guy Benyei11169dd2012-12-18 14:30:41 +00003122void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003123 llvm::MDNode *Back = nullptr;
3124 if (!LexicalBlockStack.empty())
3125 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003126 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003127 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003128 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003129}
3130
Eric Christopher0fdcb312013-05-16 00:52:20 +00003131void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3132 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 // Set our current location.
3134 setLocation(Loc);
3135
Guy Benyei11169dd2012-12-18 14:30:41 +00003136 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00003137 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3138 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00003139
Benjamin Kramer8c305922016-02-02 11:06:51 +00003140 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003141 return;
3142
3143 // Create a new lexical block and push it on the stack.
3144 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003145}
3146
Eric Christopher0fdcb312013-05-16 00:52:20 +00003147void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3148 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3150
3151 // Provide an entry in the line table for the end of the block.
3152 EmitLocation(Builder, Loc);
3153
Benjamin Kramer8c305922016-02-02 11:06:51 +00003154 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003155 return;
3156
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 LexicalBlockStack.pop_back();
3158}
3159
Guy Benyei11169dd2012-12-18 14:30:41 +00003160void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3161 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3162 unsigned RCount = FnBeginRegionCount.back();
3163 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3164
3165 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003166 while (LexicalBlockStack.size() != RCount) {
3167 // Provide an entry in the line table for the end of the block.
3168 EmitLocation(Builder, CurLoc);
3169 LexicalBlockStack.pop_back();
3170 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003171 FnBeginRegionCount.pop_back();
3172}
3173
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003174llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003175 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003176
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003177 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003178 QualType FType;
3179 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003180 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003181
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003182 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003183 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
3185 FieldOffset = 0;
3186 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3187 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3188 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3189 FType = CGM.getContext().IntTy;
3190 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3191 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3192
3193 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3194 if (HasCopyAndDispose) {
3195 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003196 EltTys.push_back(
3197 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3198 EltTys.push_back(
3199 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003200 }
3201 bool HasByrefExtendedLayout;
3202 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003203 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3204 HasByrefExtendedLayout) &&
3205 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003206 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003207 EltTys.push_back(
3208 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003209 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003210
Guy Benyei11169dd2012-12-18 14:30:41 +00003211 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3212 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003213 CGM.getTarget().getPointerAlign(0))) {
3214 CharUnits FieldOffsetInBytes =
3215 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003216 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003217 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003218
Guy Benyei11169dd2012-12-18 14:30:41 +00003219 if (NumPaddingBytes.isPositive()) {
3220 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3221 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3222 pad, ArrayType::Normal, 0);
3223 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3224 }
3225 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003226
Guy Benyei11169dd2012-12-18 14:30:41 +00003227 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003228 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 FieldSize = CGM.getContext().getTypeSize(FType);
3230 FieldAlign = CGM.getContext().toBits(Align);
3231
Eric Christopherb2a008c2013-05-16 00:45:12 +00003232 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003233 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003234 FieldAlign, FieldOffset,
3235 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003236 EltTys.push_back(FieldTy);
3237 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003239 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003240
Leny Kholodov80c047d2016-09-06 10:48:04 +00003241 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003242
Guy Benyei11169dd2012-12-18 14:30:41 +00003243 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003244 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003245}
3246
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003247void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3248 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003249 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003250 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003252 if (VD->hasAttr<NoDebugAttr>())
3253 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003254
David Blaikie7fceebf2013-08-19 03:37:48 +00003255 bool Unwritten =
3256 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3257 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003258 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003259 if (!Unwritten)
3260 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003261 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003262 uint64_t XOffset = 0;
3263 if (VD->hasAttr<BlocksAttr>())
3264 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003265 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003266 Ty = getOrCreateType(VD->getType(), Unit);
3267
3268 // If there is no debug info for this type then do not emit debug info
3269 // for this variable.
3270 if (!Ty)
3271 return;
3272
Guy Benyei11169dd2012-12-18 14:30:41 +00003273 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003274 unsigned Line = 0;
3275 unsigned Column = 0;
3276 if (!Unwritten) {
3277 Line = getLineNumber(VD->getLocation());
3278 Column = getColumnNumber(VD->getLocation());
3279 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003280 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003281 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003282 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003283 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003284
3285 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3286
Guy Benyei11169dd2012-12-18 14:30:41 +00003287 // If this is the first argument and it is implicit then
3288 // give it an object pointer flag.
3289 // FIXME: There has to be a better way to do this, but for static
3290 // functions there won't be an implicit param at arg1 and
3291 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003292 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003293 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003294 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003295 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3296 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003297 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003298
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003299 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003300
3301 StringRef Name = VD->getName();
3302 if (!Name.empty()) {
3303 if (VD->hasAttr<BlocksAttr>()) {
3304 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003305 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003306 // offset of __forwarding field
3307 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003308 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003309 Expr.push_back(offset.getQuantity());
3310 Expr.push_back(llvm::dwarf::DW_OP_deref);
3311 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003312 // offset of x field
3313 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003314 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003315
3316 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003317 auto *D = ArgNo
3318 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3319 *ArgNo, Unit, Line, Ty)
3320 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003321 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003322
Guy Benyei11169dd2012-12-18 14:30:41 +00003323 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003324 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3325 llvm::DebugLoc::get(Line, Column, Scope),
3326 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003327 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003328 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003329 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003330 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003331 // If VD is an anonymous union then Storage represents value for
3332 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003333 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003334 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003335 // GDB has trouble finding local variables in anonymous unions, so we emit
3336 // artifical local variables for each of the members.
3337 //
3338 // FIXME: Remove this code as soon as GDB supports this.
3339 // The debug info verifier in LLVM operates based on the assumption that a
3340 // variable has the same size as its storage and we had to disable the check
3341 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003342 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003343 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003344 StringRef FieldName = Field->getName();
3345
3346 // Ignore unnamed fields. Do not ignore unnamed records.
3347 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3348 continue;
3349
3350 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003351 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003352 auto *D = DBuilder.createAutoVariable(
3353 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003354 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003355
3356 // Insert an llvm.dbg.declare into the current block.
3357 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3358 llvm::DebugLoc::get(Line, Column, Scope),
3359 Builder.GetInsertBlock());
3360 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003361 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003362 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003363
3364 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003365 auto *D = ArgNo
3366 ? DBuilder.createParameterVariable(
3367 Scope, Name, *ArgNo, Unit, Line, Ty,
3368 CGM.getLangOpts().Optimize, Flags)
3369 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3370 CGM.getLangOpts().Optimize, Flags,
3371 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003372
3373 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003374 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3375 llvm::DebugLoc::get(Line, Column, Scope),
3376 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003377}
3378
3379void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3380 llvm::Value *Storage,
3381 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003382 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003383 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003384}
3385
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003386llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3387 llvm::DIType *Ty) {
3388 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003389 if (CachedTy)
3390 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003391 return DBuilder.createObjectPointerType(Ty);
3392}
3393
Eric Christophere7b87e52014-10-26 23:40:33 +00003394void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3395 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003396 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003397 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003399
Craig Topper8a13c412014-05-21 05:09:00 +00003400 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003402 if (VD->hasAttr<NoDebugAttr>())
3403 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003404
Guy Benyei11169dd2012-12-18 14:30:41 +00003405 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003406
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003408 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3409 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003410 if (isByRef)
3411 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003412 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003413 Ty = getOrCreateType(VD->getType(), Unit);
3414
3415 // Self is passed along as an implicit non-arg variable in a
3416 // block. Mark it as the object pointer.
3417 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003418 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003419
3420 // Get location information.
3421 unsigned Line = getLineNumber(VD->getLocation());
3422 unsigned Column = getColumnNumber(VD->getLocation());
3423
3424 const llvm::DataLayout &target = CGM.getDataLayout();
3425
3426 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003427 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3429
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003430 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003431 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003432 addr.push_back(llvm::dwarf::DW_OP_deref);
3433 addr.push_back(llvm::dwarf::DW_OP_plus);
3434 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003435 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003436 addr.push_back(llvm::dwarf::DW_OP_deref);
3437 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003438 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003439 offset =
3440 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003441 addr.push_back(offset.getQuantity());
3442 addr.push_back(llvm::dwarf::DW_OP_deref);
3443 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003444 // offset of x field
3445 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003446 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003447 }
3448
3449 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003450 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003451 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003452 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003453 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003454
Guy Benyei11169dd2012-12-18 14:30:41 +00003455 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003456 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3457 if (InsertPoint)
3458 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3459 InsertPoint);
3460 else
3461 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3462 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003463}
3464
Guy Benyei11169dd2012-12-18 14:30:41 +00003465void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3466 unsigned ArgNo,
3467 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003468 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003469 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003470}
3471
3472namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003473struct BlockLayoutChunk {
3474 uint64_t OffsetInBits;
3475 const BlockDecl::Capture *Capture;
3476};
3477bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3478 return l.OffsetInBits < r.OffsetInBits;
3479}
Guy Benyei11169dd2012-12-18 14:30:41 +00003480}
3481
3482void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003483 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003484 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003485 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003487 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 ASTContext &C = CGM.getContext();
3489 const BlockDecl *blockDecl = block.getBlockDecl();
3490
3491 // Collect some general information about the block's location.
3492 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003493 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 unsigned line = getLineNumber(loc);
3495 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003496
Guy Benyei11169dd2012-12-18 14:30:41 +00003497 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003498 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003499
3500 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003501 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003502
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003503 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003504 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003505 blockLayout->getElementOffsetInBits(0),
3506 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003507 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003508 blockLayout->getElementOffsetInBits(1),
3509 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003510 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003511 blockLayout->getElementOffsetInBits(2),
3512 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003513 auto *FnTy = block.getBlockExpr()->getFunctionType();
3514 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003515 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 blockLayout->getElementOffsetInBits(3),
3517 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003518 fields.push_back(createFieldType(
3519 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3520 ? C.getBlockDescriptorExtendedType()
3521 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003522 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003523
3524 // We want to sort the captures by offset, not because DWARF
3525 // requires this, but because we're paranoid about debuggers.
3526 SmallVector<BlockLayoutChunk, 8> chunks;
3527
3528 // 'this' capture.
3529 if (blockDecl->capturesCXXThis()) {
3530 BlockLayoutChunk chunk;
3531 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003532 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003533 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003534 chunks.push_back(chunk);
3535 }
3536
3537 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003538 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003539 const VarDecl *variable = capture.getVariable();
3540 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3541
3542 // Ignore constant captures.
3543 if (captureInfo.isConstant())
3544 continue;
3545
3546 BlockLayoutChunk chunk;
3547 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003548 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003549 chunk.Capture = &capture;
3550 chunks.push_back(chunk);
3551 }
3552
3553 // Sort by offset.
3554 llvm::array_pod_sort(chunks.begin(), chunks.end());
3555
David Majnemer58ed0f32016-07-17 00:39:12 +00003556 for (const BlockLayoutChunk &Chunk : chunks) {
3557 uint64_t offsetInBits = Chunk.OffsetInBits;
3558 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003559
3560 // If we have a null capture, this must be the C++ 'this' capture.
3561 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003562 QualType type;
3563 if (auto *Method =
3564 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3565 type = Method->getThisType(C);
3566 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3567 type = QualType(RDecl->getTypeForDecl(), 0);
3568 else
3569 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003570
David Majnemerb4b671e2016-06-30 03:01:59 +00003571 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003572 offsetInBits, tunit, tunit));
3573 continue;
3574 }
3575
3576 const VarDecl *variable = capture->getVariable();
3577 StringRef name = variable->getName();
3578
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003579 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003580 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003581 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003582 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003583
3584 // FIXME: this creates a second copy of this type!
3585 uint64_t xoffset;
3586 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003587 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003588 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3589 PtrInfo.Width, Align, offsetInBits,
3590 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003592 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003593 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003594 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 }
3596 fields.push_back(fieldType);
3597 }
3598
3599 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003600 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3601 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003602
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003603 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003604
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003605 llvm::DIType *type =
3606 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003607 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003608 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003609 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3610
3611 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003612 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003613 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003614
3615 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003616 auto *debugVar = DBuilder.createParameterVariable(
3617 scope, Arg->getName(), ArgNo, tunit, line, type,
3618 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003619
Adrian Prantl616bef42013-03-14 21:52:59 +00003620 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003621 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003622 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003623 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003624 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003625 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003626
Adrian Prantl616bef42013-03-14 21:52:59 +00003627 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003628 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3629 llvm::DebugLoc::get(line, column, scope),
3630 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003631}
3632
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003633llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003634CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3635 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003636 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003637
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003638 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003639 if (MI != StaticDataMemberCache.end()) {
3640 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003641 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003642 }
David Blaikiece763042013-08-20 21:49:21 +00003643
3644 // If the member wasn't found in the cache, lazily construct and add it to the
3645 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003646 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003647 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003648 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003649}
3650
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003651llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3652 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3653 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3654 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003655
3656 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003657 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003658 StringRef FieldName = Field->getName();
3659
3660 // Ignore unnamed fields, but recurse into anonymous records.
3661 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003662 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Eric Christophercab9fae2014-04-10 05:20:00 +00003663 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3664 Var, DContext);
3665 continue;
3666 }
3667 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003668 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003669 LineNo, FieldTy, Var->hasLocalLinkage());
3670 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003671 }
3672 return GV;
3673}
3674
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003675void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003676 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003677 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003678 if (D->hasAttr<NoDebugAttr>())
3679 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003680
3681 // If we already created a DIGlobalVariable for this declaration, just attach
3682 // it to the llvm::GlobalVariable.
3683 auto Cached = DeclCache.find(D->getCanonicalDecl());
3684 if (Cached != DeclCache.end())
3685 return Var->addDebugInfo(cast<llvm::DIGlobalVariable>(Cached->second));
3686
Guy Benyei11169dd2012-12-18 14:30:41 +00003687 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003688 llvm::DIFile *Unit = nullptr;
3689 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003690 unsigned LineNo;
3691 StringRef DeclName, LinkageName;
3692 QualType T;
3693 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003694
3695 // Attempt to store one global variable for the declaration - even if we
3696 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003697 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003698
3699 // If this is an anonymous union then we'll want to emit a global
3700 // variable for each member of the anonymous union so that it's possible
3701 // to find the name of any field in the union.
3702 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003703 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003704 assert(RD->isAnonymousStructOrUnion() &&
3705 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003706 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3707 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003708 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
David Blaikie7550b112014-10-20 17:42:23 +00003709 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003710 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003711 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003712 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003713 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003714 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003715 DeclCache[D->getCanonicalDecl()].reset(GV);
Guy Benyei11169dd2012-12-18 14:30:41 +00003716}
3717
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003718void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003719 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003720 if (VD->hasAttr<NoDebugAttr>())
3721 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003722 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003723 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003724 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003725 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003726 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003727 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3728 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003729 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3730 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3731 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003732 // Do not use global variables for enums.
3733 //
3734 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003735 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003736 return;
David Blaikiea15565562014-04-04 20:56:17 +00003737 // Do not emit separate definitions for function local const/statics.
3738 if (isa<FunctionDecl>(VD->getDeclContext()))
3739 return;
David Blaikiebb113912014-04-05 07:23:17 +00003740 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003741 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003742 if (VarD->isStaticDataMember()) {
3743 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003744 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003745 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003746 //
3747 // FIXME: This is probably unnecessary, since Ty should reference RD
3748 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003749 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003750 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003751 return;
3752 }
3753
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003754 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003755
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003756 auto &GV = DeclCache[VD];
3757 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003758 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003759 llvm::DIExpression *InitExpr = nullptr;
3760 if (Init.isInt())
3761 InitExpr =
3762 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
David Gross1118d592016-12-08 20:02:46 +00003763 else if (Init.isFloat() && CGM.getContext().getTypeSize(VD->getType()) <= 64)
3764 InitExpr = DBuilder.createConstantValueExpression(
3765 Init.getFloat().bitcastToAPInt().getZExtValue());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003766 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003767 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003768 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3769 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003770}
3771
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003772llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003773 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003774 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003775 llvm::DIScope *Mod = getParentModuleOrNull(D);
3776 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003777}
3778
David Blaikie9f88fe82013-04-22 06:13:21 +00003779void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003780 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003781 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003782 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003783 if (!NSDecl->isAnonymousNamespace() ||
3784 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003785 DBuilder.createImportedModule(
3786 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3787 getOrCreateNameSpace(NSDecl),
3788 getLineNumber(UD.getLocation()));
3789 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003790}
3791
David Blaikiebd483762013-05-20 04:58:53 +00003792void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003793 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003794 return;
3795 assert(UD.shadow_size() &&
3796 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3797 // Emitting one decl is sufficient - debuggers can detect that this is an
3798 // overloaded name & provide lookup for all the overloads.
3799 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003800
3801 // FIXME: Skip functions with undeduced auto return type for now since we
3802 // don't currently have the plumbing for separate declarations & definitions
3803 // of free functions and mismatched types (auto in the declaration, concrete
3804 // return type in the definition)
3805 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3806 if (const auto *AT =
3807 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3808 if (AT->getDeducedType().isNull())
3809 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003810 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003811 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003812 DBuilder.createImportedDeclaration(
3813 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3814 getLineNumber(USD.getLocation()));
3815}
3816
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003817void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003818 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3819 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003820 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003821 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003822 DBuilder.createImportedDeclaration(
3823 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3824 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3825 getLineNumber(ID.getLocation()));
3826 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003827}
3828
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003829llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003830CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003831 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003832 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003833 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003834 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003835 return cast<llvm::DIImportedEntity>(VH);
3836 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003837 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003838 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3839 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003840 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003841 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3842 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3843 NA.getName());
3844 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003845 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003846 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3847 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3848 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003849 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003850 return R;
3851}
3852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003853llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003854CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003855 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003856 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003857 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003858 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003859
Guy Benyei11169dd2012-12-18 14:30:41 +00003860 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003861 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003862 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00003863 llvm::DINamespace *NS = DBuilder.createNameSpace(
3864 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003865 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003866 return NS;
3867}
3868
Adrian Prantla5206ce2015-09-22 23:26:43 +00003869void CGDebugInfo::setDwoId(uint64_t Signature) {
3870 assert(TheCU && "no main compile unit");
3871 TheCU->setDWOId(Signature);
3872}
3873
3874
Guy Benyei11169dd2012-12-18 14:30:41 +00003875void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003876 // Creating types might create further types - invalidating the current
3877 // element and the size(), so don't cache/reference them.
3878 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3879 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003880 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003881 ? CreateTypeDefinition(E.Type, E.Unit)
3882 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003883 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003884 }
3885
David Blaikief427b002014-05-06 03:42:01 +00003886 for (auto p : ReplaceMap) {
3887 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003888 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003889 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003890
David Blaikief427b002014-05-06 03:42:01 +00003891 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003892 assert(it != TypeCache.end());
3893 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003894
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003895 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3896 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003897 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003898
Frederic Rissd253ed62014-11-18 03:40:51 +00003899 for (const auto &p : FwdDeclReplaceMap) {
3900 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003901 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003902 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003903
3904 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003905 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003906 // with ourselves, that will destroy the temporary MDNode and
3907 // replace it with a standard one, avoiding leaking memory.
3908 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003909 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003910 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003911 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003912
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003913 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003914 }
3915
Adrian Prantl73409ce2013-03-11 18:33:46 +00003916 // We keep our own list of retained types, because we need to look
3917 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003918 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003919 if (auto MD = TypeCache[RT])
3920 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003921
Guy Benyei11169dd2012-12-18 14:30:41 +00003922 DBuilder.finalize();
3923}
David Blaikie66088d52014-09-24 17:01:27 +00003924
3925void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003926 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003927 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003928
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003929 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003930 // Don't ignore in case of explicit cast where it is referenced indirectly.
3931 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003932}
Amara Emerson652795d2016-11-10 14:44:30 +00003933
3934llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
3935 if (LexicalBlockStack.empty())
3936 return llvm::DebugLoc();
3937
3938 llvm::MDNode *Scope = LexicalBlockStack.back();
3939 return llvm::DebugLoc::get(
3940 getLineNumber(Loc), getColumnNumber(Loc), Scope);
3941}