blob: ef4fb06c18375f10aa99ae1d7b84c42646e72adb [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;
2419 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 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002425
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002427 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002428 } while (true);
2429}
2430
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002431llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002432
2433 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002434 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002435
David Blaikief427b002014-05-06 03:42:01 +00002436 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002437 if (it != TypeCache.end()) {
2438 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002439 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002440 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002441 }
2442
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002443 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002444}
2445
David Blaikie0e716b42014-03-03 23:48:23 +00002446void CGDebugInfo::completeTemplateDefinition(
2447 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002448 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002449 return;
2450
David Blaikie0e716b42014-03-03 23:48:23 +00002451 completeClassData(&SD);
2452 // In case this type has no member function definitions being emitted, ensure
2453 // it is retained
2454 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2455}
2456
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002457llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002459 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002460
2461 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002462 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002463
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002464 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002465 return T;
2466
Adrian Prantlca844182015-09-11 17:23:03 +00002467 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002468 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002469
2470 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002471 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002472
Guy Benyei11169dd2012-12-18 14:30:41 +00002473 return Res;
2474}
2475
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002476llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002477 // A forward declaration inside a module header does not belong to the module.
2478 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2479 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002480 if (DebugTypeExtRefs && D->isFromASTFile()) {
2481 // Record a reference to an imported clang module or precompiled header.
2482 auto *Reader = CGM.getContext().getExternalSource();
2483 auto Idx = D->getOwningModuleID();
2484 auto Info = Reader->getSourceDescriptor(Idx);
2485 if (Info)
2486 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2487 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002488 // We are building a clang module or a precompiled header.
2489 //
2490 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2491 // and it wouldn't be necessary to specify the parent scope
2492 // because the type is already unique by definition (it would look
2493 // like the output of -fno-standalone-debug). On the other hand,
2494 // the parent scope helps a consumer to quickly locate the object
2495 // file where the type's definition is located, so it might be
2496 // best to make this behavior a command line or debugger tuning
2497 // option.
2498 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2499 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002500 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002501 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2502 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002503 } else {
2504 // This the precompiled header being built.
2505 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002506 }
2507 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002508
Adrian Prantl9402cef2015-09-20 16:51:35 +00002509 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002510}
2511
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002512llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002513 // Handle qualifiers, which recursively handles what they refer to.
2514 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002515 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002516
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 // Work out details of type.
2518 switch (Ty->getTypeClass()) {
2519#define TYPE(Class, Base)
2520#define ABSTRACT_TYPE(Class, Base)
2521#define NON_CANONICAL_TYPE(Class, Base)
2522#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2523#include "clang/AST/TypeNodes.def"
2524 llvm_unreachable("Dependent types cannot show up in debug information");
2525
2526 case Type::ExtVector:
2527 case Type::Vector:
2528 return CreateType(cast<VectorType>(Ty), Unit);
2529 case Type::ObjCObjectPointer:
2530 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2531 case Type::ObjCObject:
2532 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002533 case Type::ObjCTypeParam:
2534 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002535 case Type::ObjCInterface:
2536 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2537 case Type::Builtin:
2538 return CreateType(cast<BuiltinType>(Ty));
2539 case Type::Complex:
2540 return CreateType(cast<ComplexType>(Ty));
2541 case Type::Pointer:
2542 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002543 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002544 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002545 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002546 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002547 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 case Type::BlockPointer:
2549 return CreateType(cast<BlockPointerType>(Ty), Unit);
2550 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002551 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002553 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002555 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002556 case Type::FunctionProto:
2557 case Type::FunctionNoProto:
2558 return CreateType(cast<FunctionType>(Ty), Unit);
2559 case Type::ConstantArray:
2560 case Type::VariableArray:
2561 case Type::IncompleteArray:
2562 return CreateType(cast<ArrayType>(Ty), Unit);
2563
2564 case Type::LValueReference:
2565 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2566 case Type::RValueReference:
2567 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2568
2569 case Type::MemberPointer:
2570 return CreateType(cast<MemberPointerType>(Ty), Unit);
2571
2572 case Type::Atomic:
2573 return CreateType(cast<AtomicType>(Ty), Unit);
2574
Xiuli Pan9c14e282016-01-09 12:53:17 +00002575 case Type::Pipe:
2576 return CreateType(cast<PipeType>(Ty), Unit);
2577
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002579 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2580
David Blaikie42edade2014-11-11 20:44:45 +00002581 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002582 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002583 case Type::Elaborated:
2584 case Type::Paren:
2585 case Type::SubstTemplateTypeParm:
2586 case Type::TypeOfExpr:
2587 case Type::TypeOf:
2588 case Type::Decltype:
2589 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002590 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002591 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002593
David Blaikie42edade2014-11-11 20:44:45 +00002594 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002595}
2596
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002597llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2598 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002599 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002600
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002601 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002602
2603 // We may have cached a forward decl when we could have created
2604 // a non-forward decl. Go ahead and create a non-forward decl
2605 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002606 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002607 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002608
2609 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002610 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002611
2612 // Propagate members from the declaration to the definition
2613 // CreateType(const RecordType*) will overwrite this with the members in the
2614 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002615 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002616
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002618 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002619 return Res;
2620}
2621
2622// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002623llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002625
Guy Benyei11169dd2012-12-18 14:30:41 +00002626 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002627 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 unsigned Line = getLineNumber(RD->getLocation());
2629 StringRef RDName = getClassName(RD);
2630
Amjad Abouddc4531e2016-04-30 01:44:38 +00002631 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002632
David Blaikied2785892013-08-18 17:36:19 +00002633 // If we ended up creating the type during the context chain construction,
2634 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002635 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002636 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002637 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002638 return T;
David Blaikied2785892013-08-18 17:36:19 +00002639
Adrian Prantl381e7552014-02-04 21:29:50 +00002640 // If this is just a forward or incomplete declaration, construct an
2641 // appropriately marked node and just return it.
2642 const RecordDecl *D = RD->getDefinition();
2643 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002644 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002645
2646 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002647 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002648
Manman Rene0064d82013-08-29 23:19:58 +00002649 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2650
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002651 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002652 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2653 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002654
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002655 // Elements of composite types usually have back to the type, creating
2656 // uniquing cycles. Distinct nodes are more efficient.
2657 switch (RealDecl->getTag()) {
2658 default:
2659 llvm_unreachable("invalid composite type tag");
2660
2661 case llvm::dwarf::DW_TAG_array_type:
2662 case llvm::dwarf::DW_TAG_enumeration_type:
2663 // Array elements and most enumeration elements don't have back references,
2664 // so they don't tend to be involved in uniquing cycles and there is some
2665 // chance of merging them when linking together two modules. Only make
2666 // them distinct if they are ODR-uniqued.
2667 if (FullName.empty())
2668 break;
2669
2670 case llvm::dwarf::DW_TAG_structure_type:
2671 case llvm::dwarf::DW_TAG_union_type:
2672 case llvm::dwarf::DW_TAG_class_type:
2673 // Immediatley resolve to a distinct node.
2674 RealDecl =
2675 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2676 break;
2677 }
2678
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002679 RegionMap[Ty->getDecl()].reset(RealDecl);
2680 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002681
David Majnemer58ed0f32016-07-17 00:39:12 +00002682 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002683 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002684 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002685 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002686}
2687
David Blaikieadfbf992013-08-18 16:55:33 +00002688void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002689 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002690 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002691 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002692 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2693 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002694 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002695 while (1) {
2696 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2697 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2698 if (PBT && !BRL.isPrimaryBaseVirtual())
2699 PBase = PBT;
2700 else
2701 break;
2702 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002703 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002704 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2705 getOrCreateFile(RD->getLocation())));
2706 } else if (RD->isDynamicClass())
2707 ContainingType = RealDecl;
2708
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002709 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002710}
2711
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002712llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002713 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002714 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002715 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002716 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002717 llvm::DIType *Ty =
2718 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2719 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002720 *Offset += FieldSize;
2721 return Ty;
2722}
2723
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002724void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002725 StringRef &Name,
2726 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002727 llvm::DIScope *&FDContext,
2728 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002729 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002730 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002731 Name = getFunctionName(FD);
2732 // Use mangled name as linkage name for C/C++ functions.
2733 if (FD->hasPrototype()) {
2734 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002735 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002736 }
2737 // No need to replicate the linkage name if it isn't different from the
2738 // subprogram name, no need to have it at all unless coverage is enabled or
2739 // debug is set to more than just line tables.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002740 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2741 !CGM.getCodeGenOpts().EmitGcovNotes &&
2742 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002743 LinkageName = StringRef();
2744
Benjamin Kramer8c305922016-02-02 11:06:51 +00002745 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002746 if (const NamespaceDecl *NSDecl =
2747 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2748 FDContext = getOrCreateNameSpace(NSDecl);
2749 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002750 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2751 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2752 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2753 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002754 // Check if it is a noreturn-marked function
2755 if (FD->isNoReturn())
2756 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002757 // Collect template parameters.
2758 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2759 }
2760}
2761
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002762void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002763 unsigned &LineNo, QualType &T,
2764 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002765 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002766 Unit = getOrCreateFile(VD->getLocation());
2767 LineNo = getLineNumber(VD->getLocation());
2768
2769 setLocation(VD->getLocation());
2770
2771 T = VD->getType();
2772 if (T->isIncompleteArrayType()) {
2773 // CodeGen turns int[] into int[1] so we'll do the same here.
2774 llvm::APInt ConstVal(32, 1);
2775 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2776
2777 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2778 ArrayType::Normal, 0);
2779 }
2780
2781 Name = VD->getName();
2782 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2783 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2784 LinkageName = CGM.getMangledName(VD);
2785 if (LinkageName == Name)
2786 LinkageName = StringRef();
2787
2788 // Since we emit declarations (DW_AT_members) for static members, place the
2789 // definition of those static members in the namespace they were declared in
2790 // in the source code (the lexical decl context).
2791 // FIXME: Generalize this for even non-member global variables where the
2792 // declaration and definition may have different lexical decl contexts, once
2793 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002794 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2795 : VD->getDeclContext();
2796 // When a record type contains an in-line initialization of a static data
2797 // member, and the record type is marked as __declspec(dllexport), an implicit
2798 // definition of the member will be created in the record context. DWARF
2799 // doesn't seem to have a nice way to describe this in a form that consumers
2800 // are likely to understand, so fake the "normal" situation of a definition
2801 // outside the class by putting it in the global scope.
2802 if (DC->isRecord())
2803 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002804
Amjad Abouddc4531e2016-04-30 01:44:38 +00002805 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2806 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002807}
2808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002809llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002810CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002811 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002812 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002813 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Frederic Rissd253ed62014-11-18 03:40:51 +00002814 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002815 llvm::DIFile *Unit = getOrCreateFile(Loc);
2816 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002817 unsigned Line = getLineNumber(Loc);
2818
2819 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2820 TParamsArray, Flags);
2821 // Build function type.
2822 SmallVector<QualType, 16> ArgTypes;
2823 for (const ParmVarDecl *Parm: FD->parameters())
2824 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002825 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2826 QualType FnType = CGM.getContext().getFunctionType(
2827 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002828 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002829 DContext, Name, LinkageName, Unit, Line,
2830 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002831 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002832 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002833 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002834 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2835 std::make_tuple(CanonDecl),
2836 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002837 return SP;
2838}
2839
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002840llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002841CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2842 QualType T;
2843 StringRef Name, LinkageName;
2844 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002845 llvm::DIFile *Unit = getOrCreateFile(Loc);
2846 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002847 unsigned Line = getLineNumber(Loc);
2848
2849 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00002850 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002851 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2852 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Victor Leschuka7ece032016-10-20 00:13:19 +00002853 !VD->isExternallyVisible(), nullptr, nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002854 FwdDeclReplaceMap.emplace_back(
2855 std::piecewise_construct,
2856 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2857 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002858 return GV;
2859}
2860
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002861llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002862 // We only need a declaration (not a definition) of the type - so use whatever
2863 // we would otherwise do to get a type for a pointee. (forward declarations in
2864 // limited debug info, full definitions (if the type definition is available)
2865 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00002866 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00002867 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002868 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002869 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002870
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002871 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002872 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002873
2874 // No definition for now. Emit a forward definition that might be
2875 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00002876 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00002877 return getFunctionForwardDeclaration(FD);
2878 else if (const auto *VD = dyn_cast<VarDecl>(D))
2879 return getGlobalVariableForwardDeclaration(VD);
2880
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002881 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002882}
2883
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002884llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002885 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002886 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002887
David Majnemer58ed0f32016-07-17 00:39:12 +00002888 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002889 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002890 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002891
2892 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002893 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002894
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002895 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002896 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002897 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002898 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002899 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002900 }
2901 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002903 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002904 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002905 return SP;
2906 }
2907
Aaron Ballman86c93902014-03-06 23:45:36 +00002908 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002909 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002911 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002912 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002913 return SP;
2914 }
2915 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002916 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002917}
2918
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002919// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002920// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002921llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002922 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002923 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002924 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002925 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2926 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002927 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002928
David Majnemer58ed0f32016-07-17 00:39:12 +00002929 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00002931
2932 const auto *FTy = FnType->getAs<FunctionType>();
2933 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
2934
David Majnemer58ed0f32016-07-17 00:39:12 +00002935 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002936 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002937 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002938
2939 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002940 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002941
2942 // Replace the instancetype keyword with the actual type.
2943 if (ResultTy == CGM.getContext().getObjCInstanceType())
2944 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002945 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002946
Adrian Prantl7bec9032013-05-10 21:08:31 +00002947 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002949 QualType SelfDeclTy;
2950 if (auto *SelfDecl = OMethod->getSelfDecl())
2951 SelfDeclTy = SelfDecl->getType();
2952 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2953 if (FPT->getNumParams() > 1)
2954 SelfDeclTy = FPT->getParamType(0);
2955 if (!SelfDeclTy.isNull())
2956 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002958 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002959 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00002961 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00002962 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002963 // Variadic methods need a special marker at the end of the type list.
2964 if (OMethod->isVariadic())
2965 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002966
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002967 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002968 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2969 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002971
Adrian Prantl800faef2014-02-25 23:42:18 +00002972 // Handle variadic function types; they need an additional
2973 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00002974 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00002975 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002976 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002977 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00002978 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2979 for (QualType ParamType : FPT->param_types())
2980 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00002981 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002982 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00002983 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
2984 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00002985 }
2986
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002987 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002988}
2989
Eric Christophere7b87e52014-10-26 23:40:33 +00002990void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2991 SourceLocation ScopeLoc, QualType FnType,
2992 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002993
2994 StringRef Name;
2995 StringRef LinkageName;
2996
2997 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2998
2999 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003000 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003001
Leny Kholodov80c047d2016-09-06 10:48:04 +00003002 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003003 llvm::DIFile *Unit = getOrCreateFile(Loc);
3004 llvm::DIScope *FDContext = Unit;
3005 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 if (!HasDecl) {
3007 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003008 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003009 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003010 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003011 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003013 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003014 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003015 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003016 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 return;
3018 }
3019 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003020 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3021 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003022 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003023 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003024 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 } else {
3026 // Use llvm function name.
3027 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003028 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003030 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 Name = Name.substr(1);
3032
Adrian Prantl42d71b92014-04-10 23:21:53 +00003033 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003034 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003035 // Artificial functions should not silently reuse CurLoc.
3036 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003037 }
3038 unsigned LineNo = getLineNumber(Loc);
3039 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003040
Eric Christopher8018e412014-03-27 18:50:35 +00003041 // FIXME: The function declaration we're constructing here is mostly reusing
3042 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3043 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3044 // all subprograms instead of the actual context since subprogram definitions
3045 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003046 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003047 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003048 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003049 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003050 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003051 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003052 // We might get here with a VarDecl in the case we're generating
3053 // code for the initialization of globals. Do not record these decls
3054 // as they will overwrite the actual VarDecl Decl in the cache.
3055 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003056 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003057
Adrian Prantlbebb8932014-03-21 21:01:58 +00003058 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003059 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003060
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003062 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003063}
3064
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003065void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3066 QualType FnType) {
3067 StringRef Name;
3068 StringRef LinkageName;
3069
3070 const Decl *D = GD.getDecl();
3071 if (!D)
3072 return;
3073
Leny Kholodov80c047d2016-09-06 10:48:04 +00003074 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003075 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003076 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003077 llvm::DINodeArray TParamsArray;
3078 if (isa<FunctionDecl>(D)) {
3079 // If there is a DISubprogram for this function available then use it.
3080 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3081 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003082 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003083 Name = getObjCMethodName(OMD);
3084 Flags |= llvm::DINode::FlagPrototyped;
3085 } else {
3086 llvm_unreachable("not a function or ObjC method");
3087 }
3088 if (!Name.empty() && Name[0] == '\01')
3089 Name = Name.substr(1);
3090
3091 if (D->isImplicit()) {
3092 Flags |= llvm::DINode::FlagArtificial;
3093 // Artificial functions without a location should not silently reuse CurLoc.
3094 if (Loc.isInvalid())
3095 CurLoc = SourceLocation();
3096 }
3097 unsigned LineNo = getLineNumber(Loc);
3098 unsigned ScopeLine = 0;
3099
Adrian Prantle76bda52016-04-15 15:55:45 +00003100 DBuilder.retainType(DBuilder.createFunction(
3101 FDContext, Name, LinkageName, Unit, LineNo,
3102 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3103 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3104 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003105}
3106
David Blaikie835afb22015-01-21 23:08:17 +00003107void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 // Update our current location
3109 setLocation(Loc);
3110
Eric Christophere7b87e52014-10-26 23:40:33 +00003111 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3112 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003113
Adrian Prantle83b1302014-01-07 22:05:52 +00003114 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003115 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00003116 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00003117}
3118
Guy Benyei11169dd2012-12-18 14:30:41 +00003119void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003120 llvm::MDNode *Back = nullptr;
3121 if (!LexicalBlockStack.empty())
3122 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003123 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003124 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003125 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003126}
3127
Eric Christopher0fdcb312013-05-16 00:52:20 +00003128void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3129 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 // Set our current location.
3131 setLocation(Loc);
3132
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00003134 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3135 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00003136
Benjamin Kramer8c305922016-02-02 11:06:51 +00003137 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003138 return;
3139
3140 // Create a new lexical block and push it on the stack.
3141 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003142}
3143
Eric Christopher0fdcb312013-05-16 00:52:20 +00003144void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3145 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3147
3148 // Provide an entry in the line table for the end of the block.
3149 EmitLocation(Builder, Loc);
3150
Benjamin Kramer8c305922016-02-02 11:06:51 +00003151 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003152 return;
3153
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 LexicalBlockStack.pop_back();
3155}
3156
Guy Benyei11169dd2012-12-18 14:30:41 +00003157void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
3158 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3159 unsigned RCount = FnBeginRegionCount.back();
3160 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3161
3162 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003163 while (LexicalBlockStack.size() != RCount) {
3164 // Provide an entry in the line table for the end of the block.
3165 EmitLocation(Builder, CurLoc);
3166 LexicalBlockStack.pop_back();
3167 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003168 FnBeginRegionCount.pop_back();
3169}
3170
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003171llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003172 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003173
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003174 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 QualType FType;
3176 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003177 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003178
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003179 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003180 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003181
3182 FieldOffset = 0;
3183 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3184 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3185 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3186 FType = CGM.getContext().IntTy;
3187 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3188 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3189
3190 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3191 if (HasCopyAndDispose) {
3192 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003193 EltTys.push_back(
3194 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3195 EltTys.push_back(
3196 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003197 }
3198 bool HasByrefExtendedLayout;
3199 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003200 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3201 HasByrefExtendedLayout) &&
3202 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003203 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003204 EltTys.push_back(
3205 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003206 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003207
Guy Benyei11169dd2012-12-18 14:30:41 +00003208 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3209 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003210 CGM.getTarget().getPointerAlign(0))) {
3211 CharUnits FieldOffsetInBytes =
3212 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003213 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003214 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003215
Guy Benyei11169dd2012-12-18 14:30:41 +00003216 if (NumPaddingBytes.isPositive()) {
3217 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3218 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3219 pad, ArrayType::Normal, 0);
3220 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3221 }
3222 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003223
Guy Benyei11169dd2012-12-18 14:30:41 +00003224 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003225 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003226 FieldSize = CGM.getContext().getTypeSize(FType);
3227 FieldAlign = CGM.getContext().toBits(Align);
3228
Eric Christopherb2a008c2013-05-16 00:45:12 +00003229 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003230 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003231 FieldAlign, FieldOffset,
3232 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003233 EltTys.push_back(FieldTy);
3234 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003235
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003236 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003237
Leny Kholodov80c047d2016-09-06 10:48:04 +00003238 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003239
Guy Benyei11169dd2012-12-18 14:30:41 +00003240 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003241 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003242}
3243
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003244void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3245 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003246 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003247 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003249 if (VD->hasAttr<NoDebugAttr>())
3250 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003251
David Blaikie7fceebf2013-08-19 03:37:48 +00003252 bool Unwritten =
3253 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3254 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003255 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003256 if (!Unwritten)
3257 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003258 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003259 uint64_t XOffset = 0;
3260 if (VD->hasAttr<BlocksAttr>())
3261 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003262 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003263 Ty = getOrCreateType(VD->getType(), Unit);
3264
3265 // If there is no debug info for this type then do not emit debug info
3266 // for this variable.
3267 if (!Ty)
3268 return;
3269
Guy Benyei11169dd2012-12-18 14:30:41 +00003270 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003271 unsigned Line = 0;
3272 unsigned Column = 0;
3273 if (!Unwritten) {
3274 Line = getLineNumber(VD->getLocation());
3275 Column = getColumnNumber(VD->getLocation());
3276 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003277 SmallVector<int64_t, 9> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003278 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003279 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003280 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003281
3282 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3283
Guy Benyei11169dd2012-12-18 14:30:41 +00003284 // If this is the first argument and it is implicit then
3285 // give it an object pointer flag.
3286 // FIXME: There has to be a better way to do this, but for static
3287 // functions there won't be an implicit param at arg1 and
3288 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003289 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003290 Flags |= llvm::DINode::FlagObjectPointer;
David Majnemer58ed0f32016-07-17 00:39:12 +00003291 if (auto *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00003292 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
3293 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003294 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00003295
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003296 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003297
3298 StringRef Name = VD->getName();
3299 if (!Name.empty()) {
3300 if (VD->hasAttr<BlocksAttr>()) {
3301 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003302 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 // offset of __forwarding field
3304 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003305 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003306 Expr.push_back(offset.getQuantity());
3307 Expr.push_back(llvm::dwarf::DW_OP_deref);
3308 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 // offset of x field
3310 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003311 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003312
3313 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003314 auto *D = ArgNo
3315 ? DBuilder.createParameterVariable(Scope, VD->getName(),
3316 *ArgNo, Unit, Line, Ty)
3317 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003318 Line, Ty, Align);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003319
Guy Benyei11169dd2012-12-18 14:30:41 +00003320 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003321 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3322 llvm::DebugLoc::get(Line, Column, Scope),
3323 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003324 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00003325 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003326 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Majnemer58ed0f32016-07-17 00:39:12 +00003327 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003328 // If VD is an anonymous union then Storage represents value for
3329 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003330 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003331 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003332 // GDB has trouble finding local variables in anonymous unions, so we emit
3333 // artifical local variables for each of the members.
3334 //
3335 // FIXME: Remove this code as soon as GDB supports this.
3336 // The debug info verifier in LLVM operates based on the assumption that a
3337 // variable has the same size as its storage and we had to disable the check
3338 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003339 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003340 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003341 StringRef FieldName = Field->getName();
3342
3343 // Ignore unnamed fields. Do not ignore unnamed records.
3344 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3345 continue;
3346
3347 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003348 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003349 auto *D = DBuilder.createAutoVariable(
3350 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003351 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003352
3353 // Insert an llvm.dbg.declare into the current block.
3354 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3355 llvm::DebugLoc::get(Line, Column, Scope),
3356 Builder.GetInsertBlock());
3357 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003358 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003359 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003360
3361 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003362 auto *D = ArgNo
3363 ? DBuilder.createParameterVariable(
3364 Scope, Name, *ArgNo, Unit, Line, Ty,
3365 CGM.getLangOpts().Optimize, Flags)
3366 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3367 CGM.getLangOpts().Optimize, Flags,
3368 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003369
3370 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003371 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3372 llvm::DebugLoc::get(Line, Column, Scope),
3373 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003374}
3375
3376void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3377 llvm::Value *Storage,
3378 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003379 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003380 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003381}
3382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003383llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3384 llvm::DIType *Ty) {
3385 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003386 if (CachedTy)
3387 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003388 return DBuilder.createObjectPointerType(Ty);
3389}
3390
Eric Christophere7b87e52014-10-26 23:40:33 +00003391void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3392 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003393 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003394 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003396
Craig Topper8a13c412014-05-21 05:09:00 +00003397 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003399 if (VD->hasAttr<NoDebugAttr>())
3400 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003401
Guy Benyei11169dd2012-12-18 14:30:41 +00003402 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003403
Guy Benyei11169dd2012-12-18 14:30:41 +00003404 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003405 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3406 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 if (isByRef)
3408 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003409 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003410 Ty = getOrCreateType(VD->getType(), Unit);
3411
3412 // Self is passed along as an implicit non-arg variable in a
3413 // block. Mark it as the object pointer.
3414 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003415 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003416
3417 // Get location information.
3418 unsigned Line = getLineNumber(VD->getLocation());
3419 unsigned Column = getColumnNumber(VD->getLocation());
3420
3421 const llvm::DataLayout &target = CGM.getDataLayout();
3422
3423 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003424 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003425 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3426
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003427 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003428 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003429 addr.push_back(llvm::dwarf::DW_OP_deref);
3430 addr.push_back(llvm::dwarf::DW_OP_plus);
3431 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003432 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003433 addr.push_back(llvm::dwarf::DW_OP_deref);
3434 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003435 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003436 offset =
3437 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003438 addr.push_back(offset.getQuantity());
3439 addr.push_back(llvm::dwarf::DW_OP_deref);
3440 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003441 // offset of x field
3442 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003443 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003444 }
3445
3446 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003447 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003448 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003449 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003450 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003451
Guy Benyei11169dd2012-12-18 14:30:41 +00003452 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003453 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3454 if (InsertPoint)
3455 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3456 InsertPoint);
3457 else
3458 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3459 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003460}
3461
Guy Benyei11169dd2012-12-18 14:30:41 +00003462void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3463 unsigned ArgNo,
3464 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003465 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003466 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003467}
3468
3469namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003470struct BlockLayoutChunk {
3471 uint64_t OffsetInBits;
3472 const BlockDecl::Capture *Capture;
3473};
3474bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3475 return l.OffsetInBits < r.OffsetInBits;
3476}
Guy Benyei11169dd2012-12-18 14:30:41 +00003477}
3478
3479void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003480 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003481 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003482 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003484 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003485 ASTContext &C = CGM.getContext();
3486 const BlockDecl *blockDecl = block.getBlockDecl();
3487
3488 // Collect some general information about the block's location.
3489 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003490 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003491 unsigned line = getLineNumber(loc);
3492 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003493
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003495 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003496
3497 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003498 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003499
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003500 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003501 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003502 blockLayout->getElementOffsetInBits(0),
3503 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003504 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003505 blockLayout->getElementOffsetInBits(1),
3506 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003507 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003508 blockLayout->getElementOffsetInBits(2),
3509 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003510 auto *FnTy = block.getBlockExpr()->getFunctionType();
3511 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003512 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003513 blockLayout->getElementOffsetInBits(3),
3514 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003515 fields.push_back(createFieldType(
3516 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3517 ? C.getBlockDescriptorExtendedType()
3518 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003519 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003520
3521 // We want to sort the captures by offset, not because DWARF
3522 // requires this, but because we're paranoid about debuggers.
3523 SmallVector<BlockLayoutChunk, 8> chunks;
3524
3525 // 'this' capture.
3526 if (blockDecl->capturesCXXThis()) {
3527 BlockLayoutChunk chunk;
3528 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003529 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003530 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003531 chunks.push_back(chunk);
3532 }
3533
3534 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003535 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003536 const VarDecl *variable = capture.getVariable();
3537 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3538
3539 // Ignore constant captures.
3540 if (captureInfo.isConstant())
3541 continue;
3542
3543 BlockLayoutChunk chunk;
3544 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003545 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003546 chunk.Capture = &capture;
3547 chunks.push_back(chunk);
3548 }
3549
3550 // Sort by offset.
3551 llvm::array_pod_sort(chunks.begin(), chunks.end());
3552
David Majnemer58ed0f32016-07-17 00:39:12 +00003553 for (const BlockLayoutChunk &Chunk : chunks) {
3554 uint64_t offsetInBits = Chunk.OffsetInBits;
3555 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003556
3557 // If we have a null capture, this must be the C++ 'this' capture.
3558 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003559 QualType type;
3560 if (auto *Method =
3561 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3562 type = Method->getThisType(C);
3563 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3564 type = QualType(RDecl->getTypeForDecl(), 0);
3565 else
3566 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003567
David Majnemerb4b671e2016-06-30 03:01:59 +00003568 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003569 offsetInBits, tunit, tunit));
3570 continue;
3571 }
3572
3573 const VarDecl *variable = capture->getVariable();
3574 StringRef name = variable->getName();
3575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003576 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003578 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003579 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003580
3581 // FIXME: this creates a second copy of this type!
3582 uint64_t xoffset;
3583 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003584 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003585 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3586 PtrInfo.Width, Align, offsetInBits,
3587 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003588 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003589 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003590 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003591 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003592 }
3593 fields.push_back(fieldType);
3594 }
3595
3596 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003597 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3598 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003599
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003600 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003601
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003602 llvm::DIType *type =
3603 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003604 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003605 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003606 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3607
3608 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003609 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003610 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003611
3612 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003613 auto *debugVar = DBuilder.createParameterVariable(
3614 scope, Arg->getName(), ArgNo, tunit, line, type,
3615 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003616
Adrian Prantl616bef42013-03-14 21:52:59 +00003617 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003618 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003619 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003620 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003621 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003622 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003623
Adrian Prantl616bef42013-03-14 21:52:59 +00003624 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003625 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3626 llvm::DebugLoc::get(line, column, scope),
3627 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003628}
3629
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003630llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003631CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3632 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003633 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003634
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003635 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003636 if (MI != StaticDataMemberCache.end()) {
3637 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003638 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003639 }
David Blaikiece763042013-08-20 21:49:21 +00003640
3641 // If the member wasn't found in the cache, lazily construct and add it to the
3642 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003643 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003644 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003645 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003646}
3647
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003648llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3649 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3650 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3651 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003652
3653 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003654 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003655 StringRef FieldName = Field->getName();
3656
3657 // Ignore unnamed fields, but recurse into anonymous records.
3658 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003659 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Eric Christophercab9fae2014-04-10 05:20:00 +00003660 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3661 Var, DContext);
3662 continue;
3663 }
3664 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003665 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003666 LineNo, FieldTy, Var->hasLocalLinkage());
3667 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003668 }
3669 return GV;
3670}
3671
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003672void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003673 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003674 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003675 if (D->hasAttr<NoDebugAttr>())
3676 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003677
3678 // If we already created a DIGlobalVariable for this declaration, just attach
3679 // it to the llvm::GlobalVariable.
3680 auto Cached = DeclCache.find(D->getCanonicalDecl());
3681 if (Cached != DeclCache.end())
3682 return Var->addDebugInfo(cast<llvm::DIGlobalVariable>(Cached->second));
3683
Guy Benyei11169dd2012-12-18 14:30:41 +00003684 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003685 llvm::DIFile *Unit = nullptr;
3686 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003687 unsigned LineNo;
3688 StringRef DeclName, LinkageName;
3689 QualType T;
3690 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003691
3692 // Attempt to store one global variable for the declaration - even if we
3693 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003694 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003695
3696 // If this is an anonymous union then we'll want to emit a global
3697 // variable for each member of the anonymous union so that it's possible
3698 // to find the name of any field in the union.
3699 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003700 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003701 assert(RD->isAnonymousStructOrUnion() &&
3702 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003703 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3704 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003705 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
David Blaikie7550b112014-10-20 17:42:23 +00003706 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003707 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003708 Var->hasLocalLinkage(), /*Expr=*/nullptr,
Victor Leschuka7ece032016-10-20 00:13:19 +00003709 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003710 Var->addDebugInfo(GV);
Eric Christophercab9fae2014-04-10 05:20:00 +00003711 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003712 DeclCache[D->getCanonicalDecl()].reset(GV);
Guy Benyei11169dd2012-12-18 14:30:41 +00003713}
3714
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003715void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003716 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003717 if (VD->hasAttr<NoDebugAttr>())
3718 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003719 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003720 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003721 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003722 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003723 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003724 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3725 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003726 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3727 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3728 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003729 // Do not use global variables for enums.
3730 //
3731 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003732 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003733 return;
David Blaikiea15565562014-04-04 20:56:17 +00003734 // Do not emit separate definitions for function local const/statics.
3735 if (isa<FunctionDecl>(VD->getDeclContext()))
3736 return;
David Blaikiebb113912014-04-05 07:23:17 +00003737 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003738 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003739 if (VarD->isStaticDataMember()) {
3740 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003741 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003742 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003743 //
3744 // FIXME: This is probably unnecessary, since Ty should reference RD
3745 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003746 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003747 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003748 return;
3749 }
3750
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003751 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003752
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003753 auto &GV = DeclCache[VD];
3754 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003755 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003756 llvm::DIExpression *InitExpr = nullptr;
3757 if (Init.isInt())
3758 InitExpr =
3759 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003760 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003761 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003762 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3763 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003764}
3765
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003766llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003767 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003768 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003769 llvm::DIScope *Mod = getParentModuleOrNull(D);
3770 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003771}
3772
David Blaikie9f88fe82013-04-22 06:13:21 +00003773void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003774 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003775 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003776 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003777 if (!NSDecl->isAnonymousNamespace() ||
3778 CGM.getCodeGenOpts().DebugExplicitImport) {
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003779 DBuilder.createImportedModule(
3780 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3781 getOrCreateNameSpace(NSDecl),
3782 getLineNumber(UD.getLocation()));
3783 }
David Blaikie9f88fe82013-04-22 06:13:21 +00003784}
3785
David Blaikiebd483762013-05-20 04:58:53 +00003786void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003787 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003788 return;
3789 assert(UD.shadow_size() &&
3790 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3791 // Emitting one decl is sufficient - debuggers can detect that this is an
3792 // overloaded name & provide lookup for all the overloads.
3793 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00003794
3795 // FIXME: Skip functions with undeduced auto return type for now since we
3796 // don't currently have the plumbing for separate declarations & definitions
3797 // of free functions and mismatched types (auto in the declaration, concrete
3798 // return type in the definition)
3799 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
3800 if (const auto *AT =
3801 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
3802 if (AT->getDeducedType().isNull())
3803 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003804 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003805 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003806 DBuilder.createImportedDeclaration(
3807 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3808 getLineNumber(USD.getLocation()));
3809}
3810
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003811void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00003812 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
3813 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00003814 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00003815 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl8a634c12015-12-18 19:44:31 +00003816 DBuilder.createImportedDeclaration(
3817 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3818 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3819 getLineNumber(ID.getLocation()));
3820 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003821}
3822
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003823llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003824CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003825 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003826 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003827 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003828 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003829 return cast<llvm::DIImportedEntity>(VH);
3830 llvm::DIImportedEntity *R;
David Majnemer58ed0f32016-07-17 00:39:12 +00003831 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00003832 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3833 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003834 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003835 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3836 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3837 NA.getName());
3838 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003839 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003840 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3841 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3842 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003843 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003844 return R;
3845}
3846
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003847llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003848CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003849 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003850 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003851 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003852 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003853
Guy Benyei11169dd2012-12-18 14:30:41 +00003854 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003855 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003856 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantlbd87eb42016-11-03 19:42:14 +00003857 llvm::DINamespace *NS = DBuilder.createNameSpace(
3858 Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003859 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003860 return NS;
3861}
3862
Adrian Prantla5206ce2015-09-22 23:26:43 +00003863void CGDebugInfo::setDwoId(uint64_t Signature) {
3864 assert(TheCU && "no main compile unit");
3865 TheCU->setDWOId(Signature);
3866}
3867
3868
Guy Benyei11169dd2012-12-18 14:30:41 +00003869void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003870 // Creating types might create further types - invalidating the current
3871 // element and the size(), so don't cache/reference them.
3872 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3873 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003874 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003875 ? CreateTypeDefinition(E.Type, E.Unit)
3876 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003877 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003878 }
3879
David Blaikief427b002014-05-06 03:42:01 +00003880 for (auto p : ReplaceMap) {
3881 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003882 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003883 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003884
David Blaikief427b002014-05-06 03:42:01 +00003885 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003886 assert(it != TypeCache.end());
3887 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003888
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003889 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3890 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003891 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003892
Frederic Rissd253ed62014-11-18 03:40:51 +00003893 for (const auto &p : FwdDeclReplaceMap) {
3894 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003895 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003896 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003897
3898 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003899 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003900 // with ourselves, that will destroy the temporary MDNode and
3901 // replace it with a standard one, avoiding leaking memory.
3902 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003903 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003904 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003905 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003906
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003907 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003908 }
3909
Adrian Prantl73409ce2013-03-11 18:33:46 +00003910 // We keep our own list of retained types, because we need to look
3911 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003912 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003913 if (auto MD = TypeCache[RT])
3914 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003915
Guy Benyei11169dd2012-12-18 14:30:41 +00003916 DBuilder.finalize();
3917}
David Blaikie66088d52014-09-24 17:01:27 +00003918
3919void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003920 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00003921 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003922
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003923 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003924 // Don't ignore in case of explicit cast where it is referenced indirectly.
3925 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003926}