blob: 24c76c2037fd64c8d931bbd3366093403efc6eea [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"
Matthew Voss20165362018-10-03 18:45:04 +000044#include "llvm/IR/Metadata.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000045#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000047#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000048#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000049using namespace clang;
50using namespace clang::CodeGen;
51
Victor Leschuka7ece032016-10-20 00:13:19 +000052static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
53 auto TI = Ctx.getTypeInfo(Ty);
54 return TI.AlignIsRequired ? TI.Align : 0;
55}
56
57static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
58 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
59}
60
61static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
62 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
63}
64
Guy Benyei11169dd2012-12-18 14:30:41 +000065CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000066 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000067 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000068 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000069 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
70 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000071 CreateCompileUnit();
72}
73
74CGDebugInfo::~CGDebugInfo() {
75 assert(LexicalBlockStack.empty() &&
76 "Region stack mismatch, stack not empty!");
77}
78
David Blaikie66e41972015-01-14 07:38:27 +000079ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000080 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000081 : CGF(&CGF) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +000082 init(TemporaryLocation);
David Blaikie9b479662015-01-25 01:19:10 +000083}
84
Adrian Prantl39428e72015-02-03 18:40:42 +000085ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000086 bool DefaultToEmpty,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000087 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000088 : CGF(&CGF) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +000089 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000090}
91
92void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000093 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000094 auto *DI = CGF->getDebugInfo();
95 if (!DI) {
96 CGF = nullptr;
97 return;
David Blaikie66e41972015-01-14 07:38:27 +000098 }
David Blaikied7057d92015-08-12 23:49:57 +000099
100 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
Bob Haarmanc6c9b8f2017-09-11 22:11:57 +0000101
102 if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
103 return;
104
David Blaikied7057d92015-08-12 23:49:57 +0000105 if (TemporaryLocation.isValid()) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +0000106 DI->EmitLocation(CGF->Builder, TemporaryLocation);
David Blaikied7057d92015-08-12 23:49:57 +0000107 return;
108 }
109
110 if (DefaultToEmpty) {
111 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
112 return;
113 }
114
115 // Construct a location that has a valid scope, but no line info.
116 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000117 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
118 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000119}
120
David Blaikie9b479662015-01-25 01:19:10 +0000121ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000122 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000123 init(E->getExprLoc());
124}
125
David Blaikie66e41972015-01-14 07:38:27 +0000126ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000127 : CGF(&CGF) {
128 if (!CGF.getDebugInfo()) {
129 this->CGF = nullptr;
130 return;
David Blaikie66e41972015-01-14 07:38:27 +0000131 }
David Blaikied7057d92015-08-12 23:49:57 +0000132 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
133 if (Loc)
134 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000135}
136
137ApplyDebugLocation::~ApplyDebugLocation() {
138 // Query CGF so the location isn't overwritten when location updates are
139 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000140 if (CGF)
141 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000142}
143
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000144ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
145 GlobalDecl InlinedFn)
146 : CGF(&CGF) {
147 if (!CGF.getDebugInfo()) {
148 this->CGF = nullptr;
149 return;
150 }
151 auto &DI = *CGF.getDebugInfo();
152 SavedLocation = DI.getLocation();
153 assert((DI.getInlinedAt() ==
154 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
155 "CGDebugInfo and IRBuilder are out of sync");
156
157 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
158}
159
160ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
161 if (!CGF)
162 return;
163 auto &DI = *CGF->getDebugInfo();
164 DI.EmitInlineFunctionEnd(CGF->Builder);
165 DI.EmitLocation(CGF->Builder, SavedLocation);
166}
167
Guy Benyei11169dd2012-12-18 14:30:41 +0000168void CGDebugInfo::setLocation(SourceLocation Loc) {
169 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000170 if (Loc.isInvalid())
171 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000172
173 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
174
175 // If we've changed files in the middle of a lexical scope go ahead
176 // and create a new lexical scope with file node if it's different
177 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000178 if (LexicalBlockStack.empty())
179 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000180
181 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000182 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000184
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000185 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 return;
187
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000188 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000189 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000190 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
191 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000192 } else if (isa<llvm::DILexicalBlock>(Scope) ||
193 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000194 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000195 LexicalBlockStack.emplace_back(
196 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000197 }
198}
199
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000200llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000201 llvm::DIScope *Mod = getParentModuleOrNull(D);
202 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
203 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000204}
205
206llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
207 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000208 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000209 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000210
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000211 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000212 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000213 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000214 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000215 }
216
217 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000218 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
219 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000220
David Majnemer58ed0f32016-07-17 00:39:12 +0000221 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000222 if (!RDecl->isDependentType())
223 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000225 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000226}
227
Reid Kleckner59d12202017-08-08 01:33:53 +0000228PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
229 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
230
231 // If we're emitting codeview, it's important to try to match MSVC's naming so
232 // that visualizers written for MSVC will trigger for our class names. In
233 // particular, we can't have spaces between arguments of standard templates
234 // like basic_string and vector.
235 if (CGM.getCodeGenOpts().EmitCodeView)
236 PP.MSVCFormatting = true;
237
238 return PP;
239}
240
Guy Benyei11169dd2012-12-18 14:30:41 +0000241StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000242 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000243 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000244 FunctionTemplateSpecializationInfo *Info =
245 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000246
Reid Kleckner31abd802016-06-30 17:41:31 +0000247 // Emit the unqualified name in normal operation. LLVM and the debugger can
248 // compute the fully qualified name from the scope chain. If we're only
249 // emitting line table info, there won't be any scope chains, so emit the
250 // fully qualified name here so that stack traces are more accurate.
251 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
252 // evaluating the size impact.
253 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
254 CGM.getCodeGenOpts().EmitCodeView;
255
256 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000257 return FII->getName();
258
Benjamin Kramer9170e912013-02-22 15:46:01 +0000259 SmallString<128> NS;
260 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000261 if (!UseQualifiedName)
262 FD->printName(OS);
263 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000264 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000265
Reid Kleckner829398e2016-06-17 16:11:20 +0000266 // Add any template specialization args.
267 if (Info) {
268 const TemplateArgumentList *TArgs = Info->TemplateArguments;
Serge Pavlov03e672c2017-11-28 16:14:14 +0000269 printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000270 }
271
272 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000273 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000274}
275
276StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
277 SmallString<256> MethodName;
278 llvm::raw_svector_ostream OS(MethodName);
279 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
280 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000281 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000282 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000283 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000284 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000285 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000286 if (OC->IsClassExtension()) {
287 OS << OC->getClassInterface()->getName();
288 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000289 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000290 << OC->getIdentifier()->getNameStart() << ')';
291 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000292 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000293 OS << OCD->getClassInterface()->getName() << '(' << 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
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000388Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
389 FileID FID) {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000390 if (!CGM.getCodeGenOpts().EmbedSource)
391 return None;
392
393 bool SourceInvalid = false;
394 StringRef Source = SM.getBufferData(FID, &SourceInvalid);
395
396 if (SourceInvalid)
397 return None;
398
399 return Source;
400}
401
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000402llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000403 if (!Loc.isValid())
404 // If Location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000405 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000406
407 SourceManager &SM = CGM.getContext().getSourceManager();
408 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
409
410 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
411 // If the location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000412 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000413
414 // Cache the results.
415 const char *fname = PLoc.getFilename();
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000416 auto It = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000417
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000418 if (It != DIFileCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 // Verify that the information still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000420 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000421 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 }
423
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000424 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000425 Optional<llvm::DIFile::ChecksumKind> CSKind =
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000426 computeChecksum(SM.getFileID(Loc), Checksum);
Scott Linder94834f12018-02-12 19:47:05 +0000427 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
428 if (CSKind)
429 CSInfo.emplace(*CSKind, Checksum);
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000430
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000431 llvm::DIFile *F = DBuilder.createFile(
432 remapDIPath(PLoc.getFilename()), remapDIPath(getCurrentDirname()), 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(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000441 remapDIPath(TheCU->getFilename()), remapDIPath(TheCU->getDirectory()),
Scott Lindera2fbcef2018-02-26 17:32:31 +0000442 TheCU->getFile()->getChecksum(),
443 CGM.getCodeGenOpts().EmbedSource ? TheCU->getSource() : None);
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000444}
445
446std::string CGDebugInfo::remapDIPath(StringRef Path) const {
447 for (const auto &Entry : DebugPrefixMap)
448 if (Path.startswith(Entry.first))
449 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
450 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000451}
452
Guy Benyei11169dd2012-12-18 14:30:41 +0000453unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
454 if (Loc.isInvalid() && CurLoc.isInvalid())
455 return 0;
456 SourceManager &SM = CGM.getContext().getSourceManager();
457 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000459}
460
Adrian Prantlc7822422013-03-12 20:43:25 +0000461unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000462 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000463 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 return 0;
465
466 // If the location is invalid then use the current column.
467 if (Loc.isInvalid() && CurLoc.isInvalid())
468 return 0;
469 SourceManager &SM = CGM.getContext().getSourceManager();
470 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000471 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472}
473
474StringRef CGDebugInfo::getCurrentDirname() {
475 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
476 return CGM.getCodeGenOpts().DebugCompilationDir;
477
478 if (!CWDName.empty())
479 return CWDName;
480 SmallString<256> CWD;
481 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000482 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000483}
484
Guy Benyei11169dd2012-12-18 14:30:41 +0000485void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000486 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000487 Optional<llvm::DIFile::ChecksumKind> CSKind;
488 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000489
David Blaikieaabde052014-05-14 00:29:00 +0000490 // Should we be asking the SourceManager for the main file name, instead of
491 // accepting it as an argument? This just causes the main file name to
492 // mismatch with source locations and create extra lexical scopes or
493 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
494 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
495 // because that's what the SourceManager says)
496
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 // Get absolute path name.
498 SourceManager &SM = CGM.getContext().getSourceManager();
499 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
500 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000501 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000502
503 // The main file name provided via the "-main-file-name" option contains just
504 // the file name itself with no path information. This file name may have had
505 // a relative path, so we look into the actual file entry for the main
506 // file to determine the real absolute path for the file.
507 std::string MainFileDir;
508 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000509 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000510 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000511 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
512 llvm::sys::path::append(MainFileDirSS, MainFileName);
513 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000514 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000515 // If the main file name provided is identical to the input file name, and
516 // if the input file is a preprocessed source, use the module name for
517 // debug info. The module name comes from the name specified in the first
518 // linemarker if the input is a preprocessed source.
519 if (MainFile->getName() == MainFileName &&
520 FrontendOptions::getInputKindForExtension(
521 MainFile->getName().rsplit('.').second)
522 .isPreprocessed())
523 MainFileName = CGM.getModule().getName().str();
524
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000525 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000526 }
527
Ed Masteda706022014-05-07 12:49:30 +0000528 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 const LangOptions &LO = CGM.getLangOpts();
530 if (LO.CPlusPlus) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000531 if (LO.ObjC)
Guy Benyei11169dd2012-12-18 14:30:41 +0000532 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
533 else
534 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000535 } else if (LO.ObjC) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000536 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000537 } else if (LO.RenderScript) {
538 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000539 } else if (LO.C99) {
540 LangTag = llvm::dwarf::DW_LANG_C99;
541 } else {
542 LangTag = llvm::dwarf::DW_LANG_C89;
543 }
544
545 std::string Producer = getClangFullVersion();
546
547 // Figure out which version of the ObjC runtime we have.
548 unsigned RuntimeVers = 0;
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000549 if (LO.ObjC)
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
551
Adrian Prantl826824e2016-04-08 22:43:06 +0000552 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
553 switch (DebugKind) {
554 case codegenoptions::NoDebugInfo:
555 case codegenoptions::LocTrackingOnly:
556 EmissionKind = llvm::DICompileUnit::NoDebug;
557 break;
558 case codegenoptions::DebugLineTablesOnly:
559 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
560 break;
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000561 case codegenoptions::DebugDirectivesOnly:
562 EmissionKind = llvm::DICompileUnit::DebugDirectivesOnly;
563 break;
Adrian Prantl826824e2016-04-08 22:43:06 +0000564 case codegenoptions::LimitedDebugInfo:
565 case codegenoptions::FullDebugInfo:
566 EmissionKind = llvm::DICompileUnit::FullDebug;
567 break;
568 }
569
Scott Linder94834f12018-02-12 19:47:05 +0000570 if (CSKind)
571 CSInfo.emplace(*CSKind, Checksum);
572
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000574 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000575 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000576 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000577 LangTag,
578 DBuilder.createFile(remapDIPath(MainFileName),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000579 remapDIPath(getCurrentDirname()), CSInfo,
Scott Lindera2fbcef2018-02-26 17:32:31 +0000580 getSource(SM, SM.getMainFileID())),
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +0000581 CGOpts.EmitVersionIdentMetadata ? Producer : "",
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000582 LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
Adrian Prantlb4423022017-08-04 23:08:57 +0000583 CGOpts.DwarfDebugFlags, RuntimeVers,
584 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000585 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
David Blaikie9982bb82018-08-16 23:56:32 +0000586 CGM.getTarget().getTriple().isNVPTX()
587 ? llvm::DICompileUnit::DebugNameTableKind::None
David Blaikie65864522018-08-20 20:14:08 +0000588 : static_cast<llvm::DICompileUnit::DebugNameTableKind>(
David Blaikie27692de2018-11-13 20:08:13 +0000589 CGOpts.DebugNameTable),
590 CGOpts.DebugRangesBaseAddress);
Guy Benyei11169dd2012-12-18 14:30:41 +0000591}
592
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000593llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000594 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000595 StringRef BTName;
596 switch (BT->getKind()) {
597#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000598#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000599#include "clang/AST/BuiltinTypes.def"
600 case BuiltinType::Dependent:
601 llvm_unreachable("Unexpected builtin type");
602 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000603 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000604 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000605 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000606 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000607 if (!ClassTy)
608 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
609 "objc_class", TheCU,
610 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000611 return ClassTy;
612 case BuiltinType::ObjCId: {
613 // typedef struct objc_class *Class;
614 // typedef struct objc_object {
615 // Class isa;
616 // } *id;
617
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000618 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000619 return ObjTy;
620
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000621 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000622 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
623 "objc_class", TheCU,
624 getOrCreateMainFile(), 0);
625
626 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000627
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000628 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000629
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000630 ObjTy = DBuilder.createStructType(
631 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
632 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000633
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000634 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000635 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
636 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
637 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000638 return ObjTy;
639 }
640 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000641 if (!SelTy)
642 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
643 "objc_selector", TheCU,
644 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000645 return SelTy;
646 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000647
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000648#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
649 case BuiltinType::Id: \
650 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
Alexey Bader954ba212016-04-08 13:40:33 +0000651 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000652#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000653 case BuiltinType::OCLSampler:
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000654 return getOrCreateStructPtrType("opencl_sampler_t", OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000655 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000656 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000657 case BuiltinType::OCLClkEvent:
658 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
659 case BuiltinType::OCLQueue:
660 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000661 case BuiltinType::OCLReserveID:
662 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000663#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
664 case BuiltinType::Id: \
665 return getOrCreateStructPtrType("opencl_" #ExtType, Id##Ty);
666#include "clang/Basic/OpenCLExtensionTypes.def"
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000667
Guy Benyei11169dd2012-12-18 14:30:41 +0000668 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000669 case BuiltinType::Char_U:
670 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
671 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000673 case BuiltinType::SChar:
674 Encoding = llvm::dwarf::DW_ATE_signed_char;
675 break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000676 case BuiltinType::Char8:
Guy Benyei11169dd2012-12-18 14:30:41 +0000677 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000678 case BuiltinType::Char32:
679 Encoding = llvm::dwarf::DW_ATE_UTF;
680 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000681 case BuiltinType::UShort:
682 case BuiltinType::UInt:
683 case BuiltinType::UInt128:
684 case BuiltinType::ULong:
685 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000686 case BuiltinType::ULongLong:
687 Encoding = llvm::dwarf::DW_ATE_unsigned;
688 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 case BuiltinType::Short:
690 case BuiltinType::Int:
691 case BuiltinType::Int128:
692 case BuiltinType::Long:
693 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000694 case BuiltinType::LongLong:
695 Encoding = llvm::dwarf::DW_ATE_signed;
696 break;
697 case BuiltinType::Bool:
698 Encoding = llvm::dwarf::DW_ATE_boolean;
699 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 case BuiltinType::Half:
701 case BuiltinType::Float:
702 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000703 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000704 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000705 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000706 // FIXME: For targets where long double and __float128 have the same size,
707 // they are currently indistinguishable in the debugger without some
708 // special treatment. However, there is currently no consensus on encoding
709 // and this should be updated once a DWARF encoding exists for distinct
710 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000711 Encoding = llvm::dwarf::DW_ATE_float;
712 break;
Leonard Chanf921d852018-06-04 16:07:52 +0000713 case BuiltinType::ShortAccum:
714 case BuiltinType::Accum:
715 case BuiltinType::LongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000716 case BuiltinType::ShortFract:
717 case BuiltinType::Fract:
718 case BuiltinType::LongFract:
719 case BuiltinType::SatShortFract:
720 case BuiltinType::SatFract:
721 case BuiltinType::SatLongFract:
722 case BuiltinType::SatShortAccum:
723 case BuiltinType::SatAccum:
724 case BuiltinType::SatLongAccum:
Leonard Chanf921d852018-06-04 16:07:52 +0000725 Encoding = llvm::dwarf::DW_ATE_signed_fixed;
726 break;
727 case BuiltinType::UShortAccum:
728 case BuiltinType::UAccum:
729 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000730 case BuiltinType::UShortFract:
731 case BuiltinType::UFract:
732 case BuiltinType::ULongFract:
733 case BuiltinType::SatUShortAccum:
734 case BuiltinType::SatUAccum:
735 case BuiltinType::SatULongAccum:
736 case BuiltinType::SatUShortFract:
737 case BuiltinType::SatUFract:
738 case BuiltinType::SatULongFract:
Leonard Chanf921d852018-06-04 16:07:52 +0000739 Encoding = llvm::dwarf::DW_ATE_unsigned_fixed;
740 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 }
742
743 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000744 case BuiltinType::Long:
745 BTName = "long int";
746 break;
747 case BuiltinType::LongLong:
748 BTName = "long long int";
749 break;
750 case BuiltinType::ULong:
751 BTName = "long unsigned int";
752 break;
753 case BuiltinType::ULongLong:
754 BTName = "long long unsigned int";
755 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000756 default:
757 BTName = BT->getName(CGM.getLangOpts());
758 break;
759 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000760 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000761 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000762 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000763}
764
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000765llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000766 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000767 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 if (Ty->isComplexIntegerType())
769 Encoding = llvm::dwarf::DW_ATE_lo_user;
770
771 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000772 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000773}
774
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000775llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
776 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000777 QualifierCollector Qc;
778 const Type *T = Qc.strip(Ty);
779
780 // Ignore these qualifiers for now.
781 Qc.removeObjCGCAttr();
782 Qc.removeAddressSpace();
783 Qc.removeObjCLifetime();
784
785 // We will create one Derived type for one qualifier and recurse to handle any
786 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000787 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000788 if (Qc.hasConst()) {
789 Tag = llvm::dwarf::DW_TAG_const_type;
790 Qc.removeConst();
791 } else if (Qc.hasVolatile()) {
792 Tag = llvm::dwarf::DW_TAG_volatile_type;
793 Qc.removeVolatile();
794 } else if (Qc.hasRestrict()) {
795 Tag = llvm::dwarf::DW_TAG_restrict_type;
796 Qc.removeRestrict();
797 } else {
798 assert(Qc.empty() && "Unknown type qualifier for debug info");
799 return getOrCreateType(QualType(T, 0), Unit);
800 }
801
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000802 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000803
804 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
805 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000806 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000807}
808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000809llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
810 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000811
812 // The frontend treats 'id' as a typedef to an ObjCObjectType,
813 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
814 // debug info, we want to emit 'id' in both cases.
815 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000816 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000817
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000818 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
819 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000820}
821
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000822llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
823 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000824 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 Ty->getPointeeType(), Unit);
826}
827
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000828/// \return whether a C++ mangling exists for the type defined by TD.
829static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
830 switch (TheCU->getSourceLanguage()) {
831 case llvm::dwarf::DW_LANG_C_plus_plus:
832 return true;
833 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
834 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
835 default:
836 return false;
837 }
838}
839
Reid Kleckner59320fc2018-08-17 20:59:52 +0000840// Determines if the debug info for this tag declaration needs a type
841// identifier. The purpose of the unique identifier is to deduplicate type
842// information for identical types across TUs. Because of the C++ one definition
843// rule (ODR), it is valid to assume that the type is defined the same way in
844// every TU and its debug info is equivalent.
845//
846// C does not have the ODR, and it is common for codebases to contain multiple
847// different definitions of a struct with the same name in different TUs.
848// Therefore, if the type doesn't have a C++ mangling, don't give it an
849// identifer. Type information in C is smaller and simpler than C++ type
850// information, so the increase in debug info size is negligible.
851//
852// If the type is not externally visible, it should be unique to the current TU,
853// and should not need an identifier to participate in type deduplication.
854// However, when emitting CodeView, the format internally uses these
855// unique type name identifers for references between debug info. For example,
856// the method of a class in an anonymous namespace uses the identifer to refer
857// to its parent class. The Microsoft C++ ABI attempts to provide unique names
858// for such types, so when emitting CodeView, always use identifiers for C++
859// types. This may create problems when attempting to emit CodeView when the MS
860// C++ ABI is not in use.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000861static bool needsTypeIdentifier(const TagDecl *TD, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000862 llvm::DICompileUnit *TheCU) {
863 // We only add a type identifier for types with C++ name mangling.
864 if (!hasCXXMangling(TD, TheCU))
865 return false;
866
Brock Wyma8557ec52018-05-22 12:41:19 +0000867 // Externally visible types with C++ mangling need a type identifier.
868 if (TD->isExternallyVisible())
869 return true;
870
Reid Kleckner59320fc2018-08-17 20:59:52 +0000871 // CodeView types with C++ mangling need a type identifier.
872 if (CGM.getCodeGenOpts().EmitCodeView)
873 return true;
874
Brock Wyma8557ec52018-05-22 12:41:19 +0000875 return false;
876}
877
Reid Kleckner59320fc2018-08-17 20:59:52 +0000878// Returns a unique type identifier string if one exists, or an empty string.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000879static SmallString<256> getTypeIdentifier(const TagType *Ty, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000880 llvm::DICompileUnit *TheCU) {
881 SmallString<256> Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000882 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000883
Brock Wyma8557ec52018-05-22 12:41:19 +0000884 if (!needsTypeIdentifier(TD, CGM, TheCU))
885 return Identifier;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000886
Manman Rene0064d82013-08-29 23:19:58 +0000887 // TODO: This is using the RTTI name. Is there a better way to get
888 // a unique string for a type?
Brock Wyma8557ec52018-05-22 12:41:19 +0000889 llvm::raw_svector_ostream Out(Identifier);
Manman Rene0064d82013-08-29 23:19:58 +0000890 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Brock Wyma8557ec52018-05-22 12:41:19 +0000891 return Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000892}
893
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000894/// \return the appropriate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000895static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000896 llvm::dwarf::Tag Tag;
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000897 if (RD->isStruct() || RD->isInterface())
898 Tag = llvm::dwarf::DW_TAG_structure_type;
899 else if (RD->isUnion())
900 Tag = llvm::dwarf::DW_TAG_union_type;
901 else {
902 // FIXME: This could be a struct type giving a default visibility different
903 // than C++ class type, but needs llvm metadata changes first.
904 assert(RD->isClass());
905 Tag = llvm::dwarf::DW_TAG_class_type;
906 }
907 return Tag;
908}
909
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000910llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000911CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000913 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000914 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
915 return cast<llvm::DICompositeType>(T);
916 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000917 unsigned Line = getLineNumber(RD->getLocation());
918 StringRef RDName = getClassName(RD);
919
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000920 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000921 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000922
Guy Benyei11169dd2012-12-18 14:30:41 +0000923 // Create the type.
Brock Wyma8557ec52018-05-22 12:41:19 +0000924 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000925 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000926 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +0000927 llvm::DINode::FlagFwdDecl, Identifier);
Paul Robinson1787f812017-09-28 18:37:02 +0000928 if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
929 if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
930 DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
931 CollectCXXTemplateParams(TSpecial, DefUnit));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000932 ReplaceMap.emplace_back(
933 std::piecewise_construct, std::make_tuple(Ty),
934 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000935 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000936}
937
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000938llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000939 const Type *Ty,
940 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000942 // Bit size, align and offset of the type.
943 // Size is always the size of a pointer. We can't use getTypeSize here
944 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000945 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
946 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000947 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000948 Optional<unsigned> DWARFAddressSpace =
949 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000950
Keno Fischer87842f32015-11-16 09:04:13 +0000951 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
952 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
953 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000954 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000955 else
956 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000957 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000958}
959
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000960llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
961 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000962 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000963 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000964 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
965 TheCU, getOrCreateMainFile(), 0);
966 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
967 Cache = DBuilder.createPointerType(Cache, Size);
968 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000969}
970
Scott Linder58df0e42018-08-08 15:56:12 +0000971uint64_t CGDebugInfo::collectDefaultElementTypesForBlockPointer(
972 const BlockPointerType *Ty, llvm::DIFile *Unit, llvm::DIDerivedType *DescTy,
973 unsigned LineNo, SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000974 QualType FType;
Guy Benyei11169dd2012-12-18 14:30:41 +0000975
Scott Linder58df0e42018-08-08 15:56:12 +0000976 // Advanced by calls to CreateMemberType in increments of FType, then
977 // returned as the overall size of the default elements.
978 uint64_t FieldOffset = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000979
Scott Linder58df0e42018-08-08 15:56:12 +0000980 // Blocks in OpenCL have unique constraints which make the standard fields
981 // redundant while requiring size and align fields for enqueue_kernel. See
982 // initializeForBlockHeader in CGBlocks.cpp
Scott Linder2b5cf042018-07-30 20:31:11 +0000983 if (CGM.getLangOpts().OpenCL) {
984 FType = CGM.getContext().IntTy;
985 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
986 EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset));
987 } else {
988 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
989 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
990 FType = CGM.getContext().IntTy;
991 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
992 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
993 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
994 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
995 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Scott Linder58df0e42018-08-08 15:56:12 +0000996 uint64_t FieldSize = CGM.getContext().getTypeSize(Ty);
997 uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty);
Scott Linder2b5cf042018-07-30 20:31:11 +0000998 EltTys.push_back(DBuilder.createMemberType(
Scott Linder58df0e42018-08-08 15:56:12 +0000999 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign,
1000 FieldOffset, llvm::DINode::FlagZero, DescTy));
Scott Lindera7c45682018-07-30 22:52:07 +00001001 FieldOffset += FieldSize;
Scott Linder2b5cf042018-07-30 20:31:11 +00001002 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001003
Scott Linder58df0e42018-08-08 15:56:12 +00001004 return FieldOffset;
1005}
1006
1007llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
1008 llvm::DIFile *Unit) {
1009 SmallVector<llvm::Metadata *, 8> EltTys;
1010 QualType FType;
1011 uint64_t FieldOffset;
1012 llvm::DINodeArray Elements;
1013
1014 FieldOffset = 0;
1015 FType = CGM.getContext().UnsignedLongTy;
1016 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
1017 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
1018
1019 Elements = DBuilder.getOrCreateArray(EltTys);
1020 EltTys.clear();
1021
1022 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
1023
1024 auto *EltTy =
1025 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0,
1026 FieldOffset, 0, Flags, nullptr, Elements);
1027
1028 // Bit size, align and offset of the type.
1029 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1030
1031 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
1032
1033 FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy,
1034 0, EltTys);
1035
Guy Benyei11169dd2012-12-18 14:30:41 +00001036 Elements = DBuilder.getOrCreateArray(EltTys);
1037
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001038 // The __block_literal_generic structs are marked with a special
1039 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
1040 // the debugger needs to know about. To allow type uniquing, emit
1041 // them without a name or a location.
Scott Linder58df0e42018-08-08 15:56:12 +00001042 EltTy = DBuilder.createStructType(Unit, "", nullptr, 0, FieldOffset, 0,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001043 Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001045 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001046}
1047
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001048llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1049 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +00001050 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001051 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +00001052
1053 SmallString<128> NS;
1054 llvm::raw_svector_ostream OS(NS);
Serge Pavlov03e672c2017-11-28 16:14:14 +00001055 Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false);
1056 printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +00001057
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001058 auto *AliasDecl =
1059 cast<TypeAliasTemplateDecl>(Ty->getTemplateName().getAsTemplateDecl())
1060 ->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +00001061
1062 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +00001063 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
1064 getLineNumber(Loc),
1065 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +00001066}
1067
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001068llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1069 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001070 // We don't set size information, but do specify where the typedef was
1071 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +00001072 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001073
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001074 // Typedefs are derived from some other type.
1075 return DBuilder.createTypedef(
1076 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1077 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +00001078 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001079}
1080
Reid Klecknerf00f8032016-06-08 20:41:54 +00001081static unsigned getDwarfCC(CallingConv CC) {
1082 switch (CC) {
1083 case CC_C:
1084 // Avoid emitting DW_AT_calling_convention if the C convention was used.
1085 return 0;
1086
1087 case CC_X86StdCall:
1088 return llvm::dwarf::DW_CC_BORLAND_stdcall;
1089 case CC_X86FastCall:
1090 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
1091 case CC_X86ThisCall:
1092 return llvm::dwarf::DW_CC_BORLAND_thiscall;
1093 case CC_X86VectorCall:
1094 return llvm::dwarf::DW_CC_LLVM_vectorcall;
1095 case CC_X86Pascal:
1096 return llvm::dwarf::DW_CC_BORLAND_pascal;
Martin Storsjo022e7822017-07-17 20:49:45 +00001097 case CC_Win64:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001098 return llvm::dwarf::DW_CC_LLVM_Win64;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001099 case CC_X86_64SysV:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001100 return llvm::dwarf::DW_CC_LLVM_X86_64SysV;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001101 case CC_AAPCS:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001102 return llvm::dwarf::DW_CC_LLVM_AAPCS;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001103 case CC_AAPCS_VFP:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001104 return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001105 case CC_IntelOclBicc:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001106 return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001107 case CC_SpirFunction:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001108 return llvm::dwarf::DW_CC_LLVM_SpirFunction;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00001109 case CC_OpenCLKernel:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001110 return llvm::dwarf::DW_CC_LLVM_OpenCLKernel;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001111 case CC_Swift:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001112 return llvm::dwarf::DW_CC_LLVM_Swift;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001113 case CC_PreserveMost:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001114 return llvm::dwarf::DW_CC_LLVM_PreserveMost;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001115 case CC_PreserveAll:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001116 return llvm::dwarf::DW_CC_LLVM_PreserveAll;
Erich Keane757d3172016-11-02 18:29:35 +00001117 case CC_X86RegCall:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001118 return llvm::dwarf::DW_CC_LLVM_X86RegCall;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001119 }
1120 return 0;
1121}
1122
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1124 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001125 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
1127 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001128 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001129
1130 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001131 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 if (isa<FunctionNoProtoType>(Ty))
1133 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001134 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1135 for (const QualType &ParamType : FPT->param_types())
1136 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001137 if (FPT->isVariadic())
1138 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 }
1140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001141 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001142 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001143 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001144}
1145
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001146/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001147/// As an optimization, return 0 if the access specifier equals the
1148/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001149static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1150 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001151 AccessSpecifier Default = clang::AS_none;
1152 if (RD && RD->isClass())
1153 Default = clang::AS_private;
1154 else if (RD && (RD->isStruct() || RD->isUnion()))
1155 Default = clang::AS_public;
1156
1157 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001158 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001159
Eric Christophere7b87e52014-10-26 23:40:33 +00001160 switch (Access) {
1161 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001162 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001163 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001164 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001165 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001166 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001167 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001168 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001169 }
1170 llvm_unreachable("unexpected access enumerator");
1171}
Guy Benyei11169dd2012-12-18 14:30:41 +00001172
David Majnemerb4b671e2016-06-30 03:01:59 +00001173llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1174 llvm::DIScope *RecordTy,
1175 const RecordDecl *RD) {
1176 StringRef Name = BitFieldDecl->getName();
1177 QualType Ty = BitFieldDecl->getType();
1178 SourceLocation Loc = BitFieldDecl->getLocation();
1179 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1180 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1181
1182 // Get the location for the field.
1183 llvm::DIFile *File = getOrCreateFile(Loc);
1184 unsigned Line = getLineNumber(Loc);
1185
1186 const CGBitFieldInfo &BitFieldInfo =
1187 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1188 uint64_t SizeInBits = BitFieldInfo.Size;
1189 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001190 uint64_t StorageOffsetInBits =
1191 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001192 uint64_t Offset = BitFieldInfo.Offset;
1193 // The bit offsets for big endian machines are reversed for big
1194 // endian target, compensate for that as the DIDerivedType requires
1195 // un-reversed offsets.
1196 if (CGM.getDataLayout().isBigEndian())
1197 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1198 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001199 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001200 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001201 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1202 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001203}
1204
1205llvm::DIType *
1206CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1207 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001208 uint32_t AlignInBits, llvm::DIFile *tunit,
1209 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001210 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001211
1212 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001213 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 unsigned line = getLineNumber(loc);
1215
David Majnemer34b57492014-07-30 01:30:47 +00001216 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001217 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001219 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1220 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001221 if (!Align)
1222 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 }
1224
Leny Kholodov80c047d2016-09-06 10:48:04 +00001225 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001226 return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align,
1227 offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001228}
1229
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001230void CGDebugInfo::CollectRecordLambdaFields(
1231 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001232 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001233 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1234 // has the name and the location of the variable so we should iterate over
1235 // both concurrently.
1236 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1237 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1238 unsigned fieldno = 0;
1239 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001240 E = CXXDecl->captures_end();
1241 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001242 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001243 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001244 SourceLocation Loc = C.getLocation();
1245 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001246 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001247 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001248 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001249 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001250 llvm::DIType *FieldType = createFieldType(
1251 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001252 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001253 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001254 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001255 // TODO: Need to handle 'this' in some way by probably renaming the
1256 // this of the lambda class and having a field member of 'this' or
1257 // by using AT_object_pointer for the function and having that be
1258 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001259 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001261 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001262 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001263 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001264 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001265
1266 elements.push_back(fieldType);
1267 }
1268 }
1269}
1270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001271llvm::DIDerivedType *
1272CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001273 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001274 // Create the descriptor for the static variable, with or without
1275 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001276 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001277 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1278 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001279
Eric Christopher91a31902013-01-16 01:22:32 +00001280 unsigned LineNumber = getLineNumber(Var->getLocation());
1281 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001282 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001283 if (Var->getInit()) {
1284 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001285 if (Value) {
1286 if (Value->isInt())
1287 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1288 if (Value->isFloat())
1289 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1290 }
Eric Christopher91a31902013-01-16 01:22:32 +00001291 }
1292
Leny Kholodov80c047d2016-09-06 10:48:04 +00001293 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001294 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001295 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001296 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001297 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001298 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001299}
1300
Eric Christophere7b87e52014-10-26 23:40:33 +00001301void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001302 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1303 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001304 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001305 StringRef name = field->getName();
1306 QualType type = field->getType();
1307
1308 // Ignore unnamed fields unless they're anonymous structs/unions.
1309 if (name.empty() && !type->isRecordType())
1310 return;
1311
David Majnemerb4b671e2016-06-30 03:01:59 +00001312 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001313 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001314 FieldType = createBitFieldType(field, RecordTy, RD);
1315 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001316 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001317 FieldType =
1318 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001319 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001320 }
1321
David Majnemerb4b671e2016-06-30 03:01:59 +00001322 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001323}
1324
Reid Klecknere2e82062017-08-08 20:30:14 +00001325void CGDebugInfo::CollectRecordNestedType(
1326 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1327 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001328 // Injected class names are not considered nested records.
1329 if (isa<InjectedClassNameType>(Ty))
1330 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001331 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001332 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1333 elements.push_back(nestedType);
1334}
1335
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001336void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001337 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001338 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001339 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001340 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001341
Eric Christopher91a31902013-01-16 01:22:32 +00001342 if (CXXDecl && CXXDecl->isLambda())
1343 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1344 else {
1345 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001346
Eric Christopher91a31902013-01-16 01:22:32 +00001347 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001348 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001349
Eric Christopher91a31902013-01-16 01:22:32 +00001350 // Static and non-static members should appear in the same order as
1351 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001352 for (const auto *I : record->decls())
1353 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001354 if (V->hasAttr<NoDebugAttr>())
1355 continue;
Reid Kleckner891b2712018-07-20 20:55:00 +00001356
1357 // Skip variable template specializations when emitting CodeView. MSVC
1358 // doesn't emit them.
1359 if (CGM.getCodeGenOpts().EmitCodeView &&
1360 isa<VarTemplateSpecializationDecl>(V))
1361 continue;
1362
David Blaikiece763042013-08-20 21:49:21 +00001363 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001364 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001365 if (MI != StaticDataMemberCache.end()) {
1366 assert(MI->second &&
1367 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001368 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001369 } else {
1370 auto Field = CreateRecordStaticField(V, RecordTy, record);
1371 elements.push_back(Field);
1372 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001373 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001374 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1375 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001376
1377 // Bump field number for next field.
1378 ++fieldNo;
Reid Kleckner891b2712018-07-20 20:55:00 +00001379 } else if (CGM.getCodeGenOpts().EmitCodeView) {
1380 // Debug info for nested types is included in the member list only for
1381 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001382 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1383 if (!nestedType->isImplicit() &&
1384 nestedType->getDeclContext() == record)
1385 CollectRecordNestedType(nestedType, elements);
1386 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 }
1388}
1389
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001390llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001391CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001393 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001394 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001395 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001396 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001397 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1398 Func, Unit);
1399}
David Blaikie2aaf0652013-01-07 22:24:59 +00001400
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001401llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1402 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001404 llvm::DITypeRefArray Args(
1405 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001406 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001407 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001408
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001409 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001410
1411 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001412 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001413
David Blaikie2aaf0652013-01-07 22:24:59 +00001414 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001415 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001416 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1417 // Create pointer type directly in this case.
1418 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1419 QualType PointeeTy = ThisPtrTy->getPointeeType();
1420 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001421 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001422 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001423 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1424 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001425 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001426 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001427 // TODO: This and the artificial type below are misleading, the
1428 // types aren't artificial the argument is, but the current
1429 // metadata doesn't represent that.
1430 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1431 Elts.push_back(ThisPtrType);
1432 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001433 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001434 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001435 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1436 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 }
1438
1439 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001440 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1441 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001442
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001444
Leny Kholodov80c047d2016-09-06 10:48:04 +00001445 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001446 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001448 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001449 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001450
Reid Klecknerf00f8032016-06-08 20:41:54 +00001451 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1452 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001453}
1454
Eric Christopherb2a008c2013-05-16 00:45:12 +00001455/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001456/// inside a function.
1457static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001458 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001459 return isFunctionLocalClass(NRD);
1460 if (isa<FunctionDecl>(RD->getDeclContext()))
1461 return true;
1462 return false;
1463}
1464
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001465llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1466 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001467 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001468 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001469
Guy Benyei11169dd2012-12-18 14:30:41 +00001470 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001471 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001472
1473 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1474 // make sense to give a single ctor/dtor a linkage name.
1475 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001476 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1477 // property to use here. It may've been intended to model "is non-external
1478 // type" but misses cases of non-function-local but non-external classes such
1479 // as those in anonymous namespaces as well as the reverse - external types
1480 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1482 MethodLinkageName = CGM.getMangledName(Method);
1483
1484 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001485 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001486 unsigned MethodLine = 0;
1487 if (!Method->isImplicit()) {
1488 MethodDefUnit = getOrCreateFile(Method->getLocation());
1489 MethodLine = getLineNumber(Method->getLocation());
1490 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001491
1492 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001493 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001494 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001495 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001496 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001497 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001498
Guy Benyei11169dd2012-12-18 14:30:41 +00001499 if (Method->isVirtual()) {
1500 if (Method->isPure())
1501 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1502 else
1503 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001504
Reid Kleckner216d0a12016-06-16 20:08:51 +00001505 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1506 // It doesn't make sense to give a virtual destructor a vtable index,
1507 // since a single destructor has two entries in the vtable.
1508 if (!isa<CXXDestructorDecl>(Method))
1509 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1510 } else {
1511 // Emit MS ABI vftable information. There is only one entry for the
1512 // deleting dtor.
1513 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1514 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
Reid Klecknercbec0262018-04-02 20:00:39 +00001515 MethodVFTableLocation ML =
Reid Kleckner216d0a12016-06-16 20:08:51 +00001516 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1517 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001518
1519 // CodeView only records the vftable offset in the class that introduces
1520 // the virtual method. This is possible because, unlike Itanium, the MS
1521 // C++ ABI does not include all virtual methods from non-primary bases in
1522 // the vtable for the most derived class. For example, if C inherits from
1523 // A and B, C's primary vftable will not include B's virtual methods.
Benjamin Krameracfa3392017-12-17 23:52:45 +00001524 if (Method->size_overridden_methods() == 0)
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001525 Flags |= llvm::DINode::FlagIntroducedVirtual;
1526
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001527 // The 'this' adjustment accounts for both the virtual and non-virtual
1528 // portions of the adjustment. Presumably the debugger only uses it when
1529 // it knows the dynamic type of an object.
1530 ThisAdjustment = CGM.getCXXABI()
1531 .getVirtualFunctionPrologueThisAdjustment(GD)
1532 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001533 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 ContainingType = RecordTy;
1535 }
1536
Adrian McCarthyd91bf392017-09-13 20:53:55 +00001537 if (Method->isStatic())
1538 Flags |= llvm::DINode::FlagStaticMember;
Guy Benyei11169dd2012-12-18 14:30:41 +00001539 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001540 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001541 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001542 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001545 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001547 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 }
1549 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001550 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001551 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001552 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001553 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001556 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1557 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001558 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001559 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1560 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1561 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001562
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001563 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001564
1565 return SP;
1566}
1567
Eric Christophere7b87e52014-10-26 23:40:33 +00001568void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001569 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1570 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001571
1572 // Since we want more than just the individual member decls if we
1573 // have templated functions iterate over every declaration to gather
1574 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001575 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001576 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1577 // If the member is implicit, don't add it to the member list. This avoids
1578 // the member being added to type units by LLVM, while still allowing it
1579 // to be emitted into the type declaration/reference inside the compile
1580 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001581 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001582 // FIXME: Handle Using(Shadow?)Decls here to create
1583 // DW_TAG_imported_declarations inside the class for base decls brought into
1584 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1585 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1586 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001587 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001588 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001589
1590 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1591 continue;
1592
David Blaikiefd580722014-10-06 05:18:55 +00001593 // Reuse the existing member function declaration if it exists.
1594 // It may be associated with the declaration of the type & should be
1595 // reused as we're building the definition.
1596 //
1597 // This situation can arise in the vtable-based debug info reduction where
1598 // implicit members are emitted in a non-vtable TU.
1599 auto MI = SPCache.find(Method->getCanonicalDecl());
1600 EltTys.push_back(MI == SPCache.end()
1601 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001602 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001604}
Guy Benyei11169dd2012-12-18 14:30:41 +00001605
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001606void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001607 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001608 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001609 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1610 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1611 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001612
Bob Haarmandff36732016-10-25 22:19:32 +00001613 // If we are generating CodeView debug info, we also need to emit records for
1614 // indirect virtual base classes.
1615 if (CGM.getCodeGenOpts().EmitCodeView) {
1616 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1617 llvm::DINode::FlagIndirectVirtualBase);
1618 }
1619}
1620
1621void CGDebugInfo::CollectCXXBasesAux(
1622 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1623 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1624 const CXXRecordDecl::base_class_const_range &Bases,
1625 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1626 llvm::DINode::DIFlags StartingFlags) {
1627 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1628 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001629 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001630 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001631 if (!SeenTypes.insert(Base).second)
1632 continue;
1633 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1634 llvm::DINode::DIFlags BFlags = StartingFlags;
1635 uint64_t BaseOffset;
Brock Wyma3db2b102018-05-14 21:21:22 +00001636 uint32_t VBPtrOffset = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001637
Aaron Ballman574705e2014-03-13 15:41:46 +00001638 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001639 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1640 // virtual base offset offset is -ve. The code generator emits dwarf
1641 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001642 BaseOffset = 0 - CGM.getItaniumVTableContext()
1643 .getVirtualBaseOffsetOffset(RD, Base)
1644 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001645 } else {
1646 // In the MS ABI, store the vbtable offset, which is analogous to the
1647 // vbase offset offset in Itanium.
1648 BaseOffset =
1649 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001650 VBPtrOffset = CGM.getContext()
1651 .getASTRecordLayout(RD)
1652 .getVBPtrOffset()
1653 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001654 }
Bob Haarmandff36732016-10-25 22:19:32 +00001655 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001656 } else
1657 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1658 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1659 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001660
Adrian Prantl21361fb2014-08-29 22:44:27 +00001661 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001662 llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset,
1663 VBPtrOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001664 EltTys.push_back(DTy);
1665 }
1666}
1667
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001668llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001669CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1670 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001671 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001672 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001673 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1674 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001675 StringRef Name;
1676 if (TPList)
1677 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001678 switch (TA.getKind()) {
1679 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001680 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001681 TemplateParams.push_back(
1682 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001683 } break;
1684 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001685 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001686 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1687 TheCU, Name, TTy,
1688 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001689 } break;
1690 case TemplateArgument::Declaration: {
1691 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001692 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001694 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001695 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001696 // Variable pointer template parameters have a value that is the address
1697 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001698 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001699 V = CGM.GetAddrOfGlobalVar(VD);
1700 // Member function pointers have special support for building them, though
1701 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001702 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001703 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001704 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001705 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001706 // Member data pointers have special handling too to compute the fixed
1707 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001708 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001709 // These five lines (& possibly the above member function pointer
1710 // handling) might be able to be refactored to use similar code in
1711 // CodeGenModule::getMemberPointerConstant
1712 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1713 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001714 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001715 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001716 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001717 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1718 TheCU, Name, TTy,
1719 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001720 } break;
1721 case TemplateArgument::NullPtr: {
1722 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001723 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001724 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001725 // Special case member data pointer null values since they're actually -1
1726 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001727 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001728 // But treat member function pointers as simple zero integers because
1729 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1730 // CodeGen grows handling for values of non-null member function
1731 // pointers then perhaps we could remove this special case and rely on
1732 // EmitNullMemberPointer for member function pointers.
1733 if (MPT->isMemberDataPointer())
1734 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1735 if (!V)
1736 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001737 TemplateParams.push_back(
1738 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001739 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001740 case TemplateArgument::Template:
1741 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001742 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001743 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1744 break;
1745 case TemplateArgument::Pack:
1746 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1747 TheCU, Name, nullptr,
1748 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1749 break;
David Majnemer5559d472013-08-24 08:21:10 +00001750 case TemplateArgument::Expression: {
1751 const Expr *E = TA.getAsExpr();
1752 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001753 if (E->isGLValue())
1754 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001755 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001756 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001757 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001758 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001759 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001760 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001761 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001762 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001763 case TemplateArgument::Null:
1764 llvm_unreachable(
1765 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001766 }
1767 }
1768 return DBuilder.getOrCreateArray(TemplateParams);
1769}
1770
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001771llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001772CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001773 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 if (FD->getTemplatedKind() ==
1775 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001776 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1777 ->getTemplate()
1778 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001779 return CollectTemplateParams(
1780 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001782 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001783}
1784
Matthew Voss20165362018-10-03 18:45:04 +00001785llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL,
1786 llvm::DIFile *Unit) {
1787 if (auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VL)) {
1788 auto T = TS->getSpecializedTemplateOrPartial();
1789 auto TA = TS->getTemplateArgs().asArray();
1790 // Collect parameters for a partial specialization
1791 if (T.is<VarTemplatePartialSpecializationDecl *>()) {
1792 const TemplateParameterList *TList =
1793 T.get<VarTemplatePartialSpecializationDecl *>()
1794 ->getTemplateParameters();
1795 return CollectTemplateParams(TList, TA, Unit);
1796 }
1797
1798 // Collect parameters for an explicit specialization
1799 if (T.is<VarTemplateDecl *>()) {
1800 const TemplateParameterList *TList = T.get<VarTemplateDecl *>()
1801 ->getTemplateParameters();
1802 return CollectTemplateParams(TList, TA, Unit);
1803 }
1804 }
1805 return llvm::DINodeArray();
1806}
1807
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001808llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1809 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001810 // Always get the full list of parameters, not just the ones from
1811 // the specialization.
1812 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001813 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001814 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001815 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001816}
1817
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001818llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001819 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001820 return VTablePtrType;
1821
1822 ASTContext &Context = CGM.getContext();
1823
1824 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001825 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001826 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001827 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001829 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1830 Optional<unsigned> DWARFAddressSpace =
1831 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1832
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001833 llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType(
1834 SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1836 return VTablePtrType;
1837}
1838
Guy Benyei11169dd2012-12-18 14:30:41 +00001839StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001840 // Copy the gdb compatible name on the side and use its reference.
1841 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001842}
1843
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001844void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001845 SmallVectorImpl<llvm::Metadata *> &EltTys,
1846 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001847 // If this class is not dynamic then there is not any vtable info to collect.
1848 if (!RD->isDynamicClass())
1849 return;
1850
Reid Kleckner59812422016-08-31 20:35:01 +00001851 // Don't emit any vtable shape or vptr info if this class doesn't have an
1852 // extendable vfptr. This can happen if the class doesn't have virtual
1853 // methods, or in the MS ABI if those virtual methods only come from virtually
1854 // inherited bases.
1855 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1856 if (!RL.hasExtendableVFPtr())
1857 return;
1858
Reid Klecknerdc124992016-08-31 16:11:43 +00001859 // CodeView needs to know how large the vtable of every dynamic class is, so
1860 // emit a special named pointer type into the element list. The vptr type
1861 // points to this type as well.
1862 llvm::DIType *VPtrTy = nullptr;
1863 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1864 CGM.getTarget().getCXXABI().isMicrosoft();
1865 if (NeedVTableShape) {
1866 uint64_t PtrWidth =
1867 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1868 const VTableLayout &VFTLayout =
1869 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1870 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001871 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001872 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001873 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1874 Optional<unsigned> DWARFAddressSpace =
1875 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001876
1877 // Create a very wide void* type and insert it directly in the element list.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001878 llvm::DIType *VTableType = DBuilder.createPointerType(
1879 nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001880 EltTys.push_back(VTableType);
1881
1882 // The vptr is a pointer to this special vtable type.
1883 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1884 }
1885
1886 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001887 if (RL.getPrimaryBase())
1888 return;
1889
1890 if (!VPtrTy)
1891 VPtrTy = getOrCreateVTablePtrType(Unit);
1892
Guy Benyei11169dd2012-12-18 14:30:41 +00001893 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001894 llvm::DIType *VPtrMember =
1895 DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1896 llvm::DINode::FlagArtificial, VPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001897 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001898}
1899
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001900llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001901 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001902 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001903 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001904 return T;
1905}
1906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001907llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001908 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001909 return getOrCreateStandaloneType(D, Loc);
1910}
1911
1912llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1913 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001914 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001915 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001917 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001918
Adrian Prantl73409ce2013-03-11 18:33:46 +00001919 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 return T;
1921}
1922
David Blaikie483a9da2014-05-06 18:35:21 +00001923void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001924 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001925 return;
1926 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001927 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001928 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001930 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001932 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001933 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001934}
1935
David Blaikieb2e86eb2013-08-15 20:49:17 +00001936void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001937 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001938 !CGM.getLangOpts().CPlusPlus)
1939 completeRequiredType(RD);
1940}
1941
David Blaikieb11c8732017-01-30 06:36:08 +00001942/// Return true if the class or any of its methods are marked dllimport.
1943static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1944 if (RD->hasAttr<DLLImportAttr>())
1945 return true;
1946 for (const CXXMethodDecl *MD : RD->methods())
1947 if (MD->hasAttr<DLLImportAttr>())
1948 return true;
1949 return false;
1950}
1951
Adrian Prantla43acdc2017-07-24 23:48:51 +00001952/// Does a type definition exist in an imported clang module?
1953static bool isDefinedInClangModule(const RecordDecl *RD) {
1954 // Only definitions that where imported from an AST file come from a module.
1955 if (!RD || !RD->isFromASTFile())
1956 return false;
1957 // Anonymous entities cannot be addressed. Treat them as not from module.
1958 if (!RD->isExternallyVisible() && RD->getName().empty())
1959 return false;
1960 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1961 if (!CXXDecl->isCompleteDefinition())
1962 return false;
Adrian Prantlba6fdc52018-10-24 00:06:02 +00001963 // Check wether RD is a template.
Adrian Prantla43acdc2017-07-24 23:48:51 +00001964 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1965 if (TemplateKind != TSK_Undeclared) {
Adrian Prantlba6fdc52018-10-24 00:06:02 +00001966 // Unfortunately getOwningModule() isn't accurate enough to find the
1967 // owning module of a ClassTemplateSpecializationDecl that is inside a
1968 // namespace spanning multiple modules.
1969 bool Explicit = false;
1970 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl))
1971 Explicit = TD->isExplicitInstantiationOrSpecialization();
1972 if (!Explicit && CXXDecl->getEnclosingNamespaceContext())
1973 return false;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001974 // This is a template, check the origin of the first member.
1975 if (CXXDecl->field_begin() == CXXDecl->field_end())
1976 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1977 if (!CXXDecl->field_begin()->isFromASTFile())
1978 return false;
1979 }
1980 }
1981 return true;
1982}
1983
David Blaikie6943dea2013-08-20 01:28:15 +00001984void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001985 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1986 if (CXXRD->isDynamicClass() &&
1987 CGM.getVTableLinkage(CXXRD) ==
1988 llvm::GlobalValue::AvailableExternallyLinkage &&
1989 !isClassOrMethodDLLImport(CXXRD))
1990 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001991
1992 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1993 return;
1994
David Blaikieb11c8732017-01-30 06:36:08 +00001995 completeClass(RD);
1996}
1997
1998void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001999 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00002000 return;
David Blaikie6943dea2013-08-20 01:28:15 +00002001 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00002002 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00002003 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002004 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00002005 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002006 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002007 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002008 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00002009}
2010
David Blaikie0e716b42014-03-03 23:48:23 +00002011static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
2012 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002013 for (CXXMethodDecl *MD : llvm::make_range(I, End))
2014 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00002015 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00002016 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00002017 return true;
2018 return false;
2019}
2020
Benjamin Kramer8c305922016-02-02 11:06:51 +00002021static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
2022 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00002023 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00002024 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00002025 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002026
David Blaikie1ac9c982017-04-11 21:13:37 +00002027 if (auto *ES = RD->getASTContext().getExternalSource())
2028 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
2029 return true;
2030
Benjamin Kramer8c305922016-02-02 11:06:51 +00002031 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00002032 return false;
2033
2034 if (!LangOpts.CPlusPlus)
2035 return false;
2036
2037 if (!RD->isCompleteDefinitionRequired())
2038 return true;
2039
David Majnemer58ed0f32016-07-17 00:39:12 +00002040 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00002041
2042 if (!CXXDecl)
2043 return false;
2044
Adrian McCarthy99242982016-08-16 22:11:18 +00002045 // Only emit complete debug info for a dynamic class when its vtable is
2046 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00002047 // across DLL boundaries, so skip this optimization if the class or any of its
2048 // methods are marked dllimport. This isn't a complete solution, since objects
2049 // without any dllimport methods can be used in one DLL and constructed in
2050 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00002051 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00002052 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00002053 return true;
2054
2055 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00002056 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00002057 Spec = SD->getSpecializationKind();
2058
2059 if (Spec == TSK_ExplicitInstantiationDeclaration &&
2060 hasExplicitMemberDefinition(CXXDecl->method_begin(),
2061 CXXDecl->method_end()))
2062 return true;
2063
2064 return false;
2065}
2066
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00002067void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
2068 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
2069 return;
2070
2071 QualType Ty = CGM.getContext().getRecordType(RD);
2072 llvm::DIType *T = getTypeOrNull(Ty);
2073 if (T && T->isForwardDecl())
2074 completeClassData(RD);
2075}
2076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002077llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002079 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002080 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
2081 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002082 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002083 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002084 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00002085 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002086
David Blaikieb2e86eb2013-08-15 20:49:17 +00002087 return CreateTypeDefinition(Ty);
2088}
2089
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002090llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00002091 RecordDecl *RD = Ty->getDecl();
2092
Guy Benyei11169dd2012-12-18 14:30:41 +00002093 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002094 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002095
2096 // Records and classes and unions can all be recursive. To handle them, we
2097 // first generate a debug descriptor for the struct as a forward declaration.
2098 // Then (if it is a definition) we go through and get debug info for all of
2099 // its members. Finally, we create a descriptor for the complete type (which
2100 // may refer to the forward decl if the struct is recursive) and replace all
2101 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002102 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002104 const RecordDecl *D = RD->getDefinition();
2105 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002106 return FwdDecl;
2107
David Majnemer58ed0f32016-07-17 00:39:12 +00002108 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00002109 CollectContainingType(CXXDecl, FwdDecl);
2110
Guy Benyei11169dd2012-12-18 14:30:41 +00002111 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002112 LexicalBlockStack.emplace_back(&*FwdDecl);
2113 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002114
Guy Benyei11169dd2012-12-18 14:30:41 +00002115 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002116 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00002117 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00002118
2119 // Note: The split of CXXDecl information here is intentional, the
2120 // gdb tests will depend on a certain ordering at printout. The debug
2121 // information offsets are still correct if we merge them all together
2122 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00002123 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 if (CXXDecl) {
2125 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00002126 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002127 }
2128
Eric Christopher91a31902013-01-16 01:22:32 +00002129 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00002130 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00002131 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002133
2134 LexicalBlockStack.pop_back();
2135 RegionMap.erase(Ty->getDecl());
2136
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002137 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002138 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002139
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002140 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002141 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002142 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002143
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002144 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002145 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002146}
2147
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002148llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
2149 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002150 // Ignore protocols.
2151 return getOrCreateType(Ty->getBaseType(), Unit);
2152}
2153
Manman Rene6be26c2016-09-13 17:25:08 +00002154llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
2155 llvm::DIFile *Unit) {
2156 // Ignore protocols.
2157 SourceLocation Loc = Ty->getDecl()->getLocation();
2158
2159 // Use Typedefs to represent ObjCTypeParamType.
2160 return DBuilder.createTypedef(
2161 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
2162 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2163 getDeclContextDescriptor(Ty->getDecl()));
2164}
2165
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002166/// \return true if Getter has the default name for the property PD.
2167static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2168 const ObjCMethodDecl *Getter) {
2169 assert(PD);
2170 if (!Getter)
2171 return true;
2172
2173 assert(Getter->getDeclName().isObjCZeroArgSelector());
2174 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002175 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002176}
2177
2178/// \return true if Setter has the default name for the property PD.
2179static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2180 const ObjCMethodDecl *Setter) {
2181 assert(PD);
2182 if (!Setter)
2183 return true;
2184
2185 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002186 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002187 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002188}
2189
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002190llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2191 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002192 ObjCInterfaceDecl *ID = Ty->getDecl();
2193 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002194 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002195
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002196 // Return a forward declaration if this type was imported from a clang module,
2197 // and this is not the compile unit with the implementation of the type (which
2198 // may contain hidden ivars).
2199 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2200 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002201 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2202 ID->getName(),
2203 getDeclContextDescriptor(ID), Unit, 0);
2204
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002206 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002208 auto RuntimeLang =
2209 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002210
2211 // If this is just a forward declaration return a special forward-declaration
2212 // debug type since we won't be able to lay out the entire type.
2213 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002214 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002215 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002216 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002217 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2218 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002219 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 return FwdDecl;
2221 }
2222
David Blaikieef8a9512014-05-05 23:23:53 +00002223 return CreateTypeDefinition(Ty, Unit);
2224}
2225
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002226llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002227CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2228 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002229 // Use the Module pointer as the key into the cache. This is a
2230 // nullptr if the "Module" is a PCH, which is safe because we don't
2231 // support chained PCH debug info, so there can only be a single PCH.
2232 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002233 auto ModRef = ModuleCache.find(M);
2234 if (ModRef != ModuleCache.end())
2235 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002236
2237 // Macro definitions that were defined with "-D" on the command line.
2238 SmallString<128> ConfigMacros;
2239 {
2240 llvm::raw_svector_ostream OS(ConfigMacros);
2241 const auto &PPOpts = CGM.getPreprocessorOpts();
2242 unsigned I = 0;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002243 // Translate the macro definitions back into a command line.
Adrian Prantl2388ead2015-06-30 18:01:05 +00002244 for (auto &M : PPOpts.Macros) {
2245 if (++I > 1)
2246 OS << " ";
2247 const std::string &Macro = M.first;
2248 bool Undef = M.second;
2249 OS << "\"-" << (Undef ? 'U' : 'D');
2250 for (char c : Macro)
2251 switch (c) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002252 case '\\':
2253 OS << "\\\\";
2254 break;
2255 case '"':
2256 OS << "\\\"";
2257 break;
2258 default:
2259 OS << c;
Adrian Prantl2388ead2015-06-30 18:01:05 +00002260 }
2261 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002262 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002263 }
Adrian Prantl66689202015-09-18 23:01:45 +00002264
Adrian Prantl835e6632015-09-24 16:10:10 +00002265 bool IsRootModule = M ? !M->Parent : true;
2266 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002267 // PCH files don't have a signature field in the control block,
2268 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002269 // We use the lower 64 bits for debug info.
2270 uint64_t Signature =
2271 Mod.getSignature()
2272 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2273 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002274 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002275 DIB.createCompileUnit(TheCU->getSourceLanguage(),
Scott Lindera2fbcef2018-02-26 17:32:31 +00002276 // TODO: Support "Source" from external AST providers?
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002277 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2278 TheCU->getProducer(), true, StringRef(), 0,
2279 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2280 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002281 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002282 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002283 llvm::DIModule *Parent =
2284 IsRootModule ? nullptr
2285 : getOrCreateModuleRef(
2286 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2287 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002288 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002289 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2290 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002291 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002292 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002293}
2294
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002295llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2296 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002297 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002298 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002299 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002300 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002301
2302 // Bit size, align and offset of the type.
2303 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002304 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002305
Leny Kholodov80c047d2016-09-06 10:48:04 +00002306 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002308 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002309
Adrian Prantlfd696112015-10-01 00:48:51 +00002310 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002311 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002312 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2313 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002314
David Blaikieef8a9512014-05-05 23:23:53 +00002315 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002316 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002318 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002319 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002320 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002321
2322 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002323 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002324
2325 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2326 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002327 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002328 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002329 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002330 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002331
Brock Wyma3db2b102018-05-14 21:21:22 +00002332 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002333 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 EltTys.push_back(InhTag);
2335 }
2336
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002337 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002338 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002340 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002341 unsigned PLine = getLineNumber(Loc);
2342 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2343 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002344 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2345 PD->getName(), PUnit, PLine,
2346 hasDefaultGetterName(PD, Getter) ? ""
2347 : getSelectorName(PD->getGetterName()),
2348 hasDefaultSetterName(PD, Setter) ? ""
2349 : getSelectorName(PD->getSetterName()),
2350 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002352 };
2353 {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002354 llvm::SmallPtrSet<const IdentifierInfo *, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002355 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002356 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002357 PropertySet.insert(PD->getIdentifier());
2358 AddProperty(PD);
2359 }
Manman Renefe1bac2016-01-27 20:00:32 +00002360 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002361 // Don't emit duplicate metadata for properties that were already in a
2362 // class extension.
2363 if (!PropertySet.insert(PD->getIdentifier()).second)
2364 continue;
2365 AddProperty(PD);
2366 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002367 }
2368
2369 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2370 unsigned FieldNo = 0;
2371 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2372 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002373 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002374 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002375 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002376
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 StringRef FieldName = Field->getName();
2378
2379 // Ignore unnamed fields.
2380 if (FieldName.empty())
2381 continue;
2382
2383 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002384 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 unsigned FieldLine = getLineNumber(Field->getLocation());
2386 QualType FType = Field->getType();
2387 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002388 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002389
2390 if (!FType->isIncompleteArrayType()) {
2391
2392 // Bit size, align and offset of the type.
2393 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002394 ? Field->getBitWidthValue(CGM.getContext())
2395 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002396 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 }
2398
2399 uint64_t FieldOffset;
2400 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2401 // We don't know the runtime offset of an ivar if we're using the
2402 // non-fragile ABI. For bitfields, use the bit offset into the first
2403 // byte of storage of the bitfield. For other fields, use zero.
2404 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002405 FieldOffset =
2406 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 FieldOffset %= CGM.getContext().getCharWidth();
2408 } else {
2409 FieldOffset = 0;
2410 }
2411 } else {
2412 FieldOffset = RL.getFieldOffset(FieldNo);
2413 }
2414
Leny Kholodov80c047d2016-09-06 10:48:04 +00002415 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002416 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002417 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002418 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002419 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002420 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002421 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002422
Craig Topper8a13c412014-05-21 05:09:00 +00002423 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002425 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002426 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002427 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002428 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002429 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002430 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002431 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2432 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002433 PropertyNode = DBuilder.createObjCProperty(
2434 PD->getName(), PUnit, PLine,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002435 hasDefaultGetterName(PD, Getter)
2436 ? ""
2437 : getSelectorName(PD->getGetterName()),
2438 hasDefaultSetterName(PD, Setter)
2439 ? ""
2440 : getSelectorName(PD->getSetterName()),
Eric Christophere7b87e52014-10-26 23:40:33 +00002441 PD->getPropertyAttributes(),
2442 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002443 }
2444 }
2445 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002446 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2447 FieldSize, FieldAlign, FieldOffset, Flags,
2448 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002449 EltTys.push_back(FieldTy);
2450 }
2451
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002452 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002453 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002454
Guy Benyei11169dd2012-12-18 14:30:41 +00002455 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002456 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002457}
2458
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002459llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2460 llvm::DIFile *Unit) {
2461 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 int64_t Count = Ty->getNumElements();
Guy Benyei11169dd2012-12-18 14:30:41 +00002463
Sander de Smalen891af03a2018-02-03 13:55:59 +00002464 llvm::Metadata *Subscript;
2465 QualType QTy(Ty, 0);
2466 auto SizeExpr = SizeExprCache.find(QTy);
2467 if (SizeExpr != SizeExprCache.end())
2468 Subscript = DBuilder.getOrCreateSubrange(0, SizeExpr->getSecond());
2469 else
2470 Subscript = DBuilder.getOrCreateSubrange(0, Count ? Count : -1);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002471 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002472
2473 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002474 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002475
2476 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2477}
2478
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002479llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002480 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002481 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002482
2483 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002484 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002486 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2487 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 } else if (Ty->isIncompleteArrayType()) {
2489 Size = 0;
2490 if (Ty->getElementType()->isIncompleteType())
2491 Align = 0;
2492 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002493 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002494 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 Size = 0;
2496 Align = 0;
2497 } else {
2498 // Size and align of the whole array, not the element type.
2499 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002500 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 }
2502
2503 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2504 // interior arrays, do we care? Why aren't nested arrays represented the
2505 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002506 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 QualType EltTy(Ty, 0);
2508 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2509 // If the number of elements is known, then count is that number. Otherwise,
2510 // it's -1. This allows us to represent a subrange with an array of 0
2511 // elements, like this:
2512 //
2513 // struct foo {
2514 // int x[0];
2515 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002516 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002517 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002519 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002520 if (Expr *Size = VAT->getSizeExpr()) {
2521 llvm::APSInt V;
2522 if (Size->EvaluateAsInt(V, CGM.getContext()))
2523 Count = V.getExtValue();
2524 }
David Blaikie87173f12016-08-22 17:49:56 +00002525 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002526
Sander de Smalen891af03a2018-02-03 13:55:59 +00002527 auto SizeNode = SizeExprCache.find(EltTy);
2528 if (SizeNode != SizeExprCache.end())
2529 Subscripts.push_back(
2530 DBuilder.getOrCreateSubrange(0, SizeNode->getSecond()));
2531 else
2532 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
Guy Benyei11169dd2012-12-18 14:30:41 +00002533 EltTy = Ty->getElementType();
2534 }
2535
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002537
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002538 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2539 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002540}
2541
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002542llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2543 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002544 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2545 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002546}
2547
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002548llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2549 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002550 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2551 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002552}
2553
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002554llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2555 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002556 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002557 uint64_t Size = 0;
2558
2559 if (!Ty->isIncompleteType()) {
2560 Size = CGM.getContext().getTypeSize(Ty);
2561
2562 // Set the MS inheritance model. There is no flag for the unspecified model.
2563 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2564 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2565 case MSInheritanceAttr::Keyword_single_inheritance:
2566 Flags |= llvm::DINode::FlagSingleInheritance;
2567 break;
2568 case MSInheritanceAttr::Keyword_multiple_inheritance:
2569 Flags |= llvm::DINode::FlagMultipleInheritance;
2570 break;
2571 case MSInheritanceAttr::Keyword_virtual_inheritance:
2572 Flags |= llvm::DINode::FlagVirtualInheritance;
2573 break;
2574 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2575 break;
2576 }
2577 }
2578 }
2579
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002580 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002581 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002582 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002583 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2584 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002585
2586 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002587 Ty->getPointeeType()->getAs<FunctionProtoType>();
2588 return DBuilder.createMemberPointerType(
2589 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2590 Ty->getClass(), FPT->getTypeQuals())),
2591 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002592 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002593}
2594
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002595llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002596 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2597 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002598}
2599
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002600llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00002601 return getOrCreateType(Ty->getElementType(), U);
2602}
2603
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002604llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002605 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002606
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002608 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002609 if (!ED->getTypeForDecl()->isIncompleteType()) {
2610 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002611 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 }
2613
Brock Wyma8557ec52018-05-22 12:41:19 +00002614 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002615
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002616 bool isImportedFromModule =
2617 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2618
Guy Benyei11169dd2012-12-18 14:30:41 +00002619 // If this is just a forward declaration, construct an appropriately
2620 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002621 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002622 // Note that it is possible for enums to be created as part of
2623 // their own declcontext. In this case a FwdDecl will be created
2624 // twice. This doesn't cause a problem because both FwdDecls are
2625 // entered into the ReplaceMap: finalize() will replace the first
2626 // FwdDecl with the second and then replace the second with
2627 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002628 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002629 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002630 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2631 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002632
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 unsigned Line = getLineNumber(ED->getLocation());
2634 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002635 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002636 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Brock Wyma8557ec52018-05-22 12:41:19 +00002637 0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
Adrian Prantla40030f2016-02-06 01:59:09 +00002638
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002639 ReplaceMap.emplace_back(
2640 std::piecewise_construct, std::make_tuple(Ty),
2641 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002642 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 }
2644
David Blaikie483a9da2014-05-06 18:35:21 +00002645 return CreateTypeDefinition(Ty);
2646}
2647
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002648llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002649 const EnumDecl *ED = Ty->getDecl();
2650 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002651 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002652 if (!ED->getTypeForDecl()->isIncompleteType()) {
2653 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002654 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002655 }
2656
Brock Wyma8557ec52018-05-22 12:41:19 +00002657 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
David Blaikie483a9da2014-05-06 18:35:21 +00002658
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002659 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002660 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 ED = ED->getDefinition();
Momchil Velikov25f6be52018-02-12 16:12:52 +00002662 bool IsSigned = ED->getIntegerType()->isSignedIntegerType();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002663 for (const auto *Enum : ED->enumerators()) {
Momchil Velikov25f6be52018-02-12 16:12:52 +00002664 const auto &InitVal = Enum->getInitVal();
2665 auto Value = IsSigned ? InitVal.getSExtValue() : InitVal.getZExtValue();
2666 Enumerators.push_back(
2667 DBuilder.createEnumerator(Enum->getName(), Value, !IsSigned));
Guy Benyei11169dd2012-12-18 14:30:41 +00002668 }
2669
2670 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002671 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002672
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002673 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002675 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Momchil Velikov25f6be52018-02-12 16:12:52 +00002676 llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002677 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2678 Line, Size, Align, EltArray, ClassTy,
Brock Wyma8557ec52018-05-22 12:41:19 +00002679 Identifier, ED->isFixed());
Guy Benyei11169dd2012-12-18 14:30:41 +00002680}
2681
Amjad Aboud546bc112017-02-09 22:07:24 +00002682llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2683 unsigned MType, SourceLocation LineLoc,
2684 StringRef Name, StringRef Value) {
2685 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2686 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2687}
2688
2689llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2690 SourceLocation LineLoc,
2691 SourceLocation FileLoc) {
2692 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2693 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2694 return DBuilder.createTempMacroFile(Parent, Line, FName);
2695}
2696
David Blaikie05491062013-01-21 04:37:12 +00002697static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2698 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002700 Qualifiers InnerQuals = T.getLocalQualifiers();
2701 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2702 // that is already there.
2703 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2704 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 QualType LastT = T;
2706 switch (T->getTypeClass()) {
2707 default:
David Blaikie05491062013-01-21 04:37:12 +00002708 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002709 case Type::TemplateSpecialization: {
2710 const auto *Spec = cast<TemplateSpecializationType>(T);
2711 if (Spec->isTypeAlias())
2712 return C.getQualifiedType(T.getTypePtr(), Quals);
2713 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002714 break;
2715 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 case Type::TypeOfExpr:
2717 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2718 break;
2719 case Type::TypeOf:
2720 T = cast<TypeOfType>(T)->getUnderlyingType();
2721 break;
2722 case Type::Decltype:
2723 T = cast<DecltypeType>(T)->getUnderlyingType();
2724 break;
2725 case Type::UnaryTransform:
2726 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2727 break;
2728 case Type::Attributed:
2729 T = cast<AttributedType>(T)->getEquivalentType();
2730 break;
2731 case Type::Elaborated:
2732 T = cast<ElaboratedType>(T)->getNamedType();
2733 break;
2734 case Type::Paren:
2735 T = cast<ParenType>(T)->getInnerType();
2736 break;
David Blaikie05491062013-01-21 04:37:12 +00002737 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002738 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002740 case Type::Auto:
2741 case Type::DeducedTemplateSpecialization: {
2742 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002743 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002744 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 break;
2746 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002747 case Type::Adjusted:
2748 case Type::Decayed:
2749 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2750 T = cast<AdjustedType>(T)->getAdjustedType();
2751 break;
2752 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002753
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002755 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 } while (true);
2757}
2758
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002759llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002760
2761 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002762 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002763
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002764 auto It = TypeCache.find(Ty.getAsOpaquePtr());
2765 if (It != TypeCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002766 // Verify that the debug info still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002767 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002769 }
2770
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002771 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002772}
2773
David Blaikie0e716b42014-03-03 23:48:23 +00002774void CGDebugInfo::completeTemplateDefinition(
2775 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002776 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002777 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002778 completeUnusedClass(SD);
2779}
David Blaikie0856f662014-03-04 22:01:08 +00002780
David Blaikie1ac9c982017-04-11 21:13:37 +00002781void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2782 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2783 return;
2784
2785 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002786 // In case this type has no member function definitions being emitted, ensure
2787 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002788 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002789}
2790
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002791llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002793 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002794
2795 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002796 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002797
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002798 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002799 return T;
2800
Adrian Prantlca844182015-09-11 17:23:03 +00002801 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002802 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002803
2804 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002805 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002806
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 return Res;
2808}
2809
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002810llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002811 // A forward declaration inside a module header does not belong to the module.
2812 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2813 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002814 if (DebugTypeExtRefs && D->isFromASTFile()) {
2815 // Record a reference to an imported clang module or precompiled header.
2816 auto *Reader = CGM.getContext().getExternalSource();
2817 auto Idx = D->getOwningModuleID();
2818 auto Info = Reader->getSourceDescriptor(Idx);
2819 if (Info)
2820 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2821 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002822 // We are building a clang module or a precompiled header.
2823 //
2824 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2825 // and it wouldn't be necessary to specify the parent scope
2826 // because the type is already unique by definition (it would look
2827 // like the output of -fno-standalone-debug). On the other hand,
2828 // the parent scope helps a consumer to quickly locate the object
2829 // file where the type's definition is located, so it might be
2830 // best to make this behavior a command line or debugger tuning
2831 // option.
Richard Smith54f04402017-05-18 02:29:20 +00002832 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002833 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002834 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2835 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002836 } else {
2837 // This the precompiled header being built.
2838 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002839 }
2840 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002841
Adrian Prantl9402cef2015-09-20 16:51:35 +00002842 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002843}
2844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002845llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 // Handle qualifiers, which recursively handles what they refer to.
2847 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002848 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002849
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 // Work out details of type.
2851 switch (Ty->getTypeClass()) {
2852#define TYPE(Class, Base)
2853#define ABSTRACT_TYPE(Class, Base)
2854#define NON_CANONICAL_TYPE(Class, Base)
2855#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2856#include "clang/AST/TypeNodes.def"
2857 llvm_unreachable("Dependent types cannot show up in debug information");
2858
2859 case Type::ExtVector:
2860 case Type::Vector:
2861 return CreateType(cast<VectorType>(Ty), Unit);
2862 case Type::ObjCObjectPointer:
2863 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2864 case Type::ObjCObject:
2865 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002866 case Type::ObjCTypeParam:
2867 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 case Type::ObjCInterface:
2869 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2870 case Type::Builtin:
2871 return CreateType(cast<BuiltinType>(Ty));
2872 case Type::Complex:
2873 return CreateType(cast<ComplexType>(Ty));
2874 case Type::Pointer:
2875 return CreateType(cast<PointerType>(Ty), Unit);
2876 case Type::BlockPointer:
2877 return CreateType(cast<BlockPointerType>(Ty), Unit);
2878 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002879 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002881 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002883 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 case Type::FunctionProto:
2885 case Type::FunctionNoProto:
2886 return CreateType(cast<FunctionType>(Ty), Unit);
2887 case Type::ConstantArray:
2888 case Type::VariableArray:
2889 case Type::IncompleteArray:
2890 return CreateType(cast<ArrayType>(Ty), Unit);
2891
2892 case Type::LValueReference:
2893 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2894 case Type::RValueReference:
2895 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2896
2897 case Type::MemberPointer:
2898 return CreateType(cast<MemberPointerType>(Ty), Unit);
2899
2900 case Type::Atomic:
2901 return CreateType(cast<AtomicType>(Ty), Unit);
2902
Xiuli Pan9c14e282016-01-09 12:53:17 +00002903 case Type::Pipe:
2904 return CreateType(cast<PipeType>(Ty), Unit);
2905
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002907 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2908
David Blaikie42edade2014-11-11 20:44:45 +00002909 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002910 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002911 case Type::Adjusted:
2912 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002913 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002914 case Type::Elaborated:
2915 case Type::Paren:
2916 case Type::SubstTemplateTypeParm:
2917 case Type::TypeOfExpr:
2918 case Type::TypeOf:
2919 case Type::Decltype:
2920 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002921 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002922 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002923 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002924
David Blaikie42edade2014-11-11 20:44:45 +00002925 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002926}
2927
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002928llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2929 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002930 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002931
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002932 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002933
2934 // We may have cached a forward decl when we could have created
2935 // a non-forward decl. Go ahead and create a non-forward decl
2936 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002937 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002938 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002939
2940 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002941 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002942
2943 // Propagate members from the declaration to the definition
2944 // CreateType(const RecordType*) will overwrite this with the members in the
2945 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002946 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002947
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002949 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 return Res;
2951}
2952
2953// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002954llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002955 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002956
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002958 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002959 unsigned Line = getLineNumber(RD->getLocation());
2960 StringRef RDName = getClassName(RD);
2961
Amjad Abouddc4531e2016-04-30 01:44:38 +00002962 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002963
David Blaikied2785892013-08-18 17:36:19 +00002964 // If we ended up creating the type during the context chain construction,
2965 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002966 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002967 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002968 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002969 return T;
David Blaikied2785892013-08-18 17:36:19 +00002970
Adrian Prantl381e7552014-02-04 21:29:50 +00002971 // If this is just a forward or incomplete declaration, construct an
2972 // appropriately marked node and just return it.
2973 const RecordDecl *D = RD->getDefinition();
2974 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002975 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002976
2977 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002978 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002979
Brock Wyma8557ec52018-05-22 12:41:19 +00002980 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002981
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002982 // Explicitly record the calling convention for C++ records.
2983 auto Flags = llvm::DINode::FlagZero;
2984 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2985 if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect)
2986 Flags |= llvm::DINode::FlagTypePassByReference;
2987 else
2988 Flags |= llvm::DINode::FlagTypePassByValue;
Aaron Smith044326c2018-07-23 20:49:07 +00002989
2990 // Record if a C++ record is trivial type.
2991 if (CXXRD->isTrivial())
2992 Flags |= llvm::DINode::FlagTrivial;
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002993 }
2994
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002995 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002996 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +00002997 Flags, Identifier);
Guy Benyei11169dd2012-12-18 14:30:41 +00002998
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002999 // Elements of composite types usually have back to the type, creating
3000 // uniquing cycles. Distinct nodes are more efficient.
3001 switch (RealDecl->getTag()) {
3002 default:
3003 llvm_unreachable("invalid composite type tag");
3004
3005 case llvm::dwarf::DW_TAG_array_type:
3006 case llvm::dwarf::DW_TAG_enumeration_type:
3007 // Array elements and most enumeration elements don't have back references,
3008 // so they don't tend to be involved in uniquing cycles and there is some
3009 // chance of merging them when linking together two modules. Only make
3010 // them distinct if they are ODR-uniqued.
Brock Wyma8557ec52018-05-22 12:41:19 +00003011 if (Identifier.empty())
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003012 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00003013 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003014
3015 case llvm::dwarf::DW_TAG_structure_type:
3016 case llvm::dwarf::DW_TAG_union_type:
3017 case llvm::dwarf::DW_TAG_class_type:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003018 // Immediately resolve to a distinct node.
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003019 RealDecl =
3020 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
3021 break;
3022 }
3023
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003024 RegionMap[Ty->getDecl()].reset(RealDecl);
3025 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003026
David Majnemer58ed0f32016-07-17 00:39:12 +00003027 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003028 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00003029 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00003030 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00003031}
3032
David Blaikieadfbf992013-08-18 16:55:33 +00003033void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003034 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00003035 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003036 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00003037 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3038 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00003039 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00003040 while (1) {
3041 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
3042 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
3043 if (PBT && !BRL.isPrimaryBaseVirtual())
3044 PBase = PBT;
3045 else
3046 break;
3047 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003048 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00003049 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
3050 getOrCreateFile(RD->getLocation())));
3051 } else if (RD->isDynamicClass())
3052 ContainingType = RealDecl;
3053
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00003054 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00003055}
3056
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003057llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003058 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003059 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00003061 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003062 llvm::DIType *Ty =
3063 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
3064 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003065 *Offset += FieldSize;
3066 return Ty;
3067}
3068
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003069void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00003070 StringRef &Name,
3071 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003072 llvm::DIScope *&FDContext,
3073 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003074 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003075 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00003076 Name = getFunctionName(FD);
3077 // Use mangled name as linkage name for C/C++ functions.
3078 if (FD->hasPrototype()) {
3079 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003080 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00003081 }
3082 // No need to replicate the linkage name if it isn't different from the
3083 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00003084 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00003085 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
3086 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00003087 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00003088 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00003089 LinkageName = StringRef();
3090
Benjamin Kramer8c305922016-02-02 11:06:51 +00003091 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003092 if (const NamespaceDecl *NSDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003093 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00003094 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00003095 else if (const RecordDecl *RDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003096 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003097 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
3098 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
3099 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00003100 // Check if it is a noreturn-marked function
3101 if (FD->isNoReturn())
3102 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00003103 // Collect template parameters.
3104 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
3105 }
3106}
3107
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003108void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00003109 unsigned &LineNo, QualType &T,
3110 StringRef &Name, StringRef &LinkageName,
Matthew Voss20165362018-10-03 18:45:04 +00003111 llvm::MDTuple *&TemplateParameters,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003112 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003113 Unit = getOrCreateFile(VD->getLocation());
3114 LineNo = getLineNumber(VD->getLocation());
3115
3116 setLocation(VD->getLocation());
3117
3118 T = VD->getType();
3119 if (T->isIncompleteArrayType()) {
3120 // CodeGen turns int[] into int[1] so we'll do the same here.
3121 llvm::APInt ConstVal(32, 1);
3122 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3123
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003124 T = CGM.getContext().getConstantArrayType(ET, ConstVal, ArrayType::Normal,
3125 0);
Frederic Riss9db79f12014-11-18 03:40:46 +00003126 }
3127
3128 Name = VD->getName();
3129 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
3130 !isa<ObjCMethodDecl>(VD->getDeclContext()))
3131 LinkageName = CGM.getMangledName(VD);
3132 if (LinkageName == Name)
3133 LinkageName = StringRef();
3134
Matthew Voss20165362018-10-03 18:45:04 +00003135 if (isa<VarTemplateSpecializationDecl>(VD)) {
3136 llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VD, &*Unit);
3137 TemplateParameters = parameterNodes.get();
3138 } else {
3139 TemplateParameters = nullptr;
3140 }
3141
Frederic Riss9db79f12014-11-18 03:40:46 +00003142 // Since we emit declarations (DW_AT_members) for static members, place the
3143 // definition of those static members in the namespace they were declared in
3144 // in the source code (the lexical decl context).
3145 // FIXME: Generalize this for even non-member global variables where the
3146 // declaration and definition may have different lexical decl contexts, once
3147 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003148 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
3149 : VD->getDeclContext();
3150 // When a record type contains an in-line initialization of a static data
3151 // member, and the record type is marked as __declspec(dllexport), an implicit
3152 // definition of the member will be created in the record context. DWARF
3153 // doesn't seem to have a nice way to describe this in a form that consumers
3154 // are likely to understand, so fake the "normal" situation of a definition
3155 // outside the class by putting it in the global scope.
3156 if (DC->isRecord())
3157 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003158
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003159 llvm::DIScope *Mod = getParentModuleOrNull(VD);
3160 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00003161}
3162
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003163llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
3164 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003165 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00003166 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003167 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003168 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 llvm::DIFile *Unit = getOrCreateFile(Loc);
3170 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003171 unsigned Line = getLineNumber(Loc);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003172 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray,
3173 Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003174 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
3175
Frederic Rissd253ed62014-11-18 03:40:51 +00003176 // Build function type.
3177 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003178 if (FD)
3179 for (const ParmVarDecl *Parm : FD->parameters())
3180 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00003181 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
3182 QualType FnType = CGM.getContext().getFunctionType(
3183 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003184 if (Stub) {
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003185 Flags |= getCallSiteRelatedAttrs();
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003186 return DBuilder.createFunction(
3187 DContext, Name, LinkageName, Unit, Line,
3188 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3189 !FD->isExternallyVisible(),
3190 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
3191 TParamsArray.get(), getFunctionDeclaration(FD));
3192 }
3193
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003194 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003195 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003196 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
3197 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003198 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003199 TParamsArray.get(), getFunctionDeclaration(FD));
George Burgess IV00f70bd2018-03-01 05:43:23 +00003200 const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003201 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3202 std::make_tuple(CanonDecl),
3203 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003204 return SP;
3205}
3206
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003207llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003208 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3209}
3210
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003211llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003212 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3213}
3214
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003215llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003216CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3217 QualType T;
3218 StringRef Name, LinkageName;
3219 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003220 llvm::DIFile *Unit = getOrCreateFile(Loc);
3221 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003222 unsigned Line = getLineNumber(Loc);
Matthew Voss20165362018-10-03 18:45:04 +00003223 llvm::MDTuple *TemplateParameters = nullptr;
Frederic Rissd253ed62014-11-18 03:40:51 +00003224
Matthew Voss20165362018-10-03 18:45:04 +00003225 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, TemplateParameters,
3226 DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003227 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003228 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3229 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Matthew Voss20165362018-10-03 18:45:04 +00003230 !VD->isExternallyVisible(), nullptr, TemplateParameters, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003231 FwdDeclReplaceMap.emplace_back(
3232 std::piecewise_construct,
3233 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3234 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003235 return GV;
3236}
3237
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003238llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003239 // We only need a declaration (not a definition) of the type - so use whatever
3240 // we would otherwise do to get a type for a pointee. (forward declarations in
3241 // limited debug info, full definitions (if the type definition is available)
3242 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003243 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003244 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003245 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003246 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003247
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003248 if (I != DeclCache.end()) {
3249 auto N = I->second;
3250 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3251 return GVE->getVariable();
3252 return dyn_cast_or_null<llvm::DINode>(N);
3253 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003254
3255 // No definition for now. Emit a forward definition that might be
3256 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003257 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003258 return getFunctionForwardDeclaration(FD);
3259 else if (const auto *VD = dyn_cast<VarDecl>(D))
3260 return getGlobalVariableForwardDeclaration(VD);
3261
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003262 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003263}
3264
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003265llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003266 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003267 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003268
David Majnemer58ed0f32016-07-17 00:39:12 +00003269 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003270 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003271 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003272
3273 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003274 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003275
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003276 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003277 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003278 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003279 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003280 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003281 }
3282 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003283 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003284 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003285 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003286 return SP;
3287 }
3288
Aaron Ballman86c93902014-03-06 23:45:36 +00003289 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003290 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003291 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003292 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003293 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 return SP;
3295 }
3296 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003297 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003298}
3299
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003300// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003301// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003302llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003303 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003304 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003305 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003306 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3307 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003308 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003309
David Majnemer58ed0f32016-07-17 00:39:12 +00003310 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003311 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003312
3313 const auto *FTy = FnType->getAs<FunctionType>();
3314 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3315
David Majnemer58ed0f32016-07-17 00:39:12 +00003316 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003318 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003319
3320 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003321 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003322
3323 // Replace the instancetype keyword with the actual type.
3324 if (ResultTy == CGM.getContext().getObjCInstanceType())
3325 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003326 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003327
Adrian Prantl7bec9032013-05-10 21:08:31 +00003328 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003329 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003330 QualType SelfDeclTy;
3331 if (auto *SelfDecl = OMethod->getSelfDecl())
3332 SelfDeclTy = SelfDecl->getType();
3333 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3334 if (FPT->getNumParams() > 1)
3335 SelfDeclTy = FPT->getParamType(0);
3336 if (!SelfDeclTy.isNull())
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003337 Elts.push_back(
3338 CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003340 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003341 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003343 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003344 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003345 // Variadic methods need a special marker at the end of the type list.
3346 if (OMethod->isVariadic())
3347 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003348
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003349 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003350 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3351 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003352 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003353
Adrian Prantl800faef2014-02-25 23:42:18 +00003354 // Handle variadic function types; they need an additional
3355 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003356 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003357 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003358 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003359 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003360 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3361 for (QualType ParamType : FPT->param_types())
3362 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003363 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003364 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003365 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3366 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003367 }
3368
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003369 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003370}
3371
Eric Christophere7b87e52014-10-26 23:40:33 +00003372void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3373 SourceLocation ScopeLoc, QualType FnType,
Brock Wyma94ece8f2018-04-16 16:53:57 +00003374 llvm::Function *Fn, bool CurFuncIsThunk,
3375 CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003376
3377 StringRef Name;
3378 StringRef LinkageName;
3379
3380 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3381
3382 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003383 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003384
Leny Kholodov80c047d2016-09-06 10:48:04 +00003385 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003386 llvm::DIFile *Unit = getOrCreateFile(Loc);
3387 llvm::DIScope *FDContext = Unit;
3388 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003389 if (!HasDecl) {
3390 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003391 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003392 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003393 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003394 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003396 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003397 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003398 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003399 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 return;
3401 }
3402 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003403 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3404 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003405 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003406 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003407 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003408 } else {
3409 // Use llvm function name.
3410 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003411 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003413 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 Name = Name.substr(1);
3415
Erich Keane293a0552018-02-14 00:14:07 +00003416 if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003417 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003418 // Artificial functions should not silently reuse CurLoc.
3419 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003420 }
Brock Wyma94ece8f2018-04-16 16:53:57 +00003421
3422 if (CurFuncIsThunk)
3423 Flags |= llvm::DINode::FlagThunk;
3424
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003425 llvm::DINode::DIFlags FlagsForDef = Flags | getCallSiteRelatedAttrs();
3426
Adrian Prantl42d71b92014-04-10 23:21:53 +00003427 unsigned LineNo = getLineNumber(Loc);
3428 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003429
Eric Christopher8018e412014-03-27 18:50:35 +00003430 // FIXME: The function declaration we're constructing here is mostly reusing
3431 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3432 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3433 // all subprograms instead of the actual context since subprogram definitions
3434 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003435 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003436 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003437 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003438 true /*definition*/, ScopeLine, FlagsForDef, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003439 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003440 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003441 // We might get here with a VarDecl in the case we're generating
3442 // code for the initialization of globals. Do not record these decls
3443 // as they will overwrite the actual VarDecl Decl in the cache.
3444 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003445 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003446
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00003447 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
3448 // Starting with DWARF V5 method declarations are emitted as children of
3449 // the interface type.
3450 if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
3451 const ObjCInterfaceDecl *ID = OMD->getClassInterface();
3452 QualType QTy(ID->getTypeForDecl(), 0);
3453 auto It = TypeCache.find(QTy.getAsOpaquePtr());
3454 if (It != TypeCache.end()) {
3455 llvm::DICompositeType *InterfaceDecl =
3456 cast<llvm::DICompositeType>(It->second);
3457 llvm::DISubprogram *FD = DBuilder.createFunction(
3458 InterfaceDecl, Name, LinkageName, Unit, LineNo,
3459 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
3460 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3461 TParamsArray.get());
3462 DBuilder.finalizeSubprogram(FD);
3463 ObjCMethodCache[ID].push_back(FD);
3464 }
3465 }
3466 }
3467
Adrian Prantlbebb8932014-03-21 21:01:58 +00003468 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003469 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003470
Guy Benyei11169dd2012-12-18 14:30:41 +00003471 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003472 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003473}
3474
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003475void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3476 QualType FnType) {
3477 StringRef Name;
3478 StringRef LinkageName;
3479
3480 const Decl *D = GD.getDecl();
3481 if (!D)
3482 return;
3483
Leny Kholodov80c047d2016-09-06 10:48:04 +00003484 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003485 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003486 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003487 llvm::DINodeArray TParamsArray;
3488 if (isa<FunctionDecl>(D)) {
3489 // If there is a DISubprogram for this function available then use it.
3490 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3491 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003492 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003493 Name = getObjCMethodName(OMD);
3494 Flags |= llvm::DINode::FlagPrototyped;
3495 } else {
3496 llvm_unreachable("not a function or ObjC method");
3497 }
3498 if (!Name.empty() && Name[0] == '\01')
3499 Name = Name.substr(1);
3500
3501 if (D->isImplicit()) {
3502 Flags |= llvm::DINode::FlagArtificial;
3503 // Artificial functions without a location should not silently reuse CurLoc.
3504 if (Loc.isInvalid())
3505 CurLoc = SourceLocation();
3506 }
3507 unsigned LineNo = getLineNumber(Loc);
3508 unsigned ScopeLine = 0;
3509
Adrian Prantle76bda52016-04-15 15:55:45 +00003510 DBuilder.retainType(DBuilder.createFunction(
3511 FDContext, Name, LinkageName, Unit, LineNo,
3512 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3513 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3514 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003515}
3516
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003517void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3518 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3519 // If there is a subprogram for this function available then use it.
3520 auto FI = SPCache.find(FD->getCanonicalDecl());
3521 llvm::DISubprogram *SP = nullptr;
3522 if (FI != SPCache.end())
3523 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003524 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003525 SP = getFunctionStub(GD);
3526 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3527 LexicalBlockStack.emplace_back(SP);
3528 setInlinedAt(Builder.getCurrentDebugLocation());
3529 EmitLocation(Builder, FD->getLocation());
3530}
3531
3532void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3533 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003534 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003535 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3536}
3537
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003538void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003539 // Update our current location
3540 setLocation(Loc);
3541
Adrian Prantl42ab39f2018-11-09 21:17:38 +00003542 if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
Eric Christophere7b87e52014-10-26 23:40:33 +00003543 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003544
Adrian Prantle83b1302014-01-07 22:05:52 +00003545 llvm::MDNode *Scope = LexicalBlockStack.back();
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003546 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3547 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003548}
3549
Guy Benyei11169dd2012-12-18 14:30:41 +00003550void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003551 llvm::MDNode *Back = nullptr;
3552 if (!LexicalBlockStack.empty())
3553 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003554 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003555 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003556 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003557}
3558
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003559void CGDebugInfo::AppendAddressSpaceXDeref(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003560 unsigned AddressSpace, SmallVectorImpl<int64_t> &Expr) const {
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003561 Optional<unsigned> DWARFAddressSpace =
3562 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3563 if (!DWARFAddressSpace)
3564 return;
3565
3566 Expr.push_back(llvm::dwarf::DW_OP_constu);
3567 Expr.push_back(DWARFAddressSpace.getValue());
3568 Expr.push_back(llvm::dwarf::DW_OP_swap);
3569 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3570}
3571
Eric Christopher0fdcb312013-05-16 00:52:20 +00003572void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3573 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 // Set our current location.
3575 setLocation(Loc);
3576
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003578 Builder.SetCurrentDebugLocation(
3579 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3580 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003581
Benjamin Kramer8c305922016-02-02 11:06:51 +00003582 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003583 return;
3584
3585 // Create a new lexical block and push it on the stack.
3586 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003587}
3588
Eric Christopher0fdcb312013-05-16 00:52:20 +00003589void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3590 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3592
3593 // Provide an entry in the line table for the end of the block.
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003594 EmitLocation(Builder, Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003595
Benjamin Kramer8c305922016-02-02 11:06:51 +00003596 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003597 return;
3598
Guy Benyei11169dd2012-12-18 14:30:41 +00003599 LexicalBlockStack.pop_back();
3600}
3601
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003602void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003603 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3604 unsigned RCount = FnBeginRegionCount.back();
3605 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3606
3607 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003608 while (LexicalBlockStack.size() != RCount) {
3609 // Provide an entry in the line table for the end of the block.
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003610 EmitLocation(Builder, CurLoc);
David Blaikie60a877b2014-10-22 19:34:33 +00003611 LexicalBlockStack.pop_back();
3612 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003613 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003614
3615 if (Fn && Fn->getSubprogram())
3616 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003617}
3618
Adrian Prantl05a623e2018-09-10 16:14:28 +00003619CGDebugInfo::BlockByRefType
3620CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
3621 uint64_t *XOffset) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003622 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 QualType FType;
3624 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003625 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003626
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003627 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003628 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003629
3630 FieldOffset = 0;
3631 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3632 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3633 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3634 FType = CGM.getContext().IntTy;
3635 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3636 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3637
3638 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3639 if (HasCopyAndDispose) {
3640 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003641 EltTys.push_back(
3642 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3643 EltTys.push_back(
3644 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003645 }
3646 bool HasByrefExtendedLayout;
3647 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003648 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3649 HasByrefExtendedLayout) &&
3650 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003651 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003652 EltTys.push_back(
3653 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003654 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003655
Guy Benyei11169dd2012-12-18 14:30:41 +00003656 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3657 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003658 CGM.getTarget().getPointerAlign(0))) {
3659 CharUnits FieldOffsetInBytes =
3660 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003661 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003662 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003663
Guy Benyei11169dd2012-12-18 14:30:41 +00003664 if (NumPaddingBytes.isPositive()) {
3665 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3666 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3667 pad, ArrayType::Normal, 0);
3668 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3669 }
3670 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003671
Guy Benyei11169dd2012-12-18 14:30:41 +00003672 FType = Type;
Adrian Prantl05a623e2018-09-10 16:14:28 +00003673 llvm::DIType *WrappedTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003674 FieldSize = CGM.getContext().getTypeSize(FType);
3675 FieldAlign = CGM.getContext().toBits(Align);
3676
Eric Christopherb2a008c2013-05-16 00:45:12 +00003677 *XOffset = FieldOffset;
Adrian Prantl05a623e2018-09-10 16:14:28 +00003678 llvm::DIType *FieldTy = DBuilder.createMemberType(
3679 Unit, VD->getName(), Unit, 0, FieldSize, FieldAlign, FieldOffset,
3680 llvm::DINode::FlagZero, WrappedTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003681 EltTys.push_back(FieldTy);
3682 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003683
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003684 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Adrian Prantl05a623e2018-09-10 16:14:28 +00003685 return {DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0,
3686 llvm::DINode::FlagZero, nullptr, Elements),
3687 WrappedTy};
Guy Benyei11169dd2012-12-18 14:30:41 +00003688}
3689
Sander de Smalen891af03a2018-02-03 13:55:59 +00003690llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
3691 llvm::Value *Storage,
3692 llvm::Optional<unsigned> ArgNo,
3693 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003694 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003695 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003696 if (VD->hasAttr<NoDebugAttr>())
Sander de Smalen891af03a2018-02-03 13:55:59 +00003697 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003698
David Blaikie7fceebf2013-08-19 03:37:48 +00003699 bool Unwritten =
3700 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3701 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003702 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003703 if (!Unwritten)
3704 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003705 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003706 uint64_t XOffset = 0;
3707 if (VD->hasAttr<BlocksAttr>())
Adrian Prantl05a623e2018-09-10 16:14:28 +00003708 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003709 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003710 Ty = getOrCreateType(VD->getType(), Unit);
3711
3712 // If there is no debug info for this type then do not emit debug info
3713 // for this variable.
3714 if (!Ty)
Sander de Smalen891af03a2018-02-03 13:55:59 +00003715 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003716
Guy Benyei11169dd2012-12-18 14:30:41 +00003717 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003718 unsigned Line = 0;
3719 unsigned Column = 0;
3720 if (!Unwritten) {
3721 Line = getLineNumber(VD->getLocation());
3722 Column = getColumnNumber(VD->getLocation());
3723 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003724 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003725 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003726 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003727 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003728
3729 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3730
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003731 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3732 AppendAddressSpaceXDeref(AddressSpace, Expr);
3733
Alexey Bataev24f710182017-06-09 13:55:08 +00003734 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3735 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003736 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3737 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3738 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3739 Flags |= llvm::DINode::FlagObjectPointer;
3740 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003741
Adrian Prantlc3782a12017-04-18 01:22:01 +00003742 // Note: Older versions of clang used to emit byval references with an extra
3743 // DW_OP_deref, because they referenced the IR arg directly instead of
3744 // referencing an alloca. Newer versions of LLVM don't treat allocas
3745 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003746 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003747 StringRef Name = VD->getName();
3748 if (!Name.empty()) {
3749 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003750 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003751 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003752 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003753 // offset of __forwarding field
3754 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003755 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003756 Expr.push_back(offset.getQuantity());
3757 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003758 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003759 // offset of x field
3760 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003761 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003762 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003763 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003764 // If VD is an anonymous union then Storage represents value for
3765 // all union fields.
George Burgess IV00f70bd2018-03-01 05:43:23 +00003766 const RecordDecl *RD = RT->getDecl();
Adrian Prantl0d820892015-04-29 15:05:50 +00003767 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003768 // GDB has trouble finding local variables in anonymous unions, so we emit
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003769 // artificial local variables for each of the members.
Adrian Prantl00820ab2015-04-29 16:52:31 +00003770 //
3771 // FIXME: Remove this code as soon as GDB supports this.
3772 // The debug info verifier in LLVM operates based on the assumption that a
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003773 // variable has the same size as its storage and we had to disable the
3774 // check for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003775 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003776 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003777 StringRef FieldName = Field->getName();
3778
3779 // Ignore unnamed fields. Do not ignore unnamed records.
3780 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3781 continue;
3782
3783 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003784 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003785 auto *D = DBuilder.createAutoVariable(
3786 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003787 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003788
3789 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003790 DBuilder.insertDeclare(
3791 Storage, D, DBuilder.createExpression(Expr),
3792 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3793 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003794 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003795 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003796 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003797
3798 // Create the descriptor for the variable.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003799 auto *D = ArgNo ? DBuilder.createParameterVariable(
3800 Scope, Name, *ArgNo, Unit, Line, Ty,
3801 CGM.getLangOpts().Optimize, Flags)
3802 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3803 CGM.getLangOpts().Optimize,
3804 Flags, Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003805
3806 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003807 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003808 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003809 Builder.GetInsertBlock());
Sander de Smalen891af03a2018-02-03 13:55:59 +00003810
3811 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00003812}
3813
Sander de Smalen891af03a2018-02-03 13:55:59 +00003814llvm::DILocalVariable *
3815CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage,
3816 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003817 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Sander de Smalen891af03a2018-02-03 13:55:59 +00003818 return EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003819}
3820
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003821llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3822 llvm::DIType *Ty) {
3823 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003824 if (CachedTy)
3825 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003826 return DBuilder.createObjectPointerType(Ty);
3827}
3828
Eric Christophere7b87e52014-10-26 23:40:33 +00003829void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3830 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003831 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003832 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003833 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003834
Craig Topper8a13c412014-05-21 05:09:00 +00003835 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003836 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003837 if (VD->hasAttr<NoDebugAttr>())
3838 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003839
Guy Benyei11169dd2012-12-18 14:30:41 +00003840 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003841
Guy Benyei11169dd2012-12-18 14:30:41 +00003842 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003843 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3844 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003845 if (isByRef)
Adrian Prantl05a623e2018-09-10 16:14:28 +00003846 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003847 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003848 Ty = getOrCreateType(VD->getType(), Unit);
3849
3850 // Self is passed along as an implicit non-arg variable in a
3851 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003852 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3853 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3854 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003855
3856 // Get location information.
3857 unsigned Line = getLineNumber(VD->getLocation());
3858 unsigned Column = getColumnNumber(VD->getLocation());
3859
3860 const llvm::DataLayout &target = CGM.getDataLayout();
3861
3862 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003863 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003864 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3865
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003866 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003867 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003868 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003869 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003870 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003871 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003872 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003873 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003874 offset =
3875 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003876 addr.push_back(offset.getQuantity());
3877 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003878 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003879 // offset of x field
3880 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003881 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003882 }
3883
3884 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003885 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003886 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003887 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003888 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003889
Guy Benyei11169dd2012-12-18 14:30:41 +00003890 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003891 auto DL =
3892 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003893 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003894 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003895 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003896 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003897 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003898}
3899
Guy Benyei11169dd2012-12-18 14:30:41 +00003900void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3901 unsigned ArgNo,
3902 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003903 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003904 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003905}
3906
3907namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003908struct BlockLayoutChunk {
3909 uint64_t OffsetInBits;
3910 const BlockDecl::Capture *Capture;
3911};
3912bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3913 return l.OffsetInBits < r.OffsetInBits;
3914}
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003915} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00003916
Scott Linder58df0e42018-08-08 15:56:12 +00003917void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
3918 const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
3919 const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
3920 SmallVectorImpl<llvm::Metadata *> &Fields) {
3921 // Blocks in OpenCL have unique constraints which make the standard fields
3922 // redundant while requiring size and align fields for enqueue_kernel. See
3923 // initializeForBlockHeader in CGBlocks.cpp
3924 if (CGM.getLangOpts().OpenCL) {
3925 Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public,
3926 BlockLayout.getElementOffsetInBits(0),
3927 Unit, Unit));
3928 Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
3929 BlockLayout.getElementOffsetInBits(1),
3930 Unit, Unit));
3931 } else {
3932 Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
3933 BlockLayout.getElementOffsetInBits(0),
3934 Unit, Unit));
3935 Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
3936 BlockLayout.getElementOffsetInBits(1),
3937 Unit, Unit));
3938 Fields.push_back(
3939 createFieldType("__reserved", Context.IntTy, Loc, AS_public,
3940 BlockLayout.getElementOffsetInBits(2), Unit, Unit));
3941 auto *FnTy = Block.getBlockExpr()->getFunctionType();
3942 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3943 Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
3944 BlockLayout.getElementOffsetInBits(3),
3945 Unit, Unit));
3946 Fields.push_back(createFieldType(
3947 "__descriptor",
3948 Context.getPointerType(Block.NeedsCopyDispose
3949 ? Context.getBlockDescriptorExtendedType()
3950 : Context.getBlockDescriptorType()),
3951 Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
3952 }
3953}
3954
Guy Benyei11169dd2012-12-18 14:30:41 +00003955void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl356347b2017-10-26 20:08:52 +00003956 StringRef Name,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003957 unsigned ArgNo,
Adrian Prantl356347b2017-10-26 20:08:52 +00003958 llvm::AllocaInst *Alloca,
Guy Benyei11169dd2012-12-18 14:30:41 +00003959 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003960 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003961 ASTContext &C = CGM.getContext();
3962 const BlockDecl *blockDecl = block.getBlockDecl();
3963
3964 // Collect some general information about the block's location.
3965 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003966 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003967 unsigned line = getLineNumber(loc);
3968 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003969
Guy Benyei11169dd2012-12-18 14:30:41 +00003970 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003971 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003972
3973 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003974 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003975
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003976 SmallVector<llvm::Metadata *, 16> fields;
Scott Linder58df0e42018-08-08 15:56:12 +00003977 collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
3978 fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003979
3980 // We want to sort the captures by offset, not because DWARF
3981 // requires this, but because we're paranoid about debuggers.
3982 SmallVector<BlockLayoutChunk, 8> chunks;
3983
3984 // 'this' capture.
3985 if (blockDecl->capturesCXXThis()) {
3986 BlockLayoutChunk chunk;
3987 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003988 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003989 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003990 chunks.push_back(chunk);
3991 }
3992
3993 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003994 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003995 const VarDecl *variable = capture.getVariable();
3996 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3997
3998 // Ignore constant captures.
3999 if (captureInfo.isConstant())
4000 continue;
4001
4002 BlockLayoutChunk chunk;
4003 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00004004 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00004005 chunk.Capture = &capture;
4006 chunks.push_back(chunk);
4007 }
4008
4009 // Sort by offset.
4010 llvm::array_pod_sort(chunks.begin(), chunks.end());
4011
David Majnemer58ed0f32016-07-17 00:39:12 +00004012 for (const BlockLayoutChunk &Chunk : chunks) {
4013 uint64_t offsetInBits = Chunk.OffsetInBits;
4014 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00004015
4016 // If we have a null capture, this must be the C++ 'this' capture.
4017 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00004018 QualType type;
4019 if (auto *Method =
4020 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
4021 type = Method->getThisType(C);
4022 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
4023 type = QualType(RDecl->getTypeForDecl(), 0);
4024 else
4025 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00004026
David Majnemerb4b671e2016-06-30 03:01:59 +00004027 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00004028 offsetInBits, tunit, tunit));
4029 continue;
4030 }
4031
4032 const VarDecl *variable = capture->getVariable();
4033 StringRef name = variable->getName();
4034
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004035 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00004036 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00004037 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00004038 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Adrian Prantl05a623e2018-09-10 16:14:28 +00004039 // FIXME: This recomputes the layout of the BlockByRefWrapper.
Guy Benyei11169dd2012-12-18 14:30:41 +00004040 uint64_t xoffset;
Adrian Prantl05a623e2018-09-10 16:14:28 +00004041 fieldType =
4042 EmitTypeForVarWithBlocksAttr(variable, &xoffset).BlockByRefWrapper;
David Majnemer34b57492014-07-30 01:30:47 +00004043 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00004044 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
4045 PtrInfo.Width, Align, offsetInBits,
4046 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00004047 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00004048 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00004049 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00004050 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00004051 }
4052 fields.push_back(fieldType);
4053 }
4054
4055 SmallString<36> typeName;
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004056 llvm::raw_svector_ostream(typeName)
4057 << "__block_literal_" << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00004058
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004059 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00004060
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004061 llvm::DIType *type =
4062 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00004063 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004064 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00004065 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
4066
4067 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00004068 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004069 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00004070
4071 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00004072 auto *debugVar = DBuilder.createParameterVariable(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004073 scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00004074
Adrian Prantl616bef42013-03-14 21:52:59 +00004075 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl356347b2017-10-26 20:08:52 +00004076 DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00004077 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00004078 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00004079}
4080
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004081llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00004082CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
4083 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00004084 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00004085
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004086 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00004087 if (MI != StaticDataMemberCache.end()) {
4088 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00004089 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00004090 }
David Blaikiece763042013-08-20 21:49:21 +00004091
4092 // If the member wasn't found in the cache, lazily construct and add it to the
4093 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00004094 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004095 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00004096 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00004097}
4098
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004099llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004100 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
4101 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004102 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004103
4104 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004105 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00004106 StringRef FieldName = Field->getName();
4107
4108 // Ignore unnamed fields, but recurse into anonymous records.
4109 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00004110 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004111 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004112 Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004113 continue;
4114 }
4115 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004116 GVE = DBuilder.createGlobalVariableExpression(
4117 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
4118 Var->hasLocalLinkage());
4119 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004120 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004121 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00004122}
4123
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004124void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00004125 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004126 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004127 if (D->hasAttr<NoDebugAttr>())
4128 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004129
4130 // If we already created a DIGlobalVariable for this declaration, just attach
4131 // it to the llvm::GlobalVariable.
4132 auto Cached = DeclCache.find(D->getCanonicalDecl());
4133 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004134 return Var->addDebugInfo(
4135 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004136
Guy Benyei11169dd2012-12-18 14:30:41 +00004137 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004138 llvm::DIFile *Unit = nullptr;
4139 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00004140 unsigned LineNo;
4141 StringRef DeclName, LinkageName;
4142 QualType T;
Matthew Voss20165362018-10-03 18:45:04 +00004143 llvm::MDTuple *TemplateParameters = nullptr;
4144 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName,
4145 TemplateParameters, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004146
4147 // Attempt to store one global variable for the declaration - even if we
4148 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004149 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004150
4151 // If this is an anonymous union then we'll want to emit a global
4152 // variable for each member of the anonymous union so that it's possible
4153 // to find the name of any field in the union.
4154 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00004155 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00004156 assert(RD->isAnonymousStructOrUnion() &&
4157 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004158 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004159 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00004160 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004161
4162 SmallVector<int64_t, 4> Expr;
4163 unsigned AddressSpace =
4164 CGM.getContext().getTargetAddressSpace(D->getType());
4165 AppendAddressSpaceXDeref(AddressSpace, Expr);
4166
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004167 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00004168 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004169 Var->hasLocalLinkage(),
4170 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Matthew Voss20165362018-10-03 18:45:04 +00004171 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
4172 Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004173 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004174 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004175 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004176}
4177
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004178void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004179 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004180 if (VD->hasAttr<NoDebugAttr>())
4181 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00004182 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004183 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004184 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004185 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004186 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00004187 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
4188 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004189 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
4190 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
4191 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004192 // Do not use global variables for enums.
4193 //
4194 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004195 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004196 return;
David Blaikiea15565562014-04-04 20:56:17 +00004197 // Do not emit separate definitions for function local const/statics.
4198 if (isa<FunctionDecl>(VD->getDeclContext()))
4199 return;
David Blaikiebb113912014-04-05 07:23:17 +00004200 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00004201 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004202 if (VarD->isStaticDataMember()) {
4203 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004204 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00004205 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00004206 //
4207 // FIXME: This is probably unnecessary, since Ty should reference RD
4208 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00004209 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00004210 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00004211 return;
4212 }
4213
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004214 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004215
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004216 auto &GV = DeclCache[VD];
4217 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00004218 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004219 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00004220 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
4221 // FIXME: Add a representation for integer constants wider than 64 bits.
4222 if (Init.isInt())
4223 InitExpr =
4224 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
4225 else if (Init.isFloat())
4226 InitExpr = DBuilder.createConstantValueExpression(
4227 Init.getFloat().bitcastToAPInt().getZExtValue());
4228 }
Matthew Voss20165362018-10-03 18:45:04 +00004229
4230 llvm::MDTuple *TemplateParameters = nullptr;
4231
4232 if (isa<VarTemplateSpecializationDecl>(VD))
4233 if (VarD) {
4234 llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VarD, &*Unit);
4235 TemplateParameters = parameterNodes.get();
4236 }
4237
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004238 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00004239 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00004240 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
Matthew Voss20165362018-10-03 18:45:04 +00004241 TemplateParameters, Align));
David Blaikiebd483762013-05-20 04:58:53 +00004242}
4243
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004244llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00004245 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00004246 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00004247 llvm::DIScope *Mod = getParentModuleOrNull(D);
4248 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00004249}
4250
David Blaikie9f88fe82013-04-22 06:13:21 +00004251void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004252 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004253 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004254 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00004255 if (!NSDecl->isAnonymousNamespace() ||
4256 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004257 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004258 DBuilder.createImportedModule(
4259 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004260 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004261 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004262}
4263
David Blaikiebd483762013-05-20 04:58:53 +00004264void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004265 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004266 return;
4267 assert(UD.shadow_size() &&
4268 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4269 // Emitting one decl is sufficient - debuggers can detect that this is an
4270 // overloaded name & provide lookup for all the overloads.
4271 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004272
4273 // FIXME: Skip functions with undeduced auto return type for now since we
4274 // don't currently have the plumbing for separate declarations & definitions
4275 // of free functions and mismatched types (auto in the declaration, concrete
4276 // return type in the definition)
4277 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4278 if (const auto *AT =
4279 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4280 if (AT->getDeducedType().isNull())
4281 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004282 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004283 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4284 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004285 DBuilder.createImportedDeclaration(
4286 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004287 getOrCreateFile(Loc), getLineNumber(Loc));
4288 }
David Blaikiebd483762013-05-20 04:58:53 +00004289}
4290
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004291void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004292 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4293 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004294 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004295 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004296 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004297 DBuilder.createImportedDeclaration(
4298 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004299 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4300 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004301 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004302}
4303
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004304llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004305CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004306 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004307 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004308 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004309 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004310 return cast<llvm::DIImportedEntity>(VH);
4311 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004312 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004313 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004314 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4315 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004316 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004317 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004318 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4319 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004320 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004321 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004322 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004323 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004324 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004325 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004326 return R;
4327}
4328
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004329llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004330CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4331 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4332 // if necessary, and this way multiple declarations of the same namespace in
4333 // different parent modules stay distinct.
4334 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004335 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004336 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004337
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004338 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004339 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004340 llvm::DINamespace *NS =
4341 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4342 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004343 return NS;
4344}
4345
Adrian Prantla5206ce2015-09-22 23:26:43 +00004346void CGDebugInfo::setDwoId(uint64_t Signature) {
4347 assert(TheCU && "no main compile unit");
4348 TheCU->setDWOId(Signature);
4349}
4350
Guy Benyei11169dd2012-12-18 14:30:41 +00004351void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004352 // Creating types might create further types - invalidating the current
4353 // element and the size(), so don't cache/reference them.
4354 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4355 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004356 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004357 ? CreateTypeDefinition(E.Type, E.Unit)
4358 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004359 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004360 }
4361
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004362 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
4363 // Add methods to interface.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004364 for (const auto &P : ObjCMethodCache) {
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004365 if (P.second.empty())
4366 continue;
4367
4368 QualType QTy(P.first->getTypeForDecl(), 0);
4369 auto It = TypeCache.find(QTy.getAsOpaquePtr());
4370 assert(It != TypeCache.end());
4371
4372 llvm::DICompositeType *InterfaceDecl =
4373 cast<llvm::DICompositeType>(It->second);
4374
4375 SmallVector<llvm::Metadata *, 16> EltTys;
4376 auto CurrenetElts = InterfaceDecl->getElements();
4377 EltTys.append(CurrenetElts.begin(), CurrenetElts.end());
4378 for (auto &MD : P.second)
4379 EltTys.push_back(MD);
4380 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
4381 DBuilder.replaceArrays(InterfaceDecl, Elements);
4382 }
4383 }
4384
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004385 for (const auto &P : ReplaceMap) {
4386 assert(P.second);
4387 auto *Ty = cast<llvm::DIType>(P.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004388 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004389
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004390 auto It = TypeCache.find(P.first);
4391 assert(It != TypeCache.end());
4392 assert(It->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004394 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004395 cast<llvm::DIType>(It->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004396 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004397
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004398 for (const auto &P : FwdDeclReplaceMap) {
4399 assert(P.second);
4400 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004401 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004402
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004403 auto It = DeclCache.find(P.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004404 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004405 // with ourselves, that will destroy the temporary MDNode and
4406 // replace it with a standard one, avoiding leaking memory.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004407 if (It == DeclCache.end())
4408 Repl = P.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004409 else
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004410 Repl = It->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004411
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004412 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4413 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004414 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004415 }
4416
Adrian Prantl73409ce2013-03-11 18:33:46 +00004417 // We keep our own list of retained types, because we need to look
4418 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004419 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004420 if (auto MD = TypeCache[RT])
4421 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004422
Guy Benyei11169dd2012-12-18 14:30:41 +00004423 DBuilder.finalize();
4424}
David Blaikie66088d52014-09-24 17:01:27 +00004425
4426void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004427 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004428 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004429
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004430 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004431 // Don't ignore in case of explicit cast where it is referenced indirectly.
4432 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004433}
Amara Emerson652795d2016-11-10 14:44:30 +00004434
4435llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4436 if (LexicalBlockStack.empty())
4437 return llvm::DebugLoc();
4438
4439 llvm::MDNode *Scope = LexicalBlockStack.back();
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004440 return llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc), Scope);
Amara Emerson652795d2016-11-10 14:44:30 +00004441}
Vedant Kumar5931b4e2018-10-05 20:37:17 +00004442
4443llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
4444 // Call site-related attributes are only useful in optimized programs, and
4445 // when there's a possibility of debugging backtraces.
4446 if (!CGM.getLangOpts().Optimize || DebugKind == codegenoptions::NoDebugInfo ||
4447 DebugKind == codegenoptions::LocTrackingOnly)
4448 return llvm::DINode::FlagZero;
4449
4450 // Call site-related attributes are available in DWARF v5. Some debuggers,
4451 // while not fully DWARF v5-compliant, may accept these attributes as if they
4452 // were part of DWARF v4.
4453 bool SupportsDWARFv4Ext =
4454 CGM.getCodeGenOpts().DwarfVersion == 4 &&
4455 CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB;
4456 if (!SupportsDWARFv4Ext && CGM.getCodeGenOpts().DwarfVersion < 5)
4457 return llvm::DINode::FlagZero;
4458
4459 return llvm::DINode::FlagAllCallsDescribed;
4460}