blob: ac22dde9f96ec04cc1c600f3fceef3ddd5bb6716 [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)) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000292 OS << OCD->getClassInterface()->getName() << '(' << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000293 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000294 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000295 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000296 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000297 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000298 ClassTy.print(OS, PrintingPolicy(LangOptions()));
299 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000300 }
301 OS << ' ' << OMD->getSelector().getAsString() << ']';
302
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000303 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000304}
305
Guy Benyei11169dd2012-12-18 14:30:41 +0000306StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000307 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000308}
309
Eric Christophere7b87e52014-10-26 23:40:33 +0000310StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000311 if (isa<ClassTemplateSpecializationDecl>(RD)) {
312 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000313 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000314 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000315 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000316
317 // Copy this name on the side and use its reference.
318 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000319 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000320
David Majnemerbc5976a2016-07-01 23:12:54 +0000321 // quick optimization to avoid having to intern strings that are already
322 // stored reliably elsewhere
323 if (const IdentifierInfo *II = RD->getIdentifier())
324 return II->getName();
325
326 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
327 // used to reconstruct the fully qualified type names.
328 if (CGM.getCodeGenOpts().EmitCodeView) {
329 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
330 assert(RD->getDeclContext() == D->getDeclContext() &&
331 "Typedef should not be in another decl context!");
332 assert(D->getDeclName().getAsIdentifierInfo() &&
333 "Typedef was not named!");
334 return D->getDeclName().getAsIdentifierInfo()->getName();
335 }
336
337 if (CGM.getLangOpts().CPlusPlus) {
338 StringRef Name;
339
340 ASTContext &Context = CGM.getContext();
341 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
342 // Anonymous types without a name for linkage purposes have their
343 // declarator mangled in if they have one.
344 Name = DD->getName();
345 else if (const TypedefNameDecl *TND =
346 Context.getTypedefNameForUnnamedTagDecl(RD))
347 // Anonymous types without a name for linkage purposes have their
348 // associate typedef mangled in if they have one.
349 Name = TND->getName();
350
351 if (!Name.empty()) {
352 SmallString<256> UnnamedType("<unnamed-type-");
353 UnnamedType += Name;
354 UnnamedType += '>';
355 return internString(UnnamedType);
356 }
357 }
358 }
359
360 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000361}
362
Scott Linder94834f12018-02-12 19:47:05 +0000363Optional<llvm::DIFile::ChecksumKind>
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000364CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
365 Checksum.clear();
366
Paul Robinson76178632018-05-25 22:35:59 +0000367 if (!CGM.getCodeGenOpts().EmitCodeView &&
368 CGM.getCodeGenOpts().DwarfVersion < 5)
Scott Linder94834f12018-02-12 19:47:05 +0000369 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000370
371 SourceManager &SM = CGM.getContext().getSourceManager();
372 bool Invalid;
Paul Robinson76178632018-05-25 22:35:59 +0000373 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
374 if (Invalid)
Scott Linder94834f12018-02-12 19:47:05 +0000375 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000376
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
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000387Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
388 FileID FID) {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000389 if (!CGM.getCodeGenOpts().EmbedSource)
390 return None;
391
392 bool SourceInvalid = false;
393 StringRef Source = SM.getBufferData(FID, &SourceInvalid);
394
395 if (SourceInvalid)
396 return None;
397
398 return Source;
399}
400
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000401llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000402 if (!Loc.isValid())
403 // If Location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000404 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000405
406 SourceManager &SM = CGM.getContext().getSourceManager();
407 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
408
409 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
410 // If the location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000411 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000412
413 // Cache the results.
414 const char *fname = PLoc.getFilename();
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000415 auto It = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000416
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000417 if (It != DIFileCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000418 // Verify that the information still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000419 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000420 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 }
422
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000423 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000424 Optional<llvm::DIFile::ChecksumKind> CSKind =
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000425 computeChecksum(SM.getFileID(Loc), Checksum);
Scott Linder94834f12018-02-12 19:47:05 +0000426 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
427 if (CSKind)
428 CSInfo.emplace(*CSKind, Checksum);
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000429
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000430 llvm::DIFile *F = DBuilder.createFile(
431 remapDIPath(PLoc.getFilename()), remapDIPath(getCurrentDirname()), CSInfo,
432 getSource(SM, SM.getFileID(Loc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000433
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000434 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return F;
436}
437
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000438llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000439 return DBuilder.createFile(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000440 remapDIPath(TheCU->getFilename()), remapDIPath(TheCU->getDirectory()),
Scott Lindera2fbcef2018-02-26 17:32:31 +0000441 TheCU->getFile()->getChecksum(),
442 CGM.getCodeGenOpts().EmbedSource ? TheCU->getSource() : None);
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000443}
444
445std::string CGDebugInfo::remapDIPath(StringRef Path) const {
446 for (const auto &Entry : DebugPrefixMap)
447 if (Path.startswith(Entry.first))
448 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
449 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000450}
451
Guy Benyei11169dd2012-12-18 14:30:41 +0000452unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
453 if (Loc.isInvalid() && CurLoc.isInvalid())
454 return 0;
455 SourceManager &SM = CGM.getContext().getSourceManager();
456 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000457 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000458}
459
Adrian Prantlc7822422013-03-12 20:43:25 +0000460unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000461 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000462 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 return 0;
464
465 // If the location is invalid then use the current column.
466 if (Loc.isInvalid() && CurLoc.isInvalid())
467 return 0;
468 SourceManager &SM = CGM.getContext().getSourceManager();
469 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000470 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471}
472
473StringRef CGDebugInfo::getCurrentDirname() {
474 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
475 return CGM.getCodeGenOpts().DebugCompilationDir;
476
477 if (!CWDName.empty())
478 return CWDName;
479 SmallString<256> CWD;
480 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000481 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000482}
483
Guy Benyei11169dd2012-12-18 14:30:41 +0000484void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000485 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000486 Optional<llvm::DIFile::ChecksumKind> CSKind;
487 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000488
David Blaikieaabde052014-05-14 00:29:00 +0000489 // Should we be asking the SourceManager for the main file name, instead of
490 // accepting it as an argument? This just causes the main file name to
491 // mismatch with source locations and create extra lexical scopes or
492 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
493 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
494 // because that's what the SourceManager says)
495
Guy Benyei11169dd2012-12-18 14:30:41 +0000496 // Get absolute path name.
497 SourceManager &SM = CGM.getContext().getSourceManager();
498 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
499 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000500 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000501
502 // The main file name provided via the "-main-file-name" option contains just
503 // the file name itself with no path information. This file name may have had
504 // a relative path, so we look into the actual file entry for the main
505 // file to determine the real absolute path for the file.
506 std::string MainFileDir;
507 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000508 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000509 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000510 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
511 llvm::sys::path::append(MainFileDirSS, MainFileName);
512 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000513 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000514 // If the main file name provided is identical to the input file name, and
515 // if the input file is a preprocessed source, use the module name for
516 // debug info. The module name comes from the name specified in the first
517 // linemarker if the input is a preprocessed source.
518 if (MainFile->getName() == MainFileName &&
519 FrontendOptions::getInputKindForExtension(
520 MainFile->getName().rsplit('.').second)
521 .isPreprocessed())
522 MainFileName = CGM.getModule().getName().str();
523
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000524 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 }
526
Ed Masteda706022014-05-07 12:49:30 +0000527 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 const LangOptions &LO = CGM.getLangOpts();
529 if (LO.CPlusPlus) {
530 if (LO.ObjC1)
531 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
532 else
533 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
534 } else if (LO.ObjC1) {
535 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000536 } else if (LO.RenderScript) {
537 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000538 } else if (LO.C99) {
539 LangTag = llvm::dwarf::DW_LANG_C99;
540 } else {
541 LangTag = llvm::dwarf::DW_LANG_C89;
542 }
543
544 std::string Producer = getClangFullVersion();
545
546 // Figure out which version of the ObjC runtime we have.
547 unsigned RuntimeVers = 0;
548 if (LO.ObjC1)
549 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
550
Adrian Prantl826824e2016-04-08 22:43:06 +0000551 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
552 switch (DebugKind) {
553 case codegenoptions::NoDebugInfo:
554 case codegenoptions::LocTrackingOnly:
555 EmissionKind = llvm::DICompileUnit::NoDebug;
556 break;
557 case codegenoptions::DebugLineTablesOnly:
558 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
559 break;
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000560 case codegenoptions::DebugDirectivesOnly:
561 EmissionKind = llvm::DICompileUnit::DebugDirectivesOnly;
562 break;
Adrian Prantl826824e2016-04-08 22:43:06 +0000563 case codegenoptions::LimitedDebugInfo:
564 case codegenoptions::FullDebugInfo:
565 EmissionKind = llvm::DICompileUnit::FullDebug;
566 break;
567 }
568
Scott Linder94834f12018-02-12 19:47:05 +0000569 if (CSKind)
570 CSInfo.emplace(*CSKind, Checksum);
571
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000574 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000575 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000576 LangTag,
577 DBuilder.createFile(remapDIPath(MainFileName),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000578 remapDIPath(getCurrentDirname()), CSInfo,
Scott Lindera2fbcef2018-02-26 17:32:31 +0000579 getSource(SM, SM.getMainFileID())),
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +0000580 CGOpts.EmitVersionIdentMetadata ? Producer : "",
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000581 LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
Adrian Prantlb4423022017-08-04 23:08:57 +0000582 CGOpts.DwarfDebugFlags, RuntimeVers,
583 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000584 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
David Blaikie9982bb82018-08-16 23:56:32 +0000585 CGM.getTarget().getTriple().isNVPTX()
586 ? llvm::DICompileUnit::DebugNameTableKind::None
David Blaikie65864522018-08-20 20:14:08 +0000587 : static_cast<llvm::DICompileUnit::DebugNameTableKind>(
588 CGOpts.DebugNameTable));
Guy Benyei11169dd2012-12-18 14:30:41 +0000589}
590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000591llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000592 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 StringRef BTName;
594 switch (BT->getKind()) {
595#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000596#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000597#include "clang/AST/BuiltinTypes.def"
598 case BuiltinType::Dependent:
599 llvm_unreachable("Unexpected builtin type");
600 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000601 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000602 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000603 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000604 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000605 if (!ClassTy)
606 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
607 "objc_class", TheCU,
608 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 return ClassTy;
610 case BuiltinType::ObjCId: {
611 // typedef struct objc_class *Class;
612 // typedef struct objc_object {
613 // Class isa;
614 // } *id;
615
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000616 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 return ObjTy;
618
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000619 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
621 "objc_class", TheCU,
622 getOrCreateMainFile(), 0);
623
624 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000625
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000626 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000627
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000628 ObjTy = DBuilder.createStructType(
629 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
630 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000631
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000632 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000633 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
634 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
635 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 return ObjTy;
637 }
638 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000639 if (!SelTy)
640 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
641 "objc_selector", TheCU,
642 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 return SelTy;
644 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000645
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000646#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
647 case BuiltinType::Id: \
648 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
Alexey Bader954ba212016-04-08 13:40:33 +0000649 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000650#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000651 case BuiltinType::OCLSampler:
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000652 return getOrCreateStructPtrType("opencl_sampler_t", OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000653 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000654 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000655 case BuiltinType::OCLClkEvent:
656 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
657 case BuiltinType::OCLQueue:
658 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000659 case BuiltinType::OCLReserveID:
660 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000661
Guy Benyei11169dd2012-12-18 14:30:41 +0000662 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000663 case BuiltinType::Char_U:
664 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
665 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000666 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000667 case BuiltinType::SChar:
668 Encoding = llvm::dwarf::DW_ATE_signed_char;
669 break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000670 case BuiltinType::Char8:
Guy Benyei11169dd2012-12-18 14:30:41 +0000671 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000672 case BuiltinType::Char32:
673 Encoding = llvm::dwarf::DW_ATE_UTF;
674 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 case BuiltinType::UShort:
676 case BuiltinType::UInt:
677 case BuiltinType::UInt128:
678 case BuiltinType::ULong:
679 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000680 case BuiltinType::ULongLong:
681 Encoding = llvm::dwarf::DW_ATE_unsigned;
682 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 case BuiltinType::Short:
684 case BuiltinType::Int:
685 case BuiltinType::Int128:
686 case BuiltinType::Long:
687 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000688 case BuiltinType::LongLong:
689 Encoding = llvm::dwarf::DW_ATE_signed;
690 break;
691 case BuiltinType::Bool:
692 Encoding = llvm::dwarf::DW_ATE_boolean;
693 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000694 case BuiltinType::Half:
695 case BuiltinType::Float:
696 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000697 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000698 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000699 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000700 // FIXME: For targets where long double and __float128 have the same size,
701 // they are currently indistinguishable in the debugger without some
702 // special treatment. However, there is currently no consensus on encoding
703 // and this should be updated once a DWARF encoding exists for distinct
704 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000705 Encoding = llvm::dwarf::DW_ATE_float;
706 break;
Leonard Chanf921d852018-06-04 16:07:52 +0000707 case BuiltinType::ShortAccum:
708 case BuiltinType::Accum:
709 case BuiltinType::LongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000710 case BuiltinType::ShortFract:
711 case BuiltinType::Fract:
712 case BuiltinType::LongFract:
713 case BuiltinType::SatShortFract:
714 case BuiltinType::SatFract:
715 case BuiltinType::SatLongFract:
716 case BuiltinType::SatShortAccum:
717 case BuiltinType::SatAccum:
718 case BuiltinType::SatLongAccum:
Leonard Chanf921d852018-06-04 16:07:52 +0000719 Encoding = llvm::dwarf::DW_ATE_signed_fixed;
720 break;
721 case BuiltinType::UShortAccum:
722 case BuiltinType::UAccum:
723 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000724 case BuiltinType::UShortFract:
725 case BuiltinType::UFract:
726 case BuiltinType::ULongFract:
727 case BuiltinType::SatUShortAccum:
728 case BuiltinType::SatUAccum:
729 case BuiltinType::SatULongAccum:
730 case BuiltinType::SatUShortFract:
731 case BuiltinType::SatUFract:
732 case BuiltinType::SatULongFract:
Leonard Chanf921d852018-06-04 16:07:52 +0000733 Encoding = llvm::dwarf::DW_ATE_unsigned_fixed;
734 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000735 }
736
737 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000738 case BuiltinType::Long:
739 BTName = "long int";
740 break;
741 case BuiltinType::LongLong:
742 BTName = "long long int";
743 break;
744 case BuiltinType::ULong:
745 BTName = "long unsigned int";
746 break;
747 case BuiltinType::ULongLong:
748 BTName = "long long unsigned int";
749 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000750 default:
751 BTName = BT->getName(CGM.getLangOpts());
752 break;
753 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000754 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000755 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000756 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000757}
758
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000759llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000760 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000761 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000762 if (Ty->isComplexIntegerType())
763 Encoding = llvm::dwarf::DW_ATE_lo_user;
764
765 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000766 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000767}
768
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000769llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
770 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000771 QualifierCollector Qc;
772 const Type *T = Qc.strip(Ty);
773
774 // Ignore these qualifiers for now.
775 Qc.removeObjCGCAttr();
776 Qc.removeAddressSpace();
777 Qc.removeObjCLifetime();
778
779 // We will create one Derived type for one qualifier and recurse to handle any
780 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000781 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000782 if (Qc.hasConst()) {
783 Tag = llvm::dwarf::DW_TAG_const_type;
784 Qc.removeConst();
785 } else if (Qc.hasVolatile()) {
786 Tag = llvm::dwarf::DW_TAG_volatile_type;
787 Qc.removeVolatile();
788 } else if (Qc.hasRestrict()) {
789 Tag = llvm::dwarf::DW_TAG_restrict_type;
790 Qc.removeRestrict();
791 } else {
792 assert(Qc.empty() && "Unknown type qualifier for debug info");
793 return getOrCreateType(QualType(T, 0), Unit);
794 }
795
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000796 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000797
798 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
799 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000800 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000801}
802
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
804 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000805
806 // The frontend treats 'id' as a typedef to an ObjCObjectType,
807 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
808 // debug info, we want to emit 'id' in both cases.
809 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000810 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000811
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000812 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
813 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000814}
815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
817 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000818 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 Ty->getPointeeType(), Unit);
820}
821
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000822/// \return whether a C++ mangling exists for the type defined by TD.
823static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
824 switch (TheCU->getSourceLanguage()) {
825 case llvm::dwarf::DW_LANG_C_plus_plus:
826 return true;
827 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
828 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
829 default:
830 return false;
831 }
832}
833
Reid Kleckner59320fc2018-08-17 20:59:52 +0000834// Determines if the debug info for this tag declaration needs a type
835// identifier. The purpose of the unique identifier is to deduplicate type
836// information for identical types across TUs. Because of the C++ one definition
837// rule (ODR), it is valid to assume that the type is defined the same way in
838// every TU and its debug info is equivalent.
839//
840// C does not have the ODR, and it is common for codebases to contain multiple
841// different definitions of a struct with the same name in different TUs.
842// Therefore, if the type doesn't have a C++ mangling, don't give it an
843// identifer. Type information in C is smaller and simpler than C++ type
844// information, so the increase in debug info size is negligible.
845//
846// If the type is not externally visible, it should be unique to the current TU,
847// and should not need an identifier to participate in type deduplication.
848// However, when emitting CodeView, the format internally uses these
849// unique type name identifers for references between debug info. For example,
850// the method of a class in an anonymous namespace uses the identifer to refer
851// to its parent class. The Microsoft C++ ABI attempts to provide unique names
852// for such types, so when emitting CodeView, always use identifiers for C++
853// types. This may create problems when attempting to emit CodeView when the MS
854// C++ ABI is not in use.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000855static bool needsTypeIdentifier(const TagDecl *TD, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000856 llvm::DICompileUnit *TheCU) {
857 // We only add a type identifier for types with C++ name mangling.
858 if (!hasCXXMangling(TD, TheCU))
859 return false;
860
Brock Wyma8557ec52018-05-22 12:41:19 +0000861 // Externally visible types with C++ mangling need a type identifier.
862 if (TD->isExternallyVisible())
863 return true;
864
Reid Kleckner59320fc2018-08-17 20:59:52 +0000865 // CodeView types with C++ mangling need a type identifier.
866 if (CGM.getCodeGenOpts().EmitCodeView)
867 return true;
868
Brock Wyma8557ec52018-05-22 12:41:19 +0000869 return false;
870}
871
Reid Kleckner59320fc2018-08-17 20:59:52 +0000872// Returns a unique type identifier string if one exists, or an empty string.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000873static SmallString<256> getTypeIdentifier(const TagType *Ty, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000874 llvm::DICompileUnit *TheCU) {
875 SmallString<256> Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000876 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000877
Brock Wyma8557ec52018-05-22 12:41:19 +0000878 if (!needsTypeIdentifier(TD, CGM, TheCU))
879 return Identifier;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000880
Manman Rene0064d82013-08-29 23:19:58 +0000881 // TODO: This is using the RTTI name. Is there a better way to get
882 // a unique string for a type?
Brock Wyma8557ec52018-05-22 12:41:19 +0000883 llvm::raw_svector_ostream Out(Identifier);
Manman Rene0064d82013-08-29 23:19:58 +0000884 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Brock Wyma8557ec52018-05-22 12:41:19 +0000885 return Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000886}
887
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000888/// \return the appropriate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000889static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000890 llvm::dwarf::Tag Tag;
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000891 if (RD->isStruct() || RD->isInterface())
892 Tag = llvm::dwarf::DW_TAG_structure_type;
893 else if (RD->isUnion())
894 Tag = llvm::dwarf::DW_TAG_union_type;
895 else {
896 // FIXME: This could be a struct type giving a default visibility different
897 // than C++ class type, but needs llvm metadata changes first.
898 assert(RD->isClass());
899 Tag = llvm::dwarf::DW_TAG_class_type;
900 }
901 return Tag;
902}
903
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000904llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000905CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000906 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000907 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
909 return cast<llvm::DICompositeType>(T);
910 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000911 unsigned Line = getLineNumber(RD->getLocation());
912 StringRef RDName = getClassName(RD);
913
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000914 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000915 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000916
Guy Benyei11169dd2012-12-18 14:30:41 +0000917 // Create the type.
Brock Wyma8557ec52018-05-22 12:41:19 +0000918 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000919 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000920 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +0000921 llvm::DINode::FlagFwdDecl, Identifier);
Paul Robinson1787f812017-09-28 18:37:02 +0000922 if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
923 if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
924 DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
925 CollectCXXTemplateParams(TSpecial, DefUnit));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000926 ReplaceMap.emplace_back(
927 std::piecewise_construct, std::make_tuple(Ty),
928 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000929 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000930}
931
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000932llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000933 const Type *Ty,
934 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000935 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000936 // Bit size, align and offset of the type.
937 // Size is always the size of a pointer. We can't use getTypeSize here
938 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000939 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
940 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000941 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000942 Optional<unsigned> DWARFAddressSpace =
943 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000944
Keno Fischer87842f32015-11-16 09:04:13 +0000945 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
946 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
947 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000948 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000949 else
950 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000951 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000952}
953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
955 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000956 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000957 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000958 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
959 TheCU, getOrCreateMainFile(), 0);
960 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
961 Cache = DBuilder.createPointerType(Cache, Size);
962 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000963}
964
Scott Linder58df0e42018-08-08 15:56:12 +0000965uint64_t CGDebugInfo::collectDefaultElementTypesForBlockPointer(
966 const BlockPointerType *Ty, llvm::DIFile *Unit, llvm::DIDerivedType *DescTy,
967 unsigned LineNo, SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000968 QualType FType;
Guy Benyei11169dd2012-12-18 14:30:41 +0000969
Scott Linder58df0e42018-08-08 15:56:12 +0000970 // Advanced by calls to CreateMemberType in increments of FType, then
971 // returned as the overall size of the default elements.
972 uint64_t FieldOffset = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000973
Scott Linder58df0e42018-08-08 15:56:12 +0000974 // Blocks in OpenCL have unique constraints which make the standard fields
975 // redundant while requiring size and align fields for enqueue_kernel. See
976 // initializeForBlockHeader in CGBlocks.cpp
Scott Linder2b5cf042018-07-30 20:31:11 +0000977 if (CGM.getLangOpts().OpenCL) {
978 FType = CGM.getContext().IntTy;
979 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
980 EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset));
981 } else {
982 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
983 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
984 FType = CGM.getContext().IntTy;
985 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
986 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
987 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
988 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
989 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Scott Linder58df0e42018-08-08 15:56:12 +0000990 uint64_t FieldSize = CGM.getContext().getTypeSize(Ty);
991 uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty);
Scott Linder2b5cf042018-07-30 20:31:11 +0000992 EltTys.push_back(DBuilder.createMemberType(
Scott Linder58df0e42018-08-08 15:56:12 +0000993 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign,
994 FieldOffset, llvm::DINode::FlagZero, DescTy));
Scott Lindera7c45682018-07-30 22:52:07 +0000995 FieldOffset += FieldSize;
Scott Linder2b5cf042018-07-30 20:31:11 +0000996 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000997
Scott Linder58df0e42018-08-08 15:56:12 +0000998 return FieldOffset;
999}
1000
1001llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
1002 llvm::DIFile *Unit) {
1003 SmallVector<llvm::Metadata *, 8> EltTys;
1004 QualType FType;
1005 uint64_t FieldOffset;
1006 llvm::DINodeArray Elements;
1007
1008 FieldOffset = 0;
1009 FType = CGM.getContext().UnsignedLongTy;
1010 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
1011 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
1012
1013 Elements = DBuilder.getOrCreateArray(EltTys);
1014 EltTys.clear();
1015
1016 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
1017
1018 auto *EltTy =
1019 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0,
1020 FieldOffset, 0, Flags, nullptr, Elements);
1021
1022 // Bit size, align and offset of the type.
1023 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1024
1025 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
1026
1027 FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy,
1028 0, EltTys);
1029
Guy Benyei11169dd2012-12-18 14:30:41 +00001030 Elements = DBuilder.getOrCreateArray(EltTys);
1031
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001032 // The __block_literal_generic structs are marked with a special
1033 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
1034 // the debugger needs to know about. To allow type uniquing, emit
1035 // them without a name or a location.
Scott Linder58df0e42018-08-08 15:56:12 +00001036 EltTy = DBuilder.createStructType(Unit, "", nullptr, 0, FieldOffset, 0,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001037 Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001038
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001039 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001040}
1041
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001042llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1043 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +00001044 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001045 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +00001046
1047 SmallString<128> NS;
1048 llvm::raw_svector_ostream OS(NS);
Serge Pavlov03e672c2017-11-28 16:14:14 +00001049 Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false);
1050 printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +00001051
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001052 auto *AliasDecl =
1053 cast<TypeAliasTemplateDecl>(Ty->getTemplateName().getAsTemplateDecl())
1054 ->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +00001055
1056 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +00001057 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
1058 getLineNumber(Loc),
1059 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +00001060}
1061
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001062llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1063 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001064 // We don't set size information, but do specify where the typedef was
1065 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +00001066 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001067
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001068 // Typedefs are derived from some other type.
1069 return DBuilder.createTypedef(
1070 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1071 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +00001072 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001073}
1074
Reid Klecknerf00f8032016-06-08 20:41:54 +00001075static unsigned getDwarfCC(CallingConv CC) {
1076 switch (CC) {
1077 case CC_C:
1078 // Avoid emitting DW_AT_calling_convention if the C convention was used.
1079 return 0;
1080
1081 case CC_X86StdCall:
1082 return llvm::dwarf::DW_CC_BORLAND_stdcall;
1083 case CC_X86FastCall:
1084 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
1085 case CC_X86ThisCall:
1086 return llvm::dwarf::DW_CC_BORLAND_thiscall;
1087 case CC_X86VectorCall:
1088 return llvm::dwarf::DW_CC_LLVM_vectorcall;
1089 case CC_X86Pascal:
1090 return llvm::dwarf::DW_CC_BORLAND_pascal;
Martin Storsjo022e7822017-07-17 20:49:45 +00001091 case CC_Win64:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001092 return llvm::dwarf::DW_CC_LLVM_Win64;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001093 case CC_X86_64SysV:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001094 return llvm::dwarf::DW_CC_LLVM_X86_64SysV;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001095 case CC_AAPCS:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001096 return llvm::dwarf::DW_CC_LLVM_AAPCS;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001097 case CC_AAPCS_VFP:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001098 return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001099 case CC_IntelOclBicc:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001100 return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001101 case CC_SpirFunction:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001102 return llvm::dwarf::DW_CC_LLVM_SpirFunction;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00001103 case CC_OpenCLKernel:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001104 return llvm::dwarf::DW_CC_LLVM_OpenCLKernel;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001105 case CC_Swift:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001106 return llvm::dwarf::DW_CC_LLVM_Swift;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001107 case CC_PreserveMost:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001108 return llvm::dwarf::DW_CC_LLVM_PreserveMost;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001109 case CC_PreserveAll:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001110 return llvm::dwarf::DW_CC_LLVM_PreserveAll;
Erich Keane757d3172016-11-02 18:29:35 +00001111 case CC_X86RegCall:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001112 return llvm::dwarf::DW_CC_LLVM_X86RegCall;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001113 }
1114 return 0;
1115}
1116
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001117llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1118 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001119 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001120
1121 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001122 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001123
1124 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001125 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 if (isa<FunctionNoProtoType>(Ty))
1127 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001128 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1129 for (const QualType &ParamType : FPT->param_types())
1130 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001131 if (FPT->isVariadic())
1132 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001133 }
1134
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001136 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001137 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001138}
1139
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001140/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001141/// As an optimization, return 0 if the access specifier equals the
1142/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001143static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1144 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001145 AccessSpecifier Default = clang::AS_none;
1146 if (RD && RD->isClass())
1147 Default = clang::AS_private;
1148 else if (RD && (RD->isStruct() || RD->isUnion()))
1149 Default = clang::AS_public;
1150
1151 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001152 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001153
Eric Christophere7b87e52014-10-26 23:40:33 +00001154 switch (Access) {
1155 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001156 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001157 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001158 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001159 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001160 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001161 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001162 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001163 }
1164 llvm_unreachable("unexpected access enumerator");
1165}
Guy Benyei11169dd2012-12-18 14:30:41 +00001166
David Majnemerb4b671e2016-06-30 03:01:59 +00001167llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1168 llvm::DIScope *RecordTy,
1169 const RecordDecl *RD) {
1170 StringRef Name = BitFieldDecl->getName();
1171 QualType Ty = BitFieldDecl->getType();
1172 SourceLocation Loc = BitFieldDecl->getLocation();
1173 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1174 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1175
1176 // Get the location for the field.
1177 llvm::DIFile *File = getOrCreateFile(Loc);
1178 unsigned Line = getLineNumber(Loc);
1179
1180 const CGBitFieldInfo &BitFieldInfo =
1181 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1182 uint64_t SizeInBits = BitFieldInfo.Size;
1183 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001184 uint64_t StorageOffsetInBits =
1185 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001186 uint64_t Offset = BitFieldInfo.Offset;
1187 // The bit offsets for big endian machines are reversed for big
1188 // endian target, compensate for that as the DIDerivedType requires
1189 // un-reversed offsets.
1190 if (CGM.getDataLayout().isBigEndian())
1191 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1192 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001193 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001194 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001195 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1196 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001197}
1198
1199llvm::DIType *
1200CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1201 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001202 uint32_t AlignInBits, llvm::DIFile *tunit,
1203 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001204 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001205
1206 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001207 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001208 unsigned line = getLineNumber(loc);
1209
David Majnemer34b57492014-07-30 01:30:47 +00001210 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001211 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001212 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001213 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1214 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001215 if (!Align)
1216 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001217 }
1218
Leny Kholodov80c047d2016-09-06 10:48:04 +00001219 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001220 return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align,
1221 offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001222}
1223
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001224void CGDebugInfo::CollectRecordLambdaFields(
1225 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001226 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001227 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1228 // has the name and the location of the variable so we should iterate over
1229 // both concurrently.
1230 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1231 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1232 unsigned fieldno = 0;
1233 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001234 E = CXXDecl->captures_end();
1235 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001236 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001237 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001238 SourceLocation Loc = C.getLocation();
1239 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001240 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001241 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001242 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001243 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001244 llvm::DIType *FieldType = createFieldType(
1245 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001246 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001247 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001248 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001249 // TODO: Need to handle 'this' in some way by probably renaming the
1250 // this of the lambda class and having a field member of 'this' or
1251 // by using AT_object_pointer for the function and having that be
1252 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001253 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001254 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001255 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001256 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001257 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001258 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001259
1260 elements.push_back(fieldType);
1261 }
1262 }
1263}
1264
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265llvm::DIDerivedType *
1266CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001267 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001268 // Create the descriptor for the static variable, with or without
1269 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001270 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1272 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001273
Eric Christopher91a31902013-01-16 01:22:32 +00001274 unsigned LineNumber = getLineNumber(Var->getLocation());
1275 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001276 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001277 if (Var->getInit()) {
1278 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001279 if (Value) {
1280 if (Value->isInt())
1281 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1282 if (Value->isFloat())
1283 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1284 }
Eric Christopher91a31902013-01-16 01:22:32 +00001285 }
1286
Leny Kholodov80c047d2016-09-06 10:48:04 +00001287 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001288 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001289 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001290 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001291 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001292 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001293}
1294
Eric Christophere7b87e52014-10-26 23:40:33 +00001295void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001296 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1297 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001298 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001299 StringRef name = field->getName();
1300 QualType type = field->getType();
1301
1302 // Ignore unnamed fields unless they're anonymous structs/unions.
1303 if (name.empty() && !type->isRecordType())
1304 return;
1305
David Majnemerb4b671e2016-06-30 03:01:59 +00001306 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001307 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001308 FieldType = createBitFieldType(field, RecordTy, RD);
1309 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001310 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001311 FieldType =
1312 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001313 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001314 }
1315
David Majnemerb4b671e2016-06-30 03:01:59 +00001316 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001317}
1318
Reid Klecknere2e82062017-08-08 20:30:14 +00001319void CGDebugInfo::CollectRecordNestedType(
1320 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1321 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001322 // Injected class names are not considered nested records.
1323 if (isa<InjectedClassNameType>(Ty))
1324 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001325 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001326 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1327 elements.push_back(nestedType);
1328}
1329
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001330void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001331 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001332 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001334 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001335
Eric Christopher91a31902013-01-16 01:22:32 +00001336 if (CXXDecl && CXXDecl->isLambda())
1337 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1338 else {
1339 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
Eric Christopher91a31902013-01-16 01:22:32 +00001341 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001342 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001343
Eric Christopher91a31902013-01-16 01:22:32 +00001344 // Static and non-static members should appear in the same order as
1345 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001346 for (const auto *I : record->decls())
1347 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001348 if (V->hasAttr<NoDebugAttr>())
1349 continue;
Reid Kleckner891b2712018-07-20 20:55:00 +00001350
1351 // Skip variable template specializations when emitting CodeView. MSVC
1352 // doesn't emit them.
1353 if (CGM.getCodeGenOpts().EmitCodeView &&
1354 isa<VarTemplateSpecializationDecl>(V))
1355 continue;
1356
David Blaikiece763042013-08-20 21:49:21 +00001357 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001358 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001359 if (MI != StaticDataMemberCache.end()) {
1360 assert(MI->second &&
1361 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001362 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001363 } else {
1364 auto Field = CreateRecordStaticField(V, RecordTy, record);
1365 elements.push_back(Field);
1366 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001367 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001368 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1369 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001370
1371 // Bump field number for next field.
1372 ++fieldNo;
Reid Kleckner891b2712018-07-20 20:55:00 +00001373 } else if (CGM.getCodeGenOpts().EmitCodeView) {
1374 // Debug info for nested types is included in the member list only for
1375 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001376 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1377 if (!nestedType->isImplicit() &&
1378 nestedType->getDeclContext() == record)
1379 CollectRecordNestedType(nestedType, elements);
1380 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001381 }
1382}
1383
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001384llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001385CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001387 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001388 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001390 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001391 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1392 Func, Unit);
1393}
David Blaikie2aaf0652013-01-07 22:24:59 +00001394
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1396 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398 llvm::DITypeRefArray Args(
1399 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001400 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001401 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001402
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001403 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001404
1405 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001406 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001407
David Blaikie2aaf0652013-01-07 22:24:59 +00001408 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001409 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001410 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1411 // Create pointer type directly in this case.
1412 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1413 QualType PointeeTy = ThisPtrTy->getPointeeType();
1414 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001415 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001416 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001417 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1418 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001419 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001420 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001421 // TODO: This and the artificial type below are misleading, the
1422 // types aren't artificial the argument is, but the current
1423 // metadata doesn't represent that.
1424 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1425 Elts.push_back(ThisPtrType);
1426 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001427 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001428 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001429 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1430 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001431 }
1432
1433 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001434 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1435 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001436
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001437 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001438
Leny Kholodov80c047d2016-09-06 10:48:04 +00001439 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001440 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001442 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001444
Reid Klecknerf00f8032016-06-08 20:41:54 +00001445 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1446 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001447}
1448
Eric Christopherb2a008c2013-05-16 00:45:12 +00001449/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001450/// inside a function.
1451static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001452 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001453 return isFunctionLocalClass(NRD);
1454 if (isa<FunctionDecl>(RD->getDeclContext()))
1455 return true;
1456 return false;
1457}
1458
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001459llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1460 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001461 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001462 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001463
Guy Benyei11169dd2012-12-18 14:30:41 +00001464 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001465 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001466
1467 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1468 // make sense to give a single ctor/dtor a linkage name.
1469 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001470 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1471 // property to use here. It may've been intended to model "is non-external
1472 // type" but misses cases of non-function-local but non-external classes such
1473 // as those in anonymous namespaces as well as the reverse - external types
1474 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001475 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1476 MethodLinkageName = CGM.getMangledName(Method);
1477
1478 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001479 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001480 unsigned MethodLine = 0;
1481 if (!Method->isImplicit()) {
1482 MethodDefUnit = getOrCreateFile(Method->getLocation());
1483 MethodLine = getLineNumber(Method->getLocation());
1484 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001485
1486 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001487 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001488 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001489 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001490 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001491 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001492
Guy Benyei11169dd2012-12-18 14:30:41 +00001493 if (Method->isVirtual()) {
1494 if (Method->isPure())
1495 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1496 else
1497 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001498
Reid Kleckner216d0a12016-06-16 20:08:51 +00001499 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1500 // It doesn't make sense to give a virtual destructor a vtable index,
1501 // since a single destructor has two entries in the vtable.
1502 if (!isa<CXXDestructorDecl>(Method))
1503 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1504 } else {
1505 // Emit MS ABI vftable information. There is only one entry for the
1506 // deleting dtor.
1507 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1508 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
Reid Klecknercbec0262018-04-02 20:00:39 +00001509 MethodVFTableLocation ML =
Reid Kleckner216d0a12016-06-16 20:08:51 +00001510 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1511 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001512
1513 // CodeView only records the vftable offset in the class that introduces
1514 // the virtual method. This is possible because, unlike Itanium, the MS
1515 // C++ ABI does not include all virtual methods from non-primary bases in
1516 // the vtable for the most derived class. For example, if C inherits from
1517 // A and B, C's primary vftable will not include B's virtual methods.
Benjamin Krameracfa3392017-12-17 23:52:45 +00001518 if (Method->size_overridden_methods() == 0)
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001519 Flags |= llvm::DINode::FlagIntroducedVirtual;
1520
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001521 // The 'this' adjustment accounts for both the virtual and non-virtual
1522 // portions of the adjustment. Presumably the debugger only uses it when
1523 // it knows the dynamic type of an object.
1524 ThisAdjustment = CGM.getCXXABI()
1525 .getVirtualFunctionPrologueThisAdjustment(GD)
1526 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001527 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 ContainingType = RecordTy;
1529 }
1530
Adrian McCarthyd91bf392017-09-13 20:53:55 +00001531 if (Method->isStatic())
1532 Flags |= llvm::DINode::FlagStaticMember;
Guy Benyei11169dd2012-12-18 14:30:41 +00001533 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001534 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001535 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001536 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001537 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001538 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001539 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001540 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001541 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001542 }
1543 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001545 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001546 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001547 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001548 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001549
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001550 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1551 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001552 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001553 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1554 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1555 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001556
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001557 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001558
1559 return SP;
1560}
1561
Eric Christophere7b87e52014-10-26 23:40:33 +00001562void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001563 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1564 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001565
1566 // Since we want more than just the individual member decls if we
1567 // have templated functions iterate over every declaration to gather
1568 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001569 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001570 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1571 // If the member is implicit, don't add it to the member list. This avoids
1572 // the member being added to type units by LLVM, while still allowing it
1573 // to be emitted into the type declaration/reference inside the compile
1574 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001575 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001576 // FIXME: Handle Using(Shadow?)Decls here to create
1577 // DW_TAG_imported_declarations inside the class for base decls brought into
1578 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1579 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1580 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001581 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001582 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001583
1584 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1585 continue;
1586
David Blaikiefd580722014-10-06 05:18:55 +00001587 // Reuse the existing member function declaration if it exists.
1588 // It may be associated with the declaration of the type & should be
1589 // reused as we're building the definition.
1590 //
1591 // This situation can arise in the vtable-based debug info reduction where
1592 // implicit members are emitted in a non-vtable TU.
1593 auto MI = SPCache.find(Method->getCanonicalDecl());
1594 EltTys.push_back(MI == SPCache.end()
1595 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001596 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001598}
Guy Benyei11169dd2012-12-18 14:30:41 +00001599
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001600void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001601 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001602 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001603 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1604 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1605 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001606
Bob Haarmandff36732016-10-25 22:19:32 +00001607 // If we are generating CodeView debug info, we also need to emit records for
1608 // indirect virtual base classes.
1609 if (CGM.getCodeGenOpts().EmitCodeView) {
1610 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1611 llvm::DINode::FlagIndirectVirtualBase);
1612 }
1613}
1614
1615void CGDebugInfo::CollectCXXBasesAux(
1616 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1617 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1618 const CXXRecordDecl::base_class_const_range &Bases,
1619 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1620 llvm::DINode::DIFlags StartingFlags) {
1621 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1622 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001623 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001624 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001625 if (!SeenTypes.insert(Base).second)
1626 continue;
1627 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1628 llvm::DINode::DIFlags BFlags = StartingFlags;
1629 uint64_t BaseOffset;
Brock Wyma3db2b102018-05-14 21:21:22 +00001630 uint32_t VBPtrOffset = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001631
Aaron Ballman574705e2014-03-13 15:41:46 +00001632 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001633 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1634 // virtual base offset offset is -ve. The code generator emits dwarf
1635 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001636 BaseOffset = 0 - CGM.getItaniumVTableContext()
1637 .getVirtualBaseOffsetOffset(RD, Base)
1638 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001639 } else {
1640 // In the MS ABI, store the vbtable offset, which is analogous to the
1641 // vbase offset offset in Itanium.
1642 BaseOffset =
1643 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001644 VBPtrOffset = CGM.getContext()
1645 .getASTRecordLayout(RD)
1646 .getVBPtrOffset()
1647 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001648 }
Bob Haarmandff36732016-10-25 22:19:32 +00001649 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001650 } else
1651 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1652 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1653 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001654
Adrian Prantl21361fb2014-08-29 22:44:27 +00001655 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001656 llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset,
1657 VBPtrOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 EltTys.push_back(DTy);
1659 }
1660}
1661
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001662llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001663CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1664 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001665 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001666 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1668 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001669 StringRef Name;
1670 if (TPList)
1671 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001672 switch (TA.getKind()) {
1673 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001674 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001675 TemplateParams.push_back(
1676 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001677 } break;
1678 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001680 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1681 TheCU, Name, TTy,
1682 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001683 } break;
1684 case TemplateArgument::Declaration: {
1685 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001686 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001687 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001688 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001689 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001690 // Variable pointer template parameters have a value that is the address
1691 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001692 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001693 V = CGM.GetAddrOfGlobalVar(VD);
1694 // Member function pointers have special support for building them, though
1695 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001696 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001697 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001698 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001699 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001700 // Member data pointers have special handling too to compute the fixed
1701 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001702 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001703 // These five lines (& possibly the above member function pointer
1704 // handling) might be able to be refactored to use similar code in
1705 // CodeGenModule::getMemberPointerConstant
1706 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1707 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001708 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001709 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001710 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001711 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1712 TheCU, Name, TTy,
1713 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001714 } break;
1715 case TemplateArgument::NullPtr: {
1716 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001717 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001718 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001719 // Special case member data pointer null values since they're actually -1
1720 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001721 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001722 // But treat member function pointers as simple zero integers because
1723 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1724 // CodeGen grows handling for values of non-null member function
1725 // pointers then perhaps we could remove this special case and rely on
1726 // EmitNullMemberPointer for member function pointers.
1727 if (MPT->isMemberDataPointer())
1728 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1729 if (!V)
1730 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001731 TemplateParams.push_back(
1732 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001733 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001734 case TemplateArgument::Template:
1735 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001736 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001737 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1738 break;
1739 case TemplateArgument::Pack:
1740 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1741 TheCU, Name, nullptr,
1742 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1743 break;
David Majnemer5559d472013-08-24 08:21:10 +00001744 case TemplateArgument::Expression: {
1745 const Expr *E = TA.getAsExpr();
1746 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001747 if (E->isGLValue())
1748 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001749 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001750 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001751 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001752 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001753 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001754 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001755 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001756 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001757 case TemplateArgument::Null:
1758 llvm_unreachable(
1759 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 }
1761 }
1762 return DBuilder.getOrCreateArray(TemplateParams);
1763}
1764
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001765llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001766CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001767 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001768 if (FD->getTemplatedKind() ==
1769 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001770 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1771 ->getTemplate()
1772 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001773 return CollectTemplateParams(
1774 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001776 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001777}
1778
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001779llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1780 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001781 // Always get the full list of parameters, not just the ones from
1782 // the specialization.
1783 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001784 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001785 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001786 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001787}
1788
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001789llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001790 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 return VTablePtrType;
1792
1793 ASTContext &Context = CGM.getContext();
1794
1795 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001796 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001797 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001798 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001800 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1801 Optional<unsigned> DWARFAddressSpace =
1802 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1803
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001804 llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType(
1805 SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001806 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1807 return VTablePtrType;
1808}
1809
Guy Benyei11169dd2012-12-18 14:30:41 +00001810StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001811 // Copy the gdb compatible name on the side and use its reference.
1812 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001813}
1814
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001815void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001816 SmallVectorImpl<llvm::Metadata *> &EltTys,
1817 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 // If this class is not dynamic then there is not any vtable info to collect.
1819 if (!RD->isDynamicClass())
1820 return;
1821
Reid Kleckner59812422016-08-31 20:35:01 +00001822 // Don't emit any vtable shape or vptr info if this class doesn't have an
1823 // extendable vfptr. This can happen if the class doesn't have virtual
1824 // methods, or in the MS ABI if those virtual methods only come from virtually
1825 // inherited bases.
1826 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1827 if (!RL.hasExtendableVFPtr())
1828 return;
1829
Reid Klecknerdc124992016-08-31 16:11:43 +00001830 // CodeView needs to know how large the vtable of every dynamic class is, so
1831 // emit a special named pointer type into the element list. The vptr type
1832 // points to this type as well.
1833 llvm::DIType *VPtrTy = nullptr;
1834 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1835 CGM.getTarget().getCXXABI().isMicrosoft();
1836 if (NeedVTableShape) {
1837 uint64_t PtrWidth =
1838 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1839 const VTableLayout &VFTLayout =
1840 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1841 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001842 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001843 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001844 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1845 Optional<unsigned> DWARFAddressSpace =
1846 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001847
1848 // Create a very wide void* type and insert it directly in the element list.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001849 llvm::DIType *VTableType = DBuilder.createPointerType(
1850 nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001851 EltTys.push_back(VTableType);
1852
1853 // The vptr is a pointer to this special vtable type.
1854 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1855 }
1856
1857 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001858 if (RL.getPrimaryBase())
1859 return;
1860
1861 if (!VPtrTy)
1862 VPtrTy = getOrCreateVTablePtrType(Unit);
1863
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001865 llvm::DIType *VPtrMember =
1866 DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1867 llvm::DINode::FlagArtificial, VPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001868 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001869}
1870
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001872 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001873 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001874 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 return T;
1876}
1877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001878llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001879 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001880 return getOrCreateStandaloneType(D, Loc);
1881}
1882
1883llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1884 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001885 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001886 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001887 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001888 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001889
Adrian Prantl73409ce2013-03-11 18:33:46 +00001890 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 return T;
1892}
1893
David Blaikie483a9da2014-05-06 18:35:21 +00001894void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001895 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001896 return;
1897 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001899 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001900 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001901 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001902 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001903 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001904 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001905}
1906
David Blaikieb2e86eb2013-08-15 20:49:17 +00001907void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001908 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001909 !CGM.getLangOpts().CPlusPlus)
1910 completeRequiredType(RD);
1911}
1912
David Blaikieb11c8732017-01-30 06:36:08 +00001913/// Return true if the class or any of its methods are marked dllimport.
1914static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1915 if (RD->hasAttr<DLLImportAttr>())
1916 return true;
1917 for (const CXXMethodDecl *MD : RD->methods())
1918 if (MD->hasAttr<DLLImportAttr>())
1919 return true;
1920 return false;
1921}
1922
Adrian Prantla43acdc2017-07-24 23:48:51 +00001923/// Does a type definition exist in an imported clang module?
1924static bool isDefinedInClangModule(const RecordDecl *RD) {
1925 // Only definitions that where imported from an AST file come from a module.
1926 if (!RD || !RD->isFromASTFile())
1927 return false;
1928 // Anonymous entities cannot be addressed. Treat them as not from module.
1929 if (!RD->isExternallyVisible() && RD->getName().empty())
1930 return false;
1931 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1932 if (!CXXDecl->isCompleteDefinition())
1933 return false;
1934 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1935 if (TemplateKind != TSK_Undeclared) {
1936 // This is a template, check the origin of the first member.
1937 if (CXXDecl->field_begin() == CXXDecl->field_end())
1938 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1939 if (!CXXDecl->field_begin()->isFromASTFile())
1940 return false;
1941 }
1942 }
1943 return true;
1944}
1945
David Blaikie6943dea2013-08-20 01:28:15 +00001946void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001947 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1948 if (CXXRD->isDynamicClass() &&
1949 CGM.getVTableLinkage(CXXRD) ==
1950 llvm::GlobalValue::AvailableExternallyLinkage &&
1951 !isClassOrMethodDLLImport(CXXRD))
1952 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001953
1954 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1955 return;
1956
David Blaikieb11c8732017-01-30 06:36:08 +00001957 completeClass(RD);
1958}
1959
1960void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001961 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001962 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001963 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001964 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001965 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001966 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001967 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001969 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001970 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001971}
1972
David Blaikie0e716b42014-03-03 23:48:23 +00001973static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1974 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001975 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1976 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001977 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001978 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001979 return true;
1980 return false;
1981}
1982
Benjamin Kramer8c305922016-02-02 11:06:51 +00001983static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1984 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001985 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001986 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001987 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001988
David Blaikie1ac9c982017-04-11 21:13:37 +00001989 if (auto *ES = RD->getASTContext().getExternalSource())
1990 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1991 return true;
1992
Benjamin Kramer8c305922016-02-02 11:06:51 +00001993 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001994 return false;
1995
1996 if (!LangOpts.CPlusPlus)
1997 return false;
1998
1999 if (!RD->isCompleteDefinitionRequired())
2000 return true;
2001
David Majnemer58ed0f32016-07-17 00:39:12 +00002002 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00002003
2004 if (!CXXDecl)
2005 return false;
2006
Adrian McCarthy99242982016-08-16 22:11:18 +00002007 // Only emit complete debug info for a dynamic class when its vtable is
2008 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00002009 // across DLL boundaries, so skip this optimization if the class or any of its
2010 // methods are marked dllimport. This isn't a complete solution, since objects
2011 // without any dllimport methods can be used in one DLL and constructed in
2012 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00002013 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00002014 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00002015 return true;
2016
2017 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00002018 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00002019 Spec = SD->getSpecializationKind();
2020
2021 if (Spec == TSK_ExplicitInstantiationDeclaration &&
2022 hasExplicitMemberDefinition(CXXDecl->method_begin(),
2023 CXXDecl->method_end()))
2024 return true;
2025
2026 return false;
2027}
2028
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00002029void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
2030 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
2031 return;
2032
2033 QualType Ty = CGM.getContext().getRecordType(RD);
2034 llvm::DIType *T = getTypeOrNull(Ty);
2035 if (T && T->isForwardDecl())
2036 completeClassData(RD);
2037}
2038
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002039llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002041 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002042 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
2043 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002044 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002045 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002046 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00002047 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002048
David Blaikieb2e86eb2013-08-15 20:49:17 +00002049 return CreateTypeDefinition(Ty);
2050}
2051
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002052llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00002053 RecordDecl *RD = Ty->getDecl();
2054
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002056 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002057
2058 // Records and classes and unions can all be recursive. To handle them, we
2059 // first generate a debug descriptor for the struct as a forward declaration.
2060 // Then (if it is a definition) we go through and get debug info for all of
2061 // its members. Finally, we create a descriptor for the complete type (which
2062 // may refer to the forward decl if the struct is recursive) and replace all
2063 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002064 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002065
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002066 const RecordDecl *D = RD->getDefinition();
2067 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 return FwdDecl;
2069
David Majnemer58ed0f32016-07-17 00:39:12 +00002070 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00002071 CollectContainingType(CXXDecl, FwdDecl);
2072
Guy Benyei11169dd2012-12-18 14:30:41 +00002073 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002074 LexicalBlockStack.emplace_back(&*FwdDecl);
2075 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002076
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002078 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00002079 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00002080
2081 // Note: The split of CXXDecl information here is intentional, the
2082 // gdb tests will depend on a certain ordering at printout. The debug
2083 // information offsets are still correct if we merge them all together
2084 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00002085 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002086 if (CXXDecl) {
2087 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00002088 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 }
2090
Eric Christopher91a31902013-01-16 01:22:32 +00002091 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00002092 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00002093 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002095
2096 LexicalBlockStack.pop_back();
2097 RegionMap.erase(Ty->getDecl());
2098
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002099 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002100 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002101
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002102 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002103 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002104 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002105
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002106 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002107 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002108}
2109
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002110llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
2111 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 // Ignore protocols.
2113 return getOrCreateType(Ty->getBaseType(), Unit);
2114}
2115
Manman Rene6be26c2016-09-13 17:25:08 +00002116llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
2117 llvm::DIFile *Unit) {
2118 // Ignore protocols.
2119 SourceLocation Loc = Ty->getDecl()->getLocation();
2120
2121 // Use Typedefs to represent ObjCTypeParamType.
2122 return DBuilder.createTypedef(
2123 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
2124 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2125 getDeclContextDescriptor(Ty->getDecl()));
2126}
2127
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002128/// \return true if Getter has the default name for the property PD.
2129static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2130 const ObjCMethodDecl *Getter) {
2131 assert(PD);
2132 if (!Getter)
2133 return true;
2134
2135 assert(Getter->getDeclName().isObjCZeroArgSelector());
2136 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002137 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002138}
2139
2140/// \return true if Setter has the default name for the property PD.
2141static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2142 const ObjCMethodDecl *Setter) {
2143 assert(PD);
2144 if (!Setter)
2145 return true;
2146
2147 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002148 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002149 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002150}
2151
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002152llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2153 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002154 ObjCInterfaceDecl *ID = Ty->getDecl();
2155 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002156 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002157
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002158 // Return a forward declaration if this type was imported from a clang module,
2159 // and this is not the compile unit with the implementation of the type (which
2160 // may contain hidden ivars).
2161 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2162 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002163 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2164 ID->getName(),
2165 getDeclContextDescriptor(ID), Unit, 0);
2166
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002168 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002169 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002170 auto RuntimeLang =
2171 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002172
2173 // If this is just a forward declaration return a special forward-declaration
2174 // debug type since we won't be able to lay out the entire type.
2175 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002176 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002177 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002179 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2180 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002181 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 return FwdDecl;
2183 }
2184
David Blaikieef8a9512014-05-05 23:23:53 +00002185 return CreateTypeDefinition(Ty, Unit);
2186}
2187
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002188llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002189CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2190 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002191 // Use the Module pointer as the key into the cache. This is a
2192 // nullptr if the "Module" is a PCH, which is safe because we don't
2193 // support chained PCH debug info, so there can only be a single PCH.
2194 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002195 auto ModRef = ModuleCache.find(M);
2196 if (ModRef != ModuleCache.end())
2197 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002198
2199 // Macro definitions that were defined with "-D" on the command line.
2200 SmallString<128> ConfigMacros;
2201 {
2202 llvm::raw_svector_ostream OS(ConfigMacros);
2203 const auto &PPOpts = CGM.getPreprocessorOpts();
2204 unsigned I = 0;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002205 // Translate the macro definitions back into a command line.
Adrian Prantl2388ead2015-06-30 18:01:05 +00002206 for (auto &M : PPOpts.Macros) {
2207 if (++I > 1)
2208 OS << " ";
2209 const std::string &Macro = M.first;
2210 bool Undef = M.second;
2211 OS << "\"-" << (Undef ? 'U' : 'D');
2212 for (char c : Macro)
2213 switch (c) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002214 case '\\':
2215 OS << "\\\\";
2216 break;
2217 case '"':
2218 OS << "\\\"";
2219 break;
2220 default:
2221 OS << c;
Adrian Prantl2388ead2015-06-30 18:01:05 +00002222 }
2223 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002224 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002225 }
Adrian Prantl66689202015-09-18 23:01:45 +00002226
Adrian Prantl835e6632015-09-24 16:10:10 +00002227 bool IsRootModule = M ? !M->Parent : true;
2228 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002229 // PCH files don't have a signature field in the control block,
2230 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002231 // We use the lower 64 bits for debug info.
2232 uint64_t Signature =
2233 Mod.getSignature()
2234 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2235 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002236 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002237 DIB.createCompileUnit(TheCU->getSourceLanguage(),
Scott Lindera2fbcef2018-02-26 17:32:31 +00002238 // TODO: Support "Source" from external AST providers?
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002239 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2240 TheCU->getProducer(), true, StringRef(), 0,
2241 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2242 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002243 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002244 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002245 llvm::DIModule *Parent =
2246 IsRootModule ? nullptr
2247 : getOrCreateModuleRef(
2248 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2249 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002250 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002251 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2252 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002253 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002254 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002255}
2256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002257llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2258 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002259 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002260 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002261 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002262 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002263
2264 // Bit size, align and offset of the type.
2265 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002266 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002267
Leny Kholodov80c047d2016-09-06 10:48:04 +00002268 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002269 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002270 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002271
Adrian Prantlfd696112015-10-01 00:48:51 +00002272 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002273 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002274 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2275 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002276
David Blaikieef8a9512014-05-05 23:23:53 +00002277 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002278 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002279
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002280 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002281 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002282 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002283
2284 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002285 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002286
2287 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2288 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002289 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002290 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002291 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002292 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002293
Brock Wyma3db2b102018-05-14 21:21:22 +00002294 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002295 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002296 EltTys.push_back(InhTag);
2297 }
2298
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002299 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002300 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002301 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002302 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 unsigned PLine = getLineNumber(Loc);
2304 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2305 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002306 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2307 PD->getName(), PUnit, PLine,
2308 hasDefaultGetterName(PD, Getter) ? ""
2309 : getSelectorName(PD->getGetterName()),
2310 hasDefaultSetterName(PD, Setter) ? ""
2311 : getSelectorName(PD->getSetterName()),
2312 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002314 };
2315 {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002316 llvm::SmallPtrSet<const IdentifierInfo *, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002317 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002318 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002319 PropertySet.insert(PD->getIdentifier());
2320 AddProperty(PD);
2321 }
Manman Renefe1bac2016-01-27 20:00:32 +00002322 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002323 // Don't emit duplicate metadata for properties that were already in a
2324 // class extension.
2325 if (!PropertySet.insert(PD->getIdentifier()).second)
2326 continue;
2327 AddProperty(PD);
2328 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002329 }
2330
2331 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2332 unsigned FieldNo = 0;
2333 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2334 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002335 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002336 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002337 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002338
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 StringRef FieldName = Field->getName();
2340
2341 // Ignore unnamed fields.
2342 if (FieldName.empty())
2343 continue;
2344
2345 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002346 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 unsigned FieldLine = getLineNumber(Field->getLocation());
2348 QualType FType = Field->getType();
2349 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002350 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002351
2352 if (!FType->isIncompleteArrayType()) {
2353
2354 // Bit size, align and offset of the type.
2355 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002356 ? Field->getBitWidthValue(CGM.getContext())
2357 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002358 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002359 }
2360
2361 uint64_t FieldOffset;
2362 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2363 // We don't know the runtime offset of an ivar if we're using the
2364 // non-fragile ABI. For bitfields, use the bit offset into the first
2365 // byte of storage of the bitfield. For other fields, use zero.
2366 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002367 FieldOffset =
2368 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 FieldOffset %= CGM.getContext().getCharWidth();
2370 } else {
2371 FieldOffset = 0;
2372 }
2373 } else {
2374 FieldOffset = RL.getFieldOffset(FieldNo);
2375 }
2376
Leny Kholodov80c047d2016-09-06 10:48:04 +00002377 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002378 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002379 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002382 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002383 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002384
Craig Topper8a13c412014-05-21 05:09:00 +00002385 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002387 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002388 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002389 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002390 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002391 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002392 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002393 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2394 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002395 PropertyNode = DBuilder.createObjCProperty(
2396 PD->getName(), PUnit, PLine,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002397 hasDefaultGetterName(PD, Getter)
2398 ? ""
2399 : getSelectorName(PD->getGetterName()),
2400 hasDefaultSetterName(PD, Setter)
2401 ? ""
2402 : getSelectorName(PD->getSetterName()),
Eric Christophere7b87e52014-10-26 23:40:33 +00002403 PD->getPropertyAttributes(),
2404 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002405 }
2406 }
2407 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002408 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2409 FieldSize, FieldAlign, FieldOffset, Flags,
2410 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002411 EltTys.push_back(FieldTy);
2412 }
2413
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002414 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002415 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002416
Guy Benyei11169dd2012-12-18 14:30:41 +00002417 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002418 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002419}
2420
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002421llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2422 llvm::DIFile *Unit) {
2423 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 int64_t Count = Ty->getNumElements();
Guy Benyei11169dd2012-12-18 14:30:41 +00002425
Sander de Smalen891af03a2018-02-03 13:55:59 +00002426 llvm::Metadata *Subscript;
2427 QualType QTy(Ty, 0);
2428 auto SizeExpr = SizeExprCache.find(QTy);
2429 if (SizeExpr != SizeExprCache.end())
2430 Subscript = DBuilder.getOrCreateSubrange(0, SizeExpr->getSecond());
2431 else
2432 Subscript = DBuilder.getOrCreateSubrange(0, Count ? Count : -1);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002433 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002434
2435 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002436 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002437
2438 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2439}
2440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002442 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002443 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002444
2445 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002446 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002447 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002448 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2449 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002450 } else if (Ty->isIncompleteArrayType()) {
2451 Size = 0;
2452 if (Ty->getElementType()->isIncompleteType())
2453 Align = 0;
2454 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002455 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002456 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002457 Size = 0;
2458 Align = 0;
2459 } else {
2460 // Size and align of the whole array, not the element type.
2461 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002462 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 }
2464
2465 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2466 // interior arrays, do we care? Why aren't nested arrays represented the
2467 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002468 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002469 QualType EltTy(Ty, 0);
2470 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2471 // If the number of elements is known, then count is that number. Otherwise,
2472 // it's -1. This allows us to represent a subrange with an array of 0
2473 // elements, like this:
2474 //
2475 // struct foo {
2476 // int x[0];
2477 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002478 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002479 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002480 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002481 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002482 if (Expr *Size = VAT->getSizeExpr()) {
2483 llvm::APSInt V;
2484 if (Size->EvaluateAsInt(V, CGM.getContext()))
2485 Count = V.getExtValue();
2486 }
David Blaikie87173f12016-08-22 17:49:56 +00002487 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002488
Sander de Smalen891af03a2018-02-03 13:55:59 +00002489 auto SizeNode = SizeExprCache.find(EltTy);
2490 if (SizeNode != SizeExprCache.end())
2491 Subscripts.push_back(
2492 DBuilder.getOrCreateSubrange(0, SizeNode->getSecond()));
2493 else
2494 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 EltTy = Ty->getElementType();
2496 }
2497
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002498 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002499
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002500 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2501 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002502}
2503
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002504llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2505 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002506 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2507 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002508}
2509
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002510llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2511 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002512 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2513 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002514}
2515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002516llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2517 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002518 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002519 uint64_t Size = 0;
2520
2521 if (!Ty->isIncompleteType()) {
2522 Size = CGM.getContext().getTypeSize(Ty);
2523
2524 // Set the MS inheritance model. There is no flag for the unspecified model.
2525 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2526 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2527 case MSInheritanceAttr::Keyword_single_inheritance:
2528 Flags |= llvm::DINode::FlagSingleInheritance;
2529 break;
2530 case MSInheritanceAttr::Keyword_multiple_inheritance:
2531 Flags |= llvm::DINode::FlagMultipleInheritance;
2532 break;
2533 case MSInheritanceAttr::Keyword_virtual_inheritance:
2534 Flags |= llvm::DINode::FlagVirtualInheritance;
2535 break;
2536 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2537 break;
2538 }
2539 }
2540 }
2541
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002542 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002543 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002544 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002545 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2546 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002547
2548 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002549 Ty->getPointeeType()->getAs<FunctionProtoType>();
2550 return DBuilder.createMemberPointerType(
2551 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2552 Ty->getClass(), FPT->getTypeQuals())),
2553 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002554 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002555}
2556
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002557llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002558 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2559 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002560}
2561
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002562llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00002563 return getOrCreateType(Ty->getElementType(), U);
2564}
2565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002566llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002567 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002570 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 if (!ED->getTypeForDecl()->isIncompleteType()) {
2572 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002573 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 }
2575
Brock Wyma8557ec52018-05-22 12:41:19 +00002576 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002577
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002578 bool isImportedFromModule =
2579 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2580
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 // If this is just a forward declaration, construct an appropriately
2582 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002583 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002584 // Note that it is possible for enums to be created as part of
2585 // their own declcontext. In this case a FwdDecl will be created
2586 // twice. This doesn't cause a problem because both FwdDecls are
2587 // entered into the ReplaceMap: finalize() will replace the first
2588 // FwdDecl with the second and then replace the second with
2589 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002590 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002591 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002592 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2593 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002594
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 unsigned Line = getLineNumber(ED->getLocation());
2596 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002597 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002598 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Brock Wyma8557ec52018-05-22 12:41:19 +00002599 0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
Adrian Prantla40030f2016-02-06 01:59:09 +00002600
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002601 ReplaceMap.emplace_back(
2602 std::piecewise_construct, std::make_tuple(Ty),
2603 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002604 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002605 }
2606
David Blaikie483a9da2014-05-06 18:35:21 +00002607 return CreateTypeDefinition(Ty);
2608}
2609
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002610llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002611 const EnumDecl *ED = Ty->getDecl();
2612 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002613 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002614 if (!ED->getTypeForDecl()->isIncompleteType()) {
2615 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002616 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002617 }
2618
Brock Wyma8557ec52018-05-22 12:41:19 +00002619 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
David Blaikie483a9da2014-05-06 18:35:21 +00002620
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002621 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002622 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 ED = ED->getDefinition();
Momchil Velikov25f6be52018-02-12 16:12:52 +00002624 bool IsSigned = ED->getIntegerType()->isSignedIntegerType();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002625 for (const auto *Enum : ED->enumerators()) {
Momchil Velikov25f6be52018-02-12 16:12:52 +00002626 const auto &InitVal = Enum->getInitVal();
2627 auto Value = IsSigned ? InitVal.getSExtValue() : InitVal.getZExtValue();
2628 Enumerators.push_back(
2629 DBuilder.createEnumerator(Enum->getName(), Value, !IsSigned));
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 }
2631
2632 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002633 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002634
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002635 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002637 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Momchil Velikov25f6be52018-02-12 16:12:52 +00002638 llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002639 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2640 Line, Size, Align, EltArray, ClassTy,
Brock Wyma8557ec52018-05-22 12:41:19 +00002641 Identifier, ED->isFixed());
Guy Benyei11169dd2012-12-18 14:30:41 +00002642}
2643
Amjad Aboud546bc112017-02-09 22:07:24 +00002644llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2645 unsigned MType, SourceLocation LineLoc,
2646 StringRef Name, StringRef Value) {
2647 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2648 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2649}
2650
2651llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2652 SourceLocation LineLoc,
2653 SourceLocation FileLoc) {
2654 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2655 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2656 return DBuilder.createTempMacroFile(Parent, Line, FName);
2657}
2658
David Blaikie05491062013-01-21 04:37:12 +00002659static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2660 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002662 Qualifiers InnerQuals = T.getLocalQualifiers();
2663 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2664 // that is already there.
2665 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2666 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 QualType LastT = T;
2668 switch (T->getTypeClass()) {
2669 default:
David Blaikie05491062013-01-21 04:37:12 +00002670 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002671 case Type::TemplateSpecialization: {
2672 const auto *Spec = cast<TemplateSpecializationType>(T);
2673 if (Spec->isTypeAlias())
2674 return C.getQualifiedType(T.getTypePtr(), Quals);
2675 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002676 break;
2677 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002678 case Type::TypeOfExpr:
2679 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2680 break;
2681 case Type::TypeOf:
2682 T = cast<TypeOfType>(T)->getUnderlyingType();
2683 break;
2684 case Type::Decltype:
2685 T = cast<DecltypeType>(T)->getUnderlyingType();
2686 break;
2687 case Type::UnaryTransform:
2688 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2689 break;
2690 case Type::Attributed:
2691 T = cast<AttributedType>(T)->getEquivalentType();
2692 break;
2693 case Type::Elaborated:
2694 T = cast<ElaboratedType>(T)->getNamedType();
2695 break;
2696 case Type::Paren:
2697 T = cast<ParenType>(T)->getInnerType();
2698 break;
David Blaikie05491062013-01-21 04:37:12 +00002699 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002702 case Type::Auto:
2703 case Type::DeducedTemplateSpecialization: {
2704 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002705 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002706 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 break;
2708 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002709 case Type::Adjusted:
2710 case Type::Decayed:
2711 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2712 T = cast<AdjustedType>(T)->getAdjustedType();
2713 break;
2714 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002717 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 } while (true);
2719}
2720
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002721llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002722
2723 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002724 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002725
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002726 auto It = TypeCache.find(Ty.getAsOpaquePtr());
2727 if (It != TypeCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002728 // Verify that the debug info still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002729 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002730 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 }
2732
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002733 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002734}
2735
David Blaikie0e716b42014-03-03 23:48:23 +00002736void CGDebugInfo::completeTemplateDefinition(
2737 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002738 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002739 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002740 completeUnusedClass(SD);
2741}
David Blaikie0856f662014-03-04 22:01:08 +00002742
David Blaikie1ac9c982017-04-11 21:13:37 +00002743void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2744 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2745 return;
2746
2747 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002748 // In case this type has no member function definitions being emitted, ensure
2749 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002750 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002751}
2752
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002753llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002755 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002756
2757 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002758 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002759
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002760 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 return T;
2762
Adrian Prantlca844182015-09-11 17:23:03 +00002763 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002764 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002765
2766 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002767 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002768
Guy Benyei11169dd2012-12-18 14:30:41 +00002769 return Res;
2770}
2771
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002772llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002773 // A forward declaration inside a module header does not belong to the module.
2774 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2775 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002776 if (DebugTypeExtRefs && D->isFromASTFile()) {
2777 // Record a reference to an imported clang module or precompiled header.
2778 auto *Reader = CGM.getContext().getExternalSource();
2779 auto Idx = D->getOwningModuleID();
2780 auto Info = Reader->getSourceDescriptor(Idx);
2781 if (Info)
2782 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2783 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002784 // We are building a clang module or a precompiled header.
2785 //
2786 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2787 // and it wouldn't be necessary to specify the parent scope
2788 // because the type is already unique by definition (it would look
2789 // like the output of -fno-standalone-debug). On the other hand,
2790 // the parent scope helps a consumer to quickly locate the object
2791 // file where the type's definition is located, so it might be
2792 // best to make this behavior a command line or debugger tuning
2793 // option.
Richard Smith54f04402017-05-18 02:29:20 +00002794 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002795 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002796 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2797 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002798 } else {
2799 // This the precompiled header being built.
2800 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002801 }
2802 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002803
Adrian Prantl9402cef2015-09-20 16:51:35 +00002804 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002805}
2806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002807llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 // Handle qualifiers, which recursively handles what they refer to.
2809 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002810 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002811
Guy Benyei11169dd2012-12-18 14:30:41 +00002812 // Work out details of type.
2813 switch (Ty->getTypeClass()) {
2814#define TYPE(Class, Base)
2815#define ABSTRACT_TYPE(Class, Base)
2816#define NON_CANONICAL_TYPE(Class, Base)
2817#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2818#include "clang/AST/TypeNodes.def"
2819 llvm_unreachable("Dependent types cannot show up in debug information");
2820
2821 case Type::ExtVector:
2822 case Type::Vector:
2823 return CreateType(cast<VectorType>(Ty), Unit);
2824 case Type::ObjCObjectPointer:
2825 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2826 case Type::ObjCObject:
2827 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002828 case Type::ObjCTypeParam:
2829 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002830 case Type::ObjCInterface:
2831 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2832 case Type::Builtin:
2833 return CreateType(cast<BuiltinType>(Ty));
2834 case Type::Complex:
2835 return CreateType(cast<ComplexType>(Ty));
2836 case Type::Pointer:
2837 return CreateType(cast<PointerType>(Ty), Unit);
2838 case Type::BlockPointer:
2839 return CreateType(cast<BlockPointerType>(Ty), Unit);
2840 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002841 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002843 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002845 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 case Type::FunctionProto:
2847 case Type::FunctionNoProto:
2848 return CreateType(cast<FunctionType>(Ty), Unit);
2849 case Type::ConstantArray:
2850 case Type::VariableArray:
2851 case Type::IncompleteArray:
2852 return CreateType(cast<ArrayType>(Ty), Unit);
2853
2854 case Type::LValueReference:
2855 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2856 case Type::RValueReference:
2857 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2858
2859 case Type::MemberPointer:
2860 return CreateType(cast<MemberPointerType>(Ty), Unit);
2861
2862 case Type::Atomic:
2863 return CreateType(cast<AtomicType>(Ty), Unit);
2864
Xiuli Pan9c14e282016-01-09 12:53:17 +00002865 case Type::Pipe:
2866 return CreateType(cast<PipeType>(Ty), Unit);
2867
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002869 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2870
David Blaikie42edade2014-11-11 20:44:45 +00002871 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002872 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002873 case Type::Adjusted:
2874 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002875 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 case Type::Elaborated:
2877 case Type::Paren:
2878 case Type::SubstTemplateTypeParm:
2879 case Type::TypeOfExpr:
2880 case Type::TypeOf:
2881 case Type::Decltype:
2882 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002883 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002884 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002886
David Blaikie42edade2014-11-11 20:44:45 +00002887 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002888}
2889
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002890llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2891 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002892 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002893
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002894 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002895
2896 // We may have cached a forward decl when we could have created
2897 // a non-forward decl. Go ahead and create a non-forward decl
2898 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002899 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002900 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002901
2902 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002903 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002904
2905 // Propagate members from the declaration to the definition
2906 // CreateType(const RecordType*) will overwrite this with the members in the
2907 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002908 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002909
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002911 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002912 return Res;
2913}
2914
2915// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002916llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002917 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002918
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002920 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 unsigned Line = getLineNumber(RD->getLocation());
2922 StringRef RDName = getClassName(RD);
2923
Amjad Abouddc4531e2016-04-30 01:44:38 +00002924 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002925
David Blaikied2785892013-08-18 17:36:19 +00002926 // If we ended up creating the type during the context chain construction,
2927 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002928 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002929 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002930 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002931 return T;
David Blaikied2785892013-08-18 17:36:19 +00002932
Adrian Prantl381e7552014-02-04 21:29:50 +00002933 // If this is just a forward or incomplete declaration, construct an
2934 // appropriately marked node and just return it.
2935 const RecordDecl *D = RD->getDefinition();
2936 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002937 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002938
2939 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002940 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002941
Brock Wyma8557ec52018-05-22 12:41:19 +00002942 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002943
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002944 // Explicitly record the calling convention for C++ records.
2945 auto Flags = llvm::DINode::FlagZero;
2946 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2947 if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect)
2948 Flags |= llvm::DINode::FlagTypePassByReference;
2949 else
2950 Flags |= llvm::DINode::FlagTypePassByValue;
Aaron Smith044326c2018-07-23 20:49:07 +00002951
2952 // Record if a C++ record is trivial type.
2953 if (CXXRD->isTrivial())
2954 Flags |= llvm::DINode::FlagTrivial;
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002955 }
2956
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002957 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002958 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +00002959 Flags, Identifier);
Guy Benyei11169dd2012-12-18 14:30:41 +00002960
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002961 // Elements of composite types usually have back to the type, creating
2962 // uniquing cycles. Distinct nodes are more efficient.
2963 switch (RealDecl->getTag()) {
2964 default:
2965 llvm_unreachable("invalid composite type tag");
2966
2967 case llvm::dwarf::DW_TAG_array_type:
2968 case llvm::dwarf::DW_TAG_enumeration_type:
2969 // Array elements and most enumeration elements don't have back references,
2970 // so they don't tend to be involved in uniquing cycles and there is some
2971 // chance of merging them when linking together two modules. Only make
2972 // them distinct if they are ODR-uniqued.
Brock Wyma8557ec52018-05-22 12:41:19 +00002973 if (Identifier.empty())
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002974 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002975 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002976
2977 case llvm::dwarf::DW_TAG_structure_type:
2978 case llvm::dwarf::DW_TAG_union_type:
2979 case llvm::dwarf::DW_TAG_class_type:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002980 // Immediately resolve to a distinct node.
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002981 RealDecl =
2982 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2983 break;
2984 }
2985
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002986 RegionMap[Ty->getDecl()].reset(RealDecl);
2987 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002988
David Majnemer58ed0f32016-07-17 00:39:12 +00002989 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002990 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002991 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002992 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002993}
2994
David Blaikieadfbf992013-08-18 16:55:33 +00002995void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002996 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002997 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002998 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002999 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3000 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00003001 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00003002 while (1) {
3003 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
3004 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
3005 if (PBT && !BRL.isPrimaryBaseVirtual())
3006 PBase = PBT;
3007 else
3008 break;
3009 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003010 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00003011 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
3012 getOrCreateFile(RD->getLocation())));
3013 } else if (RD->isDynamicClass())
3014 ContainingType = RealDecl;
3015
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00003016 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00003017}
3018
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003019llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003020 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003021 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00003023 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003024 llvm::DIType *Ty =
3025 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
3026 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 *Offset += FieldSize;
3028 return Ty;
3029}
3030
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003031void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00003032 StringRef &Name,
3033 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003034 llvm::DIScope *&FDContext,
3035 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003036 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003037 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00003038 Name = getFunctionName(FD);
3039 // Use mangled name as linkage name for C/C++ functions.
3040 if (FD->hasPrototype()) {
3041 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003042 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00003043 }
3044 // No need to replicate the linkage name if it isn't different from the
3045 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00003046 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00003047 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
3048 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00003049 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00003050 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00003051 LinkageName = StringRef();
3052
Benjamin Kramer8c305922016-02-02 11:06:51 +00003053 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003054 if (const NamespaceDecl *NSDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003055 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00003056 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00003057 else if (const RecordDecl *RDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003058 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003059 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
3060 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
3061 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00003062 // Check if it is a noreturn-marked function
3063 if (FD->isNoReturn())
3064 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00003065 // Collect template parameters.
3066 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
3067 }
3068}
3069
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003070void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00003071 unsigned &LineNo, QualType &T,
3072 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003073 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003074 Unit = getOrCreateFile(VD->getLocation());
3075 LineNo = getLineNumber(VD->getLocation());
3076
3077 setLocation(VD->getLocation());
3078
3079 T = VD->getType();
3080 if (T->isIncompleteArrayType()) {
3081 // CodeGen turns int[] into int[1] so we'll do the same here.
3082 llvm::APInt ConstVal(32, 1);
3083 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3084
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003085 T = CGM.getContext().getConstantArrayType(ET, ConstVal, ArrayType::Normal,
3086 0);
Frederic Riss9db79f12014-11-18 03:40:46 +00003087 }
3088
3089 Name = VD->getName();
3090 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
3091 !isa<ObjCMethodDecl>(VD->getDeclContext()))
3092 LinkageName = CGM.getMangledName(VD);
3093 if (LinkageName == Name)
3094 LinkageName = StringRef();
3095
3096 // Since we emit declarations (DW_AT_members) for static members, place the
3097 // definition of those static members in the namespace they were declared in
3098 // in the source code (the lexical decl context).
3099 // FIXME: Generalize this for even non-member global variables where the
3100 // declaration and definition may have different lexical decl contexts, once
3101 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003102 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
3103 : VD->getDeclContext();
3104 // When a record type contains an in-line initialization of a static data
3105 // member, and the record type is marked as __declspec(dllexport), an implicit
3106 // definition of the member will be created in the record context. DWARF
3107 // doesn't seem to have a nice way to describe this in a form that consumers
3108 // are likely to understand, so fake the "normal" situation of a definition
3109 // outside the class by putting it in the global scope.
3110 if (DC->isRecord())
3111 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003112
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003113 llvm::DIScope *Mod = getParentModuleOrNull(VD);
3114 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00003115}
3116
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003117llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
3118 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003119 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00003120 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003121 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003122 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003123 llvm::DIFile *Unit = getOrCreateFile(Loc);
3124 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003125 unsigned Line = getLineNumber(Loc);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003126 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray,
3127 Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003128 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
3129
Frederic Rissd253ed62014-11-18 03:40:51 +00003130 // Build function type.
3131 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003132 if (FD)
3133 for (const ParmVarDecl *Parm : FD->parameters())
3134 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00003135 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
3136 QualType FnType = CGM.getContext().getFunctionType(
3137 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003138 if (Stub) {
3139 return DBuilder.createFunction(
3140 DContext, Name, LinkageName, Unit, Line,
3141 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3142 !FD->isExternallyVisible(),
3143 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
3144 TParamsArray.get(), getFunctionDeclaration(FD));
3145 }
3146
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003147 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003148 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003149 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3150 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003151 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003152 TParamsArray.get(), getFunctionDeclaration(FD));
George Burgess IV00f70bd2018-03-01 05:43:23 +00003153 const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003154 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3155 std::make_tuple(CanonDecl),
3156 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003157 return SP;
3158}
3159
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003160llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003161 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3162}
3163
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003164llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003165 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3166}
3167
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003168llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003169CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3170 QualType T;
3171 StringRef Name, LinkageName;
3172 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003173 llvm::DIFile *Unit = getOrCreateFile(Loc);
3174 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003175 unsigned Line = getLineNumber(Loc);
3176
3177 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003178 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003179 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3180 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003181 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003182 FwdDeclReplaceMap.emplace_back(
3183 std::piecewise_construct,
3184 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3185 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003186 return GV;
3187}
3188
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003189llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003190 // We only need a declaration (not a definition) of the type - so use whatever
3191 // we would otherwise do to get a type for a pointee. (forward declarations in
3192 // limited debug info, full definitions (if the type definition is available)
3193 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003194 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003195 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003196 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003197 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003198
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003199 if (I != DeclCache.end()) {
3200 auto N = I->second;
3201 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3202 return GVE->getVariable();
3203 return dyn_cast_or_null<llvm::DINode>(N);
3204 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003205
3206 // No definition for now. Emit a forward definition that might be
3207 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003208 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003209 return getFunctionForwardDeclaration(FD);
3210 else if (const auto *VD = dyn_cast<VarDecl>(D))
3211 return getGlobalVariableForwardDeclaration(VD);
3212
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003213 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003214}
3215
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003216llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003217 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003218 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003219
David Majnemer58ed0f32016-07-17 00:39:12 +00003220 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003221 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003222 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003223
3224 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003225 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003226
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003227 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003228 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003229 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003230 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003231 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003232 }
3233 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003235 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003236 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 return SP;
3238 }
3239
Aaron Ballman86c93902014-03-06 23:45:36 +00003240 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003241 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003242 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003243 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003244 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 return SP;
3246 }
3247 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003248 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003249}
3250
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003251// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003252// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003253llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003254 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003255 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003256 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003257 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3258 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003259 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003260
David Majnemer58ed0f32016-07-17 00:39:12 +00003261 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003262 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003263
3264 const auto *FTy = FnType->getAs<FunctionType>();
3265 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3266
David Majnemer58ed0f32016-07-17 00:39:12 +00003267 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003268 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003269 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003270
3271 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003272 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003273
3274 // Replace the instancetype keyword with the actual type.
3275 if (ResultTy == CGM.getContext().getObjCInstanceType())
3276 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003277 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003278
Adrian Prantl7bec9032013-05-10 21:08:31 +00003279 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003281 QualType SelfDeclTy;
3282 if (auto *SelfDecl = OMethod->getSelfDecl())
3283 SelfDeclTy = SelfDecl->getType();
3284 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3285 if (FPT->getNumParams() > 1)
3286 SelfDeclTy = FPT->getParamType(0);
3287 if (!SelfDeclTy.isNull())
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003288 Elts.push_back(
3289 CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003290 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003291 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003292 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003293 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003294 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003295 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003296 // Variadic methods need a special marker at the end of the type list.
3297 if (OMethod->isVariadic())
3298 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003300 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003301 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3302 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003304
Adrian Prantl800faef2014-02-25 23:42:18 +00003305 // Handle variadic function types; they need an additional
3306 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003307 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003308 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003309 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003310 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003311 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3312 for (QualType ParamType : FPT->param_types())
3313 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003314 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003315 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003316 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3317 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003318 }
3319
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003320 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003321}
3322
Eric Christophere7b87e52014-10-26 23:40:33 +00003323void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3324 SourceLocation ScopeLoc, QualType FnType,
Brock Wyma94ece8f2018-04-16 16:53:57 +00003325 llvm::Function *Fn, bool CurFuncIsThunk,
3326 CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003327
3328 StringRef Name;
3329 StringRef LinkageName;
3330
3331 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3332
3333 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003334 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003335
Leny Kholodov80c047d2016-09-06 10:48:04 +00003336 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003337 llvm::DIFile *Unit = getOrCreateFile(Loc);
3338 llvm::DIScope *FDContext = Unit;
3339 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 if (!HasDecl) {
3341 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003342 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003343 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003344 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003345 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003346 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003347 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003348 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003349 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003350 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003351 return;
3352 }
3353 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003354 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3355 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003356 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003357 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003358 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003359 } else {
3360 // Use llvm function name.
3361 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003362 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003364 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003365 Name = Name.substr(1);
3366
Erich Keane293a0552018-02-14 00:14:07 +00003367 if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003368 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003369 // Artificial functions should not silently reuse CurLoc.
3370 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003371 }
Brock Wyma94ece8f2018-04-16 16:53:57 +00003372
3373 if (CurFuncIsThunk)
3374 Flags |= llvm::DINode::FlagThunk;
3375
Adrian Prantl42d71b92014-04-10 23:21:53 +00003376 unsigned LineNo = getLineNumber(Loc);
3377 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003378
Eric Christopher8018e412014-03-27 18:50:35 +00003379 // FIXME: The function declaration we're constructing here is mostly reusing
3380 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3381 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3382 // all subprograms instead of the actual context since subprogram definitions
3383 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003384 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003385 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003386 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003387 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003388 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003389 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003390 // We might get here with a VarDecl in the case we're generating
3391 // code for the initialization of globals. Do not record these decls
3392 // as they will overwrite the actual VarDecl Decl in the cache.
3393 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003394 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003395
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00003396 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
3397 // Starting with DWARF V5 method declarations are emitted as children of
3398 // the interface type.
3399 if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
3400 const ObjCInterfaceDecl *ID = OMD->getClassInterface();
3401 QualType QTy(ID->getTypeForDecl(), 0);
3402 auto It = TypeCache.find(QTy.getAsOpaquePtr());
3403 if (It != TypeCache.end()) {
3404 llvm::DICompositeType *InterfaceDecl =
3405 cast<llvm::DICompositeType>(It->second);
3406 llvm::DISubprogram *FD = DBuilder.createFunction(
3407 InterfaceDecl, Name, LinkageName, Unit, LineNo,
3408 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
3409 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3410 TParamsArray.get());
3411 DBuilder.finalizeSubprogram(FD);
3412 ObjCMethodCache[ID].push_back(FD);
3413 }
3414 }
3415 }
3416
Adrian Prantlbebb8932014-03-21 21:01:58 +00003417 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003418 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003419
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003421 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003422}
3423
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003424void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3425 QualType FnType) {
3426 StringRef Name;
3427 StringRef LinkageName;
3428
3429 const Decl *D = GD.getDecl();
3430 if (!D)
3431 return;
3432
Leny Kholodov80c047d2016-09-06 10:48:04 +00003433 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003434 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003435 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003436 llvm::DINodeArray TParamsArray;
3437 if (isa<FunctionDecl>(D)) {
3438 // If there is a DISubprogram for this function available then use it.
3439 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3440 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003441 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003442 Name = getObjCMethodName(OMD);
3443 Flags |= llvm::DINode::FlagPrototyped;
3444 } else {
3445 llvm_unreachable("not a function or ObjC method");
3446 }
3447 if (!Name.empty() && Name[0] == '\01')
3448 Name = Name.substr(1);
3449
3450 if (D->isImplicit()) {
3451 Flags |= llvm::DINode::FlagArtificial;
3452 // Artificial functions without a location should not silently reuse CurLoc.
3453 if (Loc.isInvalid())
3454 CurLoc = SourceLocation();
3455 }
3456 unsigned LineNo = getLineNumber(Loc);
3457 unsigned ScopeLine = 0;
3458
Adrian Prantle76bda52016-04-15 15:55:45 +00003459 DBuilder.retainType(DBuilder.createFunction(
3460 FDContext, Name, LinkageName, Unit, LineNo,
3461 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3462 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3463 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003464}
3465
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003466void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3467 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3468 // If there is a subprogram for this function available then use it.
3469 auto FI = SPCache.find(FD->getCanonicalDecl());
3470 llvm::DISubprogram *SP = nullptr;
3471 if (FI != SPCache.end())
3472 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003473 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003474 SP = getFunctionStub(GD);
3475 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3476 LexicalBlockStack.emplace_back(SP);
3477 setInlinedAt(Builder.getCurrentDebugLocation());
3478 EmitLocation(Builder, FD->getLocation());
3479}
3480
3481void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3482 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003483 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003484 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3485}
3486
David Blaikie835afb22015-01-21 23:08:17 +00003487void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 // Update our current location
3489 setLocation(Loc);
3490
Eric Christophere7b87e52014-10-26 23:40:33 +00003491 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3492 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003493
Adrian Prantle83b1302014-01-07 22:05:52 +00003494 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003495 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003496 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003497}
3498
Guy Benyei11169dd2012-12-18 14:30:41 +00003499void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003500 llvm::MDNode *Back = nullptr;
3501 if (!LexicalBlockStack.empty())
3502 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003503 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003504 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003505 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003506}
3507
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003508void CGDebugInfo::AppendAddressSpaceXDeref(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003509 unsigned AddressSpace, SmallVectorImpl<int64_t> &Expr) const {
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003510 Optional<unsigned> DWARFAddressSpace =
3511 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3512 if (!DWARFAddressSpace)
3513 return;
3514
3515 Expr.push_back(llvm::dwarf::DW_OP_constu);
3516 Expr.push_back(DWARFAddressSpace.getValue());
3517 Expr.push_back(llvm::dwarf::DW_OP_swap);
3518 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3519}
3520
Eric Christopher0fdcb312013-05-16 00:52:20 +00003521void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3522 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003523 // Set our current location.
3524 setLocation(Loc);
3525
Guy Benyei11169dd2012-12-18 14:30:41 +00003526 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003527 Builder.SetCurrentDebugLocation(
3528 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3529 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003530
Benjamin Kramer8c305922016-02-02 11:06:51 +00003531 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003532 return;
3533
3534 // Create a new lexical block and push it on the stack.
3535 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003536}
3537
Eric Christopher0fdcb312013-05-16 00:52:20 +00003538void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3539 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003540 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3541
3542 // Provide an entry in the line table for the end of the block.
3543 EmitLocation(Builder, Loc);
3544
Benjamin Kramer8c305922016-02-02 11:06:51 +00003545 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003546 return;
3547
Guy Benyei11169dd2012-12-18 14:30:41 +00003548 LexicalBlockStack.pop_back();
3549}
3550
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003551void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003552 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3553 unsigned RCount = FnBeginRegionCount.back();
3554 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3555
3556 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003557 while (LexicalBlockStack.size() != RCount) {
3558 // Provide an entry in the line table for the end of the block.
3559 EmitLocation(Builder, CurLoc);
3560 LexicalBlockStack.pop_back();
3561 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003563
3564 if (Fn && Fn->getSubprogram())
3565 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003566}
3567
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003568llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003569 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003570
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003571 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003572 QualType FType;
3573 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003574 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003576 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003577 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003578
3579 FieldOffset = 0;
3580 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3581 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3582 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3583 FType = CGM.getContext().IntTy;
3584 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3585 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3586
3587 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3588 if (HasCopyAndDispose) {
3589 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003590 EltTys.push_back(
3591 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3592 EltTys.push_back(
3593 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003594 }
3595 bool HasByrefExtendedLayout;
3596 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003597 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3598 HasByrefExtendedLayout) &&
3599 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003600 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003601 EltTys.push_back(
3602 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003603 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003604
Guy Benyei11169dd2012-12-18 14:30:41 +00003605 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3606 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003607 CGM.getTarget().getPointerAlign(0))) {
3608 CharUnits FieldOffsetInBytes =
3609 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003610 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003611 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003612
Guy Benyei11169dd2012-12-18 14:30:41 +00003613 if (NumPaddingBytes.isPositive()) {
3614 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3615 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3616 pad, ArrayType::Normal, 0);
3617 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3618 }
3619 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003620
Guy Benyei11169dd2012-12-18 14:30:41 +00003621 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003622 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 FieldSize = CGM.getContext().getTypeSize(FType);
3624 FieldAlign = CGM.getContext().toBits(Align);
3625
Eric Christopherb2a008c2013-05-16 00:45:12 +00003626 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003627 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003628 FieldAlign, FieldOffset,
3629 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003630 EltTys.push_back(FieldTy);
3631 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003632
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003633 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003634
Leny Kholodov80c047d2016-09-06 10:48:04 +00003635 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003636
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003638 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003639}
3640
Sander de Smalen891af03a2018-02-03 13:55:59 +00003641llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
3642 llvm::Value *Storage,
3643 llvm::Optional<unsigned> ArgNo,
3644 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003645 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003647 if (VD->hasAttr<NoDebugAttr>())
Sander de Smalen891af03a2018-02-03 13:55:59 +00003648 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003649
David Blaikie7fceebf2013-08-19 03:37:48 +00003650 bool Unwritten =
3651 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3652 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003653 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003654 if (!Unwritten)
3655 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003656 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003657 uint64_t XOffset = 0;
3658 if (VD->hasAttr<BlocksAttr>())
3659 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003660 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003661 Ty = getOrCreateType(VD->getType(), Unit);
3662
3663 // If there is no debug info for this type then do not emit debug info
3664 // for this variable.
3665 if (!Ty)
Sander de Smalen891af03a2018-02-03 13:55:59 +00003666 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003667
Guy Benyei11169dd2012-12-18 14:30:41 +00003668 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003669 unsigned Line = 0;
3670 unsigned Column = 0;
3671 if (!Unwritten) {
3672 Line = getLineNumber(VD->getLocation());
3673 Column = getColumnNumber(VD->getLocation());
3674 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003675 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003676 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003677 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003678 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003679
3680 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3681
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003682 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3683 AppendAddressSpaceXDeref(AddressSpace, Expr);
3684
Alexey Bataev24f710182017-06-09 13:55:08 +00003685 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3686 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003687 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3688 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3689 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3690 Flags |= llvm::DINode::FlagObjectPointer;
3691 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003692
Adrian Prantlc3782a12017-04-18 01:22:01 +00003693 // Note: Older versions of clang used to emit byval references with an extra
3694 // DW_OP_deref, because they referenced the IR arg directly instead of
3695 // referencing an alloca. Newer versions of LLVM don't treat allocas
3696 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003697 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003698 StringRef Name = VD->getName();
3699 if (!Name.empty()) {
3700 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003701 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003703 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003704 // offset of __forwarding field
3705 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003706 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003707 Expr.push_back(offset.getQuantity());
3708 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003709 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003710 // offset of x field
3711 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003712 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003713 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003714 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003715 // If VD is an anonymous union then Storage represents value for
3716 // all union fields.
George Burgess IV00f70bd2018-03-01 05:43:23 +00003717 const RecordDecl *RD = RT->getDecl();
Adrian Prantl0d820892015-04-29 15:05:50 +00003718 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003719 // GDB has trouble finding local variables in anonymous unions, so we emit
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003720 // artificial local variables for each of the members.
Adrian Prantl00820ab2015-04-29 16:52:31 +00003721 //
3722 // FIXME: Remove this code as soon as GDB supports this.
3723 // The debug info verifier in LLVM operates based on the assumption that a
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003724 // variable has the same size as its storage and we had to disable the
3725 // check for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003726 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003727 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003728 StringRef FieldName = Field->getName();
3729
3730 // Ignore unnamed fields. Do not ignore unnamed records.
3731 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3732 continue;
3733
3734 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003735 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003736 auto *D = DBuilder.createAutoVariable(
3737 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003738 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003739
3740 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003741 DBuilder.insertDeclare(
3742 Storage, D, DBuilder.createExpression(Expr),
3743 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3744 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003745 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003746 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003747 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003748
3749 // Create the descriptor for the variable.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003750 auto *D = ArgNo ? DBuilder.createParameterVariable(
3751 Scope, Name, *ArgNo, Unit, Line, Ty,
3752 CGM.getLangOpts().Optimize, Flags)
3753 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3754 CGM.getLangOpts().Optimize,
3755 Flags, Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003756
3757 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003758 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003759 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003760 Builder.GetInsertBlock());
Sander de Smalen891af03a2018-02-03 13:55:59 +00003761
3762 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00003763}
3764
Sander de Smalen891af03a2018-02-03 13:55:59 +00003765llvm::DILocalVariable *
3766CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage,
3767 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003768 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Sander de Smalen891af03a2018-02-03 13:55:59 +00003769 return EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003770}
3771
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003772llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3773 llvm::DIType *Ty) {
3774 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003775 if (CachedTy)
3776 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003777 return DBuilder.createObjectPointerType(Ty);
3778}
3779
Eric Christophere7b87e52014-10-26 23:40:33 +00003780void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3781 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003782 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003783 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003784 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003785
Craig Topper8a13c412014-05-21 05:09:00 +00003786 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003787 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003788 if (VD->hasAttr<NoDebugAttr>())
3789 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003790
Guy Benyei11169dd2012-12-18 14:30:41 +00003791 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003792
Guy Benyei11169dd2012-12-18 14:30:41 +00003793 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003794 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3795 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003796 if (isByRef)
3797 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003798 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003799 Ty = getOrCreateType(VD->getType(), Unit);
3800
3801 // Self is passed along as an implicit non-arg variable in a
3802 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003803 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3804 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3805 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003806
3807 // Get location information.
3808 unsigned Line = getLineNumber(VD->getLocation());
3809 unsigned Column = getColumnNumber(VD->getLocation());
3810
3811 const llvm::DataLayout &target = CGM.getDataLayout();
3812
3813 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003814 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003815 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3816
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003817 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003818 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003819 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003820 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003821 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003822 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003823 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003824 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003825 offset =
3826 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003827 addr.push_back(offset.getQuantity());
3828 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003829 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003830 // offset of x field
3831 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003832 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003833 }
3834
3835 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003836 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003837 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003838 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003839 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003840
Guy Benyei11169dd2012-12-18 14:30:41 +00003841 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003842 auto DL =
3843 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003844 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003845 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003846 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003847 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003848 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003849}
3850
Guy Benyei11169dd2012-12-18 14:30:41 +00003851void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3852 unsigned ArgNo,
3853 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003854 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003855 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003856}
3857
3858namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003859struct BlockLayoutChunk {
3860 uint64_t OffsetInBits;
3861 const BlockDecl::Capture *Capture;
3862};
3863bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3864 return l.OffsetInBits < r.OffsetInBits;
3865}
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003866} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00003867
Scott Linder58df0e42018-08-08 15:56:12 +00003868void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
3869 const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
3870 const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
3871 SmallVectorImpl<llvm::Metadata *> &Fields) {
3872 // Blocks in OpenCL have unique constraints which make the standard fields
3873 // redundant while requiring size and align fields for enqueue_kernel. See
3874 // initializeForBlockHeader in CGBlocks.cpp
3875 if (CGM.getLangOpts().OpenCL) {
3876 Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public,
3877 BlockLayout.getElementOffsetInBits(0),
3878 Unit, Unit));
3879 Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
3880 BlockLayout.getElementOffsetInBits(1),
3881 Unit, Unit));
3882 } else {
3883 Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
3884 BlockLayout.getElementOffsetInBits(0),
3885 Unit, Unit));
3886 Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
3887 BlockLayout.getElementOffsetInBits(1),
3888 Unit, Unit));
3889 Fields.push_back(
3890 createFieldType("__reserved", Context.IntTy, Loc, AS_public,
3891 BlockLayout.getElementOffsetInBits(2), Unit, Unit));
3892 auto *FnTy = Block.getBlockExpr()->getFunctionType();
3893 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3894 Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
3895 BlockLayout.getElementOffsetInBits(3),
3896 Unit, Unit));
3897 Fields.push_back(createFieldType(
3898 "__descriptor",
3899 Context.getPointerType(Block.NeedsCopyDispose
3900 ? Context.getBlockDescriptorExtendedType()
3901 : Context.getBlockDescriptorType()),
3902 Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
3903 }
3904}
3905
Guy Benyei11169dd2012-12-18 14:30:41 +00003906void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl356347b2017-10-26 20:08:52 +00003907 StringRef Name,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003908 unsigned ArgNo,
Adrian Prantl356347b2017-10-26 20:08:52 +00003909 llvm::AllocaInst *Alloca,
Guy Benyei11169dd2012-12-18 14:30:41 +00003910 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003911 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003912 ASTContext &C = CGM.getContext();
3913 const BlockDecl *blockDecl = block.getBlockDecl();
3914
3915 // Collect some general information about the block's location.
3916 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003917 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003918 unsigned line = getLineNumber(loc);
3919 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003920
Guy Benyei11169dd2012-12-18 14:30:41 +00003921 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003922 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003923
3924 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003925 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003926
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003927 SmallVector<llvm::Metadata *, 16> fields;
Scott Linder58df0e42018-08-08 15:56:12 +00003928 collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
3929 fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003930
3931 // We want to sort the captures by offset, not because DWARF
3932 // requires this, but because we're paranoid about debuggers.
3933 SmallVector<BlockLayoutChunk, 8> chunks;
3934
3935 // 'this' capture.
3936 if (blockDecl->capturesCXXThis()) {
3937 BlockLayoutChunk chunk;
3938 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003939 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003940 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003941 chunks.push_back(chunk);
3942 }
3943
3944 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003945 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003946 const VarDecl *variable = capture.getVariable();
3947 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3948
3949 // Ignore constant captures.
3950 if (captureInfo.isConstant())
3951 continue;
3952
3953 BlockLayoutChunk chunk;
3954 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003955 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003956 chunk.Capture = &capture;
3957 chunks.push_back(chunk);
3958 }
3959
3960 // Sort by offset.
3961 llvm::array_pod_sort(chunks.begin(), chunks.end());
3962
David Majnemer58ed0f32016-07-17 00:39:12 +00003963 for (const BlockLayoutChunk &Chunk : chunks) {
3964 uint64_t offsetInBits = Chunk.OffsetInBits;
3965 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003966
3967 // If we have a null capture, this must be the C++ 'this' capture.
3968 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003969 QualType type;
3970 if (auto *Method =
3971 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3972 type = Method->getThisType(C);
3973 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3974 type = QualType(RDecl->getTypeForDecl(), 0);
3975 else
3976 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003977
David Majnemerb4b671e2016-06-30 03:01:59 +00003978 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003979 offsetInBits, tunit, tunit));
3980 continue;
3981 }
3982
3983 const VarDecl *variable = capture->getVariable();
3984 StringRef name = variable->getName();
3985
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003986 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003987 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003988 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003989 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003990
3991 // FIXME: this creates a second copy of this type!
3992 uint64_t xoffset;
3993 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003994 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003995 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3996 PtrInfo.Width, Align, offsetInBits,
3997 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003998 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003999 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00004000 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00004001 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00004002 }
4003 fields.push_back(fieldType);
4004 }
4005
4006 SmallString<36> typeName;
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004007 llvm::raw_svector_ostream(typeName)
4008 << "__block_literal_" << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00004009
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004010 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00004011
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004012 llvm::DIType *type =
4013 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00004014 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004015 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00004016 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
4017
4018 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00004019 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004020 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00004021
4022 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00004023 auto *debugVar = DBuilder.createParameterVariable(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004024 scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00004025
Adrian Prantl616bef42013-03-14 21:52:59 +00004026 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl356347b2017-10-26 20:08:52 +00004027 DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00004028 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00004029 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00004030}
4031
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004032llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00004033CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
4034 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00004035 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00004036
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004037 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00004038 if (MI != StaticDataMemberCache.end()) {
4039 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00004040 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00004041 }
David Blaikiece763042013-08-20 21:49:21 +00004042
4043 // If the member wasn't found in the cache, lazily construct and add it to the
4044 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00004045 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004046 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00004047 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00004048}
4049
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004050llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004051 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
4052 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004053 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004054
4055 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004056 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00004057 StringRef FieldName = Field->getName();
4058
4059 // Ignore unnamed fields, but recurse into anonymous records.
4060 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00004061 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004062 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004063 Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004064 continue;
4065 }
4066 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004067 GVE = DBuilder.createGlobalVariableExpression(
4068 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
4069 Var->hasLocalLinkage());
4070 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004071 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004072 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00004073}
4074
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004075void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00004076 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004077 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004078 if (D->hasAttr<NoDebugAttr>())
4079 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004080
4081 // If we already created a DIGlobalVariable for this declaration, just attach
4082 // it to the llvm::GlobalVariable.
4083 auto Cached = DeclCache.find(D->getCanonicalDecl());
4084 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004085 return Var->addDebugInfo(
4086 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004087
Guy Benyei11169dd2012-12-18 14:30:41 +00004088 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004089 llvm::DIFile *Unit = nullptr;
4090 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00004091 unsigned LineNo;
4092 StringRef DeclName, LinkageName;
4093 QualType T;
4094 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004095
4096 // Attempt to store one global variable for the declaration - even if we
4097 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004098 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004099
4100 // If this is an anonymous union then we'll want to emit a global
4101 // variable for each member of the anonymous union so that it's possible
4102 // to find the name of any field in the union.
4103 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00004104 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00004105 assert(RD->isAnonymousStructOrUnion() &&
4106 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004107 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004108 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00004109 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004110
4111 SmallVector<int64_t, 4> Expr;
4112 unsigned AddressSpace =
4113 CGM.getContext().getTargetAddressSpace(D->getType());
4114 AppendAddressSpaceXDeref(AddressSpace, Expr);
4115
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004116 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00004117 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004118 Var->hasLocalLinkage(),
4119 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00004120 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004121 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004122 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004123 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004124}
4125
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004126void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004127 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004128 if (VD->hasAttr<NoDebugAttr>())
4129 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00004130 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004131 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004132 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004133 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004134 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00004135 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
4136 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004137 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
4138 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
4139 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004140 // Do not use global variables for enums.
4141 //
4142 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004143 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004144 return;
David Blaikiea15565562014-04-04 20:56:17 +00004145 // Do not emit separate definitions for function local const/statics.
4146 if (isa<FunctionDecl>(VD->getDeclContext()))
4147 return;
David Blaikiebb113912014-04-05 07:23:17 +00004148 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00004149 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004150 if (VarD->isStaticDataMember()) {
4151 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004152 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00004153 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00004154 //
4155 // FIXME: This is probably unnecessary, since Ty should reference RD
4156 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00004157 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00004158 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00004159 return;
4160 }
4161
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004162 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004164 auto &GV = DeclCache[VD];
4165 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00004166 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004167 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00004168 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
4169 // FIXME: Add a representation for integer constants wider than 64 bits.
4170 if (Init.isInt())
4171 InitExpr =
4172 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
4173 else if (Init.isFloat())
4174 InitExpr = DBuilder.createConstantValueExpression(
4175 Init.getFloat().bitcastToAPInt().getZExtValue());
4176 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004177 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00004178 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00004179 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
4180 Align));
David Blaikiebd483762013-05-20 04:58:53 +00004181}
4182
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004183llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00004184 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00004185 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00004186 llvm::DIScope *Mod = getParentModuleOrNull(D);
4187 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00004188}
4189
David Blaikie9f88fe82013-04-22 06:13:21 +00004190void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004191 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004192 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004193 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00004194 if (!NSDecl->isAnonymousNamespace() ||
4195 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004196 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004197 DBuilder.createImportedModule(
4198 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004199 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004200 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004201}
4202
David Blaikiebd483762013-05-20 04:58:53 +00004203void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004204 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004205 return;
4206 assert(UD.shadow_size() &&
4207 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4208 // Emitting one decl is sufficient - debuggers can detect that this is an
4209 // overloaded name & provide lookup for all the overloads.
4210 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004211
4212 // FIXME: Skip functions with undeduced auto return type for now since we
4213 // don't currently have the plumbing for separate declarations & definitions
4214 // of free functions and mismatched types (auto in the declaration, concrete
4215 // return type in the definition)
4216 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4217 if (const auto *AT =
4218 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4219 if (AT->getDeducedType().isNull())
4220 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004221 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004222 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4223 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004224 DBuilder.createImportedDeclaration(
4225 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004226 getOrCreateFile(Loc), getLineNumber(Loc));
4227 }
David Blaikiebd483762013-05-20 04:58:53 +00004228}
4229
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004230void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004231 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4232 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004233 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004234 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004235 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004236 DBuilder.createImportedDeclaration(
4237 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004238 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4239 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004240 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004241}
4242
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004243llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004244CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004245 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004246 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004247 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004248 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004249 return cast<llvm::DIImportedEntity>(VH);
4250 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004251 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004252 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004253 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4254 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004255 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004256 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004257 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4258 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004259 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004260 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004261 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004262 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004263 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004264 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004265 return R;
4266}
4267
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004268llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004269CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4270 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4271 // if necessary, and this way multiple declarations of the same namespace in
4272 // different parent modules stay distinct.
4273 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004274 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004275 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004276
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004277 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004278 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004279 llvm::DINamespace *NS =
4280 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4281 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004282 return NS;
4283}
4284
Adrian Prantla5206ce2015-09-22 23:26:43 +00004285void CGDebugInfo::setDwoId(uint64_t Signature) {
4286 assert(TheCU && "no main compile unit");
4287 TheCU->setDWOId(Signature);
4288}
4289
Guy Benyei11169dd2012-12-18 14:30:41 +00004290void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004291 // Creating types might create further types - invalidating the current
4292 // element and the size(), so don't cache/reference them.
4293 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4294 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004295 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004296 ? CreateTypeDefinition(E.Type, E.Unit)
4297 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004298 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004299 }
4300
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004301 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
4302 // Add methods to interface.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004303 for (const auto &P : ObjCMethodCache) {
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004304 if (P.second.empty())
4305 continue;
4306
4307 QualType QTy(P.first->getTypeForDecl(), 0);
4308 auto It = TypeCache.find(QTy.getAsOpaquePtr());
4309 assert(It != TypeCache.end());
4310
4311 llvm::DICompositeType *InterfaceDecl =
4312 cast<llvm::DICompositeType>(It->second);
4313
4314 SmallVector<llvm::Metadata *, 16> EltTys;
4315 auto CurrenetElts = InterfaceDecl->getElements();
4316 EltTys.append(CurrenetElts.begin(), CurrenetElts.end());
4317 for (auto &MD : P.second)
4318 EltTys.push_back(MD);
4319 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
4320 DBuilder.replaceArrays(InterfaceDecl, Elements);
4321 }
4322 }
4323
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004324 for (const auto &P : ReplaceMap) {
4325 assert(P.second);
4326 auto *Ty = cast<llvm::DIType>(P.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004327 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004328
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004329 auto It = TypeCache.find(P.first);
4330 assert(It != TypeCache.end());
4331 assert(It->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004332
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004333 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004334 cast<llvm::DIType>(It->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004335 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004336
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004337 for (const auto &P : FwdDeclReplaceMap) {
4338 assert(P.second);
4339 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004340 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004341
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004342 auto It = DeclCache.find(P.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004343 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004344 // with ourselves, that will destroy the temporary MDNode and
4345 // replace it with a standard one, avoiding leaking memory.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004346 if (It == DeclCache.end())
4347 Repl = P.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004348 else
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004349 Repl = It->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004350
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004351 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4352 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004353 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004354 }
4355
Adrian Prantl73409ce2013-03-11 18:33:46 +00004356 // We keep our own list of retained types, because we need to look
4357 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004358 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004359 if (auto MD = TypeCache[RT])
4360 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004361
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 DBuilder.finalize();
4363}
David Blaikie66088d52014-09-24 17:01:27 +00004364
4365void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004366 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004367 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004368
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004369 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004370 // Don't ignore in case of explicit cast where it is referenced indirectly.
4371 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004372}
Amara Emerson652795d2016-11-10 14:44:30 +00004373
4374llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4375 if (LexicalBlockStack.empty())
4376 return llvm::DebugLoc();
4377
4378 llvm::MDNode *Scope = LexicalBlockStack.back();
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004379 return llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc), Scope);
Amara Emerson652795d2016-11-10 14:44:30 +00004380}