blob: fe2de36ac42b8aec583831eb898d92b1a047b0c2 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
John McCallde0fe072017-08-15 21:42:52 +000021#include "ConstantEmitter.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/RecordLayout.h"
28#include "clang/Basic/FileManager.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000031#include "clang/Frontend/CodeGenOptions.h"
Taewook Oh0fb5b782017-08-16 19:36:24 +000032#include "clang/Frontend/FrontendOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000034#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000035#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000036#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000037#include "llvm/ADT/SmallVector.h"
38#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000039#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000046#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000047#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000048using namespace clang;
49using namespace clang::CodeGen;
50
Victor Leschuka7ece032016-10-20 00:13:19 +000051static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
52 auto TI = Ctx.getTypeInfo(Ty);
53 return TI.AlignIsRequired ? TI.Align : 0;
54}
55
56static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
57 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
58}
59
60static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
61 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
62}
63
Guy Benyei11169dd2012-12-18 14:30:41 +000064CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000065 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000066 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000067 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000068 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
69 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000070 CreateCompileUnit();
71}
72
73CGDebugInfo::~CGDebugInfo() {
74 assert(LexicalBlockStack.empty() &&
75 "Region stack mismatch, stack not empty!");
76}
77
David Blaikie66e41972015-01-14 07:38:27 +000078ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000079 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000080 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000081 init(TemporaryLocation);
82}
83
Adrian Prantl39428e72015-02-03 18:40:42 +000084ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000085 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000086 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000087 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000088 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000089}
90
91void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000092 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000093 auto *DI = CGF->getDebugInfo();
94 if (!DI) {
95 CGF = nullptr;
96 return;
David Blaikie66e41972015-01-14 07:38:27 +000097 }
David Blaikied7057d92015-08-12 23:49:57 +000098
99 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
100 if (TemporaryLocation.isValid()) {
101 DI->EmitLocation(CGF->Builder, TemporaryLocation);
102 return;
103 }
104
105 if (DefaultToEmpty) {
106 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
107 return;
108 }
109
110 // Construct a location that has a valid scope, but no line info.
111 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000112 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
113 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000114}
115
David Blaikie9b479662015-01-25 01:19:10 +0000116ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000117 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000118 init(E->getExprLoc());
119}
120
David Blaikie66e41972015-01-14 07:38:27 +0000121ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000122 : CGF(&CGF) {
123 if (!CGF.getDebugInfo()) {
124 this->CGF = nullptr;
125 return;
David Blaikie66e41972015-01-14 07:38:27 +0000126 }
David Blaikied7057d92015-08-12 23:49:57 +0000127 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
128 if (Loc)
129 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000130}
131
132ApplyDebugLocation::~ApplyDebugLocation() {
133 // Query CGF so the location isn't overwritten when location updates are
134 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000135 if (CGF)
136 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000137}
138
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000139ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
140 GlobalDecl InlinedFn)
141 : CGF(&CGF) {
142 if (!CGF.getDebugInfo()) {
143 this->CGF = nullptr;
144 return;
145 }
146 auto &DI = *CGF.getDebugInfo();
147 SavedLocation = DI.getLocation();
148 assert((DI.getInlinedAt() ==
149 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
150 "CGDebugInfo and IRBuilder are out of sync");
151
152 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
153}
154
155ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
156 if (!CGF)
157 return;
158 auto &DI = *CGF->getDebugInfo();
159 DI.EmitInlineFunctionEnd(CGF->Builder);
160 DI.EmitLocation(CGF->Builder, SavedLocation);
161}
162
Guy Benyei11169dd2012-12-18 14:30:41 +0000163void CGDebugInfo::setLocation(SourceLocation Loc) {
164 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000165 if (Loc.isInvalid())
166 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000167
168 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
169
170 // If we've changed files in the middle of a lexical scope go ahead
171 // and create a new lexical scope with file node if it's different
172 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000173 if (LexicalBlockStack.empty())
174 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000175
176 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000177 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000178 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000179
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000180 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000181 return;
182
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000183 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000184 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000185 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
186 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000187 } else if (isa<llvm::DILexicalBlock>(Scope) ||
188 isa<llvm::DISubprogram>(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(
191 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000192 }
193}
194
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000195llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000196 llvm::DIScope *Mod = getParentModuleOrNull(D);
197 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
198 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000199}
200
201llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
202 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000203 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000204 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000205
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000206 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000207 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000208 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000209 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000210 }
211
212 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000213 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
214 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000215
David Majnemer58ed0f32016-07-17 00:39:12 +0000216 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000217 if (!RDecl->isDependentType())
218 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000219 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000220 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000221}
222
Reid Kleckner59d12202017-08-08 01:33:53 +0000223PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
224 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
225
226 // If we're emitting codeview, it's important to try to match MSVC's naming so
227 // that visualizers written for MSVC will trigger for our class names. In
228 // particular, we can't have spaces between arguments of standard templates
229 // like basic_string and vector.
230 if (CGM.getCodeGenOpts().EmitCodeView)
231 PP.MSVCFormatting = true;
232
233 return PP;
234}
235
Guy Benyei11169dd2012-12-18 14:30:41 +0000236StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000237 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000238 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000239 FunctionTemplateSpecializationInfo *Info =
240 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000241
Reid Kleckner31abd802016-06-30 17:41:31 +0000242 // Emit the unqualified name in normal operation. LLVM and the debugger can
243 // compute the fully qualified name from the scope chain. If we're only
244 // emitting line table info, there won't be any scope chains, so emit the
245 // fully qualified name here so that stack traces are more accurate.
246 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
247 // evaluating the size impact.
248 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
249 CGM.getCodeGenOpts().EmitCodeView;
250
251 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000252 return FII->getName();
253
Benjamin Kramer9170e912013-02-22 15:46:01 +0000254 SmallString<128> NS;
255 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000256 if (!UseQualifiedName)
257 FD->printName(OS);
258 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000259 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000260
Reid Kleckner829398e2016-06-17 16:11:20 +0000261 // Add any template specialization args.
262 if (Info) {
263 const TemplateArgumentList *TArgs = Info->TemplateArguments;
David Majnemer6fbeee32016-07-07 04:43:07 +0000264 TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(),
Reid Kleckner59d12202017-08-08 01:33:53 +0000265 getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000266 }
267
268 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000269 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000270}
271
272StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
273 SmallString<256> MethodName;
274 llvm::raw_svector_ostream OS(MethodName);
275 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
276 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000277 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000278 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000279 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000280 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000281 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000282 if (OC->IsClassExtension()) {
283 OS << OC->getClassInterface()->getName();
284 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000285 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000286 << OC->getIdentifier()->getNameStart() << ')';
287 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000288 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +0000289 OS << OCD->getClassInterface()->getName() << '('
290 << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000291 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000292 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000293 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000294 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000295 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000296 ClassTy.print(OS, PrintingPolicy(LangOptions()));
297 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000298 }
299 OS << ' ' << OMD->getSelector().getAsString() << ']';
300
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000301 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000302}
303
Guy Benyei11169dd2012-12-18 14:30:41 +0000304StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000305 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000306}
307
Eric Christophere7b87e52014-10-26 23:40:33 +0000308StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000309 if (isa<ClassTemplateSpecializationDecl>(RD)) {
310 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000311 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000312 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000313 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000314
315 // Copy this name on the side and use its reference.
316 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000317 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000318
David Majnemerbc5976a2016-07-01 23:12:54 +0000319 // quick optimization to avoid having to intern strings that are already
320 // stored reliably elsewhere
321 if (const IdentifierInfo *II = RD->getIdentifier())
322 return II->getName();
323
324 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
325 // used to reconstruct the fully qualified type names.
326 if (CGM.getCodeGenOpts().EmitCodeView) {
327 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
328 assert(RD->getDeclContext() == D->getDeclContext() &&
329 "Typedef should not be in another decl context!");
330 assert(D->getDeclName().getAsIdentifierInfo() &&
331 "Typedef was not named!");
332 return D->getDeclName().getAsIdentifierInfo()->getName();
333 }
334
335 if (CGM.getLangOpts().CPlusPlus) {
336 StringRef Name;
337
338 ASTContext &Context = CGM.getContext();
339 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
340 // Anonymous types without a name for linkage purposes have their
341 // declarator mangled in if they have one.
342 Name = DD->getName();
343 else if (const TypedefNameDecl *TND =
344 Context.getTypedefNameForUnnamedTagDecl(RD))
345 // Anonymous types without a name for linkage purposes have their
346 // associate typedef mangled in if they have one.
347 Name = TND->getName();
348
349 if (!Name.empty()) {
350 SmallString<256> UnnamedType("<unnamed-type-");
351 UnnamedType += Name;
352 UnnamedType += '>';
353 return internString(UnnamedType);
354 }
355 }
356 }
357
358 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000359}
360
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000361llvm::DIFile::ChecksumKind
362CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
363 Checksum.clear();
364
365 if (!CGM.getCodeGenOpts().EmitCodeView)
366 return llvm::DIFile::CSK_None;
367
368 SourceManager &SM = CGM.getContext().getSourceManager();
369 bool Invalid;
370 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
371 if (Invalid)
372 return llvm::DIFile::CSK_None;
373
374 llvm::MD5 Hash;
375 llvm::MD5::MD5Result Result;
376
377 Hash.update(MemBuffer->getBuffer());
378 Hash.final(Result);
379
380 Hash.stringifyResult(Result, Checksum);
381 return llvm::DIFile::CSK_MD5;
382}
383
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000384llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000385 if (!Loc.isValid())
386 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000387 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000388 remapDIPath(TheCU->getDirectory()),
389 TheCU->getFile()->getChecksumKind(),
390 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000391
392 SourceManager &SM = CGM.getContext().getSourceManager();
393 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
394
395 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
396 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000397 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000398 remapDIPath(TheCU->getDirectory()),
399 TheCU->getFile()->getChecksumKind(),
400 TheCU->getFile()->getChecksum());
Guy Benyei11169dd2012-12-18 14:30:41 +0000401
402 // Cache the results.
403 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000404 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000405
406 if (it != DIFileCache.end()) {
407 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000408 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000409 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 }
411
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000412 SmallString<32> Checksum;
413 llvm::DIFile::ChecksumKind CSKind =
414 computeChecksum(SM.getFileID(Loc), Checksum);
415
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000416 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000417 remapDIPath(getCurrentDirname()),
418 CSKind, Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000420 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000421 return F;
422}
423
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000424llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000425 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000426 remapDIPath(TheCU->getDirectory()),
427 TheCU->getFile()->getChecksumKind(),
428 TheCU->getFile()->getChecksum());
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000429}
430
431std::string CGDebugInfo::remapDIPath(StringRef Path) const {
432 for (const auto &Entry : DebugPrefixMap)
433 if (Path.startswith(Entry.first))
434 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
435 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000436}
437
Guy Benyei11169dd2012-12-18 14:30:41 +0000438unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
439 if (Loc.isInvalid() && CurLoc.isInvalid())
440 return 0;
441 SourceManager &SM = CGM.getContext().getSourceManager();
442 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000444}
445
Adrian Prantlc7822422013-03-12 20:43:25 +0000446unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000448 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000449 return 0;
450
451 // If the location is invalid then use the current column.
452 if (Loc.isInvalid() && CurLoc.isInvalid())
453 return 0;
454 SourceManager &SM = CGM.getContext().getSourceManager();
455 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000456 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000457}
458
459StringRef CGDebugInfo::getCurrentDirname() {
460 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
461 return CGM.getCodeGenOpts().DebugCompilationDir;
462
463 if (!CWDName.empty())
464 return CWDName;
465 SmallString<256> CWD;
466 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000467 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000468}
469
Guy Benyei11169dd2012-12-18 14:30:41 +0000470void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000471 SmallString<32> Checksum;
472 llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473
David Blaikieaabde052014-05-14 00:29:00 +0000474 // Should we be asking the SourceManager for the main file name, instead of
475 // accepting it as an argument? This just causes the main file name to
476 // mismatch with source locations and create extra lexical scopes or
477 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
478 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
479 // because that's what the SourceManager says)
480
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 // Get absolute path name.
482 SourceManager &SM = CGM.getContext().getSourceManager();
483 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
484 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000485 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000486
487 // The main file name provided via the "-main-file-name" option contains just
488 // the file name itself with no path information. This file name may have had
489 // a relative path, so we look into the actual file entry for the main
490 // file to determine the real absolute path for the file.
491 std::string MainFileDir;
492 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000493 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000494 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000495 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
496 llvm::sys::path::append(MainFileDirSS, MainFileName);
497 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000498 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000499 // If the main file name provided is identical to the input file name, and
500 // if the input file is a preprocessed source, use the module name for
501 // debug info. The module name comes from the name specified in the first
502 // linemarker if the input is a preprocessed source.
503 if (MainFile->getName() == MainFileName &&
504 FrontendOptions::getInputKindForExtension(
505 MainFile->getName().rsplit('.').second)
506 .isPreprocessed())
507 MainFileName = CGM.getModule().getName().str();
508
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000509 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000510 }
511
Ed Masteda706022014-05-07 12:49:30 +0000512 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 const LangOptions &LO = CGM.getLangOpts();
514 if (LO.CPlusPlus) {
515 if (LO.ObjC1)
516 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
517 else
518 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
519 } else if (LO.ObjC1) {
520 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000521 } else if (LO.RenderScript) {
522 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000523 } else if (LO.C99) {
524 LangTag = llvm::dwarf::DW_LANG_C99;
525 } else {
526 LangTag = llvm::dwarf::DW_LANG_C89;
527 }
528
529 std::string Producer = getClangFullVersion();
530
531 // Figure out which version of the ObjC runtime we have.
532 unsigned RuntimeVers = 0;
533 if (LO.ObjC1)
534 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
535
Adrian Prantl826824e2016-04-08 22:43:06 +0000536 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
537 switch (DebugKind) {
538 case codegenoptions::NoDebugInfo:
539 case codegenoptions::LocTrackingOnly:
540 EmissionKind = llvm::DICompileUnit::NoDebug;
541 break;
542 case codegenoptions::DebugLineTablesOnly:
543 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
544 break;
545 case codegenoptions::LimitedDebugInfo:
546 case codegenoptions::FullDebugInfo:
547 EmissionKind = llvm::DICompileUnit::FullDebug;
548 break;
549 }
550
Guy Benyei11169dd2012-12-18 14:30:41 +0000551 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000552 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000553 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000554 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000555 LangTag,
556 DBuilder.createFile(remapDIPath(MainFileName),
557 remapDIPath(getCurrentDirname()), CSKind, Checksum),
Adrian Prantlb4423022017-08-04 23:08:57 +0000558 Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex,
559 CGOpts.DwarfDebugFlags, RuntimeVers,
560 CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind,
561 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling);
Guy Benyei11169dd2012-12-18 14:30:41 +0000562}
563
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000564llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000565 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000566 StringRef BTName;
567 switch (BT->getKind()) {
568#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000569#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000570#include "clang/AST/BuiltinTypes.def"
571 case BuiltinType::Dependent:
572 llvm_unreachable("Unexpected builtin type");
573 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000574 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000575 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000576 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000577 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000578 if (!ClassTy)
579 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
580 "objc_class", TheCU,
581 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 return ClassTy;
583 case BuiltinType::ObjCId: {
584 // typedef struct objc_class *Class;
585 // typedef struct objc_object {
586 // Class isa;
587 // } *id;
588
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000589 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000590 return ObjTy;
591
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000592 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
594 "objc_class", TheCU,
595 getOrCreateMainFile(), 0);
596
597 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000598
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000599 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000600
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000601 ObjTy = DBuilder.createStructType(
602 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
603 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000604
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000605 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000606 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
607 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
608 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000609 return ObjTy;
610 }
611 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000612 if (!SelTy)
613 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
614 "objc_selector", TheCU,
615 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 return SelTy;
617 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000618
Alexey Bader954ba212016-04-08 13:40:33 +0000619#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
620 case BuiltinType::Id: \
621 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
622 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000623#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000624 case BuiltinType::OCLSampler:
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +0000625 return getOrCreateStructPtrType("opencl_sampler_t",
626 OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000627 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000628 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000629 case BuiltinType::OCLClkEvent:
630 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
631 case BuiltinType::OCLQueue:
632 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000633 case BuiltinType::OCLReserveID:
634 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000635
Guy Benyei11169dd2012-12-18 14:30:41 +0000636 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000637 case BuiltinType::Char_U:
638 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
639 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000641 case BuiltinType::SChar:
642 Encoding = llvm::dwarf::DW_ATE_signed_char;
643 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000644 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000645 case BuiltinType::Char32:
646 Encoding = llvm::dwarf::DW_ATE_UTF;
647 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000648 case BuiltinType::UShort:
649 case BuiltinType::UInt:
650 case BuiltinType::UInt128:
651 case BuiltinType::ULong:
652 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000653 case BuiltinType::ULongLong:
654 Encoding = llvm::dwarf::DW_ATE_unsigned;
655 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000656 case BuiltinType::Short:
657 case BuiltinType::Int:
658 case BuiltinType::Int128:
659 case BuiltinType::Long:
660 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000661 case BuiltinType::LongLong:
662 Encoding = llvm::dwarf::DW_ATE_signed;
663 break;
664 case BuiltinType::Bool:
665 Encoding = llvm::dwarf::DW_ATE_boolean;
666 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000667 case BuiltinType::Half:
668 case BuiltinType::Float:
669 case BuiltinType::LongDouble:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000670 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000671 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000672 // FIXME: For targets where long double and __float128 have the same size,
673 // they are currently indistinguishable in the debugger without some
674 // special treatment. However, there is currently no consensus on encoding
675 // and this should be updated once a DWARF encoding exists for distinct
676 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000677 Encoding = llvm::dwarf::DW_ATE_float;
678 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000679 }
680
681 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000682 case BuiltinType::Long:
683 BTName = "long int";
684 break;
685 case BuiltinType::LongLong:
686 BTName = "long long int";
687 break;
688 case BuiltinType::ULong:
689 BTName = "long unsigned int";
690 break;
691 case BuiltinType::ULongLong:
692 BTName = "long long unsigned int";
693 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000694 default:
695 BTName = BT->getName(CGM.getLangOpts());
696 break;
697 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000698 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000700 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000701}
702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000703llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000704 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000705 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 if (Ty->isComplexIntegerType())
707 Encoding = llvm::dwarf::DW_ATE_lo_user;
708
709 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000710 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000711}
712
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000713llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
714 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000715 QualifierCollector Qc;
716 const Type *T = Qc.strip(Ty);
717
718 // Ignore these qualifiers for now.
719 Qc.removeObjCGCAttr();
720 Qc.removeAddressSpace();
721 Qc.removeObjCLifetime();
722
723 // We will create one Derived type for one qualifier and recurse to handle any
724 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000725 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000726 if (Qc.hasConst()) {
727 Tag = llvm::dwarf::DW_TAG_const_type;
728 Qc.removeConst();
729 } else if (Qc.hasVolatile()) {
730 Tag = llvm::dwarf::DW_TAG_volatile_type;
731 Qc.removeVolatile();
732 } else if (Qc.hasRestrict()) {
733 Tag = llvm::dwarf::DW_TAG_restrict_type;
734 Qc.removeRestrict();
735 } else {
736 assert(Qc.empty() && "Unknown type qualifier for debug info");
737 return getOrCreateType(QualType(T, 0), Unit);
738 }
739
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000740 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000741
742 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
743 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000744 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745}
746
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000747llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
748 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000749
750 // The frontend treats 'id' as a typedef to an ObjCObjectType,
751 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
752 // debug info, we want to emit 'id' in both cases.
753 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000754 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000755
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000756 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
757 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000758}
759
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000760llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
761 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000762 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000763 Ty->getPointeeType(), Unit);
764}
765
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000766/// \return whether a C++ mangling exists for the type defined by TD.
767static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
768 switch (TheCU->getSourceLanguage()) {
769 case llvm::dwarf::DW_LANG_C_plus_plus:
770 return true;
771 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
772 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
773 default:
774 return false;
775 }
776}
777
Manman Rene0064d82013-08-29 23:19:58 +0000778/// In C++ mode, types have linkage, so we can rely on the ODR and
779/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000780static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
781 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000782 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000783 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000784 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000785
786 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000787 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000788
Manman Rene0064d82013-08-29 23:19:58 +0000789 // TODO: This is using the RTTI name. Is there a better way to get
790 // a unique string for a type?
791 llvm::raw_svector_ostream Out(FullName);
792 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000793 return FullName;
794}
795
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000796/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000797static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
798 llvm::dwarf::Tag Tag;
799 if (RD->isStruct() || RD->isInterface())
800 Tag = llvm::dwarf::DW_TAG_structure_type;
801 else if (RD->isUnion())
802 Tag = llvm::dwarf::DW_TAG_union_type;
803 else {
804 // FIXME: This could be a struct type giving a default visibility different
805 // than C++ class type, but needs llvm metadata changes first.
806 assert(RD->isClass());
807 Tag = llvm::dwarf::DW_TAG_class_type;
808 }
809 return Tag;
810}
811
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000812llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000813CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000814 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000815 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
817 return cast<llvm::DICompositeType>(T);
818 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 unsigned Line = getLineNumber(RD->getLocation());
820 StringRef RDName = getClassName(RD);
821
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000822 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000823 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000824
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000826 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000827 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000828 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000829 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000830 ReplaceMap.emplace_back(
831 std::piecewise_construct, std::make_tuple(Ty),
832 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000833 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000834}
835
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000836llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000837 const Type *Ty,
838 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000839 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 // Bit size, align and offset of the type.
841 // Size is always the size of a pointer. We can't use getTypeSize here
842 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000843 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
844 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000845 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000846 Optional<unsigned> DWARFAddressSpace =
847 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000848
Keno Fischer87842f32015-11-16 09:04:13 +0000849 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
850 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
851 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000852 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000853 else
854 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000855 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000856}
857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000858llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
859 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000860 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000861 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000862 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
863 TheCU, getOrCreateMainFile(), 0);
864 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
865 Cache = DBuilder.createPointerType(Cache, Size);
866 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000867}
868
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000869llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
870 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000871 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000872 QualType FType;
873 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000874 uint32_t FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000876
877 FieldOffset = 0;
878 FType = CGM.getContext().UnsignedLongTy;
879 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
880 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
881
882 Elements = DBuilder.getOrCreateArray(EltTys);
883 EltTys.clear();
884
Leny Kholodov80c047d2016-09-06 10:48:04 +0000885 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000886 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000887
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000888 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000889 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000890 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000891
892 // Bit size, align and offset of the type.
893 uint64_t Size = CGM.getContext().getTypeSize(Ty);
894
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000895 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000896
897 FieldOffset = 0;
898 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
899 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
900 FType = CGM.getContext().IntTy;
901 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
902 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000903 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000904 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
905
906 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000907 FieldSize = CGM.getContext().getTypeSize(Ty);
908 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000909 EltTys.push_back(DBuilder.createMemberType(
910 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset,
911 llvm::DINode::FlagZero, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000912
913 FieldOffset += FieldSize;
914 Elements = DBuilder.getOrCreateArray(EltTys);
915
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000916 // The __block_literal_generic structs are marked with a special
917 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
918 // the debugger needs to know about. To allow type uniquing, emit
919 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000920 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000921 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000922 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000923
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000924 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000925}
926
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000927llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
928 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000929 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000930 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000931
932 SmallString<128> NS;
933 llvm::raw_svector_ostream OS(NS);
Reid Kleckner59d12202017-08-08 01:33:53 +0000934 Ty->getTemplateName().print(OS, getPrintingPolicy(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000935 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000936
937 TemplateSpecializationType::PrintTemplateArgumentList(
Reid Kleckner59d12202017-08-08 01:33:53 +0000938 OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +0000939
David Majnemer58ed0f32016-07-17 00:39:12 +0000940 auto *AliasDecl = cast<TypeAliasTemplateDecl>(
Eric Christophere7b87e52014-10-26 23:40:33 +0000941 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000942
943 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000944 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
945 getLineNumber(Loc),
946 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000947}
948
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000949llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
950 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000951 // We don't set size information, but do specify where the typedef was
952 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +0000953 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000954
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000955 // Typedefs are derived from some other type.
956 return DBuilder.createTypedef(
957 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
958 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +0000959 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000960}
961
Reid Klecknerf00f8032016-06-08 20:41:54 +0000962static unsigned getDwarfCC(CallingConv CC) {
963 switch (CC) {
964 case CC_C:
965 // Avoid emitting DW_AT_calling_convention if the C convention was used.
966 return 0;
967
968 case CC_X86StdCall:
969 return llvm::dwarf::DW_CC_BORLAND_stdcall;
970 case CC_X86FastCall:
971 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
972 case CC_X86ThisCall:
973 return llvm::dwarf::DW_CC_BORLAND_thiscall;
974 case CC_X86VectorCall:
975 return llvm::dwarf::DW_CC_LLVM_vectorcall;
976 case CC_X86Pascal:
977 return llvm::dwarf::DW_CC_BORLAND_pascal;
978
979 // FIXME: Create new DW_CC_ codes for these calling conventions.
Martin Storsjo022e7822017-07-17 20:49:45 +0000980 case CC_Win64:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000981 case CC_X86_64SysV:
982 case CC_AAPCS:
983 case CC_AAPCS_VFP:
984 case CC_IntelOclBicc:
985 case CC_SpirFunction:
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000986 case CC_OpenCLKernel:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000987 case CC_Swift:
988 case CC_PreserveMost:
989 case CC_PreserveAll:
Erich Keane757d3172016-11-02 18:29:35 +0000990 case CC_X86RegCall:
Reid Klecknerf00f8032016-06-08 20:41:54 +0000991 return 0;
992 }
993 return 0;
994}
995
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000996llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
997 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000998 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000999
1000 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001001 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001002
1003 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001004 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001005 if (isa<FunctionNoProtoType>(Ty))
1006 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001007 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1008 for (const QualType &ParamType : FPT->param_types())
1009 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001010 if (FPT->isVariadic())
1011 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001012 }
1013
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001014 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001015 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001016 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001017}
1018
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001019/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001020/// As an optimization, return 0 if the access specifier equals the
1021/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001022static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1023 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001024 AccessSpecifier Default = clang::AS_none;
1025 if (RD && RD->isClass())
1026 Default = clang::AS_private;
1027 else if (RD && (RD->isStruct() || RD->isUnion()))
1028 Default = clang::AS_public;
1029
1030 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001031 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001032
Eric Christophere7b87e52014-10-26 23:40:33 +00001033 switch (Access) {
1034 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001035 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001036 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001037 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001038 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001039 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001040 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001041 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001042 }
1043 llvm_unreachable("unexpected access enumerator");
1044}
Guy Benyei11169dd2012-12-18 14:30:41 +00001045
David Majnemerb4b671e2016-06-30 03:01:59 +00001046llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1047 llvm::DIScope *RecordTy,
1048 const RecordDecl *RD) {
1049 StringRef Name = BitFieldDecl->getName();
1050 QualType Ty = BitFieldDecl->getType();
1051 SourceLocation Loc = BitFieldDecl->getLocation();
1052 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1053 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1054
1055 // Get the location for the field.
1056 llvm::DIFile *File = getOrCreateFile(Loc);
1057 unsigned Line = getLineNumber(Loc);
1058
1059 const CGBitFieldInfo &BitFieldInfo =
1060 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1061 uint64_t SizeInBits = BitFieldInfo.Size;
1062 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001063 uint64_t StorageOffsetInBits =
1064 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001065 uint64_t Offset = BitFieldInfo.Offset;
1066 // The bit offsets for big endian machines are reversed for big
1067 // endian target, compensate for that as the DIDerivedType requires
1068 // un-reversed offsets.
1069 if (CGM.getDataLayout().isBigEndian())
1070 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1071 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001072 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001073 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001074 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1075 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001076}
1077
1078llvm::DIType *
1079CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1080 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001081 uint32_t AlignInBits, llvm::DIFile *tunit,
1082 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001083 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001084
1085 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001086 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001087 unsigned line = getLineNumber(loc);
1088
David Majnemer34b57492014-07-30 01:30:47 +00001089 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001090 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001091 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001092 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1093 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001094 if (!Align)
1095 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 }
1097
Leny Kholodov80c047d2016-09-06 10:48:04 +00001098 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +00001099 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001100 Align, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001101}
1102
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001103void CGDebugInfo::CollectRecordLambdaFields(
1104 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001105 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001106 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1107 // has the name and the location of the variable so we should iterate over
1108 // both concurrently.
1109 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1110 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1111 unsigned fieldno = 0;
1112 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001113 E = CXXDecl->captures_end();
1114 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001115 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001116 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001117 SourceLocation Loc = C.getLocation();
1118 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001119 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001120 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001121 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001122 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001123 llvm::DIType *FieldType = createFieldType(
1124 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001125 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001126 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001127 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001128 // TODO: Need to handle 'this' in some way by probably renaming the
1129 // this of the lambda class and having a field member of 'this' or
1130 // by using AT_object_pointer for the function and having that be
1131 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001132 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001133 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001134 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001136 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001137 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001138
1139 elements.push_back(fieldType);
1140 }
1141 }
1142}
1143
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001144llvm::DIDerivedType *
1145CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001146 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001147 // Create the descriptor for the static variable, with or without
1148 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001149 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1151 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001152
Eric Christopher91a31902013-01-16 01:22:32 +00001153 unsigned LineNumber = getLineNumber(Var->getLocation());
1154 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001155 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001156 if (Var->getInit()) {
1157 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001158 if (Value) {
1159 if (Value->isInt())
1160 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1161 if (Value->isFloat())
1162 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1163 }
Eric Christopher91a31902013-01-16 01:22:32 +00001164 }
1165
Leny Kholodov80c047d2016-09-06 10:48:04 +00001166 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001167 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001168 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001169 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001170 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001171 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001172}
1173
Eric Christophere7b87e52014-10-26 23:40:33 +00001174void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001175 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1176 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001177 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001178 StringRef name = field->getName();
1179 QualType type = field->getType();
1180
1181 // Ignore unnamed fields unless they're anonymous structs/unions.
1182 if (name.empty() && !type->isRecordType())
1183 return;
1184
David Majnemerb4b671e2016-06-30 03:01:59 +00001185 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001186 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001187 FieldType = createBitFieldType(field, RecordTy, RD);
1188 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001189 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001190 FieldType =
1191 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001192 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001193 }
1194
David Majnemerb4b671e2016-06-30 03:01:59 +00001195 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001196}
1197
Reid Klecknere2e82062017-08-08 20:30:14 +00001198void CGDebugInfo::CollectRecordNestedType(
1199 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1200 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001201 // Injected class names are not considered nested records.
1202 if (isa<InjectedClassNameType>(Ty))
1203 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001204 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001205 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1206 elements.push_back(nestedType);
1207}
1208
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001209void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001210 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001211 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001213 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001214
Eric Christopher91a31902013-01-16 01:22:32 +00001215 if (CXXDecl && CXXDecl->isLambda())
1216 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1217 else {
1218 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001219
Reid Klecknere2e82062017-08-08 20:30:14 +00001220 // Debug info for nested types is included in the member list only for
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001221 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001222 bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView;
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001223
Eric Christopher91a31902013-01-16 01:22:32 +00001224 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001225 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001226
Eric Christopher91a31902013-01-16 01:22:32 +00001227 // Static and non-static members should appear in the same order as
1228 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001229 for (const auto *I : record->decls())
1230 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001231 if (V->hasAttr<NoDebugAttr>())
1232 continue;
David Blaikiece763042013-08-20 21:49:21 +00001233 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001234 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001235 if (MI != StaticDataMemberCache.end()) {
1236 assert(MI->second &&
1237 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001238 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001239 } else {
1240 auto Field = CreateRecordStaticField(V, RecordTy, record);
1241 elements.push_back(Field);
1242 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001243 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001244 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1245 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001246
1247 // Bump field number for next field.
1248 ++fieldNo;
Reid Klecknere2e82062017-08-08 20:30:14 +00001249 } else if (IncludeNestedTypes) {
1250 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1251 if (!nestedType->isImplicit() &&
1252 nestedType->getDeclContext() == record)
1253 CollectRecordNestedType(nestedType, elements);
1254 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001255 }
1256}
1257
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001258llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001259CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001261 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001262 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001263 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001264 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001265 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1266 Func, Unit);
1267}
David Blaikie2aaf0652013-01-07 22:24:59 +00001268
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1270 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001271 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001272 llvm::DITypeRefArray Args(
1273 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001274 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001275 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001276
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001277 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001278
1279 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001280 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001281
David Blaikie2aaf0652013-01-07 22:24:59 +00001282 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001283 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001284 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1285 // Create pointer type directly in this case.
1286 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1287 QualType PointeeTy = ThisPtrTy->getPointeeType();
1288 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001289 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001290 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001291 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1292 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001293 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001294 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001295 // TODO: This and the artificial type below are misleading, the
1296 // types aren't artificial the argument is, but the current
1297 // metadata doesn't represent that.
1298 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1299 Elts.push_back(ThisPtrType);
1300 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001301 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001302 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001303 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1304 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001305 }
1306
1307 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001308 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1309 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001310
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001311 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001312
Leny Kholodov80c047d2016-09-06 10:48:04 +00001313 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001314 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001315 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001316 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001317 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001318
Reid Klecknerf00f8032016-06-08 20:41:54 +00001319 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1320 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001321}
1322
Eric Christopherb2a008c2013-05-16 00:45:12 +00001323/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001324/// inside a function.
1325static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001326 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001327 return isFunctionLocalClass(NRD);
1328 if (isa<FunctionDecl>(RD->getDeclContext()))
1329 return true;
1330 return false;
1331}
1332
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001333llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1334 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001335 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001336 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001337
Guy Benyei11169dd2012-12-18 14:30:41 +00001338 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001339 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
1341 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1342 // make sense to give a single ctor/dtor a linkage name.
1343 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001344 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1345 // property to use here. It may've been intended to model "is non-external
1346 // type" but misses cases of non-function-local but non-external classes such
1347 // as those in anonymous namespaces as well as the reverse - external types
1348 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001349 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1350 MethodLinkageName = CGM.getMangledName(Method);
1351
1352 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001353 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001354 unsigned MethodLine = 0;
1355 if (!Method->isImplicit()) {
1356 MethodDefUnit = getOrCreateFile(Method->getLocation());
1357 MethodLine = getLineNumber(Method->getLocation());
1358 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001359
1360 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001362 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001364 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001365 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001366
Guy Benyei11169dd2012-12-18 14:30:41 +00001367 if (Method->isVirtual()) {
1368 if (Method->isPure())
1369 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1370 else
1371 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001372
Reid Kleckner216d0a12016-06-16 20:08:51 +00001373 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1374 // It doesn't make sense to give a virtual destructor a vtable index,
1375 // since a single destructor has two entries in the vtable.
1376 if (!isa<CXXDestructorDecl>(Method))
1377 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1378 } else {
1379 // Emit MS ABI vftable information. There is only one entry for the
1380 // deleting dtor.
1381 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1382 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
1383 MicrosoftVTableContext::MethodVFTableLocation ML =
1384 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1385 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001386
1387 // CodeView only records the vftable offset in the class that introduces
1388 // the virtual method. This is possible because, unlike Itanium, the MS
1389 // C++ ABI does not include all virtual methods from non-primary bases in
1390 // the vtable for the most derived class. For example, if C inherits from
1391 // A and B, C's primary vftable will not include B's virtual methods.
1392 if (Method->begin_overridden_methods() == Method->end_overridden_methods())
1393 Flags |= llvm::DINode::FlagIntroducedVirtual;
1394
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001395 // The 'this' adjustment accounts for both the virtual and non-virtual
1396 // portions of the adjustment. Presumably the debugger only uses it when
1397 // it knows the dynamic type of an object.
1398 ThisAdjustment = CGM.getCXXABI()
1399 .getVirtualFunctionPrologueThisAdjustment(GD)
1400 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001401 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001402 ContainingType = RecordTy;
1403 }
1404
Guy Benyei11169dd2012-12-18 14:30:41 +00001405 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001406 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001407 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001408 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001409 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001410 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001411 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001412 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001413 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001414 }
1415 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001416 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001417 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001418 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001419 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001420 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001422 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1423 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001424 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001425 MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality,
1426 VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize,
1427 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001428
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001429 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001430
1431 return SP;
1432}
1433
Eric Christophere7b87e52014-10-26 23:40:33 +00001434void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001435 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1436 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001437
1438 // Since we want more than just the individual member decls if we
1439 // have templated functions iterate over every declaration to gather
1440 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001441 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001442 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1443 // If the member is implicit, don't add it to the member list. This avoids
1444 // the member being added to type units by LLVM, while still allowing it
1445 // to be emitted into the type declaration/reference inside the compile
1446 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001447 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001448 // FIXME: Handle Using(Shadow?)Decls here to create
1449 // DW_TAG_imported_declarations inside the class for base decls brought into
1450 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1451 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1452 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001453 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001454 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001455
1456 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1457 continue;
1458
David Blaikiefd580722014-10-06 05:18:55 +00001459 // Reuse the existing member function declaration if it exists.
1460 // It may be associated with the declaration of the type & should be
1461 // reused as we're building the definition.
1462 //
1463 // This situation can arise in the vtable-based debug info reduction where
1464 // implicit members are emitted in a non-vtable TU.
1465 auto MI = SPCache.find(Method->getCanonicalDecl());
1466 EltTys.push_back(MI == SPCache.end()
1467 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001468 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001469 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001470}
Guy Benyei11169dd2012-12-18 14:30:41 +00001471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001473 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001474 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001475 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1476 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1477 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001478
Bob Haarmandff36732016-10-25 22:19:32 +00001479 // If we are generating CodeView debug info, we also need to emit records for
1480 // indirect virtual base classes.
1481 if (CGM.getCodeGenOpts().EmitCodeView) {
1482 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1483 llvm::DINode::FlagIndirectVirtualBase);
1484 }
1485}
1486
1487void CGDebugInfo::CollectCXXBasesAux(
1488 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1489 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1490 const CXXRecordDecl::base_class_const_range &Bases,
1491 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1492 llvm::DINode::DIFlags StartingFlags) {
1493 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1494 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001495 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001496 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001497 if (!SeenTypes.insert(Base).second)
1498 continue;
1499 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1500 llvm::DINode::DIFlags BFlags = StartingFlags;
1501 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001502
Aaron Ballman574705e2014-03-13 15:41:46 +00001503 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001504 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1505 // virtual base offset offset is -ve. The code generator emits dwarf
1506 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001507 BaseOffset = 0 - CGM.getItaniumVTableContext()
1508 .getVirtualBaseOffsetOffset(RD, Base)
1509 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001510 } else {
1511 // In the MS ABI, store the vbtable offset, which is analogous to the
1512 // vbase offset offset in Itanium.
1513 BaseOffset =
1514 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1515 }
Bob Haarmandff36732016-10-25 22:19:32 +00001516 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001517 } else
1518 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1519 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1520 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001521
Adrian Prantl21361fb2014-08-29 22:44:27 +00001522 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Bob Haarmandff36732016-10-25 22:19:32 +00001523 llvm::DIType *DTy =
1524 DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001525 EltTys.push_back(DTy);
1526 }
1527}
1528
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001529llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001530CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1531 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001532 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001533 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1535 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001536 StringRef Name;
1537 if (TPList)
1538 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001539 switch (TA.getKind()) {
1540 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001541 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001542 TemplateParams.push_back(
1543 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001544 } break;
1545 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001546 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001547 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1548 TheCU, Name, TTy,
1549 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001550 } break;
1551 case TemplateArgument::Declaration: {
1552 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001553 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001555 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001556 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001557 // Variable pointer template parameters have a value that is the address
1558 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001559 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001560 V = CGM.GetAddrOfGlobalVar(VD);
1561 // Member function pointers have special support for building them, though
1562 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001563 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001564 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001565 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001566 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001567 // Member data pointers have special handling too to compute the fixed
1568 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001569 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001570 // These five lines (& possibly the above member function pointer
1571 // handling) might be able to be refactored to use similar code in
1572 // CodeGenModule::getMemberPointerConstant
1573 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1574 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001575 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001576 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001577 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001578 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1579 TheCU, Name, TTy,
1580 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001581 } break;
1582 case TemplateArgument::NullPtr: {
1583 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001584 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001585 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001586 // Special case member data pointer null values since they're actually -1
1587 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001588 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001589 // But treat member function pointers as simple zero integers because
1590 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1591 // CodeGen grows handling for values of non-null member function
1592 // pointers then perhaps we could remove this special case and rely on
1593 // EmitNullMemberPointer for member function pointers.
1594 if (MPT->isMemberDataPointer())
1595 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1596 if (!V)
1597 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001598 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001599 TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001600 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001601 case TemplateArgument::Template:
1602 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001603 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001604 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1605 break;
1606 case TemplateArgument::Pack:
1607 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1608 TheCU, Name, nullptr,
1609 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1610 break;
David Majnemer5559d472013-08-24 08:21:10 +00001611 case TemplateArgument::Expression: {
1612 const Expr *E = TA.getAsExpr();
1613 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001614 if (E->isGLValue())
1615 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001616 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001617 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001618 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001619 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001620 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001621 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001622 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001623 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001624 case TemplateArgument::Null:
1625 llvm_unreachable(
1626 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001627 }
1628 }
1629 return DBuilder.getOrCreateArray(TemplateParams);
1630}
1631
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001632llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001633CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001634 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 if (FD->getTemplatedKind() ==
1636 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001637 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1638 ->getTemplate()
1639 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001640 return CollectTemplateParams(
1641 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001643 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001644}
1645
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001646llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1647 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001648 // Always get the full list of parameters, not just the ones from
1649 // the specialization.
1650 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001651 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001652 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001653 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001654}
1655
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001656llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001657 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 return VTablePtrType;
1659
1660 ASTContext &Context = CGM.getContext();
1661
1662 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001663 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001664 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001665 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001666 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001667 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1668 Optional<unsigned> DWARFAddressSpace =
1669 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1670
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001671 llvm::DIType *vtbl_ptr_type =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001672 DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace,
1673 "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1675 return VTablePtrType;
1676}
1677
Guy Benyei11169dd2012-12-18 14:30:41 +00001678StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001679 // Copy the gdb compatible name on the side and use its reference.
1680 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001681}
1682
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001683void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001684 SmallVectorImpl<llvm::Metadata *> &EltTys,
1685 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 // If this class is not dynamic then there is not any vtable info to collect.
1687 if (!RD->isDynamicClass())
1688 return;
1689
Reid Kleckner59812422016-08-31 20:35:01 +00001690 // Don't emit any vtable shape or vptr info if this class doesn't have an
1691 // extendable vfptr. This can happen if the class doesn't have virtual
1692 // methods, or in the MS ABI if those virtual methods only come from virtually
1693 // inherited bases.
1694 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1695 if (!RL.hasExtendableVFPtr())
1696 return;
1697
Reid Klecknerdc124992016-08-31 16:11:43 +00001698 // CodeView needs to know how large the vtable of every dynamic class is, so
1699 // emit a special named pointer type into the element list. The vptr type
1700 // points to this type as well.
1701 llvm::DIType *VPtrTy = nullptr;
1702 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1703 CGM.getTarget().getCXXABI().isMicrosoft();
1704 if (NeedVTableShape) {
1705 uint64_t PtrWidth =
1706 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1707 const VTableLayout &VFTLayout =
1708 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1709 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001710 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001711 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001712 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1713 Optional<unsigned> DWARFAddressSpace =
1714 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001715
1716 // Create a very wide void* type and insert it directly in the element list.
1717 llvm::DIType *VTableType =
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001718 DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace,
1719 "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001720 EltTys.push_back(VTableType);
1721
1722 // The vptr is a pointer to this special vtable type.
1723 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1724 }
1725
1726 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001727 if (RL.getPrimaryBase())
1728 return;
1729
1730 if (!VPtrTy)
1731 VPtrTy = getOrCreateVTablePtrType(Unit);
1732
Guy Benyei11169dd2012-12-18 14:30:41 +00001733 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001734 llvm::DIType *VPtrMember = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001735 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Reid Klecknerdc124992016-08-31 16:11:43 +00001736 llvm::DINode::FlagArtificial, VPtrTy);
1737 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001738}
1739
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001740llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001741 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001742 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001743 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001744 return T;
1745}
1746
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001747llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001748 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001749 return getOrCreateStandaloneType(D, Loc);
1750}
1751
1752llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1753 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001754 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001755 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001756 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001757 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001758
Adrian Prantl73409ce2013-03-11 18:33:46 +00001759 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 return T;
1761}
1762
David Blaikie483a9da2014-05-06 18:35:21 +00001763void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001764 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001765 return;
1766 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001767 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001768 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001769 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001770 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001771 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001772 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001773 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001774}
1775
David Blaikieb2e86eb2013-08-15 20:49:17 +00001776void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001777 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001778 !CGM.getLangOpts().CPlusPlus)
1779 completeRequiredType(RD);
1780}
1781
David Blaikieb11c8732017-01-30 06:36:08 +00001782/// Return true if the class or any of its methods are marked dllimport.
1783static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1784 if (RD->hasAttr<DLLImportAttr>())
1785 return true;
1786 for (const CXXMethodDecl *MD : RD->methods())
1787 if (MD->hasAttr<DLLImportAttr>())
1788 return true;
1789 return false;
1790}
1791
Adrian Prantla43acdc2017-07-24 23:48:51 +00001792/// Does a type definition exist in an imported clang module?
1793static bool isDefinedInClangModule(const RecordDecl *RD) {
1794 // Only definitions that where imported from an AST file come from a module.
1795 if (!RD || !RD->isFromASTFile())
1796 return false;
1797 // Anonymous entities cannot be addressed. Treat them as not from module.
1798 if (!RD->isExternallyVisible() && RD->getName().empty())
1799 return false;
1800 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1801 if (!CXXDecl->isCompleteDefinition())
1802 return false;
1803 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1804 if (TemplateKind != TSK_Undeclared) {
1805 // This is a template, check the origin of the first member.
1806 if (CXXDecl->field_begin() == CXXDecl->field_end())
1807 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1808 if (!CXXDecl->field_begin()->isFromASTFile())
1809 return false;
1810 }
1811 }
1812 return true;
1813}
1814
David Blaikie6943dea2013-08-20 01:28:15 +00001815void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001816 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1817 if (CXXRD->isDynamicClass() &&
1818 CGM.getVTableLinkage(CXXRD) ==
1819 llvm::GlobalValue::AvailableExternallyLinkage &&
1820 !isClassOrMethodDLLImport(CXXRD))
1821 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001822
1823 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
1824 return;
1825
David Blaikieb11c8732017-01-30 06:36:08 +00001826 completeClass(RD);
1827}
1828
1829void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001830 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001831 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001832 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001833 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001834 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001835 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001836 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001837 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001838 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001839 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001840}
1841
David Blaikie0e716b42014-03-03 23:48:23 +00001842static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1843 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001844 for (CXXMethodDecl *MD : llvm::make_range(I, End))
1845 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001846 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00001847 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001848 return true;
1849 return false;
1850}
1851
Benjamin Kramer8c305922016-02-02 11:06:51 +00001852static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
1853 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00001854 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00001855 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00001856 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001857
David Blaikie1ac9c982017-04-11 21:13:37 +00001858 if (auto *ES = RD->getASTContext().getExternalSource())
1859 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
1860 return true;
1861
Benjamin Kramer8c305922016-02-02 11:06:51 +00001862 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00001863 return false;
1864
1865 if (!LangOpts.CPlusPlus)
1866 return false;
1867
1868 if (!RD->isCompleteDefinitionRequired())
1869 return true;
1870
David Majnemer58ed0f32016-07-17 00:39:12 +00001871 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00001872
1873 if (!CXXDecl)
1874 return false;
1875
Adrian McCarthy99242982016-08-16 22:11:18 +00001876 // Only emit complete debug info for a dynamic class when its vtable is
1877 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00001878 // across DLL boundaries, so skip this optimization if the class or any of its
1879 // methods are marked dllimport. This isn't a complete solution, since objects
1880 // without any dllimport methods can be used in one DLL and constructed in
1881 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00001882 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00001883 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00001884 return true;
1885
1886 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00001887 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00001888 Spec = SD->getSpecializationKind();
1889
1890 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1891 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1892 CXXDecl->method_end()))
1893 return true;
1894
1895 return false;
1896}
1897
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00001898void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
1899 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
1900 return;
1901
1902 QualType Ty = CGM.getContext().getRecordType(RD);
1903 llvm::DIType *T = getTypeOrNull(Ty);
1904 if (T && T->isForwardDecl())
1905 completeClassData(RD);
1906}
1907
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001908llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001910 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001911 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1912 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001913 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001914 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001915 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001916 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001917
David Blaikieb2e86eb2013-08-15 20:49:17 +00001918 return CreateTypeDefinition(Ty);
1919}
1920
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001921llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001922 RecordDecl *RD = Ty->getDecl();
1923
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001925 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001926
1927 // Records and classes and unions can all be recursive. To handle them, we
1928 // first generate a debug descriptor for the struct as a forward declaration.
1929 // Then (if it is a definition) we go through and get debug info for all of
1930 // its members. Finally, we create a descriptor for the complete type (which
1931 // may refer to the forward decl if the struct is recursive) and replace all
1932 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001933 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001934
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001935 const RecordDecl *D = RD->getDefinition();
1936 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001937 return FwdDecl;
1938
David Majnemer58ed0f32016-07-17 00:39:12 +00001939 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00001940 CollectContainingType(CXXDecl, FwdDecl);
1941
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001943 LexicalBlockStack.emplace_back(&*FwdDecl);
1944 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001945
Guy Benyei11169dd2012-12-18 14:30:41 +00001946 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001947 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001948 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001949
1950 // Note: The split of CXXDecl information here is intentional, the
1951 // gdb tests will depend on a certain ordering at printout. The debug
1952 // information offsets are still correct if we merge them all together
1953 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00001954 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001955 if (CXXDecl) {
1956 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00001957 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001958 }
1959
Eric Christopher91a31902013-01-16 01:22:32 +00001960 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001962 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001964
1965 LexicalBlockStack.pop_back();
1966 RegionMap.erase(Ty->getDecl());
1967
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001969 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001970
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001971 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001972 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001974
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001975 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001976 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001977}
1978
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001979llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1980 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001981 // Ignore protocols.
1982 return getOrCreateType(Ty->getBaseType(), Unit);
1983}
1984
Manman Rene6be26c2016-09-13 17:25:08 +00001985llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
1986 llvm::DIFile *Unit) {
1987 // Ignore protocols.
1988 SourceLocation Loc = Ty->getDecl()->getLocation();
1989
1990 // Use Typedefs to represent ObjCTypeParamType.
1991 return DBuilder.createTypedef(
1992 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1993 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
1994 getDeclContextDescriptor(Ty->getDecl()));
1995}
1996
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001997/// \return true if Getter has the default name for the property PD.
1998static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1999 const ObjCMethodDecl *Getter) {
2000 assert(PD);
2001 if (!Getter)
2002 return true;
2003
2004 assert(Getter->getDeclName().isObjCZeroArgSelector());
2005 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002006 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002007}
2008
2009/// \return true if Setter has the default name for the property PD.
2010static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2011 const ObjCMethodDecl *Setter) {
2012 assert(PD);
2013 if (!Setter)
2014 return true;
2015
2016 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002017 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002018 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002019}
2020
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002021llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2022 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 ObjCInterfaceDecl *ID = Ty->getDecl();
2024 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002025 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002026
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002027 // Return a forward declaration if this type was imported from a clang module,
2028 // and this is not the compile unit with the implementation of the type (which
2029 // may contain hidden ivars).
2030 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2031 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002032 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2033 ID->getName(),
2034 getDeclContextDescriptor(ID), Unit, 0);
2035
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002037 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002039 auto RuntimeLang =
2040 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002041
2042 // If this is just a forward declaration return a special forward-declaration
2043 // debug type since we won't be able to lay out the entire type.
2044 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002045 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002046 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002047 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002048 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2049 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002050 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002051 return FwdDecl;
2052 }
2053
David Blaikieef8a9512014-05-05 23:23:53 +00002054 return CreateTypeDefinition(Ty, Unit);
2055}
2056
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002057llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002058CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2059 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002060 // Use the Module pointer as the key into the cache. This is a
2061 // nullptr if the "Module" is a PCH, which is safe because we don't
2062 // support chained PCH debug info, so there can only be a single PCH.
2063 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002064 auto ModRef = ModuleCache.find(M);
2065 if (ModRef != ModuleCache.end())
2066 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002067
2068 // Macro definitions that were defined with "-D" on the command line.
2069 SmallString<128> ConfigMacros;
2070 {
2071 llvm::raw_svector_ostream OS(ConfigMacros);
2072 const auto &PPOpts = CGM.getPreprocessorOpts();
2073 unsigned I = 0;
2074 // Translate the macro definitions back into a commmand line.
2075 for (auto &M : PPOpts.Macros) {
2076 if (++I > 1)
2077 OS << " ";
2078 const std::string &Macro = M.first;
2079 bool Undef = M.second;
2080 OS << "\"-" << (Undef ? 'U' : 'D');
2081 for (char c : Macro)
2082 switch (c) {
2083 case '\\' : OS << "\\\\"; break;
2084 case '"' : OS << "\\\""; break;
2085 default: OS << c;
2086 }
2087 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002088 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002089 }
Adrian Prantl66689202015-09-18 23:01:45 +00002090
Adrian Prantl835e6632015-09-24 16:10:10 +00002091 bool IsRootModule = M ? !M->Parent : true;
2092 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002093 // PCH files don't have a signature field in the control block,
2094 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002095 // We use the lower 64 bits for debug info.
2096 uint64_t Signature =
2097 Mod.getSignature()
2098 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2099 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002100 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002101 DIB.createCompileUnit(TheCU->getSourceLanguage(),
2102 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2103 TheCU->getProducer(), true, StringRef(), 0,
2104 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2105 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002106 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002107 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002108 llvm::DIModule *Parent =
2109 IsRootModule ? nullptr
2110 : getOrCreateModuleRef(
2111 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2112 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002113 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002114 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2115 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002116 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002117 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002118}
2119
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002120llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2121 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002122 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002123 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002124 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002125 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002126
2127 // Bit size, align and offset of the type.
2128 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002129 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002130
Leny Kholodov80c047d2016-09-06 10:48:04 +00002131 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002133 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002134
Adrian Prantlfd696112015-10-01 00:48:51 +00002135 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002136 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002137 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2138 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002139
David Blaikieef8a9512014-05-05 23:23:53 +00002140 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002141 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002142
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002143 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002144 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002145 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002146
2147 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002148 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002149
2150 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2151 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002152 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002153 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002154 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002155 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002156
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002157 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0,
2158 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 EltTys.push_back(InhTag);
2160 }
2161
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002162 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002163 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002165 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002166 unsigned PLine = getLineNumber(Loc);
2167 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2168 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002169 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2170 PD->getName(), PUnit, PLine,
2171 hasDefaultGetterName(PD, Getter) ? ""
2172 : getSelectorName(PD->getGetterName()),
2173 hasDefaultSetterName(PD, Setter) ? ""
2174 : getSelectorName(PD->getSetterName()),
2175 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002177 };
2178 {
2179 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002180 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002181 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002182 PropertySet.insert(PD->getIdentifier());
2183 AddProperty(PD);
2184 }
Manman Renefe1bac2016-01-27 20:00:32 +00002185 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002186 // Don't emit duplicate metadata for properties that were already in a
2187 // class extension.
2188 if (!PropertySet.insert(PD->getIdentifier()).second)
2189 continue;
2190 AddProperty(PD);
2191 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002192 }
2193
2194 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2195 unsigned FieldNo = 0;
2196 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2197 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002198 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002199 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002200 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002201
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 StringRef FieldName = Field->getName();
2203
2204 // Ignore unnamed fields.
2205 if (FieldName.empty())
2206 continue;
2207
2208 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002209 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 unsigned FieldLine = getLineNumber(Field->getLocation());
2211 QualType FType = Field->getType();
2212 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002213 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002214
2215 if (!FType->isIncompleteArrayType()) {
2216
2217 // Bit size, align and offset of the type.
2218 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002219 ? Field->getBitWidthValue(CGM.getContext())
2220 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002221 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 }
2223
2224 uint64_t FieldOffset;
2225 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2226 // We don't know the runtime offset of an ivar if we're using the
2227 // non-fragile ABI. For bitfields, use the bit offset into the first
2228 // byte of storage of the bitfield. For other fields, use zero.
2229 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002230 FieldOffset =
2231 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002232 FieldOffset %= CGM.getContext().getCharWidth();
2233 } else {
2234 FieldOffset = 0;
2235 }
2236 } else {
2237 FieldOffset = RL.getFieldOffset(FieldNo);
2238 }
2239
Leny Kholodov80c047d2016-09-06 10:48:04 +00002240 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002242 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002243 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002244 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002245 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002246 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002247
Craig Topper8a13c412014-05-21 05:09:00 +00002248 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002250 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002251 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002253 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002254 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002255 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002256 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2257 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002258 PropertyNode = DBuilder.createObjCProperty(
2259 PD->getName(), PUnit, PLine,
2260 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
2261 PD->getGetterName()),
2262 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
2263 PD->getSetterName()),
2264 PD->getPropertyAttributes(),
2265 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002266 }
2267 }
2268 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002269 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2270 FieldSize, FieldAlign, FieldOffset, Flags,
2271 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 EltTys.push_back(FieldTy);
2273 }
2274
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002275 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002276 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002277
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002279 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002280}
2281
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002282llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2283 llvm::DIFile *Unit) {
2284 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 int64_t Count = Ty->getNumElements();
2286 if (Count == 0)
2287 // If number of elements are not known then this is an unbounded array.
2288 // Use Count == -1 to express such arrays.
2289 Count = -1;
2290
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002291 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002292 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002293
2294 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002295 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002296
2297 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2298}
2299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002301 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002302 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002303
2304 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002305 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002306 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002307 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2308 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 } else if (Ty->isIncompleteArrayType()) {
2310 Size = 0;
2311 if (Ty->getElementType()->isIncompleteType())
2312 Align = 0;
2313 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002314 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002315 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002316 Size = 0;
2317 Align = 0;
2318 } else {
2319 // Size and align of the whole array, not the element type.
2320 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002321 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002322 }
2323
2324 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2325 // interior arrays, do we care? Why aren't nested arrays represented the
2326 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002327 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002328 QualType EltTy(Ty, 0);
2329 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2330 // If the number of elements is known, then count is that number. Otherwise,
2331 // it's -1. This allows us to represent a subrange with an array of 0
2332 // elements, like this:
2333 //
2334 // struct foo {
2335 // int x[0];
2336 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002337 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002338 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002340 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002341 if (Expr *Size = VAT->getSizeExpr()) {
2342 llvm::APSInt V;
2343 if (Size->EvaluateAsInt(V, CGM.getContext()))
2344 Count = V.getExtValue();
2345 }
David Blaikie87173f12016-08-22 17:49:56 +00002346 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002347
Guy Benyei11169dd2012-12-18 14:30:41 +00002348 // FIXME: Verify this is right for VLAs.
2349 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
2350 EltTy = Ty->getElementType();
2351 }
2352
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002353 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002354
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002355 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2356 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002357}
2358
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002359llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2360 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002361 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2362 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002363}
2364
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002365llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2366 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002367 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2368 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002369}
2370
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002371llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2372 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002373 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002374 uint64_t Size = 0;
2375
2376 if (!Ty->isIncompleteType()) {
2377 Size = CGM.getContext().getTypeSize(Ty);
2378
2379 // Set the MS inheritance model. There is no flag for the unspecified model.
2380 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2381 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2382 case MSInheritanceAttr::Keyword_single_inheritance:
2383 Flags |= llvm::DINode::FlagSingleInheritance;
2384 break;
2385 case MSInheritanceAttr::Keyword_multiple_inheritance:
2386 Flags |= llvm::DINode::FlagMultipleInheritance;
2387 break;
2388 case MSInheritanceAttr::Keyword_virtual_inheritance:
2389 Flags |= llvm::DINode::FlagVirtualInheritance;
2390 break;
2391 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2392 break;
2393 }
2394 }
2395 }
2396
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002397 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002398 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002399 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002400 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2401 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002402
2403 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002404 Ty->getPointeeType()->getAs<FunctionProtoType>();
2405 return DBuilder.createMemberPointerType(
2406 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2407 Ty->getClass(), FPT->getTypeQuals())),
2408 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002409 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002410}
2411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002413 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2414 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002415}
2416
Xiuli Pan9c14e282016-01-09 12:53:17 +00002417llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty,
2418 llvm::DIFile *U) {
2419 return getOrCreateType(Ty->getElementType(), U);
2420}
2421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002422llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002423 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002424
Guy Benyei11169dd2012-12-18 14:30:41 +00002425 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002426 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002427 if (!ED->getTypeForDecl()->isIncompleteType()) {
2428 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002429 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 }
2431
Manman Rene0064d82013-08-29 23:19:58 +00002432 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2433
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002434 bool isImportedFromModule =
2435 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2436
Guy Benyei11169dd2012-12-18 14:30:41 +00002437 // If this is just a forward declaration, construct an appropriately
2438 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002439 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002440 // Note that it is possible for enums to be created as part of
2441 // their own declcontext. In this case a FwdDecl will be created
2442 // twice. This doesn't cause a problem because both FwdDecls are
2443 // entered into the ReplaceMap: finalize() will replace the first
2444 // FwdDecl with the second and then replace the second with
2445 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002446 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002447 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002448 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2449 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002450
Guy Benyei11169dd2012-12-18 14:30:41 +00002451 unsigned Line = getLineNumber(ED->getLocation());
2452 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002453 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002454 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
2455 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Adrian Prantla40030f2016-02-06 01:59:09 +00002456
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002457 ReplaceMap.emplace_back(
2458 std::piecewise_construct, std::make_tuple(Ty),
2459 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002460 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002461 }
2462
David Blaikie483a9da2014-05-06 18:35:21 +00002463 return CreateTypeDefinition(Ty);
2464}
2465
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002466llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002467 const EnumDecl *ED = Ty->getDecl();
2468 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002469 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002470 if (!ED->getTypeForDecl()->isIncompleteType()) {
2471 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002472 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002473 }
2474
2475 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2476
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002477 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002478 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002480 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002481 Enumerators.push_back(DBuilder.createEnumerator(
2482 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002483 }
2484
2485 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002486 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002487
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002488 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002490 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002491 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002492 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2493 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2494 Line, Size, Align, EltArray, ClassTy,
2495 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002496}
2497
Amjad Aboud546bc112017-02-09 22:07:24 +00002498llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2499 unsigned MType, SourceLocation LineLoc,
2500 StringRef Name, StringRef Value) {
2501 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2502 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2503}
2504
2505llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2506 SourceLocation LineLoc,
2507 SourceLocation FileLoc) {
2508 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2509 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2510 return DBuilder.createTempMacroFile(Parent, Line, FName);
2511}
2512
David Blaikie05491062013-01-21 04:37:12 +00002513static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2514 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002516 Qualifiers InnerQuals = T.getLocalQualifiers();
2517 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2518 // that is already there.
2519 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2520 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 QualType LastT = T;
2522 switch (T->getTypeClass()) {
2523 default:
David Blaikie05491062013-01-21 04:37:12 +00002524 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002525 case Type::TemplateSpecialization: {
2526 const auto *Spec = cast<TemplateSpecializationType>(T);
2527 if (Spec->isTypeAlias())
2528 return C.getQualifiedType(T.getTypePtr(), Quals);
2529 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002530 break;
2531 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002532 case Type::TypeOfExpr:
2533 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2534 break;
2535 case Type::TypeOf:
2536 T = cast<TypeOfType>(T)->getUnderlyingType();
2537 break;
2538 case Type::Decltype:
2539 T = cast<DecltypeType>(T)->getUnderlyingType();
2540 break;
2541 case Type::UnaryTransform:
2542 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2543 break;
2544 case Type::Attributed:
2545 T = cast<AttributedType>(T)->getEquivalentType();
2546 break;
2547 case Type::Elaborated:
2548 T = cast<ElaboratedType>(T)->getNamedType();
2549 break;
2550 case Type::Paren:
2551 T = cast<ParenType>(T)->getInnerType();
2552 break;
David Blaikie05491062013-01-21 04:37:12 +00002553 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002556 case Type::Auto:
2557 case Type::DeducedTemplateSpecialization: {
2558 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002559 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002560 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002561 break;
2562 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002563 case Type::Adjusted:
2564 case Type::Decayed:
2565 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2566 T = cast<AdjustedType>(T)->getAdjustedType();
2567 break;
2568 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002569
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002571 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 } while (true);
2573}
2574
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002575llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002576
2577 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002578 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002579
David Blaikief427b002014-05-06 03:42:01 +00002580 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 if (it != TypeCache.end()) {
2582 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002583 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002584 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 }
2586
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002587 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002588}
2589
David Blaikie0e716b42014-03-03 23:48:23 +00002590void CGDebugInfo::completeTemplateDefinition(
2591 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002592 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002593 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002594 completeUnusedClass(SD);
2595}
David Blaikie0856f662014-03-04 22:01:08 +00002596
David Blaikie1ac9c982017-04-11 21:13:37 +00002597void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2598 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2599 return;
2600
2601 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002602 // In case this type has no member function definitions being emitted, ensure
2603 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002604 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002605}
2606
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002607llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002609 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002610
2611 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002612 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002613
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002614 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002615 return T;
2616
Adrian Prantlca844182015-09-11 17:23:03 +00002617 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002618 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002619
2620 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002621 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002622
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 return Res;
2624}
2625
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002626llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002627 // A forward declaration inside a module header does not belong to the module.
2628 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2629 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002630 if (DebugTypeExtRefs && D->isFromASTFile()) {
2631 // Record a reference to an imported clang module or precompiled header.
2632 auto *Reader = CGM.getContext().getExternalSource();
2633 auto Idx = D->getOwningModuleID();
2634 auto Info = Reader->getSourceDescriptor(Idx);
2635 if (Info)
2636 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2637 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002638 // We are building a clang module or a precompiled header.
2639 //
2640 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2641 // and it wouldn't be necessary to specify the parent scope
2642 // because the type is already unique by definition (it would look
2643 // like the output of -fno-standalone-debug). On the other hand,
2644 // the parent scope helps a consumer to quickly locate the object
2645 // file where the type's definition is located, so it might be
2646 // best to make this behavior a command line or debugger tuning
2647 // option.
2648 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
Richard Smith54f04402017-05-18 02:29:20 +00002649 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002650 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002651 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2652 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002653 } else {
2654 // This the precompiled header being built.
2655 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002656 }
2657 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002658
Adrian Prantl9402cef2015-09-20 16:51:35 +00002659 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002660}
2661
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002662llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002663 // Handle qualifiers, which recursively handles what they refer to.
2664 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002665 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002666
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 // Work out details of type.
2668 switch (Ty->getTypeClass()) {
2669#define TYPE(Class, Base)
2670#define ABSTRACT_TYPE(Class, Base)
2671#define NON_CANONICAL_TYPE(Class, Base)
2672#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2673#include "clang/AST/TypeNodes.def"
2674 llvm_unreachable("Dependent types cannot show up in debug information");
2675
2676 case Type::ExtVector:
2677 case Type::Vector:
2678 return CreateType(cast<VectorType>(Ty), Unit);
2679 case Type::ObjCObjectPointer:
2680 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2681 case Type::ObjCObject:
2682 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002683 case Type::ObjCTypeParam:
2684 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 case Type::ObjCInterface:
2686 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2687 case Type::Builtin:
2688 return CreateType(cast<BuiltinType>(Ty));
2689 case Type::Complex:
2690 return CreateType(cast<ComplexType>(Ty));
2691 case Type::Pointer:
2692 return CreateType(cast<PointerType>(Ty), Unit);
2693 case Type::BlockPointer:
2694 return CreateType(cast<BlockPointerType>(Ty), Unit);
2695 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002696 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002698 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002700 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 case Type::FunctionProto:
2702 case Type::FunctionNoProto:
2703 return CreateType(cast<FunctionType>(Ty), Unit);
2704 case Type::ConstantArray:
2705 case Type::VariableArray:
2706 case Type::IncompleteArray:
2707 return CreateType(cast<ArrayType>(Ty), Unit);
2708
2709 case Type::LValueReference:
2710 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2711 case Type::RValueReference:
2712 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2713
2714 case Type::MemberPointer:
2715 return CreateType(cast<MemberPointerType>(Ty), Unit);
2716
2717 case Type::Atomic:
2718 return CreateType(cast<AtomicType>(Ty), Unit);
2719
Xiuli Pan9c14e282016-01-09 12:53:17 +00002720 case Type::Pipe:
2721 return CreateType(cast<PipeType>(Ty), Unit);
2722
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002724 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2725
David Blaikie42edade2014-11-11 20:44:45 +00002726 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002727 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002728 case Type::Adjusted:
2729 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002730 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 case Type::Elaborated:
2732 case Type::Paren:
2733 case Type::SubstTemplateTypeParm:
2734 case Type::TypeOfExpr:
2735 case Type::TypeOf:
2736 case Type::Decltype:
2737 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002738 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002739 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002741
David Blaikie42edade2014-11-11 20:44:45 +00002742 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002743}
2744
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002745llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2746 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002747 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002748
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002749 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002750
2751 // We may have cached a forward decl when we could have created
2752 // a non-forward decl. Go ahead and create a non-forward decl
2753 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002754 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002755 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002756
2757 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002758 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002759
2760 // Propagate members from the declaration to the definition
2761 // CreateType(const RecordType*) will overwrite this with the members in the
2762 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002763 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002764
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002766 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 return Res;
2768}
2769
2770// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002771llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002773
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002775 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 unsigned Line = getLineNumber(RD->getLocation());
2777 StringRef RDName = getClassName(RD);
2778
Amjad Abouddc4531e2016-04-30 01:44:38 +00002779 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002780
David Blaikied2785892013-08-18 17:36:19 +00002781 // If we ended up creating the type during the context chain construction,
2782 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002783 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002784 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002785 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002786 return T;
David Blaikied2785892013-08-18 17:36:19 +00002787
Adrian Prantl381e7552014-02-04 21:29:50 +00002788 // If this is just a forward or incomplete declaration, construct an
2789 // appropriately marked node and just return it.
2790 const RecordDecl *D = RD->getDefinition();
2791 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002792 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002793
2794 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002795 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002796
Manman Rene0064d82013-08-29 23:19:58 +00002797 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2798
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002799 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00002800 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
2801 llvm::DINode::FlagZero, FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002802
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002803 // Elements of composite types usually have back to the type, creating
2804 // uniquing cycles. Distinct nodes are more efficient.
2805 switch (RealDecl->getTag()) {
2806 default:
2807 llvm_unreachable("invalid composite type tag");
2808
2809 case llvm::dwarf::DW_TAG_array_type:
2810 case llvm::dwarf::DW_TAG_enumeration_type:
2811 // Array elements and most enumeration elements don't have back references,
2812 // so they don't tend to be involved in uniquing cycles and there is some
2813 // chance of merging them when linking together two modules. Only make
2814 // them distinct if they are ODR-uniqued.
2815 if (FullName.empty())
2816 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00002817 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00002818
2819 case llvm::dwarf::DW_TAG_structure_type:
2820 case llvm::dwarf::DW_TAG_union_type:
2821 case llvm::dwarf::DW_TAG_class_type:
2822 // Immediatley resolve to a distinct node.
2823 RealDecl =
2824 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
2825 break;
2826 }
2827
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002828 RegionMap[Ty->getDecl()].reset(RealDecl);
2829 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002830
David Majnemer58ed0f32016-07-17 00:39:12 +00002831 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002832 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002833 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002834 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002835}
2836
David Blaikieadfbf992013-08-18 16:55:33 +00002837void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002838 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002839 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002840 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002841 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2842 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002843 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002844 while (1) {
2845 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2846 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2847 if (PBT && !BRL.isPrimaryBaseVirtual())
2848 PBase = PBT;
2849 else
2850 break;
2851 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002852 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002853 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2854 getOrCreateFile(RD->getLocation())));
2855 } else if (RD->isDynamicClass())
2856 ContainingType = RealDecl;
2857
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002858 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002859}
2860
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002861llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002862 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002863 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002865 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002866 llvm::DIType *Ty =
2867 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
2868 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002869 *Offset += FieldSize;
2870 return Ty;
2871}
2872
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002873void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002874 StringRef &Name,
2875 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002876 llvm::DIScope *&FDContext,
2877 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00002878 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002879 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00002880 Name = getFunctionName(FD);
2881 // Use mangled name as linkage name for C/C++ functions.
2882 if (FD->hasPrototype()) {
2883 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002884 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002885 }
2886 // No need to replicate the linkage name if it isn't different from the
2887 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00002888 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00002889 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
2890 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00002891 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00002892 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00002893 LinkageName = StringRef();
2894
Benjamin Kramer8c305922016-02-02 11:06:51 +00002895 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002896 if (const NamespaceDecl *NSDecl =
Adrian Prantl6fc88752017-05-16 23:46:10 +00002897 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00002898 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00002899 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002900 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2901 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2902 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2903 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00002904 // Check if it is a noreturn-marked function
2905 if (FD->isNoReturn())
2906 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00002907 // Collect template parameters.
2908 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2909 }
2910}
2911
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002912void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002913 unsigned &LineNo, QualType &T,
2914 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002915 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002916 Unit = getOrCreateFile(VD->getLocation());
2917 LineNo = getLineNumber(VD->getLocation());
2918
2919 setLocation(VD->getLocation());
2920
2921 T = VD->getType();
2922 if (T->isIncompleteArrayType()) {
2923 // CodeGen turns int[] into int[1] so we'll do the same here.
2924 llvm::APInt ConstVal(32, 1);
2925 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2926
2927 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2928 ArrayType::Normal, 0);
2929 }
2930
2931 Name = VD->getName();
2932 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2933 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2934 LinkageName = CGM.getMangledName(VD);
2935 if (LinkageName == Name)
2936 LinkageName = StringRef();
2937
2938 // Since we emit declarations (DW_AT_members) for static members, place the
2939 // definition of those static members in the namespace they were declared in
2940 // in the source code (the lexical decl context).
2941 // FIXME: Generalize this for even non-member global variables where the
2942 // declaration and definition may have different lexical decl contexts, once
2943 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002944 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2945 : VD->getDeclContext();
2946 // When a record type contains an in-line initialization of a static data
2947 // member, and the record type is marked as __declspec(dllexport), an implicit
2948 // definition of the member will be created in the record context. DWARF
2949 // doesn't seem to have a nice way to describe this in a form that consumers
2950 // are likely to understand, so fake the "normal" situation of a definition
2951 // outside the class by putting it in the global scope.
2952 if (DC->isRecord())
2953 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002954
Amjad Abouddc4531e2016-04-30 01:44:38 +00002955 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2956 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002957}
2958
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002959llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
2960 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002961 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002962 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00002963 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002964 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002965 llvm::DIFile *Unit = getOrCreateFile(Loc);
2966 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002967 unsigned Line = getLineNumber(Loc);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002968 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext,
Frederic Rissd253ed62014-11-18 03:40:51 +00002969 TParamsArray, Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002970 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
2971
Frederic Rissd253ed62014-11-18 03:40:51 +00002972 // Build function type.
2973 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002974 if (FD)
2975 for (const ParmVarDecl *Parm : FD->parameters())
2976 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00002977 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
2978 QualType FnType = CGM.getContext().getFunctionType(
2979 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002980 if (Stub) {
2981 return DBuilder.createFunction(
2982 DContext, Name, LinkageName, Unit, Line,
2983 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2984 !FD->isExternallyVisible(),
2985 /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize,
2986 TParamsArray.get(), getFunctionDeclaration(FD));
2987 }
2988
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002989 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002990 DContext, Name, LinkageName, Unit, Line,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00002991 getOrCreateFunctionType(GD.getDecl(), FnType, Unit),
2992 !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002993 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002994 TParamsArray.get(), getFunctionDeclaration(FD));
David Majnemer58ed0f32016-07-17 00:39:12 +00002995 const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002996 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2997 std::make_tuple(CanonDecl),
2998 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002999 return SP;
3000}
3001
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003002llvm::DISubprogram *
3003CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
3004 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3005}
3006
3007llvm::DISubprogram *
3008CGDebugInfo::getFunctionStub(GlobalDecl GD) {
3009 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3010}
3011
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003012llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003013CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3014 QualType T;
3015 StringRef Name, LinkageName;
3016 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003017 llvm::DIFile *Unit = getOrCreateFile(Loc);
3018 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003019 unsigned Line = getLineNumber(Loc);
3020
3021 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003022 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003023 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3024 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003025 !VD->isExternallyVisible(), nullptr, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003026 FwdDeclReplaceMap.emplace_back(
3027 std::piecewise_construct,
3028 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3029 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003030 return GV;
3031}
3032
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003033llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003034 // We only need a declaration (not a definition) of the type - so use whatever
3035 // we would otherwise do to get a type for a pointee. (forward declarations in
3036 // limited debug info, full definitions (if the type definition is available)
3037 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003038 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003039 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003040 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003041 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003042
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003043 if (I != DeclCache.end()) {
3044 auto N = I->second;
3045 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3046 return GVE->getVariable();
3047 return dyn_cast_or_null<llvm::DINode>(N);
3048 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003049
3050 // No definition for now. Emit a forward definition that might be
3051 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003052 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003053 return getFunctionForwardDeclaration(FD);
3054 else if (const auto *VD = dyn_cast<VarDecl>(D))
3055 return getGlobalVariableForwardDeclaration(VD);
3056
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003057 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003058}
3059
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003060llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003061 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003062 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003063
David Majnemer58ed0f32016-07-17 00:39:12 +00003064 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003065 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003066 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003067
3068 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003069 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003070
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003071 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003072 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003073 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003074 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003075 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003076 }
3077 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003079 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003080 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 return SP;
3082 }
3083
Aaron Ballman86c93902014-03-06 23:45:36 +00003084 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003085 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003087 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003088 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 return SP;
3090 }
3091 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003092 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003093}
3094
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003095// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003096// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003097llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003098 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003099 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003100 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003101 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3102 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003103 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003104
David Majnemer58ed0f32016-07-17 00:39:12 +00003105 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003106 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003107
3108 const auto *FTy = FnType->getAs<FunctionType>();
3109 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3110
David Majnemer58ed0f32016-07-17 00:39:12 +00003111 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003112 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003113 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003114
3115 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003116 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003117
3118 // Replace the instancetype keyword with the actual type.
3119 if (ResultTy == CGM.getContext().getObjCInstanceType())
3120 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003121 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003122
Adrian Prantl7bec9032013-05-10 21:08:31 +00003123 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003125 QualType SelfDeclTy;
3126 if (auto *SelfDecl = OMethod->getSelfDecl())
3127 SelfDeclTy = SelfDecl->getType();
3128 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3129 if (FPT->getNumParams() > 1)
3130 SelfDeclTy = FPT->getParamType(0);
3131 if (!SelfDeclTy.isNull())
3132 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003134 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003135 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003136 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003137 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003138 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003139 // Variadic methods need a special marker at the end of the type list.
3140 if (OMethod->isVariadic())
3141 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003142
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003143 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003144 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3145 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003147
Adrian Prantl800faef2014-02-25 23:42:18 +00003148 // Handle variadic function types; they need an additional
3149 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003150 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003151 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003152 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003153 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003154 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3155 for (QualType ParamType : FPT->param_types())
3156 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003157 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003158 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003159 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3160 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003161 }
3162
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003163 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003164}
3165
Eric Christophere7b87e52014-10-26 23:40:33 +00003166void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3167 SourceLocation ScopeLoc, QualType FnType,
3168 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003169
3170 StringRef Name;
3171 StringRef LinkageName;
3172
3173 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3174
3175 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003176 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003177
Leny Kholodov80c047d2016-09-06 10:48:04 +00003178 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003179 llvm::DIFile *Unit = getOrCreateFile(Loc);
3180 llvm::DIScope *FDContext = Unit;
3181 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 if (!HasDecl) {
3183 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003184 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003185 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003186 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003187 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003189 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003190 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003191 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003192 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003193 return;
3194 }
3195 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003196 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3197 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003198 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003199 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003200 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 } else {
3202 // Use llvm function name.
3203 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003204 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003206 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003207 Name = Name.substr(1);
3208
Adrian Prantl42d71b92014-04-10 23:21:53 +00003209 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003210 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003211 // Artificial functions should not silently reuse CurLoc.
3212 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003213 }
3214 unsigned LineNo = getLineNumber(Loc);
3215 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003216
Eric Christopher8018e412014-03-27 18:50:35 +00003217 // FIXME: The function declaration we're constructing here is mostly reusing
3218 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3219 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3220 // all subprograms instead of the actual context since subprogram definitions
3221 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003222 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003223 FDContext, Name, LinkageName, Unit, LineNo,
David Majnemer36a6e002016-07-06 21:07:53 +00003224 getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003225 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003226 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003227 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003228 // We might get here with a VarDecl in the case we're generating
3229 // code for the initialization of globals. Do not record these decls
3230 // as they will overwrite the actual VarDecl Decl in the cache.
3231 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003232 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003233
Adrian Prantlbebb8932014-03-21 21:01:58 +00003234 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003235 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003236
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003238 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003239}
3240
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003241void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3242 QualType FnType) {
3243 StringRef Name;
3244 StringRef LinkageName;
3245
3246 const Decl *D = GD.getDecl();
3247 if (!D)
3248 return;
3249
Leny Kholodov80c047d2016-09-06 10:48:04 +00003250 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003251 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003252 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003253 llvm::DINodeArray TParamsArray;
3254 if (isa<FunctionDecl>(D)) {
3255 // If there is a DISubprogram for this function available then use it.
3256 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3257 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003258 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003259 Name = getObjCMethodName(OMD);
3260 Flags |= llvm::DINode::FlagPrototyped;
3261 } else {
3262 llvm_unreachable("not a function or ObjC method");
3263 }
3264 if (!Name.empty() && Name[0] == '\01')
3265 Name = Name.substr(1);
3266
3267 if (D->isImplicit()) {
3268 Flags |= llvm::DINode::FlagArtificial;
3269 // Artificial functions without a location should not silently reuse CurLoc.
3270 if (Loc.isInvalid())
3271 CurLoc = SourceLocation();
3272 }
3273 unsigned LineNo = getLineNumber(Loc);
3274 unsigned ScopeLine = 0;
3275
Adrian Prantle76bda52016-04-15 15:55:45 +00003276 DBuilder.retainType(DBuilder.createFunction(
3277 FDContext, Name, LinkageName, Unit, LineNo,
3278 getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/,
3279 false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
3280 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003281}
3282
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003283void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3284 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3285 // If there is a subprogram for this function available then use it.
3286 auto FI = SPCache.find(FD->getCanonicalDecl());
3287 llvm::DISubprogram *SP = nullptr;
3288 if (FI != SPCache.end())
3289 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
3290 if (!SP)
3291 SP = getFunctionStub(GD);
3292 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3293 LexicalBlockStack.emplace_back(SP);
3294 setInlinedAt(Builder.getCurrentDebugLocation());
3295 EmitLocation(Builder, FD->getLocation());
3296}
3297
3298void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3299 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003300 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003301 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3302}
3303
David Blaikie835afb22015-01-21 23:08:17 +00003304void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 // Update our current location
3306 setLocation(Loc);
3307
Eric Christophere7b87e52014-10-26 23:40:33 +00003308 if (CurLoc.isInvalid() || CurLoc.isMacroID())
3309 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003310
Adrian Prantle83b1302014-01-07 22:05:52 +00003311 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00003312 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003313 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003314}
3315
Guy Benyei11169dd2012-12-18 14:30:41 +00003316void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003317 llvm::MDNode *Back = nullptr;
3318 if (!LexicalBlockStack.empty())
3319 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003320 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003321 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003322 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003323}
3324
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003325void CGDebugInfo::AppendAddressSpaceXDeref(
3326 unsigned AddressSpace,
3327 SmallVectorImpl<int64_t> &Expr) const {
3328 Optional<unsigned> DWARFAddressSpace =
3329 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3330 if (!DWARFAddressSpace)
3331 return;
3332
3333 Expr.push_back(llvm::dwarf::DW_OP_constu);
3334 Expr.push_back(DWARFAddressSpace.getValue());
3335 Expr.push_back(llvm::dwarf::DW_OP_swap);
3336 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3337}
3338
Eric Christopher0fdcb312013-05-16 00:52:20 +00003339void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3340 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003341 // Set our current location.
3342 setLocation(Loc);
3343
Guy Benyei11169dd2012-12-18 14:30:41 +00003344 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003345 Builder.SetCurrentDebugLocation(
3346 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3347 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003348
Benjamin Kramer8c305922016-02-02 11:06:51 +00003349 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003350 return;
3351
3352 // Create a new lexical block and push it on the stack.
3353 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003354}
3355
Eric Christopher0fdcb312013-05-16 00:52:20 +00003356void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3357 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003358 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3359
3360 // Provide an entry in the line table for the end of the block.
3361 EmitLocation(Builder, Loc);
3362
Benjamin Kramer8c305922016-02-02 11:06:51 +00003363 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003364 return;
3365
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 LexicalBlockStack.pop_back();
3367}
3368
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003369void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3371 unsigned RCount = FnBeginRegionCount.back();
3372 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3373
3374 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003375 while (LexicalBlockStack.size() != RCount) {
3376 // Provide an entry in the line table for the end of the block.
3377 EmitLocation(Builder, CurLoc);
3378 LexicalBlockStack.pop_back();
3379 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003380 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003381
3382 if (Fn && Fn->getSubprogram())
3383 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003384}
3385
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003386llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003387 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003388
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003389 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003390 QualType FType;
3391 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003392 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003394 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003395 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003396
3397 FieldOffset = 0;
3398 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3399 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3400 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3401 FType = CGM.getContext().IntTy;
3402 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3403 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3404
3405 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3406 if (HasCopyAndDispose) {
3407 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003408 EltTys.push_back(
3409 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3410 EltTys.push_back(
3411 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 }
3413 bool HasByrefExtendedLayout;
3414 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003415 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3416 HasByrefExtendedLayout) &&
3417 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003418 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003419 EltTys.push_back(
3420 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003421 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003422
Guy Benyei11169dd2012-12-18 14:30:41 +00003423 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3424 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003425 CGM.getTarget().getPointerAlign(0))) {
3426 CharUnits FieldOffsetInBytes =
3427 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003428 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003429 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003430
Guy Benyei11169dd2012-12-18 14:30:41 +00003431 if (NumPaddingBytes.isPositive()) {
3432 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3433 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3434 pad, ArrayType::Normal, 0);
3435 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3436 }
3437 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003438
Guy Benyei11169dd2012-12-18 14:30:41 +00003439 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003440 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003441 FieldSize = CGM.getContext().getTypeSize(FType);
3442 FieldAlign = CGM.getContext().toBits(Align);
3443
Eric Christopherb2a008c2013-05-16 00:45:12 +00003444 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00003445 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003446 FieldAlign, FieldOffset,
3447 llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003448 EltTys.push_back(FieldTy);
3449 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003450
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003451 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003452
Leny Kholodov80c047d2016-09-06 10:48:04 +00003453 llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003454
Guy Benyei11169dd2012-12-18 14:30:41 +00003455 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003456 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00003457}
3458
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003459void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
3460 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00003461 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003462 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003463 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003464 if (VD->hasAttr<NoDebugAttr>())
3465 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003466
David Blaikie7fceebf2013-08-19 03:37:48 +00003467 bool Unwritten =
3468 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3469 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003470 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003471 if (!Unwritten)
3472 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003473 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003474 uint64_t XOffset = 0;
3475 if (VD->hasAttr<BlocksAttr>())
3476 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003477 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003478 Ty = getOrCreateType(VD->getType(), Unit);
3479
3480 // If there is no debug info for this type then do not emit debug info
3481 // for this variable.
3482 if (!Ty)
3483 return;
3484
Guy Benyei11169dd2012-12-18 14:30:41 +00003485 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003486 unsigned Line = 0;
3487 unsigned Column = 0;
3488 if (!Unwritten) {
3489 Line = getLineNumber(VD->getLocation());
3490 Column = getColumnNumber(VD->getLocation());
3491 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003492 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003493 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003495 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003496
3497 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3498
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003499 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3500 AppendAddressSpaceXDeref(AddressSpace, Expr);
3501
Alexey Bataev24f710182017-06-09 13:55:08 +00003502 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3503 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003504 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3505 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3506 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3507 Flags |= llvm::DINode::FlagObjectPointer;
3508 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003509
Adrian Prantlc3782a12017-04-18 01:22:01 +00003510 // Note: Older versions of clang used to emit byval references with an extra
3511 // DW_OP_deref, because they referenced the IR arg directly instead of
3512 // referencing an alloca. Newer versions of LLVM don't treat allocas
3513 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003514 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003515 StringRef Name = VD->getName();
3516 if (!Name.empty()) {
3517 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003518 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003519 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003520 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003521 // offset of __forwarding field
3522 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003523 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003524 Expr.push_back(offset.getQuantity());
3525 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003526 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 // offset of x field
3528 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003529 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003530 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003531 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003532 // If VD is an anonymous union then Storage represents value for
3533 // all union fields.
David Majnemer58ed0f32016-07-17 00:39:12 +00003534 const auto *RD = cast<RecordDecl>(RT->getDecl());
Adrian Prantl0d820892015-04-29 15:05:50 +00003535 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003536 // GDB has trouble finding local variables in anonymous unions, so we emit
3537 // artifical local variables for each of the members.
3538 //
3539 // FIXME: Remove this code as soon as GDB supports this.
3540 // The debug info verifier in LLVM operates based on the assumption that a
3541 // variable has the same size as its storage and we had to disable the check
3542 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003543 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003544 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003545 StringRef FieldName = Field->getName();
3546
3547 // Ignore unnamed fields. Do not ignore unnamed records.
3548 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3549 continue;
3550
3551 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003552 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003553 auto *D = DBuilder.createAutoVariable(
3554 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003555 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003556
3557 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003558 DBuilder.insertDeclare(
3559 Storage, D, DBuilder.createExpression(Expr),
3560 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3561 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003562 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003563 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003564 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003565
3566 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003567 auto *D = ArgNo
3568 ? DBuilder.createParameterVariable(
3569 Scope, Name, *ArgNo, Unit, Line, Ty,
3570 CGM.getLangOpts().Optimize, Flags)
3571 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3572 CGM.getLangOpts().Optimize, Flags,
3573 Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003574
3575 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003576 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003577 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003578 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003579}
3580
3581void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3582 llvm::Value *Storage,
3583 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003584 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003585 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003586}
3587
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003588llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3589 llvm::DIType *Ty) {
3590 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003591 if (CachedTy)
3592 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003593 return DBuilder.createObjectPointerType(Ty);
3594}
3595
Eric Christophere7b87e52014-10-26 23:40:33 +00003596void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3597 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003598 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003599 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003600 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003601
Craig Topper8a13c412014-05-21 05:09:00 +00003602 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003603 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003604 if (VD->hasAttr<NoDebugAttr>())
3605 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003606
Guy Benyei11169dd2012-12-18 14:30:41 +00003607 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003608
Guy Benyei11169dd2012-12-18 14:30:41 +00003609 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003610 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3611 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003612 if (isByRef)
3613 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003614 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 Ty = getOrCreateType(VD->getType(), Unit);
3616
3617 // Self is passed along as an implicit non-arg variable in a
3618 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003619 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3620 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3621 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003622
3623 // Get location information.
3624 unsigned Line = getLineNumber(VD->getLocation());
3625 unsigned Column = getColumnNumber(VD->getLocation());
3626
3627 const llvm::DataLayout &target = CGM.getDataLayout();
3628
3629 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003630 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003631 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3632
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003633 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003634 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003635 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003636 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003638 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003639 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003640 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003641 offset =
3642 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003643 addr.push_back(offset.getQuantity());
3644 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003645 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 // offset of x field
3647 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003648 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003649 }
3650
3651 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003652 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003653 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003654 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003655 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003656
Guy Benyei11169dd2012-12-18 14:30:41 +00003657 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003658 auto DL =
3659 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003660 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003661 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003662 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003663 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003664 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003665}
3666
Guy Benyei11169dd2012-12-18 14:30:41 +00003667void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3668 unsigned ArgNo,
3669 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003670 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003671 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003672}
3673
3674namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003675struct BlockLayoutChunk {
3676 uint64_t OffsetInBits;
3677 const BlockDecl::Capture *Capture;
3678};
3679bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3680 return l.OffsetInBits < r.OffsetInBits;
3681}
Guy Benyei11169dd2012-12-18 14:30:41 +00003682}
3683
3684void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003685 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003686 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003687 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003689 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003690 ASTContext &C = CGM.getContext();
3691 const BlockDecl *blockDecl = block.getBlockDecl();
3692
3693 // Collect some general information about the block's location.
3694 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003695 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003696 unsigned line = getLineNumber(loc);
3697 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003698
Guy Benyei11169dd2012-12-18 14:30:41 +00003699 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003700 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003701
3702 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003703 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003704
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003705 SmallVector<llvm::Metadata *, 16> fields;
David Majnemerb4b671e2016-06-30 03:01:59 +00003706 fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003707 blockLayout->getElementOffsetInBits(0),
3708 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003709 fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003710 blockLayout->getElementOffsetInBits(1),
3711 tunit, tunit));
David Majnemerb4b671e2016-06-30 03:01:59 +00003712 fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003713 blockLayout->getElementOffsetInBits(2),
3714 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003715 auto *FnTy = block.getBlockExpr()->getFunctionType();
3716 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
David Majnemerb4b671e2016-06-30 03:01:59 +00003717 fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003718 blockLayout->getElementOffsetInBits(3),
3719 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003720 fields.push_back(createFieldType(
3721 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3722 ? C.getBlockDescriptorExtendedType()
3723 : C.getBlockDescriptorType()),
David Majnemerb4b671e2016-06-30 03:01:59 +00003724 loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003725
3726 // We want to sort the captures by offset, not because DWARF
3727 // requires this, but because we're paranoid about debuggers.
3728 SmallVector<BlockLayoutChunk, 8> chunks;
3729
3730 // 'this' capture.
3731 if (blockDecl->capturesCXXThis()) {
3732 BlockLayoutChunk chunk;
3733 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003734 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003735 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003736 chunks.push_back(chunk);
3737 }
3738
3739 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003740 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003741 const VarDecl *variable = capture.getVariable();
3742 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3743
3744 // Ignore constant captures.
3745 if (captureInfo.isConstant())
3746 continue;
3747
3748 BlockLayoutChunk chunk;
3749 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003750 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003751 chunk.Capture = &capture;
3752 chunks.push_back(chunk);
3753 }
3754
3755 // Sort by offset.
3756 llvm::array_pod_sort(chunks.begin(), chunks.end());
3757
David Majnemer58ed0f32016-07-17 00:39:12 +00003758 for (const BlockLayoutChunk &Chunk : chunks) {
3759 uint64_t offsetInBits = Chunk.OffsetInBits;
3760 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00003761
3762 // If we have a null capture, this must be the C++ 'this' capture.
3763 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00003764 QualType type;
3765 if (auto *Method =
3766 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
3767 type = Method->getThisType(C);
3768 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
3769 type = QualType(RDecl->getTypeForDecl(), 0);
3770 else
3771 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00003772
David Majnemerb4b671e2016-06-30 03:01:59 +00003773 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003774 offsetInBits, tunit, tunit));
3775 continue;
3776 }
3777
3778 const VarDecl *variable = capture->getVariable();
3779 StringRef name = variable->getName();
3780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003781 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003782 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003783 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00003784 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00003785
3786 // FIXME: this creates a second copy of this type!
3787 uint64_t xoffset;
3788 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003789 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00003790 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
3791 PtrInfo.Width, Align, offsetInBits,
3792 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003793 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003794 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00003795 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00003796 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003797 }
3798 fields.push_back(fieldType);
3799 }
3800
3801 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003802 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3803 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003804
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003805 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003806
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003807 llvm::DIType *type =
3808 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00003809 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003810 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003811 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3812
3813 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00003814 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003815 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003816
3817 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003818 auto *debugVar = DBuilder.createParameterVariable(
3819 scope, Arg->getName(), ArgNo, tunit, line, type,
3820 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003821
Adrian Prantl616bef42013-03-14 21:52:59 +00003822 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003823 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003824 DBuilder.insertDbgValueIntrinsic(
Adrian Prantl1fa18852017-07-28 20:21:08 +00003825 LocalAddr, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003826 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
3827 Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003828 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003829
Adrian Prantl616bef42013-03-14 21:52:59 +00003830 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003831 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003832 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003833 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003834}
3835
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003836llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003837CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3838 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003839 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003840
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003841 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003842 if (MI != StaticDataMemberCache.end()) {
3843 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003844 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003845 }
David Blaikiece763042013-08-20 21:49:21 +00003846
3847 // If the member wasn't found in the cache, lazily construct and add it to the
3848 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003849 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003850 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003851 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003852}
3853
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003854llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003855 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3856 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003857 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003858
3859 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003860 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003861 StringRef FieldName = Field->getName();
3862
3863 // Ignore unnamed fields, but recurse into anonymous records.
3864 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003865 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003866 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Eric Christophercab9fae2014-04-10 05:20:00 +00003867 Var, DContext);
3868 continue;
3869 }
3870 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003871 GVE = DBuilder.createGlobalVariableExpression(
3872 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
3873 Var->hasLocalLinkage());
3874 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003875 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003876 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00003877}
3878
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003879void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003880 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003881 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003882 if (D->hasAttr<NoDebugAttr>())
3883 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003884
3885 // If we already created a DIGlobalVariable for this declaration, just attach
3886 // it to the llvm::GlobalVariable.
3887 auto Cached = DeclCache.find(D->getCanonicalDecl());
3888 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003889 return Var->addDebugInfo(
3890 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00003891
Guy Benyei11169dd2012-12-18 14:30:41 +00003892 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003893 llvm::DIFile *Unit = nullptr;
3894 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003895 unsigned LineNo;
3896 StringRef DeclName, LinkageName;
3897 QualType T;
3898 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003899
3900 // Attempt to store one global variable for the declaration - even if we
3901 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003902 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003903
3904 // If this is an anonymous union then we'll want to emit a global
3905 // variable for each member of the anonymous union so that it's possible
3906 // to find the name of any field in the union.
3907 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003908 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003909 assert(RD->isAnonymousStructOrUnion() &&
3910 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003911 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003912 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00003913 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003914
3915 SmallVector<int64_t, 4> Expr;
3916 unsigned AddressSpace =
3917 CGM.getContext().getTargetAddressSpace(D->getType());
3918 AppendAddressSpaceXDeref(AddressSpace, Expr);
3919
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003920 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00003921 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003922 Var->hasLocalLinkage(),
3923 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Victor Leschuka7ece032016-10-20 00:13:19 +00003924 getOrCreateStaticDataMemberDeclarationOrNull(D), Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003925 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00003926 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003927 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00003928}
3929
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003930void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003931 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00003932 if (VD->hasAttr<NoDebugAttr>())
3933 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00003934 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003935 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003936 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003937 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003938 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00003939 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3940 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003941 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3942 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3943 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003944 // Do not use global variables for enums.
3945 //
3946 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003947 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003948 return;
David Blaikiea15565562014-04-04 20:56:17 +00003949 // Do not emit separate definitions for function local const/statics.
3950 if (isa<FunctionDecl>(VD->getDeclContext()))
3951 return;
David Blaikiebb113912014-04-05 07:23:17 +00003952 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003953 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003954 if (VarD->isStaticDataMember()) {
3955 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003956 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003957 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00003958 //
3959 // FIXME: This is probably unnecessary, since Ty should reference RD
3960 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00003961 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003962 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003963 return;
3964 }
3965
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003966 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003967
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003968 auto &GV = DeclCache[VD];
3969 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003970 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00003971 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00003972 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
3973 // FIXME: Add a representation for integer constants wider than 64 bits.
3974 if (Init.isInt())
3975 InitExpr =
3976 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
3977 else if (Init.isFloat())
3978 InitExpr = DBuilder.createConstantValueExpression(
3979 Init.getFloat().bitcastToAPInt().getZExtValue());
3980 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003981 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00003982 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00003983 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
3984 Align));
David Blaikiebd483762013-05-20 04:58:53 +00003985}
3986
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003987llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003988 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003989 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003990 llvm::DIScope *Mod = getParentModuleOrNull(D);
3991 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003992}
3993
David Blaikie9f88fe82013-04-22 06:13:21 +00003994void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003995 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00003996 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00003997 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00003998 if (!NSDecl->isAnonymousNamespace() ||
3999 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004000 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004001 DBuilder.createImportedModule(
4002 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004003 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004004 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004005}
4006
David Blaikiebd483762013-05-20 04:58:53 +00004007void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004008 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004009 return;
4010 assert(UD.shadow_size() &&
4011 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4012 // Emitting one decl is sufficient - debuggers can detect that this is an
4013 // overloaded name & provide lookup for all the overloads.
4014 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004015
4016 // FIXME: Skip functions with undeduced auto return type for now since we
4017 // don't currently have the plumbing for separate declarations & definitions
4018 // of free functions and mismatched types (auto in the declaration, concrete
4019 // return type in the definition)
4020 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4021 if (const auto *AT =
4022 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4023 if (AT->getDeducedType().isNull())
4024 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004025 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004026 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4027 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004028 DBuilder.createImportedDeclaration(
4029 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004030 getOrCreateFile(Loc), getLineNumber(Loc));
4031 }
David Blaikiebd483762013-05-20 04:58:53 +00004032}
4033
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004034void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004035 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4036 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004037 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004038 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004039 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004040 DBuilder.createImportedDeclaration(
4041 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004042 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4043 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004044 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004045}
4046
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004047llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004048CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004049 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004050 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004051 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004052 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004053 return cast<llvm::DIImportedEntity>(VH);
4054 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004055 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004056 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004057 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4058 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004059 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004060 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004061 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4062 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004063 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004064 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004065 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004066 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004067 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004068 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004069 return R;
4070}
4071
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004072llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004073CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4074 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4075 // if necessary, and this way multiple declarations of the same namespace in
4076 // different parent modules stay distinct.
4077 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004078 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004079 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004080
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004081 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004082 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004083 llvm::DINamespace *NS =
4084 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4085 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004086 return NS;
4087}
4088
Adrian Prantla5206ce2015-09-22 23:26:43 +00004089void CGDebugInfo::setDwoId(uint64_t Signature) {
4090 assert(TheCU && "no main compile unit");
4091 TheCU->setDWOId(Signature);
4092}
4093
4094
Guy Benyei11169dd2012-12-18 14:30:41 +00004095void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004096 // Creating types might create further types - invalidating the current
4097 // element and the size(), so don't cache/reference them.
4098 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4099 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004100 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004101 ? CreateTypeDefinition(E.Type, E.Unit)
4102 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004103 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004104 }
4105
David Blaikief427b002014-05-06 03:42:01 +00004106 for (auto p : ReplaceMap) {
4107 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004108 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004109 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004110
David Blaikief427b002014-05-06 03:42:01 +00004111 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00004112 assert(it != TypeCache.end());
4113 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004114
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004115 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
4116 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004117 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004118
Frederic Rissd253ed62014-11-18 03:40:51 +00004119 for (const auto &p : FwdDeclReplaceMap) {
4120 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004121 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004122 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004123
4124 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004125 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004126 // with ourselves, that will destroy the temporary MDNode and
4127 // replace it with a standard one, avoiding leaking memory.
4128 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004129 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004130 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004131 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004132
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004133 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4134 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004135 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004136 }
4137
Adrian Prantl73409ce2013-03-11 18:33:46 +00004138 // We keep our own list of retained types, because we need to look
4139 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004140 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004141 if (auto MD = TypeCache[RT])
4142 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004143
Guy Benyei11169dd2012-12-18 14:30:41 +00004144 DBuilder.finalize();
4145}
David Blaikie66088d52014-09-24 17:01:27 +00004146
4147void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004148 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004149 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004150
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004151 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004152 // Don't ignore in case of explicit cast where it is referenced indirectly.
4153 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004154}
Amara Emerson652795d2016-11-10 14:44:30 +00004155
4156llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4157 if (LexicalBlockStack.empty())
4158 return llvm::DebugLoc();
4159
4160 llvm::MDNode *Scope = LexicalBlockStack.back();
4161 return llvm::DebugLoc::get(
4162 getLineNumber(Loc), getColumnNumber(Loc), Scope);
4163}