blob: 9e99a646b0bff2ca89580f77b923e8dcef20c23b [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
John McCallde0fe072017-08-15 21:42:52 +000021#include "ConstantEmitter.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/RecordLayout.h"
28#include "clang/Basic/FileManager.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000031#include "clang/Frontend/CodeGenOptions.h"
Taewook Oh0fb5b782017-08-16 19:36:24 +000032#include "clang/Frontend/FrontendOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000034#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000035#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000036#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000037#include "llvm/ADT/SmallVector.h"
38#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000039#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000046#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000047#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000048using namespace clang;
49using namespace clang::CodeGen;
50
Victor Leschuka7ece032016-10-20 00:13:19 +000051static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
52 auto TI = Ctx.getTypeInfo(Ty);
53 return TI.AlignIsRequired ? TI.Align : 0;
54}
55
56static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
57 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
58}
59
60static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
61 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
62}
63
Guy Benyei11169dd2012-12-18 14:30:41 +000064CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000066 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000067 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000068 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
69 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000070 CreateCompileUnit();
71}
72
73CGDebugInfo::~CGDebugInfo() {
74 assert(LexicalBlockStack.empty() &&
75 "Region stack mismatch, stack not empty!");
76}
77
David Blaikie66e41972015-01-14 07:38:27 +000078ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000079 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000080 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000081 init(TemporaryLocation);
82}
83
Adrian Prantl39428e72015-02-03 18:40:42 +000084ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000085 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000086 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000087 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000088 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000089}
90
91void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000092 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000093 auto *DI = CGF->getDebugInfo();
94 if (!DI) {
95 CGF = nullptr;
96 return;
David Blaikie66e41972015-01-14 07:38:27 +000097 }
David Blaikied7057d92015-08-12 23:49:57 +000098
99 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
Bob Haarmanc6c9b8f2017-09-11 22:11:57 +0000100
101 if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
102 return;
103
David Blaikied7057d92015-08-12 23:49:57 +0000104 if (TemporaryLocation.isValid()) {
105 DI->EmitLocation(CGF->Builder, TemporaryLocation);
106 return;
107 }
108
109 if (DefaultToEmpty) {
110 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
111 return;
112 }
113
114 // Construct a location that has a valid scope, but no line info.
115 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000116 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
117 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000118}
119
David Blaikie9b479662015-01-25 01:19:10 +0000120ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000121 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000122 init(E->getExprLoc());
123}
124
David Blaikie66e41972015-01-14 07:38:27 +0000125ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000126 : CGF(&CGF) {
127 if (!CGF.getDebugInfo()) {
128 this->CGF = nullptr;
129 return;
David Blaikie66e41972015-01-14 07:38:27 +0000130 }
David Blaikied7057d92015-08-12 23:49:57 +0000131 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
132 if (Loc)
133 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000134}
135
136ApplyDebugLocation::~ApplyDebugLocation() {
137 // Query CGF so the location isn't overwritten when location updates are
138 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000139 if (CGF)
140 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000141}
142
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000143ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
144 GlobalDecl InlinedFn)
145 : CGF(&CGF) {
146 if (!CGF.getDebugInfo()) {
147 this->CGF = nullptr;
148 return;
149 }
150 auto &DI = *CGF.getDebugInfo();
151 SavedLocation = DI.getLocation();
152 assert((DI.getInlinedAt() ==
153 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
154 "CGDebugInfo and IRBuilder are out of sync");
155
156 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
157}
158
159ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
160 if (!CGF)
161 return;
162 auto &DI = *CGF->getDebugInfo();
163 DI.EmitInlineFunctionEnd(CGF->Builder);
164 DI.EmitLocation(CGF->Builder, SavedLocation);
165}
166
Guy Benyei11169dd2012-12-18 14:30:41 +0000167void CGDebugInfo::setLocation(SourceLocation Loc) {
168 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000169 if (Loc.isInvalid())
170 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000171
172 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
173
174 // If we've changed files in the middle of a lexical scope go ahead
175 // and create a new lexical scope with file node if it's different
176 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 if (LexicalBlockStack.empty())
178 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179
180 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000181 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000182 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000184 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000185 return;
186
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000187 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000188 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000189 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
190 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000191 } else if (isa<llvm::DILexicalBlock>(Scope) ||
192 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000193 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000194 LexicalBlockStack.emplace_back(
195 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000196 }
197}
198
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000199llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000200 llvm::DIScope *Mod = getParentModuleOrNull(D);
201 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
202 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000203}
204
205llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
206 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000207 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000208 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000209
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000210 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000211 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000212 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000213 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000214 }
215
216 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000217 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
218 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000219
David Majnemer58ed0f32016-07-17 00:39:12 +0000220 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000221 if (!RDecl->isDependentType())
222 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000223 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000224 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000225}
226
Reid Kleckner59d12202017-08-08 01:33:53 +0000227PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
228 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
229
230 // If we're emitting codeview, it's important to try to match MSVC's naming so
231 // that visualizers written for MSVC will trigger for our class names. In
232 // particular, we can't have spaces between arguments of standard templates
233 // like basic_string and vector.
234 if (CGM.getCodeGenOpts().EmitCodeView)
235 PP.MSVCFormatting = true;
236
237 return PP;
238}
239
Guy Benyei11169dd2012-12-18 14:30:41 +0000240StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000241 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000242 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000243 FunctionTemplateSpecializationInfo *Info =
244 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000245
Reid Kleckner31abd802016-06-30 17:41:31 +0000246 // Emit the unqualified name in normal operation. LLVM and the debugger can
247 // compute the fully qualified name from the scope chain. If we're only
248 // emitting line table info, there won't be any scope chains, so emit the
249 // fully qualified name here so that stack traces are more accurate.
250 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
251 // evaluating the size impact.
252 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
253 CGM.getCodeGenOpts().EmitCodeView;
254
255 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000256 return FII->getName();
257
Benjamin Kramer9170e912013-02-22 15:46:01 +0000258 SmallString<128> NS;
259 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000260 if (!UseQualifiedName)
261 FD->printName(OS);
262 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000263 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000264
Reid Kleckner829398e2016-06-17 16:11:20 +0000265 // Add any template specialization args.
266 if (Info) {
267 const TemplateArgumentList *TArgs = Info->TemplateArguments;
Serge Pavlov03e672c2017-11-28 16:14:14 +0000268 printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269 }
270
271 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000272 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000273}
274
275StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
276 SmallString<256> MethodName;
277 llvm::raw_svector_ostream OS(MethodName);
278 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
279 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000280 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000281 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000282 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000283 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000284 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000285 if (OC->IsClassExtension()) {
286 OS << OC->getClassInterface()->getName();
287 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000288 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000289 << OC->getIdentifier()->getNameStart() << ')';
290 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000291 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000292 OS << OCD->getClassInterface()->getName() << '('
293 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000294 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000295 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000296 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000297 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000298 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000299 ClassTy.print(OS, PrintingPolicy(LangOptions()));
300 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000301 }
302 OS << ' ' << OMD->getSelector().getAsString() << ']';
303
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000304 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
Guy Benyei11169dd2012-12-18 14:30:41 +0000307StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000308 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000309}
310
Eric Christophere7b87e52014-10-26 23:40:33 +0000311StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000312 if (isa<ClassTemplateSpecializationDecl>(RD)) {
313 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000314 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000315 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000316 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000317
318 // Copy this name on the side and use its reference.
319 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000320 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000321
David Majnemerbc5976a2016-07-01 23:12:54 +0000322 // quick optimization to avoid having to intern strings that are already
323 // stored reliably elsewhere
324 if (const IdentifierInfo *II = RD->getIdentifier())
325 return II->getName();
326
327 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
328 // used to reconstruct the fully qualified type names.
329 if (CGM.getCodeGenOpts().EmitCodeView) {
330 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
331 assert(RD->getDeclContext() == D->getDeclContext() &&
332 "Typedef should not be in another decl context!");
333 assert(D->getDeclName().getAsIdentifierInfo() &&
334 "Typedef was not named!");
335 return D->getDeclName().getAsIdentifierInfo()->getName();
336 }
337
338 if (CGM.getLangOpts().CPlusPlus) {
339 StringRef Name;
340
341 ASTContext &Context = CGM.getContext();
342 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
343 // Anonymous types without a name for linkage purposes have their
344 // declarator mangled in if they have one.
345 Name = DD->getName();
346 else if (const TypedefNameDecl *TND =
347 Context.getTypedefNameForUnnamedTagDecl(RD))
348 // Anonymous types without a name for linkage purposes have their
349 // associate typedef mangled in if they have one.
350 Name = TND->getName();
351
352 if (!Name.empty()) {
353 SmallString<256> UnnamedType("<unnamed-type-");
354 UnnamedType += Name;
355 UnnamedType += '>';
356 return internString(UnnamedType);
357 }
358 }
359 }
360
361 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000362}
363
Scott Linder94834f12018-02-12 19:47:05 +0000364Optional<llvm::DIFile::ChecksumKind>
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000365CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
366 Checksum.clear();
367
Paul Robinson76178632018-05-25 22:35:59 +0000368 if (!CGM.getCodeGenOpts().EmitCodeView &&
369 CGM.getCodeGenOpts().DwarfVersion < 5)
Scott Linder94834f12018-02-12 19:47:05 +0000370 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000371
372 SourceManager &SM = CGM.getContext().getSourceManager();
373 bool Invalid;
Paul Robinson76178632018-05-25 22:35:59 +0000374 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
375 if (Invalid)
Scott Linder94834f12018-02-12 19:47:05 +0000376 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000377
378 llvm::MD5 Hash;
379 llvm::MD5::MD5Result Result;
380
381 Hash.update(MemBuffer->getBuffer());
382 Hash.final(Result);
383
384 Hash.stringifyResult(Result, Checksum);
385 return llvm::DIFile::CSK_MD5;
386}
387
Scott Lindera2fbcef2018-02-26 17:32:31 +0000388Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM, FileID FID) {
389 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();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000415 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000416
417 if (it != DIFileCache.end()) {
418 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +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
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000430 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000431 remapDIPath(getCurrentDirname()),
Scott Lindera2fbcef2018-02-26 17:32:31 +0000432 CSInfo,
433 getSource(SM, SM.getFileID(Loc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000434
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000435 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 return F;
437}
438
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000439llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000440 return DBuilder.createFile(
441 remapDIPath(TheCU->getFilename()),
442 remapDIPath(TheCU->getDirectory()),
443 TheCU->getFile()->getChecksum(),
444 CGM.getCodeGenOpts().EmbedSource ? TheCU->getSource() : None);
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000445}
446
447std::string CGDebugInfo::remapDIPath(StringRef Path) const {
448 for (const auto &Entry : DebugPrefixMap)
449 if (Path.startswith(Entry.first))
450 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
451 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000452}
453
Guy Benyei11169dd2012-12-18 14:30:41 +0000454unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
455 if (Loc.isInvalid() && CurLoc.isInvalid())
456 return 0;
457 SourceManager &SM = CGM.getContext().getSourceManager();
458 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000460}
461
Adrian Prantlc7822422013-03-12 20:43:25 +0000462unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000464 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 return 0;
466
467 // If the location is invalid then use the current column.
468 if (Loc.isInvalid() && CurLoc.isInvalid())
469 return 0;
470 SourceManager &SM = CGM.getContext().getSourceManager();
471 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473}
474
475StringRef CGDebugInfo::getCurrentDirname() {
476 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
477 return CGM.getCodeGenOpts().DebugCompilationDir;
478
479 if (!CWDName.empty())
480 return CWDName;
481 SmallString<256> CWD;
482 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000483 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000484}
485
Guy Benyei11169dd2012-12-18 14:30:41 +0000486void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000487 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000488 Optional<llvm::DIFile::ChecksumKind> CSKind;
489 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490
David Blaikieaabde052014-05-14 00:29:00 +0000491 // Should we be asking the SourceManager for the main file name, instead of
492 // accepting it as an argument? This just causes the main file name to
493 // mismatch with source locations and create extra lexical scopes or
494 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
495 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
496 // because that's what the SourceManager says)
497
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 // Get absolute path name.
499 SourceManager &SM = CGM.getContext().getSourceManager();
500 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
501 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000502 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000503
504 // The main file name provided via the "-main-file-name" option contains just
505 // the file name itself with no path information. This file name may have had
506 // a relative path, so we look into the actual file entry for the main
507 // file to determine the real absolute path for the file.
508 std::string MainFileDir;
509 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000510 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000511 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000512 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
513 llvm::sys::path::append(MainFileDirSS, MainFileName);
514 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000515 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000516 // If the main file name provided is identical to the input file name, and
517 // if the input file is a preprocessed source, use the module name for
518 // debug info. The module name comes from the name specified in the first
519 // linemarker if the input is a preprocessed source.
520 if (MainFile->getName() == MainFileName &&
521 FrontendOptions::getInputKindForExtension(
522 MainFile->getName().rsplit('.').second)
523 .isPreprocessed())
524 MainFileName = CGM.getModule().getName().str();
525
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000526 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 }
528
Ed Masteda706022014-05-07 12:49:30 +0000529 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000530 const LangOptions &LO = CGM.getLangOpts();
531 if (LO.CPlusPlus) {
532 if (LO.ObjC1)
533 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
534 else
535 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
536 } else if (LO.ObjC1) {
537 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000538 } else if (LO.RenderScript) {
539 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 } else if (LO.C99) {
541 LangTag = llvm::dwarf::DW_LANG_C99;
542 } else {
543 LangTag = llvm::dwarf::DW_LANG_C89;
544 }
545
546 std::string Producer = getClangFullVersion();
547
548 // Figure out which version of the ObjC runtime we have.
549 unsigned RuntimeVers = 0;
550 if (LO.ObjC1)
551 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
552
Adrian Prantl826824e2016-04-08 22:43:06 +0000553 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
554 switch (DebugKind) {
555 case codegenoptions::NoDebugInfo:
556 case codegenoptions::LocTrackingOnly:
557 EmissionKind = llvm::DICompileUnit::NoDebug;
558 break;
559 case codegenoptions::DebugLineTablesOnly:
560 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
561 break;
562 case codegenoptions::LimitedDebugInfo:
563 case codegenoptions::FullDebugInfo:
564 EmissionKind = llvm::DICompileUnit::FullDebug;
565 break;
566 }
567
Scott Linder94834f12018-02-12 19:47:05 +0000568 if (CSKind)
569 CSInfo.emplace(*CSKind, Checksum);
570
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000572 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000573 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000574 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000575 LangTag,
576 DBuilder.createFile(remapDIPath(MainFileName),
Scott Lindera2fbcef2018-02-26 17:32:31 +0000577 remapDIPath(getCurrentDirname()),
578 CSInfo,
579 getSource(SM, SM.getMainFileID())),
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +0000580 CGOpts.EmitVersionIdentMetadata ? Producer : "",
581 LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
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,
585 CGOpts.GnuPubnames);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586}
587
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000588llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000589 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000590 StringRef BTName;
591 switch (BT->getKind()) {
592#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000593#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000594#include "clang/AST/BuiltinTypes.def"
595 case BuiltinType::Dependent:
596 llvm_unreachable("Unexpected builtin type");
597 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000598 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000599 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000600 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000602 if (!ClassTy)
603 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
604 "objc_class", TheCU,
605 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000606 return ClassTy;
607 case BuiltinType::ObjCId: {
608 // typedef struct objc_class *Class;
609 // typedef struct objc_object {
610 // Class isa;
611 // } *id;
612
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000613 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000614 return ObjTy;
615
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000616 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
618 "objc_class", TheCU,
619 getOrCreateMainFile(), 0);
620
621 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000622
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000623 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000624
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000625 ObjTy = DBuilder.createStructType(
626 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
627 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000628
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000629 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000630 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
631 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
632 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 return ObjTy;
634 }
635 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000636 if (!SelTy)
637 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
638 "objc_selector", TheCU,
639 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 return SelTy;
641 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000642
Alexey Bader954ba212016-04-08 13:40:33 +0000643#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
644 case BuiltinType::Id: \
645 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
646 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000647#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000648 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000649 return getOrCreateStructPtrType("opencl_sampler_t",
650 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000651 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000652 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000653 case BuiltinType::OCLClkEvent:
654 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
655 case BuiltinType::OCLQueue:
656 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000657 case BuiltinType::OCLReserveID:
658 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000659
Guy Benyei11169dd2012-12-18 14:30:41 +0000660 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000661 case BuiltinType::Char_U:
662 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
663 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000664 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000665 case BuiltinType::SChar:
666 Encoding = llvm::dwarf::DW_ATE_signed_char;
667 break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000668 case BuiltinType::Char8:
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000670 case BuiltinType::Char32:
671 Encoding = llvm::dwarf::DW_ATE_UTF;
672 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 case BuiltinType::UShort:
674 case BuiltinType::UInt:
675 case BuiltinType::UInt128:
676 case BuiltinType::ULong:
677 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000678 case BuiltinType::ULongLong:
679 Encoding = llvm::dwarf::DW_ATE_unsigned;
680 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 case BuiltinType::Short:
682 case BuiltinType::Int:
683 case BuiltinType::Int128:
684 case BuiltinType::Long:
685 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000686 case BuiltinType::LongLong:
687 Encoding = llvm::dwarf::DW_ATE_signed;
688 break;
689 case BuiltinType::Bool:
690 Encoding = llvm::dwarf::DW_ATE_boolean;
691 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000692 case BuiltinType::Half:
693 case BuiltinType::Float:
694 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000695 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000696 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000697 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000698 // FIXME: For targets where long double and __float128 have the same size,
699 // they are currently indistinguishable in the debugger without some
700 // special treatment. However, there is currently no consensus on encoding
701 // and this should be updated once a DWARF encoding exists for distinct
702 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000703 Encoding = llvm::dwarf::DW_ATE_float;
704 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000705 }
706
707 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000708 case BuiltinType::Long:
709 BTName = "long int";
710 break;
711 case BuiltinType::LongLong:
712 BTName = "long long int";
713 break;
714 case BuiltinType::ULong:
715 BTName = "long unsigned int";
716 break;
717 case BuiltinType::ULongLong:
718 BTName = "long long unsigned int";
719 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000720 default:
721 BTName = BT->getName(CGM.getLangOpts());
722 break;
723 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000724 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000725 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000726 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000727}
728
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000729llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000730 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000731 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 if (Ty->isComplexIntegerType())
733 Encoding = llvm::dwarf::DW_ATE_lo_user;
734
735 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000736 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000737}
738
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000739llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
740 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 QualifierCollector Qc;
742 const Type *T = Qc.strip(Ty);
743
744 // Ignore these qualifiers for now.
745 Qc.removeObjCGCAttr();
746 Qc.removeAddressSpace();
747 Qc.removeObjCLifetime();
748
749 // We will create one Derived type for one qualifier and recurse to handle any
750 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000751 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000752 if (Qc.hasConst()) {
753 Tag = llvm::dwarf::DW_TAG_const_type;
754 Qc.removeConst();
755 } else if (Qc.hasVolatile()) {
756 Tag = llvm::dwarf::DW_TAG_volatile_type;
757 Qc.removeVolatile();
758 } else if (Qc.hasRestrict()) {
759 Tag = llvm::dwarf::DW_TAG_restrict_type;
760 Qc.removeRestrict();
761 } else {
762 assert(Qc.empty() && "Unknown type qualifier for debug info");
763 return getOrCreateType(QualType(T, 0), Unit);
764 }
765
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000766 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000767
768 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
769 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000770 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000771}
772
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000773llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
774 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000775
776 // The frontend treats 'id' as a typedef to an ObjCObjectType,
777 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
778 // debug info, we want to emit 'id' in both cases.
779 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000780 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000781
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000782 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
783 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000784}
785
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000786llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
787 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000788 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000789 Ty->getPointeeType(), Unit);
790}
791
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000792/// \return whether a C++ mangling exists for the type defined by TD.
793static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
794 switch (TheCU->getSourceLanguage()) {
795 case llvm::dwarf::DW_LANG_C_plus_plus:
796 return true;
797 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
798 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
799 default:
800 return false;
801 }
802}
803
Brock Wyma8557ec52018-05-22 12:41:19 +0000804// Determines if the tag declaration will require a type identifier.
805static bool needsTypeIdentifier(const TagDecl *TD,
806 CodeGenModule& CGM,
807 llvm::DICompileUnit *TheCU) {
808 // We only add a type identifier for types with C++ name mangling.
809 if (!hasCXXMangling(TD, TheCU))
810 return false;
811
812 // CodeView types with C++ mangling need a type identifier.
813 if (CGM.getCodeGenOpts().EmitCodeView)
814 return true;
815
816 // Externally visible types with C++ mangling need a type identifier.
817 if (TD->isExternallyVisible())
818 return true;
819
820 return false;
821}
822
823// When emitting CodeView debug information we need to produce a type
824// identifier for all types which have a C++ mangling. Until a GUID is added
825// to the identifier (not currently implemented) the result will not be unique
826// across compilation units.
827// When emitting DWARF debug information, we need to produce a type identifier
828// for all externally visible types with C++ name mangling. This identifier
829// should be unique across ODR-compliant compilation units.
830static SmallString<256> getTypeIdentifier(const TagType *Ty,
831 CodeGenModule &CGM,
832 llvm::DICompileUnit *TheCU) {
833 SmallString<256> Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000834 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000835
Brock Wyma8557ec52018-05-22 12:41:19 +0000836 if (!needsTypeIdentifier(TD, CGM, TheCU))
837 return Identifier;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000838
Manman Rene0064d82013-08-29 23:19:58 +0000839 // TODO: This is using the RTTI name. Is there a better way to get
840 // a unique string for a type?
Brock Wyma8557ec52018-05-22 12:41:19 +0000841 llvm::raw_svector_ostream Out(Identifier);
Manman Rene0064d82013-08-29 23:19:58 +0000842 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Brock Wyma8557ec52018-05-22 12:41:19 +0000843 return Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000844}
845
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000846/// \return the appropriate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000847static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
848 llvm::dwarf::Tag Tag;
849 if (RD->isStruct() || RD->isInterface())
850 Tag = llvm::dwarf::DW_TAG_structure_type;
851 else if (RD->isUnion())
852 Tag = llvm::dwarf::DW_TAG_union_type;
853 else {
854 // FIXME: This could be a struct type giving a default visibility different
855 // than C++ class type, but needs llvm metadata changes first.
856 assert(RD->isClass());
857 Tag = llvm::dwarf::DW_TAG_class_type;
858 }
859 return Tag;
860}
861
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000862llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000863CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000864 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000865 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000866 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
867 return cast<llvm::DICompositeType>(T);
868 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000869 unsigned Line = getLineNumber(RD->getLocation());
870 StringRef RDName = getClassName(RD);
871
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000872 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000873 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000874
Guy Benyei11169dd2012-12-18 14:30:41 +0000875 // Create the type.
Brock Wyma8557ec52018-05-22 12:41:19 +0000876 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000878 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +0000879 llvm::DINode::FlagFwdDecl, Identifier);
Paul Robinson1787f812017-09-28 18:37:02 +0000880 if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
881 if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
882 DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
883 CollectCXXTemplateParams(TSpecial, DefUnit));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000884 ReplaceMap.emplace_back(
885 std::piecewise_construct, std::make_tuple(Ty),
886 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000887 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000888}
889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000890llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000891 const Type *Ty,
892 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000893 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000894 // Bit size, align and offset of the type.
895 // Size is always the size of a pointer. We can't use getTypeSize here
896 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000897 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
898 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000899 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000900 Optional<unsigned> DWARFAddressSpace =
901 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000902
Keno Fischer87842f32015-11-16 09:04:13 +0000903 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
904 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
905 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000906 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000907 else
908 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000909 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000910}
911
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
913 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000914 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000915 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000916 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
917 TheCU, getOrCreateMainFile(), 0);
918 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
919 Cache = DBuilder.createPointerType(Cache, Size);
920 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000921}
922
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000923llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
924 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000925 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000926 QualType FType;
927 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000928 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000929 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000930
931 FieldOffset = 0;
932 FType = CGM.getContext().UnsignedLongTy;
933 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
934 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
935
936 Elements = DBuilder.getOrCreateArray(EltTys);
937 EltTys.clear();
938
Leny Kholodov80c047d2016-09-06 10:48:04 +0000939 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000940 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000941
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000942 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000943 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000944 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000945
946 // Bit size, align and offset of the type.
947 uint64_t Size = CGM.getContext().getTypeSize(Ty);
948
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000949 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000950
951 FieldOffset = 0;
952 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
953 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
954 FType = CGM.getContext().IntTy;
955 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
956 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000957 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000958 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
959
960 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000961 FieldSize = CGM.getContext().getTypeSize(Ty);
962 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000963 EltTys.push_back(DBuilder.createMemberType(
964 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
965 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000966
967 FieldOffset += FieldSize;
968 Elements = DBuilder.getOrCreateArray(EltTys);
969
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000970 // The __block_literal_generic structs are marked with a special
971 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
972 // the debugger needs to know about. To allow type uniquing, emit
973 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000974 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000975 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000976 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000977
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000978 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000979}
980
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000981llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
982 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000983 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000984 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000985
986 SmallString<128> NS;
987 llvm::raw_svector_ostream OS(NS);
Serge Pavlov03e672c2017-11-28 16:14:14 +0000988 Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false);
989 printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +0000990
David Majnemer58ed0f32016-07-17 00:39:12 +0000991 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000992 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000993
994 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000995 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
996 getLineNumber(Loc),
997 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000998}
999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1001 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001002 // We don't set size information, but do specify where the typedef was
1003 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +00001004 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001005
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001006 // Typedefs are derived from some other type.
1007 return DBuilder.createTypedef(
1008 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1009 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +00001010 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001011}
1012
Reid Klecknerf00f8032016-06-08 20:41:54 +00001013static unsigned getDwarfCC(CallingConv CC) {
1014 switch (CC) {
1015 case CC_C:
1016 // Avoid emitting DW_AT_calling_convention if the C convention was used.
1017 return 0;
1018
1019 case CC_X86StdCall:
1020 return llvm::dwarf::DW_CC_BORLAND_stdcall;
1021 case CC_X86FastCall:
1022 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
1023 case CC_X86ThisCall:
1024 return llvm::dwarf::DW_CC_BORLAND_thiscall;
1025 case CC_X86VectorCall:
1026 return llvm::dwarf::DW_CC_LLVM_vectorcall;
1027 case CC_X86Pascal:
1028 return llvm::dwarf::DW_CC_BORLAND_pascal;
Martin Storsjo022e7822017-07-17 20:49:45 +00001029 case CC_Win64:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001030 return llvm::dwarf::DW_CC_LLVM_Win64;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001031 case CC_X86_64SysV:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001032 return llvm::dwarf::DW_CC_LLVM_X86_64SysV;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001033 case CC_AAPCS:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001034 return llvm::dwarf::DW_CC_LLVM_AAPCS;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001035 case CC_AAPCS_VFP:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001036 return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001037 case CC_IntelOclBicc:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001038 return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001039 case CC_SpirFunction:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001040 return llvm::dwarf::DW_CC_LLVM_SpirFunction;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00001041 case CC_OpenCLKernel:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001042 return llvm::dwarf::DW_CC_LLVM_OpenCLKernel;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001043 case CC_Swift:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001044 return llvm::dwarf::DW_CC_LLVM_Swift;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001045 case CC_PreserveMost:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001046 return llvm::dwarf::DW_CC_LLVM_PreserveMost;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001047 case CC_PreserveAll:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001048 return llvm::dwarf::DW_CC_LLVM_PreserveAll;
Erich Keane757d3172016-11-02 18:29:35 +00001049 case CC_X86RegCall:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001050 return llvm::dwarf::DW_CC_LLVM_X86RegCall;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001051 }
1052 return 0;
1053}
1054
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001055llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1056 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001057 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001058
1059 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001060 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001061
1062 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001063 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001064 if (isa<FunctionNoProtoType>(Ty))
1065 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001066 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1067 for (const QualType &ParamType : FPT->param_types())
1068 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001069 if (FPT->isVariadic())
1070 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001071 }
1072
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001073 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001074 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001075 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001076}
1077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001079/// As an optimization, return 0 if the access specifier equals the
1080/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001081static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1082 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001083 AccessSpecifier Default = clang::AS_none;
1084 if (RD && RD->isClass())
1085 Default = clang::AS_private;
1086 else if (RD && (RD->isStruct() || RD->isUnion()))
1087 Default = clang::AS_public;
1088
1089 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001090 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001091
Eric Christophere7b87e52014-10-26 23:40:33 +00001092 switch (Access) {
1093 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001094 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001095 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001096 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001097 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001098 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001099 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001100 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001101 }
1102 llvm_unreachable("unexpected access enumerator");
1103}
Guy Benyei11169dd2012-12-18 14:30:41 +00001104
David Majnemerb4b671e2016-06-30 03:01:59 +00001105llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1106 llvm::DIScope *RecordTy,
1107 const RecordDecl *RD) {
1108 StringRef Name = BitFieldDecl->getName();
1109 QualType Ty = BitFieldDecl->getType();
1110 SourceLocation Loc = BitFieldDecl->getLocation();
1111 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1112 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1113
1114 // Get the location for the field.
1115 llvm::DIFile *File = getOrCreateFile(Loc);
1116 unsigned Line = getLineNumber(Loc);
1117
1118 const CGBitFieldInfo &BitFieldInfo =
1119 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1120 uint64_t SizeInBits = BitFieldInfo.Size;
1121 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001122 uint64_t StorageOffsetInBits =
1123 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001124 uint64_t Offset = BitFieldInfo.Offset;
1125 // The bit offsets for big endian machines are reversed for big
1126 // endian target, compensate for that as the DIDerivedType requires
1127 // un-reversed offsets.
1128 if (CGM.getDataLayout().isBigEndian())
1129 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1130 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001131 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001132 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001133 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1134 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001135}
1136
1137llvm::DIType *
1138CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1139 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001140 uint32_t AlignInBits, llvm::DIFile *tunit,
1141 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001142 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001143
1144 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001145 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 unsigned line = getLineNumber(loc);
1147
David Majnemer34b57492014-07-30 01:30:47 +00001148 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001149 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001151 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1152 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001153 if (!Align)
1154 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001155 }
1156
Leny Kholodov80c047d2016-09-06 10:48:04 +00001157 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001158 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001159 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001160}
1161
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001162void CGDebugInfo::CollectRecordLambdaFields(
1163 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001164 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001165 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1166 // has the name and the location of the variable so we should iterate over
1167 // both concurrently.
1168 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1169 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1170 unsigned fieldno = 0;
1171 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001172 E = CXXDecl->captures_end();
1173 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001174 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001175 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001176 SourceLocation Loc = C.getLocation();
1177 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001178 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001179 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001180 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001181 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001182 llvm::DIType *FieldType = createFieldType(
1183 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001184 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001185 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001186 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001187 // TODO: Need to handle 'this' in some way by probably renaming the
1188 // this of the lambda class and having a field member of 'this' or
1189 // by using AT_object_pointer for the function and having that be
1190 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001191 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001192 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001193 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001194 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001195 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001196 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001197
1198 elements.push_back(fieldType);
1199 }
1200 }
1201}
1202
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001203llvm::DIDerivedType *
1204CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001205 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001206 // Create the descriptor for the static variable, with or without
1207 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001208 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001209 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1210 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001211
Eric Christopher91a31902013-01-16 01:22:32 +00001212 unsigned LineNumber = getLineNumber(Var->getLocation());
1213 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001214 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001215 if (Var->getInit()) {
1216 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001217 if (Value) {
1218 if (Value->isInt())
1219 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1220 if (Value->isFloat())
1221 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1222 }
Eric Christopher91a31902013-01-16 01:22:32 +00001223 }
1224
Leny Kholodov80c047d2016-09-06 10:48:04 +00001225 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001226 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001227 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001228 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001229 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001230 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001231}
1232
Eric Christophere7b87e52014-10-26 23:40:33 +00001233void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1235 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001236 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001237 StringRef name = field->getName();
1238 QualType type = field->getType();
1239
1240 // Ignore unnamed fields unless they're anonymous structs/unions.
1241 if (name.empty() && !type->isRecordType())
1242 return;
1243
David Majnemerb4b671e2016-06-30 03:01:59 +00001244 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001245 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001246 FieldType = createBitFieldType(field, RecordTy, RD);
1247 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001248 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001249 FieldType =
1250 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001251 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001252 }
1253
David Majnemerb4b671e2016-06-30 03:01:59 +00001254 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001255}
1256
Reid Klecknere2e82062017-08-08 20:30:14 +00001257void CGDebugInfo::CollectRecordNestedType(
1258 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1259 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001260 // Injected class names are not considered nested records.
1261 if (isa<InjectedClassNameType>(Ty))
1262 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001263 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001264 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1265 elements.push_back(nestedType);
1266}
1267
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001268void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001270 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001272 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001273
Eric Christopher91a31902013-01-16 01:22:32 +00001274 if (CXXDecl && CXXDecl->isLambda())
1275 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1276 else {
1277 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001278
Reid Klecknere2e82062017-08-08 20:30:14 +00001279 // Debug info for nested types is included in the member list only for
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001280 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001281 bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001282
Eric Christopher91a31902013-01-16 01:22:32 +00001283 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001284 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001285
Eric Christopher91a31902013-01-16 01:22:32 +00001286 // Static and non-static members should appear in the same order as
1287 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001288 for (const auto *I : record->decls())
1289 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001290 if (V->hasAttr<NoDebugAttr>())
1291 continue;
David Blaikiece763042013-08-20 21:49:21 +00001292 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001293 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001294 if (MI != StaticDataMemberCache.end()) {
1295 assert(MI->second &&
1296 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001297 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001298 } else {
1299 auto Field = CreateRecordStaticField(V, RecordTy, record);
1300 elements.push_back(Field);
1301 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001302 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001303 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1304 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001305
1306 // Bump field number for next field.
1307 ++fieldNo;
Reid Klecknere2e82062017-08-08 20:30:14 +00001308 } else if (IncludeNestedTypes) {
1309 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1310 if (!nestedType->isImplicit() &&
1311 nestedType->getDeclContext() == record)
1312 CollectRecordNestedType(nestedType, elements);
1313 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001314 }
1315}
1316
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001318CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001319 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001320 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001321 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001322 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001323 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001324 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1325 Func, Unit);
1326}
David Blaikie2aaf0652013-01-07 22:24:59 +00001327
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1329 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001330 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001331 llvm::DITypeRefArray Args(
1332 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001333 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001334 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001335
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001336 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001337
1338 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001339 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
David Blaikie2aaf0652013-01-07 22:24:59 +00001341 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001342 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001343 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1344 // Create pointer type directly in this case.
1345 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1346 QualType PointeeTy = ThisPtrTy->getPointeeType();
1347 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001348 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001349 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001350 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1351 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001352 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001353 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001354 // TODO: This and the artificial type below are misleading, the
1355 // types aren't artificial the argument is, but the current
1356 // metadata doesn't represent that.
1357 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1358 Elts.push_back(ThisPtrType);
1359 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001360 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001361 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001362 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1363 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 }
1365
1366 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001367 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1368 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001369
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001370 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001371
Leny Kholodov80c047d2016-09-06 10:48:04 +00001372 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001373 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001374 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001375 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001376 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001377
Reid Klecknerf00f8032016-06-08 20:41:54 +00001378 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1379 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001380}
1381
Eric Christopherb2a008c2013-05-16 00:45:12 +00001382/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001383/// inside a function.
1384static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001385 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001386 return isFunctionLocalClass(NRD);
1387 if (isa<FunctionDecl>(RD->getDeclContext()))
1388 return true;
1389 return false;
1390}
1391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1393 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001394 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001395 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001396
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001399
1400 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1401 // make sense to give a single ctor/dtor a linkage name.
1402 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001403 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1404 // property to use here. It may've been intended to model "is non-external
1405 // type" but misses cases of non-function-local but non-external classes such
1406 // as those in anonymous namespaces as well as the reverse - external types
1407 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1409 MethodLinkageName = CGM.getMangledName(Method);
1410
1411 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001413 unsigned MethodLine = 0;
1414 if (!Method->isImplicit()) {
1415 MethodDefUnit = getOrCreateFile(Method->getLocation());
1416 MethodLine = getLineNumber(Method->getLocation());
1417 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001418
1419 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001420 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001421 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001423 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001424 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001425
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 if (Method->isVirtual()) {
1427 if (Method->isPure())
1428 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1429 else
1430 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001431
Reid Kleckner216d0a12016-06-16 20:08:51 +00001432 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1433 // It doesn't make sense to give a virtual destructor a vtable index,
1434 // since a single destructor has two entries in the vtable.
1435 if (!isa<CXXDestructorDecl>(Method))
1436 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1437 } else {
1438 // Emit MS ABI vftable information. There is only one entry for the
1439 // deleting dtor.
1440 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1441 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
Reid Klecknercbec0262018-04-02 20:00:39 +00001442 MethodVFTableLocation ML =
Reid Kleckner216d0a12016-06-16 20:08:51 +00001443 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1444 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001445
1446 // CodeView only records the vftable offset in the class that introduces
1447 // the virtual method. This is possible because, unlike Itanium, the MS
1448 // C++ ABI does not include all virtual methods from non-primary bases in
1449 // the vtable for the most derived class. For example, if C inherits from
1450 // A and B, C's primary vftable will not include B's virtual methods.
Benjamin Krameracfa3392017-12-17 23:52:45 +00001451 if (Method->size_overridden_methods() == 0)
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001452 Flags |= llvm::DINode::FlagIntroducedVirtual;
1453
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001454 // The 'this' adjustment accounts for both the virtual and non-virtual
1455 // portions of the adjustment. Presumably the debugger only uses it when
1456 // it knows the dynamic type of an object.
1457 ThisAdjustment = CGM.getCXXABI()
1458 .getVirtualFunctionPrologueThisAdjustment(GD)
1459 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001460 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 ContainingType = RecordTy;
1462 }
1463
Adrian McCarthyd91bf392017-09-13 20:53:55 +00001464 if (Method->isStatic())
1465 Flags |= llvm::DINode::FlagStaticMember;
Guy Benyei11169dd2012-12-18 14:30:41 +00001466 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001467 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001468 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001469 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001471 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001472 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001473 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001474 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001475 }
1476 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001477 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001478 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001479 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001480 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001481 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001482
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001483 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1484 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001485 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001486 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1487 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1488 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001489
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001490 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001491
1492 return SP;
1493}
1494
Eric Christophere7b87e52014-10-26 23:40:33 +00001495void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001496 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1497 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001498
1499 // Since we want more than just the individual member decls if we
1500 // have templated functions iterate over every declaration to gather
1501 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001502 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001503 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1504 // If the member is implicit, don't add it to the member list. This avoids
1505 // the member being added to type units by LLVM, while still allowing it
1506 // to be emitted into the type declaration/reference inside the compile
1507 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001508 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001509 // FIXME: Handle Using(Shadow?)Decls here to create
1510 // DW_TAG_imported_declarations inside the class for base decls brought into
1511 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1512 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1513 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001514 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001515 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001516
1517 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1518 continue;
1519
David Blaikiefd580722014-10-06 05:18:55 +00001520 // Reuse the existing member function declaration if it exists.
1521 // It may be associated with the declaration of the type & should be
1522 // reused as we're building the definition.
1523 //
1524 // This situation can arise in the vtable-based debug info reduction where
1525 // implicit members are emitted in a non-vtable TU.
1526 auto MI = SPCache.find(Method->getCanonicalDecl());
1527 EltTys.push_back(MI == SPCache.end()
1528 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001529 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001531}
Guy Benyei11169dd2012-12-18 14:30:41 +00001532
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001533void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001534 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001535 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001536 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1537 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1538 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001539
Bob Haarmandff36732016-10-25 22:19:32 +00001540 // If we are generating CodeView debug info, we also need to emit records for
1541 // indirect virtual base classes.
1542 if (CGM.getCodeGenOpts().EmitCodeView) {
1543 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1544 llvm::DINode::FlagIndirectVirtualBase);
1545 }
1546}
1547
1548void CGDebugInfo::CollectCXXBasesAux(
1549 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1550 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1551 const CXXRecordDecl::base_class_const_range &Bases,
1552 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1553 llvm::DINode::DIFlags StartingFlags) {
1554 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1555 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001556 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001557 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001558 if (!SeenTypes.insert(Base).second)
1559 continue;
1560 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1561 llvm::DINode::DIFlags BFlags = StartingFlags;
1562 uint64_t BaseOffset;
Brock Wyma3db2b102018-05-14 21:21:22 +00001563 uint32_t VBPtrOffset = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001564
Aaron Ballman574705e2014-03-13 15:41:46 +00001565 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001566 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1567 // virtual base offset offset is -ve. The code generator emits dwarf
1568 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001569 BaseOffset = 0 - CGM.getItaniumVTableContext()
1570 .getVirtualBaseOffsetOffset(RD, Base)
1571 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001572 } else {
1573 // In the MS ABI, store the vbtable offset, which is analogous to the
1574 // vbase offset offset in Itanium.
1575 BaseOffset =
1576 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
Brock Wyma3db2b102018-05-14 21:21:22 +00001577 VBPtrOffset = CGM.getContext().getASTRecordLayout(RD).getVBPtrOffset()
1578 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001579 }
Bob Haarmandff36732016-10-25 22:19:32 +00001580 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001581 } else
1582 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1583 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1584 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001585
Adrian Prantl21361fb2014-08-29 22:44:27 +00001586 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001587 llvm::DIType *DTy =
Brock Wyma3db2b102018-05-14 21:21:22 +00001588 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, VBPtrOffset,
1589 BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 EltTys.push_back(DTy);
1591 }
1592}
1593
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001594llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001595CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1596 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001597 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001598 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001599 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1600 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001601 StringRef Name;
1602 if (TPList)
1603 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001604 switch (TA.getKind()) {
1605 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001606 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001607 TemplateParams.push_back(
1608 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001609 } break;
1610 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001611 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001612 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1613 TheCU, Name, TTy,
1614 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001615 } break;
1616 case TemplateArgument::Declaration: {
1617 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001618 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001619 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001620 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001621 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001622 // Variable pointer template parameters have a value that is the address
1623 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001624 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001625 V = CGM.GetAddrOfGlobalVar(VD);
1626 // Member function pointers have special support for building them, though
1627 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001628 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001629 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001630 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001631 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001632 // Member data pointers have special handling too to compute the fixed
1633 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001634 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001635 // These five lines (& possibly the above member function pointer
1636 // handling) might be able to be refactored to use similar code in
1637 // CodeGenModule::getMemberPointerConstant
1638 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1639 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001640 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001641 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001642 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001643 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1644 TheCU, Name, TTy,
1645 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001646 } break;
1647 case TemplateArgument::NullPtr: {
1648 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001649 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001650 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001651 // Special case member data pointer null values since they're actually -1
1652 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001653 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001654 // But treat member function pointers as simple zero integers because
1655 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1656 // CodeGen grows handling for values of non-null member function
1657 // pointers then perhaps we could remove this special case and rely on
1658 // EmitNullMemberPointer for member function pointers.
1659 if (MPT->isMemberDataPointer())
1660 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1661 if (!V)
1662 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001663 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001664 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001665 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001666 case TemplateArgument::Template:
1667 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001668 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001669 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1670 break;
1671 case TemplateArgument::Pack:
1672 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1673 TheCU, Name, nullptr,
1674 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1675 break;
David Majnemer5559d472013-08-24 08:21:10 +00001676 case TemplateArgument::Expression: {
1677 const Expr *E = TA.getAsExpr();
1678 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001679 if (E->isGLValue())
1680 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001681 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001682 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001683 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001684 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001685 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001686 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001687 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001688 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001689 case TemplateArgument::Null:
1690 llvm_unreachable(
1691 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 }
1693 }
1694 return DBuilder.getOrCreateArray(TemplateParams);
1695}
1696
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001697llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001698CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001699 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 if (FD->getTemplatedKind() ==
1701 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001702 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1703 ->getTemplate()
1704 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001705 return CollectTemplateParams(
1706 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001707 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001708 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001709}
1710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001711llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1712 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001713 // Always get the full list of parameters, not just the ones from
1714 // the specialization.
1715 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001716 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001717 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001718 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001719}
1720
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001721llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001722 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001723 return VTablePtrType;
1724
1725 ASTContext &Context = CGM.getContext();
1726
1727 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001728 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001730 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001732 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1733 Optional<unsigned> DWARFAddressSpace =
1734 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1735
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001736 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001737 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1738 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1740 return VTablePtrType;
1741}
1742
Guy Benyei11169dd2012-12-18 14:30:41 +00001743StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001744 // Copy the gdb compatible name on the side and use its reference.
1745 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001746}
1747
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001748void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001749 SmallVectorImpl<llvm::Metadata *> &EltTys,
1750 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001751 // If this class is not dynamic then there is not any vtable info to collect.
1752 if (!RD->isDynamicClass())
1753 return;
1754
Reid Kleckner59812422016-08-31 20:35:01 +00001755 // Don't emit any vtable shape or vptr info if this class doesn't have an
1756 // extendable vfptr. This can happen if the class doesn't have virtual
1757 // methods, or in the MS ABI if those virtual methods only come from virtually
1758 // inherited bases.
1759 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1760 if (!RL.hasExtendableVFPtr())
1761 return;
1762
Reid Klecknerdc124992016-08-31 16:11:43 +00001763 // CodeView needs to know how large the vtable of every dynamic class is, so
1764 // emit a special named pointer type into the element list. The vptr type
1765 // points to this type as well.
1766 llvm::DIType *VPtrTy = nullptr;
1767 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1768 CGM.getTarget().getCXXABI().isMicrosoft();
1769 if (NeedVTableShape) {
1770 uint64_t PtrWidth =
1771 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1772 const VTableLayout &VFTLayout =
1773 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1774 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001775 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001776 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001777 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1778 Optional<unsigned> DWARFAddressSpace =
1779 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001780
1781 // Create a very wide void* type and insert it directly in the element list.
1782 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001783 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1784 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001785 EltTys.push_back(VTableType);
1786
1787 // The vptr is a pointer to this special vtable type.
1788 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1789 }
1790
1791 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001792 if (RL.getPrimaryBase())
1793 return;
1794
1795 if (!VPtrTy)
1796 VPtrTy = getOrCreateVTablePtrType(Unit);
1797
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001799 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001800 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001801 llvm::DINode::FlagArtificial, VPtrTy);
1802 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001803}
1804
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001805llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001806 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001807 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001808 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 return T;
1810}
1811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001812llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001813 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001814 return getOrCreateStandaloneType(D, Loc);
1815}
1816
1817llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1818 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001819 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001820 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001821 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001822 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001823
Adrian Prantl73409ce2013-03-11 18:33:46 +00001824 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 return T;
1826}
1827
David Blaikie483a9da2014-05-06 18:35:21 +00001828void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001829 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001830 return;
1831 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001832 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001833 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001834 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001835 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001836 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001837 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001838 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001839}
1840
David Blaikieb2e86eb2013-08-15 20:49:17 +00001841void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001842 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001843 !CGM.getLangOpts().CPlusPlus)
1844 completeRequiredType(RD);
1845}
1846
David Blaikieb11c8732017-01-30 06:36:08 +00001847/// Return true if the class or any of its methods are marked dllimport.
1848static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1849 if (RD->hasAttr<DLLImportAttr>())
1850 return true;
1851 for (const CXXMethodDecl *MD : RD->methods())
1852 if (MD->hasAttr<DLLImportAttr>())
1853 return true;
1854 return false;
1855}
1856
Adrian Prantla43acdc2017-07-24 23:48:51 +00001857/// Does a type definition exist in an imported clang module?
1858static bool isDefinedInClangModule(const RecordDecl *RD) {
1859 // Only definitions that where imported from an AST file come from a module.
1860 if (!RD || !RD->isFromASTFile())
1861 return false;
1862 // Anonymous entities cannot be addressed. Treat them as not from module.
1863 if (!RD->isExternallyVisible() && RD->getName().empty())
1864 return false;
1865 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1866 if (!CXXDecl->isCompleteDefinition())
1867 return false;
1868 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1869 if (TemplateKind != TSK_Undeclared) {
1870 // This is a template, check the origin of the first member.
1871 if (CXXDecl->field_begin() == CXXDecl->field_end())
1872 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1873 if (!CXXDecl->field_begin()->isFromASTFile())
1874 return false;
1875 }
1876 }
1877 return true;
1878}
1879
David Blaikie6943dea2013-08-20 01:28:15 +00001880void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001881 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1882 if (CXXRD->isDynamicClass() &&
1883 CGM.getVTableLinkage(CXXRD) ==
1884 llvm::GlobalValue::AvailableExternallyLinkage &&
1885 !isClassOrMethodDLLImport(CXXRD))
1886 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001887
1888 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1889 return;
1890
David Blaikieb11c8732017-01-30 06:36:08 +00001891 completeClass(RD);
1892}
1893
1894void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001895 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001896 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001897 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +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 Blaikieb2e86eb2013-08-15 20:49:17 +00001901 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001902 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
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 Blaikieb2e86eb2013-08-15 20:49:17 +00001905}
1906
David Blaikie0e716b42014-03-03 23:48:23 +00001907static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1908 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001909 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1910 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001911 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001912 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001913 return true;
1914 return false;
1915}
1916
Benjamin Kramer8c305922016-02-02 11:06:51 +00001917static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1918 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001919 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001920 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001921 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001922
David Blaikie1ac9c982017-04-11 21:13:37 +00001923 if (auto *ES = RD->getASTContext().getExternalSource())
1924 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1925 return true;
1926
Benjamin Kramer8c305922016-02-02 11:06:51 +00001927 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001928 return false;
1929
1930 if (!LangOpts.CPlusPlus)
1931 return false;
1932
1933 if (!RD->isCompleteDefinitionRequired())
1934 return true;
1935
David Majnemer58ed0f32016-07-17 00:39:12 +00001936 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001937
1938 if (!CXXDecl)
1939 return false;
1940
Adrian McCarthy99242982016-08-16 22:11:18 +00001941 // Only emit complete debug info for a dynamic class when its vtable is
1942 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001943 // across DLL boundaries, so skip this optimization if the class or any of its
1944 // methods are marked dllimport. This isn't a complete solution, since objects
1945 // without any dllimport methods can be used in one DLL and constructed in
1946 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001947 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001948 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001949 return true;
1950
1951 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001952 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001953 Spec = SD->getSpecializationKind();
1954
1955 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1956 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1957 CXXDecl->method_end()))
1958 return true;
1959
1960 return false;
1961}
1962
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001963void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1964 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1965 return;
1966
1967 QualType Ty = CGM.getContext().getRecordType(RD);
1968 llvm::DIType *T = getTypeOrNull(Ty);
1969 if (T && T->isForwardDecl())
1970 completeClassData(RD);
1971}
1972
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001975 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001976 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1977 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001978 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001979 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001980 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001981 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001982
David Blaikieb2e86eb2013-08-15 20:49:17 +00001983 return CreateTypeDefinition(Ty);
1984}
1985
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001987 RecordDecl *RD = Ty->getDecl();
1988
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001990 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001991
1992 // Records and classes and unions can all be recursive. To handle them, we
1993 // first generate a debug descriptor for the struct as a forward declaration.
1994 // Then (if it is a definition) we go through and get debug info for all of
1995 // its members. Finally, we create a descriptor for the complete type (which
1996 // may refer to the forward decl if the struct is recursive) and replace all
1997 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001998 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001999
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002000 const RecordDecl *D = RD->getDefinition();
2001 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 return FwdDecl;
2003
David Majnemer58ed0f32016-07-17 00:39:12 +00002004 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00002005 CollectContainingType(CXXDecl, FwdDecl);
2006
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002008 LexicalBlockStack.emplace_back(&*FwdDecl);
2009 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002010
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002012 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00002013 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00002014
2015 // Note: The split of CXXDecl information here is intentional, the
2016 // gdb tests will depend on a certain ordering at printout. The debug
2017 // information offsets are still correct if we merge them all together
2018 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00002019 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 if (CXXDecl) {
2021 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00002022 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 }
2024
Eric Christopher91a31902013-01-16 01:22:32 +00002025 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00002027 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002029
2030 LexicalBlockStack.pop_back();
2031 RegionMap.erase(Ty->getDecl());
2032
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002033 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002034 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002035
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002036 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002037 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002038 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002039
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002040 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002041 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002042}
2043
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002044llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
2045 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 // Ignore protocols.
2047 return getOrCreateType(Ty->getBaseType(), Unit);
2048}
2049
Manman Rene6be26c2016-09-13 17:25:08 +00002050llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
2051 llvm::DIFile *Unit) {
2052 // Ignore protocols.
2053 SourceLocation Loc = Ty->getDecl()->getLocation();
2054
2055 // Use Typedefs to represent ObjCTypeParamType.
2056 return DBuilder.createTypedef(
2057 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
2058 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2059 getDeclContextDescriptor(Ty->getDecl()));
2060}
2061
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002062/// \return true if Getter has the default name for the property PD.
2063static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2064 const ObjCMethodDecl *Getter) {
2065 assert(PD);
2066 if (!Getter)
2067 return true;
2068
2069 assert(Getter->getDeclName().isObjCZeroArgSelector());
2070 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002071 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002072}
2073
2074/// \return true if Setter has the default name for the property PD.
2075static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2076 const ObjCMethodDecl *Setter) {
2077 assert(PD);
2078 if (!Setter)
2079 return true;
2080
2081 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002082 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002083 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002084}
2085
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002086llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2087 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 ObjCInterfaceDecl *ID = Ty->getDecl();
2089 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002090 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002091
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002092 // Return a forward declaration if this type was imported from a clang module,
2093 // and this is not the compile unit with the implementation of the type (which
2094 // may contain hidden ivars).
2095 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2096 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002097 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2098 ID->getName(),
2099 getDeclContextDescriptor(ID), Unit, 0);
2100
Guy Benyei11169dd2012-12-18 14:30:41 +00002101 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002102 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002103 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002104 auto RuntimeLang =
2105 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002106
2107 // If this is just a forward declaration return a special forward-declaration
2108 // debug type since we won't be able to lay out the entire type.
2109 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002110 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002111 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002112 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002113 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2114 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002115 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 return FwdDecl;
2117 }
2118
David Blaikieef8a9512014-05-05 23:23:53 +00002119 return CreateTypeDefinition(Ty, Unit);
2120}
2121
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002122llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002123CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2124 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002125 // Use the Module pointer as the key into the cache. This is a
2126 // nullptr if the "Module" is a PCH, which is safe because we don't
2127 // support chained PCH debug info, so there can only be a single PCH.
2128 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002129 auto ModRef = ModuleCache.find(M);
2130 if (ModRef != ModuleCache.end())
2131 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002132
2133 // Macro definitions that were defined with "-D" on the command line.
2134 SmallString<128> ConfigMacros;
2135 {
2136 llvm::raw_svector_ostream OS(ConfigMacros);
2137 const auto &PPOpts = CGM.getPreprocessorOpts();
2138 unsigned I = 0;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002139 // Translate the macro definitions back into a command line.
Adrian Prantl2388ead2015-06-30 18:01:05 +00002140 for (auto &M : PPOpts.Macros) {
2141 if (++I > 1)
2142 OS << " ";
2143 const std::string &Macro = M.first;
2144 bool Undef = M.second;
2145 OS << "\"-" << (Undef ? 'U' : 'D');
2146 for (char c : Macro)
2147 switch (c) {
2148 case '\\' : OS << "\\\\"; break;
2149 case '"' : OS << "\\\""; break;
2150 default: OS << c;
2151 }
2152 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002153 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002154 }
Adrian Prantl66689202015-09-18 23:01:45 +00002155
Adrian Prantl835e6632015-09-24 16:10:10 +00002156 bool IsRootModule = M ? !M->Parent : true;
2157 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002158 // PCH files don't have a signature field in the control block,
2159 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002160 // We use the lower 64 bits for debug info.
2161 uint64_t Signature =
2162 Mod.getSignature()
2163 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2164 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002165 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002166 DIB.createCompileUnit(TheCU->getSourceLanguage(),
Scott Lindera2fbcef2018-02-26 17:32:31 +00002167 // TODO: Support "Source" from external AST providers?
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002168 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2169 TheCU->getProducer(), true, StringRef(), 0,
2170 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2171 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002172 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002173 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002174 llvm::DIModule *Parent =
2175 IsRootModule ? nullptr
2176 : getOrCreateModuleRef(
2177 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2178 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002179 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002180 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2181 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002182 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002183 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002184}
2185
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002186llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2187 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002188 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002189 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002190 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002191 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002192
2193 // Bit size, align and offset of the type.
2194 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002195 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002196
Leny Kholodov80c047d2016-09-06 10:48:04 +00002197 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002199 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002200
Adrian Prantlfd696112015-10-01 00:48:51 +00002201 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002202 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002203 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2204 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002205
David Blaikieef8a9512014-05-05 23:23:53 +00002206 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002207 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002208
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002209 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002210 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002211 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002212
2213 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002214 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002215
2216 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2217 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002218 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002219 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002220 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002221 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002222
Brock Wyma3db2b102018-05-14 21:21:22 +00002223 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002224 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 EltTys.push_back(InhTag);
2226 }
2227
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002228 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002229 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002230 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002231 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 unsigned PLine = getLineNumber(Loc);
2233 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2234 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002235 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2236 PD->getName(), PUnit, PLine,
2237 hasDefaultGetterName(PD, Getter) ? ""
2238 : getSelectorName(PD->getGetterName()),
2239 hasDefaultSetterName(PD, Setter) ? ""
2240 : getSelectorName(PD->getSetterName()),
2241 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002242 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002243 };
2244 {
2245 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002246 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002247 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002248 PropertySet.insert(PD->getIdentifier());
2249 AddProperty(PD);
2250 }
Manman Renefe1bac2016-01-27 20:00:32 +00002251 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002252 // Don't emit duplicate metadata for properties that were already in a
2253 // class extension.
2254 if (!PropertySet.insert(PD->getIdentifier()).second)
2255 continue;
2256 AddProperty(PD);
2257 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 }
2259
2260 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2261 unsigned FieldNo = 0;
2262 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2263 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002264 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002265 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002266 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002267
Guy Benyei11169dd2012-12-18 14:30:41 +00002268 StringRef FieldName = Field->getName();
2269
2270 // Ignore unnamed fields.
2271 if (FieldName.empty())
2272 continue;
2273
2274 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002275 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 unsigned FieldLine = getLineNumber(Field->getLocation());
2277 QualType FType = Field->getType();
2278 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002279 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002280
2281 if (!FType->isIncompleteArrayType()) {
2282
2283 // Bit size, align and offset of the type.
2284 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002285 ? Field->getBitWidthValue(CGM.getContext())
2286 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002287 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002288 }
2289
2290 uint64_t FieldOffset;
2291 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2292 // We don't know the runtime offset of an ivar if we're using the
2293 // non-fragile ABI. For bitfields, use the bit offset into the first
2294 // byte of storage of the bitfield. For other fields, use zero.
2295 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002296 FieldOffset =
2297 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 FieldOffset %= CGM.getContext().getCharWidth();
2299 } else {
2300 FieldOffset = 0;
2301 }
2302 } else {
2303 FieldOffset = RL.getFieldOffset(FieldNo);
2304 }
2305
Leny Kholodov80c047d2016-09-06 10:48:04 +00002306 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002308 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002310 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002311 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002312 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002313
Craig Topper8a13c412014-05-21 05:09:00 +00002314 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002315 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002316 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002317 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002318 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002319 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002320 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002321 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002322 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2323 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002324 PropertyNode = DBuilder.createObjCProperty(
2325 PD->getName(), PUnit, PLine,
2326 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2327 PD->getGetterName()),
2328 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2329 PD->getSetterName()),
2330 PD->getPropertyAttributes(),
2331 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002332 }
2333 }
2334 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002335 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2336 FieldSize, FieldAlign, FieldOffset, Flags,
2337 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002338 EltTys.push_back(FieldTy);
2339 }
2340
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002341 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002342 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002343
Guy Benyei11169dd2012-12-18 14:30:41 +00002344 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002345 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002346}
2347
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002348llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2349 llvm::DIFile *Unit) {
2350 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 int64_t Count = Ty->getNumElements();
Guy Benyei11169dd2012-12-18 14:30:41 +00002352
Sander de Smalen891af03a2018-02-03 13:55:59 +00002353 llvm::Metadata *Subscript;
2354 QualType QTy(Ty, 0);
2355 auto SizeExpr = SizeExprCache.find(QTy);
2356 if (SizeExpr != SizeExprCache.end())
2357 Subscript = DBuilder.getOrCreateSubrange(0, SizeExpr->getSecond());
2358 else
2359 Subscript = DBuilder.getOrCreateSubrange(0, Count ? Count : -1);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002360 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002361
2362 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002363 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002364
2365 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2366}
2367
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002368llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002370 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002371
2372 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002373 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002374 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002375 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2376 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 } else if (Ty->isIncompleteArrayType()) {
2378 Size = 0;
2379 if (Ty->getElementType()->isIncompleteType())
2380 Align = 0;
2381 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002382 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002383 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002384 Size = 0;
2385 Align = 0;
2386 } else {
2387 // Size and align of the whole array, not the element type.
2388 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002389 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002390 }
2391
2392 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2393 // interior arrays, do we care? Why aren't nested arrays represented the
2394 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002395 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002396 QualType EltTy(Ty, 0);
2397 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2398 // If the number of elements is known, then count is that number. Otherwise,
2399 // it's -1. This allows us to represent a subrange with an array of 0
2400 // elements, like this:
2401 //
2402 // struct foo {
2403 // int x[0];
2404 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002405 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002406 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002408 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002409 if (Expr *Size = VAT->getSizeExpr()) {
2410 llvm::APSInt V;
2411 if (Size->EvaluateAsInt(V, CGM.getContext()))
2412 Count = V.getExtValue();
2413 }
David Blaikie87173f12016-08-22 17:49:56 +00002414 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002415
Sander de Smalen891af03a2018-02-03 13:55:59 +00002416 auto SizeNode = SizeExprCache.find(EltTy);
2417 if (SizeNode != SizeExprCache.end())
2418 Subscripts.push_back(
2419 DBuilder.getOrCreateSubrange(0, SizeNode->getSecond()));
2420 else
2421 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
Guy Benyei11169dd2012-12-18 14:30:41 +00002422 EltTy = Ty->getElementType();
2423 }
2424
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002425 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002426
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002427 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2428 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002429}
2430
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002431llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2432 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002433 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2434 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002435}
2436
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2438 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002439 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2440 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002441}
2442
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002443llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2444 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002445 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002446 uint64_t Size = 0;
2447
2448 if (!Ty->isIncompleteType()) {
2449 Size = CGM.getContext().getTypeSize(Ty);
2450
2451 // Set the MS inheritance model. There is no flag for the unspecified model.
2452 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2453 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2454 case MSInheritanceAttr::Keyword_single_inheritance:
2455 Flags |= llvm::DINode::FlagSingleInheritance;
2456 break;
2457 case MSInheritanceAttr::Keyword_multiple_inheritance:
2458 Flags |= llvm::DINode::FlagMultipleInheritance;
2459 break;
2460 case MSInheritanceAttr::Keyword_virtual_inheritance:
2461 Flags |= llvm::DINode::FlagVirtualInheritance;
2462 break;
2463 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2464 break;
2465 }
2466 }
2467 }
2468
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002469 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002470 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002471 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002472 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2473 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002474
2475 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002476 Ty->getPointeeType()->getAs<FunctionProtoType>();
2477 return DBuilder.createMemberPointerType(
2478 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2479 Ty->getClass(), FPT->getTypeQuals())),
2480 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002481 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002482}
2483
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002485 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2486 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002487}
2488
Xiuli Pan9c14e282016-01-09 12:53:17 +00002489llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2490 llvm::DIFile *U) {
2491 return getOrCreateType(Ty->getElementType(), U);
2492}
2493
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002494llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002495 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002496
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002498 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002499 if (!ED->getTypeForDecl()->isIncompleteType()) {
2500 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002501 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 }
2503
Brock Wyma8557ec52018-05-22 12:41:19 +00002504 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002505
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002506 bool isImportedFromModule =
2507 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2508
Guy Benyei11169dd2012-12-18 14:30:41 +00002509 // If this is just a forward declaration, construct an appropriately
2510 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002511 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002512 // Note that it is possible for enums to be created as part of
2513 // their own declcontext. In this case a FwdDecl will be created
2514 // twice. This doesn't cause a problem because both FwdDecls are
2515 // entered into the ReplaceMap: finalize() will replace the first
2516 // FwdDecl with the second and then replace the second with
2517 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002518 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002519 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002520 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2521 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002522
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 unsigned Line = getLineNumber(ED->getLocation());
2524 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002525 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002526 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Brock Wyma8557ec52018-05-22 12:41:19 +00002527 0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
Adrian Prantla40030f2016-02-06 01:59:09 +00002528
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002529 ReplaceMap.emplace_back(
2530 std::piecewise_construct, std::make_tuple(Ty),
2531 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002532 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002533 }
2534
David Blaikie483a9da2014-05-06 18:35:21 +00002535 return CreateTypeDefinition(Ty);
2536}
2537
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002538llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002539 const EnumDecl *ED = Ty->getDecl();
2540 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002541 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002542 if (!ED->getTypeForDecl()->isIncompleteType()) {
2543 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002544 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002545 }
2546
Brock Wyma8557ec52018-05-22 12:41:19 +00002547 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
David Blaikie483a9da2014-05-06 18:35:21 +00002548
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002549 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002550 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002551 ED = ED->getDefinition();
Momchil Velikov25f6be52018-02-12 16:12:52 +00002552 bool IsSigned = ED->getIntegerType()->isSignedIntegerType();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002553 for (const auto *Enum : ED->enumerators()) {
Momchil Velikov25f6be52018-02-12 16:12:52 +00002554 const auto &InitVal = Enum->getInitVal();
2555 auto Value = IsSigned ? InitVal.getSExtValue() : InitVal.getZExtValue();
2556 Enumerators.push_back(
2557 DBuilder.createEnumerator(Enum->getName(), Value, !IsSigned));
Guy Benyei11169dd2012-12-18 14:30:41 +00002558 }
2559
2560 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002561 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002563 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002565 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Momchil Velikov25f6be52018-02-12 16:12:52 +00002566 llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002567 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2568 Line, Size, Align, EltArray, ClassTy,
Brock Wyma8557ec52018-05-22 12:41:19 +00002569 Identifier, ED->isFixed());
Guy Benyei11169dd2012-12-18 14:30:41 +00002570}
2571
Amjad Aboud546bc112017-02-09 22:07:24 +00002572llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2573 unsigned MType, SourceLocation LineLoc,
2574 StringRef Name, StringRef Value) {
2575 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2576 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2577}
2578
2579llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2580 SourceLocation LineLoc,
2581 SourceLocation FileLoc) {
2582 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2583 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2584 return DBuilder.createTempMacroFile(Parent, Line, FName);
2585}
2586
David Blaikie05491062013-01-21 04:37:12 +00002587static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2588 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002590 Qualifiers InnerQuals = T.getLocalQualifiers();
2591 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2592 // that is already there.
2593 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2594 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 QualType LastT = T;
2596 switch (T->getTypeClass()) {
2597 default:
David Blaikie05491062013-01-21 04:37:12 +00002598 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002599 case Type::TemplateSpecialization: {
2600 const auto *Spec = cast<TemplateSpecializationType>(T);
2601 if (Spec->isTypeAlias())
2602 return C.getQualifiedType(T.getTypePtr(), Quals);
2603 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002604 break;
2605 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002606 case Type::TypeOfExpr:
2607 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2608 break;
2609 case Type::TypeOf:
2610 T = cast<TypeOfType>(T)->getUnderlyingType();
2611 break;
2612 case Type::Decltype:
2613 T = cast<DecltypeType>(T)->getUnderlyingType();
2614 break;
2615 case Type::UnaryTransform:
2616 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2617 break;
2618 case Type::Attributed:
2619 T = cast<AttributedType>(T)->getEquivalentType();
2620 break;
2621 case Type::Elaborated:
2622 T = cast<ElaboratedType>(T)->getNamedType();
2623 break;
2624 case Type::Paren:
2625 T = cast<ParenType>(T)->getInnerType();
2626 break;
David Blaikie05491062013-01-21 04:37:12 +00002627 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002630 case Type::Auto:
2631 case Type::DeducedTemplateSpecialization: {
2632 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002633 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002634 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002635 break;
2636 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002637 case Type::Adjusted:
2638 case Type::Decayed:
2639 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2640 T = cast<AdjustedType>(T)->getAdjustedType();
2641 break;
2642 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002643
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002645 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002646 } while (true);
2647}
2648
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002649llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002650
2651 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002652 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002653
David Blaikief427b002014-05-06 03:42:01 +00002654 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002655 if (it != TypeCache.end()) {
2656 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002657 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002658 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 }
2660
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002661 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002662}
2663
David Blaikie0e716b42014-03-03 23:48:23 +00002664void CGDebugInfo::completeTemplateDefinition(
2665 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002666 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002667 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002668 completeUnusedClass(SD);
2669}
David Blaikie0856f662014-03-04 22:01:08 +00002670
David Blaikie1ac9c982017-04-11 21:13:37 +00002671void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2672 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2673 return;
2674
2675 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002676 // In case this type has no member function definitions being emitted, ensure
2677 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002678 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002679}
2680
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002681llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002683 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002684
2685 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002686 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002687
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002688 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002689 return T;
2690
Adrian Prantlca844182015-09-11 17:23:03 +00002691 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002692 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002693
2694 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002695 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002696
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 return Res;
2698}
2699
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002700llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002701 // A forward declaration inside a module header does not belong to the module.
2702 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2703 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002704 if (DebugTypeExtRefs && D->isFromASTFile()) {
2705 // Record a reference to an imported clang module or precompiled header.
2706 auto *Reader = CGM.getContext().getExternalSource();
2707 auto Idx = D->getOwningModuleID();
2708 auto Info = Reader->getSourceDescriptor(Idx);
2709 if (Info)
2710 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2711 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002712 // We are building a clang module or a precompiled header.
2713 //
2714 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2715 // and it wouldn't be necessary to specify the parent scope
2716 // because the type is already unique by definition (it would look
2717 // like the output of -fno-standalone-debug). On the other hand,
2718 // the parent scope helps a consumer to quickly locate the object
2719 // file where the type's definition is located, so it might be
2720 // best to make this behavior a command line or debugger tuning
2721 // option.
Richard Smith54f04402017-05-18 02:29:20 +00002722 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002723 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002724 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2725 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002726 } else {
2727 // This the precompiled header being built.
2728 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002729 }
2730 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002731
Adrian Prantl9402cef2015-09-20 16:51:35 +00002732 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002733}
2734
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002735llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 // Handle qualifiers, which recursively handles what they refer to.
2737 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002738 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002739
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 // Work out details of type.
2741 switch (Ty->getTypeClass()) {
2742#define TYPE(Class, Base)
2743#define ABSTRACT_TYPE(Class, Base)
2744#define NON_CANONICAL_TYPE(Class, Base)
2745#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2746#include "clang/AST/TypeNodes.def"
2747 llvm_unreachable("Dependent types cannot show up in debug information");
2748
2749 case Type::ExtVector:
2750 case Type::Vector:
2751 return CreateType(cast<VectorType>(Ty), Unit);
2752 case Type::ObjCObjectPointer:
2753 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2754 case Type::ObjCObject:
2755 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002756 case Type::ObjCTypeParam:
2757 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002758 case Type::ObjCInterface:
2759 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2760 case Type::Builtin:
2761 return CreateType(cast<BuiltinType>(Ty));
2762 case Type::Complex:
2763 return CreateType(cast<ComplexType>(Ty));
2764 case Type::Pointer:
2765 return CreateType(cast<PointerType>(Ty), Unit);
2766 case Type::BlockPointer:
2767 return CreateType(cast<BlockPointerType>(Ty), Unit);
2768 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002769 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002771 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002773 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 case Type::FunctionProto:
2775 case Type::FunctionNoProto:
2776 return CreateType(cast<FunctionType>(Ty), Unit);
2777 case Type::ConstantArray:
2778 case Type::VariableArray:
2779 case Type::IncompleteArray:
2780 return CreateType(cast<ArrayType>(Ty), Unit);
2781
2782 case Type::LValueReference:
2783 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2784 case Type::RValueReference:
2785 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2786
2787 case Type::MemberPointer:
2788 return CreateType(cast<MemberPointerType>(Ty), Unit);
2789
2790 case Type::Atomic:
2791 return CreateType(cast<AtomicType>(Ty), Unit);
2792
Xiuli Pan9c14e282016-01-09 12:53:17 +00002793 case Type::Pipe:
2794 return CreateType(cast<PipeType>(Ty), Unit);
2795
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002797 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2798
David Blaikie42edade2014-11-11 20:44:45 +00002799 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002800 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002801 case Type::Adjusted:
2802 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002803 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002804 case Type::Elaborated:
2805 case Type::Paren:
2806 case Type::SubstTemplateTypeParm:
2807 case Type::TypeOfExpr:
2808 case Type::TypeOf:
2809 case Type::Decltype:
2810 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002811 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002812 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002814
David Blaikie42edade2014-11-11 20:44:45 +00002815 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002816}
2817
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002818llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2819 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002820 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002821
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002822 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002823
2824 // We may have cached a forward decl when we could have created
2825 // a non-forward decl. Go ahead and create a non-forward decl
2826 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002827 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002828 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002829
2830 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002831 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002832
2833 // Propagate members from the declaration to the definition
2834 // CreateType(const RecordType*) will overwrite this with the members in the
2835 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002836 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002837
Guy Benyei11169dd2012-12-18 14:30:41 +00002838 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002839 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 return Res;
2841}
2842
2843// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002844llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002846
Guy Benyei11169dd2012-12-18 14:30:41 +00002847 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002848 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 unsigned Line = getLineNumber(RD->getLocation());
2850 StringRef RDName = getClassName(RD);
2851
Amjad Abouddc4531e2016-04-30 01:44:38 +00002852 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002853
David Blaikied2785892013-08-18 17:36:19 +00002854 // If we ended up creating the type during the context chain construction,
2855 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002856 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002857 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002858 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002859 return T;
David Blaikied2785892013-08-18 17:36:19 +00002860
Adrian Prantl381e7552014-02-04 21:29:50 +00002861 // If this is just a forward or incomplete declaration, construct an
2862 // appropriately marked node and just return it.
2863 const RecordDecl *D = RD->getDefinition();
2864 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002865 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002866
2867 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002868 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002869
Brock Wyma8557ec52018-05-22 12:41:19 +00002870 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002871
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002872 // Explicitly record the calling convention for C++ records.
2873 auto Flags = llvm::DINode::FlagZero;
2874 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2875 if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect)
2876 Flags |= llvm::DINode::FlagTypePassByReference;
2877 else
2878 Flags |= llvm::DINode::FlagTypePassByValue;
2879 }
2880
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002881 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002882 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +00002883 Flags, Identifier);
Guy Benyei11169dd2012-12-18 14:30:41 +00002884
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002885 // Elements of composite types usually have back to the type, creating
2886 // uniquing cycles. Distinct nodes are more efficient.
2887 switch (RealDecl->getTag()) {
2888 default:
2889 llvm_unreachable("invalid composite type tag");
2890
2891 case llvm::dwarf::DW_TAG_array_type:
2892 case llvm::dwarf::DW_TAG_enumeration_type:
2893 // Array elements and most enumeration elements don't have back references,
2894 // so they don't tend to be involved in uniquing cycles and there is some
2895 // chance of merging them when linking together two modules. Only make
2896 // them distinct if they are ODR-uniqued.
Brock Wyma8557ec52018-05-22 12:41:19 +00002897 if (Identifier.empty())
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002898 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002899 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002900
2901 case llvm::dwarf::DW_TAG_structure_type:
2902 case llvm::dwarf::DW_TAG_union_type:
2903 case llvm::dwarf::DW_TAG_class_type:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002904 // Immediately resolve to a distinct node.
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002905 RealDecl =
2906 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2907 break;
2908 }
2909
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002910 RegionMap[Ty->getDecl()].reset(RealDecl);
2911 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002912
David Majnemer58ed0f32016-07-17 00:39:12 +00002913 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002914 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002915 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002916 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002917}
2918
David Blaikieadfbf992013-08-18 16:55:33 +00002919void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002920 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002921 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002922 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002923 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2924 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002925 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002926 while (1) {
2927 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2928 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2929 if (PBT && !BRL.isPrimaryBaseVirtual())
2930 PBase = PBT;
2931 else
2932 break;
2933 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002934 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002935 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2936 getOrCreateFile(RD->getLocation())));
2937 } else if (RD->isDynamicClass())
2938 ContainingType = RealDecl;
2939
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002940 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002941}
2942
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002943llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002944 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002945 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002947 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002948 llvm::DIType *Ty =
2949 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2950 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 *Offset += FieldSize;
2952 return Ty;
2953}
2954
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002955void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002956 StringRef &Name,
2957 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002958 llvm::DIScope *&FDContext,
2959 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002960 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002961 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002962 Name = getFunctionName(FD);
2963 // Use mangled name as linkage name for C/C++ functions.
2964 if (FD->hasPrototype()) {
2965 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002966 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002967 }
2968 // No need to replicate the linkage name if it isn't different from the
2969 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002970 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002971 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2972 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002973 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002974 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002975 LinkageName = StringRef();
2976
Benjamin Kramer8c305922016-02-02 11:06:51 +00002977 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002978 if (const NamespaceDecl *NSDecl =
Adrian Prantl6fc88752017-05-16 23:46:10 +00002979 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00002980 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00002981 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002982 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2983 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2984 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2985 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002986 // Check if it is a noreturn-marked function
2987 if (FD->isNoReturn())
2988 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002989 // Collect template parameters.
2990 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2991 }
2992}
2993
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002994void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002995 unsigned &LineNo, QualType &T,
2996 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002997 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002998 Unit = getOrCreateFile(VD->getLocation());
2999 LineNo = getLineNumber(VD->getLocation());
3000
3001 setLocation(VD->getLocation());
3002
3003 T = VD->getType();
3004 if (T->isIncompleteArrayType()) {
3005 // CodeGen turns int[] into int[1] so we'll do the same here.
3006 llvm::APInt ConstVal(32, 1);
3007 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3008
3009 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
3010 ArrayType::Normal, 0);
3011 }
3012
3013 Name = VD->getName();
3014 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
3015 !isa<ObjCMethodDecl>(VD->getDeclContext()))
3016 LinkageName = CGM.getMangledName(VD);
3017 if (LinkageName == Name)
3018 LinkageName = StringRef();
3019
3020 // Since we emit declarations (DW_AT_members) for static members, place the
3021 // definition of those static members in the namespace they were declared in
3022 // in the source code (the lexical decl context).
3023 // FIXME: Generalize this for even non-member global variables where the
3024 // declaration and definition may have different lexical decl contexts, once
3025 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003026 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
3027 : VD->getDeclContext();
3028 // When a record type contains an in-line initialization of a static data
3029 // member, and the record type is marked as __declspec(dllexport), an implicit
3030 // definition of the member will be created in the record context. DWARF
3031 // doesn't seem to have a nice way to describe this in a form that consumers
3032 // are likely to understand, so fake the "normal" situation of a definition
3033 // outside the class by putting it in the global scope.
3034 if (DC->isRecord())
3035 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003036
Amjad Abouddc4531e2016-04-30 01:44:38 +00003037 llvm::DIScope *Mod = getParentModuleOrNull(VD);
3038 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00003039}
3040
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003041llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
3042 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003043 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00003044 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003045 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003046 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003047 llvm::DIFile *Unit = getOrCreateFile(Loc);
3048 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003049 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003050 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00003051 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003052 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
3053
Frederic Rissd253ed62014-11-18 03:40:51 +00003054 // Build function type.
3055 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003056 if (FD)
3057 for (const ParmVarDecl *Parm : FD->parameters())
3058 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00003059 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
3060 QualType FnType = CGM.getContext().getFunctionType(
3061 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003062 if (Stub) {
3063 return DBuilder.createFunction(
3064 DContext, Name, LinkageName, Unit, Line,
3065 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3066 !FD->isExternallyVisible(),
3067 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
3068 TParamsArray.get(), getFunctionDeclaration(FD));
3069 }
3070
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003071 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003072 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003073 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3074 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003075 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003076 TParamsArray.get(), getFunctionDeclaration(FD));
George Burgess IV00f70bd2018-03-01 05:43:23 +00003077 const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003078 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3079 std::make_tuple(CanonDecl),
3080 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003081 return SP;
3082}
3083
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003084llvm::DISubprogram *
3085CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
3086 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3087}
3088
3089llvm::DISubprogram *
3090CGDebugInfo::getFunctionStub(GlobalDecl GD) {
3091 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3092}
3093
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003094llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003095CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3096 QualType T;
3097 StringRef Name, LinkageName;
3098 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003099 llvm::DIFile *Unit = getOrCreateFile(Loc);
3100 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003101 unsigned Line = getLineNumber(Loc);
3102
3103 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003104 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003105 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3106 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003107 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003108 FwdDeclReplaceMap.emplace_back(
3109 std::piecewise_construct,
3110 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3111 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003112 return GV;
3113}
3114
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003115llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003116 // We only need a declaration (not a definition) of the type - so use whatever
3117 // we would otherwise do to get a type for a pointee. (forward declarations in
3118 // limited debug info, full definitions (if the type definition is available)
3119 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003120 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003121 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003122 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003123 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003124
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003125 if (I != DeclCache.end()) {
3126 auto N = I->second;
3127 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3128 return GVE->getVariable();
3129 return dyn_cast_or_null<llvm::DINode>(N);
3130 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003131
3132 // No definition for now. Emit a forward definition that might be
3133 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003134 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003135 return getFunctionForwardDeclaration(FD);
3136 else if (const auto *VD = dyn_cast<VarDecl>(D))
3137 return getGlobalVariableForwardDeclaration(VD);
3138
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003139 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003140}
3141
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003142llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003143 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003144 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003145
David Majnemer58ed0f32016-07-17 00:39:12 +00003146 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003147 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003148 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003149
3150 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003151 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003152
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003153 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003154 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003155 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003156 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003157 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003158 }
3159 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003160 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003161 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003162 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 return SP;
3164 }
3165
Aaron Ballman86c93902014-03-06 23:45:36 +00003166 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003167 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003168 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003170 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003171 return SP;
3172 }
3173 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003174 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003175}
3176
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003177// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003178// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003179llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003180 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003181 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003182 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003183 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3184 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003185 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003186
David Majnemer58ed0f32016-07-17 00:39:12 +00003187 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003189
3190 const auto *FTy = FnType->getAs<FunctionType>();
3191 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3192
David Majnemer58ed0f32016-07-17 00:39:12 +00003193 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003194 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003195 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003196
3197 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003198 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003199
3200 // Replace the instancetype keyword with the actual type.
3201 if (ResultTy == CGM.getContext().getObjCInstanceType())
3202 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003203 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003204
Adrian Prantl7bec9032013-05-10 21:08:31 +00003205 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003206 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003207 QualType SelfDeclTy;
3208 if (auto *SelfDecl = OMethod->getSelfDecl())
3209 SelfDeclTy = SelfDecl->getType();
3210 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3211 if (FPT->getNumParams() > 1)
3212 SelfDeclTy = FPT->getParamType(0);
3213 if (!SelfDeclTy.isNull())
3214 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003215 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003216 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003217 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003219 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003220 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003221 // Variadic methods need a special marker at the end of the type list.
3222 if (OMethod->isVariadic())
3223 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003224
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003225 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003226 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3227 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003228 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003229
Adrian Prantl800faef2014-02-25 23:42:18 +00003230 // Handle variadic function types; they need an additional
3231 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003232 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003233 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003234 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003235 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003236 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3237 for (QualType ParamType : FPT->param_types())
3238 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003239 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003240 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003241 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3242 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003243 }
3244
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003245 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003246}
3247
Eric Christophere7b87e52014-10-26 23:40:33 +00003248void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3249 SourceLocation ScopeLoc, QualType FnType,
Brock Wyma94ece8f2018-04-16 16:53:57 +00003250 llvm::Function *Fn, bool CurFuncIsThunk,
3251 CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003252
3253 StringRef Name;
3254 StringRef LinkageName;
3255
3256 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3257
3258 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003259 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003260
Leny Kholodov80c047d2016-09-06 10:48:04 +00003261 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003262 llvm::DIFile *Unit = getOrCreateFile(Loc);
3263 llvm::DIScope *FDContext = Unit;
3264 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003265 if (!HasDecl) {
3266 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003267 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003268 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003269 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003270 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003271 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003272 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003273 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003274 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003275 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003276 return;
3277 }
3278 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003279 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3280 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003281 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003282 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003283 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003284 } else {
3285 // Use llvm function name.
3286 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003287 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003289 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003290 Name = Name.substr(1);
3291
Erich Keane293a0552018-02-14 00:14:07 +00003292 if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003293 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003294 // Artificial functions should not silently reuse CurLoc.
3295 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003296 }
Brock Wyma94ece8f2018-04-16 16:53:57 +00003297
3298 if (CurFuncIsThunk)
3299 Flags |= llvm::DINode::FlagThunk;
3300
Adrian Prantl42d71b92014-04-10 23:21:53 +00003301 unsigned LineNo = getLineNumber(Loc);
3302 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003303
Eric Christopher8018e412014-03-27 18:50:35 +00003304 // FIXME: The function declaration we're constructing here is mostly reusing
3305 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3306 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3307 // all subprograms instead of the actual context since subprogram definitions
3308 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003309 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003310 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003311 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003312 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003313 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003314 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003315 // We might get here with a VarDecl in the case we're generating
3316 // code for the initialization of globals. Do not record these decls
3317 // as they will overwrite the actual VarDecl Decl in the cache.
3318 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003319 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003320
Adrian Prantlbebb8932014-03-21 21:01:58 +00003321 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003322 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003323
Guy Benyei11169dd2012-12-18 14:30:41 +00003324 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003325 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003326}
3327
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003328void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3329 QualType FnType) {
3330 StringRef Name;
3331 StringRef LinkageName;
3332
3333 const Decl *D = GD.getDecl();
3334 if (!D)
3335 return;
3336
Leny Kholodov80c047d2016-09-06 10:48:04 +00003337 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003338 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003339 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003340 llvm::DINodeArray TParamsArray;
3341 if (isa<FunctionDecl>(D)) {
3342 // If there is a DISubprogram for this function available then use it.
3343 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3344 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003345 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003346 Name = getObjCMethodName(OMD);
3347 Flags |= llvm::DINode::FlagPrototyped;
3348 } else {
3349 llvm_unreachable("not a function or ObjC method");
3350 }
3351 if (!Name.empty() && Name[0] == '\01')
3352 Name = Name.substr(1);
3353
3354 if (D->isImplicit()) {
3355 Flags |= llvm::DINode::FlagArtificial;
3356 // Artificial functions without a location should not silently reuse CurLoc.
3357 if (Loc.isInvalid())
3358 CurLoc = SourceLocation();
3359 }
3360 unsigned LineNo = getLineNumber(Loc);
3361 unsigned ScopeLine = 0;
3362
Adrian Prantle76bda52016-04-15 15:55:45 +00003363 DBuilder.retainType(DBuilder.createFunction(
3364 FDContext, Name, LinkageName, Unit, LineNo,
3365 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3366 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3367 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003368}
3369
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003370void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3371 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3372 // If there is a subprogram for this function available then use it.
3373 auto FI = SPCache.find(FD->getCanonicalDecl());
3374 llvm::DISubprogram *SP = nullptr;
3375 if (FI != SPCache.end())
3376 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003377 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003378 SP = getFunctionStub(GD);
3379 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3380 LexicalBlockStack.emplace_back(SP);
3381 setInlinedAt(Builder.getCurrentDebugLocation());
3382 EmitLocation(Builder, FD->getLocation());
3383}
3384
3385void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3386 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003387 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003388 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3389}
3390
David Blaikie835afb22015-01-21 23:08:17 +00003391void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003392 // Update our current location
3393 setLocation(Loc);
3394
Eric Christophere7b87e52014-10-26 23:40:33 +00003395 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3396 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003397
Adrian Prantle83b1302014-01-07 22:05:52 +00003398 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003399 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003400 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003401}
3402
Guy Benyei11169dd2012-12-18 14:30:41 +00003403void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003404 llvm::MDNode *Back = nullptr;
3405 if (!LexicalBlockStack.empty())
3406 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003407 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003408 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003409 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003410}
3411
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003412void CGDebugInfo::AppendAddressSpaceXDeref(
3413 unsigned AddressSpace,
3414 SmallVectorImpl<int64_t> &Expr) const {
3415 Optional<unsigned> DWARFAddressSpace =
3416 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3417 if (!DWARFAddressSpace)
3418 return;
3419
3420 Expr.push_back(llvm::dwarf::DW_OP_constu);
3421 Expr.push_back(DWARFAddressSpace.getValue());
3422 Expr.push_back(llvm::dwarf::DW_OP_swap);
3423 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3424}
3425
Eric Christopher0fdcb312013-05-16 00:52:20 +00003426void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3427 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 // Set our current location.
3429 setLocation(Loc);
3430
Guy Benyei11169dd2012-12-18 14:30:41 +00003431 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003432 Builder.SetCurrentDebugLocation(
3433 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3434 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003435
Benjamin Kramer8c305922016-02-02 11:06:51 +00003436 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003437 return;
3438
3439 // Create a new lexical block and push it on the stack.
3440 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003441}
3442
Eric Christopher0fdcb312013-05-16 00:52:20 +00003443void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3444 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003445 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3446
3447 // Provide an entry in the line table for the end of the block.
3448 EmitLocation(Builder, Loc);
3449
Benjamin Kramer8c305922016-02-02 11:06:51 +00003450 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003451 return;
3452
Guy Benyei11169dd2012-12-18 14:30:41 +00003453 LexicalBlockStack.pop_back();
3454}
3455
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003456void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3458 unsigned RCount = FnBeginRegionCount.back();
3459 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3460
3461 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003462 while (LexicalBlockStack.size() != RCount) {
3463 // Provide an entry in the line table for the end of the block.
3464 EmitLocation(Builder, CurLoc);
3465 LexicalBlockStack.pop_back();
3466 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003468
3469 if (Fn && Fn->getSubprogram())
3470 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003471}
3472
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003473llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003474 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003475
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003476 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003477 QualType FType;
3478 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003479 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003480
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003481 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003482 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003483
3484 FieldOffset = 0;
3485 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3486 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3487 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3488 FType = CGM.getContext().IntTy;
3489 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3490 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3491
3492 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3493 if (HasCopyAndDispose) {
3494 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003495 EltTys.push_back(
3496 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3497 EltTys.push_back(
3498 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 }
3500 bool HasByrefExtendedLayout;
3501 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003502 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3503 HasByrefExtendedLayout) &&
3504 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003505 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003506 EltTys.push_back(
3507 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003508 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003509
Guy Benyei11169dd2012-12-18 14:30:41 +00003510 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3511 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003512 CGM.getTarget().getPointerAlign(0))) {
3513 CharUnits FieldOffsetInBytes =
3514 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003515 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003516 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003517
Guy Benyei11169dd2012-12-18 14:30:41 +00003518 if (NumPaddingBytes.isPositive()) {
3519 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3520 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3521 pad, ArrayType::Normal, 0);
3522 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3523 }
3524 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003525
Guy Benyei11169dd2012-12-18 14:30:41 +00003526 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003527 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003528 FieldSize = CGM.getContext().getTypeSize(FType);
3529 FieldAlign = CGM.getContext().toBits(Align);
3530
Eric Christopherb2a008c2013-05-16 00:45:12 +00003531 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003532 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003533 FieldAlign, FieldOffset,
3534 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003535 EltTys.push_back(FieldTy);
3536 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003537
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003538 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003539
Leny Kholodov80c047d2016-09-06 10:48:04 +00003540 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003541
Guy Benyei11169dd2012-12-18 14:30:41 +00003542 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003543 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003544}
3545
Sander de Smalen891af03a2018-02-03 13:55:59 +00003546llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
3547 llvm::Value *Storage,
3548 llvm::Optional<unsigned> ArgNo,
3549 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003550 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003551 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003552 if (VD->hasAttr<NoDebugAttr>())
Sander de Smalen891af03a2018-02-03 13:55:59 +00003553 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003554
David Blaikie7fceebf2013-08-19 03:37:48 +00003555 bool Unwritten =
3556 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3557 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003558 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003559 if (!Unwritten)
3560 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003561 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 uint64_t XOffset = 0;
3563 if (VD->hasAttr<BlocksAttr>())
3564 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003565 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003566 Ty = getOrCreateType(VD->getType(), Unit);
3567
3568 // If there is no debug info for this type then do not emit debug info
3569 // for this variable.
3570 if (!Ty)
Sander de Smalen891af03a2018-02-03 13:55:59 +00003571 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003572
Guy Benyei11169dd2012-12-18 14:30:41 +00003573 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003574 unsigned Line = 0;
3575 unsigned Column = 0;
3576 if (!Unwritten) {
3577 Line = getLineNumber(VD->getLocation());
3578 Column = getColumnNumber(VD->getLocation());
3579 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003580 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003581 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003582 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003583 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003584
3585 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3586
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003587 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3588 AppendAddressSpaceXDeref(AddressSpace, Expr);
3589
Alexey Bataev24f710182017-06-09 13:55:08 +00003590 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3591 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003592 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3593 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3594 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3595 Flags |= llvm::DINode::FlagObjectPointer;
3596 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003597
Adrian Prantlc3782a12017-04-18 01:22:01 +00003598 // Note: Older versions of clang used to emit byval references with an extra
3599 // DW_OP_deref, because they referenced the IR arg directly instead of
3600 // referencing an alloca. Newer versions of LLVM don't treat allocas
3601 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003602 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003603 StringRef Name = VD->getName();
3604 if (!Name.empty()) {
3605 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003606 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003607 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003608 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003609 // offset of __forwarding field
3610 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003611 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003612 Expr.push_back(offset.getQuantity());
3613 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003614 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 // offset of x field
3616 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003617 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003618 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003619 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003620 // If VD is an anonymous union then Storage represents value for
3621 // all union fields.
George Burgess IV00f70bd2018-03-01 05:43:23 +00003622 const RecordDecl *RD = RT->getDecl();
Adrian Prantl0d820892015-04-29 15:05:50 +00003623 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003624 // GDB has trouble finding local variables in anonymous unions, so we emit
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003625 // artificial local variables for each of the members.
Adrian Prantl00820ab2015-04-29 16:52:31 +00003626 //
3627 // FIXME: Remove this code as soon as GDB supports this.
3628 // The debug info verifier in LLVM operates based on the assumption that a
3629 // variable has the same size as its storage and we had to disable the check
3630 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003631 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003632 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003633 StringRef FieldName = Field->getName();
3634
3635 // Ignore unnamed fields. Do not ignore unnamed records.
3636 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3637 continue;
3638
3639 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003640 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003641 auto *D = DBuilder.createAutoVariable(
3642 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003643 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003644
3645 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003646 DBuilder.insertDeclare(
3647 Storage, D, DBuilder.createExpression(Expr),
3648 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3649 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003650 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003651 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003652 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003653
3654 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003655 auto *D = ArgNo
3656 ? DBuilder.createParameterVariable(
3657 Scope, Name, *ArgNo, Unit, Line, Ty,
3658 CGM.getLangOpts().Optimize, Flags)
3659 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3660 CGM.getLangOpts().Optimize, Flags,
3661 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003662
3663 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003664 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003665 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003666 Builder.GetInsertBlock());
Sander de Smalen891af03a2018-02-03 13:55:59 +00003667
3668 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00003669}
3670
Sander de Smalen891af03a2018-02-03 13:55:59 +00003671llvm::DILocalVariable *
3672CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage,
3673 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003674 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Sander de Smalen891af03a2018-02-03 13:55:59 +00003675 return EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003676}
3677
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003678llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3679 llvm::DIType *Ty) {
3680 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003681 if (CachedTy)
3682 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003683 return DBuilder.createObjectPointerType(Ty);
3684}
3685
Eric Christophere7b87e52014-10-26 23:40:33 +00003686void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3687 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003688 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003689 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003690 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003691
Craig Topper8a13c412014-05-21 05:09:00 +00003692 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003693 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003694 if (VD->hasAttr<NoDebugAttr>())
3695 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003696
Guy Benyei11169dd2012-12-18 14:30:41 +00003697 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003698
Guy Benyei11169dd2012-12-18 14:30:41 +00003699 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003700 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3701 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 if (isByRef)
3703 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003704 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003705 Ty = getOrCreateType(VD->getType(), Unit);
3706
3707 // Self is passed along as an implicit non-arg variable in a
3708 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003709 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3710 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3711 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003712
3713 // Get location information.
3714 unsigned Line = getLineNumber(VD->getLocation());
3715 unsigned Column = getColumnNumber(VD->getLocation());
3716
3717 const llvm::DataLayout &target = CGM.getDataLayout();
3718
3719 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003720 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003721 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3722
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003723 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003724 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003725 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003726 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003727 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003728 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003729 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003730 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003731 offset =
3732 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003733 addr.push_back(offset.getQuantity());
3734 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003735 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003736 // offset of x field
3737 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003738 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003739 }
3740
3741 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003742 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003743 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003744 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003745 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003746
Guy Benyei11169dd2012-12-18 14:30:41 +00003747 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003748 auto DL =
3749 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003750 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003751 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003752 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003753 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003754 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003755}
3756
Guy Benyei11169dd2012-12-18 14:30:41 +00003757void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3758 unsigned ArgNo,
3759 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003760 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003761 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003762}
3763
3764namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003765struct BlockLayoutChunk {
3766 uint64_t OffsetInBits;
3767 const BlockDecl::Capture *Capture;
3768};
3769bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3770 return l.OffsetInBits < r.OffsetInBits;
3771}
Guy Benyei11169dd2012-12-18 14:30:41 +00003772}
3773
3774void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl356347b2017-10-26 20:08:52 +00003775 StringRef Name,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003776 unsigned ArgNo,
Adrian Prantl356347b2017-10-26 20:08:52 +00003777 llvm::AllocaInst *Alloca,
Guy Benyei11169dd2012-12-18 14:30:41 +00003778 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003779 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003780 ASTContext &C = CGM.getContext();
3781 const BlockDecl *blockDecl = block.getBlockDecl();
3782
3783 // Collect some general information about the block's location.
3784 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003785 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003786 unsigned line = getLineNumber(loc);
3787 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003788
Guy Benyei11169dd2012-12-18 14:30:41 +00003789 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003790 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003791
3792 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003793 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003794
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003795 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003796 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003797 blockLayout->getElementOffsetInBits(0),
3798 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003799 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003800 blockLayout->getElementOffsetInBits(1),
3801 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003802 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003803 blockLayout->getElementOffsetInBits(2),
3804 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003805 auto *FnTy = block.getBlockExpr()->getFunctionType();
3806 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003807 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003808 blockLayout->getElementOffsetInBits(3),
3809 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003810 fields.push_back(createFieldType(
3811 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3812 ? C.getBlockDescriptorExtendedType()
3813 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003814 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003815
3816 // We want to sort the captures by offset, not because DWARF
3817 // requires this, but because we're paranoid about debuggers.
3818 SmallVector<BlockLayoutChunk, 8> chunks;
3819
3820 // 'this' capture.
3821 if (blockDecl->capturesCXXThis()) {
3822 BlockLayoutChunk chunk;
3823 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003824 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003825 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003826 chunks.push_back(chunk);
3827 }
3828
3829 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003830 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003831 const VarDecl *variable = capture.getVariable();
3832 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3833
3834 // Ignore constant captures.
3835 if (captureInfo.isConstant())
3836 continue;
3837
3838 BlockLayoutChunk chunk;
3839 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003840 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003841 chunk.Capture = &capture;
3842 chunks.push_back(chunk);
3843 }
3844
3845 // Sort by offset.
3846 llvm::array_pod_sort(chunks.begin(), chunks.end());
3847
David Majnemer58ed0f32016-07-17 00:39:12 +00003848 for (const BlockLayoutChunk &Chunk : chunks) {
3849 uint64_t offsetInBits = Chunk.OffsetInBits;
3850 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003851
3852 // If we have a null capture, this must be the C++ 'this' capture.
3853 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003854 QualType type;
3855 if (auto *Method =
3856 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3857 type = Method->getThisType(C);
3858 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3859 type = QualType(RDecl->getTypeForDecl(), 0);
3860 else
3861 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003862
David Majnemerb4b671e2016-06-30 03:01:59 +00003863 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003864 offsetInBits, tunit, tunit));
3865 continue;
3866 }
3867
3868 const VarDecl *variable = capture->getVariable();
3869 StringRef name = variable->getName();
3870
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003871 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003872 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003873 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003874 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003875
3876 // FIXME: this creates a second copy of this type!
3877 uint64_t xoffset;
3878 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003879 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003880 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3881 PtrInfo.Width, Align, offsetInBits,
3882 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003883 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003884 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003885 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003886 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003887 }
3888 fields.push_back(fieldType);
3889 }
3890
3891 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003892 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3893 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003894
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003895 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003896
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003897 llvm::DIType *type =
3898 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003899 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003900 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003901 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3902
3903 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003904 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003905 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003906
3907 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003908 auto *debugVar = DBuilder.createParameterVariable(
Adrian Prantl356347b2017-10-26 20:08:52 +00003909 scope, Name, ArgNo, tunit, line, type,
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003910 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003911
Adrian Prantl616bef42013-03-14 21:52:59 +00003912 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl356347b2017-10-26 20:08:52 +00003913 DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003914 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003915 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003916}
3917
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003918llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003919CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3920 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003921 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003922
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003923 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003924 if (MI != StaticDataMemberCache.end()) {
3925 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003926 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003927 }
David Blaikiece763042013-08-20 21:49:21 +00003928
3929 // If the member wasn't found in the cache, lazily construct and add it to the
3930 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003931 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003932 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003933 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003934}
3935
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003936llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003937 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3938 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003939 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003940
3941 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003942 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003943 StringRef FieldName = Field->getName();
3944
3945 // Ignore unnamed fields, but recurse into anonymous records.
3946 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003947 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003948 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003949 Var, DContext);
3950 continue;
3951 }
3952 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003953 GVE = DBuilder.createGlobalVariableExpression(
3954 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3955 Var->hasLocalLinkage());
3956 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003957 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003958 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003959}
3960
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003961void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003962 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003963 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003964 if (D->hasAttr<NoDebugAttr>())
3965 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003966
3967 // If we already created a DIGlobalVariable for this declaration, just attach
3968 // it to the llvm::GlobalVariable.
3969 auto Cached = DeclCache.find(D->getCanonicalDecl());
3970 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003971 return Var->addDebugInfo(
3972 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003973
Guy Benyei11169dd2012-12-18 14:30:41 +00003974 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003975 llvm::DIFile *Unit = nullptr;
3976 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003977 unsigned LineNo;
3978 StringRef DeclName, LinkageName;
3979 QualType T;
3980 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003981
3982 // Attempt to store one global variable for the declaration - even if we
3983 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003984 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003985
3986 // If this is an anonymous union then we'll want to emit a global
3987 // variable for each member of the anonymous union so that it's possible
3988 // to find the name of any field in the union.
3989 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003990 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003991 assert(RD->isAnonymousStructOrUnion() &&
3992 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003993 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003994 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003995 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003996
3997 SmallVector<int64_t, 4> Expr;
3998 unsigned AddressSpace =
3999 CGM.getContext().getTargetAddressSpace(D->getType());
4000 AppendAddressSpaceXDeref(AddressSpace, Expr);
4001
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004002 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00004003 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004004 Var->hasLocalLinkage(),
4005 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00004006 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004007 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004008 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004009 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004010}
4011
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004012void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004013 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004014 if (VD->hasAttr<NoDebugAttr>())
4015 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00004016 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004017 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004018 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004019 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004020 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00004021 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
4022 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004023 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
4024 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
4025 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004026 // Do not use global variables for enums.
4027 //
4028 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004029 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004030 return;
David Blaikiea15565562014-04-04 20:56:17 +00004031 // Do not emit separate definitions for function local const/statics.
4032 if (isa<FunctionDecl>(VD->getDeclContext()))
4033 return;
David Blaikiebb113912014-04-05 07:23:17 +00004034 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00004035 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004036 if (VarD->isStaticDataMember()) {
4037 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004038 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00004039 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00004040 //
4041 // FIXME: This is probably unnecessary, since Ty should reference RD
4042 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00004043 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00004044 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00004045 return;
4046 }
4047
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004048 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004049
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004050 auto &GV = DeclCache[VD];
4051 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00004052 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004053 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00004054 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
4055 // FIXME: Add a representation for integer constants wider than 64 bits.
4056 if (Init.isInt())
4057 InitExpr =
4058 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
4059 else if (Init.isFloat())
4060 InitExpr = DBuilder.createConstantValueExpression(
4061 Init.getFloat().bitcastToAPInt().getZExtValue());
4062 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004063 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00004064 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00004065 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
4066 Align));
David Blaikiebd483762013-05-20 04:58:53 +00004067}
4068
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004069llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00004070 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00004071 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00004072 llvm::DIScope *Mod = getParentModuleOrNull(D);
4073 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00004074}
4075
David Blaikie9f88fe82013-04-22 06:13:21 +00004076void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004077 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004078 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004079 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00004080 if (!NSDecl->isAnonymousNamespace() ||
4081 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004082 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004083 DBuilder.createImportedModule(
4084 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004085 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004086 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004087}
4088
David Blaikiebd483762013-05-20 04:58:53 +00004089void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004090 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004091 return;
4092 assert(UD.shadow_size() &&
4093 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4094 // Emitting one decl is sufficient - debuggers can detect that this is an
4095 // overloaded name & provide lookup for all the overloads.
4096 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004097
4098 // FIXME: Skip functions with undeduced auto return type for now since we
4099 // don't currently have the plumbing for separate declarations & definitions
4100 // of free functions and mismatched types (auto in the declaration, concrete
4101 // return type in the definition)
4102 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4103 if (const auto *AT =
4104 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4105 if (AT->getDeducedType().isNull())
4106 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004107 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004108 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4109 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004110 DBuilder.createImportedDeclaration(
4111 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004112 getOrCreateFile(Loc), getLineNumber(Loc));
4113 }
David Blaikiebd483762013-05-20 04:58:53 +00004114}
4115
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004116void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004117 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4118 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004119 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004120 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004121 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004122 DBuilder.createImportedDeclaration(
4123 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004124 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4125 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004126 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004127}
4128
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004129llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004130CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004131 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004132 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004133 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004134 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004135 return cast<llvm::DIImportedEntity>(VH);
4136 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004137 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004138 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004139 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4140 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004141 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004142 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004143 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4144 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004145 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004146 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004147 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004148 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004149 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004150 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004151 return R;
4152}
4153
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004154llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004155CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4156 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4157 // if necessary, and this way multiple declarations of the same namespace in
4158 // different parent modules stay distinct.
4159 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004160 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004161 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004162
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004163 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004164 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004165 llvm::DINamespace *NS =
4166 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4167 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004168 return NS;
4169}
4170
Adrian Prantla5206ce2015-09-22 23:26:43 +00004171void CGDebugInfo::setDwoId(uint64_t Signature) {
4172 assert(TheCU && "no main compile unit");
4173 TheCU->setDWOId(Signature);
4174}
4175
4176
Guy Benyei11169dd2012-12-18 14:30:41 +00004177void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004178 // Creating types might create further types - invalidating the current
4179 // element and the size(), so don't cache/reference them.
4180 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4181 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004182 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004183 ? CreateTypeDefinition(E.Type, E.Unit)
4184 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004185 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004186 }
4187
David Blaikief427b002014-05-06 03:42:01 +00004188 for (auto p : ReplaceMap) {
4189 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004190 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004191 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004192
David Blaikief427b002014-05-06 03:42:01 +00004193 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004194 assert(it != TypeCache.end());
4195 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004196
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004197 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4198 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004199 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004200
Frederic Rissd253ed62014-11-18 03:40:51 +00004201 for (const auto &p : FwdDeclReplaceMap) {
4202 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004203 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004204 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004205
4206 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004207 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004208 // with ourselves, that will destroy the temporary MDNode and
4209 // replace it with a standard one, avoiding leaking memory.
4210 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004211 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004212 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004213 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004214
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004215 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4216 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004217 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004218 }
4219
Adrian Prantl73409ce2013-03-11 18:33:46 +00004220 // We keep our own list of retained types, because we need to look
4221 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004222 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004223 if (auto MD = TypeCache[RT])
4224 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004225
Guy Benyei11169dd2012-12-18 14:30:41 +00004226 DBuilder.finalize();
4227}
David Blaikie66088d52014-09-24 17:01:27 +00004228
4229void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004230 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004231 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004232
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004233 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004234 // Don't ignore in case of explicit cast where it is referenced indirectly.
4235 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004236}
Amara Emerson652795d2016-11-10 14:44:30 +00004237
4238llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4239 if (LexicalBlockStack.empty())
4240 return llvm::DebugLoc();
4241
4242 llvm::MDNode *Scope = LexicalBlockStack.back();
4243 return llvm::DebugLoc::get(
4244 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4245}