blob: a052ffb617349e9ce06505de35c94c90ce2d421a [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
John McCallde0fe072017-08-15 21:42:52 +000021#include "ConstantEmitter.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/RecordLayout.h"
28#include "clang/Basic/FileManager.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000031#include "clang/Frontend/CodeGenOptions.h"
Taewook Oh0fb5b782017-08-16 19:36:24 +000032#include "clang/Frontend/FrontendOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000034#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000035#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000036#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000037#include "llvm/ADT/SmallVector.h"
38#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000039#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000046#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000047#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000048using namespace clang;
49using namespace clang::CodeGen;
50
Victor Leschuka7ece032016-10-20 00:13:19 +000051static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
52 auto TI = Ctx.getTypeInfo(Ty);
53 return TI.AlignIsRequired ? TI.Align : 0;
54}
55
56static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
57 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
58}
59
60static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
61 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
62}
63
Guy Benyei11169dd2012-12-18 14:30:41 +000064CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000066 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000067 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000068 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
69 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000070 CreateCompileUnit();
71}
72
73CGDebugInfo::~CGDebugInfo() {
74 assert(LexicalBlockStack.empty() &&
75 "Region stack mismatch, stack not empty!");
76}
77
David Blaikie66e41972015-01-14 07:38:27 +000078ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000079 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000080 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000081 init(TemporaryLocation);
82}
83
Adrian Prantl39428e72015-02-03 18:40:42 +000084ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000085 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000086 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000087 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000088 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000089}
90
91void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000092 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000093 auto *DI = CGF->getDebugInfo();
94 if (!DI) {
95 CGF = nullptr;
96 return;
David Blaikie66e41972015-01-14 07:38:27 +000097 }
David Blaikied7057d92015-08-12 23:49:57 +000098
99 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
Bob Haarmanc6c9b8f2017-09-11 22:11:57 +0000100
101 if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
102 return;
103
David Blaikied7057d92015-08-12 23:49:57 +0000104 if (TemporaryLocation.isValid()) {
105 DI->EmitLocation(CGF->Builder, TemporaryLocation);
106 return;
107 }
108
109 if (DefaultToEmpty) {
110 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
111 return;
112 }
113
114 // Construct a location that has a valid scope, but no line info.
115 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000116 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
117 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000118}
119
David Blaikie9b479662015-01-25 01:19:10 +0000120ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000121 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000122 init(E->getExprLoc());
123}
124
David Blaikie66e41972015-01-14 07:38:27 +0000125ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000126 : CGF(&CGF) {
127 if (!CGF.getDebugInfo()) {
128 this->CGF = nullptr;
129 return;
David Blaikie66e41972015-01-14 07:38:27 +0000130 }
David Blaikied7057d92015-08-12 23:49:57 +0000131 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
132 if (Loc)
133 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000134}
135
136ApplyDebugLocation::~ApplyDebugLocation() {
137 // Query CGF so the location isn't overwritten when location updates are
138 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000139 if (CGF)
140 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000141}
142
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000143ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
144 GlobalDecl InlinedFn)
145 : CGF(&CGF) {
146 if (!CGF.getDebugInfo()) {
147 this->CGF = nullptr;
148 return;
149 }
150 auto &DI = *CGF.getDebugInfo();
151 SavedLocation = DI.getLocation();
152 assert((DI.getInlinedAt() ==
153 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
154 "CGDebugInfo and IRBuilder are out of sync");
155
156 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
157}
158
159ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
160 if (!CGF)
161 return;
162 auto &DI = *CGF->getDebugInfo();
163 DI.EmitInlineFunctionEnd(CGF->Builder);
164 DI.EmitLocation(CGF->Builder, SavedLocation);
165}
166
Guy Benyei11169dd2012-12-18 14:30:41 +0000167void CGDebugInfo::setLocation(SourceLocation Loc) {
168 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000169 if (Loc.isInvalid())
170 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000171
172 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
173
174 // If we've changed files in the middle of a lexical scope go ahead
175 // and create a new lexical scope with file node if it's different
176 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 if (LexicalBlockStack.empty())
178 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179
180 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000181 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000182 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000184 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000185 return;
186
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000187 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000188 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000189 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
190 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000191 } else if (isa<llvm::DILexicalBlock>(Scope) ||
192 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000193 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000194 LexicalBlockStack.emplace_back(
195 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000196 }
197}
198
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000199llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000200 llvm::DIScope *Mod = getParentModuleOrNull(D);
201 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
202 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000203}
204
205llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
206 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000207 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000208 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000209
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000210 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000211 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000212 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000213 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000214 }
215
216 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000217 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
218 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000219
David Majnemer58ed0f32016-07-17 00:39:12 +0000220 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000221 if (!RDecl->isDependentType())
222 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000223 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000224 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000225}
226
Reid Kleckner59d12202017-08-08 01:33:53 +0000227PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
228 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
229
230 // If we're emitting codeview, it's important to try to match MSVC's naming so
231 // that visualizers written for MSVC will trigger for our class names. In
232 // particular, we can't have spaces between arguments of standard templates
233 // like basic_string and vector.
234 if (CGM.getCodeGenOpts().EmitCodeView)
235 PP.MSVCFormatting = true;
236
237 return PP;
238}
239
Guy Benyei11169dd2012-12-18 14:30:41 +0000240StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000242 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000243 FunctionTemplateSpecializationInfo *Info =
244 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000245
Reid Kleckner31abd802016-06-30 17:41:31 +0000246 // Emit the unqualified name in normal operation. LLVM and the debugger can
247 // compute the fully qualified name from the scope chain. If we're only
248 // emitting line table info, there won't be any scope chains, so emit the
249 // fully qualified name here so that stack traces are more accurate.
250 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
251 // evaluating the size impact.
252 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
253 CGM.getCodeGenOpts().EmitCodeView;
254
255 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000256 return FII->getName();
257
Benjamin Kramer9170e912013-02-22 15:46:01 +0000258 SmallString<128> NS;
259 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000260 if (!UseQualifiedName)
261 FD->printName(OS);
262 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000263 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000264
Reid Kleckner829398e2016-06-17 16:11:20 +0000265 // Add any template specialization args.
266 if (Info) {
267 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000268 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner59d12202017-08-08 01:33:53 +0000269 getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000270 }
271
272 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000273 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000274}
275
276StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
277 SmallString<256> MethodName;
278 llvm::raw_svector_ostream OS(MethodName);
279 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
280 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000281 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000282 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000283 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000284 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000285 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000286 if (OC->IsClassExtension()) {
287 OS << OC->getClassInterface()->getName();
288 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000289 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000290 << OC->getIdentifier()->getNameStart() << ')';
291 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000292 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000293 OS << OCD->getClassInterface()->getName() << '('
294 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000295 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000296 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000297 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000298 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000299 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000300 ClassTy.print(OS, PrintingPolicy(LangOptions()));
301 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000302 }
303 OS << ' ' << OMD->getSelector().getAsString() << ']';
304
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000305 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000306}
307
Guy Benyei11169dd2012-12-18 14:30:41 +0000308StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000309 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000310}
311
Eric Christophere7b87e52014-10-26 23:40:33 +0000312StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000313 if (isa<ClassTemplateSpecializationDecl>(RD)) {
314 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000315 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000316 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000317 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000318
319 // Copy this name on the side and use its reference.
320 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000321 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000322
David Majnemerbc5976a2016-07-01 23:12:54 +0000323 // quick optimization to avoid having to intern strings that are already
324 // stored reliably elsewhere
325 if (const IdentifierInfo *II = RD->getIdentifier())
326 return II->getName();
327
328 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
329 // used to reconstruct the fully qualified type names.
330 if (CGM.getCodeGenOpts().EmitCodeView) {
331 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
332 assert(RD->getDeclContext() == D->getDeclContext() &&
333 "Typedef should not be in another decl context!");
334 assert(D->getDeclName().getAsIdentifierInfo() &&
335 "Typedef was not named!");
336 return D->getDeclName().getAsIdentifierInfo()->getName();
337 }
338
339 if (CGM.getLangOpts().CPlusPlus) {
340 StringRef Name;
341
342 ASTContext &Context = CGM.getContext();
343 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
344 // Anonymous types without a name for linkage purposes have their
345 // declarator mangled in if they have one.
346 Name = DD->getName();
347 else if (const TypedefNameDecl *TND =
348 Context.getTypedefNameForUnnamedTagDecl(RD))
349 // Anonymous types without a name for linkage purposes have their
350 // associate typedef mangled in if they have one.
351 Name = TND->getName();
352
353 if (!Name.empty()) {
354 SmallString<256> UnnamedType("<unnamed-type-");
355 UnnamedType += Name;
356 UnnamedType += '>';
357 return internString(UnnamedType);
358 }
359 }
360 }
361
362 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000363}
364
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000365llvm::DIFile::ChecksumKind
366CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
367 Checksum.clear();
368
369 if (!CGM.getCodeGenOpts().EmitCodeView)
370 return llvm::DIFile::CSK_None;
371
372 SourceManager &SM = CGM.getContext().getSourceManager();
373 bool Invalid;
374 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
375 if (Invalid)
376 return llvm::DIFile::CSK_None;
377
378 llvm::MD5 Hash;
379 llvm::MD5::MD5Result Result;
380
381 Hash.update(MemBuffer->getBuffer());
382 Hash.final(Result);
383
384 Hash.stringifyResult(Result, Checksum);
385 return llvm::DIFile::CSK_MD5;
386}
387
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000388llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000389 if (!Loc.isValid())
390 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000391 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000392 remapDIPath(TheCU->getDirectory()),
393 TheCU->getFile()->getChecksumKind(),
394 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000395
396 SourceManager &SM = CGM.getContext().getSourceManager();
397 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
398
399 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
400 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000401 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000402 remapDIPath(TheCU->getDirectory()),
403 TheCU->getFile()->getChecksumKind(),
404 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000405
406 // Cache the results.
407 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000408 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000409
410 if (it != DIFileCache.end()) {
411 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000412 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000413 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000414 }
415
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000416 SmallString<32> Checksum;
417 llvm::DIFile::ChecksumKind CSKind =
418 computeChecksum(SM.getFileID(Loc), Checksum);
419
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000420 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000421 remapDIPath(getCurrentDirname()),
422 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000423
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000424 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 return F;
426}
427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000428llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000429 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000430 remapDIPath(TheCU->getDirectory()),
431 TheCU->getFile()->getChecksumKind(),
432 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000433}
434
435std::string CGDebugInfo::remapDIPath(StringRef Path) const {
436 for (const auto &Entry : DebugPrefixMap)
437 if (Path.startswith(Entry.first))
438 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
439 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000440}
441
Guy Benyei11169dd2012-12-18 14:30:41 +0000442unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
443 if (Loc.isInvalid() && CurLoc.isInvalid())
444 return 0;
445 SourceManager &SM = CGM.getContext().getSourceManager();
446 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000447 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000448}
449
Adrian Prantlc7822422013-03-12 20:43:25 +0000450unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000451 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000452 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000453 return 0;
454
455 // If the location is invalid then use the current column.
456 if (Loc.isInvalid() && CurLoc.isInvalid())
457 return 0;
458 SourceManager &SM = CGM.getContext().getSourceManager();
459 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000460 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000461}
462
463StringRef CGDebugInfo::getCurrentDirname() {
464 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
465 return CGM.getCodeGenOpts().DebugCompilationDir;
466
467 if (!CWDName.empty())
468 return CWDName;
469 SmallString<256> CWD;
470 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000471 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000472}
473
Guy Benyei11169dd2012-12-18 14:30:41 +0000474void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000475 SmallString<32> Checksum;
476 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000477
David Blaikieaabde052014-05-14 00:29:00 +0000478 // Should we be asking the SourceManager for the main file name, instead of
479 // accepting it as an argument? This just causes the main file name to
480 // mismatch with source locations and create extra lexical scopes or
481 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
482 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
483 // because that's what the SourceManager says)
484
Guy Benyei11169dd2012-12-18 14:30:41 +0000485 // Get absolute path name.
486 SourceManager &SM = CGM.getContext().getSourceManager();
487 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
488 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000489 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000490
491 // The main file name provided via the "-main-file-name" option contains just
492 // the file name itself with no path information. This file name may have had
493 // a relative path, so we look into the actual file entry for the main
494 // file to determine the real absolute path for the file.
495 std::string MainFileDir;
496 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000497 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000498 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000499 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
500 llvm::sys::path::append(MainFileDirSS, MainFileName);
501 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000502 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000503 // If the main file name provided is identical to the input file name, and
504 // if the input file is a preprocessed source, use the module name for
505 // debug info. The module name comes from the name specified in the first
506 // linemarker if the input is a preprocessed source.
507 if (MainFile->getName() == MainFileName &&
508 FrontendOptions::getInputKindForExtension(
509 MainFile->getName().rsplit('.').second)
510 .isPreprocessed())
511 MainFileName = CGM.getModule().getName().str();
512
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000513 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000514 }
515
Ed Masteda706022014-05-07 12:49:30 +0000516 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 const LangOptions &LO = CGM.getLangOpts();
518 if (LO.CPlusPlus) {
519 if (LO.ObjC1)
520 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
521 else
522 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
523 } else if (LO.ObjC1) {
524 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000525 } else if (LO.RenderScript) {
526 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 } else if (LO.C99) {
528 LangTag = llvm::dwarf::DW_LANG_C99;
529 } else {
530 LangTag = llvm::dwarf::DW_LANG_C89;
531 }
532
533 std::string Producer = getClangFullVersion();
534
535 // Figure out which version of the ObjC runtime we have.
536 unsigned RuntimeVers = 0;
537 if (LO.ObjC1)
538 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
539
Adrian Prantl826824e2016-04-08 22:43:06 +0000540 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
541 switch (DebugKind) {
542 case codegenoptions::NoDebugInfo:
543 case codegenoptions::LocTrackingOnly:
544 EmissionKind = llvm::DICompileUnit::NoDebug;
545 break;
546 case codegenoptions::DebugLineTablesOnly:
547 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
548 break;
549 case codegenoptions::LimitedDebugInfo:
550 case codegenoptions::FullDebugInfo:
551 EmissionKind = llvm::DICompileUnit::FullDebug;
552 break;
553 }
554
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000556 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000557 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000558 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000559 LangTag,
560 DBuilder.createFile(remapDIPath(MainFileName),
561 remapDIPath(getCurrentDirname()), CSKind, Checksum),
Adrian Prantlb4423022017-08-04 23:08:57 +0000562 Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
563 CGOpts.DwarfDebugFlags, RuntimeVers,
564 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000565 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
566 CGOpts.GnuPubnames);
Guy Benyei11169dd2012-12-18 14:30:41 +0000567}
568
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000569llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000570 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 StringRef BTName;
572 switch (BT->getKind()) {
573#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000574#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000575#include "clang/AST/BuiltinTypes.def"
576 case BuiltinType::Dependent:
577 llvm_unreachable("Unexpected builtin type");
578 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000579 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000581 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000583 if (!ClassTy)
584 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
585 "objc_class", TheCU,
586 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000587 return ClassTy;
588 case BuiltinType::ObjCId: {
589 // typedef struct objc_class *Class;
590 // typedef struct objc_object {
591 // Class isa;
592 // } *id;
593
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000594 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000595 return ObjTy;
596
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000597 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000598 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
599 "objc_class", TheCU,
600 getOrCreateMainFile(), 0);
601
602 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000603
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000604 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000605
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000606 ObjTy = DBuilder.createStructType(
607 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
608 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000609
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000610 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000611 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
612 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
613 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000614 return ObjTy;
615 }
616 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000617 if (!SelTy)
618 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
619 "objc_selector", TheCU,
620 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000621 return SelTy;
622 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000623
Alexey Bader954ba212016-04-08 13:40:33 +0000624#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
625 case BuiltinType::Id: \
626 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
627 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000628#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000629 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000630 return getOrCreateStructPtrType("opencl_sampler_t",
631 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000632 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000633 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000634 case BuiltinType::OCLClkEvent:
635 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
636 case BuiltinType::OCLQueue:
637 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000638 case BuiltinType::OCLReserveID:
639 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000640
Guy Benyei11169dd2012-12-18 14:30:41 +0000641 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000642 case BuiltinType::Char_U:
643 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
644 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000645 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000646 case BuiltinType::SChar:
647 Encoding = llvm::dwarf::DW_ATE_signed_char;
648 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000649 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000650 case BuiltinType::Char32:
651 Encoding = llvm::dwarf::DW_ATE_UTF;
652 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000653 case BuiltinType::UShort:
654 case BuiltinType::UInt:
655 case BuiltinType::UInt128:
656 case BuiltinType::ULong:
657 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000658 case BuiltinType::ULongLong:
659 Encoding = llvm::dwarf::DW_ATE_unsigned;
660 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000661 case BuiltinType::Short:
662 case BuiltinType::Int:
663 case BuiltinType::Int128:
664 case BuiltinType::Long:
665 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000666 case BuiltinType::LongLong:
667 Encoding = llvm::dwarf::DW_ATE_signed;
668 break;
669 case BuiltinType::Bool:
670 Encoding = llvm::dwarf::DW_ATE_boolean;
671 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 case BuiltinType::Half:
673 case BuiltinType::Float:
674 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000675 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000676 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000677 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000678 // FIXME: For targets where long double and __float128 have the same size,
679 // they are currently indistinguishable in the debugger without some
680 // special treatment. However, there is currently no consensus on encoding
681 // and this should be updated once a DWARF encoding exists for distinct
682 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000683 Encoding = llvm::dwarf::DW_ATE_float;
684 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000685 }
686
687 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000688 case BuiltinType::Long:
689 BTName = "long int";
690 break;
691 case BuiltinType::LongLong:
692 BTName = "long long int";
693 break;
694 case BuiltinType::ULong:
695 BTName = "long unsigned int";
696 break;
697 case BuiltinType::ULongLong:
698 BTName = "long long unsigned int";
699 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 default:
701 BTName = BT->getName(CGM.getLangOpts());
702 break;
703 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000704 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000705 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000706 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000707}
708
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000709llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000710 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000711 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 if (Ty->isComplexIntegerType())
713 Encoding = llvm::dwarf::DW_ATE_lo_user;
714
715 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000716 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717}
718
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000719llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
720 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 QualifierCollector Qc;
722 const Type *T = Qc.strip(Ty);
723
724 // Ignore these qualifiers for now.
725 Qc.removeObjCGCAttr();
726 Qc.removeAddressSpace();
727 Qc.removeObjCLifetime();
728
729 // We will create one Derived type for one qualifier and recurse to handle any
730 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000731 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 if (Qc.hasConst()) {
733 Tag = llvm::dwarf::DW_TAG_const_type;
734 Qc.removeConst();
735 } else if (Qc.hasVolatile()) {
736 Tag = llvm::dwarf::DW_TAG_volatile_type;
737 Qc.removeVolatile();
738 } else if (Qc.hasRestrict()) {
739 Tag = llvm::dwarf::DW_TAG_restrict_type;
740 Qc.removeRestrict();
741 } else {
742 assert(Qc.empty() && "Unknown type qualifier for debug info");
743 return getOrCreateType(QualType(T, 0), Unit);
744 }
745
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000746 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000747
748 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
749 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000750 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000751}
752
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000753llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
754 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000755
756 // The frontend treats 'id' as a typedef to an ObjCObjectType,
757 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
758 // debug info, we want to emit 'id' in both cases.
759 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000760 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000761
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000762 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
763 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000764}
765
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000766llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
767 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000768 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000769 Ty->getPointeeType(), Unit);
770}
771
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000772/// \return whether a C++ mangling exists for the type defined by TD.
773static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
774 switch (TheCU->getSourceLanguage()) {
775 case llvm::dwarf::DW_LANG_C_plus_plus:
776 return true;
777 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
778 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
779 default:
780 return false;
781 }
782}
783
Manman Rene0064d82013-08-29 23:19:58 +0000784/// In C++ mode, types have linkage, so we can rely on the ODR and
785/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000786static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
787 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000788 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000789 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000790 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000791
792 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000793 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000794
Manman Rene0064d82013-08-29 23:19:58 +0000795 // TODO: This is using the RTTI name. Is there a better way to get
796 // a unique string for a type?
797 llvm::raw_svector_ostream Out(FullName);
798 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000799 return FullName;
800}
801
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000802/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000803static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
804 llvm::dwarf::Tag Tag;
805 if (RD->isStruct() || RD->isInterface())
806 Tag = llvm::dwarf::DW_TAG_structure_type;
807 else if (RD->isUnion())
808 Tag = llvm::dwarf::DW_TAG_union_type;
809 else {
810 // FIXME: This could be a struct type giving a default visibility different
811 // than C++ class type, but needs llvm metadata changes first.
812 assert(RD->isClass());
813 Tag = llvm::dwarf::DW_TAG_class_type;
814 }
815 return Tag;
816}
817
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000818llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000819CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000820 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000821 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000822 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
823 return cast<llvm::DICompositeType>(T);
824 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 unsigned Line = getLineNumber(RD->getLocation());
826 StringRef RDName = getClassName(RD);
827
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000828 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000829 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000830
Guy Benyei11169dd2012-12-18 14:30:41 +0000831 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000832 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000833 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000834 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000835 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000836 ReplaceMap.emplace_back(
837 std::piecewise_construct, std::make_tuple(Ty),
838 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000839 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000840}
841
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000842llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000843 const Type *Ty,
844 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 // Bit size, align and offset of the type.
847 // Size is always the size of a pointer. We can't use getTypeSize here
848 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000849 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
850 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000851 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000852 Optional<unsigned> DWARFAddressSpace =
853 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000854
Keno Fischer87842f32015-11-16 09:04:13 +0000855 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
856 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
857 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000858 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000859 else
860 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000861 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000862}
863
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000864llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
865 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000866 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000867 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000868 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
869 TheCU, getOrCreateMainFile(), 0);
870 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
871 Cache = DBuilder.createPointerType(Cache, Size);
872 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000873}
874
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
876 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000877 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000878 QualType FType;
879 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000880 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000881 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000882
883 FieldOffset = 0;
884 FType = CGM.getContext().UnsignedLongTy;
885 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
886 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
887
888 Elements = DBuilder.getOrCreateArray(EltTys);
889 EltTys.clear();
890
Leny Kholodov80c047d2016-09-06 10:48:04 +0000891 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000892 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000893
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000894 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000895 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000896 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000897
898 // Bit size, align and offset of the type.
899 uint64_t Size = CGM.getContext().getTypeSize(Ty);
900
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000901 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000902
903 FieldOffset = 0;
904 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
905 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
906 FType = CGM.getContext().IntTy;
907 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
908 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000909 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000910 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
911
912 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000913 FieldSize = CGM.getContext().getTypeSize(Ty);
914 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000915 EltTys.push_back(DBuilder.createMemberType(
916 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
917 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000918
919 FieldOffset += FieldSize;
920 Elements = DBuilder.getOrCreateArray(EltTys);
921
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000922 // The __block_literal_generic structs are marked with a special
923 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
924 // the debugger needs to know about. To allow type uniquing, emit
925 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000926 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000927 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000928 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000929
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000930 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000931}
932
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000933llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
934 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000935 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000936 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000937
938 SmallString<128> NS;
939 llvm::raw_svector_ostream OS(NS);
Reid Kleckner59d12202017-08-08 01:33:53 +0000940 Ty->getTemplateName().print(OS, getPrintingPolicy(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000941 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000942
943 TemplateSpecializationType::PrintTemplateArgumentList(
Reid Kleckner59d12202017-08-08 01:33:53 +0000944 OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +0000945
David Majnemer58ed0f32016-07-17 00:39:12 +0000946 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000947 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000948
949 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000950 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
951 getLineNumber(Loc),
952 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000953}
954
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000955llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
956 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000957 // We don't set size information, but do specify where the typedef was
958 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000959 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000960
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000961 // Typedefs are derived from some other type.
962 return DBuilder.createTypedef(
963 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
964 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000965 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000966}
967
Reid Klecknerf00f8032016-06-08 20:41:54 +0000968static unsigned getDwarfCC(CallingConv CC) {
969 switch (CC) {
970 case CC_C:
971 // Avoid emitting DW_AT_calling_convention if the C convention was used.
972 return 0;
973
974 case CC_X86StdCall:
975 return llvm::dwarf::DW_CC_BORLAND_stdcall;
976 case CC_X86FastCall:
977 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
978 case CC_X86ThisCall:
979 return llvm::dwarf::DW_CC_BORLAND_thiscall;
980 case CC_X86VectorCall:
981 return llvm::dwarf::DW_CC_LLVM_vectorcall;
982 case CC_X86Pascal:
983 return llvm::dwarf::DW_CC_BORLAND_pascal;
984
985 // FIXME: Create new DW_CC_ codes for these calling conventions.
Martin Storsjo022e7822017-07-17 20:49:45 +0000986 case CC_Win64:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000987 case CC_X86_64SysV:
988 case CC_AAPCS:
989 case CC_AAPCS_VFP:
990 case CC_IntelOclBicc:
991 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000992 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000993 case CC_Swift:
994 case CC_PreserveMost:
995 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000996 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000997 return 0;
998 }
999 return 0;
1000}
1001
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001002llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1003 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001004 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001005
1006 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001007 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001008
1009 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001010 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 if (isa<FunctionNoProtoType>(Ty))
1012 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001013 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1014 for (const QualType &ParamType : FPT->param_types())
1015 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001016 if (FPT->isVariadic())
1017 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001018 }
1019
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001020 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001021 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001022 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001023}
1024
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001025/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001026/// As an optimization, return 0 if the access specifier equals the
1027/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001028static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1029 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001030 AccessSpecifier Default = clang::AS_none;
1031 if (RD && RD->isClass())
1032 Default = clang::AS_private;
1033 else if (RD && (RD->isStruct() || RD->isUnion()))
1034 Default = clang::AS_public;
1035
1036 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001037 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001038
Eric Christophere7b87e52014-10-26 23:40:33 +00001039 switch (Access) {
1040 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001041 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001042 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001043 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001044 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001045 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001046 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001047 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001048 }
1049 llvm_unreachable("unexpected access enumerator");
1050}
Guy Benyei11169dd2012-12-18 14:30:41 +00001051
David Majnemerb4b671e2016-06-30 03:01:59 +00001052llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1053 llvm::DIScope *RecordTy,
1054 const RecordDecl *RD) {
1055 StringRef Name = BitFieldDecl->getName();
1056 QualType Ty = BitFieldDecl->getType();
1057 SourceLocation Loc = BitFieldDecl->getLocation();
1058 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1059 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1060
1061 // Get the location for the field.
1062 llvm::DIFile *File = getOrCreateFile(Loc);
1063 unsigned Line = getLineNumber(Loc);
1064
1065 const CGBitFieldInfo &BitFieldInfo =
1066 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1067 uint64_t SizeInBits = BitFieldInfo.Size;
1068 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001069 uint64_t StorageOffsetInBits =
1070 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001071 uint64_t Offset = BitFieldInfo.Offset;
1072 // The bit offsets for big endian machines are reversed for big
1073 // endian target, compensate for that as the DIDerivedType requires
1074 // un-reversed offsets.
1075 if (CGM.getDataLayout().isBigEndian())
1076 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1077 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001078 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001079 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001080 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1081 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001082}
1083
1084llvm::DIType *
1085CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1086 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001087 uint32_t AlignInBits, llvm::DIFile *tunit,
1088 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001089 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001090
1091 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001092 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001093 unsigned line = getLineNumber(loc);
1094
David Majnemer34b57492014-07-30 01:30:47 +00001095 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001096 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001098 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1099 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001100 if (!Align)
1101 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001102 }
1103
Leny Kholodov80c047d2016-09-06 10:48:04 +00001104 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001105 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001106 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001107}
1108
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001109void CGDebugInfo::CollectRecordLambdaFields(
1110 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001111 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001112 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1113 // has the name and the location of the variable so we should iterate over
1114 // both concurrently.
1115 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1116 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1117 unsigned fieldno = 0;
1118 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001119 E = CXXDecl->captures_end();
1120 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001121 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001122 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001123 SourceLocation Loc = C.getLocation();
1124 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001125 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001126 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001127 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001128 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001129 llvm::DIType *FieldType = createFieldType(
1130 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001131 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001132 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001133 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001134 // TODO: Need to handle 'this' in some way by probably renaming the
1135 // this of the lambda class and having a field member of 'this' or
1136 // by using AT_object_pointer for the function and having that be
1137 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001138 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001139 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001140 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001141 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001142 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001143 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001144
1145 elements.push_back(fieldType);
1146 }
1147 }
1148}
1149
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150llvm::DIDerivedType *
1151CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001152 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001153 // Create the descriptor for the static variable, with or without
1154 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001155 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001156 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1157 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001158
Eric Christopher91a31902013-01-16 01:22:32 +00001159 unsigned LineNumber = getLineNumber(Var->getLocation());
1160 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001161 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001162 if (Var->getInit()) {
1163 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001164 if (Value) {
1165 if (Value->isInt())
1166 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1167 if (Value->isFloat())
1168 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1169 }
Eric Christopher91a31902013-01-16 01:22:32 +00001170 }
1171
Leny Kholodov80c047d2016-09-06 10:48:04 +00001172 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001173 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001175 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001176 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001177 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001178}
1179
Eric Christophere7b87e52014-10-26 23:40:33 +00001180void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001181 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1182 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001183 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001184 StringRef name = field->getName();
1185 QualType type = field->getType();
1186
1187 // Ignore unnamed fields unless they're anonymous structs/unions.
1188 if (name.empty() && !type->isRecordType())
1189 return;
1190
David Majnemerb4b671e2016-06-30 03:01:59 +00001191 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001192 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001193 FieldType = createBitFieldType(field, RecordTy, RD);
1194 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001195 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001196 FieldType =
1197 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001198 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001199 }
1200
David Majnemerb4b671e2016-06-30 03:01:59 +00001201 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001202}
1203
Reid Klecknere2e82062017-08-08 20:30:14 +00001204void CGDebugInfo::CollectRecordNestedType(
1205 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1206 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001207 // Injected class names are not considered nested records.
1208 if (isa<InjectedClassNameType>(Ty))
1209 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001210 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001211 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1212 elements.push_back(nestedType);
1213}
1214
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001215void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001216 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001217 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001218 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001219 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001220
Eric Christopher91a31902013-01-16 01:22:32 +00001221 if (CXXDecl && CXXDecl->isLambda())
1222 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1223 else {
1224 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001225
Reid Klecknere2e82062017-08-08 20:30:14 +00001226 // Debug info for nested types is included in the member list only for
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001227 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001228 bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001229
Eric Christopher91a31902013-01-16 01:22:32 +00001230 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001231 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001232
Eric Christopher91a31902013-01-16 01:22:32 +00001233 // Static and non-static members should appear in the same order as
1234 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001235 for (const auto *I : record->decls())
1236 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001237 if (V->hasAttr<NoDebugAttr>())
1238 continue;
David Blaikiece763042013-08-20 21:49:21 +00001239 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001240 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001241 if (MI != StaticDataMemberCache.end()) {
1242 assert(MI->second &&
1243 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001244 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001245 } else {
1246 auto Field = CreateRecordStaticField(V, RecordTy, record);
1247 elements.push_back(Field);
1248 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001249 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001250 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1251 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001252
1253 // Bump field number for next field.
1254 ++fieldNo;
Reid Klecknere2e82062017-08-08 20:30:14 +00001255 } else if (IncludeNestedTypes) {
1256 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1257 if (!nestedType->isImplicit() &&
1258 nestedType->getDeclContext() == record)
1259 CollectRecordNestedType(nestedType, elements);
1260 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001261 }
1262}
1263
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001264llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001265CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001266 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001267 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001268 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001270 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001271 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1272 Func, Unit);
1273}
David Blaikie2aaf0652013-01-07 22:24:59 +00001274
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001275llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1276 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001277 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278 llvm::DITypeRefArray Args(
1279 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001280 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001281 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001282
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001283 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001284
1285 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001286 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001287
David Blaikie2aaf0652013-01-07 22:24:59 +00001288 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001289 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001290 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1291 // Create pointer type directly in this case.
1292 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1293 QualType PointeeTy = ThisPtrTy->getPointeeType();
1294 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001295 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001296 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001297 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1298 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001299 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001300 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001301 // TODO: This and the artificial type below are misleading, the
1302 // types aren't artificial the argument is, but the current
1303 // metadata doesn't represent that.
1304 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1305 Elts.push_back(ThisPtrType);
1306 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001307 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001308 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001309 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1310 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 }
1312
1313 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001314 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1315 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001316
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001318
Leny Kholodov80c047d2016-09-06 10:48:04 +00001319 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001320 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001321 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001322 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001323 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001324
Reid Klecknerf00f8032016-06-08 20:41:54 +00001325 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1326 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001327}
1328
Eric Christopherb2a008c2013-05-16 00:45:12 +00001329/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001330/// inside a function.
1331static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001332 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001333 return isFunctionLocalClass(NRD);
1334 if (isa<FunctionDecl>(RD->getDeclContext()))
1335 return true;
1336 return false;
1337}
1338
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001339llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1340 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001341 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001342 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001343
Guy Benyei11169dd2012-12-18 14:30:41 +00001344 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001345 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001346
1347 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1348 // make sense to give a single ctor/dtor a linkage name.
1349 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001350 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1351 // property to use here. It may've been intended to model "is non-external
1352 // type" but misses cases of non-function-local but non-external classes such
1353 // as those in anonymous namespaces as well as the reverse - external types
1354 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1356 MethodLinkageName = CGM.getMangledName(Method);
1357
1358 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001359 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001360 unsigned MethodLine = 0;
1361 if (!Method->isImplicit()) {
1362 MethodDefUnit = getOrCreateFile(Method->getLocation());
1363 MethodLine = getLineNumber(Method->getLocation());
1364 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001365
1366 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001367 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001368 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001370 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001371 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001372
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 if (Method->isVirtual()) {
1374 if (Method->isPure())
1375 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1376 else
1377 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001378
Reid Kleckner216d0a12016-06-16 20:08:51 +00001379 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1380 // It doesn't make sense to give a virtual destructor a vtable index,
1381 // since a single destructor has two entries in the vtable.
1382 if (!isa<CXXDestructorDecl>(Method))
1383 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1384 } else {
1385 // Emit MS ABI vftable information. There is only one entry for the
1386 // deleting dtor.
1387 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1388 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1389 MicrosoftVTableContext::MethodVFTableLocation ML =
1390 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1391 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001392
1393 // CodeView only records the vftable offset in the class that introduces
1394 // the virtual method. This is possible because, unlike Itanium, the MS
1395 // C++ ABI does not include all virtual methods from non-primary bases in
1396 // the vtable for the most derived class. For example, if C inherits from
1397 // A and B, C's primary vftable will not include B's virtual methods.
1398 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1399 Flags |= llvm::DINode::FlagIntroducedVirtual;
1400
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001401 // The 'this' adjustment accounts for both the virtual and non-virtual
1402 // portions of the adjustment. Presumably the debugger only uses it when
1403 // it knows the dynamic type of an object.
1404 ThisAdjustment = CGM.getCXXABI()
1405 .getVirtualFunctionPrologueThisAdjustment(GD)
1406 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001407 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 ContainingType = RecordTy;
1409 }
1410
Guy Benyei11169dd2012-12-18 14:30:41 +00001411 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001413 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001414 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001415 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001416 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001417 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001418 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001419 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 }
1421 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001422 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001423 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001425 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001428 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1429 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001430 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001431 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1432 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1433 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001434
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001435 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436
1437 return SP;
1438}
1439
Eric Christophere7b87e52014-10-26 23:40:33 +00001440void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1442 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001443
1444 // Since we want more than just the individual member decls if we
1445 // have templated functions iterate over every declaration to gather
1446 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001447 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001448 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1449 // If the member is implicit, don't add it to the member list. This avoids
1450 // the member being added to type units by LLVM, while still allowing it
1451 // to be emitted into the type declaration/reference inside the compile
1452 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001453 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001454 // FIXME: Handle Using(Shadow?)Decls here to create
1455 // DW_TAG_imported_declarations inside the class for base decls brought into
1456 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1457 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1458 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001459 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001460 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001461
1462 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1463 continue;
1464
David Blaikiefd580722014-10-06 05:18:55 +00001465 // Reuse the existing member function declaration if it exists.
1466 // It may be associated with the declaration of the type & should be
1467 // reused as we're building the definition.
1468 //
1469 // This situation can arise in the vtable-based debug info reduction where
1470 // implicit members are emitted in a non-vtable TU.
1471 auto MI = SPCache.find(Method->getCanonicalDecl());
1472 EltTys.push_back(MI == SPCache.end()
1473 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001474 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001475 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001476}
Guy Benyei11169dd2012-12-18 14:30:41 +00001477
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001478void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001479 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001480 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001481 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1482 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1483 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001484
Bob Haarmandff36732016-10-25 22:19:32 +00001485 // If we are generating CodeView debug info, we also need to emit records for
1486 // indirect virtual base classes.
1487 if (CGM.getCodeGenOpts().EmitCodeView) {
1488 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1489 llvm::DINode::FlagIndirectVirtualBase);
1490 }
1491}
1492
1493void CGDebugInfo::CollectCXXBasesAux(
1494 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1495 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1496 const CXXRecordDecl::base_class_const_range &Bases,
1497 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1498 llvm::DINode::DIFlags StartingFlags) {
1499 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1500 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001501 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001502 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001503 if (!SeenTypes.insert(Base).second)
1504 continue;
1505 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1506 llvm::DINode::DIFlags BFlags = StartingFlags;
1507 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001508
Aaron Ballman574705e2014-03-13 15:41:46 +00001509 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001510 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1511 // virtual base offset offset is -ve. The code generator emits dwarf
1512 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001513 BaseOffset = 0 - CGM.getItaniumVTableContext()
1514 .getVirtualBaseOffsetOffset(RD, Base)
1515 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001516 } else {
1517 // In the MS ABI, store the vbtable offset, which is analogous to the
1518 // vbase offset offset in Itanium.
1519 BaseOffset =
1520 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1521 }
Bob Haarmandff36732016-10-25 22:19:32 +00001522 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001523 } else
1524 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1525 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1526 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001527
Adrian Prantl21361fb2014-08-29 22:44:27 +00001528 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001529 llvm::DIType *DTy =
1530 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 EltTys.push_back(DTy);
1532 }
1533}
1534
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001535llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001536CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1537 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001538 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001539 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001540 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1541 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001542 StringRef Name;
1543 if (TPList)
1544 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001545 switch (TA.getKind()) {
1546 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001547 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001548 TemplateParams.push_back(
1549 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001550 } break;
1551 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001552 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001553 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1554 TheCU, Name, TTy,
1555 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001556 } break;
1557 case TemplateArgument::Declaration: {
1558 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001559 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001560 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001561 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001562 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001563 // Variable pointer template parameters have a value that is the address
1564 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001565 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001566 V = CGM.GetAddrOfGlobalVar(VD);
1567 // Member function pointers have special support for building them, though
1568 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001569 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001570 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001571 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001572 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001573 // Member data pointers have special handling too to compute the fixed
1574 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001575 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001576 // These five lines (& possibly the above member function pointer
1577 // handling) might be able to be refactored to use similar code in
1578 // CodeGenModule::getMemberPointerConstant
1579 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1580 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001581 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001582 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001583 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001584 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1585 TheCU, Name, TTy,
1586 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001587 } break;
1588 case TemplateArgument::NullPtr: {
1589 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001590 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001591 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001592 // Special case member data pointer null values since they're actually -1
1593 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001594 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001595 // But treat member function pointers as simple zero integers because
1596 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1597 // CodeGen grows handling for values of non-null member function
1598 // pointers then perhaps we could remove this special case and rely on
1599 // EmitNullMemberPointer for member function pointers.
1600 if (MPT->isMemberDataPointer())
1601 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1602 if (!V)
1603 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001604 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001605 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001606 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001607 case TemplateArgument::Template:
1608 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001609 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001610 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1611 break;
1612 case TemplateArgument::Pack:
1613 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1614 TheCU, Name, nullptr,
1615 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1616 break;
David Majnemer5559d472013-08-24 08:21:10 +00001617 case TemplateArgument::Expression: {
1618 const Expr *E = TA.getAsExpr();
1619 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001620 if (E->isGLValue())
1621 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001622 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001623 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001624 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001625 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001626 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001627 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001628 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001629 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001630 case TemplateArgument::Null:
1631 llvm_unreachable(
1632 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001633 }
1634 }
1635 return DBuilder.getOrCreateArray(TemplateParams);
1636}
1637
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001638llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001639CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001640 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001641 if (FD->getTemplatedKind() ==
1642 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001643 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1644 ->getTemplate()
1645 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001646 return CollectTemplateParams(
1647 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001648 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001649 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001650}
1651
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001652llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1653 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001654 // Always get the full list of parameters, not just the ones from
1655 // the specialization.
1656 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001657 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001658 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001659 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001660}
1661
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001662llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001663 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001664 return VTablePtrType;
1665
1666 ASTContext &Context = CGM.getContext();
1667
1668 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001669 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001670 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001671 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001672 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001673 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1674 Optional<unsigned> DWARFAddressSpace =
1675 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1676
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001678 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1679 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001680 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1681 return VTablePtrType;
1682}
1683
Guy Benyei11169dd2012-12-18 14:30:41 +00001684StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001685 // Copy the gdb compatible name on the side and use its reference.
1686 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001687}
1688
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001689void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001690 SmallVectorImpl<llvm::Metadata *> &EltTys,
1691 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 // If this class is not dynamic then there is not any vtable info to collect.
1693 if (!RD->isDynamicClass())
1694 return;
1695
Reid Kleckner59812422016-08-31 20:35:01 +00001696 // Don't emit any vtable shape or vptr info if this class doesn't have an
1697 // extendable vfptr. This can happen if the class doesn't have virtual
1698 // methods, or in the MS ABI if those virtual methods only come from virtually
1699 // inherited bases.
1700 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1701 if (!RL.hasExtendableVFPtr())
1702 return;
1703
Reid Klecknerdc124992016-08-31 16:11:43 +00001704 // CodeView needs to know how large the vtable of every dynamic class is, so
1705 // emit a special named pointer type into the element list. The vptr type
1706 // points to this type as well.
1707 llvm::DIType *VPtrTy = nullptr;
1708 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1709 CGM.getTarget().getCXXABI().isMicrosoft();
1710 if (NeedVTableShape) {
1711 uint64_t PtrWidth =
1712 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1713 const VTableLayout &VFTLayout =
1714 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1715 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001716 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001717 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001718 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1719 Optional<unsigned> DWARFAddressSpace =
1720 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001721
1722 // Create a very wide void* type and insert it directly in the element list.
1723 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001724 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1725 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001726 EltTys.push_back(VTableType);
1727
1728 // The vptr is a pointer to this special vtable type.
1729 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1730 }
1731
1732 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001733 if (RL.getPrimaryBase())
1734 return;
1735
1736 if (!VPtrTy)
1737 VPtrTy = getOrCreateVTablePtrType(Unit);
1738
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001740 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001741 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001742 llvm::DINode::FlagArtificial, VPtrTy);
1743 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001744}
1745
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001746llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001747 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001748 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001749 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001750 return T;
1751}
1752
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001753llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001754 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001755 return getOrCreateStandaloneType(D, Loc);
1756}
1757
1758llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1759 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001760 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001761 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001762 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001763 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001764
Adrian Prantl73409ce2013-03-11 18:33:46 +00001765 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001766 return T;
1767}
1768
David Blaikie483a9da2014-05-06 18:35:21 +00001769void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001770 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001771 return;
1772 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001773 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001774 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001775 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001776 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001777 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001778 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001779 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001780}
1781
David Blaikieb2e86eb2013-08-15 20:49:17 +00001782void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001783 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001784 !CGM.getLangOpts().CPlusPlus)
1785 completeRequiredType(RD);
1786}
1787
David Blaikieb11c8732017-01-30 06:36:08 +00001788/// Return true if the class or any of its methods are marked dllimport.
1789static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1790 if (RD->hasAttr<DLLImportAttr>())
1791 return true;
1792 for (const CXXMethodDecl *MD : RD->methods())
1793 if (MD->hasAttr<DLLImportAttr>())
1794 return true;
1795 return false;
1796}
1797
Adrian Prantla43acdc2017-07-24 23:48:51 +00001798/// Does a type definition exist in an imported clang module?
1799static bool isDefinedInClangModule(const RecordDecl *RD) {
1800 // Only definitions that where imported from an AST file come from a module.
1801 if (!RD || !RD->isFromASTFile())
1802 return false;
1803 // Anonymous entities cannot be addressed. Treat them as not from module.
1804 if (!RD->isExternallyVisible() && RD->getName().empty())
1805 return false;
1806 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1807 if (!CXXDecl->isCompleteDefinition())
1808 return false;
1809 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1810 if (TemplateKind != TSK_Undeclared) {
1811 // This is a template, check the origin of the first member.
1812 if (CXXDecl->field_begin() == CXXDecl->field_end())
1813 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1814 if (!CXXDecl->field_begin()->isFromASTFile())
1815 return false;
1816 }
1817 }
1818 return true;
1819}
1820
David Blaikie6943dea2013-08-20 01:28:15 +00001821void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001822 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1823 if (CXXRD->isDynamicClass() &&
1824 CGM.getVTableLinkage(CXXRD) ==
1825 llvm::GlobalValue::AvailableExternallyLinkage &&
1826 !isClassOrMethodDLLImport(CXXRD))
1827 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001828
1829 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1830 return;
1831
David Blaikieb11c8732017-01-30 06:36:08 +00001832 completeClass(RD);
1833}
1834
1835void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001836 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001837 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001838 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001839 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001840 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001841 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001842 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001843 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001844 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001845 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001846}
1847
David Blaikie0e716b42014-03-03 23:48:23 +00001848static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1849 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001850 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1851 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001852 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001853 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001854 return true;
1855 return false;
1856}
1857
Benjamin Kramer8c305922016-02-02 11:06:51 +00001858static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1859 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001860 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001861 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001862 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001863
David Blaikie1ac9c982017-04-11 21:13:37 +00001864 if (auto *ES = RD->getASTContext().getExternalSource())
1865 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1866 return true;
1867
Benjamin Kramer8c305922016-02-02 11:06:51 +00001868 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001869 return false;
1870
1871 if (!LangOpts.CPlusPlus)
1872 return false;
1873
1874 if (!RD->isCompleteDefinitionRequired())
1875 return true;
1876
David Majnemer58ed0f32016-07-17 00:39:12 +00001877 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001878
1879 if (!CXXDecl)
1880 return false;
1881
Adrian McCarthy99242982016-08-16 22:11:18 +00001882 // Only emit complete debug info for a dynamic class when its vtable is
1883 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001884 // across DLL boundaries, so skip this optimization if the class or any of its
1885 // methods are marked dllimport. This isn't a complete solution, since objects
1886 // without any dllimport methods can be used in one DLL and constructed in
1887 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001888 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001889 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001890 return true;
1891
1892 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001893 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001894 Spec = SD->getSpecializationKind();
1895
1896 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1897 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1898 CXXDecl->method_end()))
1899 return true;
1900
1901 return false;
1902}
1903
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001904void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1905 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1906 return;
1907
1908 QualType Ty = CGM.getContext().getRecordType(RD);
1909 llvm::DIType *T = getTypeOrNull(Ty);
1910 if (T && T->isForwardDecl())
1911 completeClassData(RD);
1912}
1913
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001914llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001917 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1918 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001919 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001920 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001921 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001922 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001923
David Blaikieb2e86eb2013-08-15 20:49:17 +00001924 return CreateTypeDefinition(Ty);
1925}
1926
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001927llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001928 RecordDecl *RD = Ty->getDecl();
1929
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001932
1933 // Records and classes and unions can all be recursive. To handle them, we
1934 // first generate a debug descriptor for the struct as a forward declaration.
1935 // Then (if it is a definition) we go through and get debug info for all of
1936 // its members. Finally, we create a descriptor for the complete type (which
1937 // may refer to the forward decl if the struct is recursive) and replace all
1938 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001939 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001940
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001941 const RecordDecl *D = RD->getDefinition();
1942 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001943 return FwdDecl;
1944
David Majnemer58ed0f32016-07-17 00:39:12 +00001945 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001946 CollectContainingType(CXXDecl, FwdDecl);
1947
Guy Benyei11169dd2012-12-18 14:30:41 +00001948 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001949 LexicalBlockStack.emplace_back(&*FwdDecl);
1950 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001951
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001953 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001954 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001955
1956 // Note: The split of CXXDecl information here is intentional, the
1957 // gdb tests will depend on a certain ordering at printout. The debug
1958 // information offsets are still correct if we merge them all together
1959 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001960 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 if (CXXDecl) {
1962 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001963 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 }
1965
Eric Christopher91a31902013-01-16 01:22:32 +00001966 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001968 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001969 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001970
1971 LexicalBlockStack.pop_back();
1972 RegionMap.erase(Ty->getDecl());
1973
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001974 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001975 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001976
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001977 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001978 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001979 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001980
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001981 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001982 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001983}
1984
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001985llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1986 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 // Ignore protocols.
1988 return getOrCreateType(Ty->getBaseType(), Unit);
1989}
1990
Manman Rene6be26c2016-09-13 17:25:08 +00001991llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1992 llvm::DIFile *Unit) {
1993 // Ignore protocols.
1994 SourceLocation Loc = Ty->getDecl()->getLocation();
1995
1996 // Use Typedefs to represent ObjCTypeParamType.
1997 return DBuilder.createTypedef(
1998 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1999 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2000 getDeclContextDescriptor(Ty->getDecl()));
2001}
2002
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002003/// \return true if Getter has the default name for the property PD.
2004static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2005 const ObjCMethodDecl *Getter) {
2006 assert(PD);
2007 if (!Getter)
2008 return true;
2009
2010 assert(Getter->getDeclName().isObjCZeroArgSelector());
2011 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002012 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002013}
2014
2015/// \return true if Setter has the default name for the property PD.
2016static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2017 const ObjCMethodDecl *Setter) {
2018 assert(PD);
2019 if (!Setter)
2020 return true;
2021
2022 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002023 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002024 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002025}
2026
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002027llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2028 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 ObjCInterfaceDecl *ID = Ty->getDecl();
2030 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002031 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002032
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002033 // Return a forward declaration if this type was imported from a clang module,
2034 // and this is not the compile unit with the implementation of the type (which
2035 // may contain hidden ivars).
2036 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2037 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002038 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2039 ID->getName(),
2040 getDeclContextDescriptor(ID), Unit, 0);
2041
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002043 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002045 auto RuntimeLang =
2046 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002047
2048 // If this is just a forward declaration return a special forward-declaration
2049 // debug type since we won't be able to lay out the entire type.
2050 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002051 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002052 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002053 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002054 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2055 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002056 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002057 return FwdDecl;
2058 }
2059
David Blaikieef8a9512014-05-05 23:23:53 +00002060 return CreateTypeDefinition(Ty, Unit);
2061}
2062
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002063llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002064CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2065 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002066 // Use the Module pointer as the key into the cache. This is a
2067 // nullptr if the "Module" is a PCH, which is safe because we don't
2068 // support chained PCH debug info, so there can only be a single PCH.
2069 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002070 auto ModRef = ModuleCache.find(M);
2071 if (ModRef != ModuleCache.end())
2072 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002073
2074 // Macro definitions that were defined with "-D" on the command line.
2075 SmallString<128> ConfigMacros;
2076 {
2077 llvm::raw_svector_ostream OS(ConfigMacros);
2078 const auto &PPOpts = CGM.getPreprocessorOpts();
2079 unsigned I = 0;
2080 // Translate the macro definitions back into a commmand line.
2081 for (auto &M : PPOpts.Macros) {
2082 if (++I > 1)
2083 OS << " ";
2084 const std::string &Macro = M.first;
2085 bool Undef = M.second;
2086 OS << "\"-" << (Undef ? 'U' : 'D');
2087 for (char c : Macro)
2088 switch (c) {
2089 case '\\' : OS << "\\\\"; break;
2090 case '"' : OS << "\\\""; break;
2091 default: OS << c;
2092 }
2093 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002094 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002095 }
Adrian Prantl66689202015-09-18 23:01:45 +00002096
Adrian Prantl835e6632015-09-24 16:10:10 +00002097 bool IsRootModule = M ? !M->Parent : true;
2098 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002099 // PCH files don't have a signature field in the control block,
2100 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002101 // We use the lower 64 bits for debug info.
2102 uint64_t Signature =
2103 Mod.getSignature()
2104 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2105 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002106 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002107 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2108 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2109 TheCU->getProducer(), true, StringRef(), 0,
2110 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2111 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002112 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002113 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002114 llvm::DIModule *Parent =
2115 IsRootModule ? nullptr
2116 : getOrCreateModuleRef(
2117 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2118 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002119 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002120 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2121 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002122 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002123 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002124}
2125
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002126llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2127 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002128 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002129 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002130 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002131 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002132
2133 // Bit size, align and offset of the type.
2134 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002135 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002136
Leny Kholodov80c047d2016-09-06 10:48:04 +00002137 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002139 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002140
Adrian Prantlfd696112015-10-01 00:48:51 +00002141 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002142 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002143 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2144 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002145
David Blaikieef8a9512014-05-05 23:23:53 +00002146 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002147 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002148
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002149 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002150 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002151 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002152
2153 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002154 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002155
2156 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2157 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002158 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002159 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002160 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002161 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002162
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002163 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2164 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 EltTys.push_back(InhTag);
2166 }
2167
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002168 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002169 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002170 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002171 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002172 unsigned PLine = getLineNumber(Loc);
2173 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2174 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002175 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2176 PD->getName(), PUnit, PLine,
2177 hasDefaultGetterName(PD, Getter) ? ""
2178 : getSelectorName(PD->getGetterName()),
2179 hasDefaultSetterName(PD, Setter) ? ""
2180 : getSelectorName(PD->getSetterName()),
2181 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002183 };
2184 {
2185 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002186 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002187 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002188 PropertySet.insert(PD->getIdentifier());
2189 AddProperty(PD);
2190 }
Manman Renefe1bac2016-01-27 20:00:32 +00002191 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002192 // Don't emit duplicate metadata for properties that were already in a
2193 // class extension.
2194 if (!PropertySet.insert(PD->getIdentifier()).second)
2195 continue;
2196 AddProperty(PD);
2197 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 }
2199
2200 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2201 unsigned FieldNo = 0;
2202 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2203 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002204 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002205 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002206 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002207
Guy Benyei11169dd2012-12-18 14:30:41 +00002208 StringRef FieldName = Field->getName();
2209
2210 // Ignore unnamed fields.
2211 if (FieldName.empty())
2212 continue;
2213
2214 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002215 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 unsigned FieldLine = getLineNumber(Field->getLocation());
2217 QualType FType = Field->getType();
2218 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002219 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
2221 if (!FType->isIncompleteArrayType()) {
2222
2223 // Bit size, align and offset of the type.
2224 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002225 ? Field->getBitWidthValue(CGM.getContext())
2226 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002227 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 }
2229
2230 uint64_t FieldOffset;
2231 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2232 // We don't know the runtime offset of an ivar if we're using the
2233 // non-fragile ABI. For bitfields, use the bit offset into the first
2234 // byte of storage of the bitfield. For other fields, use zero.
2235 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002236 FieldOffset =
2237 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 FieldOffset %= CGM.getContext().getCharWidth();
2239 } else {
2240 FieldOffset = 0;
2241 }
2242 } else {
2243 FieldOffset = RL.getFieldOffset(FieldNo);
2244 }
2245
Leny Kholodov80c047d2016-09-06 10:48:04 +00002246 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002248 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002250 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002251 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002252 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002253
Craig Topper8a13c412014-05-21 05:09:00 +00002254 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002256 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002257 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002259 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002260 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002261 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002262 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2263 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002264 PropertyNode = DBuilder.createObjCProperty(
2265 PD->getName(), PUnit, PLine,
2266 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2267 PD->getGetterName()),
2268 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2269 PD->getSetterName()),
2270 PD->getPropertyAttributes(),
2271 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 }
2273 }
2274 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002275 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2276 FieldSize, FieldAlign, FieldOffset, Flags,
2277 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 EltTys.push_back(FieldTy);
2279 }
2280
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002281 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002282 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002283
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002285 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002286}
2287
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002288llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2289 llvm::DIFile *Unit) {
2290 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002291 int64_t Count = Ty->getNumElements();
2292 if (Count == 0)
2293 // If number of elements are not known then this is an unbounded array.
2294 // Use Count == -1 to express such arrays.
2295 Count = -1;
2296
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002297 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002298 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002299
2300 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002301 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002302
2303 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2304}
2305
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002306llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002308 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002309
2310 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002311 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002312 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002313 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2314 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002315 } else if (Ty->isIncompleteArrayType()) {
2316 Size = 0;
2317 if (Ty->getElementType()->isIncompleteType())
2318 Align = 0;
2319 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002320 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002321 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002322 Size = 0;
2323 Align = 0;
2324 } else {
2325 // Size and align of the whole array, not the element type.
2326 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002327 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002328 }
2329
2330 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2331 // interior arrays, do we care? Why aren't nested arrays represented the
2332 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002333 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 QualType EltTy(Ty, 0);
2335 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2336 // If the number of elements is known, then count is that number. Otherwise,
2337 // it's -1. This allows us to represent a subrange with an array of 0
2338 // elements, like this:
2339 //
2340 // struct foo {
2341 // int x[0];
2342 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002343 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002344 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002345 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002346 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002347 if (Expr *Size = VAT->getSizeExpr()) {
2348 llvm::APSInt V;
2349 if (Size->EvaluateAsInt(V, CGM.getContext()))
2350 Count = V.getExtValue();
2351 }
David Blaikie87173f12016-08-22 17:49:56 +00002352 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002353
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 // FIXME: Verify this is right for VLAs.
2355 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2356 EltTy = Ty->getElementType();
2357 }
2358
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002359 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002360
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002361 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2362 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002363}
2364
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002365llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2366 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002367 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2368 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002369}
2370
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002371llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2372 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002373 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2374 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002375}
2376
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002377llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2378 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002379 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002380 uint64_t Size = 0;
2381
2382 if (!Ty->isIncompleteType()) {
2383 Size = CGM.getContext().getTypeSize(Ty);
2384
2385 // Set the MS inheritance model. There is no flag for the unspecified model.
2386 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2387 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2388 case MSInheritanceAttr::Keyword_single_inheritance:
2389 Flags |= llvm::DINode::FlagSingleInheritance;
2390 break;
2391 case MSInheritanceAttr::Keyword_multiple_inheritance:
2392 Flags |= llvm::DINode::FlagMultipleInheritance;
2393 break;
2394 case MSInheritanceAttr::Keyword_virtual_inheritance:
2395 Flags |= llvm::DINode::FlagVirtualInheritance;
2396 break;
2397 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2398 break;
2399 }
2400 }
2401 }
2402
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002403 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002404 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002405 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002406 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2407 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002408
2409 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002410 Ty->getPointeeType()->getAs<FunctionProtoType>();
2411 return DBuilder.createMemberPointerType(
2412 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2413 Ty->getClass(), FPT->getTypeQuals())),
2414 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002415 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002416}
2417
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002418llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002419 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2420 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002421}
2422
Xiuli Pan9c14e282016-01-09 12:53:17 +00002423llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2424 llvm::DIFile *U) {
2425 return getOrCreateType(Ty->getElementType(), U);
2426}
2427
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002428llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002429 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002430
Guy Benyei11169dd2012-12-18 14:30:41 +00002431 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002432 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002433 if (!ED->getTypeForDecl()->isIncompleteType()) {
2434 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002435 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002436 }
2437
Manman Rene0064d82013-08-29 23:19:58 +00002438 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2439
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002440 bool isImportedFromModule =
2441 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2442
Guy Benyei11169dd2012-12-18 14:30:41 +00002443 // If this is just a forward declaration, construct an appropriately
2444 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002445 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002446 // Note that it is possible for enums to be created as part of
2447 // their own declcontext. In this case a FwdDecl will be created
2448 // twice. This doesn't cause a problem because both FwdDecls are
2449 // entered into the ReplaceMap: finalize() will replace the first
2450 // FwdDecl with the second and then replace the second with
2451 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002452 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002453 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002454 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2455 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002456
Guy Benyei11169dd2012-12-18 14:30:41 +00002457 unsigned Line = getLineNumber(ED->getLocation());
2458 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002459 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002460 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2461 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002462
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002463 ReplaceMap.emplace_back(
2464 std::piecewise_construct, std::make_tuple(Ty),
2465 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002466 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002467 }
2468
David Blaikie483a9da2014-05-06 18:35:21 +00002469 return CreateTypeDefinition(Ty);
2470}
2471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002473 const EnumDecl *ED = Ty->getDecl();
2474 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002475 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002476 if (!ED->getTypeForDecl()->isIncompleteType()) {
2477 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002478 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002479 }
2480
2481 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2482
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002483 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002484 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002486 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002487 Enumerators.push_back(DBuilder.createEnumerator(
2488 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 }
2490
2491 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002493
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002496 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002497 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002498 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2499 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2500 Line, Size, Align, EltArray, ClassTy,
2501 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002502}
2503
Amjad Aboud546bc112017-02-09 22:07:24 +00002504llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2505 unsigned MType, SourceLocation LineLoc,
2506 StringRef Name, StringRef Value) {
2507 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2508 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2509}
2510
2511llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2512 SourceLocation LineLoc,
2513 SourceLocation FileLoc) {
2514 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2515 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2516 return DBuilder.createTempMacroFile(Parent, Line, FName);
2517}
2518
David Blaikie05491062013-01-21 04:37:12 +00002519static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2520 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002522 Qualifiers InnerQuals = T.getLocalQualifiers();
2523 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2524 // that is already there.
2525 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2526 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 QualType LastT = T;
2528 switch (T->getTypeClass()) {
2529 default:
David Blaikie05491062013-01-21 04:37:12 +00002530 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002531 case Type::TemplateSpecialization: {
2532 const auto *Spec = cast<TemplateSpecializationType>(T);
2533 if (Spec->isTypeAlias())
2534 return C.getQualifiedType(T.getTypePtr(), Quals);
2535 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002536 break;
2537 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002538 case Type::TypeOfExpr:
2539 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2540 break;
2541 case Type::TypeOf:
2542 T = cast<TypeOfType>(T)->getUnderlyingType();
2543 break;
2544 case Type::Decltype:
2545 T = cast<DecltypeType>(T)->getUnderlyingType();
2546 break;
2547 case Type::UnaryTransform:
2548 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2549 break;
2550 case Type::Attributed:
2551 T = cast<AttributedType>(T)->getEquivalentType();
2552 break;
2553 case Type::Elaborated:
2554 T = cast<ElaboratedType>(T)->getNamedType();
2555 break;
2556 case Type::Paren:
2557 T = cast<ParenType>(T)->getInnerType();
2558 break;
David Blaikie05491062013-01-21 04:37:12 +00002559 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002561 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002562 case Type::Auto:
2563 case Type::DeducedTemplateSpecialization: {
2564 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002565 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002566 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002567 break;
2568 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002569 case Type::Adjusted:
2570 case Type::Decayed:
2571 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2572 T = cast<AdjustedType>(T)->getAdjustedType();
2573 break;
2574 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002575
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002577 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 } while (true);
2579}
2580
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002581llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002582
2583 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002584 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002585
David Blaikief427b002014-05-06 03:42:01 +00002586 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 if (it != TypeCache.end()) {
2588 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002589 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002590 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002591 }
2592
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002593 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002594}
2595
David Blaikie0e716b42014-03-03 23:48:23 +00002596void CGDebugInfo::completeTemplateDefinition(
2597 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002598 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002599 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002600 completeUnusedClass(SD);
2601}
David Blaikie0856f662014-03-04 22:01:08 +00002602
David Blaikie1ac9c982017-04-11 21:13:37 +00002603void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2604 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2605 return;
2606
2607 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002608 // In case this type has no member function definitions being emitted, ensure
2609 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002610 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002611}
2612
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002613llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002615 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002616
2617 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002618 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002619
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002620 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002621 return T;
2622
Adrian Prantlca844182015-09-11 17:23:03 +00002623 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002624 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002625
2626 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002627 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002628
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 return Res;
2630}
2631
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002632llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002633 // A forward declaration inside a module header does not belong to the module.
2634 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2635 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002636 if (DebugTypeExtRefs && D->isFromASTFile()) {
2637 // Record a reference to an imported clang module or precompiled header.
2638 auto *Reader = CGM.getContext().getExternalSource();
2639 auto Idx = D->getOwningModuleID();
2640 auto Info = Reader->getSourceDescriptor(Idx);
2641 if (Info)
2642 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2643 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002644 // We are building a clang module or a precompiled header.
2645 //
2646 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2647 // and it wouldn't be necessary to specify the parent scope
2648 // because the type is already unique by definition (it would look
2649 // like the output of -fno-standalone-debug). On the other hand,
2650 // the parent scope helps a consumer to quickly locate the object
2651 // file where the type's definition is located, so it might be
2652 // best to make this behavior a command line or debugger tuning
2653 // option.
2654 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
Richard Smith54f04402017-05-18 02:29:20 +00002655 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002656 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002657 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2658 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002659 } else {
2660 // This the precompiled header being built.
2661 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002662 }
2663 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002664
Adrian Prantl9402cef2015-09-20 16:51:35 +00002665 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002666}
2667
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002668llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 // Handle qualifiers, which recursively handles what they refer to.
2670 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002671 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002672
Guy Benyei11169dd2012-12-18 14:30:41 +00002673 // Work out details of type.
2674 switch (Ty->getTypeClass()) {
2675#define TYPE(Class, Base)
2676#define ABSTRACT_TYPE(Class, Base)
2677#define NON_CANONICAL_TYPE(Class, Base)
2678#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2679#include "clang/AST/TypeNodes.def"
2680 llvm_unreachable("Dependent types cannot show up in debug information");
2681
2682 case Type::ExtVector:
2683 case Type::Vector:
2684 return CreateType(cast<VectorType>(Ty), Unit);
2685 case Type::ObjCObjectPointer:
2686 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2687 case Type::ObjCObject:
2688 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002689 case Type::ObjCTypeParam:
2690 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002691 case Type::ObjCInterface:
2692 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2693 case Type::Builtin:
2694 return CreateType(cast<BuiltinType>(Ty));
2695 case Type::Complex:
2696 return CreateType(cast<ComplexType>(Ty));
2697 case Type::Pointer:
2698 return CreateType(cast<PointerType>(Ty), Unit);
2699 case Type::BlockPointer:
2700 return CreateType(cast<BlockPointerType>(Ty), Unit);
2701 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002702 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002704 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002706 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 case Type::FunctionProto:
2708 case Type::FunctionNoProto:
2709 return CreateType(cast<FunctionType>(Ty), Unit);
2710 case Type::ConstantArray:
2711 case Type::VariableArray:
2712 case Type::IncompleteArray:
2713 return CreateType(cast<ArrayType>(Ty), Unit);
2714
2715 case Type::LValueReference:
2716 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2717 case Type::RValueReference:
2718 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2719
2720 case Type::MemberPointer:
2721 return CreateType(cast<MemberPointerType>(Ty), Unit);
2722
2723 case Type::Atomic:
2724 return CreateType(cast<AtomicType>(Ty), Unit);
2725
Xiuli Pan9c14e282016-01-09 12:53:17 +00002726 case Type::Pipe:
2727 return CreateType(cast<PipeType>(Ty), Unit);
2728
Guy Benyei11169dd2012-12-18 14:30:41 +00002729 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002730 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2731
David Blaikie42edade2014-11-11 20:44:45 +00002732 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002733 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002734 case Type::Adjusted:
2735 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002736 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002737 case Type::Elaborated:
2738 case Type::Paren:
2739 case Type::SubstTemplateTypeParm:
2740 case Type::TypeOfExpr:
2741 case Type::TypeOf:
2742 case Type::Decltype:
2743 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002744 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002745 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002747
David Blaikie42edade2014-11-11 20:44:45 +00002748 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002749}
2750
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002751llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2752 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002753 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002754
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002755 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002756
2757 // We may have cached a forward decl when we could have created
2758 // a non-forward decl. Go ahead and create a non-forward decl
2759 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002760 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002761 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002762
2763 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002764 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002765
2766 // Propagate members from the declaration to the definition
2767 // CreateType(const RecordType*) will overwrite this with the members in the
2768 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002769 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002770
Guy Benyei11169dd2012-12-18 14:30:41 +00002771 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002772 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002773 return Res;
2774}
2775
2776// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002777llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002779
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002781 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 unsigned Line = getLineNumber(RD->getLocation());
2783 StringRef RDName = getClassName(RD);
2784
Amjad Abouddc4531e2016-04-30 01:44:38 +00002785 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002786
David Blaikied2785892013-08-18 17:36:19 +00002787 // If we ended up creating the type during the context chain construction,
2788 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002789 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002790 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002791 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002792 return T;
David Blaikied2785892013-08-18 17:36:19 +00002793
Adrian Prantl381e7552014-02-04 21:29:50 +00002794 // If this is just a forward or incomplete declaration, construct an
2795 // appropriately marked node and just return it.
2796 const RecordDecl *D = RD->getDefinition();
2797 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002798 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002799
2800 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002801 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002802
Manman Rene0064d82013-08-29 23:19:58 +00002803 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2804
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002805 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002806 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2807 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002808
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002809 // Elements of composite types usually have back to the type, creating
2810 // uniquing cycles. Distinct nodes are more efficient.
2811 switch (RealDecl->getTag()) {
2812 default:
2813 llvm_unreachable("invalid composite type tag");
2814
2815 case llvm::dwarf::DW_TAG_array_type:
2816 case llvm::dwarf::DW_TAG_enumeration_type:
2817 // Array elements and most enumeration elements don't have back references,
2818 // so they don't tend to be involved in uniquing cycles and there is some
2819 // chance of merging them when linking together two modules. Only make
2820 // them distinct if they are ODR-uniqued.
2821 if (FullName.empty())
2822 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002823 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002824
2825 case llvm::dwarf::DW_TAG_structure_type:
2826 case llvm::dwarf::DW_TAG_union_type:
2827 case llvm::dwarf::DW_TAG_class_type:
2828 // Immediatley resolve to a distinct node.
2829 RealDecl =
2830 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2831 break;
2832 }
2833
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002834 RegionMap[Ty->getDecl()].reset(RealDecl);
2835 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002836
David Majnemer58ed0f32016-07-17 00:39:12 +00002837 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002838 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002839 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002840 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002841}
2842
David Blaikieadfbf992013-08-18 16:55:33 +00002843void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002844 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002845 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002846 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002847 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2848 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002849 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002850 while (1) {
2851 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2852 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2853 if (PBT && !BRL.isPrimaryBaseVirtual())
2854 PBase = PBT;
2855 else
2856 break;
2857 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002858 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002859 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2860 getOrCreateFile(RD->getLocation())));
2861 } else if (RD->isDynamicClass())
2862 ContainingType = RealDecl;
2863
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002864 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002865}
2866
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002867llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002868 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002869 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002871 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002872 llvm::DIType *Ty =
2873 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2874 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002875 *Offset += FieldSize;
2876 return Ty;
2877}
2878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002879void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002880 StringRef &Name,
2881 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002882 llvm::DIScope *&FDContext,
2883 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002884 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002885 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002886 Name = getFunctionName(FD);
2887 // Use mangled name as linkage name for C/C++ functions.
2888 if (FD->hasPrototype()) {
2889 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002890 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002891 }
2892 // No need to replicate the linkage name if it isn't different from the
2893 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002894 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002895 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2896 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002897 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002898 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002899 LinkageName = StringRef();
2900
Benjamin Kramer8c305922016-02-02 11:06:51 +00002901 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002902 if (const NamespaceDecl *NSDecl =
Adrian Prantl6fc88752017-05-16 23:46:10 +00002903 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00002904 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00002905 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002906 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2907 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2908 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2909 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002910 // Check if it is a noreturn-marked function
2911 if (FD->isNoReturn())
2912 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002913 // Collect template parameters.
2914 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2915 }
2916}
2917
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002918void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002919 unsigned &LineNo, QualType &T,
2920 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002921 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002922 Unit = getOrCreateFile(VD->getLocation());
2923 LineNo = getLineNumber(VD->getLocation());
2924
2925 setLocation(VD->getLocation());
2926
2927 T = VD->getType();
2928 if (T->isIncompleteArrayType()) {
2929 // CodeGen turns int[] into int[1] so we'll do the same here.
2930 llvm::APInt ConstVal(32, 1);
2931 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2932
2933 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2934 ArrayType::Normal, 0);
2935 }
2936
2937 Name = VD->getName();
2938 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2939 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2940 LinkageName = CGM.getMangledName(VD);
2941 if (LinkageName == Name)
2942 LinkageName = StringRef();
2943
2944 // Since we emit declarations (DW_AT_members) for static members, place the
2945 // definition of those static members in the namespace they were declared in
2946 // in the source code (the lexical decl context).
2947 // FIXME: Generalize this for even non-member global variables where the
2948 // declaration and definition may have different lexical decl contexts, once
2949 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002950 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2951 : VD->getDeclContext();
2952 // When a record type contains an in-line initialization of a static data
2953 // member, and the record type is marked as __declspec(dllexport), an implicit
2954 // definition of the member will be created in the record context. DWARF
2955 // doesn't seem to have a nice way to describe this in a form that consumers
2956 // are likely to understand, so fake the "normal" situation of a definition
2957 // outside the class by putting it in the global scope.
2958 if (DC->isRecord())
2959 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002960
Amjad Abouddc4531e2016-04-30 01:44:38 +00002961 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2962 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002963}
2964
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002965llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2966 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002967 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002968 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002969 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002970 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002971 llvm::DIFile *Unit = getOrCreateFile(Loc);
2972 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002973 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002974 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002975 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002976 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2977
Frederic Rissd253ed62014-11-18 03:40:51 +00002978 // Build function type.
2979 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002980 if (FD)
2981 for (const ParmVarDecl *Parm : FD->parameters())
2982 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002983 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2984 QualType FnType = CGM.getContext().getFunctionType(
2985 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002986 if (Stub) {
2987 return DBuilder.createFunction(
2988 DContext, Name, LinkageName, Unit, Line,
2989 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2990 !FD->isExternallyVisible(),
2991 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2992 TParamsArray.get(), getFunctionDeclaration(FD));
2993 }
2994
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002995 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002996 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002997 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2998 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002999 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003000 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00003001 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003002 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3003 std::make_tuple(CanonDecl),
3004 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003005 return SP;
3006}
3007
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003008llvm::DISubprogram *
3009CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
3010 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3011}
3012
3013llvm::DISubprogram *
3014CGDebugInfo::getFunctionStub(GlobalDecl GD) {
3015 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3016}
3017
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003018llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003019CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3020 QualType T;
3021 StringRef Name, LinkageName;
3022 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003023 llvm::DIFile *Unit = getOrCreateFile(Loc);
3024 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003025 unsigned Line = getLineNumber(Loc);
3026
3027 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003028 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003029 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3030 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003031 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003032 FwdDeclReplaceMap.emplace_back(
3033 std::piecewise_construct,
3034 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3035 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003036 return GV;
3037}
3038
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003039llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003040 // We only need a declaration (not a definition) of the type - so use whatever
3041 // we would otherwise do to get a type for a pointee. (forward declarations in
3042 // limited debug info, full definitions (if the type definition is available)
3043 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003044 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003045 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003046 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003047 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003048
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003049 if (I != DeclCache.end()) {
3050 auto N = I->second;
3051 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3052 return GVE->getVariable();
3053 return dyn_cast_or_null<llvm::DINode>(N);
3054 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003055
3056 // No definition for now. Emit a forward definition that might be
3057 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003058 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003059 return getFunctionForwardDeclaration(FD);
3060 else if (const auto *VD = dyn_cast<VarDecl>(D))
3061 return getGlobalVariableForwardDeclaration(VD);
3062
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003063 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003064}
3065
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003066llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003067 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003068 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003069
David Majnemer58ed0f32016-07-17 00:39:12 +00003070 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003071 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003072 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003073
3074 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003075 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003076
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003077 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003078 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003079 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003080 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003081 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003082 }
3083 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003085 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003086 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 return SP;
3088 }
3089
Aaron Ballman86c93902014-03-06 23:45:36 +00003090 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003091 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003092 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003093 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003094 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003095 return SP;
3096 }
3097 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003098 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003099}
3100
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003101// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003102// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003103llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003104 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003105 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003106 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003107 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3108 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003109 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003110
David Majnemer58ed0f32016-07-17 00:39:12 +00003111 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003112 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003113
3114 const auto *FTy = FnType->getAs<FunctionType>();
3115 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3116
David Majnemer58ed0f32016-07-17 00:39:12 +00003117 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003118 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003119 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120
3121 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003122 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003123
3124 // Replace the instancetype keyword with the actual type.
3125 if (ResultTy == CGM.getContext().getObjCInstanceType())
3126 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003127 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003128
Adrian Prantl7bec9032013-05-10 21:08:31 +00003129 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003131 QualType SelfDeclTy;
3132 if (auto *SelfDecl = OMethod->getSelfDecl())
3133 SelfDeclTy = SelfDecl->getType();
3134 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3135 if (FPT->getNumParams() > 1)
3136 SelfDeclTy = FPT->getParamType(0);
3137 if (!SelfDeclTy.isNull())
3138 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003140 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003141 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003142 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003143 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003144 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003145 // Variadic methods need a special marker at the end of the type list.
3146 if (OMethod->isVariadic())
3147 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003148
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003149 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003150 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3151 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003152 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003153
Adrian Prantl800faef2014-02-25 23:42:18 +00003154 // Handle variadic function types; they need an additional
3155 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003156 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003157 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003158 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003159 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003160 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3161 for (QualType ParamType : FPT->param_types())
3162 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003163 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003164 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003165 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3166 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003167 }
3168
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003170}
3171
Eric Christophere7b87e52014-10-26 23:40:33 +00003172void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3173 SourceLocation ScopeLoc, QualType FnType,
3174 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003175
3176 StringRef Name;
3177 StringRef LinkageName;
3178
3179 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3180
3181 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003182 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003183
Leny Kholodov80c047d2016-09-06 10:48:04 +00003184 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003185 llvm::DIFile *Unit = getOrCreateFile(Loc);
3186 llvm::DIScope *FDContext = Unit;
3187 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 if (!HasDecl) {
3189 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003190 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003191 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003192 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003193 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003194 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003195 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003196 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003197 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003198 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003199 return;
3200 }
3201 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003202 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3203 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003204 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003206 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003207 } else {
3208 // Use llvm function name.
3209 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003210 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003211 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003212 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 Name = Name.substr(1);
3214
Adrian Prantl42d71b92014-04-10 23:21:53 +00003215 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003216 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003217 // Artificial functions should not silently reuse CurLoc.
3218 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003219 }
3220 unsigned LineNo = getLineNumber(Loc);
3221 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003222
Eric Christopher8018e412014-03-27 18:50:35 +00003223 // FIXME: The function declaration we're constructing here is mostly reusing
3224 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3225 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3226 // all subprograms instead of the actual context since subprogram definitions
3227 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003228 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003229 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003230 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003231 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003232 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003233 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003234 // We might get here with a VarDecl in the case we're generating
3235 // code for the initialization of globals. Do not record these decls
3236 // as they will overwrite the actual VarDecl Decl in the cache.
3237 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003238 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003239
Adrian Prantlbebb8932014-03-21 21:01:58 +00003240 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003241 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003242
Guy Benyei11169dd2012-12-18 14:30:41 +00003243 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003244 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003245}
3246
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003247void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3248 QualType FnType) {
3249 StringRef Name;
3250 StringRef LinkageName;
3251
3252 const Decl *D = GD.getDecl();
3253 if (!D)
3254 return;
3255
Leny Kholodov80c047d2016-09-06 10:48:04 +00003256 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003257 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003258 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003259 llvm::DINodeArray TParamsArray;
3260 if (isa<FunctionDecl>(D)) {
3261 // If there is a DISubprogram for this function available then use it.
3262 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3263 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003264 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003265 Name = getObjCMethodName(OMD);
3266 Flags |= llvm::DINode::FlagPrototyped;
3267 } else {
3268 llvm_unreachable("not a function or ObjC method");
3269 }
3270 if (!Name.empty() && Name[0] == '\01')
3271 Name = Name.substr(1);
3272
3273 if (D->isImplicit()) {
3274 Flags |= llvm::DINode::FlagArtificial;
3275 // Artificial functions without a location should not silently reuse CurLoc.
3276 if (Loc.isInvalid())
3277 CurLoc = SourceLocation();
3278 }
3279 unsigned LineNo = getLineNumber(Loc);
3280 unsigned ScopeLine = 0;
3281
Adrian Prantle76bda52016-04-15 15:55:45 +00003282 DBuilder.retainType(DBuilder.createFunction(
3283 FDContext, Name, LinkageName, Unit, LineNo,
3284 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3285 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3286 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003287}
3288
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003289void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3290 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3291 // If there is a subprogram for this function available then use it.
3292 auto FI = SPCache.find(FD->getCanonicalDecl());
3293 llvm::DISubprogram *SP = nullptr;
3294 if (FI != SPCache.end())
3295 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003296 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003297 SP = getFunctionStub(GD);
3298 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3299 LexicalBlockStack.emplace_back(SP);
3300 setInlinedAt(Builder.getCurrentDebugLocation());
3301 EmitLocation(Builder, FD->getLocation());
3302}
3303
3304void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3305 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003306 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003307 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3308}
3309
David Blaikie835afb22015-01-21 23:08:17 +00003310void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003311 // Update our current location
3312 setLocation(Loc);
3313
Eric Christophere7b87e52014-10-26 23:40:33 +00003314 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3315 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003316
Adrian Prantle83b1302014-01-07 22:05:52 +00003317 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003318 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003319 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003320}
3321
Guy Benyei11169dd2012-12-18 14:30:41 +00003322void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003323 llvm::MDNode *Back = nullptr;
3324 if (!LexicalBlockStack.empty())
3325 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003326 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003327 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003328 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003329}
3330
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003331void CGDebugInfo::AppendAddressSpaceXDeref(
3332 unsigned AddressSpace,
3333 SmallVectorImpl<int64_t> &Expr) const {
3334 Optional<unsigned> DWARFAddressSpace =
3335 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3336 if (!DWARFAddressSpace)
3337 return;
3338
3339 Expr.push_back(llvm::dwarf::DW_OP_constu);
3340 Expr.push_back(DWARFAddressSpace.getValue());
3341 Expr.push_back(llvm::dwarf::DW_OP_swap);
3342 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3343}
3344
Eric Christopher0fdcb312013-05-16 00:52:20 +00003345void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3346 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003347 // Set our current location.
3348 setLocation(Loc);
3349
Guy Benyei11169dd2012-12-18 14:30:41 +00003350 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003351 Builder.SetCurrentDebugLocation(
3352 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3353 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003354
Benjamin Kramer8c305922016-02-02 11:06:51 +00003355 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003356 return;
3357
3358 // Create a new lexical block and push it on the stack.
3359 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003360}
3361
Eric Christopher0fdcb312013-05-16 00:52:20 +00003362void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3363 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3365
3366 // Provide an entry in the line table for the end of the block.
3367 EmitLocation(Builder, Loc);
3368
Benjamin Kramer8c305922016-02-02 11:06:51 +00003369 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003370 return;
3371
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 LexicalBlockStack.pop_back();
3373}
3374
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003375void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3377 unsigned RCount = FnBeginRegionCount.back();
3378 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3379
3380 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003381 while (LexicalBlockStack.size() != RCount) {
3382 // Provide an entry in the line table for the end of the block.
3383 EmitLocation(Builder, CurLoc);
3384 LexicalBlockStack.pop_back();
3385 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003386 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003387
3388 if (Fn && Fn->getSubprogram())
3389 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003390}
3391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003392llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003393 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003394
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003395 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 QualType FType;
3397 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003398 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003399
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003400 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003401 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003402
3403 FieldOffset = 0;
3404 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3405 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3406 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3407 FType = CGM.getContext().IntTy;
3408 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3409 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3410
3411 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3412 if (HasCopyAndDispose) {
3413 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003414 EltTys.push_back(
3415 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3416 EltTys.push_back(
3417 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 }
3419 bool HasByrefExtendedLayout;
3420 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003421 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3422 HasByrefExtendedLayout) &&
3423 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003424 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003425 EltTys.push_back(
3426 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003427 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003428
Guy Benyei11169dd2012-12-18 14:30:41 +00003429 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3430 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003431 CGM.getTarget().getPointerAlign(0))) {
3432 CharUnits FieldOffsetInBytes =
3433 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003434 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003435 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003436
Guy Benyei11169dd2012-12-18 14:30:41 +00003437 if (NumPaddingBytes.isPositive()) {
3438 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3439 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3440 pad, ArrayType::Normal, 0);
3441 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3442 }
3443 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003444
Guy Benyei11169dd2012-12-18 14:30:41 +00003445 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003446 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003447 FieldSize = CGM.getContext().getTypeSize(FType);
3448 FieldAlign = CGM.getContext().toBits(Align);
3449
Eric Christopherb2a008c2013-05-16 00:45:12 +00003450 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003451 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003452 FieldAlign, FieldOffset,
3453 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003454 EltTys.push_back(FieldTy);
3455 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003456
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003457 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003458
Leny Kholodov80c047d2016-09-06 10:48:04 +00003459 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003460
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003462 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003463}
3464
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003465void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3466 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003467 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003468 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003469 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003470 if (VD->hasAttr<NoDebugAttr>())
3471 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003472
David Blaikie7fceebf2013-08-19 03:37:48 +00003473 bool Unwritten =
3474 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3475 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003476 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003477 if (!Unwritten)
3478 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003479 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003480 uint64_t XOffset = 0;
3481 if (VD->hasAttr<BlocksAttr>())
3482 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003483 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003484 Ty = getOrCreateType(VD->getType(), Unit);
3485
3486 // If there is no debug info for this type then do not emit debug info
3487 // for this variable.
3488 if (!Ty)
3489 return;
3490
Guy Benyei11169dd2012-12-18 14:30:41 +00003491 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003492 unsigned Line = 0;
3493 unsigned Column = 0;
3494 if (!Unwritten) {
3495 Line = getLineNumber(VD->getLocation());
3496 Column = getColumnNumber(VD->getLocation());
3497 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003498 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003499 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003500 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003501 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003502
3503 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3504
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003505 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3506 AppendAddressSpaceXDeref(AddressSpace, Expr);
3507
Alexey Bataev24f710182017-06-09 13:55:08 +00003508 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3509 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003510 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3511 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3512 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3513 Flags |= llvm::DINode::FlagObjectPointer;
3514 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003515
Adrian Prantlc3782a12017-04-18 01:22:01 +00003516 // Note: Older versions of clang used to emit byval references with an extra
3517 // DW_OP_deref, because they referenced the IR arg directly instead of
3518 // referencing an alloca. Newer versions of LLVM don't treat allocas
3519 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003520 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003521 StringRef Name = VD->getName();
3522 if (!Name.empty()) {
3523 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003524 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003525 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003526 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 // offset of __forwarding field
3528 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003529 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003530 Expr.push_back(offset.getQuantity());
3531 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003532 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003533 // offset of x field
3534 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003535 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003536 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003537 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003538 // If VD is an anonymous union then Storage represents value for
3539 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003540 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003541 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003542 // GDB has trouble finding local variables in anonymous unions, so we emit
3543 // artifical local variables for each of the members.
3544 //
3545 // FIXME: Remove this code as soon as GDB supports this.
3546 // The debug info verifier in LLVM operates based on the assumption that a
3547 // variable has the same size as its storage and we had to disable the check
3548 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003549 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003550 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003551 StringRef FieldName = Field->getName();
3552
3553 // Ignore unnamed fields. Do not ignore unnamed records.
3554 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3555 continue;
3556
3557 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003558 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003559 auto *D = DBuilder.createAutoVariable(
3560 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003561 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003562
3563 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003564 DBuilder.insertDeclare(
3565 Storage, D, DBuilder.createExpression(Expr),
3566 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3567 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003568 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003569 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003570 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003571
3572 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003573 auto *D = ArgNo
3574 ? DBuilder.createParameterVariable(
3575 Scope, Name, *ArgNo, Unit, Line, Ty,
3576 CGM.getLangOpts().Optimize, Flags)
3577 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3578 CGM.getLangOpts().Optimize, Flags,
3579 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003580
3581 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003582 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003583 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003584 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003585}
3586
3587void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3588 llvm::Value *Storage,
3589 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003590 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003591 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003592}
3593
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003594llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3595 llvm::DIType *Ty) {
3596 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003597 if (CachedTy)
3598 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003599 return DBuilder.createObjectPointerType(Ty);
3600}
3601
Eric Christophere7b87e52014-10-26 23:40:33 +00003602void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3603 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003604 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003605 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003606 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003607
Craig Topper8a13c412014-05-21 05:09:00 +00003608 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003609 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003610 if (VD->hasAttr<NoDebugAttr>())
3611 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003612
Guy Benyei11169dd2012-12-18 14:30:41 +00003613 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003614
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003616 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3617 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003618 if (isByRef)
3619 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003620 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003621 Ty = getOrCreateType(VD->getType(), Unit);
3622
3623 // Self is passed along as an implicit non-arg variable in a
3624 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003625 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3626 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3627 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003628
3629 // Get location information.
3630 unsigned Line = getLineNumber(VD->getLocation());
3631 unsigned Column = getColumnNumber(VD->getLocation());
3632
3633 const llvm::DataLayout &target = CGM.getDataLayout();
3634
3635 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003636 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3638
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003639 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003640 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003641 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003642 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003643 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003644 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003645 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003647 offset =
3648 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003649 addr.push_back(offset.getQuantity());
3650 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003651 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003652 // offset of x field
3653 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003654 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003655 }
3656
3657 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003658 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003659 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003660 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003661 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003662
Guy Benyei11169dd2012-12-18 14:30:41 +00003663 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003664 auto DL =
3665 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003666 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003667 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003668 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003669 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003670 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003671}
3672
Guy Benyei11169dd2012-12-18 14:30:41 +00003673void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3674 unsigned ArgNo,
3675 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003676 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003677 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003678}
3679
3680namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003681struct BlockLayoutChunk {
3682 uint64_t OffsetInBits;
3683 const BlockDecl::Capture *Capture;
3684};
3685bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3686 return l.OffsetInBits < r.OffsetInBits;
3687}
Guy Benyei11169dd2012-12-18 14:30:41 +00003688}
3689
3690void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003691 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003692 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003693 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003694 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003695 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003696 ASTContext &C = CGM.getContext();
3697 const BlockDecl *blockDecl = block.getBlockDecl();
3698
3699 // Collect some general information about the block's location.
3700 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003701 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 unsigned line = getLineNumber(loc);
3703 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003704
Guy Benyei11169dd2012-12-18 14:30:41 +00003705 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003706 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003707
3708 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003709 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003710
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003711 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003712 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003713 blockLayout->getElementOffsetInBits(0),
3714 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003715 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003716 blockLayout->getElementOffsetInBits(1),
3717 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003718 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003719 blockLayout->getElementOffsetInBits(2),
3720 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003721 auto *FnTy = block.getBlockExpr()->getFunctionType();
3722 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003723 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003724 blockLayout->getElementOffsetInBits(3),
3725 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003726 fields.push_back(createFieldType(
3727 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3728 ? C.getBlockDescriptorExtendedType()
3729 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003730 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003731
3732 // We want to sort the captures by offset, not because DWARF
3733 // requires this, but because we're paranoid about debuggers.
3734 SmallVector<BlockLayoutChunk, 8> chunks;
3735
3736 // 'this' capture.
3737 if (blockDecl->capturesCXXThis()) {
3738 BlockLayoutChunk chunk;
3739 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003740 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003741 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003742 chunks.push_back(chunk);
3743 }
3744
3745 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003746 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003747 const VarDecl *variable = capture.getVariable();
3748 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3749
3750 // Ignore constant captures.
3751 if (captureInfo.isConstant())
3752 continue;
3753
3754 BlockLayoutChunk chunk;
3755 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003756 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003757 chunk.Capture = &capture;
3758 chunks.push_back(chunk);
3759 }
3760
3761 // Sort by offset.
3762 llvm::array_pod_sort(chunks.begin(), chunks.end());
3763
David Majnemer58ed0f32016-07-17 00:39:12 +00003764 for (const BlockLayoutChunk &Chunk : chunks) {
3765 uint64_t offsetInBits = Chunk.OffsetInBits;
3766 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003767
3768 // If we have a null capture, this must be the C++ 'this' capture.
3769 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003770 QualType type;
3771 if (auto *Method =
3772 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3773 type = Method->getThisType(C);
3774 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3775 type = QualType(RDecl->getTypeForDecl(), 0);
3776 else
3777 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003778
David Majnemerb4b671e2016-06-30 03:01:59 +00003779 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003780 offsetInBits, tunit, tunit));
3781 continue;
3782 }
3783
3784 const VarDecl *variable = capture->getVariable();
3785 StringRef name = variable->getName();
3786
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003787 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003788 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003789 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003790 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003791
3792 // FIXME: this creates a second copy of this type!
3793 uint64_t xoffset;
3794 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003795 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003796 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3797 PtrInfo.Width, Align, offsetInBits,
3798 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003799 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003800 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003801 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003802 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003803 }
3804 fields.push_back(fieldType);
3805 }
3806
3807 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003808 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3809 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003811 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003812
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003813 llvm::DIType *type =
3814 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003815 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003816 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003817 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3818
3819 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003820 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003821 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003822
3823 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003824 auto *debugVar = DBuilder.createParameterVariable(
3825 scope, Arg->getName(), ArgNo, tunit, line, type,
3826 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003827
Adrian Prantl616bef42013-03-14 21:52:59 +00003828 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003829 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003830 DBuilder.insertDbgValueIntrinsic(
Adrian Prantl1fa18852017-07-28 20:21:08 +00003831 LocalAddr, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003832 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3833 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003834 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003835
Adrian Prantl616bef42013-03-14 21:52:59 +00003836 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003837 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003838 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003839 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003840}
3841
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003842llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003843CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3844 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003845 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003846
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003847 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003848 if (MI != StaticDataMemberCache.end()) {
3849 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003850 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003851 }
David Blaikiece763042013-08-20 21:49:21 +00003852
3853 // If the member wasn't found in the cache, lazily construct and add it to the
3854 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003855 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003856 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003857 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003858}
3859
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003860llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003861 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3862 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003863 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003864
3865 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003866 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003867 StringRef FieldName = Field->getName();
3868
3869 // Ignore unnamed fields, but recurse into anonymous records.
3870 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003871 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003872 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003873 Var, DContext);
3874 continue;
3875 }
3876 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003877 GVE = DBuilder.createGlobalVariableExpression(
3878 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3879 Var->hasLocalLinkage());
3880 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003881 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003882 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003883}
3884
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003885void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003886 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003887 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003888 if (D->hasAttr<NoDebugAttr>())
3889 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003890
3891 // If we already created a DIGlobalVariable for this declaration, just attach
3892 // it to the llvm::GlobalVariable.
3893 auto Cached = DeclCache.find(D->getCanonicalDecl());
3894 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003895 return Var->addDebugInfo(
3896 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003897
Guy Benyei11169dd2012-12-18 14:30:41 +00003898 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003899 llvm::DIFile *Unit = nullptr;
3900 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003901 unsigned LineNo;
3902 StringRef DeclName, LinkageName;
3903 QualType T;
3904 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003905
3906 // Attempt to store one global variable for the declaration - even if we
3907 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003908 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003909
3910 // If this is an anonymous union then we'll want to emit a global
3911 // variable for each member of the anonymous union so that it's possible
3912 // to find the name of any field in the union.
3913 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003914 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003915 assert(RD->isAnonymousStructOrUnion() &&
3916 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003917 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003918 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003919 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003920
3921 SmallVector<int64_t, 4> Expr;
3922 unsigned AddressSpace =
3923 CGM.getContext().getTargetAddressSpace(D->getType());
3924 AppendAddressSpaceXDeref(AddressSpace, Expr);
3925
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003926 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003927 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003928 Var->hasLocalLinkage(),
3929 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00003930 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003931 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003932 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003933 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003934}
3935
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003936void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003937 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003938 if (VD->hasAttr<NoDebugAttr>())
3939 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003940 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003941 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003942 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003943 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003944 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003945 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3946 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003947 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3948 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3949 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003950 // Do not use global variables for enums.
3951 //
3952 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003953 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003954 return;
David Blaikiea15565562014-04-04 20:56:17 +00003955 // Do not emit separate definitions for function local const/statics.
3956 if (isa<FunctionDecl>(VD->getDeclContext()))
3957 return;
David Blaikiebb113912014-04-05 07:23:17 +00003958 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003959 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003960 if (VarD->isStaticDataMember()) {
3961 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003962 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003963 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003964 //
3965 // FIXME: This is probably unnecessary, since Ty should reference RD
3966 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003967 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003968 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003969 return;
3970 }
3971
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003972 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003973
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003974 auto &GV = DeclCache[VD];
3975 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003976 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003977 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003978 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3979 // FIXME: Add a representation for integer constants wider than 64 bits.
3980 if (Init.isInt())
3981 InitExpr =
3982 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3983 else if (Init.isFloat())
3984 InitExpr = DBuilder.createConstantValueExpression(
3985 Init.getFloat().bitcastToAPInt().getZExtValue());
3986 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003987 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003988 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003989 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3990 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003991}
3992
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003993llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003994 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003995 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003996 llvm::DIScope *Mod = getParentModuleOrNull(D);
3997 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003998}
3999
David Blaikie9f88fe82013-04-22 06:13:21 +00004000void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004001 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004002 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004003 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00004004 if (!NSDecl->isAnonymousNamespace() ||
4005 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004006 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004007 DBuilder.createImportedModule(
4008 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004009 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004010 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004011}
4012
David Blaikiebd483762013-05-20 04:58:53 +00004013void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004014 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004015 return;
4016 assert(UD.shadow_size() &&
4017 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4018 // Emitting one decl is sufficient - debuggers can detect that this is an
4019 // overloaded name & provide lookup for all the overloads.
4020 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004021
4022 // FIXME: Skip functions with undeduced auto return type for now since we
4023 // don't currently have the plumbing for separate declarations & definitions
4024 // of free functions and mismatched types (auto in the declaration, concrete
4025 // return type in the definition)
4026 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4027 if (const auto *AT =
4028 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4029 if (AT->getDeducedType().isNull())
4030 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004031 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004032 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4033 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004034 DBuilder.createImportedDeclaration(
4035 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004036 getOrCreateFile(Loc), getLineNumber(Loc));
4037 }
David Blaikiebd483762013-05-20 04:58:53 +00004038}
4039
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004040void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004041 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4042 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004043 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004044 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004045 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004046 DBuilder.createImportedDeclaration(
4047 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004048 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4049 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004050 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004051}
4052
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004053llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004054CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004055 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004056 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004057 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004058 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004059 return cast<llvm::DIImportedEntity>(VH);
4060 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004061 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004062 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004063 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4064 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004065 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004066 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004067 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4068 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004069 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004070 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004071 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004072 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004073 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004074 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004075 return R;
4076}
4077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004078llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004079CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4080 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4081 // if necessary, and this way multiple declarations of the same namespace in
4082 // different parent modules stay distinct.
4083 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004084 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004085 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004086
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004087 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004088 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004089 llvm::DINamespace *NS =
4090 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4091 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004092 return NS;
4093}
4094
Adrian Prantla5206ce2015-09-22 23:26:43 +00004095void CGDebugInfo::setDwoId(uint64_t Signature) {
4096 assert(TheCU && "no main compile unit");
4097 TheCU->setDWOId(Signature);
4098}
4099
4100
Guy Benyei11169dd2012-12-18 14:30:41 +00004101void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004102 // Creating types might create further types - invalidating the current
4103 // element and the size(), so don't cache/reference them.
4104 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4105 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004106 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004107 ? CreateTypeDefinition(E.Type, E.Unit)
4108 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004109 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004110 }
4111
David Blaikief427b002014-05-06 03:42:01 +00004112 for (auto p : ReplaceMap) {
4113 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004114 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004115 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004116
David Blaikief427b002014-05-06 03:42:01 +00004117 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004118 assert(it != TypeCache.end());
4119 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004120
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004121 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4122 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004123 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004124
Frederic Rissd253ed62014-11-18 03:40:51 +00004125 for (const auto &p : FwdDeclReplaceMap) {
4126 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004127 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004128 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004129
4130 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004131 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004132 // with ourselves, that will destroy the temporary MDNode and
4133 // replace it with a standard one, avoiding leaking memory.
4134 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004135 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004136 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004137 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004138
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004139 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4140 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004141 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004142 }
4143
Adrian Prantl73409ce2013-03-11 18:33:46 +00004144 // We keep our own list of retained types, because we need to look
4145 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004146 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004147 if (auto MD = TypeCache[RT])
4148 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004149
Guy Benyei11169dd2012-12-18 14:30:41 +00004150 DBuilder.finalize();
4151}
David Blaikie66088d52014-09-24 17:01:27 +00004152
4153void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004154 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004155 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004156
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004157 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004158 // Don't ignore in case of explicit cast where it is referenced indirectly.
4159 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004160}
Amara Emerson652795d2016-11-10 14:44:30 +00004161
4162llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4163 if (LexicalBlockStack.empty())
4164 return llvm::DebugLoc();
4165
4166 llvm::MDNode *Scope = LexicalBlockStack.back();
4167 return llvm::DebugLoc::get(
4168 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4169}