blob: 5a288abe36596516cff61664ecf3411be7935ace [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;
Serge Pavlov03e672c2017-11-28 16:14:14 +0000268 printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269 }
270
271 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000272 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000273}
274
275StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
276 SmallString<256> MethodName;
277 llvm::raw_svector_ostream OS(MethodName);
278 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
279 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000280 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000281 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000282 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000283 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000284 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000285 if (OC->IsClassExtension()) {
286 OS << OC->getClassInterface()->getName();
287 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000288 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000289 << OC->getIdentifier()->getNameStart() << ')';
290 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000291 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000292 OS << OCD->getClassInterface()->getName() << '('
293 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000294 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000295 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000296 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000297 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000298 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000299 ClassTy.print(OS, PrintingPolicy(LangOptions()));
300 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000301 }
302 OS << ' ' << OMD->getSelector().getAsString() << ']';
303
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000304 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
Guy Benyei11169dd2012-12-18 14:30:41 +0000307StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000308 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000309}
310
Eric Christophere7b87e52014-10-26 23:40:33 +0000311StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000312 if (isa<ClassTemplateSpecializationDecl>(RD)) {
313 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000314 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000315 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000316 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000317
318 // Copy this name on the side and use its reference.
319 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000320 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000321
David Majnemerbc5976a2016-07-01 23:12:54 +0000322 // quick optimization to avoid having to intern strings that are already
323 // stored reliably elsewhere
324 if (const IdentifierInfo *II = RD->getIdentifier())
325 return II->getName();
326
327 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
328 // used to reconstruct the fully qualified type names.
329 if (CGM.getCodeGenOpts().EmitCodeView) {
330 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
331 assert(RD->getDeclContext() == D->getDeclContext() &&
332 "Typedef should not be in another decl context!");
333 assert(D->getDeclName().getAsIdentifierInfo() &&
334 "Typedef was not named!");
335 return D->getDeclName().getAsIdentifierInfo()->getName();
336 }
337
338 if (CGM.getLangOpts().CPlusPlus) {
339 StringRef Name;
340
341 ASTContext &Context = CGM.getContext();
342 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
343 // Anonymous types without a name for linkage purposes have their
344 // declarator mangled in if they have one.
345 Name = DD->getName();
346 else if (const TypedefNameDecl *TND =
347 Context.getTypedefNameForUnnamedTagDecl(RD))
348 // Anonymous types without a name for linkage purposes have their
349 // associate typedef mangled in if they have one.
350 Name = TND->getName();
351
352 if (!Name.empty()) {
353 SmallString<256> UnnamedType("<unnamed-type-");
354 UnnamedType += Name;
355 UnnamedType += '>';
356 return internString(UnnamedType);
357 }
358 }
359 }
360
361 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000362}
363
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000364llvm::DIFile::ChecksumKind
365CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
366 Checksum.clear();
367
368 if (!CGM.getCodeGenOpts().EmitCodeView)
369 return llvm::DIFile::CSK_None;
370
371 SourceManager &SM = CGM.getContext().getSourceManager();
372 bool Invalid;
373 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
374 if (Invalid)
375 return llvm::DIFile::CSK_None;
376
377 llvm::MD5 Hash;
378 llvm::MD5::MD5Result Result;
379
380 Hash.update(MemBuffer->getBuffer());
381 Hash.final(Result);
382
383 Hash.stringifyResult(Result, Checksum);
384 return llvm::DIFile::CSK_MD5;
385}
386
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000387llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000388 if (!Loc.isValid())
389 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000390 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000391 remapDIPath(TheCU->getDirectory()),
392 TheCU->getFile()->getChecksumKind(),
393 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000394
395 SourceManager &SM = CGM.getContext().getSourceManager();
396 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
397
398 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
399 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000400 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000401 remapDIPath(TheCU->getDirectory()),
402 TheCU->getFile()->getChecksumKind(),
403 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000404
405 // Cache the results.
406 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000407 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408
409 if (it != DIFileCache.end()) {
410 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000411 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000412 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 }
414
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000415 SmallString<32> Checksum;
416 llvm::DIFile::ChecksumKind CSKind =
417 computeChecksum(SM.getFileID(Loc), Checksum);
418
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000419 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000420 remapDIPath(getCurrentDirname()),
421 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000422
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000423 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 return F;
425}
426
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000427llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000428 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000429 remapDIPath(TheCU->getDirectory()),
430 TheCU->getFile()->getChecksumKind(),
431 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000432}
433
434std::string CGDebugInfo::remapDIPath(StringRef Path) const {
435 for (const auto &Entry : DebugPrefixMap)
436 if (Path.startswith(Entry.first))
437 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
438 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000439}
440
Guy Benyei11169dd2012-12-18 14:30:41 +0000441unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
442 if (Loc.isInvalid() && CurLoc.isInvalid())
443 return 0;
444 SourceManager &SM = CGM.getContext().getSourceManager();
445 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000447}
448
Adrian Prantlc7822422013-03-12 20:43:25 +0000449unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000450 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000451 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000452 return 0;
453
454 // If the location is invalid then use the current column.
455 if (Loc.isInvalid() && CurLoc.isInvalid())
456 return 0;
457 SourceManager &SM = CGM.getContext().getSourceManager();
458 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000460}
461
462StringRef CGDebugInfo::getCurrentDirname() {
463 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
464 return CGM.getCodeGenOpts().DebugCompilationDir;
465
466 if (!CWDName.empty())
467 return CWDName;
468 SmallString<256> CWD;
469 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000470 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000471}
472
Guy Benyei11169dd2012-12-18 14:30:41 +0000473void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000474 SmallString<32> Checksum;
475 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000476
David Blaikieaabde052014-05-14 00:29:00 +0000477 // Should we be asking the SourceManager for the main file name, instead of
478 // accepting it as an argument? This just causes the main file name to
479 // mismatch with source locations and create extra lexical scopes or
480 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
481 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
482 // because that's what the SourceManager says)
483
Guy Benyei11169dd2012-12-18 14:30:41 +0000484 // Get absolute path name.
485 SourceManager &SM = CGM.getContext().getSourceManager();
486 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
487 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000488 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000489
490 // The main file name provided via the "-main-file-name" option contains just
491 // the file name itself with no path information. This file name may have had
492 // a relative path, so we look into the actual file entry for the main
493 // file to determine the real absolute path for the file.
494 std::string MainFileDir;
495 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000496 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000497 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000498 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
499 llvm::sys::path::append(MainFileDirSS, MainFileName);
500 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000501 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000502 // If the main file name provided is identical to the input file name, and
503 // if the input file is a preprocessed source, use the module name for
504 // debug info. The module name comes from the name specified in the first
505 // linemarker if the input is a preprocessed source.
506 if (MainFile->getName() == MainFileName &&
507 FrontendOptions::getInputKindForExtension(
508 MainFile->getName().rsplit('.').second)
509 .isPreprocessed())
510 MainFileName = CGM.getModule().getName().str();
511
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000512 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 }
514
Ed Masteda706022014-05-07 12:49:30 +0000515 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516 const LangOptions &LO = CGM.getLangOpts();
517 if (LO.CPlusPlus) {
518 if (LO.ObjC1)
519 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
520 else
521 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
522 } else if (LO.ObjC1) {
523 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000524 } else if (LO.RenderScript) {
525 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 } else if (LO.C99) {
527 LangTag = llvm::dwarf::DW_LANG_C99;
528 } else {
529 LangTag = llvm::dwarf::DW_LANG_C89;
530 }
531
532 std::string Producer = getClangFullVersion();
533
534 // Figure out which version of the ObjC runtime we have.
535 unsigned RuntimeVers = 0;
536 if (LO.ObjC1)
537 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
538
Adrian Prantl826824e2016-04-08 22:43:06 +0000539 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
540 switch (DebugKind) {
541 case codegenoptions::NoDebugInfo:
542 case codegenoptions::LocTrackingOnly:
543 EmissionKind = llvm::DICompileUnit::NoDebug;
544 break;
545 case codegenoptions::DebugLineTablesOnly:
546 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
547 break;
548 case codegenoptions::LimitedDebugInfo:
549 case codegenoptions::FullDebugInfo:
550 EmissionKind = llvm::DICompileUnit::FullDebug;
551 break;
552 }
553
Guy Benyei11169dd2012-12-18 14:30:41 +0000554 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000556 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000557 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000558 LangTag,
559 DBuilder.createFile(remapDIPath(MainFileName),
560 remapDIPath(getCurrentDirname()), CSKind, Checksum),
Adrian Prantlb4423022017-08-04 23:08:57 +0000561 Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
562 CGOpts.DwarfDebugFlags, RuntimeVers,
563 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000564 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
565 CGOpts.GnuPubnames);
Guy Benyei11169dd2012-12-18 14:30:41 +0000566}
567
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000568llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000569 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000570 StringRef BTName;
571 switch (BT->getKind()) {
572#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000573#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000574#include "clang/AST/BuiltinTypes.def"
575 case BuiltinType::Dependent:
576 llvm_unreachable("Unexpected builtin type");
577 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000578 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000580 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000581 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000582 if (!ClassTy)
583 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
584 "objc_class", TheCU,
585 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 return ClassTy;
587 case BuiltinType::ObjCId: {
588 // typedef struct objc_class *Class;
589 // typedef struct objc_object {
590 // Class isa;
591 // } *id;
592
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000593 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000594 return ObjTy;
595
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000596 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000597 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
598 "objc_class", TheCU,
599 getOrCreateMainFile(), 0);
600
601 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000602
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000603 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000604
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000605 ObjTy = DBuilder.createStructType(
606 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
607 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000608
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000609 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000610 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
611 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
612 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000613 return ObjTy;
614 }
615 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000616 if (!SelTy)
617 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
618 "objc_selector", TheCU,
619 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 return SelTy;
621 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000622
Alexey Bader954ba212016-04-08 13:40:33 +0000623#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
624 case BuiltinType::Id: \
625 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
626 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000627#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000628 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000629 return getOrCreateStructPtrType("opencl_sampler_t",
630 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000631 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000632 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000633 case BuiltinType::OCLClkEvent:
634 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
635 case BuiltinType::OCLQueue:
636 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000637 case BuiltinType::OCLReserveID:
638 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000639
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000641 case BuiltinType::Char_U:
642 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
643 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000644 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000645 case BuiltinType::SChar:
646 Encoding = llvm::dwarf::DW_ATE_signed_char;
647 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000648 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000649 case BuiltinType::Char32:
650 Encoding = llvm::dwarf::DW_ATE_UTF;
651 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000652 case BuiltinType::UShort:
653 case BuiltinType::UInt:
654 case BuiltinType::UInt128:
655 case BuiltinType::ULong:
656 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000657 case BuiltinType::ULongLong:
658 Encoding = llvm::dwarf::DW_ATE_unsigned;
659 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000660 case BuiltinType::Short:
661 case BuiltinType::Int:
662 case BuiltinType::Int128:
663 case BuiltinType::Long:
664 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000665 case BuiltinType::LongLong:
666 Encoding = llvm::dwarf::DW_ATE_signed;
667 break;
668 case BuiltinType::Bool:
669 Encoding = llvm::dwarf::DW_ATE_boolean;
670 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000671 case BuiltinType::Half:
672 case BuiltinType::Float:
673 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000674 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000675 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000676 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000677 // FIXME: For targets where long double and __float128 have the same size,
678 // they are currently indistinguishable in the debugger without some
679 // special treatment. However, there is currently no consensus on encoding
680 // and this should be updated once a DWARF encoding exists for distinct
681 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000682 Encoding = llvm::dwarf::DW_ATE_float;
683 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000684 }
685
686 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000687 case BuiltinType::Long:
688 BTName = "long int";
689 break;
690 case BuiltinType::LongLong:
691 BTName = "long long int";
692 break;
693 case BuiltinType::ULong:
694 BTName = "long unsigned int";
695 break;
696 case BuiltinType::ULongLong:
697 BTName = "long long unsigned int";
698 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 default:
700 BTName = BT->getName(CGM.getLangOpts());
701 break;
702 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000703 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000705 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000706}
707
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000708llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000709 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000710 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000711 if (Ty->isComplexIntegerType())
712 Encoding = llvm::dwarf::DW_ATE_lo_user;
713
714 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000715 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000716}
717
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000718llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
719 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000720 QualifierCollector Qc;
721 const Type *T = Qc.strip(Ty);
722
723 // Ignore these qualifiers for now.
724 Qc.removeObjCGCAttr();
725 Qc.removeAddressSpace();
726 Qc.removeObjCLifetime();
727
728 // We will create one Derived type for one qualifier and recurse to handle any
729 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000730 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000731 if (Qc.hasConst()) {
732 Tag = llvm::dwarf::DW_TAG_const_type;
733 Qc.removeConst();
734 } else if (Qc.hasVolatile()) {
735 Tag = llvm::dwarf::DW_TAG_volatile_type;
736 Qc.removeVolatile();
737 } else if (Qc.hasRestrict()) {
738 Tag = llvm::dwarf::DW_TAG_restrict_type;
739 Qc.removeRestrict();
740 } else {
741 assert(Qc.empty() && "Unknown type qualifier for debug info");
742 return getOrCreateType(QualType(T, 0), Unit);
743 }
744
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000745 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000746
747 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
748 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000749 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000750}
751
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000752llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
753 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000754
755 // The frontend treats 'id' as a typedef to an ObjCObjectType,
756 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
757 // debug info, we want to emit 'id' in both cases.
758 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000759 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000760
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000761 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
762 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000763}
764
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000765llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
766 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000767 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 Ty->getPointeeType(), Unit);
769}
770
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000771/// \return whether a C++ mangling exists for the type defined by TD.
772static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
773 switch (TheCU->getSourceLanguage()) {
774 case llvm::dwarf::DW_LANG_C_plus_plus:
775 return true;
776 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
777 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
778 default:
779 return false;
780 }
781}
782
Manman Rene0064d82013-08-29 23:19:58 +0000783/// In C++ mode, types have linkage, so we can rely on the ODR and
784/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000785static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
786 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000787 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000788 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000789 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000790
791 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000792 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000793
Manman Rene0064d82013-08-29 23:19:58 +0000794 // TODO: This is using the RTTI name. Is there a better way to get
795 // a unique string for a type?
796 llvm::raw_svector_ostream Out(FullName);
797 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000798 return FullName;
799}
800
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000801/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000802static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
803 llvm::dwarf::Tag Tag;
804 if (RD->isStruct() || RD->isInterface())
805 Tag = llvm::dwarf::DW_TAG_structure_type;
806 else if (RD->isUnion())
807 Tag = llvm::dwarf::DW_TAG_union_type;
808 else {
809 // FIXME: This could be a struct type giving a default visibility different
810 // than C++ class type, but needs llvm metadata changes first.
811 assert(RD->isClass());
812 Tag = llvm::dwarf::DW_TAG_class_type;
813 }
814 return Tag;
815}
816
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000817llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000818CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000819 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000820 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000821 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
822 return cast<llvm::DICompositeType>(T);
823 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 unsigned Line = getLineNumber(RD->getLocation());
825 StringRef RDName = getClassName(RD);
826
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000827 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000828 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000829
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000831 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000832 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000833 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000834 llvm::DINode::FlagFwdDecl, FullName);
Paul Robinson1787f812017-09-28 18:37:02 +0000835 if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
836 if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
837 DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
838 CollectCXXTemplateParams(TSpecial, DefUnit));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000839 ReplaceMap.emplace_back(
840 std::piecewise_construct, std::make_tuple(Ty),
841 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000842 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000843}
844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000845llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000846 const Type *Ty,
847 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000848 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 // Bit size, align and offset of the type.
850 // Size is always the size of a pointer. We can't use getTypeSize here
851 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000852 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
853 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000854 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000855 Optional<unsigned> DWARFAddressSpace =
856 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000857
Keno Fischer87842f32015-11-16 09:04:13 +0000858 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
859 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
860 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000861 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000862 else
863 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000864 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000865}
866
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000867llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
868 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000869 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000870 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000871 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
872 TheCU, getOrCreateMainFile(), 0);
873 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
874 Cache = DBuilder.createPointerType(Cache, Size);
875 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000876}
877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000878llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
879 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000880 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 QualType FType;
882 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000883 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000884 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000885
886 FieldOffset = 0;
887 FType = CGM.getContext().UnsignedLongTy;
888 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
889 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
890
891 Elements = DBuilder.getOrCreateArray(EltTys);
892 EltTys.clear();
893
Leny Kholodov80c047d2016-09-06 10:48:04 +0000894 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000895 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000896
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000897 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000898 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000899 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000900
901 // Bit size, align and offset of the type.
902 uint64_t Size = CGM.getContext().getTypeSize(Ty);
903
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000904 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000905
906 FieldOffset = 0;
907 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
908 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
909 FType = CGM.getContext().IntTy;
910 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
911 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000912 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000913 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
914
915 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000916 FieldSize = CGM.getContext().getTypeSize(Ty);
917 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000918 EltTys.push_back(DBuilder.createMemberType(
919 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
920 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000921
922 FieldOffset += FieldSize;
923 Elements = DBuilder.getOrCreateArray(EltTys);
924
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000925 // The __block_literal_generic structs are marked with a special
926 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
927 // the debugger needs to know about. To allow type uniquing, emit
928 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000929 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000930 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000931 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000932
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000933 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000934}
935
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000936llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
937 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000938 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000939 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000940
941 SmallString<128> NS;
942 llvm::raw_svector_ostream OS(NS);
Serge Pavlov03e672c2017-11-28 16:14:14 +0000943 Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false);
944 printTemplateArgumentList(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
Adrian McCarthyd91bf392017-09-13 20:53:55 +00001411 if (Method->isStatic())
1412 Flags |= llvm::DINode::FlagStaticMember;
Guy Benyei11169dd2012-12-18 14:30:41 +00001413 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001415 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001416 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001418 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001419 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001420 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001421 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 }
1423 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001425 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001427 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001428 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001429
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001430 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1431 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001432 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001433 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1434 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1435 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001436
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001437 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001438
1439 return SP;
1440}
1441
Eric Christophere7b87e52014-10-26 23:40:33 +00001442void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1444 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001445
1446 // Since we want more than just the individual member decls if we
1447 // have templated functions iterate over every declaration to gather
1448 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001449 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001450 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1451 // If the member is implicit, don't add it to the member list. This avoids
1452 // the member being added to type units by LLVM, while still allowing it
1453 // to be emitted into the type declaration/reference inside the compile
1454 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001455 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001456 // FIXME: Handle Using(Shadow?)Decls here to create
1457 // DW_TAG_imported_declarations inside the class for base decls brought into
1458 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1459 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1460 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001461 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001462 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001463
1464 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1465 continue;
1466
David Blaikiefd580722014-10-06 05:18:55 +00001467 // Reuse the existing member function declaration if it exists.
1468 // It may be associated with the declaration of the type & should be
1469 // reused as we're building the definition.
1470 //
1471 // This situation can arise in the vtable-based debug info reduction where
1472 // implicit members are emitted in a non-vtable TU.
1473 auto MI = SPCache.find(Method->getCanonicalDecl());
1474 EltTys.push_back(MI == SPCache.end()
1475 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001476 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001478}
Guy Benyei11169dd2012-12-18 14:30:41 +00001479
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001480void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001481 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001482 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001483 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1484 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1485 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001486
Bob Haarmandff36732016-10-25 22:19:32 +00001487 // If we are generating CodeView debug info, we also need to emit records for
1488 // indirect virtual base classes.
1489 if (CGM.getCodeGenOpts().EmitCodeView) {
1490 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1491 llvm::DINode::FlagIndirectVirtualBase);
1492 }
1493}
1494
1495void CGDebugInfo::CollectCXXBasesAux(
1496 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1497 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1498 const CXXRecordDecl::base_class_const_range &Bases,
1499 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1500 llvm::DINode::DIFlags StartingFlags) {
1501 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1502 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001503 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001504 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001505 if (!SeenTypes.insert(Base).second)
1506 continue;
1507 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1508 llvm::DINode::DIFlags BFlags = StartingFlags;
1509 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001510
Aaron Ballman574705e2014-03-13 15:41:46 +00001511 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001512 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1513 // virtual base offset offset is -ve. The code generator emits dwarf
1514 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001515 BaseOffset = 0 - CGM.getItaniumVTableContext()
1516 .getVirtualBaseOffsetOffset(RD, Base)
1517 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001518 } else {
1519 // In the MS ABI, store the vbtable offset, which is analogous to the
1520 // vbase offset offset in Itanium.
1521 BaseOffset =
1522 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1523 }
Bob Haarmandff36732016-10-25 22:19:32 +00001524 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001525 } else
1526 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1527 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1528 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001529
Adrian Prantl21361fb2014-08-29 22:44:27 +00001530 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001531 llvm::DIType *DTy =
1532 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001533 EltTys.push_back(DTy);
1534 }
1535}
1536
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001537llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001538CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1539 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001540 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001541 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001542 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1543 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001544 StringRef Name;
1545 if (TPList)
1546 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001547 switch (TA.getKind()) {
1548 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001549 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001550 TemplateParams.push_back(
1551 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001552 } break;
1553 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001555 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1556 TheCU, Name, TTy,
1557 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001558 } break;
1559 case TemplateArgument::Declaration: {
1560 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001561 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001562 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001563 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001564 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001565 // Variable pointer template parameters have a value that is the address
1566 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001567 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001568 V = CGM.GetAddrOfGlobalVar(VD);
1569 // Member function pointers have special support for building them, though
1570 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001571 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001572 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001573 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001574 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001575 // Member data pointers have special handling too to compute the fixed
1576 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001577 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001578 // These five lines (& possibly the above member function pointer
1579 // handling) might be able to be refactored to use similar code in
1580 // CodeGenModule::getMemberPointerConstant
1581 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1582 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001583 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001584 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001585 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001586 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1587 TheCU, Name, TTy,
1588 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001589 } break;
1590 case TemplateArgument::NullPtr: {
1591 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001592 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001593 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001594 // Special case member data pointer null values since they're actually -1
1595 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001596 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001597 // But treat member function pointers as simple zero integers because
1598 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1599 // CodeGen grows handling for values of non-null member function
1600 // pointers then perhaps we could remove this special case and rely on
1601 // EmitNullMemberPointer for member function pointers.
1602 if (MPT->isMemberDataPointer())
1603 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1604 if (!V)
1605 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001606 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001607 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001608 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001609 case TemplateArgument::Template:
1610 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001611 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001612 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1613 break;
1614 case TemplateArgument::Pack:
1615 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1616 TheCU, Name, nullptr,
1617 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1618 break;
David Majnemer5559d472013-08-24 08:21:10 +00001619 case TemplateArgument::Expression: {
1620 const Expr *E = TA.getAsExpr();
1621 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001622 if (E->isGLValue())
1623 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001624 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001625 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001626 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001627 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001628 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001629 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001630 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001631 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001632 case TemplateArgument::Null:
1633 llvm_unreachable(
1634 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 }
1636 }
1637 return DBuilder.getOrCreateArray(TemplateParams);
1638}
1639
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001640llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001641CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001642 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 if (FD->getTemplatedKind() ==
1644 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001645 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1646 ->getTemplate()
1647 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001648 return CollectTemplateParams(
1649 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001650 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001651 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001652}
1653
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001654llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1655 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001656 // Always get the full list of parameters, not just the ones from
1657 // the specialization.
1658 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001659 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001660 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001661 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001662}
1663
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001664llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001665 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001666 return VTablePtrType;
1667
1668 ASTContext &Context = CGM.getContext();
1669
1670 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001671 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001672 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001673 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001675 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1676 Optional<unsigned> DWARFAddressSpace =
1677 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001680 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1681 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001682 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1683 return VTablePtrType;
1684}
1685
Guy Benyei11169dd2012-12-18 14:30:41 +00001686StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001687 // Copy the gdb compatible name on the side and use its reference.
1688 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001689}
1690
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001691void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001692 SmallVectorImpl<llvm::Metadata *> &EltTys,
1693 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 // If this class is not dynamic then there is not any vtable info to collect.
1695 if (!RD->isDynamicClass())
1696 return;
1697
Reid Kleckner59812422016-08-31 20:35:01 +00001698 // Don't emit any vtable shape or vptr info if this class doesn't have an
1699 // extendable vfptr. This can happen if the class doesn't have virtual
1700 // methods, or in the MS ABI if those virtual methods only come from virtually
1701 // inherited bases.
1702 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1703 if (!RL.hasExtendableVFPtr())
1704 return;
1705
Reid Klecknerdc124992016-08-31 16:11:43 +00001706 // CodeView needs to know how large the vtable of every dynamic class is, so
1707 // emit a special named pointer type into the element list. The vptr type
1708 // points to this type as well.
1709 llvm::DIType *VPtrTy = nullptr;
1710 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1711 CGM.getTarget().getCXXABI().isMicrosoft();
1712 if (NeedVTableShape) {
1713 uint64_t PtrWidth =
1714 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1715 const VTableLayout &VFTLayout =
1716 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1717 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001718 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001719 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001720 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1721 Optional<unsigned> DWARFAddressSpace =
1722 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001723
1724 // Create a very wide void* type and insert it directly in the element list.
1725 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001726 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1727 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001728 EltTys.push_back(VTableType);
1729
1730 // The vptr is a pointer to this special vtable type.
1731 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1732 }
1733
1734 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001735 if (RL.getPrimaryBase())
1736 return;
1737
1738 if (!VPtrTy)
1739 VPtrTy = getOrCreateVTablePtrType(Unit);
1740
Guy Benyei11169dd2012-12-18 14:30:41 +00001741 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001742 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001743 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001744 llvm::DINode::FlagArtificial, VPtrTy);
1745 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001746}
1747
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001748llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001749 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001750 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001751 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001752 return T;
1753}
1754
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001755llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001756 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001757 return getOrCreateStandaloneType(D, Loc);
1758}
1759
1760llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1761 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001762 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001763 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001764 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001765 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001766
Adrian Prantl73409ce2013-03-11 18:33:46 +00001767 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001768 return T;
1769}
1770
David Blaikie483a9da2014-05-06 18:35:21 +00001771void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001772 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001773 return;
1774 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001775 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001776 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001777 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001778 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001779 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001780 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001781 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001782}
1783
David Blaikieb2e86eb2013-08-15 20:49:17 +00001784void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001785 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001786 !CGM.getLangOpts().CPlusPlus)
1787 completeRequiredType(RD);
1788}
1789
David Blaikieb11c8732017-01-30 06:36:08 +00001790/// Return true if the class or any of its methods are marked dllimport.
1791static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1792 if (RD->hasAttr<DLLImportAttr>())
1793 return true;
1794 for (const CXXMethodDecl *MD : RD->methods())
1795 if (MD->hasAttr<DLLImportAttr>())
1796 return true;
1797 return false;
1798}
1799
Adrian Prantla43acdc2017-07-24 23:48:51 +00001800/// Does a type definition exist in an imported clang module?
1801static bool isDefinedInClangModule(const RecordDecl *RD) {
1802 // Only definitions that where imported from an AST file come from a module.
1803 if (!RD || !RD->isFromASTFile())
1804 return false;
1805 // Anonymous entities cannot be addressed. Treat them as not from module.
1806 if (!RD->isExternallyVisible() && RD->getName().empty())
1807 return false;
1808 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1809 if (!CXXDecl->isCompleteDefinition())
1810 return false;
1811 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1812 if (TemplateKind != TSK_Undeclared) {
1813 // This is a template, check the origin of the first member.
1814 if (CXXDecl->field_begin() == CXXDecl->field_end())
1815 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1816 if (!CXXDecl->field_begin()->isFromASTFile())
1817 return false;
1818 }
1819 }
1820 return true;
1821}
1822
David Blaikie6943dea2013-08-20 01:28:15 +00001823void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001824 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1825 if (CXXRD->isDynamicClass() &&
1826 CGM.getVTableLinkage(CXXRD) ==
1827 llvm::GlobalValue::AvailableExternallyLinkage &&
1828 !isClassOrMethodDLLImport(CXXRD))
1829 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001830
1831 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1832 return;
1833
David Blaikieb11c8732017-01-30 06:36:08 +00001834 completeClass(RD);
1835}
1836
1837void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001838 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001839 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001840 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001841 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001842 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001843 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001844 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001845 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001846 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001847 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001848}
1849
David Blaikie0e716b42014-03-03 23:48:23 +00001850static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1851 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001852 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1853 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001854 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001855 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001856 return true;
1857 return false;
1858}
1859
Benjamin Kramer8c305922016-02-02 11:06:51 +00001860static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1861 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001862 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001863 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001864 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001865
David Blaikie1ac9c982017-04-11 21:13:37 +00001866 if (auto *ES = RD->getASTContext().getExternalSource())
1867 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1868 return true;
1869
Benjamin Kramer8c305922016-02-02 11:06:51 +00001870 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001871 return false;
1872
1873 if (!LangOpts.CPlusPlus)
1874 return false;
1875
1876 if (!RD->isCompleteDefinitionRequired())
1877 return true;
1878
David Majnemer58ed0f32016-07-17 00:39:12 +00001879 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001880
1881 if (!CXXDecl)
1882 return false;
1883
Adrian McCarthy99242982016-08-16 22:11:18 +00001884 // Only emit complete debug info for a dynamic class when its vtable is
1885 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001886 // across DLL boundaries, so skip this optimization if the class or any of its
1887 // methods are marked dllimport. This isn't a complete solution, since objects
1888 // without any dllimport methods can be used in one DLL and constructed in
1889 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001890 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001891 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001892 return true;
1893
1894 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001895 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001896 Spec = SD->getSpecializationKind();
1897
1898 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1899 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1900 CXXDecl->method_end()))
1901 return true;
1902
1903 return false;
1904}
1905
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001906void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1907 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1908 return;
1909
1910 QualType Ty = CGM.getContext().getRecordType(RD);
1911 llvm::DIType *T = getTypeOrNull(Ty);
1912 if (T && T->isForwardDecl())
1913 completeClassData(RD);
1914}
1915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001917 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001918 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001919 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1920 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001921 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001922 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001923 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001924 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001925
David Blaikieb2e86eb2013-08-15 20:49:17 +00001926 return CreateTypeDefinition(Ty);
1927}
1928
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001930 RecordDecl *RD = Ty->getDecl();
1931
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001933 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001934
1935 // Records and classes and unions can all be recursive. To handle them, we
1936 // first generate a debug descriptor for the struct as a forward declaration.
1937 // Then (if it is a definition) we go through and get debug info for all of
1938 // its members. Finally, we create a descriptor for the complete type (which
1939 // may refer to the forward decl if the struct is recursive) and replace all
1940 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001941 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001942
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001943 const RecordDecl *D = RD->getDefinition();
1944 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001945 return FwdDecl;
1946
David Majnemer58ed0f32016-07-17 00:39:12 +00001947 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001948 CollectContainingType(CXXDecl, FwdDecl);
1949
Guy Benyei11169dd2012-12-18 14:30:41 +00001950 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001951 LexicalBlockStack.emplace_back(&*FwdDecl);
1952 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001953
Guy Benyei11169dd2012-12-18 14:30:41 +00001954 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001955 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001956 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001957
1958 // Note: The split of CXXDecl information here is intentional, the
1959 // gdb tests will depend on a certain ordering at printout. The debug
1960 // information offsets are still correct if we merge them all together
1961 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001962 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 if (CXXDecl) {
1964 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001965 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 }
1967
Eric Christopher91a31902013-01-16 01:22:32 +00001968 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001969 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001970 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001971 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001972
1973 LexicalBlockStack.pop_back();
1974 RegionMap.erase(Ty->getDecl());
1975
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001976 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001977 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001978
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001979 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001980 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001981 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001982
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001983 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001984 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001985}
1986
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001987llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1988 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 // Ignore protocols.
1990 return getOrCreateType(Ty->getBaseType(), Unit);
1991}
1992
Manman Rene6be26c2016-09-13 17:25:08 +00001993llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1994 llvm::DIFile *Unit) {
1995 // Ignore protocols.
1996 SourceLocation Loc = Ty->getDecl()->getLocation();
1997
1998 // Use Typedefs to represent ObjCTypeParamType.
1999 return DBuilder.createTypedef(
2000 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
2001 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2002 getDeclContextDescriptor(Ty->getDecl()));
2003}
2004
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002005/// \return true if Getter has the default name for the property PD.
2006static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2007 const ObjCMethodDecl *Getter) {
2008 assert(PD);
2009 if (!Getter)
2010 return true;
2011
2012 assert(Getter->getDeclName().isObjCZeroArgSelector());
2013 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002014 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002015}
2016
2017/// \return true if Setter has the default name for the property PD.
2018static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2019 const ObjCMethodDecl *Setter) {
2020 assert(PD);
2021 if (!Setter)
2022 return true;
2023
2024 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002025 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002026 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002027}
2028
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002029llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2030 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 ObjCInterfaceDecl *ID = Ty->getDecl();
2032 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002033 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002034
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002035 // Return a forward declaration if this type was imported from a clang module,
2036 // and this is not the compile unit with the implementation of the type (which
2037 // may contain hidden ivars).
2038 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2039 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002040 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2041 ID->getName(),
2042 getDeclContextDescriptor(ID), Unit, 0);
2043
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002045 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002047 auto RuntimeLang =
2048 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002049
2050 // If this is just a forward declaration return a special forward-declaration
2051 // debug type since we won't be able to lay out the entire type.
2052 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002053 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002054 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002055 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002056 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2057 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002058 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 return FwdDecl;
2060 }
2061
David Blaikieef8a9512014-05-05 23:23:53 +00002062 return CreateTypeDefinition(Ty, Unit);
2063}
2064
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002065llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002066CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2067 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002068 // Use the Module pointer as the key into the cache. This is a
2069 // nullptr if the "Module" is a PCH, which is safe because we don't
2070 // support chained PCH debug info, so there can only be a single PCH.
2071 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002072 auto ModRef = ModuleCache.find(M);
2073 if (ModRef != ModuleCache.end())
2074 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002075
2076 // Macro definitions that were defined with "-D" on the command line.
2077 SmallString<128> ConfigMacros;
2078 {
2079 llvm::raw_svector_ostream OS(ConfigMacros);
2080 const auto &PPOpts = CGM.getPreprocessorOpts();
2081 unsigned I = 0;
2082 // Translate the macro definitions back into a commmand line.
2083 for (auto &M : PPOpts.Macros) {
2084 if (++I > 1)
2085 OS << " ";
2086 const std::string &Macro = M.first;
2087 bool Undef = M.second;
2088 OS << "\"-" << (Undef ? 'U' : 'D');
2089 for (char c : Macro)
2090 switch (c) {
2091 case '\\' : OS << "\\\\"; break;
2092 case '"' : OS << "\\\""; break;
2093 default: OS << c;
2094 }
2095 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002096 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002097 }
Adrian Prantl66689202015-09-18 23:01:45 +00002098
Adrian Prantl835e6632015-09-24 16:10:10 +00002099 bool IsRootModule = M ? !M->Parent : true;
2100 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002101 // PCH files don't have a signature field in the control block,
2102 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002103 // We use the lower 64 bits for debug info.
2104 uint64_t Signature =
2105 Mod.getSignature()
2106 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2107 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002108 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002109 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2110 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2111 TheCU->getProducer(), true, StringRef(), 0,
2112 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2113 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002114 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002115 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002116 llvm::DIModule *Parent =
2117 IsRootModule ? nullptr
2118 : getOrCreateModuleRef(
2119 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2120 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002121 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002122 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2123 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002124 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002125 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002126}
2127
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002128llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2129 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002130 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002131 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002132 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002133 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002134
2135 // Bit size, align and offset of the type.
2136 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002137 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002138
Leny Kholodov80c047d2016-09-06 10:48:04 +00002139 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002141 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002142
Adrian Prantlfd696112015-10-01 00:48:51 +00002143 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002144 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002145 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2146 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002147
David Blaikieef8a9512014-05-05 23:23:53 +00002148 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002149 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002150
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002151 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002152 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002153 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002154
2155 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002156 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002157
2158 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2159 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002160 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002161 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002162 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002163 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002164
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002165 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2166 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 EltTys.push_back(InhTag);
2168 }
2169
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002170 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002171 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002172 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002173 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 unsigned PLine = getLineNumber(Loc);
2175 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2176 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002177 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2178 PD->getName(), PUnit, PLine,
2179 hasDefaultGetterName(PD, Getter) ? ""
2180 : getSelectorName(PD->getGetterName()),
2181 hasDefaultSetterName(PD, Setter) ? ""
2182 : getSelectorName(PD->getSetterName()),
2183 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002185 };
2186 {
2187 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002188 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002189 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002190 PropertySet.insert(PD->getIdentifier());
2191 AddProperty(PD);
2192 }
Manman Renefe1bac2016-01-27 20:00:32 +00002193 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002194 // Don't emit duplicate metadata for properties that were already in a
2195 // class extension.
2196 if (!PropertySet.insert(PD->getIdentifier()).second)
2197 continue;
2198 AddProperty(PD);
2199 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 }
2201
2202 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2203 unsigned FieldNo = 0;
2204 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2205 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002206 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002207 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002208 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002209
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 StringRef FieldName = Field->getName();
2211
2212 // Ignore unnamed fields.
2213 if (FieldName.empty())
2214 continue;
2215
2216 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002217 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 unsigned FieldLine = getLineNumber(Field->getLocation());
2219 QualType FType = Field->getType();
2220 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002221 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002222
2223 if (!FType->isIncompleteArrayType()) {
2224
2225 // Bit size, align and offset of the type.
2226 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002227 ? Field->getBitWidthValue(CGM.getContext())
2228 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002229 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 }
2231
2232 uint64_t FieldOffset;
2233 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2234 // We don't know the runtime offset of an ivar if we're using the
2235 // non-fragile ABI. For bitfields, use the bit offset into the first
2236 // byte of storage of the bitfield. For other fields, use zero.
2237 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002238 FieldOffset =
2239 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002240 FieldOffset %= CGM.getContext().getCharWidth();
2241 } else {
2242 FieldOffset = 0;
2243 }
2244 } else {
2245 FieldOffset = RL.getFieldOffset(FieldNo);
2246 }
2247
Leny Kholodov80c047d2016-09-06 10:48:04 +00002248 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002250 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002251 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002252 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002253 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002254 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002255
Craig Topper8a13c412014-05-21 05:09:00 +00002256 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002257 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002258 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002259 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002261 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002262 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002263 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002264 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2265 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002266 PropertyNode = DBuilder.createObjCProperty(
2267 PD->getName(), PUnit, PLine,
2268 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2269 PD->getGetterName()),
2270 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2271 PD->getSetterName()),
2272 PD->getPropertyAttributes(),
2273 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002274 }
2275 }
2276 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002277 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2278 FieldSize, FieldAlign, FieldOffset, Flags,
2279 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002280 EltTys.push_back(FieldTy);
2281 }
2282
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002283 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002284 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002285
Guy Benyei11169dd2012-12-18 14:30:41 +00002286 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002287 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002288}
2289
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002290llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2291 llvm::DIFile *Unit) {
2292 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 int64_t Count = Ty->getNumElements();
2294 if (Count == 0)
2295 // If number of elements are not known then this is an unbounded array.
2296 // Use Count == -1 to express such arrays.
2297 Count = -1;
2298
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002299 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002301
2302 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002303 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002304
2305 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2306}
2307
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002308llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002310 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002311
2312 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002313 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002314 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002315 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2316 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 } else if (Ty->isIncompleteArrayType()) {
2318 Size = 0;
2319 if (Ty->getElementType()->isIncompleteType())
2320 Align = 0;
2321 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002322 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002323 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002324 Size = 0;
2325 Align = 0;
2326 } else {
2327 // Size and align of the whole array, not the element type.
2328 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002329 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 }
2331
2332 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2333 // interior arrays, do we care? Why aren't nested arrays represented the
2334 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002335 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002336 QualType EltTy(Ty, 0);
2337 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2338 // If the number of elements is known, then count is that number. Otherwise,
2339 // it's -1. This allows us to represent a subrange with an array of 0
2340 // elements, like this:
2341 //
2342 // struct foo {
2343 // int x[0];
2344 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002345 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002346 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002348 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002349 if (Expr *Size = VAT->getSizeExpr()) {
2350 llvm::APSInt V;
2351 if (Size->EvaluateAsInt(V, CGM.getContext()))
2352 Count = V.getExtValue();
2353 }
David Blaikie87173f12016-08-22 17:49:56 +00002354 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002355
Guy Benyei11169dd2012-12-18 14:30:41 +00002356 // FIXME: Verify this is right for VLAs.
2357 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2358 EltTy = Ty->getElementType();
2359 }
2360
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002361 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002362
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002363 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2364 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002365}
2366
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002367llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2368 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002369 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2370 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002371}
2372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002373llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2374 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002375 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2376 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002377}
2378
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002379llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2380 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002381 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002382 uint64_t Size = 0;
2383
2384 if (!Ty->isIncompleteType()) {
2385 Size = CGM.getContext().getTypeSize(Ty);
2386
2387 // Set the MS inheritance model. There is no flag for the unspecified model.
2388 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2389 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2390 case MSInheritanceAttr::Keyword_single_inheritance:
2391 Flags |= llvm::DINode::FlagSingleInheritance;
2392 break;
2393 case MSInheritanceAttr::Keyword_multiple_inheritance:
2394 Flags |= llvm::DINode::FlagMultipleInheritance;
2395 break;
2396 case MSInheritanceAttr::Keyword_virtual_inheritance:
2397 Flags |= llvm::DINode::FlagVirtualInheritance;
2398 break;
2399 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2400 break;
2401 }
2402 }
2403 }
2404
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002405 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002406 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002407 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002408 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2409 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002410
2411 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002412 Ty->getPointeeType()->getAs<FunctionProtoType>();
2413 return DBuilder.createMemberPointerType(
2414 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2415 Ty->getClass(), FPT->getTypeQuals())),
2416 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002417 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002418}
2419
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002420llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002421 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2422 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002423}
2424
Xiuli Pan9c14e282016-01-09 12:53:17 +00002425llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2426 llvm::DIFile *U) {
2427 return getOrCreateType(Ty->getElementType(), U);
2428}
2429
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002430llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002431 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002432
Guy Benyei11169dd2012-12-18 14:30:41 +00002433 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002434 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002435 if (!ED->getTypeForDecl()->isIncompleteType()) {
2436 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002437 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002438 }
2439
Manman Rene0064d82013-08-29 23:19:58 +00002440 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2441
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002442 bool isImportedFromModule =
2443 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2444
Guy Benyei11169dd2012-12-18 14:30:41 +00002445 // If this is just a forward declaration, construct an appropriately
2446 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002447 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002448 // Note that it is possible for enums to be created as part of
2449 // their own declcontext. In this case a FwdDecl will be created
2450 // twice. This doesn't cause a problem because both FwdDecls are
2451 // entered into the ReplaceMap: finalize() will replace the first
2452 // FwdDecl with the second and then replace the second with
2453 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002454 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002456 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2457 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002458
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 unsigned Line = getLineNumber(ED->getLocation());
2460 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002462 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2463 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002464
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002465 ReplaceMap.emplace_back(
2466 std::piecewise_construct, std::make_tuple(Ty),
2467 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002468 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002469 }
2470
David Blaikie483a9da2014-05-06 18:35:21 +00002471 return CreateTypeDefinition(Ty);
2472}
2473
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002474llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002475 const EnumDecl *ED = Ty->getDecl();
2476 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002477 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002478 if (!ED->getTypeForDecl()->isIncompleteType()) {
2479 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002480 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002481 }
2482
2483 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2484
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002485 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002486 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002488 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002489 Enumerators.push_back(DBuilder.createEnumerator(
2490 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002491 }
2492
2493 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002495
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002496 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002498 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002499 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002500 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2501 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2502 Line, Size, Align, EltArray, ClassTy,
2503 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002504}
2505
Amjad Aboud546bc112017-02-09 22:07:24 +00002506llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2507 unsigned MType, SourceLocation LineLoc,
2508 StringRef Name, StringRef Value) {
2509 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2510 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2511}
2512
2513llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2514 SourceLocation LineLoc,
2515 SourceLocation FileLoc) {
2516 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2517 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2518 return DBuilder.createTempMacroFile(Parent, Line, FName);
2519}
2520
David Blaikie05491062013-01-21 04:37:12 +00002521static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2522 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002524 Qualifiers InnerQuals = T.getLocalQualifiers();
2525 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2526 // that is already there.
2527 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2528 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 QualType LastT = T;
2530 switch (T->getTypeClass()) {
2531 default:
David Blaikie05491062013-01-21 04:37:12 +00002532 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002533 case Type::TemplateSpecialization: {
2534 const auto *Spec = cast<TemplateSpecializationType>(T);
2535 if (Spec->isTypeAlias())
2536 return C.getQualifiedType(T.getTypePtr(), Quals);
2537 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002538 break;
2539 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002540 case Type::TypeOfExpr:
2541 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2542 break;
2543 case Type::TypeOf:
2544 T = cast<TypeOfType>(T)->getUnderlyingType();
2545 break;
2546 case Type::Decltype:
2547 T = cast<DecltypeType>(T)->getUnderlyingType();
2548 break;
2549 case Type::UnaryTransform:
2550 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2551 break;
2552 case Type::Attributed:
2553 T = cast<AttributedType>(T)->getEquivalentType();
2554 break;
2555 case Type::Elaborated:
2556 T = cast<ElaboratedType>(T)->getNamedType();
2557 break;
2558 case Type::Paren:
2559 T = cast<ParenType>(T)->getInnerType();
2560 break;
David Blaikie05491062013-01-21 04:37:12 +00002561 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002564 case Type::Auto:
2565 case Type::DeducedTemplateSpecialization: {
2566 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002567 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002568 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 break;
2570 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002571 case Type::Adjusted:
2572 case Type::Decayed:
2573 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2574 T = cast<AdjustedType>(T)->getAdjustedType();
2575 break;
2576 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002577
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002579 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 } while (true);
2581}
2582
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002583llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002584
2585 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002586 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002587
David Blaikief427b002014-05-06 03:42:01 +00002588 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 if (it != TypeCache.end()) {
2590 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002591 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002592 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 }
2594
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002595 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002596}
2597
David Blaikie0e716b42014-03-03 23:48:23 +00002598void CGDebugInfo::completeTemplateDefinition(
2599 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002600 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002601 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002602 completeUnusedClass(SD);
2603}
David Blaikie0856f662014-03-04 22:01:08 +00002604
David Blaikie1ac9c982017-04-11 21:13:37 +00002605void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2606 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2607 return;
2608
2609 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002610 // In case this type has no member function definitions being emitted, ensure
2611 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002612 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002613}
2614
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002615llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002616 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002617 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002618
2619 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002620 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002621
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002622 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 return T;
2624
Adrian Prantlca844182015-09-11 17:23:03 +00002625 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002626 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002627
2628 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002629 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002630
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 return Res;
2632}
2633
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002634llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002635 // A forward declaration inside a module header does not belong to the module.
2636 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2637 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002638 if (DebugTypeExtRefs && D->isFromASTFile()) {
2639 // Record a reference to an imported clang module or precompiled header.
2640 auto *Reader = CGM.getContext().getExternalSource();
2641 auto Idx = D->getOwningModuleID();
2642 auto Info = Reader->getSourceDescriptor(Idx);
2643 if (Info)
2644 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2645 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002646 // We are building a clang module or a precompiled header.
2647 //
2648 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2649 // and it wouldn't be necessary to specify the parent scope
2650 // because the type is already unique by definition (it would look
2651 // like the output of -fno-standalone-debug). On the other hand,
2652 // the parent scope helps a consumer to quickly locate the object
2653 // file where the type's definition is located, so it might be
2654 // best to make this behavior a command line or debugger tuning
2655 // option.
2656 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
Richard Smith54f04402017-05-18 02:29:20 +00002657 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002658 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002659 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2660 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002661 } else {
2662 // This the precompiled header being built.
2663 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002664 }
2665 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002666
Adrian Prantl9402cef2015-09-20 16:51:35 +00002667 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002668}
2669
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002670llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002671 // Handle qualifiers, which recursively handles what they refer to.
2672 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002673 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002674
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 // Work out details of type.
2676 switch (Ty->getTypeClass()) {
2677#define TYPE(Class, Base)
2678#define ABSTRACT_TYPE(Class, Base)
2679#define NON_CANONICAL_TYPE(Class, Base)
2680#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2681#include "clang/AST/TypeNodes.def"
2682 llvm_unreachable("Dependent types cannot show up in debug information");
2683
2684 case Type::ExtVector:
2685 case Type::Vector:
2686 return CreateType(cast<VectorType>(Ty), Unit);
2687 case Type::ObjCObjectPointer:
2688 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2689 case Type::ObjCObject:
2690 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002691 case Type::ObjCTypeParam:
2692 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002693 case Type::ObjCInterface:
2694 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2695 case Type::Builtin:
2696 return CreateType(cast<BuiltinType>(Ty));
2697 case Type::Complex:
2698 return CreateType(cast<ComplexType>(Ty));
2699 case Type::Pointer:
2700 return CreateType(cast<PointerType>(Ty), Unit);
2701 case Type::BlockPointer:
2702 return CreateType(cast<BlockPointerType>(Ty), Unit);
2703 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002704 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002706 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002708 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 case Type::FunctionProto:
2710 case Type::FunctionNoProto:
2711 return CreateType(cast<FunctionType>(Ty), Unit);
2712 case Type::ConstantArray:
2713 case Type::VariableArray:
2714 case Type::IncompleteArray:
2715 return CreateType(cast<ArrayType>(Ty), Unit);
2716
2717 case Type::LValueReference:
2718 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2719 case Type::RValueReference:
2720 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2721
2722 case Type::MemberPointer:
2723 return CreateType(cast<MemberPointerType>(Ty), Unit);
2724
2725 case Type::Atomic:
2726 return CreateType(cast<AtomicType>(Ty), Unit);
2727
Xiuli Pan9c14e282016-01-09 12:53:17 +00002728 case Type::Pipe:
2729 return CreateType(cast<PipeType>(Ty), Unit);
2730
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002732 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2733
David Blaikie42edade2014-11-11 20:44:45 +00002734 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002735 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002736 case Type::Adjusted:
2737 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002738 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 case Type::Elaborated:
2740 case Type::Paren:
2741 case Type::SubstTemplateTypeParm:
2742 case Type::TypeOfExpr:
2743 case Type::TypeOf:
2744 case Type::Decltype:
2745 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002746 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002747 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002748 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002749
David Blaikie42edade2014-11-11 20:44:45 +00002750 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002751}
2752
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002753llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2754 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002755 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002756
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002757 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002758
2759 // We may have cached a forward decl when we could have created
2760 // a non-forward decl. Go ahead and create a non-forward decl
2761 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002762 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002763 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002764
2765 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002766 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002767
2768 // Propagate members from the declaration to the definition
2769 // CreateType(const RecordType*) will overwrite this with the members in the
2770 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002771 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002772
Guy Benyei11169dd2012-12-18 14:30:41 +00002773 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002774 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 return Res;
2776}
2777
2778// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002779llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002781
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002783 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 unsigned Line = getLineNumber(RD->getLocation());
2785 StringRef RDName = getClassName(RD);
2786
Amjad Abouddc4531e2016-04-30 01:44:38 +00002787 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002788
David Blaikied2785892013-08-18 17:36:19 +00002789 // If we ended up creating the type during the context chain construction,
2790 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002791 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002792 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002793 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002794 return T;
David Blaikied2785892013-08-18 17:36:19 +00002795
Adrian Prantl381e7552014-02-04 21:29:50 +00002796 // If this is just a forward or incomplete declaration, construct an
2797 // appropriately marked node and just return it.
2798 const RecordDecl *D = RD->getDefinition();
2799 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002800 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002801
2802 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002803 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002804
Manman Rene0064d82013-08-29 23:19:58 +00002805 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002807 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002808 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2809 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002810
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002811 // Elements of composite types usually have back to the type, creating
2812 // uniquing cycles. Distinct nodes are more efficient.
2813 switch (RealDecl->getTag()) {
2814 default:
2815 llvm_unreachable("invalid composite type tag");
2816
2817 case llvm::dwarf::DW_TAG_array_type:
2818 case llvm::dwarf::DW_TAG_enumeration_type:
2819 // Array elements and most enumeration elements don't have back references,
2820 // so they don't tend to be involved in uniquing cycles and there is some
2821 // chance of merging them when linking together two modules. Only make
2822 // them distinct if they are ODR-uniqued.
2823 if (FullName.empty())
2824 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002825 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002826
2827 case llvm::dwarf::DW_TAG_structure_type:
2828 case llvm::dwarf::DW_TAG_union_type:
2829 case llvm::dwarf::DW_TAG_class_type:
2830 // Immediatley resolve to a distinct node.
2831 RealDecl =
2832 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2833 break;
2834 }
2835
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002836 RegionMap[Ty->getDecl()].reset(RealDecl);
2837 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002838
David Majnemer58ed0f32016-07-17 00:39:12 +00002839 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002840 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002841 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002842 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002843}
2844
David Blaikieadfbf992013-08-18 16:55:33 +00002845void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002846 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002847 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002848 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002849 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2850 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002851 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002852 while (1) {
2853 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2854 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2855 if (PBT && !BRL.isPrimaryBaseVirtual())
2856 PBase = PBT;
2857 else
2858 break;
2859 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002860 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002861 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2862 getOrCreateFile(RD->getLocation())));
2863 } else if (RD->isDynamicClass())
2864 ContainingType = RealDecl;
2865
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002866 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002867}
2868
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002869llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002870 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002871 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002872 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002873 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002874 llvm::DIType *Ty =
2875 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2876 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002877 *Offset += FieldSize;
2878 return Ty;
2879}
2880
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002881void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002882 StringRef &Name,
2883 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002884 llvm::DIScope *&FDContext,
2885 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002886 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002887 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002888 Name = getFunctionName(FD);
2889 // Use mangled name as linkage name for C/C++ functions.
2890 if (FD->hasPrototype()) {
2891 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002892 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002893 }
2894 // No need to replicate the linkage name if it isn't different from the
2895 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002896 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002897 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2898 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002899 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002900 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002901 LinkageName = StringRef();
2902
Benjamin Kramer8c305922016-02-02 11:06:51 +00002903 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002904 if (const NamespaceDecl *NSDecl =
Adrian Prantl6fc88752017-05-16 23:46:10 +00002905 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00002906 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00002907 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002908 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2909 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2910 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2911 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002912 // Check if it is a noreturn-marked function
2913 if (FD->isNoReturn())
2914 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002915 // Collect template parameters.
2916 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2917 }
2918}
2919
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002920void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002921 unsigned &LineNo, QualType &T,
2922 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002923 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002924 Unit = getOrCreateFile(VD->getLocation());
2925 LineNo = getLineNumber(VD->getLocation());
2926
2927 setLocation(VD->getLocation());
2928
2929 T = VD->getType();
2930 if (T->isIncompleteArrayType()) {
2931 // CodeGen turns int[] into int[1] so we'll do the same here.
2932 llvm::APInt ConstVal(32, 1);
2933 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2934
2935 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2936 ArrayType::Normal, 0);
2937 }
2938
2939 Name = VD->getName();
2940 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2941 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2942 LinkageName = CGM.getMangledName(VD);
2943 if (LinkageName == Name)
2944 LinkageName = StringRef();
2945
2946 // Since we emit declarations (DW_AT_members) for static members, place the
2947 // definition of those static members in the namespace they were declared in
2948 // in the source code (the lexical decl context).
2949 // FIXME: Generalize this for even non-member global variables where the
2950 // declaration and definition may have different lexical decl contexts, once
2951 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002952 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2953 : VD->getDeclContext();
2954 // When a record type contains an in-line initialization of a static data
2955 // member, and the record type is marked as __declspec(dllexport), an implicit
2956 // definition of the member will be created in the record context. DWARF
2957 // doesn't seem to have a nice way to describe this in a form that consumers
2958 // are likely to understand, so fake the "normal" situation of a definition
2959 // outside the class by putting it in the global scope.
2960 if (DC->isRecord())
2961 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002962
Amjad Abouddc4531e2016-04-30 01:44:38 +00002963 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2964 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002965}
2966
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002967llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2968 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002969 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002970 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002971 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002972 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002973 llvm::DIFile *Unit = getOrCreateFile(Loc);
2974 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002975 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002976 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002977 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002978 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2979
Frederic Rissd253ed62014-11-18 03:40:51 +00002980 // Build function type.
2981 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002982 if (FD)
2983 for (const ParmVarDecl *Parm : FD->parameters())
2984 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002985 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2986 QualType FnType = CGM.getContext().getFunctionType(
2987 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002988 if (Stub) {
2989 return DBuilder.createFunction(
2990 DContext, Name, LinkageName, Unit, Line,
2991 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2992 !FD->isExternallyVisible(),
2993 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2994 TParamsArray.get(), getFunctionDeclaration(FD));
2995 }
2996
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002997 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002998 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002999 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3000 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003001 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003002 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00003003 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003004 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3005 std::make_tuple(CanonDecl),
3006 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003007 return SP;
3008}
3009
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003010llvm::DISubprogram *
3011CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
3012 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3013}
3014
3015llvm::DISubprogram *
3016CGDebugInfo::getFunctionStub(GlobalDecl GD) {
3017 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3018}
3019
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003020llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003021CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3022 QualType T;
3023 StringRef Name, LinkageName;
3024 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003025 llvm::DIFile *Unit = getOrCreateFile(Loc);
3026 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003027 unsigned Line = getLineNumber(Loc);
3028
3029 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003030 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003031 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3032 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003033 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003034 FwdDeclReplaceMap.emplace_back(
3035 std::piecewise_construct,
3036 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3037 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003038 return GV;
3039}
3040
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003041llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003042 // We only need a declaration (not a definition) of the type - so use whatever
3043 // we would otherwise do to get a type for a pointee. (forward declarations in
3044 // limited debug info, full definitions (if the type definition is available)
3045 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003046 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003047 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003048 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003049 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003050
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003051 if (I != DeclCache.end()) {
3052 auto N = I->second;
3053 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3054 return GVE->getVariable();
3055 return dyn_cast_or_null<llvm::DINode>(N);
3056 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003057
3058 // No definition for now. Emit a forward definition that might be
3059 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003060 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003061 return getFunctionForwardDeclaration(FD);
3062 else if (const auto *VD = dyn_cast<VarDecl>(D))
3063 return getGlobalVariableForwardDeclaration(VD);
3064
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003065 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003066}
3067
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003068llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003069 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003070 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003071
David Majnemer58ed0f32016-07-17 00:39:12 +00003072 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003073 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003074 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003075
3076 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003077 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003078
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003079 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003080 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003081 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003082 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003083 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003084 }
3085 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003087 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003088 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 return SP;
3090 }
3091
Aaron Ballman86c93902014-03-06 23:45:36 +00003092 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003093 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003095 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003096 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 return SP;
3098 }
3099 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003100 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003101}
3102
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003103// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003104// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003105llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003106 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003107 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003108 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003109 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3110 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003111 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003112
David Majnemer58ed0f32016-07-17 00:39:12 +00003113 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003115
3116 const auto *FTy = FnType->getAs<FunctionType>();
3117 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3118
David Majnemer58ed0f32016-07-17 00:39:12 +00003119 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003120 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003121 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003122
3123 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003124 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003125
3126 // Replace the instancetype keyword with the actual type.
3127 if (ResultTy == CGM.getContext().getObjCInstanceType())
3128 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003129 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003130
Adrian Prantl7bec9032013-05-10 21:08:31 +00003131 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003133 QualType SelfDeclTy;
3134 if (auto *SelfDecl = OMethod->getSelfDecl())
3135 SelfDeclTy = SelfDecl->getType();
3136 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3137 if (FPT->getNumParams() > 1)
3138 SelfDeclTy = FPT->getParamType(0);
3139 if (!SelfDeclTy.isNull())
3140 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003142 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003143 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003145 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003146 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003147 // Variadic methods need a special marker at the end of the type list.
3148 if (OMethod->isVariadic())
3149 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003150
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003151 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003152 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3153 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003155
Adrian Prantl800faef2014-02-25 23:42:18 +00003156 // Handle variadic function types; they need an additional
3157 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003158 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003159 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003160 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003161 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003162 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3163 for (QualType ParamType : FPT->param_types())
3164 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003165 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003166 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003167 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3168 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003169 }
3170
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003171 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003172}
3173
Eric Christophere7b87e52014-10-26 23:40:33 +00003174void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3175 SourceLocation ScopeLoc, QualType FnType,
3176 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003177
3178 StringRef Name;
3179 StringRef LinkageName;
3180
3181 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3182
3183 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003184 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003185
Leny Kholodov80c047d2016-09-06 10:48:04 +00003186 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003187 llvm::DIFile *Unit = getOrCreateFile(Loc);
3188 llvm::DIScope *FDContext = Unit;
3189 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003190 if (!HasDecl) {
3191 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003192 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003193 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003194 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003195 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003196 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003197 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003198 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003199 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003200 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 return;
3202 }
3203 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003204 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3205 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003206 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003207 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003208 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003209 } else {
3210 // Use llvm function name.
3211 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003212 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003214 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003215 Name = Name.substr(1);
3216
Adrian Prantl42d71b92014-04-10 23:21:53 +00003217 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003218 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003219 // Artificial functions should not silently reuse CurLoc.
3220 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003221 }
3222 unsigned LineNo = getLineNumber(Loc);
3223 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003224
Eric Christopher8018e412014-03-27 18:50:35 +00003225 // FIXME: The function declaration we're constructing here is mostly reusing
3226 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3227 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3228 // all subprograms instead of the actual context since subprogram definitions
3229 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003230 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003231 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003232 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003233 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003234 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003235 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003236 // We might get here with a VarDecl in the case we're generating
3237 // code for the initialization of globals. Do not record these decls
3238 // as they will overwrite the actual VarDecl Decl in the cache.
3239 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003240 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003241
Adrian Prantlbebb8932014-03-21 21:01:58 +00003242 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003243 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003244
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003246 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003247}
3248
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003249void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3250 QualType FnType) {
3251 StringRef Name;
3252 StringRef LinkageName;
3253
3254 const Decl *D = GD.getDecl();
3255 if (!D)
3256 return;
3257
Leny Kholodov80c047d2016-09-06 10:48:04 +00003258 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003259 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003260 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003261 llvm::DINodeArray TParamsArray;
3262 if (isa<FunctionDecl>(D)) {
3263 // If there is a DISubprogram for this function available then use it.
3264 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3265 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003266 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003267 Name = getObjCMethodName(OMD);
3268 Flags |= llvm::DINode::FlagPrototyped;
3269 } else {
3270 llvm_unreachable("not a function or ObjC method");
3271 }
3272 if (!Name.empty() && Name[0] == '\01')
3273 Name = Name.substr(1);
3274
3275 if (D->isImplicit()) {
3276 Flags |= llvm::DINode::FlagArtificial;
3277 // Artificial functions without a location should not silently reuse CurLoc.
3278 if (Loc.isInvalid())
3279 CurLoc = SourceLocation();
3280 }
3281 unsigned LineNo = getLineNumber(Loc);
3282 unsigned ScopeLine = 0;
3283
Adrian Prantle76bda52016-04-15 15:55:45 +00003284 DBuilder.retainType(DBuilder.createFunction(
3285 FDContext, Name, LinkageName, Unit, LineNo,
3286 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3287 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3288 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003289}
3290
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003291void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3292 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3293 // If there is a subprogram for this function available then use it.
3294 auto FI = SPCache.find(FD->getCanonicalDecl());
3295 llvm::DISubprogram *SP = nullptr;
3296 if (FI != SPCache.end())
3297 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003298 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003299 SP = getFunctionStub(GD);
3300 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3301 LexicalBlockStack.emplace_back(SP);
3302 setInlinedAt(Builder.getCurrentDebugLocation());
3303 EmitLocation(Builder, FD->getLocation());
3304}
3305
3306void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3307 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003308 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003309 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3310}
3311
David Blaikie835afb22015-01-21 23:08:17 +00003312void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003313 // Update our current location
3314 setLocation(Loc);
3315
Eric Christophere7b87e52014-10-26 23:40:33 +00003316 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3317 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003318
Adrian Prantle83b1302014-01-07 22:05:52 +00003319 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003320 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003321 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003322}
3323
Guy Benyei11169dd2012-12-18 14:30:41 +00003324void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003325 llvm::MDNode *Back = nullptr;
3326 if (!LexicalBlockStack.empty())
3327 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003328 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003329 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003330 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003331}
3332
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003333void CGDebugInfo::AppendAddressSpaceXDeref(
3334 unsigned AddressSpace,
3335 SmallVectorImpl<int64_t> &Expr) const {
3336 Optional<unsigned> DWARFAddressSpace =
3337 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3338 if (!DWARFAddressSpace)
3339 return;
3340
3341 Expr.push_back(llvm::dwarf::DW_OP_constu);
3342 Expr.push_back(DWARFAddressSpace.getValue());
3343 Expr.push_back(llvm::dwarf::DW_OP_swap);
3344 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3345}
3346
Eric Christopher0fdcb312013-05-16 00:52:20 +00003347void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3348 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003349 // Set our current location.
3350 setLocation(Loc);
3351
Guy Benyei11169dd2012-12-18 14:30:41 +00003352 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003353 Builder.SetCurrentDebugLocation(
3354 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3355 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003356
Benjamin Kramer8c305922016-02-02 11:06:51 +00003357 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003358 return;
3359
3360 // Create a new lexical block and push it on the stack.
3361 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003362}
3363
Eric Christopher0fdcb312013-05-16 00:52:20 +00003364void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3365 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3367
3368 // Provide an entry in the line table for the end of the block.
3369 EmitLocation(Builder, Loc);
3370
Benjamin Kramer8c305922016-02-02 11:06:51 +00003371 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003372 return;
3373
Guy Benyei11169dd2012-12-18 14:30:41 +00003374 LexicalBlockStack.pop_back();
3375}
3376
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003377void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003378 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3379 unsigned RCount = FnBeginRegionCount.back();
3380 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3381
3382 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003383 while (LexicalBlockStack.size() != RCount) {
3384 // Provide an entry in the line table for the end of the block.
3385 EmitLocation(Builder, CurLoc);
3386 LexicalBlockStack.pop_back();
3387 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003389
3390 if (Fn && Fn->getSubprogram())
3391 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003392}
3393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003394llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003395 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003396
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003397 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 QualType FType;
3399 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003400 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003401
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003402 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003403 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003404
3405 FieldOffset = 0;
3406 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3407 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3408 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3409 FType = CGM.getContext().IntTy;
3410 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3411 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3412
3413 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3414 if (HasCopyAndDispose) {
3415 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003416 EltTys.push_back(
3417 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3418 EltTys.push_back(
3419 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 }
3421 bool HasByrefExtendedLayout;
3422 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003423 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3424 HasByrefExtendedLayout) &&
3425 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003427 EltTys.push_back(
3428 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003429 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003430
Guy Benyei11169dd2012-12-18 14:30:41 +00003431 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3432 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003433 CGM.getTarget().getPointerAlign(0))) {
3434 CharUnits FieldOffsetInBytes =
3435 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003436 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003437 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003438
Guy Benyei11169dd2012-12-18 14:30:41 +00003439 if (NumPaddingBytes.isPositive()) {
3440 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3441 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3442 pad, ArrayType::Normal, 0);
3443 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3444 }
3445 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003446
Guy Benyei11169dd2012-12-18 14:30:41 +00003447 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003448 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003449 FieldSize = CGM.getContext().getTypeSize(FType);
3450 FieldAlign = CGM.getContext().toBits(Align);
3451
Eric Christopherb2a008c2013-05-16 00:45:12 +00003452 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003453 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003454 FieldAlign, FieldOffset,
3455 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003456 EltTys.push_back(FieldTy);
3457 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003458
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003460
Leny Kholodov80c047d2016-09-06 10:48:04 +00003461 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003462
Guy Benyei11169dd2012-12-18 14:30:41 +00003463 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003464 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003465}
3466
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003467void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3468 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003469 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003470 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003471 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003472 if (VD->hasAttr<NoDebugAttr>())
3473 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003474
David Blaikie7fceebf2013-08-19 03:37:48 +00003475 bool Unwritten =
3476 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3477 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003478 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003479 if (!Unwritten)
3480 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003481 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003482 uint64_t XOffset = 0;
3483 if (VD->hasAttr<BlocksAttr>())
3484 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003485 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 Ty = getOrCreateType(VD->getType(), Unit);
3487
3488 // If there is no debug info for this type then do not emit debug info
3489 // for this variable.
3490 if (!Ty)
3491 return;
3492
Guy Benyei11169dd2012-12-18 14:30:41 +00003493 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003494 unsigned Line = 0;
3495 unsigned Column = 0;
3496 if (!Unwritten) {
3497 Line = getLineNumber(VD->getLocation());
3498 Column = getColumnNumber(VD->getLocation());
3499 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003500 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003501 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003502 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003503 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003504
3505 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3506
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003507 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3508 AppendAddressSpaceXDeref(AddressSpace, Expr);
3509
Alexey Bataev24f710182017-06-09 13:55:08 +00003510 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3511 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003512 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3513 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3514 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3515 Flags |= llvm::DINode::FlagObjectPointer;
3516 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003517
Adrian Prantlc3782a12017-04-18 01:22:01 +00003518 // Note: Older versions of clang used to emit byval references with an extra
3519 // DW_OP_deref, because they referenced the IR arg directly instead of
3520 // referencing an alloca. Newer versions of LLVM don't treat allocas
3521 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003522 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003523 StringRef Name = VD->getName();
3524 if (!Name.empty()) {
3525 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003526 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003528 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003529 // offset of __forwarding field
3530 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003531 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003532 Expr.push_back(offset.getQuantity());
3533 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003534 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003535 // offset of x field
3536 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003537 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003538 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003539 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003540 // If VD is an anonymous union then Storage represents value for
3541 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003542 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003543 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003544 // GDB has trouble finding local variables in anonymous unions, so we emit
3545 // artifical local variables for each of the members.
3546 //
3547 // FIXME: Remove this code as soon as GDB supports this.
3548 // The debug info verifier in LLVM operates based on the assumption that a
3549 // variable has the same size as its storage and we had to disable the check
3550 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003551 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003552 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003553 StringRef FieldName = Field->getName();
3554
3555 // Ignore unnamed fields. Do not ignore unnamed records.
3556 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3557 continue;
3558
3559 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003560 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003561 auto *D = DBuilder.createAutoVariable(
3562 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003563 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003564
3565 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003566 DBuilder.insertDeclare(
3567 Storage, D, DBuilder.createExpression(Expr),
3568 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3569 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003570 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003571 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003572 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003573
3574 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003575 auto *D = ArgNo
3576 ? DBuilder.createParameterVariable(
3577 Scope, Name, *ArgNo, Unit, Line, Ty,
3578 CGM.getLangOpts().Optimize, Flags)
3579 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3580 CGM.getLangOpts().Optimize, Flags,
3581 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003582
3583 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003584 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003585 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003586 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003587}
3588
3589void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3590 llvm::Value *Storage,
3591 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003592 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003593 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003594}
3595
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003596llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3597 llvm::DIType *Ty) {
3598 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003599 if (CachedTy)
3600 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003601 return DBuilder.createObjectPointerType(Ty);
3602}
3603
Eric Christophere7b87e52014-10-26 23:40:33 +00003604void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3605 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003606 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003607 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003608 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003609
Craig Topper8a13c412014-05-21 05:09:00 +00003610 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003611 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003612 if (VD->hasAttr<NoDebugAttr>())
3613 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003614
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003616
Guy Benyei11169dd2012-12-18 14:30:41 +00003617 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003618 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3619 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003620 if (isByRef)
3621 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003622 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 Ty = getOrCreateType(VD->getType(), Unit);
3624
3625 // Self is passed along as an implicit non-arg variable in a
3626 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003627 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3628 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3629 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003630
3631 // Get location information.
3632 unsigned Line = getLineNumber(VD->getLocation());
3633 unsigned Column = getColumnNumber(VD->getLocation());
3634
3635 const llvm::DataLayout &target = CGM.getDataLayout();
3636
3637 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003638 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003639 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3640
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003641 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003642 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003643 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003644 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003645 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003646 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003647 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003648 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003649 offset =
3650 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003651 addr.push_back(offset.getQuantity());
3652 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003653 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003654 // offset of x field
3655 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003656 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003657 }
3658
3659 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003660 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003661 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003662 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003663 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003664
Guy Benyei11169dd2012-12-18 14:30:41 +00003665 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003666 auto DL =
3667 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003668 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003669 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003670 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003671 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003672 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003673}
3674
Guy Benyei11169dd2012-12-18 14:30:41 +00003675void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3676 unsigned ArgNo,
3677 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003678 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003679 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003680}
3681
3682namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003683struct BlockLayoutChunk {
3684 uint64_t OffsetInBits;
3685 const BlockDecl::Capture *Capture;
3686};
3687bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3688 return l.OffsetInBits < r.OffsetInBits;
3689}
Guy Benyei11169dd2012-12-18 14:30:41 +00003690}
3691
3692void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl356347b2017-10-26 20:08:52 +00003693 StringRef Name,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003694 unsigned ArgNo,
Adrian Prantl356347b2017-10-26 20:08:52 +00003695 llvm::AllocaInst *Alloca,
Guy Benyei11169dd2012-12-18 14:30:41 +00003696 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003697 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003698 ASTContext &C = CGM.getContext();
3699 const BlockDecl *blockDecl = block.getBlockDecl();
3700
3701 // Collect some general information about the block's location.
3702 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003703 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003704 unsigned line = getLineNumber(loc);
3705 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003706
Guy Benyei11169dd2012-12-18 14:30:41 +00003707 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003708 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003709
3710 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003711 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003712
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003713 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003714 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003715 blockLayout->getElementOffsetInBits(0),
3716 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003717 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003718 blockLayout->getElementOffsetInBits(1),
3719 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003720 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003721 blockLayout->getElementOffsetInBits(2),
3722 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003723 auto *FnTy = block.getBlockExpr()->getFunctionType();
3724 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003725 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003726 blockLayout->getElementOffsetInBits(3),
3727 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003728 fields.push_back(createFieldType(
3729 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3730 ? C.getBlockDescriptorExtendedType()
3731 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003732 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003733
3734 // We want to sort the captures by offset, not because DWARF
3735 // requires this, but because we're paranoid about debuggers.
3736 SmallVector<BlockLayoutChunk, 8> chunks;
3737
3738 // 'this' capture.
3739 if (blockDecl->capturesCXXThis()) {
3740 BlockLayoutChunk chunk;
3741 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003742 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003743 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003744 chunks.push_back(chunk);
3745 }
3746
3747 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003748 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003749 const VarDecl *variable = capture.getVariable();
3750 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3751
3752 // Ignore constant captures.
3753 if (captureInfo.isConstant())
3754 continue;
3755
3756 BlockLayoutChunk chunk;
3757 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003758 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003759 chunk.Capture = &capture;
3760 chunks.push_back(chunk);
3761 }
3762
3763 // Sort by offset.
3764 llvm::array_pod_sort(chunks.begin(), chunks.end());
3765
David Majnemer58ed0f32016-07-17 00:39:12 +00003766 for (const BlockLayoutChunk &Chunk : chunks) {
3767 uint64_t offsetInBits = Chunk.OffsetInBits;
3768 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003769
3770 // If we have a null capture, this must be the C++ 'this' capture.
3771 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003772 QualType type;
3773 if (auto *Method =
3774 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3775 type = Method->getThisType(C);
3776 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3777 type = QualType(RDecl->getTypeForDecl(), 0);
3778 else
3779 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003780
David Majnemerb4b671e2016-06-30 03:01:59 +00003781 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003782 offsetInBits, tunit, tunit));
3783 continue;
3784 }
3785
3786 const VarDecl *variable = capture->getVariable();
3787 StringRef name = variable->getName();
3788
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003789 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003790 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003791 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003792 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003793
3794 // FIXME: this creates a second copy of this type!
3795 uint64_t xoffset;
3796 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003797 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003798 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3799 PtrInfo.Width, Align, offsetInBits,
3800 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003801 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003802 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003803 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003804 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003805 }
3806 fields.push_back(fieldType);
3807 }
3808
3809 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003810 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3811 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003812
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003813 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003814
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003815 llvm::DIType *type =
3816 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003817 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003818 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003819 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3820
3821 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003822 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003823 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003824
3825 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003826 auto *debugVar = DBuilder.createParameterVariable(
Adrian Prantl356347b2017-10-26 20:08:52 +00003827 scope, Name, ArgNo, tunit, line, type,
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003828 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003829
Adrian Prantl616bef42013-03-14 21:52:59 +00003830 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl356347b2017-10-26 20:08:52 +00003831 DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003832 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003833 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003834}
3835
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003836llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003837CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3838 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003839 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003840
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003841 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003842 if (MI != StaticDataMemberCache.end()) {
3843 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003844 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003845 }
David Blaikiece763042013-08-20 21:49:21 +00003846
3847 // If the member wasn't found in the cache, lazily construct and add it to the
3848 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003849 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003850 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003851 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003852}
3853
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003854llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003855 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3856 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003857 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003858
3859 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003860 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003861 StringRef FieldName = Field->getName();
3862
3863 // Ignore unnamed fields, but recurse into anonymous records.
3864 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003865 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003866 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003867 Var, DContext);
3868 continue;
3869 }
3870 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003871 GVE = DBuilder.createGlobalVariableExpression(
3872 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3873 Var->hasLocalLinkage());
3874 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003875 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003876 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003877}
3878
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003879void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003880 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003881 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003882 if (D->hasAttr<NoDebugAttr>())
3883 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003884
3885 // If we already created a DIGlobalVariable for this declaration, just attach
3886 // it to the llvm::GlobalVariable.
3887 auto Cached = DeclCache.find(D->getCanonicalDecl());
3888 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003889 return Var->addDebugInfo(
3890 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003891
Guy Benyei11169dd2012-12-18 14:30:41 +00003892 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003893 llvm::DIFile *Unit = nullptr;
3894 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003895 unsigned LineNo;
3896 StringRef DeclName, LinkageName;
3897 QualType T;
3898 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003899
3900 // Attempt to store one global variable for the declaration - even if we
3901 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003902 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003903
3904 // If this is an anonymous union then we'll want to emit a global
3905 // variable for each member of the anonymous union so that it's possible
3906 // to find the name of any field in the union.
3907 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003908 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003909 assert(RD->isAnonymousStructOrUnion() &&
3910 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003911 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003912 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003913 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003914
3915 SmallVector<int64_t, 4> Expr;
3916 unsigned AddressSpace =
3917 CGM.getContext().getTargetAddressSpace(D->getType());
3918 AppendAddressSpaceXDeref(AddressSpace, Expr);
3919
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003920 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003921 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003922 Var->hasLocalLinkage(),
3923 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00003924 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003925 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003926 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003927 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003928}
3929
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003930void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003931 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003932 if (VD->hasAttr<NoDebugAttr>())
3933 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003934 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003935 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003936 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003937 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003938 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003939 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3940 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003941 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3942 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3943 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003944 // Do not use global variables for enums.
3945 //
3946 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003947 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003948 return;
David Blaikiea15565562014-04-04 20:56:17 +00003949 // Do not emit separate definitions for function local const/statics.
3950 if (isa<FunctionDecl>(VD->getDeclContext()))
3951 return;
David Blaikiebb113912014-04-05 07:23:17 +00003952 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003953 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003954 if (VarD->isStaticDataMember()) {
3955 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003956 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003957 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003958 //
3959 // FIXME: This is probably unnecessary, since Ty should reference RD
3960 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003961 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003962 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003963 return;
3964 }
3965
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003966 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003967
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003968 auto &GV = DeclCache[VD];
3969 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003970 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003971 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003972 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3973 // FIXME: Add a representation for integer constants wider than 64 bits.
3974 if (Init.isInt())
3975 InitExpr =
3976 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3977 else if (Init.isFloat())
3978 InitExpr = DBuilder.createConstantValueExpression(
3979 Init.getFloat().bitcastToAPInt().getZExtValue());
3980 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003981 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003982 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003983 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3984 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003985}
3986
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003987llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003988 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003989 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003990 llvm::DIScope *Mod = getParentModuleOrNull(D);
3991 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003992}
3993
David Blaikie9f88fe82013-04-22 06:13:21 +00003994void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003995 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003996 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003997 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003998 if (!NSDecl->isAnonymousNamespace() ||
3999 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004000 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004001 DBuilder.createImportedModule(
4002 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004003 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004004 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004005}
4006
David Blaikiebd483762013-05-20 04:58:53 +00004007void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004008 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004009 return;
4010 assert(UD.shadow_size() &&
4011 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4012 // Emitting one decl is sufficient - debuggers can detect that this is an
4013 // overloaded name & provide lookup for all the overloads.
4014 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004015
4016 // FIXME: Skip functions with undeduced auto return type for now since we
4017 // don't currently have the plumbing for separate declarations & definitions
4018 // of free functions and mismatched types (auto in the declaration, concrete
4019 // return type in the definition)
4020 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4021 if (const auto *AT =
4022 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4023 if (AT->getDeducedType().isNull())
4024 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004025 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004026 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4027 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004028 DBuilder.createImportedDeclaration(
4029 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004030 getOrCreateFile(Loc), getLineNumber(Loc));
4031 }
David Blaikiebd483762013-05-20 04:58:53 +00004032}
4033
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004034void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004035 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4036 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004037 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004038 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004039 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004040 DBuilder.createImportedDeclaration(
4041 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004042 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4043 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004044 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004045}
4046
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004047llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004048CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004049 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004050 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004051 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004052 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004053 return cast<llvm::DIImportedEntity>(VH);
4054 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004055 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004056 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004057 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4058 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004059 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004060 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004061 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4062 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004063 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004064 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004065 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004066 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004067 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004068 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004069 return R;
4070}
4071
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004072llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004073CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4074 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4075 // if necessary, and this way multiple declarations of the same namespace in
4076 // different parent modules stay distinct.
4077 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004078 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004079 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004080
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004081 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004082 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004083 llvm::DINamespace *NS =
4084 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4085 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004086 return NS;
4087}
4088
Adrian Prantla5206ce2015-09-22 23:26:43 +00004089void CGDebugInfo::setDwoId(uint64_t Signature) {
4090 assert(TheCU && "no main compile unit");
4091 TheCU->setDWOId(Signature);
4092}
4093
4094
Guy Benyei11169dd2012-12-18 14:30:41 +00004095void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004096 // Creating types might create further types - invalidating the current
4097 // element and the size(), so don't cache/reference them.
4098 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4099 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004100 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004101 ? CreateTypeDefinition(E.Type, E.Unit)
4102 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004103 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004104 }
4105
David Blaikief427b002014-05-06 03:42:01 +00004106 for (auto p : ReplaceMap) {
4107 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004108 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004109 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004110
David Blaikief427b002014-05-06 03:42:01 +00004111 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004112 assert(it != TypeCache.end());
4113 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004114
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004115 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4116 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004117 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004118
Frederic Rissd253ed62014-11-18 03:40:51 +00004119 for (const auto &p : FwdDeclReplaceMap) {
4120 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004121 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004122 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004123
4124 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004125 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004126 // with ourselves, that will destroy the temporary MDNode and
4127 // replace it with a standard one, avoiding leaking memory.
4128 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004129 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004130 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004131 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004132
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004133 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4134 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004135 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004136 }
4137
Adrian Prantl73409ce2013-03-11 18:33:46 +00004138 // We keep our own list of retained types, because we need to look
4139 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004140 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004141 if (auto MD = TypeCache[RT])
4142 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004143
Guy Benyei11169dd2012-12-18 14:30:41 +00004144 DBuilder.finalize();
4145}
David Blaikie66088d52014-09-24 17:01:27 +00004146
4147void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004148 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004149 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004150
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004151 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004152 // Don't ignore in case of explicit cast where it is referenced indirectly.
4153 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004154}
Amara Emerson652795d2016-11-10 14:44:30 +00004155
4156llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4157 if (LexicalBlockStack.empty())
4158 return llvm::DebugLoc();
4159
4160 llvm::MDNode *Scope = LexicalBlockStack.back();
4161 return llvm::DebugLoc::get(
4162 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4163}