blob: 3492fbacd33a1fe133ef19138ad0925d54324d6f [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CGBlocks.h"
David Blaikie38079fd2013-05-10 21:53:14 +000016#include "CGCXXABI.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000017#include "CGObjCRuntime.h"
Bob Haarmandff36732016-10-25 22:19:32 +000018#include "CGRecordLayout.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
John McCallde0fe072017-08-15 21:42:52 +000021#include "ConstantEmitter.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/Expr.h"
27#include "clang/AST/RecordLayout.h"
28#include "clang/Basic/FileManager.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/Version.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000031#include "clang/Frontend/CodeGenOptions.h"
Taewook Oh0fb5b782017-08-16 19:36:24 +000032#include "clang/Frontend/FrontendOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000033#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000034#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000035#include "clang/Lex/PreprocessorOptions.h"
Bob Haarmandff36732016-10-25 22:19:32 +000036#include "llvm/ADT/DenseSet.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000037#include "llvm/ADT/SmallVector.h"
38#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000039#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
Matthew Voss20165362018-10-03 18:45:04 +000044#include "llvm/IR/Metadata.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000045#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046#include "llvm/Support/FileSystem.h"
Amjad Aboude2aab8c2016-12-25 10:12:27 +000047#include "llvm/Support/MD5.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000048#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000049using namespace clang;
50using namespace clang::CodeGen;
51
Victor Leschuka7ece032016-10-20 00:13:19 +000052static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
53 auto TI = Ctx.getTypeInfo(Ty);
54 return TI.AlignIsRequired ? TI.Align : 0;
55}
56
57static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
58 return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
59}
60
61static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
62 return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
63}
64
Guy Benyei11169dd2012-12-18 14:30:41 +000065CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000066 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000067 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000068 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000069 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
70 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000071 CreateCompileUnit();
72}
73
74CGDebugInfo::~CGDebugInfo() {
75 assert(LexicalBlockStack.empty() &&
76 "Region stack mismatch, stack not empty!");
77}
78
David Blaikie66e41972015-01-14 07:38:27 +000079ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000080 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000081 : CGF(&CGF) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +000082 init(TemporaryLocation);
David Blaikie9b479662015-01-25 01:19:10 +000083}
84
Adrian Prantl39428e72015-02-03 18:40:42 +000085ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000086 bool DefaultToEmpty,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000087 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000088 : CGF(&CGF) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +000089 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000090}
91
92void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Calixte Denizetfcd661d2018-09-24 18:24:18 +000093 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000094 auto *DI = CGF->getDebugInfo();
95 if (!DI) {
96 CGF = nullptr;
97 return;
David Blaikie66e41972015-01-14 07:38:27 +000098 }
David Blaikied7057d92015-08-12 23:49:57 +000099
100 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
Bob Haarmanc6c9b8f2017-09-11 22:11:57 +0000101
102 if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
103 return;
104
David Blaikied7057d92015-08-12 23:49:57 +0000105 if (TemporaryLocation.isValid()) {
Calixte Denizetfcd661d2018-09-24 18:24:18 +0000106 DI->EmitLocation(CGF->Builder, TemporaryLocation);
David Blaikied7057d92015-08-12 23:49:57 +0000107 return;
108 }
109
110 if (DefaultToEmpty) {
111 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
112 return;
113 }
114
115 // Construct a location that has a valid scope, but no line info.
116 assert(!DI->LexicalBlockStack.empty());
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000117 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
118 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +0000119}
120
David Blaikie9b479662015-01-25 01:19:10 +0000121ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +0000122 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000123 init(E->getExprLoc());
124}
125
David Blaikie66e41972015-01-14 07:38:27 +0000126ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000127 : CGF(&CGF) {
128 if (!CGF.getDebugInfo()) {
129 this->CGF = nullptr;
130 return;
David Blaikie66e41972015-01-14 07:38:27 +0000131 }
David Blaikied7057d92015-08-12 23:49:57 +0000132 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
133 if (Loc)
134 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000135}
136
137ApplyDebugLocation::~ApplyDebugLocation() {
138 // Query CGF so the location isn't overwritten when location updates are
139 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000140 if (CGF)
141 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000142}
143
Adrian Prantlb7acfc02017-02-27 21:30:05 +0000144ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
145 GlobalDecl InlinedFn)
146 : CGF(&CGF) {
147 if (!CGF.getDebugInfo()) {
148 this->CGF = nullptr;
149 return;
150 }
151 auto &DI = *CGF.getDebugInfo();
152 SavedLocation = DI.getLocation();
153 assert((DI.getInlinedAt() ==
154 CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
155 "CGDebugInfo and IRBuilder are out of sync");
156
157 DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
158}
159
160ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
161 if (!CGF)
162 return;
163 auto &DI = *CGF->getDebugInfo();
164 DI.EmitInlineFunctionEnd(CGF->Builder);
165 DI.EmitLocation(CGF->Builder, SavedLocation);
166}
167
Guy Benyei11169dd2012-12-18 14:30:41 +0000168void CGDebugInfo::setLocation(SourceLocation Loc) {
169 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000170 if (Loc.isInvalid())
171 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000172
173 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
174
175 // If we've changed files in the middle of a lexical scope go ahead
176 // and create a new lexical scope with file node if it's different
177 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000178 if (LexicalBlockStack.empty())
179 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000180
181 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000182 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Ilya Biryukov660d2332018-12-04 16:30:45 +0000184
185 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 return;
187
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000188 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000189 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000190 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
191 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000192 } else if (isa<llvm::DILexicalBlock>(Scope) ||
193 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000194 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000195 LexicalBlockStack.emplace_back(
196 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000197 }
198}
199
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000200llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000201 llvm::DIScope *Mod = getParentModuleOrNull(D);
202 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
203 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000204}
205
206llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
207 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000208 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000209 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000210
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000211 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000212 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000213 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000214 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000215 }
216
217 // Check namespace.
Adrian Prantlddb8e062017-05-12 16:23:53 +0000218 if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
219 return getOrCreateNamespace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000220
David Majnemer58ed0f32016-07-17 00:39:12 +0000221 if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000222 if (!RDecl->isDependentType())
223 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000225 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000226}
227
Reid Kleckner59d12202017-08-08 01:33:53 +0000228PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
229 PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
230
231 // If we're emitting codeview, it's important to try to match MSVC's naming so
232 // that visualizers written for MSVC will trigger for our class names. In
233 // particular, we can't have spaces between arguments of standard templates
234 // like basic_string and vector.
235 if (CGM.getCodeGenOpts().EmitCodeView)
236 PP.MSVCFormatting = true;
237
Adrian Prantl56acd5a2018-12-05 18:37:44 +0000238 // Apply -fdebug-prefix-map.
239 PP.RemapFilePaths = true;
240 PP.remapPath = [this](StringRef Path) { return remapDIPath(Path); };
Reid Kleckner59d12202017-08-08 01:33:53 +0000241 return PP;
242}
243
Guy Benyei11169dd2012-12-18 14:30:41 +0000244StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000245 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000246 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000247 FunctionTemplateSpecializationInfo *Info =
248 FD->getTemplateSpecializationInfo();
Reid Kleckner60103382015-12-16 02:04:40 +0000249
Reid Kleckner31abd802016-06-30 17:41:31 +0000250 // Emit the unqualified name in normal operation. LLVM and the debugger can
251 // compute the fully qualified name from the scope chain. If we're only
252 // emitting line table info, there won't be any scope chains, so emit the
253 // fully qualified name here so that stack traces are more accurate.
254 // FIXME: Do this when emitting DWARF as well as when emitting CodeView after
255 // evaluating the size impact.
256 bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly &&
257 CGM.getCodeGenOpts().EmitCodeView;
258
259 if (!Info && FII && !UseQualifiedName)
Guy Benyei11169dd2012-12-18 14:30:41 +0000260 return FII->getName();
261
Benjamin Kramer9170e912013-02-22 15:46:01 +0000262 SmallString<128> NS;
263 llvm::raw_svector_ostream OS(NS);
Reid Kleckner31abd802016-06-30 17:41:31 +0000264 if (!UseQualifiedName)
265 FD->printName(OS);
266 else
Reid Kleckner59d12202017-08-08 01:33:53 +0000267 FD->printQualifiedName(OS, getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000268
Reid Kleckner829398e2016-06-17 16:11:20 +0000269 // Add any template specialization args.
270 if (Info) {
271 const TemplateArgumentList *TArgs = Info->TemplateArguments;
Serge Pavlov03e672c2017-11-28 16:14:14 +0000272 printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy());
Guy Benyei11169dd2012-12-18 14:30:41 +0000273 }
274
275 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000276 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000277}
278
279StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
280 SmallString<256> MethodName;
281 llvm::raw_svector_ostream OS(MethodName);
282 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
283 const DeclContext *DC = OMD->getDeclContext();
David Majnemer58ed0f32016-07-17 00:39:12 +0000284 if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000285 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000286 } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000287 OS << OID->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +0000288 } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000289 if (OC->IsClassExtension()) {
290 OS << OC->getClassInterface()->getName();
291 } else {
David Majnemer58ed0f32016-07-17 00:39:12 +0000292 OS << OC->getIdentifier()->getNameStart() << '('
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000293 << OC->getIdentifier()->getNameStart() << ')';
294 }
David Majnemer58ed0f32016-07-17 00:39:12 +0000295 } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000296 OS << OCD->getClassInterface()->getName() << '(' << OCD->getName() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000297 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000298 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000299 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000300 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000301 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000302 ClassTy.print(OS, PrintingPolicy(LangOptions()));
303 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000304 }
305 OS << ' ' << OMD->getSelector().getAsString() << ']';
306
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000307 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000308}
309
Guy Benyei11169dd2012-12-18 14:30:41 +0000310StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000311 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000312}
313
Eric Christophere7b87e52014-10-26 23:40:33 +0000314StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Majnemerbc5976a2016-07-01 23:12:54 +0000315 if (isa<ClassTemplateSpecializationDecl>(RD)) {
316 SmallString<128> Name;
David Blaikie65813a32014-04-02 18:21:09 +0000317 llvm::raw_svector_ostream OS(Name);
Reid Kleckner59d12202017-08-08 01:33:53 +0000318 RD->getNameForDiagnostic(OS, getPrintingPolicy(),
David Blaikie65813a32014-04-02 18:21:09 +0000319 /*Qualified*/ false);
David Majnemerbc5976a2016-07-01 23:12:54 +0000320
321 // Copy this name on the side and use its reference.
322 return internString(Name);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000323 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000324
David Majnemerbc5976a2016-07-01 23:12:54 +0000325 // quick optimization to avoid having to intern strings that are already
326 // stored reliably elsewhere
327 if (const IdentifierInfo *II = RD->getIdentifier())
328 return II->getName();
329
330 // The CodeView printer in LLVM wants to see the names of unnamed types: it is
331 // used to reconstruct the fully qualified type names.
332 if (CGM.getCodeGenOpts().EmitCodeView) {
333 if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) {
334 assert(RD->getDeclContext() == D->getDeclContext() &&
335 "Typedef should not be in another decl context!");
336 assert(D->getDeclName().getAsIdentifierInfo() &&
337 "Typedef was not named!");
338 return D->getDeclName().getAsIdentifierInfo()->getName();
339 }
340
341 if (CGM.getLangOpts().CPlusPlus) {
342 StringRef Name;
343
344 ASTContext &Context = CGM.getContext();
345 if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD))
346 // Anonymous types without a name for linkage purposes have their
347 // declarator mangled in if they have one.
348 Name = DD->getName();
349 else if (const TypedefNameDecl *TND =
350 Context.getTypedefNameForUnnamedTagDecl(RD))
351 // Anonymous types without a name for linkage purposes have their
352 // associate typedef mangled in if they have one.
353 Name = TND->getName();
354
355 if (!Name.empty()) {
356 SmallString<256> UnnamedType("<unnamed-type-");
357 UnnamedType += Name;
358 UnnamedType += '>';
359 return internString(UnnamedType);
360 }
361 }
362 }
363
364 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +0000365}
366
Scott Linder94834f12018-02-12 19:47:05 +0000367Optional<llvm::DIFile::ChecksumKind>
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000368CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
369 Checksum.clear();
370
Paul Robinson76178632018-05-25 22:35:59 +0000371 if (!CGM.getCodeGenOpts().EmitCodeView &&
372 CGM.getCodeGenOpts().DwarfVersion < 5)
Scott Linder94834f12018-02-12 19:47:05 +0000373 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000374
375 SourceManager &SM = CGM.getContext().getSourceManager();
376 bool Invalid;
Paul Robinson76178632018-05-25 22:35:59 +0000377 llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid);
378 if (Invalid)
Scott Linder94834f12018-02-12 19:47:05 +0000379 return None;
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000380
381 llvm::MD5 Hash;
382 llvm::MD5::MD5Result Result;
383
384 Hash.update(MemBuffer->getBuffer());
385 Hash.final(Result);
386
387 Hash.stringifyResult(Result, Checksum);
388 return llvm::DIFile::CSK_MD5;
389}
390
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000391Optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
392 FileID FID) {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000393 if (!CGM.getCodeGenOpts().EmbedSource)
394 return None;
395
396 bool SourceInvalid = false;
397 StringRef Source = SM.getBufferData(FID, &SourceInvalid);
398
399 if (SourceInvalid)
400 return None;
401
402 return Source;
403}
404
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000405llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000406 if (!Loc.isValid())
407 // If Location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000408 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000409
410 SourceManager &SM = CGM.getContext().getSourceManager();
411 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
412
Ilya Biryukov660d2332018-12-04 16:30:45 +0000413 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Guy Benyei11169dd2012-12-18 14:30:41 +0000414 // If the location is not valid then use main input file.
Scott Linder39ceac12018-02-26 16:31:08 +0000415 return getOrCreateMainFile();
Guy Benyei11169dd2012-12-18 14:30:41 +0000416
417 // Cache the results.
Ilya Biryukov660d2332018-12-04 16:30:45 +0000418 const char *fname = PLoc.getFilename();
419 auto It = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000420
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000421 if (It != DIFileCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 // Verify that the information still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000423 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000424 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 }
426
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000427 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000428 Optional<llvm::DIFile::ChecksumKind> CSKind =
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000429 computeChecksum(SM.getFileID(Loc), Checksum);
Scott Linder94834f12018-02-12 19:47:05 +0000430 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
431 if (CSKind)
432 CSInfo.emplace(*CSKind, Checksum);
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000433
Ilya Biryukov660d2332018-12-04 16:30:45 +0000434 llvm::DIFile *F = DBuilder.createFile(
435 remapDIPath(PLoc.getFilename()), remapDIPath(getCurrentDirname()), CSInfo,
436 getSource(SM, SM.getFileID(Loc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000437
Ilya Biryukov660d2332018-12-04 16:30:45 +0000438 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 return F;
440}
441
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000442llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Scott Lindera2fbcef2018-02-26 17:32:31 +0000443 return DBuilder.createFile(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000444 remapDIPath(TheCU->getFilename()), remapDIPath(TheCU->getDirectory()),
Scott Lindera2fbcef2018-02-26 17:32:31 +0000445 TheCU->getFile()->getChecksum(),
446 CGM.getCodeGenOpts().EmbedSource ? TheCU->getSource() : None);
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000447}
448
449std::string CGDebugInfo::remapDIPath(StringRef Path) const {
450 for (const auto &Entry : DebugPrefixMap)
451 if (Path.startswith(Entry.first))
452 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
453 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000454}
455
Guy Benyei11169dd2012-12-18 14:30:41 +0000456unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
457 if (Loc.isInvalid() && CurLoc.isInvalid())
458 return 0;
459 SourceManager &SM = CGM.getContext().getSourceManager();
460 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462}
463
Adrian Prantlc7822422013-03-12 20:43:25 +0000464unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000466 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 return 0;
468
469 // If the location is invalid then use the current column.
470 if (Loc.isInvalid() && CurLoc.isInvalid())
471 return 0;
472 SourceManager &SM = CGM.getContext().getSourceManager();
473 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000474 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000475}
476
477StringRef CGDebugInfo::getCurrentDirname() {
478 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
479 return CGM.getCodeGenOpts().DebugCompilationDir;
480
481 if (!CWDName.empty())
482 return CWDName;
483 SmallString<256> CWD;
484 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000485 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000486}
487
Guy Benyei11169dd2012-12-18 14:30:41 +0000488void CGDebugInfo::CreateCompileUnit() {
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000489 SmallString<32> Checksum;
Scott Linder94834f12018-02-12 19:47:05 +0000490 Optional<llvm::DIFile::ChecksumKind> CSKind;
491 Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000492
David Blaikieaabde052014-05-14 00:29:00 +0000493 // Should we be asking the SourceManager for the main file name, instead of
494 // accepting it as an argument? This just causes the main file name to
495 // mismatch with source locations and create extra lexical scopes or
496 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
497 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
498 // because that's what the SourceManager says)
499
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 // Get absolute path name.
501 SourceManager &SM = CGM.getContext().getSourceManager();
502 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
503 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000504 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000505
506 // The main file name provided via the "-main-file-name" option contains just
507 // the file name itself with no path information. This file name may have had
508 // a relative path, so we look into the actual file entry for the main
509 // file to determine the real absolute path for the file.
510 std::string MainFileDir;
511 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000512 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000513 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000514 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
515 llvm::sys::path::append(MainFileDirSS, MainFileName);
516 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000517 }
Taewook Oh0fb5b782017-08-16 19:36:24 +0000518 // If the main file name provided is identical to the input file name, and
519 // if the input file is a preprocessed source, use the module name for
520 // debug info. The module name comes from the name specified in the first
521 // linemarker if the input is a preprocessed source.
522 if (MainFile->getName() == MainFileName &&
523 FrontendOptions::getInputKindForExtension(
524 MainFile->getName().rsplit('.').second)
525 .isPreprocessed())
526 MainFileName = CGM.getModule().getName().str();
527
Amjad Aboude2aab8c2016-12-25 10:12:27 +0000528 CSKind = computeChecksum(SM.getMainFileID(), Checksum);
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 }
530
Ed Masteda706022014-05-07 12:49:30 +0000531 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000532 const LangOptions &LO = CGM.getLangOpts();
533 if (LO.CPlusPlus) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000534 if (LO.ObjC)
Guy Benyei11169dd2012-12-18 14:30:41 +0000535 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
536 else
537 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000538 } else if (LO.ObjC) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000539 LangTag = llvm::dwarf::DW_LANG_ObjC;
Pirama Arumuga Nainara7484c92016-06-21 21:35:11 +0000540 } else if (LO.RenderScript) {
541 LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript;
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 } else if (LO.C99) {
543 LangTag = llvm::dwarf::DW_LANG_C99;
544 } else {
545 LangTag = llvm::dwarf::DW_LANG_C89;
546 }
547
548 std::string Producer = getClangFullVersion();
549
550 // Figure out which version of the ObjC runtime we have.
551 unsigned RuntimeVers = 0;
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000552 if (LO.ObjC)
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
554
Adrian Prantl826824e2016-04-08 22:43:06 +0000555 llvm::DICompileUnit::DebugEmissionKind EmissionKind;
556 switch (DebugKind) {
557 case codegenoptions::NoDebugInfo:
558 case codegenoptions::LocTrackingOnly:
559 EmissionKind = llvm::DICompileUnit::NoDebug;
560 break;
561 case codegenoptions::DebugLineTablesOnly:
562 EmissionKind = llvm::DICompileUnit::LineTablesOnly;
563 break;
Alexey Bataev80e1b5e2018-08-31 13:56:14 +0000564 case codegenoptions::DebugDirectivesOnly:
565 EmissionKind = llvm::DICompileUnit::DebugDirectivesOnly;
566 break;
Adrian Prantl826824e2016-04-08 22:43:06 +0000567 case codegenoptions::LimitedDebugInfo:
568 case codegenoptions::FullDebugInfo:
569 EmissionKind = llvm::DICompileUnit::FullDebug;
570 break;
571 }
572
Scott Linder94834f12018-02-12 19:47:05 +0000573 if (CSKind)
574 CSInfo.emplace(*CSKind, Checksum);
575
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000577 // FIXME - Eliminate TheCU.
Adrian Prantlb4423022017-08-04 23:08:57 +0000578 auto &CGOpts = CGM.getCodeGenOpts();
Eric Christophere4200a22014-02-27 01:25:08 +0000579 TheCU = DBuilder.createCompileUnit(
David Blaikie81503552017-04-21 23:35:36 +0000580 LangTag,
581 DBuilder.createFile(remapDIPath(MainFileName),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000582 remapDIPath(getCurrentDirname()), CSInfo,
Scott Lindera2fbcef2018-02-26 17:32:31 +0000583 getSource(SM, SM.getMainFileID())),
Mikhail Maltsev4a4e7a32018-04-23 10:08:46 +0000584 CGOpts.EmitVersionIdentMetadata ? Producer : "",
Tobias Edler von Koch7609cb82018-06-22 20:23:21 +0000585 LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
Adrian Prantlb4423022017-08-04 23:08:57 +0000586 CGOpts.DwarfDebugFlags, RuntimeVers,
George Rimar91829ee2018-11-14 09:22:16 +0000587 (CGOpts.getSplitDwarfMode() != CodeGenOptions::NoFission)
588 ? ""
589 : CGOpts.SplitDwarfFile,
590 EmissionKind, 0 /* DWOid */, CGOpts.SplitDwarfInlining,
591 CGOpts.DebugInfoForProfiling,
David Blaikie9982bb82018-08-16 23:56:32 +0000592 CGM.getTarget().getTriple().isNVPTX()
593 ? llvm::DICompileUnit::DebugNameTableKind::None
David Blaikie65864522018-08-20 20:14:08 +0000594 : static_cast<llvm::DICompileUnit::DebugNameTableKind>(
David Blaikie27692de2018-11-13 20:08:13 +0000595 CGOpts.DebugNameTable),
596 CGOpts.DebugRangesBaseAddress);
Guy Benyei11169dd2012-12-18 14:30:41 +0000597}
598
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000599llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000600 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 StringRef BTName;
602 switch (BT->getKind()) {
603#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000604#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000605#include "clang/AST/BuiltinTypes.def"
606 case BuiltinType::Dependent:
607 llvm_unreachable("Unexpected builtin type");
608 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000609 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000610 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000611 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000613 if (!ClassTy)
614 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
615 "objc_class", TheCU,
616 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000617 return ClassTy;
618 case BuiltinType::ObjCId: {
619 // typedef struct objc_class *Class;
620 // typedef struct objc_object {
621 // Class isa;
622 // } *id;
623
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000624 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000625 return ObjTy;
626
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000627 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000628 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
629 "objc_class", TheCU,
630 getOrCreateMainFile(), 0);
631
632 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000633
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000634 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000635
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000636 ObjTy = DBuilder.createStructType(
637 TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0,
638 llvm::DINode::FlagZero, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000639
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000640 DBuilder.replaceArrays(
Leny Kholodovdf050fd2016-09-06 17:06:14 +0000641 ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
642 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0,
643 llvm::DINode::FlagZero, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000644 return ObjTy;
645 }
646 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000647 if (!SelTy)
648 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
649 "objc_selector", TheCU,
650 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 return SelTy;
652 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000653
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000654#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
655 case BuiltinType::Id: \
656 return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \
Alexey Bader954ba212016-04-08 13:40:33 +0000657 SingletonId);
Alexey Baderb62f1442016-04-13 08:33:41 +0000658#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +0000659 case BuiltinType::OCLSampler:
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000660 return getOrCreateStructPtrType("opencl_sampler_t", OCLSamplerDITy);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000661 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000662 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000663 case BuiltinType::OCLClkEvent:
664 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
665 case BuiltinType::OCLQueue:
666 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000667 case BuiltinType::OCLReserveID:
668 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000669#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
670 case BuiltinType::Id: \
671 return getOrCreateStructPtrType("opencl_" #ExtType, Id##Ty);
672#include "clang/Basic/OpenCLExtensionTypes.def"
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000673
Guy Benyei11169dd2012-12-18 14:30:41 +0000674 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000675 case BuiltinType::Char_U:
676 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
677 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000679 case BuiltinType::SChar:
680 Encoding = llvm::dwarf::DW_ATE_signed_char;
681 break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000682 case BuiltinType::Char8:
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000684 case BuiltinType::Char32:
685 Encoding = llvm::dwarf::DW_ATE_UTF;
686 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000687 case BuiltinType::UShort:
688 case BuiltinType::UInt:
689 case BuiltinType::UInt128:
690 case BuiltinType::ULong:
691 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000692 case BuiltinType::ULongLong:
693 Encoding = llvm::dwarf::DW_ATE_unsigned;
694 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 case BuiltinType::Short:
696 case BuiltinType::Int:
697 case BuiltinType::Int128:
698 case BuiltinType::Long:
699 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000700 case BuiltinType::LongLong:
701 Encoding = llvm::dwarf::DW_ATE_signed;
702 break;
703 case BuiltinType::Bool:
704 Encoding = llvm::dwarf::DW_ATE_boolean;
705 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 case BuiltinType::Half:
707 case BuiltinType::Float:
708 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000709 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000710 case BuiltinType::Float128:
Eric Christophere7b87e52014-10-26 23:40:33 +0000711 case BuiltinType::Double:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000712 // FIXME: For targets where long double and __float128 have the same size,
713 // they are currently indistinguishable in the debugger without some
714 // special treatment. However, there is currently no consensus on encoding
715 // and this should be updated once a DWARF encoding exists for distinct
716 // floating point types of the same size.
Eric Christophere7b87e52014-10-26 23:40:33 +0000717 Encoding = llvm::dwarf::DW_ATE_float;
718 break;
Leonard Chanf921d852018-06-04 16:07:52 +0000719 case BuiltinType::ShortAccum:
720 case BuiltinType::Accum:
721 case BuiltinType::LongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000722 case BuiltinType::ShortFract:
723 case BuiltinType::Fract:
724 case BuiltinType::LongFract:
725 case BuiltinType::SatShortFract:
726 case BuiltinType::SatFract:
727 case BuiltinType::SatLongFract:
728 case BuiltinType::SatShortAccum:
729 case BuiltinType::SatAccum:
730 case BuiltinType::SatLongAccum:
Leonard Chanf921d852018-06-04 16:07:52 +0000731 Encoding = llvm::dwarf::DW_ATE_signed_fixed;
732 break;
733 case BuiltinType::UShortAccum:
734 case BuiltinType::UAccum:
735 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000736 case BuiltinType::UShortFract:
737 case BuiltinType::UFract:
738 case BuiltinType::ULongFract:
739 case BuiltinType::SatUShortAccum:
740 case BuiltinType::SatUAccum:
741 case BuiltinType::SatULongAccum:
742 case BuiltinType::SatUShortFract:
743 case BuiltinType::SatUFract:
744 case BuiltinType::SatULongFract:
Leonard Chanf921d852018-06-04 16:07:52 +0000745 Encoding = llvm::dwarf::DW_ATE_unsigned_fixed;
746 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000747 }
748
749 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000750 case BuiltinType::Long:
751 BTName = "long int";
752 break;
753 case BuiltinType::LongLong:
754 BTName = "long long int";
755 break;
756 case BuiltinType::ULong:
757 BTName = "long unsigned int";
758 break;
759 case BuiltinType::ULongLong:
760 BTName = "long long unsigned int";
761 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000762 default:
763 BTName = BT->getName(CGM.getLangOpts());
764 break;
765 }
Victor Leschuka7ece032016-10-20 00:13:19 +0000766 // Bit size and offset of the type.
Guy Benyei11169dd2012-12-18 14:30:41 +0000767 uint64_t Size = CGM.getContext().getTypeSize(BT);
Victor Leschuka7ece032016-10-20 00:13:19 +0000768 return DBuilder.createBasicType(BTName, Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000769}
770
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000771llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Victor Leschuka7ece032016-10-20 00:13:19 +0000772 // Bit size and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000773 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000774 if (Ty->isComplexIntegerType())
775 Encoding = llvm::dwarf::DW_ATE_lo_user;
776
777 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +0000778 return DBuilder.createBasicType("complex", Size, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000779}
780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
782 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000783 QualifierCollector Qc;
784 const Type *T = Qc.strip(Ty);
785
786 // Ignore these qualifiers for now.
787 Qc.removeObjCGCAttr();
788 Qc.removeAddressSpace();
789 Qc.removeObjCLifetime();
790
791 // We will create one Derived type for one qualifier and recurse to handle any
792 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000793 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000794 if (Qc.hasConst()) {
795 Tag = llvm::dwarf::DW_TAG_const_type;
796 Qc.removeConst();
797 } else if (Qc.hasVolatile()) {
798 Tag = llvm::dwarf::DW_TAG_volatile_type;
799 Qc.removeVolatile();
800 } else if (Qc.hasRestrict()) {
801 Tag = llvm::dwarf::DW_TAG_restrict_type;
802 Qc.removeRestrict();
803 } else {
804 assert(Qc.empty() && "Unknown type qualifier for debug info");
805 return getOrCreateType(QualType(T, 0), Unit);
806 }
807
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000808 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000809
810 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
811 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000812 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000813}
814
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
816 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000817
818 // The frontend treats 'id' as a typedef to an ObjCObjectType,
819 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
820 // debug info, we want to emit 'id' in both cases.
821 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000822 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000823
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000824 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
825 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000826}
827
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000828llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
829 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000830 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000831 Ty->getPointeeType(), Unit);
832}
833
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000834/// \return whether a C++ mangling exists for the type defined by TD.
835static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
836 switch (TheCU->getSourceLanguage()) {
837 case llvm::dwarf::DW_LANG_C_plus_plus:
838 return true;
839 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
840 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
841 default:
842 return false;
843 }
844}
845
Reid Kleckner59320fc2018-08-17 20:59:52 +0000846// Determines if the debug info for this tag declaration needs a type
847// identifier. The purpose of the unique identifier is to deduplicate type
848// information for identical types across TUs. Because of the C++ one definition
849// rule (ODR), it is valid to assume that the type is defined the same way in
850// every TU and its debug info is equivalent.
851//
852// C does not have the ODR, and it is common for codebases to contain multiple
853// different definitions of a struct with the same name in different TUs.
854// Therefore, if the type doesn't have a C++ mangling, don't give it an
855// identifer. Type information in C is smaller and simpler than C++ type
856// information, so the increase in debug info size is negligible.
857//
858// If the type is not externally visible, it should be unique to the current TU,
859// and should not need an identifier to participate in type deduplication.
860// However, when emitting CodeView, the format internally uses these
861// unique type name identifers for references between debug info. For example,
862// the method of a class in an anonymous namespace uses the identifer to refer
863// to its parent class. The Microsoft C++ ABI attempts to provide unique names
864// for such types, so when emitting CodeView, always use identifiers for C++
865// types. This may create problems when attempting to emit CodeView when the MS
866// C++ ABI is not in use.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000867static bool needsTypeIdentifier(const TagDecl *TD, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000868 llvm::DICompileUnit *TheCU) {
869 // We only add a type identifier for types with C++ name mangling.
870 if (!hasCXXMangling(TD, TheCU))
871 return false;
872
Brock Wyma8557ec52018-05-22 12:41:19 +0000873 // Externally visible types with C++ mangling need a type identifier.
874 if (TD->isExternallyVisible())
875 return true;
876
Reid Kleckner59320fc2018-08-17 20:59:52 +0000877 // CodeView types with C++ mangling need a type identifier.
878 if (CGM.getCodeGenOpts().EmitCodeView)
879 return true;
880
Brock Wyma8557ec52018-05-22 12:41:19 +0000881 return false;
882}
883
Reid Kleckner59320fc2018-08-17 20:59:52 +0000884// Returns a unique type identifier string if one exists, or an empty string.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000885static SmallString<256> getTypeIdentifier(const TagType *Ty, CodeGenModule &CGM,
Brock Wyma8557ec52018-05-22 12:41:19 +0000886 llvm::DICompileUnit *TheCU) {
887 SmallString<256> Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000888 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000889
Brock Wyma8557ec52018-05-22 12:41:19 +0000890 if (!needsTypeIdentifier(TD, CGM, TheCU))
891 return Identifier;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000892
Manman Rene0064d82013-08-29 23:19:58 +0000893 // TODO: This is using the RTTI name. Is there a better way to get
894 // a unique string for a type?
Brock Wyma8557ec52018-05-22 12:41:19 +0000895 llvm::raw_svector_ostream Out(Identifier);
Manman Rene0064d82013-08-29 23:19:58 +0000896 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Brock Wyma8557ec52018-05-22 12:41:19 +0000897 return Identifier;
Manman Rene0064d82013-08-29 23:19:58 +0000898}
899
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000900/// \return the appropriate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000901static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +0000902 llvm::dwarf::Tag Tag;
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000903 if (RD->isStruct() || RD->isInterface())
904 Tag = llvm::dwarf::DW_TAG_structure_type;
905 else if (RD->isUnion())
906 Tag = llvm::dwarf::DW_TAG_union_type;
907 else {
908 // FIXME: This could be a struct type giving a default visibility different
909 // than C++ class type, but needs llvm metadata changes first.
910 assert(RD->isClass());
911 Tag = llvm::dwarf::DW_TAG_class_type;
912 }
913 return Tag;
914}
915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000916llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000917CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000918 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000919 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000920 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
921 return cast<llvm::DICompositeType>(T);
922 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000923 unsigned Line = getLineNumber(RD->getLocation());
924 StringRef RDName = getClassName(RD);
925
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000926 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +0000927 uint32_t Align = 0;
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000928
Guy Benyei11169dd2012-12-18 14:30:41 +0000929 // Create the type.
Brock Wyma8557ec52018-05-22 12:41:19 +0000930 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000931 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000932 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +0000933 llvm::DINode::FlagFwdDecl, Identifier);
Paul Robinson1787f812017-09-28 18:37:02 +0000934 if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
935 if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
936 DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
937 CollectCXXTemplateParams(TSpecial, DefUnit));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000938 ReplaceMap.emplace_back(
939 std::piecewise_construct, std::make_tuple(Ty),
940 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000941 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000942}
943
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000944llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000945 const Type *Ty,
946 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000947 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000948 // Bit size, align and offset of the type.
949 // Size is always the size of a pointer. We can't use getTypeSize here
950 // because that does not return the correct value for references.
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000951 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy);
952 uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace);
Victor Leschuka7ece032016-10-20 00:13:19 +0000953 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000954 Optional<unsigned> DWARFAddressSpace =
955 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000956
Keno Fischer87842f32015-11-16 09:04:13 +0000957 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
958 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
959 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000960 Size, Align, DWARFAddressSpace);
Keno Fischer87842f32015-11-16 09:04:13 +0000961 else
962 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +0000963 Align, DWARFAddressSpace);
Guy Benyei11169dd2012-12-18 14:30:41 +0000964}
965
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000966llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
967 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000968 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000969 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000970 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
971 TheCU, getOrCreateMainFile(), 0);
972 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
973 Cache = DBuilder.createPointerType(Cache, Size);
974 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000975}
976
Scott Linder58df0e42018-08-08 15:56:12 +0000977uint64_t CGDebugInfo::collectDefaultElementTypesForBlockPointer(
978 const BlockPointerType *Ty, llvm::DIFile *Unit, llvm::DIDerivedType *DescTy,
979 unsigned LineNo, SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000980 QualType FType;
Guy Benyei11169dd2012-12-18 14:30:41 +0000981
Scott Linder58df0e42018-08-08 15:56:12 +0000982 // Advanced by calls to CreateMemberType in increments of FType, then
983 // returned as the overall size of the default elements.
984 uint64_t FieldOffset = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000985
Scott Linder58df0e42018-08-08 15:56:12 +0000986 // Blocks in OpenCL have unique constraints which make the standard fields
987 // redundant while requiring size and align fields for enqueue_kernel. See
988 // initializeForBlockHeader in CGBlocks.cpp
Scott Linder2b5cf042018-07-30 20:31:11 +0000989 if (CGM.getLangOpts().OpenCL) {
990 FType = CGM.getContext().IntTy;
991 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
992 EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset));
993 } else {
994 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
995 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
996 FType = CGM.getContext().IntTy;
997 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
998 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
999 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
1000 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
1001 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Scott Linder58df0e42018-08-08 15:56:12 +00001002 uint64_t FieldSize = CGM.getContext().getTypeSize(Ty);
1003 uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty);
Scott Linder2b5cf042018-07-30 20:31:11 +00001004 EltTys.push_back(DBuilder.createMemberType(
Scott Linder58df0e42018-08-08 15:56:12 +00001005 Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign,
1006 FieldOffset, llvm::DINode::FlagZero, DescTy));
Scott Lindera7c45682018-07-30 22:52:07 +00001007 FieldOffset += FieldSize;
Scott Linder2b5cf042018-07-30 20:31:11 +00001008 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001009
Scott Linder58df0e42018-08-08 15:56:12 +00001010 return FieldOffset;
1011}
1012
1013llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
1014 llvm::DIFile *Unit) {
1015 SmallVector<llvm::Metadata *, 8> EltTys;
1016 QualType FType;
1017 uint64_t FieldOffset;
1018 llvm::DINodeArray Elements;
1019
1020 FieldOffset = 0;
1021 FType = CGM.getContext().UnsignedLongTy;
1022 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
1023 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
1024
1025 Elements = DBuilder.getOrCreateArray(EltTys);
1026 EltTys.clear();
1027
1028 llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
1029
1030 auto *EltTy =
1031 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0,
1032 FieldOffset, 0, Flags, nullptr, Elements);
1033
1034 // Bit size, align and offset of the type.
1035 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1036
1037 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
1038
1039 FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy,
1040 0, EltTys);
1041
Guy Benyei11169dd2012-12-18 14:30:41 +00001042 Elements = DBuilder.getOrCreateArray(EltTys);
1043
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001044 // The __block_literal_generic structs are marked with a special
1045 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
1046 // the debugger needs to know about. To allow type uniquing, emit
1047 // them without a name or a location.
Scott Linder58df0e42018-08-08 15:56:12 +00001048 EltTy = DBuilder.createStructType(Unit, "", nullptr, 0, FieldOffset, 0,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001049 Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050
Adrian Prantl3d2c0512015-07-07 00:49:35 +00001051 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001052}
1053
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001054llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1055 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +00001056 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +00001058
1059 SmallString<128> NS;
1060 llvm::raw_svector_ostream OS(NS);
Serge Pavlov03e672c2017-11-28 16:14:14 +00001061 Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false);
1062 printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy());
David Blaikief1b382e2014-04-06 17:14:06 +00001063
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001064 auto *AliasDecl =
1065 cast<TypeAliasTemplateDecl>(Ty->getTemplateName().getAsTemplateDecl())
1066 ->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +00001067
1068 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +00001069 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
1070 getLineNumber(Loc),
1071 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +00001072}
1073
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001074llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1075 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001076 // We don't set size information, but do specify where the typedef was
1077 // declared.
Amjad Abouddc4531e2016-04-30 01:44:38 +00001078 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001079
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001080 // Typedefs are derived from some other type.
1081 return DBuilder.createTypedef(
1082 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
1083 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Amjad Abouddc4531e2016-04-30 01:44:38 +00001084 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001085}
1086
Reid Klecknerf00f8032016-06-08 20:41:54 +00001087static unsigned getDwarfCC(CallingConv CC) {
1088 switch (CC) {
1089 case CC_C:
1090 // Avoid emitting DW_AT_calling_convention if the C convention was used.
1091 return 0;
1092
1093 case CC_X86StdCall:
1094 return llvm::dwarf::DW_CC_BORLAND_stdcall;
1095 case CC_X86FastCall:
1096 return llvm::dwarf::DW_CC_BORLAND_msfastcall;
1097 case CC_X86ThisCall:
1098 return llvm::dwarf::DW_CC_BORLAND_thiscall;
1099 case CC_X86VectorCall:
1100 return llvm::dwarf::DW_CC_LLVM_vectorcall;
1101 case CC_X86Pascal:
1102 return llvm::dwarf::DW_CC_BORLAND_pascal;
Martin Storsjo022e7822017-07-17 20:49:45 +00001103 case CC_Win64:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001104 return llvm::dwarf::DW_CC_LLVM_Win64;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001105 case CC_X86_64SysV:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001106 return llvm::dwarf::DW_CC_LLVM_X86_64SysV;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001107 case CC_AAPCS:
Sander de Smalen44a22532018-11-26 16:38:37 +00001108 case CC_AArch64VectorCall:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001109 return llvm::dwarf::DW_CC_LLVM_AAPCS;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001110 case CC_AAPCS_VFP:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001111 return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001112 case CC_IntelOclBicc:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001113 return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001114 case CC_SpirFunction:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001115 return llvm::dwarf::DW_CC_LLVM_SpirFunction;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00001116 case CC_OpenCLKernel:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001117 return llvm::dwarf::DW_CC_LLVM_OpenCLKernel;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001118 case CC_Swift:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001119 return llvm::dwarf::DW_CC_LLVM_Swift;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001120 case CC_PreserveMost:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001121 return llvm::dwarf::DW_CC_LLVM_PreserveMost;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001122 case CC_PreserveAll:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001123 return llvm::dwarf::DW_CC_LLVM_PreserveAll;
Erich Keane757d3172016-11-02 18:29:35 +00001124 case CC_X86RegCall:
Jonas Devliegheref0702682018-03-22 13:53:30 +00001125 return llvm::dwarf::DW_CC_LLVM_X86RegCall;
Reid Klecknerf00f8032016-06-08 20:41:54 +00001126 }
1127 return 0;
1128}
1129
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001130llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1131 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001132 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001133
1134 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +00001135 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001136
1137 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +00001138 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 if (isa<FunctionNoProtoType>(Ty))
1140 EltTys.push_back(DBuilder.createUnspecifiedParameter());
David Majnemer58ed0f32016-07-17 00:39:12 +00001141 else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) {
1142 for (const QualType &ParamType : FPT->param_types())
1143 EltTys.push_back(getOrCreateType(ParamType, Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +00001144 if (FPT->isVariadic())
1145 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 }
1147
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001148 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00001149 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
Reid Klecknerf00f8032016-06-08 20:41:54 +00001150 getDwarfCC(Ty->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001151}
1152
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001153/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +00001154/// As an optimization, return 0 if the access specifier equals the
1155/// default for the containing type.
Leny Kholodovdf050fd2016-09-06 17:06:14 +00001156static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access,
1157 const RecordDecl *RD) {
Adrian Prantl21361fb2014-08-29 22:44:27 +00001158 AccessSpecifier Default = clang::AS_none;
1159 if (RD && RD->isClass())
1160 Default = clang::AS_private;
1161 else if (RD && (RD->isStruct() || RD->isUnion()))
1162 Default = clang::AS_public;
1163
1164 if (Access == Default)
Leny Kholodov80c047d2016-09-06 10:48:04 +00001165 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001166
Eric Christophere7b87e52014-10-26 23:40:33 +00001167 switch (Access) {
1168 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001169 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +00001170 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001171 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +00001172 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001173 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +00001174 case clang::AS_none:
Leny Kholodov80c047d2016-09-06 10:48:04 +00001175 return llvm::DINode::FlagZero;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001176 }
1177 llvm_unreachable("unexpected access enumerator");
1178}
Guy Benyei11169dd2012-12-18 14:30:41 +00001179
David Majnemerb4b671e2016-06-30 03:01:59 +00001180llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
1181 llvm::DIScope *RecordTy,
1182 const RecordDecl *RD) {
1183 StringRef Name = BitFieldDecl->getName();
1184 QualType Ty = BitFieldDecl->getType();
1185 SourceLocation Loc = BitFieldDecl->getLocation();
1186 llvm::DIFile *VUnit = getOrCreateFile(Loc);
1187 llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
1188
1189 // Get the location for the field.
1190 llvm::DIFile *File = getOrCreateFile(Loc);
1191 unsigned Line = getLineNumber(Loc);
1192
1193 const CGBitFieldInfo &BitFieldInfo =
1194 CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
1195 uint64_t SizeInBits = BitFieldInfo.Size;
1196 assert(SizeInBits > 0 && "found named 0-width bitfield");
David Majnemerb4b671e2016-06-30 03:01:59 +00001197 uint64_t StorageOffsetInBits =
1198 CGM.getContext().toBits(BitFieldInfo.StorageOffset);
Reid Kleckner06a4b2a2017-06-12 19:57:56 +00001199 uint64_t Offset = BitFieldInfo.Offset;
1200 // The bit offsets for big endian machines are reversed for big
1201 // endian target, compensate for that as the DIDerivedType requires
1202 // un-reversed offsets.
1203 if (CGM.getDataLayout().isBigEndian())
1204 Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset;
1205 uint64_t OffsetInBits = StorageOffsetInBits + Offset;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001206 llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD);
David Majnemerb4b671e2016-06-30 03:01:59 +00001207 return DBuilder.createBitFieldMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001208 RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1209 Flags, DebugType);
David Majnemerb4b671e2016-06-30 03:01:59 +00001210}
1211
1212llvm::DIType *
1213CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc,
1214 AccessSpecifier AS, uint64_t offsetInBits,
Victor Leschuka7ece032016-10-20 00:13:19 +00001215 uint32_t AlignInBits, llvm::DIFile *tunit,
1216 llvm::DIScope *scope, const RecordDecl *RD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001218
1219 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001220 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001221 unsigned line = getLineNumber(loc);
1222
David Majnemer34b57492014-07-30 01:30:47 +00001223 uint64_t SizeInBits = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00001224 auto Align = AlignInBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001225 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +00001226 TypeInfo TI = CGM.getContext().getTypeInfo(type);
1227 SizeInBits = TI.Width;
Victor Leschuka7ece032016-10-20 00:13:19 +00001228 if (!Align)
1229 Align = getTypeAlignIfRequired(type, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00001230 }
1231
Leny Kholodov80c047d2016-09-06 10:48:04 +00001232 llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001233 return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align,
1234 offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001235}
1236
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001237void CGDebugInfo::CollectRecordLambdaFields(
1238 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +00001240 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
1241 // has the name and the location of the variable so we should iterate over
1242 // both concurrently.
1243 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
1244 RecordDecl::field_iterator Field = CXXDecl->field_begin();
1245 unsigned fieldno = 0;
1246 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001247 E = CXXDecl->captures_end();
1248 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001249 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +00001250 if (C.capturesVariable()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001251 SourceLocation Loc = C.getLocation();
1252 assert(!Field->isBitField() && "lambdas don't have bitfield members!");
Eric Christopher91a31902013-01-16 01:22:32 +00001253 VarDecl *V = C.getCapturedVar();
Eric Christopher91a31902013-01-16 01:22:32 +00001254 StringRef VName = V->getName();
David Majnemerb4b671e2016-06-30 03:01:59 +00001255 llvm::DIFile *VUnit = getOrCreateFile(Loc);
Victor Leschuka7ece032016-10-20 00:13:19 +00001256 auto Align = getDeclAlignIfRequired(V, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001257 llvm::DIType *FieldType = createFieldType(
1258 VName, Field->getType(), Loc, Field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001259 layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
David Majnemerb4b671e2016-06-30 03:01:59 +00001260 elements.push_back(FieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00001261 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +00001262 // TODO: Need to handle 'this' in some way by probably renaming the
1263 // this of the lambda class and having a field member of 'this' or
1264 // by using AT_object_pointer for the function and having that be
1265 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +00001266 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001267 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +00001268 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269 llvm::DIType *fieldType = createFieldType(
David Majnemerb4b671e2016-06-30 03:01:59 +00001270 "this", type, f->getLocation(), f->getAccess(),
Eric Christophere7b87e52014-10-26 23:40:33 +00001271 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +00001272
1273 elements.push_back(fieldType);
1274 }
1275 }
1276}
1277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278llvm::DIDerivedType *
1279CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00001280 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001281 // Create the descriptor for the static variable, with or without
1282 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +00001283 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001284 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
1285 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +00001286
Eric Christopher91a31902013-01-16 01:22:32 +00001287 unsigned LineNumber = getLineNumber(Var->getLocation());
1288 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +00001289 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +00001290 if (Var->getInit()) {
1291 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +00001292 if (Value) {
1293 if (Value->isInt())
1294 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1295 if (Value->isFloat())
1296 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1297 }
Eric Christopher91a31902013-01-16 01:22:32 +00001298 }
1299
Leny Kholodov80c047d2016-09-06 10:48:04 +00001300 llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
Victor Leschuka7ece032016-10-20 00:13:19 +00001301 auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001302 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
Victor Leschuka7ece032016-10-20 00:13:19 +00001303 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001304 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +00001305 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +00001306}
1307
Eric Christophere7b87e52014-10-26 23:40:33 +00001308void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001309 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
1310 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00001311 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +00001312 StringRef name = field->getName();
1313 QualType type = field->getType();
1314
1315 // Ignore unnamed fields unless they're anonymous structs/unions.
1316 if (name.empty() && !type->isRecordType())
1317 return;
1318
David Majnemerb4b671e2016-06-30 03:01:59 +00001319 llvm::DIType *FieldType;
Eric Christopher91a31902013-01-16 01:22:32 +00001320 if (field->isBitField()) {
David Majnemerb4b671e2016-06-30 03:01:59 +00001321 FieldType = createBitFieldType(field, RecordTy, RD);
1322 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00001323 auto Align = getDeclAlignIfRequired(field, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00001324 FieldType =
1325 createFieldType(name, type, field->getLocation(), field->getAccess(),
Victor Leschuka7ece032016-10-20 00:13:19 +00001326 OffsetInBits, Align, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001327 }
1328
David Majnemerb4b671e2016-06-30 03:01:59 +00001329 elements.push_back(FieldType);
Eric Christopher91a31902013-01-16 01:22:32 +00001330}
1331
Reid Klecknere2e82062017-08-08 20:30:14 +00001332void CGDebugInfo::CollectRecordNestedType(
1333 const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
1334 QualType Ty = CGM.getContext().getTypeDeclType(TD);
Reid Kleckner755220b2016-08-01 18:56:13 +00001335 // Injected class names are not considered nested records.
1336 if (isa<InjectedClassNameType>(Ty))
1337 return;
Reid Klecknere2e82062017-08-08 20:30:14 +00001338 SourceLocation Loc = TD->getLocation();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00001339 llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc));
1340 elements.push_back(nestedType);
1341}
1342
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001343void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001344 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001345 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001346 llvm::DICompositeType *RecordTy) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001347 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001348
Eric Christopher91a31902013-01-16 01:22:32 +00001349 if (CXXDecl && CXXDecl->isLambda())
1350 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1351 else {
1352 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001353
Eric Christopher91a31902013-01-16 01:22:32 +00001354 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001355 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001356
Eric Christopher91a31902013-01-16 01:22:32 +00001357 // Static and non-static members should appear in the same order as
1358 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001359 for (const auto *I : record->decls())
1360 if (const auto *V = dyn_cast<VarDecl>(I)) {
Paul Robinsonb17327d2016-04-27 17:37:12 +00001361 if (V->hasAttr<NoDebugAttr>())
1362 continue;
Reid Kleckner891b2712018-07-20 20:55:00 +00001363
1364 // Skip variable template specializations when emitting CodeView. MSVC
1365 // doesn't emit them.
1366 if (CGM.getCodeGenOpts().EmitCodeView &&
1367 isa<VarTemplateSpecializationDecl>(V))
1368 continue;
1369
David Blaikiece763042013-08-20 21:49:21 +00001370 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001371 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001372 if (MI != StaticDataMemberCache.end()) {
1373 assert(MI->second &&
1374 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001375 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001376 } else {
1377 auto Field = CreateRecordStaticField(V, RecordTy, record);
1378 elements.push_back(Field);
1379 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001380 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001381 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1382 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001383
1384 // Bump field number for next field.
1385 ++fieldNo;
Reid Kleckner891b2712018-07-20 20:55:00 +00001386 } else if (CGM.getCodeGenOpts().EmitCodeView) {
1387 // Debug info for nested types is included in the member list only for
1388 // CodeView.
Reid Klecknere2e82062017-08-08 20:30:14 +00001389 if (const auto *nestedType = dyn_cast<TypeDecl>(I))
1390 if (!nestedType->isImplicit() &&
1391 nestedType->getDeclContext() == record)
1392 CollectRecordNestedType(nestedType, elements);
1393 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 }
1395}
1396
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001397llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001398CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001399 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001400 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001401 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001402 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001403 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001404 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1405 Func, Unit);
1406}
David Blaikie2aaf0652013-01-07 22:24:59 +00001407
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001408llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1409 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001411 llvm::DITypeRefArray Args(
1412 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001413 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001414 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001415
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001416 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001417
1418 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001419 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001420
David Blaikie2aaf0652013-01-07 22:24:59 +00001421 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001422 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001423 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1424 // Create pointer type directly in this case.
1425 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1426 QualType PointeeTy = ThisPtrTy->getPointeeType();
1427 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001428 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Victor Leschuka7ece032016-10-20 00:13:19 +00001429 auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001430 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1431 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001432 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001433 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001434 // TODO: This and the artificial type below are misleading, the
1435 // types aren't artificial the argument is, but the current
1436 // metadata doesn't represent that.
1437 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1438 Elts.push_back(ThisPtrType);
1439 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001440 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001441 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001442 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1443 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 }
1445
1446 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001447 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1448 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001449
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001450 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001451
Leny Kholodov80c047d2016-09-06 10:48:04 +00001452 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001453 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001454 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001455 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001456 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001457
Reid Klecknerf00f8032016-06-08 20:41:54 +00001458 return DBuilder.createSubroutineType(EltTypeArray, Flags,
1459 getDwarfCC(Func->getCallConv()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001460}
1461
Eric Christopherb2a008c2013-05-16 00:45:12 +00001462/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001463/// inside a function.
1464static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001465 if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Guy Benyei11169dd2012-12-18 14:30:41 +00001466 return isFunctionLocalClass(NRD);
1467 if (isa<FunctionDecl>(RD->getDeclContext()))
1468 return true;
1469 return false;
1470}
1471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1473 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001474 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001475 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001476
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001478 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001479
1480 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1481 // make sense to give a single ctor/dtor a linkage name.
1482 StringRef MethodLinkageName;
David Blaikie71647672016-04-12 21:22:48 +00001483 // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional
1484 // property to use here. It may've been intended to model "is non-external
1485 // type" but misses cases of non-function-local but non-external classes such
1486 // as those in anonymous namespaces as well as the reverse - external types
1487 // that are function local, such as those in (non-local) inline functions.
Guy Benyei11169dd2012-12-18 14:30:41 +00001488 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1489 MethodLinkageName = CGM.getMangledName(Method);
1490
1491 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001492 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001493 unsigned MethodLine = 0;
1494 if (!Method->isImplicit()) {
1495 MethodDefUnit = getOrCreateFile(Method->getLocation());
1496 MethodLine = getLineNumber(Method->getLocation());
1497 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001498
1499 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001500 llvm::DIType *ContainingType = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001501 unsigned VIndex = 0;
Leny Kholodov80c047d2016-09-06 10:48:04 +00001502 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Paul Robinsoncda54212018-11-19 18:29:28 +00001503 llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001504 int ThisAdjustment = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001505
Guy Benyei11169dd2012-12-18 14:30:41 +00001506 if (Method->isVirtual()) {
1507 if (Method->isPure())
Paul Robinsoncda54212018-11-19 18:29:28 +00001508 SPFlags |= llvm::DISubprogram::SPFlagPureVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001509 else
Paul Robinsoncda54212018-11-19 18:29:28 +00001510 SPFlags |= llvm::DISubprogram::SPFlagVirtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001511
Reid Kleckner216d0a12016-06-16 20:08:51 +00001512 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1513 // It doesn't make sense to give a virtual destructor a vtable index,
1514 // since a single destructor has two entries in the vtable.
1515 if (!isa<CXXDestructorDecl>(Method))
1516 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
1517 } else {
1518 // Emit MS ABI vftable information. There is only one entry for the
1519 // deleting dtor.
1520 const auto *DD = dyn_cast<CXXDestructorDecl>(Method);
1521 GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method);
Reid Klecknercbec0262018-04-02 20:00:39 +00001522 MethodVFTableLocation ML =
Reid Kleckner216d0a12016-06-16 20:08:51 +00001523 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1524 VIndex = ML.Index;
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001525
1526 // CodeView only records the vftable offset in the class that introduces
1527 // the virtual method. This is possible because, unlike Itanium, the MS
1528 // C++ ABI does not include all virtual methods from non-primary bases in
1529 // the vtable for the most derived class. For example, if C inherits from
1530 // A and B, C's primary vftable will not include B's virtual methods.
Benjamin Krameracfa3392017-12-17 23:52:45 +00001531 if (Method->size_overridden_methods() == 0)
Reid Klecknerc4871ed2016-06-22 18:34:45 +00001532 Flags |= llvm::DINode::FlagIntroducedVirtual;
1533
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001534 // The 'this' adjustment accounts for both the virtual and non-virtual
1535 // portions of the adjustment. Presumably the debugger only uses it when
1536 // it knows the dynamic type of an object.
1537 ThisAdjustment = CGM.getCXXABI()
1538 .getVirtualFunctionPrologueThisAdjustment(GD)
1539 .getQuantity();
Reid Kleckner216d0a12016-06-16 20:08:51 +00001540 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001541 ContainingType = RecordTy;
1542 }
1543
Adrian McCarthyd91bf392017-09-13 20:53:55 +00001544 if (Method->isStatic())
1545 Flags |= llvm::DINode::FlagStaticMember;
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001547 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001548 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
David Majnemer58ed0f32016-07-17 00:39:12 +00001549 if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001551 Flags |= llvm::DINode::FlagExplicit;
David Majnemer58ed0f32016-07-17 00:39:12 +00001552 } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001553 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001555 }
1556 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001558 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001559 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001560 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001561 Flags |= llvm::DINode::FlagRValueReference;
Paul Robinsoncda54212018-11-19 18:29:28 +00001562 if (CGM.getLangOpts().Optimize)
1563 SPFlags |= llvm::DISubprogram::SPFlagOptimized;
Guy Benyei11169dd2012-12-18 14:30:41 +00001564
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001565 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1566 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001567 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
Paul Robinsoncda54212018-11-19 18:29:28 +00001568 MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags,
Reid Kleckner0358cbf2016-07-01 02:41:25 +00001569 TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001570
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001571 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
1573 return SP;
1574}
1575
Eric Christophere7b87e52014-10-26 23:40:33 +00001576void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001577 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1578 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001579
1580 // Since we want more than just the individual member decls if we
1581 // have templated functions iterate over every declaration to gather
1582 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001583 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001584 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1585 // If the member is implicit, don't add it to the member list. This avoids
1586 // the member being added to type units by LLVM, while still allowing it
1587 // to be emitted into the type declaration/reference inside the compile
1588 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001589 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001590 // FIXME: Handle Using(Shadow?)Decls here to create
1591 // DW_TAG_imported_declarations inside the class for base decls brought into
1592 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1593 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1594 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001595 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001596 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001597
1598 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1599 continue;
1600
David Blaikiefd580722014-10-06 05:18:55 +00001601 // Reuse the existing member function declaration if it exists.
1602 // It may be associated with the declaration of the type & should be
1603 // reused as we're building the definition.
1604 //
1605 // This situation can arise in the vtable-based debug info reduction where
1606 // implicit members are emitted in a non-vtable TU.
1607 auto MI = SPCache.find(Method->getCanonicalDecl());
1608 EltTys.push_back(MI == SPCache.end()
1609 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001610 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001612}
Guy Benyei11169dd2012-12-18 14:30:41 +00001613
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001614void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001615 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001616 llvm::DIType *RecordTy) {
Bob Haarmandff36732016-10-25 22:19:32 +00001617 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes;
1618 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes,
1619 llvm::DINode::FlagZero);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001620
Bob Haarmandff36732016-10-25 22:19:32 +00001621 // If we are generating CodeView debug info, we also need to emit records for
1622 // indirect virtual base classes.
1623 if (CGM.getCodeGenOpts().EmitCodeView) {
1624 CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes,
1625 llvm::DINode::FlagIndirectVirtualBase);
1626 }
1627}
1628
1629void CGDebugInfo::CollectCXXBasesAux(
1630 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1631 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy,
1632 const CXXRecordDecl::base_class_const_range &Bases,
1633 llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes,
1634 llvm::DINode::DIFlags StartingFlags) {
1635 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1636 for (const auto &BI : Bases) {
David Majnemer58ed0f32016-07-17 00:39:12 +00001637 const auto *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001638 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Bob Haarmandff36732016-10-25 22:19:32 +00001639 if (!SeenTypes.insert(Base).second)
1640 continue;
1641 auto *BaseTy = getOrCreateType(BI.getType(), Unit);
1642 llvm::DINode::DIFlags BFlags = StartingFlags;
1643 uint64_t BaseOffset;
Brock Wyma3db2b102018-05-14 21:21:22 +00001644 uint32_t VBPtrOffset = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001645
Aaron Ballman574705e2014-03-13 15:41:46 +00001646 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001647 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1648 // virtual base offset offset is -ve. The code generator emits dwarf
1649 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001650 BaseOffset = 0 - CGM.getItaniumVTableContext()
1651 .getVirtualBaseOffsetOffset(RD, Base)
1652 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001653 } else {
1654 // In the MS ABI, store the vbtable offset, which is analogous to the
1655 // vbase offset offset in Itanium.
1656 BaseOffset =
1657 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001658 VBPtrOffset = CGM.getContext()
1659 .getASTRecordLayout(RD)
1660 .getVBPtrOffset()
1661 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001662 }
Bob Haarmandff36732016-10-25 22:19:32 +00001663 BFlags |= llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001664 } else
1665 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1666 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1667 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001668
Adrian Prantl21361fb2014-08-29 22:44:27 +00001669 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001670 llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset,
1671 VBPtrOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001672 EltTys.push_back(DTy);
1673 }
1674}
1675
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001676llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001677CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1678 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001680 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001681 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1682 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001683 StringRef Name;
1684 if (TPList)
1685 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001686 switch (TA.getKind()) {
1687 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001688 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001689 TemplateParams.push_back(
1690 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001691 } break;
1692 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001694 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1695 TheCU, Name, TTy,
1696 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001697 } break;
1698 case TemplateArgument::Declaration: {
1699 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001700 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001701 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001702 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001703 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001704 // Variable pointer template parameters have a value that is the address
1705 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001706 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001707 V = CGM.GetAddrOfGlobalVar(VD);
1708 // Member function pointers have special support for building them, though
1709 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001710 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001711 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001712 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001713 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001714 // Member data pointers have special handling too to compute the fixed
1715 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001716 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001717 // These five lines (& possibly the above member function pointer
1718 // handling) might be able to be refactored to use similar code in
1719 // CodeGenModule::getMemberPointerConstant
1720 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1721 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001722 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001723 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001724 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001725 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1726 TheCU, Name, TTy,
1727 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001728 } break;
1729 case TemplateArgument::NullPtr: {
1730 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001731 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001732 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001733 // Special case member data pointer null values since they're actually -1
1734 // instead of zero.
David Majnemer58ed0f32016-07-17 00:39:12 +00001735 if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr()))
David Blaikie38079fd2013-05-10 21:53:14 +00001736 // But treat member function pointers as simple zero integers because
1737 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1738 // CodeGen grows handling for values of non-null member function
1739 // pointers then perhaps we could remove this special case and rely on
1740 // EmitNullMemberPointer for member function pointers.
1741 if (MPT->isMemberDataPointer())
1742 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1743 if (!V)
1744 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001745 TemplateParams.push_back(
1746 DBuilder.createTemplateValueParameter(TheCU, Name, TTy, V));
David Blaikie38079fd2013-05-10 21:53:14 +00001747 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001748 case TemplateArgument::Template:
1749 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001750 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001751 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1752 break;
1753 case TemplateArgument::Pack:
1754 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1755 TheCU, Name, nullptr,
1756 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1757 break;
David Majnemer5559d472013-08-24 08:21:10 +00001758 case TemplateArgument::Expression: {
1759 const Expr *E = TA.getAsExpr();
1760 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001761 if (E->isGLValue())
1762 T = CGM.getContext().getLValueReferenceType(T);
John McCallde0fe072017-08-15 21:42:52 +00001763 llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001764 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001765 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001766 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
David Majnemer58ed0f32016-07-17 00:39:12 +00001767 TheCU, Name, TTy, V->stripPointerCasts()));
David Majnemer5559d472013-08-24 08:21:10 +00001768 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001769 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001770 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001771 case TemplateArgument::Null:
1772 llvm_unreachable(
1773 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 }
1775 }
1776 return DBuilder.getOrCreateArray(TemplateParams);
1777}
1778
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001779llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001780CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 if (FD->getTemplatedKind() ==
1783 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001784 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1785 ->getTemplate()
1786 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001787 return CollectTemplateParams(
1788 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001790 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001791}
1792
Matthew Voss20165362018-10-03 18:45:04 +00001793llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL,
1794 llvm::DIFile *Unit) {
1795 if (auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VL)) {
1796 auto T = TS->getSpecializedTemplateOrPartial();
1797 auto TA = TS->getTemplateArgs().asArray();
1798 // Collect parameters for a partial specialization
1799 if (T.is<VarTemplatePartialSpecializationDecl *>()) {
1800 const TemplateParameterList *TList =
1801 T.get<VarTemplatePartialSpecializationDecl *>()
1802 ->getTemplateParameters();
1803 return CollectTemplateParams(TList, TA, Unit);
1804 }
1805
1806 // Collect parameters for an explicit specialization
1807 if (T.is<VarTemplateDecl *>()) {
1808 const TemplateParameterList *TList = T.get<VarTemplateDecl *>()
1809 ->getTemplateParameters();
1810 return CollectTemplateParams(TList, TA, Unit);
1811 }
1812 }
1813 return llvm::DINodeArray();
1814}
1815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001816llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1817 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001818 // Always get the full list of parameters, not just the ones from
1819 // the specialization.
1820 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001821 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001822 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001823 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001824}
1825
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001826llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001827 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 return VTablePtrType;
1829
1830 ASTContext &Context = CGM.getContext();
1831
1832 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001833 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001834 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001835 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001837 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1838 Optional<unsigned> DWARFAddressSpace =
1839 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
1840
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001841 llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType(
1842 SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001843 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1844 return VTablePtrType;
1845}
1846
Guy Benyei11169dd2012-12-18 14:30:41 +00001847StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001848 // Copy the gdb compatible name on the side and use its reference.
1849 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001850}
1851
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001852void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Reid Klecknerdc124992016-08-31 16:11:43 +00001853 SmallVectorImpl<llvm::Metadata *> &EltTys,
1854 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 // If this class is not dynamic then there is not any vtable info to collect.
1856 if (!RD->isDynamicClass())
1857 return;
1858
Reid Kleckner59812422016-08-31 20:35:01 +00001859 // Don't emit any vtable shape or vptr info if this class doesn't have an
1860 // extendable vfptr. This can happen if the class doesn't have virtual
1861 // methods, or in the MS ABI if those virtual methods only come from virtually
1862 // inherited bases.
1863 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1864 if (!RL.hasExtendableVFPtr())
1865 return;
1866
Reid Klecknerdc124992016-08-31 16:11:43 +00001867 // CodeView needs to know how large the vtable of every dynamic class is, so
1868 // emit a special named pointer type into the element list. The vptr type
1869 // points to this type as well.
1870 llvm::DIType *VPtrTy = nullptr;
1871 bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView &&
1872 CGM.getTarget().getCXXABI().isMicrosoft();
1873 if (NeedVTableShape) {
1874 uint64_t PtrWidth =
1875 CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1876 const VTableLayout &VFTLayout =
1877 CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero());
1878 unsigned VSlotCount =
Peter Collingbournee53683f2016-09-08 01:14:39 +00001879 VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData;
Reid Klecknerdc124992016-08-31 16:11:43 +00001880 unsigned VTableWidth = PtrWidth * VSlotCount;
Konstantin Zhuravlyovd1ba16e2017-03-08 23:56:48 +00001881 unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
1882 Optional<unsigned> DWARFAddressSpace =
1883 CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
Reid Klecknerdc124992016-08-31 16:11:43 +00001884
1885 // Create a very wide void* type and insert it directly in the element list.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001886 llvm::DIType *VTableType = DBuilder.createPointerType(
1887 nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type");
Reid Klecknerdc124992016-08-31 16:11:43 +00001888 EltTys.push_back(VTableType);
1889
1890 // The vptr is a pointer to this special vtable type.
1891 VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth);
1892 }
1893
1894 // If there is a primary base then the artificial vptr member lives there.
Reid Klecknerdc124992016-08-31 16:11:43 +00001895 if (RL.getPrimaryBase())
1896 return;
1897
1898 if (!VPtrTy)
1899 VPtrTy = getOrCreateVTablePtrType(Unit);
1900
Guy Benyei11169dd2012-12-18 14:30:41 +00001901 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00001902 llvm::DIType *VPtrMember =
1903 DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
1904 llvm::DINode::FlagArtificial, VPtrTy);
Reid Klecknerdc124992016-08-31 16:11:43 +00001905 EltTys.push_back(VPtrMember);
Guy Benyei11169dd2012-12-18 14:30:41 +00001906}
1907
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001908llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001909 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001910 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001911 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001912 return T;
1913}
1914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001915llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001916 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001917 return getOrCreateStandaloneType(D, Loc);
1918}
1919
1920llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1921 SourceLocation Loc) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001922 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001923 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001924 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001925 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001926
Adrian Prantl73409ce2013-03-11 18:33:46 +00001927 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 return T;
1929}
1930
David Blaikie483a9da2014-05-06 18:35:21 +00001931void CGDebugInfo::completeType(const EnumDecl *ED) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001932 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie483a9da2014-05-06 18:35:21 +00001933 return;
1934 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001935 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001936 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001937 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001938 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001939 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001940 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001941 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001942}
1943
David Blaikieb2e86eb2013-08-15 20:49:17 +00001944void CGDebugInfo::completeType(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00001945 if (DebugKind > codegenoptions::LimitedDebugInfo ||
David Blaikieb2e86eb2013-08-15 20:49:17 +00001946 !CGM.getLangOpts().CPlusPlus)
1947 completeRequiredType(RD);
1948}
1949
David Blaikieb11c8732017-01-30 06:36:08 +00001950/// Return true if the class or any of its methods are marked dllimport.
1951static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) {
1952 if (RD->hasAttr<DLLImportAttr>())
1953 return true;
1954 for (const CXXMethodDecl *MD : RD->methods())
1955 if (MD->hasAttr<DLLImportAttr>())
1956 return true;
1957 return false;
1958}
1959
Adrian Prantla43acdc2017-07-24 23:48:51 +00001960/// Does a type definition exist in an imported clang module?
1961static bool isDefinedInClangModule(const RecordDecl *RD) {
1962 // Only definitions that where imported from an AST file come from a module.
1963 if (!RD || !RD->isFromASTFile())
1964 return false;
1965 // Anonymous entities cannot be addressed. Treat them as not from module.
1966 if (!RD->isExternallyVisible() && RD->getName().empty())
1967 return false;
1968 if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
1969 if (!CXXDecl->isCompleteDefinition())
1970 return false;
Adrian Prantlba6fdc52018-10-24 00:06:02 +00001971 // Check wether RD is a template.
Adrian Prantla43acdc2017-07-24 23:48:51 +00001972 auto TemplateKind = CXXDecl->getTemplateSpecializationKind();
1973 if (TemplateKind != TSK_Undeclared) {
Adrian Prantlba6fdc52018-10-24 00:06:02 +00001974 // Unfortunately getOwningModule() isn't accurate enough to find the
1975 // owning module of a ClassTemplateSpecializationDecl that is inside a
1976 // namespace spanning multiple modules.
1977 bool Explicit = false;
1978 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl))
1979 Explicit = TD->isExplicitInstantiationOrSpecialization();
1980 if (!Explicit && CXXDecl->getEnclosingNamespaceContext())
1981 return false;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001982 // This is a template, check the origin of the first member.
1983 if (CXXDecl->field_begin() == CXXDecl->field_end())
1984 return TemplateKind == TSK_ExplicitInstantiationDeclaration;
1985 if (!CXXDecl->field_begin()->isFromASTFile())
1986 return false;
1987 }
1988 }
1989 return true;
1990}
1991
David Blaikie6943dea2013-08-20 01:28:15 +00001992void CGDebugInfo::completeClassData(const RecordDecl *RD) {
David Blaikieb11c8732017-01-30 06:36:08 +00001993 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1994 if (CXXRD->isDynamicClass() &&
1995 CGM.getVTableLinkage(CXXRD) ==
1996 llvm::GlobalValue::AvailableExternallyLinkage &&
1997 !isClassOrMethodDLLImport(CXXRD))
1998 return;
Adrian Prantla43acdc2017-07-24 23:48:51 +00001999
2000 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
2001 return;
2002
David Blaikieb11c8732017-01-30 06:36:08 +00002003 completeClass(RD);
2004}
2005
2006void CGDebugInfo::completeClass(const RecordDecl *RD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002007 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00002008 return;
David Blaikie6943dea2013-08-20 01:28:15 +00002009 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00002010 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00002011 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002012 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00002013 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002014 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002015 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002016 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00002017}
2018
David Blaikie0e716b42014-03-03 23:48:23 +00002019static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
2020 CXXRecordDecl::method_iterator End) {
David Majnemer58ed0f32016-07-17 00:39:12 +00002021 for (CXXMethodDecl *MD : llvm::make_range(I, End))
2022 if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00002023 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
David Majnemer58ed0f32016-07-17 00:39:12 +00002024 !MD->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00002025 return true;
2026 return false;
2027}
2028
Benjamin Kramer8c305922016-02-02 11:06:51 +00002029static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
2030 bool DebugTypeExtRefs, const RecordDecl *RD,
David Blaikie0e716b42014-03-03 23:48:23 +00002031 const LangOptions &LangOpts) {
Adrian Prantl88d79172016-04-26 21:58:18 +00002032 if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
Adrian Prantl43e00812016-01-19 23:42:53 +00002033 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002034
David Blaikie1ac9c982017-04-11 21:13:37 +00002035 if (auto *ES = RD->getASTContext().getExternalSource())
2036 if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
2037 return true;
2038
Benjamin Kramer8c305922016-02-02 11:06:51 +00002039 if (DebugKind > codegenoptions::LimitedDebugInfo)
David Blaikie0e716b42014-03-03 23:48:23 +00002040 return false;
2041
2042 if (!LangOpts.CPlusPlus)
2043 return false;
2044
2045 if (!RD->isCompleteDefinitionRequired())
2046 return true;
2047
David Majnemer58ed0f32016-07-17 00:39:12 +00002048 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
David Blaikie0e716b42014-03-03 23:48:23 +00002049
2050 if (!CXXDecl)
2051 return false;
2052
Adrian McCarthy99242982016-08-16 22:11:18 +00002053 // Only emit complete debug info for a dynamic class when its vtable is
2054 // emitted. However, Microsoft debuggers don't resolve type information
Reid Klecknerc9404e12016-09-09 16:27:04 +00002055 // across DLL boundaries, so skip this optimization if the class or any of its
2056 // methods are marked dllimport. This isn't a complete solution, since objects
2057 // without any dllimport methods can be used in one DLL and constructed in
2058 // another, but it is the current behavior of LimitedDebugInfo.
Adrian McCarthy99242982016-08-16 22:11:18 +00002059 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() &&
Reid Klecknerc9404e12016-09-09 16:27:04 +00002060 !isClassOrMethodDLLImport(CXXDecl))
David Blaikie0e716b42014-03-03 23:48:23 +00002061 return true;
2062
2063 TemplateSpecializationKind Spec = TSK_Undeclared;
David Majnemer58ed0f32016-07-17 00:39:12 +00002064 if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
David Blaikie0e716b42014-03-03 23:48:23 +00002065 Spec = SD->getSpecializationKind();
2066
2067 if (Spec == TSK_ExplicitInstantiationDeclaration &&
2068 hasExplicitMemberDefinition(CXXDecl->method_begin(),
2069 CXXDecl->method_end()))
2070 return true;
2071
2072 return false;
2073}
2074
Reid Kleckner6c7b1c62016-09-13 00:01:23 +00002075void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
2076 if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
2077 return;
2078
2079 QualType Ty = CGM.getContext().getRecordType(RD);
2080 llvm::DIType *T = getTypeOrNull(Ty);
2081 if (T && T->isForwardDecl())
2082 completeClassData(RD);
2083}
2084
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002085llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002086 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002087 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002088 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
2089 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002090 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002091 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00002092 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00002093 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002094
David Blaikieb2e86eb2013-08-15 20:49:17 +00002095 return CreateTypeDefinition(Ty);
2096}
2097
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002098llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00002099 RecordDecl *RD = Ty->getDecl();
2100
Guy Benyei11169dd2012-12-18 14:30:41 +00002101 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002102 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
2104 // Records and classes and unions can all be recursive. To handle them, we
2105 // first generate a debug descriptor for the struct as a forward declaration.
2106 // Then (if it is a definition) we go through and get debug info for all of
2107 // its members. Finally, we create a descriptor for the complete type (which
2108 // may refer to the forward decl if the struct is recursive) and replace all
2109 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002110 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002111
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002112 const RecordDecl *D = RD->getDefinition();
2113 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 return FwdDecl;
2115
David Majnemer58ed0f32016-07-17 00:39:12 +00002116 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
David Blaikieadfbf992013-08-18 16:55:33 +00002117 CollectContainingType(CXXDecl, FwdDecl);
2118
Guy Benyei11169dd2012-12-18 14:30:41 +00002119 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002120 LexicalBlockStack.emplace_back(&*FwdDecl);
2121 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002122
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002124 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00002125 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00002126
2127 // Note: The split of CXXDecl information here is intentional, the
2128 // gdb tests will depend on a certain ordering at printout. The debug
2129 // information offsets are still correct if we merge them all together
2130 // though.
David Majnemer58ed0f32016-07-17 00:39:12 +00002131 const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 if (CXXDecl) {
2133 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
Reid Klecknerdc124992016-08-31 16:11:43 +00002134 CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 }
2136
Eric Christopher91a31902013-01-16 01:22:32 +00002137 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00002139 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002141
2142 LexicalBlockStack.pop_back();
2143 RegionMap.erase(Ty->getDecl());
2144
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002145 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002146 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002147
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002148 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002149 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002150 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00002151
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002152 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002153 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002154}
2155
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002156llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
2157 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 // Ignore protocols.
2159 return getOrCreateType(Ty->getBaseType(), Unit);
2160}
2161
Manman Rene6be26c2016-09-13 17:25:08 +00002162llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
2163 llvm::DIFile *Unit) {
2164 // Ignore protocols.
2165 SourceLocation Loc = Ty->getDecl()->getLocation();
2166
2167 // Use Typedefs to represent ObjCTypeParamType.
2168 return DBuilder.createTypedef(
2169 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
2170 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
2171 getDeclContextDescriptor(Ty->getDecl()));
2172}
2173
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002174/// \return true if Getter has the default name for the property PD.
2175static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
2176 const ObjCMethodDecl *Getter) {
2177 assert(PD);
2178 if (!Getter)
2179 return true;
2180
2181 assert(Getter->getDeclName().isObjCZeroArgSelector());
2182 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002183 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002184}
2185
2186/// \return true if Setter has the default name for the property PD.
2187static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
2188 const ObjCMethodDecl *Setter) {
2189 assert(PD);
2190 if (!Setter)
2191 return true;
2192
2193 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00002194 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00002195 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00002196}
2197
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002198llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
2199 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 ObjCInterfaceDecl *ID = Ty->getDecl();
2201 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002202 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002203
Adrian Prantl50fd1a82016-04-20 23:59:32 +00002204 // Return a forward declaration if this type was imported from a clang module,
2205 // and this is not the compile unit with the implementation of the type (which
2206 // may contain hidden ivars).
2207 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() &&
2208 !ID->getImplementation())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002209 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
2210 ID->getName(),
2211 getDeclContextDescriptor(ID), Unit, 0);
2212
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002214 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002216 auto RuntimeLang =
2217 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00002218
2219 // If this is just a forward declaration return a special forward-declaration
2220 // debug type since we won't be able to lay out the entire type.
2221 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00002222 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002223 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002224 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00002225 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
2226 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00002227 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002228 return FwdDecl;
2229 }
2230
David Blaikieef8a9512014-05-05 23:23:53 +00002231 return CreateTypeDefinition(Ty, Unit);
2232}
2233
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002234llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00002235CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
2236 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00002237 // Use the Module pointer as the key into the cache. This is a
2238 // nullptr if the "Module" is a PCH, which is safe because we don't
2239 // support chained PCH debug info, so there can only be a single PCH.
2240 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002241 auto ModRef = ModuleCache.find(M);
2242 if (ModRef != ModuleCache.end())
2243 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00002244
2245 // Macro definitions that were defined with "-D" on the command line.
2246 SmallString<128> ConfigMacros;
2247 {
2248 llvm::raw_svector_ostream OS(ConfigMacros);
2249 const auto &PPOpts = CGM.getPreprocessorOpts();
2250 unsigned I = 0;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002251 // Translate the macro definitions back into a command line.
Adrian Prantl2388ead2015-06-30 18:01:05 +00002252 for (auto &M : PPOpts.Macros) {
2253 if (++I > 1)
2254 OS << " ";
2255 const std::string &Macro = M.first;
2256 bool Undef = M.second;
2257 OS << "\"-" << (Undef ? 'U' : 'D');
2258 for (char c : Macro)
2259 switch (c) {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002260 case '\\':
2261 OS << "\\\\";
2262 break;
2263 case '"':
2264 OS << "\\\"";
2265 break;
2266 default:
2267 OS << c;
Adrian Prantl2388ead2015-06-30 18:01:05 +00002268 }
2269 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002270 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002271 }
Adrian Prantl66689202015-09-18 23:01:45 +00002272
Adrian Prantl835e6632015-09-24 16:10:10 +00002273 bool IsRootModule = M ? !M->Parent : true;
2274 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantlc96da8f2016-01-22 17:43:43 +00002275 // PCH files don't have a signature field in the control block,
2276 // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002277 // We use the lower 64 bits for debug info.
2278 uint64_t Signature =
2279 Mod.getSignature()
2280 ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0]
2281 : ~1ULL;
Adrian Prantl66689202015-09-18 23:01:45 +00002282 llvm::DIBuilder DIB(CGM.getModule());
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002283 DIB.createCompileUnit(TheCU->getSourceLanguage(),
Scott Lindera2fbcef2018-02-26 17:32:31 +00002284 // TODO: Support "Source" from external AST providers?
Amjad Aboudfa9a17e2016-12-14 20:24:40 +00002285 DIB.createFile(Mod.getModuleName(), Mod.getPath()),
2286 TheCU->getProducer(), true, StringRef(), 0,
2287 Mod.getASTFile(), llvm::DICompileUnit::FullDebug,
2288 Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00002289 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00002290 }
Adrian Prantl835e6632015-09-24 16:10:10 +00002291 llvm::DIModule *Parent =
2292 IsRootModule ? nullptr
2293 : getOrCreateModuleRef(
2294 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
2295 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00002296 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00002297 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
2298 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00002299 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00002300 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00002301}
2302
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002303llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
2304 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00002305 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002306 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00002307 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00002308 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00002309
2310 // Bit size, align and offset of the type.
2311 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002312 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002313
Leny Kholodov80c047d2016-09-06 10:48:04 +00002314 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002315 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
Adrian Prantlfd696112015-10-01 00:48:51 +00002318 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002319 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00002320 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
2321 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00002322
David Blaikieef8a9512014-05-05 23:23:53 +00002323 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002324 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002325
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002326 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002327 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002328 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002329
2330 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002331 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002332
2333 ObjCInterfaceDecl *SClass = ID->getSuperClass();
2334 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002335 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00002336 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002337 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002338 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002339
Brock Wyma3db2b102018-05-14 21:21:22 +00002340 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00002341 llvm::DINode::FlagZero);
Guy Benyei11169dd2012-12-18 14:30:41 +00002342 EltTys.push_back(InhTag);
2343 }
2344
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002345 // Create entries for all of the properties.
Nico Weber7123bca2015-12-04 19:14:14 +00002346 auto AddProperty = [&](const ObjCPropertyDecl *PD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002348 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 unsigned PLine = getLineNumber(Loc);
2350 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2351 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002352 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
2353 PD->getName(), PUnit, PLine,
2354 hasDefaultGetterName(PD, Getter) ? ""
2355 : getSelectorName(PD->getGetterName()),
2356 hasDefaultSetterName(PD, Setter) ? ""
2357 : getSelectorName(PD->getSetterName()),
2358 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002359 EltTys.push_back(PropertyNode);
Nico Weber7123bca2015-12-04 19:14:14 +00002360 };
2361 {
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002362 llvm::SmallPtrSet<const IdentifierInfo *, 16> PropertySet;
Nico Weberde059e12015-12-04 19:35:45 +00002363 for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
Manman Renefe1bac2016-01-27 20:00:32 +00002364 for (auto *PD : ClassExt->properties()) {
Nico Weberde059e12015-12-04 19:35:45 +00002365 PropertySet.insert(PD->getIdentifier());
2366 AddProperty(PD);
2367 }
Manman Renefe1bac2016-01-27 20:00:32 +00002368 for (const auto *PD : ID->properties()) {
Nico Weber7123bca2015-12-04 19:14:14 +00002369 // Don't emit duplicate metadata for properties that were already in a
2370 // class extension.
2371 if (!PropertySet.insert(PD->getIdentifier()).second)
2372 continue;
2373 AddProperty(PD);
2374 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002375 }
2376
2377 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
2378 unsigned FieldNo = 0;
2379 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
2380 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00002382 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002383 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002384
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 StringRef FieldName = Field->getName();
2386
2387 // Ignore unnamed fields.
2388 if (FieldName.empty())
2389 continue;
2390
2391 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002393 unsigned FieldLine = getLineNumber(Field->getLocation());
2394 QualType FType = Field->getType();
2395 uint64_t FieldSize = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002396 uint32_t FieldAlign = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002397
2398 if (!FType->isIncompleteArrayType()) {
2399
2400 // Bit size, align and offset of the type.
2401 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00002402 ? Field->getBitWidthValue(CGM.getContext())
2403 : CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00002404 FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002405 }
2406
2407 uint64_t FieldOffset;
2408 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2409 // We don't know the runtime offset of an ivar if we're using the
2410 // non-fragile ABI. For bitfields, use the bit offset into the first
2411 // byte of storage of the bitfield. For other fields, use zero.
2412 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002413 FieldOffset =
2414 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00002415 FieldOffset %= CGM.getContext().getCharWidth();
2416 } else {
2417 FieldOffset = 0;
2418 }
2419 } else {
2420 FieldOffset = RL.getFieldOffset(FieldNo);
2421 }
2422
Leny Kholodov80c047d2016-09-06 10:48:04 +00002423 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00002424 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002425 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00002426 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002427 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00002428 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002429 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430
Craig Topper8a13c412014-05-21 05:09:00 +00002431 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002432 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00002433 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00002434 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002435 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00002436 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002437 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00002438 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002439 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
2440 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00002441 PropertyNode = DBuilder.createObjCProperty(
2442 PD->getName(), PUnit, PLine,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002443 hasDefaultGetterName(PD, Getter)
2444 ? ""
2445 : getSelectorName(PD->getGetterName()),
2446 hasDefaultSetterName(PD, Setter)
2447 ? ""
2448 : getSelectorName(PD->getSetterName()),
Eric Christophere7b87e52014-10-26 23:40:33 +00002449 PD->getPropertyAttributes(),
2450 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00002451 }
2452 }
2453 }
Eric Christophere7b87e52014-10-26 23:40:33 +00002454 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
2455 FieldSize, FieldAlign, FieldOffset, Flags,
2456 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00002457 EltTys.push_back(FieldTy);
2458 }
2459
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002460 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002461 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00002462
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00002464 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002465}
2466
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
2468 llvm::DIFile *Unit) {
2469 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 int64_t Count = Ty->getNumElements();
Guy Benyei11169dd2012-12-18 14:30:41 +00002471
Sander de Smalen891af03a2018-02-03 13:55:59 +00002472 llvm::Metadata *Subscript;
2473 QualType QTy(Ty, 0);
2474 auto SizeExpr = SizeExprCache.find(QTy);
2475 if (SizeExpr != SizeExprCache.end())
2476 Subscript = DBuilder.getOrCreateSubrange(0, SizeExpr->getSecond());
2477 else
2478 Subscript = DBuilder.getOrCreateSubrange(0, Count ? Count : -1);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002479 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00002480
2481 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002482 auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002483
2484 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
2485}
2486
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002487llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 uint64_t Size;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002489 uint32_t Align;
Guy Benyei11169dd2012-12-18 14:30:41 +00002490
2491 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
David Majnemer58ed0f32016-07-17 00:39:12 +00002492 if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 Size = 0;
Victor Leschuka7ece032016-10-20 00:13:19 +00002494 Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
2495 CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002496 } else if (Ty->isIncompleteArrayType()) {
2497 Size = 0;
2498 if (Ty->getElementType()->isIncompleteType())
2499 Align = 0;
2500 else
Victor Leschuka7ece032016-10-20 00:13:19 +00002501 Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext());
David Blaikief03b2e82013-05-09 20:48:12 +00002502 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 Size = 0;
2504 Align = 0;
2505 } else {
2506 // Size and align of the whole array, not the element type.
2507 Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002508 Align = getTypeAlignIfRequired(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002509 }
2510
2511 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
2512 // interior arrays, do we care? Why aren't nested arrays represented the
2513 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002514 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 QualType EltTy(Ty, 0);
2516 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
2517 // If the number of elements is known, then count is that number. Otherwise,
2518 // it's -1. This allows us to represent a subrange with an array of 0
2519 // elements, like this:
2520 //
2521 // struct foo {
2522 // int x[0];
2523 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00002524 int64_t Count = -1; // Count == -1 is an unbounded array.
David Majnemer58ed0f32016-07-17 00:39:12 +00002525 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 Count = CAT->getSize().getZExtValue();
David Blaikie87173f12016-08-22 17:49:56 +00002527 else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
Eli Friedman01d6b962016-10-19 22:16:32 +00002528 if (Expr *Size = VAT->getSizeExpr()) {
Fangrui Song407659a2018-11-30 23:41:18 +00002529 Expr::EvalResult Result;
2530 if (Size->EvaluateAsInt(Result, CGM.getContext()))
2531 Count = Result.Val.getInt().getExtValue();
Eli Friedman01d6b962016-10-19 22:16:32 +00002532 }
David Blaikie87173f12016-08-22 17:49:56 +00002533 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002534
Sander de Smalen891af03a2018-02-03 13:55:59 +00002535 auto SizeNode = SizeExprCache.find(EltTy);
2536 if (SizeNode != SizeExprCache.end())
2537 Subscripts.push_back(
2538 DBuilder.getOrCreateSubrange(0, SizeNode->getSecond()));
2539 else
2540 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
Guy Benyei11169dd2012-12-18 14:30:41 +00002541 EltTy = Ty->getElementType();
2542 }
2543
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002544 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002545
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002546 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
2547 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002548}
2549
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
2551 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002552 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
2553 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002554}
2555
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002556llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
2557 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002558 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
2559 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002560}
2561
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002562llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
2563 llvm::DIFile *U) {
Leny Kholodov80c047d2016-09-06 10:48:04 +00002564 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Reid Klecknerfb727182016-06-17 22:27:59 +00002565 uint64_t Size = 0;
2566
2567 if (!Ty->isIncompleteType()) {
2568 Size = CGM.getContext().getTypeSize(Ty);
2569
2570 // Set the MS inheritance model. There is no flag for the unspecified model.
2571 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
2572 switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2573 case MSInheritanceAttr::Keyword_single_inheritance:
2574 Flags |= llvm::DINode::FlagSingleInheritance;
2575 break;
2576 case MSInheritanceAttr::Keyword_multiple_inheritance:
2577 Flags |= llvm::DINode::FlagMultipleInheritance;
2578 break;
2579 case MSInheritanceAttr::Keyword_virtual_inheritance:
2580 Flags |= llvm::DINode::FlagVirtualInheritance;
2581 break;
2582 case MSInheritanceAttr::Keyword_unspecified_inheritance:
2583 break;
2584 }
2585 }
2586 }
2587
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002588 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00002589 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00002590 return DBuilder.createMemberPointerType(
Reid Klecknerfb727182016-06-17 22:27:59 +00002591 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
2592 Flags);
Adrian Prantl0866acd2013-12-19 01:38:47 +00002593
2594 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00002595 Ty->getPointeeType()->getAs<FunctionProtoType>();
2596 return DBuilder.createMemberPointerType(
2597 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
2598 Ty->getClass(), FPT->getTypeQuals())),
2599 FPT, U),
Reid Klecknerfb727182016-06-17 22:27:59 +00002600 ClassType, Size, /*Align=*/0, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002601}
2602
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002603llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Victor Leschuk0df190372016-10-31 19:09:47 +00002604 auto *FromTy = getOrCreateType(Ty->getValueType(), U);
2605 return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002606}
2607
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002608llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00002609 return getOrCreateType(Ty->getElementType(), U);
2610}
2611
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002612llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002613 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002614
Guy Benyei11169dd2012-12-18 14:30:41 +00002615 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002616 uint32_t Align = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 if (!ED->getTypeForDecl()->isIncompleteType()) {
2618 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002619 Align = getDeclAlignIfRequired(ED, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 }
2621
Brock Wyma8557ec52018-05-22 12:41:19 +00002622 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002623
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002624 bool isImportedFromModule =
2625 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2626
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 // If this is just a forward declaration, construct an appropriately
2628 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002629 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl45946062016-02-23 19:30:08 +00002630 // Note that it is possible for enums to be created as part of
2631 // their own declcontext. In this case a FwdDecl will be created
2632 // twice. This doesn't cause a problem because both FwdDecls are
2633 // entered into the ReplaceMap: finalize() will replace the first
2634 // FwdDecl with the second and then replace the second with
2635 // complete type.
Amjad Abouddc4531e2016-04-30 01:44:38 +00002636 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002637 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Adrian Prantlff9d83c2016-02-08 17:03:28 +00002638 llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
2639 llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
Adrian Prantla40030f2016-02-06 01:59:09 +00002640
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 unsigned Line = getLineNumber(ED->getLocation());
2642 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002643 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
Adrian Prantl45946062016-02-23 19:30:08 +00002644 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Brock Wyma8557ec52018-05-22 12:41:19 +00002645 0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
Adrian Prantla40030f2016-02-06 01:59:09 +00002646
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002647 ReplaceMap.emplace_back(
2648 std::piecewise_construct, std::make_tuple(Ty),
2649 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002650 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002651 }
2652
David Blaikie483a9da2014-05-06 18:35:21 +00002653 return CreateTypeDefinition(Ty);
2654}
2655
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002656llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002657 const EnumDecl *ED = Ty->getDecl();
2658 uint64_t Size = 0;
Victor Leschuk802e4a52016-10-19 22:11:07 +00002659 uint32_t Align = 0;
David Blaikie483a9da2014-05-06 18:35:21 +00002660 if (!ED->getTypeForDecl()->isIncompleteType()) {
2661 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
Victor Leschuka7ece032016-10-20 00:13:19 +00002662 Align = getDeclAlignIfRequired(ED, CGM.getContext());
David Blaikie483a9da2014-05-06 18:35:21 +00002663 }
2664
Brock Wyma8557ec52018-05-22 12:41:19 +00002665 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
David Blaikie483a9da2014-05-06 18:35:21 +00002666
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002667 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002668 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 ED = ED->getDefinition();
Momchil Velikov25f6be52018-02-12 16:12:52 +00002670 bool IsSigned = ED->getIntegerType()->isSignedIntegerType();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002671 for (const auto *Enum : ED->enumerators()) {
Momchil Velikov25f6be52018-02-12 16:12:52 +00002672 const auto &InitVal = Enum->getInitVal();
2673 auto Value = IsSigned ? InitVal.getSExtValue() : InitVal.getZExtValue();
2674 Enumerators.push_back(
2675 DBuilder.createEnumerator(Enum->getName(), Value, !IsSigned));
Guy Benyei11169dd2012-12-18 14:30:41 +00002676 }
2677
2678 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002679 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002680
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002681 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 unsigned Line = getLineNumber(ED->getLocation());
Amjad Abouddc4531e2016-04-30 01:44:38 +00002683 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Momchil Velikov25f6be52018-02-12 16:12:52 +00002684 llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002685 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2686 Line, Size, Align, EltArray, ClassTy,
Brock Wyma8557ec52018-05-22 12:41:19 +00002687 Identifier, ED->isFixed());
Guy Benyei11169dd2012-12-18 14:30:41 +00002688}
2689
Amjad Aboud546bc112017-02-09 22:07:24 +00002690llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
2691 unsigned MType, SourceLocation LineLoc,
2692 StringRef Name, StringRef Value) {
2693 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2694 return DBuilder.createMacro(Parent, Line, MType, Name, Value);
2695}
2696
2697llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
2698 SourceLocation LineLoc,
2699 SourceLocation FileLoc) {
2700 llvm::DIFile *FName = getOrCreateFile(FileLoc);
2701 unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
2702 return DBuilder.createTempMacroFile(Parent, Line, FName);
2703}
2704
David Blaikie05491062013-01-21 04:37:12 +00002705static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2706 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002708 Qualifiers InnerQuals = T.getLocalQualifiers();
2709 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2710 // that is already there.
2711 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2712 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 QualType LastT = T;
2714 switch (T->getTypeClass()) {
2715 default:
David Blaikie05491062013-01-21 04:37:12 +00002716 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002717 case Type::TemplateSpecialization: {
2718 const auto *Spec = cast<TemplateSpecializationType>(T);
2719 if (Spec->isTypeAlias())
2720 return C.getQualifiedType(T.getTypePtr(), Quals);
2721 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002722 break;
2723 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002724 case Type::TypeOfExpr:
2725 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2726 break;
2727 case Type::TypeOf:
2728 T = cast<TypeOfType>(T)->getUnderlyingType();
2729 break;
2730 case Type::Decltype:
2731 T = cast<DecltypeType>(T)->getUnderlyingType();
2732 break;
2733 case Type::UnaryTransform:
2734 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2735 break;
2736 case Type::Attributed:
2737 T = cast<AttributedType>(T)->getEquivalentType();
2738 break;
2739 case Type::Elaborated:
2740 T = cast<ElaboratedType>(T)->getNamedType();
2741 break;
2742 case Type::Paren:
2743 T = cast<ParenType>(T)->getInnerType();
2744 break;
David Blaikie05491062013-01-21 04:37:12 +00002745 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 break;
Richard Smitha0abc422017-02-22 00:13:14 +00002748 case Type::Auto:
2749 case Type::DeducedTemplateSpecialization: {
2750 QualType DT = cast<DeducedType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002751 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002752 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002753 break;
2754 }
Jordan Rose303e2f12016-11-10 23:28:17 +00002755 case Type::Adjusted:
2756 case Type::Decayed:
2757 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
2758 T = cast<AdjustedType>(T)->getAdjustedType();
2759 break;
2760 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002761
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002763 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 } while (true);
2765}
2766
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002767llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002768
2769 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002770 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002771
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002772 auto It = TypeCache.find(Ty.getAsOpaquePtr());
2773 if (It != TypeCache.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 // Verify that the debug info still exists.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002775 if (llvm::Metadata *V = It->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002776 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 }
2778
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002779 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002780}
2781
David Blaikie0e716b42014-03-03 23:48:23 +00002782void CGDebugInfo::completeTemplateDefinition(
2783 const ClassTemplateSpecializationDecl &SD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00002784 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie0856f662014-03-04 22:01:08 +00002785 return;
David Blaikie1ac9c982017-04-11 21:13:37 +00002786 completeUnusedClass(SD);
2787}
David Blaikie0856f662014-03-04 22:01:08 +00002788
David Blaikie1ac9c982017-04-11 21:13:37 +00002789void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
2790 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
2791 return;
2792
2793 completeClassData(&D);
David Blaikie0e716b42014-03-03 23:48:23 +00002794 // In case this type has no member function definitions being emitted, ensure
2795 // it is retained
David Blaikie1ac9c982017-04-11 21:13:37 +00002796 RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
David Blaikie0e716b42014-03-03 23:48:23 +00002797}
2798
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002799llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002801 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002802
2803 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002804 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002805
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002806 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 return T;
2808
Adrian Prantlca844182015-09-11 17:23:03 +00002809 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00002810 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002811
2812 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002813 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002814
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 return Res;
2816}
2817
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002818llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002819 // A forward declaration inside a module header does not belong to the module.
2820 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2821 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002822 if (DebugTypeExtRefs && D->isFromASTFile()) {
2823 // Record a reference to an imported clang module or precompiled header.
2824 auto *Reader = CGM.getContext().getExternalSource();
2825 auto Idx = D->getOwningModuleID();
2826 auto Info = Reader->getSourceDescriptor(Idx);
2827 if (Info)
2828 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2829 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002830 // We are building a clang module or a precompiled header.
2831 //
2832 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2833 // and it wouldn't be necessary to specify the parent scope
2834 // because the type is already unique by definition (it would look
2835 // like the output of -fno-standalone-debug). On the other hand,
2836 // the parent scope helps a consumer to quickly locate the object
2837 // file where the type's definition is located, so it might be
2838 // best to make this behavior a command line or debugger tuning
2839 // option.
Richard Smith54f04402017-05-18 02:29:20 +00002840 if (Module *M = D->getOwningModule()) {
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002841 // This is a (sub-)module.
Adrian Prantl9402cef2015-09-20 16:51:35 +00002842 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2843 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
Adrian Prantlaa5d08d2016-01-22 21:14:41 +00002844 } else {
2845 // This the precompiled header being built.
2846 return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
Adrian Prantl9402cef2015-09-20 16:51:35 +00002847 }
2848 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002849
Adrian Prantl9402cef2015-09-20 16:51:35 +00002850 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002851}
2852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002853llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 // Handle qualifiers, which recursively handles what they refer to.
2855 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002856 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002857
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 // Work out details of type.
2859 switch (Ty->getTypeClass()) {
2860#define TYPE(Class, Base)
2861#define ABSTRACT_TYPE(Class, Base)
2862#define NON_CANONICAL_TYPE(Class, Base)
2863#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2864#include "clang/AST/TypeNodes.def"
2865 llvm_unreachable("Dependent types cannot show up in debug information");
2866
2867 case Type::ExtVector:
2868 case Type::Vector:
2869 return CreateType(cast<VectorType>(Ty), Unit);
2870 case Type::ObjCObjectPointer:
2871 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2872 case Type::ObjCObject:
2873 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Manman Rene6be26c2016-09-13 17:25:08 +00002874 case Type::ObjCTypeParam:
2875 return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 case Type::ObjCInterface:
2877 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2878 case Type::Builtin:
2879 return CreateType(cast<BuiltinType>(Ty));
2880 case Type::Complex:
2881 return CreateType(cast<ComplexType>(Ty));
2882 case Type::Pointer:
2883 return CreateType(cast<PointerType>(Ty), Unit);
2884 case Type::BlockPointer:
2885 return CreateType(cast<BlockPointerType>(Ty), Unit);
2886 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002887 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002889 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002890 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002891 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 case Type::FunctionProto:
2893 case Type::FunctionNoProto:
2894 return CreateType(cast<FunctionType>(Ty), Unit);
2895 case Type::ConstantArray:
2896 case Type::VariableArray:
2897 case Type::IncompleteArray:
2898 return CreateType(cast<ArrayType>(Ty), Unit);
2899
2900 case Type::LValueReference:
2901 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2902 case Type::RValueReference:
2903 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2904
2905 case Type::MemberPointer:
2906 return CreateType(cast<MemberPointerType>(Ty), Unit);
2907
2908 case Type::Atomic:
2909 return CreateType(cast<AtomicType>(Ty), Unit);
2910
Xiuli Pan9c14e282016-01-09 12:53:17 +00002911 case Type::Pipe:
2912 return CreateType(cast<PipeType>(Ty), Unit);
2913
Guy Benyei11169dd2012-12-18 14:30:41 +00002914 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002915 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2916
David Blaikie42edade2014-11-11 20:44:45 +00002917 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002918 case Type::Attributed:
Jordan Rose303e2f12016-11-10 23:28:17 +00002919 case Type::Adjusted:
2920 case Type::Decayed:
Richard Smith600b5262017-01-26 20:40:47 +00002921 case Type::DeducedTemplateSpecialization:
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 case Type::Elaborated:
2923 case Type::Paren:
2924 case Type::SubstTemplateTypeParm:
2925 case Type::TypeOfExpr:
2926 case Type::TypeOf:
2927 case Type::Decltype:
2928 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002929 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002930 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002932
David Blaikie42edade2014-11-11 20:44:45 +00002933 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002934}
2935
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002936llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2937 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002938 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002939
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002940 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002941
2942 // We may have cached a forward decl when we could have created
2943 // a non-forward decl. Go ahead and create a non-forward decl
2944 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002945 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002946 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002947
2948 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002949 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002950
2951 // Propagate members from the declaration to the definition
2952 // CreateType(const RecordType*) will overwrite this with the members in the
2953 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002954 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002955
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002957 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 return Res;
2959}
2960
2961// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002962llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002963 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002964
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002966 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002967 unsigned Line = getLineNumber(RD->getLocation());
2968 StringRef RDName = getClassName(RD);
2969
Amjad Abouddc4531e2016-04-30 01:44:38 +00002970 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002971
David Blaikied2785892013-08-18 17:36:19 +00002972 // If we ended up creating the type during the context chain construction,
2973 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002974 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002975 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002976 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002977 return T;
David Blaikied2785892013-08-18 17:36:19 +00002978
Adrian Prantl381e7552014-02-04 21:29:50 +00002979 // If this is just a forward or incomplete declaration, construct an
2980 // appropriately marked node and just return it.
2981 const RecordDecl *D = RD->getDefinition();
2982 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002983 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002984
2985 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Victor Leschuka7ece032016-10-20 00:13:19 +00002986 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002987
Brock Wyma8557ec52018-05-22 12:41:19 +00002988 SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
Manman Rene0064d82013-08-29 23:19:58 +00002989
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00002990 // Explicitly record the calling convention for C++ records.
2991 auto Flags = llvm::DINode::FlagZero;
2992 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2993 if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect)
2994 Flags |= llvm::DINode::FlagTypePassByReference;
2995 else
2996 Flags |= llvm::DINode::FlagTypePassByValue;
Aaron Smith044326c2018-07-23 20:49:07 +00002997
2998 // Record if a C++ record is trivial type.
2999 if (CXXRD->isTrivial())
3000 Flags |= llvm::DINode::FlagTrivial;
Adrian Prantl6c5f03a2018-01-05 01:13:52 +00003001 }
3002
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003003 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Leny Kholodov80c047d2016-09-06 10:48:04 +00003004 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
Brock Wyma8557ec52018-05-22 12:41:19 +00003005 Flags, Identifier);
Guy Benyei11169dd2012-12-18 14:30:41 +00003006
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003007 // Elements of composite types usually have back to the type, creating
3008 // uniquing cycles. Distinct nodes are more efficient.
3009 switch (RealDecl->getTag()) {
3010 default:
3011 llvm_unreachable("invalid composite type tag");
3012
3013 case llvm::dwarf::DW_TAG_array_type:
3014 case llvm::dwarf::DW_TAG_enumeration_type:
3015 // Array elements and most enumeration elements don't have back references,
3016 // so they don't tend to be involved in uniquing cycles and there is some
3017 // chance of merging them when linking together two modules. Only make
3018 // them distinct if they are ODR-uniqued.
Brock Wyma8557ec52018-05-22 12:41:19 +00003019 if (Identifier.empty())
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003020 break;
Galina Kistanova0872d6c2017-06-03 06:30:46 +00003021 LLVM_FALLTHROUGH;
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003022
3023 case llvm::dwarf::DW_TAG_structure_type:
3024 case llvm::dwarf::DW_TAG_union_type:
3025 case llvm::dwarf::DW_TAG_class_type:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003026 // Immediately resolve to a distinct node.
Duncan P. N. Exon Smithf9521b02016-04-17 07:45:08 +00003027 RealDecl =
3028 llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl));
3029 break;
3030 }
3031
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003032 RegionMap[Ty->getDecl()].reset(RealDecl);
3033 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003034
David Majnemer58ed0f32016-07-17 00:39:12 +00003035 if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003036 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00003037 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00003038 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00003039}
3040
David Blaikieadfbf992013-08-18 16:55:33 +00003041void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003042 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00003043 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003044 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00003045 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3046 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00003047 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00003048 while (1) {
3049 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
3050 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
3051 if (PBT && !BRL.isPrimaryBaseVirtual())
3052 PBase = PBT;
3053 else
3054 break;
3055 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003056 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00003057 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
3058 getOrCreateFile(RD->getLocation())));
3059 } else if (RD->isDynamicClass())
3060 ContainingType = RealDecl;
3061
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00003062 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00003063}
3064
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003065llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003066 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003067 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003068 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
Victor Leschuka7ece032016-10-20 00:13:19 +00003069 auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
Leny Kholodovdf050fd2016-09-06 17:06:14 +00003070 llvm::DIType *Ty =
3071 DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
3072 *Offset, llvm::DINode::FlagZero, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 *Offset += FieldSize;
3074 return Ty;
3075}
3076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003077void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00003078 StringRef &Name,
3079 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003080 llvm::DIScope *&FDContext,
3081 llvm::DINodeArray &TParamsArray,
Leny Kholodov80c047d2016-09-06 10:48:04 +00003082 llvm::DINode::DIFlags &Flags) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003083 const auto *FD = cast<FunctionDecl>(GD.getDecl());
Frederic Riss9db79f12014-11-18 03:40:46 +00003084 Name = getFunctionName(FD);
3085 // Use mangled name as linkage name for C/C++ functions.
3086 if (FD->hasPrototype()) {
3087 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003088 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00003089 }
3090 // No need to replicate the linkage name if it isn't different from the
3091 // subprogram name, no need to have it at all unless coverage is enabled or
Dehao Chenb3a70de2017-01-19 00:44:21 +00003092 // debug is set to more than just line tables or extra debug info is needed.
Benjamin Kramer8c305922016-02-02 11:06:51 +00003093 if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs &&
3094 !CGM.getCodeGenOpts().EmitGcovNotes &&
Dehao Chenb3a70de2017-01-19 00:44:21 +00003095 !CGM.getCodeGenOpts().DebugInfoForProfiling &&
Benjamin Kramer8c305922016-02-02 11:06:51 +00003096 DebugKind <= codegenoptions::DebugLineTablesOnly))
Frederic Riss9db79f12014-11-18 03:40:46 +00003097 LinkageName = StringRef();
3098
Benjamin Kramer8c305922016-02-02 11:06:51 +00003099 if (DebugKind >= codegenoptions::LimitedDebugInfo) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003100 if (const NamespaceDecl *NSDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003101 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Adrian Prantlddb8e062017-05-12 16:23:53 +00003102 FDContext = getOrCreateNamespace(NSDecl);
Frederic Riss9db79f12014-11-18 03:40:46 +00003103 else if (const RecordDecl *RDecl =
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003104 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003105 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
3106 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
3107 }
Adrian Prantlfd5ac8a2016-08-17 16:20:32 +00003108 // Check if it is a noreturn-marked function
3109 if (FD->isNoReturn())
3110 Flags |= llvm::DINode::FlagNoReturn;
Frederic Riss9db79f12014-11-18 03:40:46 +00003111 // Collect template parameters.
3112 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
3113 }
3114}
3115
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003116void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00003117 unsigned &LineNo, QualType &T,
3118 StringRef &Name, StringRef &LinkageName,
Matthew Voss20165362018-10-03 18:45:04 +00003119 llvm::MDTuple *&TemplateParameters,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003120 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00003121 Unit = getOrCreateFile(VD->getLocation());
3122 LineNo = getLineNumber(VD->getLocation());
3123
3124 setLocation(VD->getLocation());
3125
3126 T = VD->getType();
3127 if (T->isIncompleteArrayType()) {
3128 // CodeGen turns int[] into int[1] so we'll do the same here.
3129 llvm::APInt ConstVal(32, 1);
3130 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
3131
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003132 T = CGM.getContext().getConstantArrayType(ET, ConstVal, ArrayType::Normal,
3133 0);
Frederic Riss9db79f12014-11-18 03:40:46 +00003134 }
3135
3136 Name = VD->getName();
3137 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
3138 !isa<ObjCMethodDecl>(VD->getDeclContext()))
3139 LinkageName = CGM.getMangledName(VD);
3140 if (LinkageName == Name)
3141 LinkageName = StringRef();
3142
Matthew Voss20165362018-10-03 18:45:04 +00003143 if (isa<VarTemplateSpecializationDecl>(VD)) {
3144 llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VD, &*Unit);
3145 TemplateParameters = parameterNodes.get();
3146 } else {
3147 TemplateParameters = nullptr;
3148 }
3149
Frederic Riss9db79f12014-11-18 03:40:46 +00003150 // Since we emit declarations (DW_AT_members) for static members, place the
3151 // definition of those static members in the namespace they were declared in
3152 // in the source code (the lexical decl context).
3153 // FIXME: Generalize this for even non-member global variables where the
3154 // declaration and definition may have different lexical decl contexts, once
3155 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003156 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
3157 : VD->getDeclContext();
3158 // When a record type contains an in-line initialization of a static data
3159 // member, and the record type is marked as __declspec(dllexport), an implicit
3160 // definition of the member will be created in the record context. DWARF
3161 // doesn't seem to have a nice way to describe this in a form that consumers
3162 // are likely to understand, so fake the "normal" situation of a definition
3163 // outside the class by putting it in the global scope.
3164 if (DC->isRecord())
3165 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003166
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003167 llvm::DIScope *Mod = getParentModuleOrNull(VD);
3168 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00003169}
3170
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003171llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
3172 bool Stub) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003173 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00003174 StringRef Name, LinkageName;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003175 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Paul Robinsoncda54212018-11-19 18:29:28 +00003176 llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003177 SourceLocation Loc = GD.getDecl()->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003178 llvm::DIFile *Unit = getOrCreateFile(Loc);
3179 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003180 unsigned Line = getLineNumber(Loc);
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003181 collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray,
3182 Flags);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003183 auto *FD = dyn_cast<FunctionDecl>(GD.getDecl());
3184
Frederic Rissd253ed62014-11-18 03:40:51 +00003185 // Build function type.
3186 SmallVector<QualType, 16> ArgTypes;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003187 if (FD)
3188 for (const ParmVarDecl *Parm : FD->parameters())
3189 ArgTypes.push_back(Parm->getType());
Reid Klecknerf00f8032016-06-08 20:41:54 +00003190 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
3191 QualType FnType = CGM.getContext().getFunctionType(
3192 FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC));
Paul Robinsoncda54212018-11-19 18:29:28 +00003193 if (!FD->isExternallyVisible())
3194 SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
3195 if (CGM.getLangOpts().Optimize)
3196 SPFlags |= llvm::DISubprogram::SPFlagOptimized;
3197
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003198 if (Stub) {
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003199 Flags |= getCallSiteRelatedAttrs();
Paul Robinsoncda54212018-11-19 18:29:28 +00003200 SPFlags |= llvm::DISubprogram::SPFlagDefinition;
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003201 return DBuilder.createFunction(
3202 DContext, Name, LinkageName, Unit, Line,
Paul Robinsoncda54212018-11-19 18:29:28 +00003203 getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003204 TParamsArray.get(), getFunctionDeclaration(FD));
3205 }
3206
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003207 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003208 DContext, Name, LinkageName, Unit, Line,
Paul Robinsoncda54212018-11-19 18:29:28 +00003209 getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00003210 TParamsArray.get(), getFunctionDeclaration(FD));
George Burgess IV00f70bd2018-03-01 05:43:23 +00003211 const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003212 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
3213 std::make_tuple(CanonDecl),
3214 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00003215 return SP;
3216}
3217
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003218llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003219 return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
3220}
3221
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003222llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) {
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003223 return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
3224}
3225
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003226llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00003227CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
3228 QualType T;
3229 StringRef Name, LinkageName;
3230 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003231 llvm::DIFile *Unit = getOrCreateFile(Loc);
3232 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00003233 unsigned Line = getLineNumber(Loc);
Matthew Voss20165362018-10-03 18:45:04 +00003234 llvm::MDTuple *TemplateParameters = nullptr;
Frederic Rissd253ed62014-11-18 03:40:51 +00003235
Matthew Voss20165362018-10-03 18:45:04 +00003236 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, TemplateParameters,
3237 DContext);
Victor Leschuka7ece032016-10-20 00:13:19 +00003238 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003239 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
3240 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
Matthew Voss20165362018-10-03 18:45:04 +00003241 !VD->isExternallyVisible(), nullptr, TemplateParameters, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003242 FwdDeclReplaceMap.emplace_back(
3243 std::piecewise_construct,
3244 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
3245 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00003246 return GV;
3247}
3248
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003249llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003250 // We only need a declaration (not a definition) of the type - so use whatever
3251 // we would otherwise do to get a type for a pointee. (forward declarations in
3252 // limited debug info, full definitions (if the type definition is available)
3253 // in unlimited debug info)
David Majnemer58ed0f32016-07-17 00:39:12 +00003254 if (const auto *TD = dyn_cast<TypeDecl>(D))
David Blaikie6b7d060c2013-08-12 23:14:36 +00003255 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00003256 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003257 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00003258
Adrian Prantl5f4740d2016-12-20 02:10:02 +00003259 if (I != DeclCache.end()) {
3260 auto N = I->second;
3261 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
3262 return GVE->getVariable();
3263 return dyn_cast_or_null<llvm::DINode>(N);
3264 }
Frederic Rissd253ed62014-11-18 03:40:51 +00003265
3266 // No definition for now. Emit a forward definition that might be
3267 // merged with a potential upcoming definition.
David Majnemer58ed0f32016-07-17 00:39:12 +00003268 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Frederic Rissd253ed62014-11-18 03:40:51 +00003269 return getFunctionForwardDeclaration(FD);
3270 else if (const auto *VD = dyn_cast<VarDecl>(D))
3271 return getGlobalVariableForwardDeclaration(VD);
3272
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003273 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00003274}
3275
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003276llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003277 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003278 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00003279
David Majnemer58ed0f32016-07-17 00:39:12 +00003280 const auto *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00003281 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003282 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003283
3284 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003285 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003286
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003287 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00003288 if (MI == SPCache.end()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00003289 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003290 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003291 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00003292 }
3293 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003295 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003296 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003297 return SP;
3298 }
3299
Aaron Ballman86c93902014-03-06 23:45:36 +00003300 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003301 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003303 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003304 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 return SP;
3306 }
3307 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003308 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003309}
3310
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003311// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00003312// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003314 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003315 llvm::DIFile *F) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003316 if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003317 // Create fake but valid subroutine type. Otherwise -verify would fail, and
3318 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00003319 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00003320
David Majnemer58ed0f32016-07-17 00:39:12 +00003321 if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00003322 return getOrCreateMethodType(Method, F);
Reid Klecknerf00f8032016-06-08 20:41:54 +00003323
3324 const auto *FTy = FnType->getAs<FunctionType>();
3325 CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C;
3326
David Majnemer58ed0f32016-07-17 00:39:12 +00003327 if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003328 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003329 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00003330
3331 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00003332 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00003333
3334 // Replace the instancetype keyword with the actual type.
3335 if (ResultTy == CGM.getContext().getObjCInstanceType())
3336 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00003337 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00003338
Adrian Prantl7bec9032013-05-10 21:08:31 +00003339 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003341 QualType SelfDeclTy;
3342 if (auto *SelfDecl = OMethod->getSelfDecl())
3343 SelfDeclTy = SelfDecl->getType();
3344 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3345 if (FPT->getNumParams() > 1)
3346 SelfDeclTy = FPT->getParamType(0);
3347 if (!SelfDeclTy.isNull())
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003348 Elts.push_back(
3349 CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003350 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003351 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003352 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 // Get rest of the arguments.
David Majnemer59f77922016-06-24 04:05:48 +00003354 for (const auto *PI : OMethod->parameters())
Aaron Ballman43b68be2014-03-07 17:50:17 +00003355 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00003356 // Variadic methods need a special marker at the end of the type list.
3357 if (OMethod->isVariadic())
3358 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00003359
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003360 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003361 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3362 getDwarfCC(CC));
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 }
Adrian Prantld45ba252014-02-25 19:38:11 +00003364
Adrian Prantl800faef2014-02-25 23:42:18 +00003365 // Handle variadic function types; they need an additional
3366 // unspecified parameter.
David Majnemer58ed0f32016-07-17 00:39:12 +00003367 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Adrian Prantld45ba252014-02-25 19:38:11 +00003368 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003369 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00003370 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
David Majnemer58ed0f32016-07-17 00:39:12 +00003371 if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
3372 for (QualType ParamType : FPT->param_types())
3373 EltTys.push_back(getOrCreateType(ParamType, F));
Adrian Prantld45ba252014-02-25 19:38:11 +00003374 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003375 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Leny Kholodov80c047d2016-09-06 10:48:04 +00003376 return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
3377 getDwarfCC(CC));
Adrian Prantld45ba252014-02-25 19:38:11 +00003378 }
3379
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003380 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00003381}
3382
Eric Christophere7b87e52014-10-26 23:40:33 +00003383void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
3384 SourceLocation ScopeLoc, QualType FnType,
Brock Wyma94ece8f2018-04-16 16:53:57 +00003385 llvm::Function *Fn, bool CurFuncIsThunk,
3386 CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003387
3388 StringRef Name;
3389 StringRef LinkageName;
3390
3391 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3392
3393 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00003394 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00003395
Leny Kholodov80c047d2016-09-06 10:48:04 +00003396 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Paul Robinsoncda54212018-11-19 18:29:28 +00003397 llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003398 llvm::DIFile *Unit = getOrCreateFile(Loc);
3399 llvm::DIScope *FDContext = Unit;
3400 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 if (!HasDecl) {
3402 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00003403 LinkageName = Fn->getName();
David Majnemer58ed0f32016-07-17 00:39:12 +00003404 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003405 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003406 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003408 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00003409 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003410 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003411 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 return;
3413 }
3414 }
Frederic Riss9db79f12014-11-18 03:40:46 +00003415 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3416 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003417 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003419 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 } else {
3421 // Use llvm function name.
3422 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003423 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00003424 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003425 if (Name.startswith("\01"))
Guy Benyei11169dd2012-12-18 14:30:41 +00003426 Name = Name.substr(1);
3427
Erich Keane293a0552018-02-14 00:14:07 +00003428 if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003429 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantldb763572016-11-09 21:43:51 +00003430 // Artificial functions should not silently reuse CurLoc.
3431 CurLoc = SourceLocation();
Adrian Prantl42d71b92014-04-10 23:21:53 +00003432 }
Brock Wyma94ece8f2018-04-16 16:53:57 +00003433
3434 if (CurFuncIsThunk)
3435 Flags |= llvm::DINode::FlagThunk;
3436
Paul Robinsoncda54212018-11-19 18:29:28 +00003437 if (Fn->hasLocalLinkage())
3438 SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit;
3439 if (CGM.getLangOpts().Optimize)
3440 SPFlags |= llvm::DISubprogram::SPFlagOptimized;
3441
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003442 llvm::DINode::DIFlags FlagsForDef = Flags | getCallSiteRelatedAttrs();
Paul Robinsoncda54212018-11-19 18:29:28 +00003443 llvm::DISubprogram::DISPFlags SPFlagsForDef =
3444 SPFlags | llvm::DISubprogram::SPFlagDefinition;
Vedant Kumar5931b4e2018-10-05 20:37:17 +00003445
Adrian Prantl42d71b92014-04-10 23:21:53 +00003446 unsigned LineNo = getLineNumber(Loc);
3447 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003448
Eric Christopher8018e412014-03-27 18:50:35 +00003449 // FIXME: The function declaration we're constructing here is mostly reusing
3450 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
3451 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
3452 // all subprograms instead of the actual context since subprogram definitions
3453 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003454 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00003455 FDContext, Name, LinkageName, Unit, LineNo,
Paul Robinsoncda54212018-11-19 18:29:28 +00003456 getOrCreateFunctionType(D, FnType, Unit), ScopeLine, FlagsForDef,
3457 SPFlagsForDef, TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00003458 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00003459 // We might get here with a VarDecl in the case we're generating
3460 // code for the initialization of globals. Do not record these decls
3461 // as they will overwrite the actual VarDecl Decl in the cache.
3462 if (HasDecl && isa<FunctionDecl>(D))
David Majnemer58ed0f32016-07-17 00:39:12 +00003463 DeclCache[D->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003464
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00003465 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
3466 // Starting with DWARF V5 method declarations are emitted as children of
3467 // the interface type.
3468 if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
3469 const ObjCInterfaceDecl *ID = OMD->getClassInterface();
3470 QualType QTy(ID->getTypeForDecl(), 0);
3471 auto It = TypeCache.find(QTy.getAsOpaquePtr());
3472 if (It != TypeCache.end()) {
3473 llvm::DICompositeType *InterfaceDecl =
3474 cast<llvm::DICompositeType>(It->second);
3475 llvm::DISubprogram *FD = DBuilder.createFunction(
3476 InterfaceDecl, Name, LinkageName, Unit, LineNo,
Paul Robinsoncda54212018-11-19 18:29:28 +00003477 getOrCreateFunctionType(D, FnType, Unit), ScopeLine, Flags, SPFlags,
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00003478 TParamsArray.get());
3479 DBuilder.finalizeSubprogram(FD);
3480 ObjCMethodCache[ID].push_back(FD);
3481 }
3482 }
3483 }
3484
Adrian Prantlbebb8932014-03-21 21:01:58 +00003485 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003486 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00003487
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003489 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00003490}
3491
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003492void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
3493 QualType FnType) {
3494 StringRef Name;
3495 StringRef LinkageName;
3496
3497 const Decl *D = GD.getDecl();
3498 if (!D)
3499 return;
3500
Leny Kholodov80c047d2016-09-06 10:48:04 +00003501 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003502 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00003503 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003504 llvm::DINodeArray TParamsArray;
3505 if (isa<FunctionDecl>(D)) {
3506 // If there is a DISubprogram for this function available then use it.
3507 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
3508 TParamsArray, Flags);
David Majnemer58ed0f32016-07-17 00:39:12 +00003509 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003510 Name = getObjCMethodName(OMD);
3511 Flags |= llvm::DINode::FlagPrototyped;
3512 } else {
3513 llvm_unreachable("not a function or ObjC method");
3514 }
3515 if (!Name.empty() && Name[0] == '\01')
3516 Name = Name.substr(1);
3517
3518 if (D->isImplicit()) {
3519 Flags |= llvm::DINode::FlagArtificial;
3520 // Artificial functions without a location should not silently reuse CurLoc.
3521 if (Loc.isInvalid())
3522 CurLoc = SourceLocation();
3523 }
3524 unsigned LineNo = getLineNumber(Loc);
3525 unsigned ScopeLine = 0;
Paul Robinsoncda54212018-11-19 18:29:28 +00003526 llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
3527 if (CGM.getLangOpts().Optimize)
3528 SPFlags |= llvm::DISubprogram::SPFlagOptimized;
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003529
Adrian Prantle76bda52016-04-15 15:55:45 +00003530 DBuilder.retainType(DBuilder.createFunction(
3531 FDContext, Name, LinkageName, Unit, LineNo,
Paul Robinsoncda54212018-11-19 18:29:28 +00003532 getOrCreateFunctionType(D, FnType, Unit), ScopeLine, Flags, SPFlags,
Adrian Prantle76bda52016-04-15 15:55:45 +00003533 TParamsArray.get(), getFunctionDeclaration(D)));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00003534}
3535
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003536void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
3537 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3538 // If there is a subprogram for this function available then use it.
3539 auto FI = SPCache.find(FD->getCanonicalDecl());
3540 llvm::DISubprogram *SP = nullptr;
3541 if (FI != SPCache.end())
3542 SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Adrian Prantl8040a212017-08-23 21:24:12 +00003543 if (!SP || !SP->isDefinition())
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003544 SP = getFunctionStub(GD);
3545 FnBeginRegionCount.push_back(LexicalBlockStack.size());
3546 LexicalBlockStack.emplace_back(SP);
3547 setInlinedAt(Builder.getCurrentDebugLocation());
3548 EmitLocation(Builder, FD->getLocation());
3549}
3550
3551void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
3552 assert(CurInlinedAt && "unbalanced inline scope stack");
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003553 EmitFunctionEnd(Builder, nullptr);
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003554 setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
3555}
3556
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003557void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003558 // Update our current location
3559 setLocation(Loc);
3560
Adrian Prantl42ab39f2018-11-09 21:17:38 +00003561 if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
Eric Christophere7b87e52014-10-26 23:40:33 +00003562 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00003563
Adrian Prantle83b1302014-01-07 22:05:52 +00003564 llvm::MDNode *Scope = LexicalBlockStack.back();
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003565 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
3566 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt));
Guy Benyei11169dd2012-12-18 14:30:41 +00003567}
3568
Guy Benyei11169dd2012-12-18 14:30:41 +00003569void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00003570 llvm::MDNode *Back = nullptr;
3571 if (!LexicalBlockStack.empty())
3572 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003573 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003574 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003575 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00003576}
3577
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003578void CGDebugInfo::AppendAddressSpaceXDeref(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003579 unsigned AddressSpace, SmallVectorImpl<int64_t> &Expr) const {
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003580 Optional<unsigned> DWARFAddressSpace =
3581 CGM.getTarget().getDWARFAddressSpace(AddressSpace);
3582 if (!DWARFAddressSpace)
3583 return;
3584
3585 Expr.push_back(llvm::dwarf::DW_OP_constu);
3586 Expr.push_back(DWARFAddressSpace.getValue());
3587 Expr.push_back(llvm::dwarf::DW_OP_swap);
3588 Expr.push_back(llvm::dwarf::DW_OP_xderef);
3589}
3590
Eric Christopher0fdcb312013-05-16 00:52:20 +00003591void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
3592 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003593 // Set our current location.
3594 setLocation(Loc);
3595
Guy Benyei11169dd2012-12-18 14:30:41 +00003596 // Emit a line table change for the current location inside the new scope.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003597 Builder.SetCurrentDebugLocation(
3598 llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc),
3599 LexicalBlockStack.back(), CurInlinedAt));
David Blaikie60a877b2014-10-22 19:34:33 +00003600
Benjamin Kramer8c305922016-02-02 11:06:51 +00003601 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003602 return;
3603
3604 // Create a new lexical block and push it on the stack.
3605 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003606}
3607
Eric Christopher0fdcb312013-05-16 00:52:20 +00003608void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
3609 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003610 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3611
3612 // Provide an entry in the line table for the end of the block.
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003613 EmitLocation(Builder, Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003614
Benjamin Kramer8c305922016-02-02 11:06:51 +00003615 if (DebugKind <= codegenoptions::DebugLineTablesOnly)
David Blaikie60a877b2014-10-22 19:34:33 +00003616 return;
3617
Guy Benyei11169dd2012-12-18 14:30:41 +00003618 LexicalBlockStack.pop_back();
3619}
3620
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003621void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003622 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3623 unsigned RCount = FnBeginRegionCount.back();
3624 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
3625
3626 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00003627 while (LexicalBlockStack.size() != RCount) {
3628 // Provide an entry in the line table for the end of the block.
Calixte Denizetfcd661d2018-09-24 18:24:18 +00003629 EmitLocation(Builder, CurLoc);
David Blaikie60a877b2014-10-22 19:34:33 +00003630 LexicalBlockStack.pop_back();
3631 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003632 FnBeginRegionCount.pop_back();
Keno Fischer41d4b4e2017-06-01 21:14:03 +00003633
3634 if (Fn && Fn->getSubprogram())
3635 DBuilder.finalizeSubprogram(Fn->getSubprogram());
Guy Benyei11169dd2012-12-18 14:30:41 +00003636}
3637
Adrian Prantl05a623e2018-09-10 16:14:28 +00003638CGDebugInfo::BlockByRefType
3639CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
3640 uint64_t *XOffset) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003641 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00003642 QualType FType;
3643 uint64_t FieldSize, FieldOffset;
Victor Leschuk802e4a52016-10-19 22:11:07 +00003644 uint32_t FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003645
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003646 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003647 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00003648
3649 FieldOffset = 0;
3650 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
3651 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
3652 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
3653 FType = CGM.getContext().IntTy;
3654 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
3655 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
3656
3657 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
3658 if (HasCopyAndDispose) {
3659 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003660 EltTys.push_back(
3661 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
3662 EltTys.push_back(
3663 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00003664 }
3665 bool HasByrefExtendedLayout;
3666 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00003667 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
3668 HasByrefExtendedLayout) &&
3669 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00003670 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003671 EltTys.push_back(
3672 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00003673 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003674
Guy Benyei11169dd2012-12-18 14:30:41 +00003675 CharUnits Align = CGM.getContext().getDeclAlign(VD);
3676 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003677 CGM.getTarget().getPointerAlign(0))) {
3678 CharUnits FieldOffsetInBytes =
3679 CGM.getContext().toCharUnitsFromBits(FieldOffset);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003680 CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align);
Eric Christophere7b87e52014-10-26 23:40:33 +00003681 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003682
Guy Benyei11169dd2012-12-18 14:30:41 +00003683 if (NumPaddingBytes.isPositive()) {
3684 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
3685 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
3686 pad, ArrayType::Normal, 0);
3687 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
3688 }
3689 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00003690
Guy Benyei11169dd2012-12-18 14:30:41 +00003691 FType = Type;
Adrian Prantl05a623e2018-09-10 16:14:28 +00003692 llvm::DIType *WrappedTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003693 FieldSize = CGM.getContext().getTypeSize(FType);
3694 FieldAlign = CGM.getContext().toBits(Align);
3695
Eric Christopherb2a008c2013-05-16 00:45:12 +00003696 *XOffset = FieldOffset;
Adrian Prantl05a623e2018-09-10 16:14:28 +00003697 llvm::DIType *FieldTy = DBuilder.createMemberType(
3698 Unit, VD->getName(), Unit, 0, FieldSize, FieldAlign, FieldOffset,
3699 llvm::DINode::FlagZero, WrappedTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003700 EltTys.push_back(FieldTy);
3701 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003703 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Adrian Prantl05a623e2018-09-10 16:14:28 +00003704 return {DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0,
3705 llvm::DINode::FlagZero, nullptr, Elements),
3706 WrappedTy};
Guy Benyei11169dd2012-12-18 14:30:41 +00003707}
3708
Sander de Smalen891af03a2018-02-03 13:55:59 +00003709llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
3710 llvm::Value *Storage,
3711 llvm::Optional<unsigned> ArgNo,
3712 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003713 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003714 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003715 if (VD->hasAttr<NoDebugAttr>())
Sander de Smalen891af03a2018-02-03 13:55:59 +00003716 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003717
David Blaikie7fceebf2013-08-19 03:37:48 +00003718 bool Unwritten =
3719 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
3720 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003721 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00003722 if (!Unwritten)
3723 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003724 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003725 uint64_t XOffset = 0;
3726 if (VD->hasAttr<BlocksAttr>())
Adrian Prantl05a623e2018-09-10 16:14:28 +00003727 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003728 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003729 Ty = getOrCreateType(VD->getType(), Unit);
3730
3731 // If there is no debug info for this type then do not emit debug info
3732 // for this variable.
3733 if (!Ty)
Sander de Smalen891af03a2018-02-03 13:55:59 +00003734 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003735
Guy Benyei11169dd2012-12-18 14:30:41 +00003736 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00003737 unsigned Line = 0;
3738 unsigned Column = 0;
3739 if (!Unwritten) {
3740 Line = getLineNumber(VD->getLocation());
3741 Column = getColumnNumber(VD->getLocation());
3742 }
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003743 SmallVector<int64_t, 13> Expr;
Leny Kholodov80c047d2016-09-06 10:48:04 +00003744 llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
Guy Benyei11169dd2012-12-18 14:30:41 +00003745 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003746 Flags |= llvm::DINode::FlagArtificial;
Victor Leschuka7ece032016-10-20 00:13:19 +00003747
3748 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
3749
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00003750 unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType());
3751 AppendAddressSpaceXDeref(AddressSpace, Expr);
3752
Alexey Bataev24f710182017-06-09 13:55:08 +00003753 // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an
3754 // object pointer flag.
Alexey Bataev56223232017-06-09 13:40:18 +00003755 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) {
3756 if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis ||
3757 IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3758 Flags |= llvm::DINode::FlagObjectPointer;
3759 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003760
Adrian Prantlc3782a12017-04-18 01:22:01 +00003761 // Note: Older versions of clang used to emit byval references with an extra
3762 // DW_OP_deref, because they referenced the IR arg directly instead of
3763 // referencing an alloca. Newer versions of LLVM don't treat allocas
3764 // differently from other function arguments when used in a dbg.declare.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003765 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003766 StringRef Name = VD->getName();
3767 if (!Name.empty()) {
3768 if (VD->hasAttr<BlocksAttr>()) {
Adrian Prantlc3782a12017-04-18 01:22:01 +00003769 // Here, we need an offset *into* the alloca.
Guy Benyei11169dd2012-12-18 14:30:41 +00003770 CharUnits offset = CharUnits::fromQuantity(32);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003771 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003772 // offset of __forwarding field
3773 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00003774 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003775 Expr.push_back(offset.getQuantity());
3776 Expr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003777 Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003778 // offset of x field
3779 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00003780 Expr.push_back(offset.getQuantity());
Adrian Prantlc3782a12017-04-18 01:22:01 +00003781 }
David Majnemer58ed0f32016-07-17 00:39:12 +00003782 } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) {
Adrian Prantl0d820892015-04-29 15:05:50 +00003783 // If VD is an anonymous union then Storage represents value for
3784 // all union fields.
George Burgess IV00f70bd2018-03-01 05:43:23 +00003785 const RecordDecl *RD = RT->getDecl();
Adrian Prantl0d820892015-04-29 15:05:50 +00003786 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00003787 // GDB has trouble finding local variables in anonymous unions, so we emit
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003788 // artificial local variables for each of the members.
Adrian Prantl00820ab2015-04-29 16:52:31 +00003789 //
3790 // FIXME: Remove this code as soon as GDB supports this.
3791 // The debug info verifier in LLVM operates based on the assumption that a
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003792 // variable has the same size as its storage and we had to disable the
3793 // check for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00003794 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003795 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00003796 StringRef FieldName = Field->getName();
3797
3798 // Ignore unnamed fields. Do not ignore unnamed records.
3799 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
3800 continue;
3801
3802 // Use VarDecl's Tag, Scope and Line number.
Victor Leschuka7ece032016-10-20 00:13:19 +00003803 auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003804 auto *D = DBuilder.createAutoVariable(
3805 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
Victor Leschuka7ece032016-10-20 00:13:19 +00003806 Flags | llvm::DINode::FlagArtificial, FieldAlign);
Adrian Prantl0d820892015-04-29 15:05:50 +00003807
3808 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003809 DBuilder.insertDeclare(
3810 Storage, D, DBuilder.createExpression(Expr),
3811 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
3812 Builder.GetInsertBlock());
Adrian Prantl0d820892015-04-29 15:05:50 +00003813 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003814 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003815 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003816
3817 // Create the descriptor for the variable.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003818 auto *D = ArgNo ? DBuilder.createParameterVariable(
3819 Scope, Name, *ArgNo, Unit, Line, Ty,
3820 CGM.getLangOpts().Optimize, Flags)
3821 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3822 CGM.getLangOpts().Optimize,
3823 Flags, Align);
David Blaikiea76a7c92013-01-05 05:58:35 +00003824
3825 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003826 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003827 llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003828 Builder.GetInsertBlock());
Sander de Smalen891af03a2018-02-03 13:55:59 +00003829
3830 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00003831}
3832
Sander de Smalen891af03a2018-02-03 13:55:59 +00003833llvm::DILocalVariable *
3834CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage,
3835 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003836 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Sander de Smalen891af03a2018-02-03 13:55:59 +00003837 return EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003838}
3839
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003840llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3841 llvm::DIType *Ty) {
3842 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003843 if (CachedTy)
3844 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003845 return DBuilder.createObjectPointerType(Ty);
3846}
3847
Eric Christophere7b87e52014-10-26 23:40:33 +00003848void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3849 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003850 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003851 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003852 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003853
Craig Topper8a13c412014-05-21 05:09:00 +00003854 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003855 return;
Paul Robinsonafd2dde2016-06-16 00:42:36 +00003856 if (VD->hasAttr<NoDebugAttr>())
3857 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003858
Guy Benyei11169dd2012-12-18 14:30:41 +00003859 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003860
Guy Benyei11169dd2012-12-18 14:30:41 +00003861 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003862 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3863 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003864 if (isByRef)
Adrian Prantl05a623e2018-09-10 16:14:28 +00003865 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003866 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 Ty = getOrCreateType(VD->getType(), Unit);
3868
3869 // Self is passed along as an implicit non-arg variable in a
3870 // block. Mark it as the object pointer.
Alexey Bataev56223232017-06-09 13:40:18 +00003871 if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD))
3872 if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf)
3873 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003874
3875 // Get location information.
3876 unsigned Line = getLineNumber(VD->getLocation());
3877 unsigned Column = getColumnNumber(VD->getLocation());
3878
3879 const llvm::DataLayout &target = CGM.getDataLayout();
3880
3881 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003882 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003883 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3884
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003885 SmallVector<int64_t, 9> addr;
Adrian Prantlc3782a12017-04-18 01:22:01 +00003886 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003887 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003888 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003889 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003890 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003891 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003892 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003893 offset =
3894 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003895 addr.push_back(offset.getQuantity());
3896 addr.push_back(llvm::dwarf::DW_OP_deref);
Florian Hahn3dbcced2017-06-13 18:06:15 +00003897 addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
Guy Benyei11169dd2012-12-18 14:30:41 +00003898 // offset of x field
3899 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003900 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003901 }
3902
3903 // Create the descriptor for the variable.
Victor Leschuka7ece032016-10-20 00:13:19 +00003904 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003905 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003906 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Victor Leschuka7ece032016-10-20 00:13:19 +00003907 Line, Ty, false, llvm::DINode::FlagZero, Align);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003908
Guy Benyei11169dd2012-12-18 14:30:41 +00003909 // Insert an llvm.dbg.declare into the current block.
Adrian Prantlb7acfc02017-02-27 21:30:05 +00003910 auto DL =
3911 llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt);
Adrian Prantlc3782a12017-04-18 01:22:01 +00003912 auto *Expr = DBuilder.createExpression(addr);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003913 if (InsertPoint)
Adrian Prantlc3782a12017-04-18 01:22:01 +00003914 DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003915 else
Adrian Prantlc3782a12017-04-18 01:22:01 +00003916 DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003917}
3918
Guy Benyei11169dd2012-12-18 14:30:41 +00003919void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3920 unsigned ArgNo,
3921 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003922 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003923 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003924}
3925
3926namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003927struct BlockLayoutChunk {
3928 uint64_t OffsetInBits;
3929 const BlockDecl::Capture *Capture;
3930};
3931bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3932 return l.OffsetInBits < r.OffsetInBits;
3933}
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00003934} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00003935
Scott Linder58df0e42018-08-08 15:56:12 +00003936void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
3937 const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
3938 const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
3939 SmallVectorImpl<llvm::Metadata *> &Fields) {
3940 // Blocks in OpenCL have unique constraints which make the standard fields
3941 // redundant while requiring size and align fields for enqueue_kernel. See
3942 // initializeForBlockHeader in CGBlocks.cpp
3943 if (CGM.getLangOpts().OpenCL) {
3944 Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public,
3945 BlockLayout.getElementOffsetInBits(0),
3946 Unit, Unit));
3947 Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
3948 BlockLayout.getElementOffsetInBits(1),
3949 Unit, Unit));
3950 } else {
3951 Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
3952 BlockLayout.getElementOffsetInBits(0),
3953 Unit, Unit));
3954 Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
3955 BlockLayout.getElementOffsetInBits(1),
3956 Unit, Unit));
3957 Fields.push_back(
3958 createFieldType("__reserved", Context.IntTy, Loc, AS_public,
3959 BlockLayout.getElementOffsetInBits(2), Unit, Unit));
3960 auto *FnTy = Block.getBlockExpr()->getFunctionType();
3961 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3962 Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
3963 BlockLayout.getElementOffsetInBits(3),
3964 Unit, Unit));
3965 Fields.push_back(createFieldType(
3966 "__descriptor",
3967 Context.getPointerType(Block.NeedsCopyDispose
3968 ? Context.getBlockDescriptorExtendedType()
3969 : Context.getBlockDescriptorType()),
3970 Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
3971 }
3972}
3973
Guy Benyei11169dd2012-12-18 14:30:41 +00003974void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl356347b2017-10-26 20:08:52 +00003975 StringRef Name,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003976 unsigned ArgNo,
Adrian Prantl356347b2017-10-26 20:08:52 +00003977 llvm::AllocaInst *Alloca,
Guy Benyei11169dd2012-12-18 14:30:41 +00003978 CGBuilderTy &Builder) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00003979 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003980 ASTContext &C = CGM.getContext();
3981 const BlockDecl *blockDecl = block.getBlockDecl();
3982
3983 // Collect some general information about the block's location.
3984 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003985 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003986 unsigned line = getLineNumber(loc);
3987 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003988
Guy Benyei11169dd2012-12-18 14:30:41 +00003989 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003990 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003991
3992 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003993 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003994
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003995 SmallVector<llvm::Metadata *, 16> fields;
Scott Linder58df0e42018-08-08 15:56:12 +00003996 collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
3997 fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003998
3999 // We want to sort the captures by offset, not because DWARF
4000 // requires this, but because we're paranoid about debuggers.
4001 SmallVector<BlockLayoutChunk, 8> chunks;
4002
4003 // 'this' capture.
4004 if (blockDecl->capturesCXXThis()) {
4005 BlockLayoutChunk chunk;
4006 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00004007 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00004008 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004009 chunks.push_back(chunk);
4010 }
4011
4012 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00004013 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004014 const VarDecl *variable = capture.getVariable();
4015 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
4016
4017 // Ignore constant captures.
4018 if (captureInfo.isConstant())
4019 continue;
4020
4021 BlockLayoutChunk chunk;
4022 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00004023 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00004024 chunk.Capture = &capture;
4025 chunks.push_back(chunk);
4026 }
4027
4028 // Sort by offset.
4029 llvm::array_pod_sort(chunks.begin(), chunks.end());
4030
David Majnemer58ed0f32016-07-17 00:39:12 +00004031 for (const BlockLayoutChunk &Chunk : chunks) {
4032 uint64_t offsetInBits = Chunk.OffsetInBits;
4033 const BlockDecl::Capture *capture = Chunk.Capture;
Guy Benyei11169dd2012-12-18 14:30:41 +00004034
4035 // If we have a null capture, this must be the C++ 'this' capture.
4036 if (!capture) {
Adrian Prantl2526fca2016-04-18 23:48:16 +00004037 QualType type;
4038 if (auto *Method =
4039 cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext()))
4040 type = Method->getThisType(C);
4041 else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
4042 type = QualType(RDecl->getTypeForDecl(), 0);
4043 else
4044 llvm_unreachable("unexpected block declcontext");
Guy Benyei11169dd2012-12-18 14:30:41 +00004045
David Majnemerb4b671e2016-06-30 03:01:59 +00004046 fields.push_back(createFieldType("this", type, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00004047 offsetInBits, tunit, tunit));
4048 continue;
4049 }
4050
4051 const VarDecl *variable = capture->getVariable();
4052 StringRef name = variable->getName();
4053
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004054 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00004055 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00004056 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Victor Leschuka7ece032016-10-20 00:13:19 +00004057 auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0;
Adrian Prantl05a623e2018-09-10 16:14:28 +00004058 // FIXME: This recomputes the layout of the BlockByRefWrapper.
Guy Benyei11169dd2012-12-18 14:30:41 +00004059 uint64_t xoffset;
Adrian Prantl05a623e2018-09-10 16:14:28 +00004060 fieldType =
4061 EmitTypeForVarWithBlocksAttr(variable, &xoffset).BlockByRefWrapper;
David Majnemer34b57492014-07-30 01:30:47 +00004062 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
Victor Leschuka7ece032016-10-20 00:13:19 +00004063 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
4064 PtrInfo.Width, Align, offsetInBits,
4065 llvm::DINode::FlagZero, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00004066 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00004067 auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
David Majnemerb4b671e2016-06-30 03:01:59 +00004068 fieldType = createFieldType(name, variable->getType(), loc, AS_public,
Victor Leschuka7ece032016-10-20 00:13:19 +00004069 offsetInBits, Align, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00004070 }
4071 fields.push_back(fieldType);
4072 }
4073
4074 SmallString<36> typeName;
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004075 llvm::raw_svector_ostream(typeName)
4076 << "__block_literal_" << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00004077
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004078 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00004079
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004080 llvm::DIType *type =
4081 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
Victor Leschuka7ece032016-10-20 00:13:19 +00004082 CGM.getContext().toBits(block.BlockSize), 0,
Leny Kholodovdf050fd2016-09-06 17:06:14 +00004083 llvm::DINode::FlagZero, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00004084 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
4085
4086 // Get overall information about the block.
Leny Kholodov80c047d2016-09-06 10:48:04 +00004087 llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004088 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00004089
4090 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00004091 auto *debugVar = DBuilder.createParameterVariable(
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004092 scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00004093
Adrian Prantl616bef42013-03-14 21:52:59 +00004094 // Insert an llvm.dbg.declare into the current block.
Adrian Prantl356347b2017-10-26 20:08:52 +00004095 DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
Adrian Prantlb7acfc02017-02-27 21:30:05 +00004096 llvm::DebugLoc::get(line, column, scope, CurInlinedAt),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00004097 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00004098}
4099
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004100llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00004101CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
4102 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00004103 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00004104
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004105 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00004106 if (MI != StaticDataMemberCache.end()) {
4107 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00004108 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00004109 }
David Blaikiece763042013-08-20 21:49:21 +00004110
4111 // If the member wasn't found in the cache, lazily construct and add it to the
4112 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00004113 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004114 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00004115 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00004116}
4117
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004118llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004119 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
4120 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004121 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004122
4123 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004124 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00004125 StringRef FieldName = Field->getName();
4126
4127 // Ignore unnamed fields, but recurse into anonymous records.
4128 if (FieldName.empty()) {
David Majnemer58ed0f32016-07-17 00:39:12 +00004129 if (const auto *RT = dyn_cast<RecordType>(Field->getType()))
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004130 GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004131 Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004132 continue;
4133 }
4134 // Use VarDecl's Tag, Scope and Line number.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004135 GVE = DBuilder.createGlobalVariableExpression(
4136 DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
4137 Var->hasLocalLinkage());
4138 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004139 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004140 return GVE;
Eric Christophercab9fae2014-04-10 05:20:00 +00004141}
4142
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004143void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00004144 const VarDecl *D) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004145 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004146 if (D->hasAttr<NoDebugAttr>())
4147 return;
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004148
4149 // If we already created a DIGlobalVariable for this declaration, just attach
4150 // it to the llvm::GlobalVariable.
4151 auto Cached = DeclCache.find(D->getCanonicalDecl());
4152 if (Cached != DeclCache.end())
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004153 return Var->addDebugInfo(
4154 cast<llvm::DIGlobalVariableExpression>(Cached->second));
Adrian Prantl338ef7a2016-11-09 00:42:03 +00004155
Guy Benyei11169dd2012-12-18 14:30:41 +00004156 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004157 llvm::DIFile *Unit = nullptr;
4158 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00004159 unsigned LineNo;
4160 StringRef DeclName, LinkageName;
4161 QualType T;
Matthew Voss20165362018-10-03 18:45:04 +00004162 llvm::MDTuple *TemplateParameters = nullptr;
4163 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName,
4164 TemplateParameters, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004165
4166 // Attempt to store one global variable for the declaration - even if we
4167 // emit a lot of fields.
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004168 llvm::DIGlobalVariableExpression *GVE = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00004169
4170 // If this is an anonymous union then we'll want to emit a global
4171 // variable for each member of the anonymous union so that it's possible
4172 // to find the name of any field in the union.
4173 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00004174 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00004175 assert(RD->isAnonymousStructOrUnion() &&
4176 "unnamed non-anonymous struct or union?");
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004177 GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00004178 } else {
Victor Leschuka7ece032016-10-20 00:13:19 +00004179 auto Align = getDeclAlignIfRequired(D, CGM.getContext());
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004180
4181 SmallVector<int64_t, 4> Expr;
4182 unsigned AddressSpace =
4183 CGM.getContext().getTargetAddressSpace(D->getType());
4184 AppendAddressSpaceXDeref(AddressSpace, Expr);
4185
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004186 GVE = DBuilder.createGlobalVariableExpression(
Eric Christophercab9fae2014-04-10 05:20:00 +00004187 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
Konstantin Zhuravlyov2b4917f2017-03-09 18:06:23 +00004188 Var->hasLocalLinkage(),
4189 Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
Matthew Voss20165362018-10-03 18:45:04 +00004190 getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
4191 Align);
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004192 Var->addDebugInfo(GVE);
Eric Christophercab9fae2014-04-10 05:20:00 +00004193 }
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004194 DeclCache[D->getCanonicalDecl()].reset(GVE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004195}
4196
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004197void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004198 assert(DebugKind >= codegenoptions::LimitedDebugInfo);
Paul Robinsonb17327d2016-04-27 17:37:12 +00004199 if (VD->hasAttr<NoDebugAttr>())
4200 return;
Victor Leschuka7ece032016-10-20 00:13:19 +00004201 auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004202 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004203 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004204 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004205 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
David Majnemer58ed0f32016-07-17 00:39:12 +00004206 if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
4207 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004208 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
4209 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
4210 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004211 // Do not use global variables for enums.
4212 //
4213 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004214 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00004215 return;
David Blaikiea15565562014-04-04 20:56:17 +00004216 // Do not emit separate definitions for function local const/statics.
4217 if (isa<FunctionDecl>(VD->getDeclContext()))
4218 return;
David Blaikiebb113912014-04-05 07:23:17 +00004219 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00004220 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004221 if (VarD->isStaticDataMember()) {
4222 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004223 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00004224 // Ensure that the type is retained even though it's otherwise unreferenced.
Duncan P. N. Exon Smith383f8412016-04-23 21:08:27 +00004225 //
4226 // FIXME: This is probably unnecessary, since Ty should reference RD
4227 // through its scope.
David Blaikie423eb5a2014-11-19 19:42:40 +00004228 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00004229 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00004230 return;
4231 }
4232
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004233 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00004234
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004235 auto &GV = DeclCache[VD];
4236 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00004237 return;
Peter Collingbourneeeb56ab2016-09-13 01:13:19 +00004238 llvm::DIExpression *InitExpr = nullptr;
Peter Collingbourneb7013632016-12-16 22:10:52 +00004239 if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
4240 // FIXME: Add a representation for integer constants wider than 64 bits.
4241 if (Init.isInt())
4242 InitExpr =
4243 DBuilder.createConstantValueExpression(Init.getInt().getExtValue());
4244 else if (Init.isFloat())
4245 InitExpr = DBuilder.createConstantValueExpression(
4246 Init.getFloat().bitcastToAPInt().getZExtValue());
4247 }
Matthew Voss20165362018-10-03 18:45:04 +00004248
4249 llvm::MDTuple *TemplateParameters = nullptr;
4250
4251 if (isa<VarTemplateSpecializationDecl>(VD))
4252 if (VarD) {
4253 llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VarD, &*Unit);
4254 TemplateParameters = parameterNodes.get();
4255 }
4256
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004257 GV.reset(DBuilder.createGlobalVariableExpression(
David Blaikie506a7452014-04-05 07:46:57 +00004258 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Victor Leschuka7ece032016-10-20 00:13:19 +00004259 true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
Matthew Voss20165362018-10-03 18:45:04 +00004260 TemplateParameters, Align));
David Blaikiebd483762013-05-20 04:58:53 +00004261}
4262
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004263llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00004264 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00004265 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00004266 llvm::DIScope *Mod = getParentModuleOrNull(D);
4267 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00004268}
4269
David Blaikie9f88fe82013-04-22 06:13:21 +00004270void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004271 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004272 return;
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004273 const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
Adrian McCarthyab1e7862016-07-21 18:43:20 +00004274 if (!NSDecl->isAnonymousNamespace() ||
4275 CGM.getCodeGenOpts().DebugExplicitImport) {
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004276 auto Loc = UD.getLocation();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004277 DBuilder.createImportedModule(
4278 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004279 getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00004280 }
David Blaikie9f88fe82013-04-22 06:13:21 +00004281}
4282
David Blaikiebd483762013-05-20 04:58:53 +00004283void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004284 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikiebd483762013-05-20 04:58:53 +00004285 return;
4286 assert(UD.shadow_size() &&
4287 "We shouldn't be codegening an invalid UsingDecl containing no decls");
4288 // Emitting one decl is sufficient - debuggers can detect that this is an
4289 // overloaded name & provide lookup for all the overloads.
4290 const UsingShadowDecl &USD = **UD.shadow_begin();
David Blaikie2a58a182016-08-05 19:03:01 +00004291
4292 // FIXME: Skip functions with undeduced auto return type for now since we
4293 // don't currently have the plumbing for separate declarations & definitions
4294 // of free functions and mismatched types (auto in the declaration, concrete
4295 // return type in the definition)
4296 if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl()))
4297 if (const auto *AT =
4298 FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
4299 if (AT->getDeducedType().isNull())
4300 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004301 if (llvm::DINode *Target =
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004302 getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
4303 auto Loc = USD.getLocation();
David Blaikiebd483762013-05-20 04:58:53 +00004304 DBuilder.createImportedDeclaration(
4305 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004306 getOrCreateFile(Loc), getLineNumber(Loc));
4307 }
David Blaikiebd483762013-05-20 04:58:53 +00004308}
4309
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004310void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
David Blaikieaf09f4a2016-05-03 23:06:40 +00004311 if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB)
4312 return;
Adrian Prantl8a634c12015-12-18 19:44:31 +00004313 if (Module *M = ID.getImportedModule()) {
Chad Rosiere5dafd12015-12-18 20:08:40 +00004314 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004315 auto Loc = ID.getLocation();
Adrian Prantl8a634c12015-12-18 19:44:31 +00004316 DBuilder.createImportedDeclaration(
4317 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004318 getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
4319 getLineNumber(Loc));
Adrian Prantl8a634c12015-12-18 19:44:31 +00004320 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00004321}
4322
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004323llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00004324CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004325 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00004326 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004327 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00004328 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004329 return cast<llvm::DIImportedEntity>(VH);
4330 llvm::DIImportedEntity *R;
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004331 auto Loc = NA.getLocation();
David Majnemer58ed0f32016-07-17 00:39:12 +00004332 if (const auto *Underlying =
David Blaikief121b932013-05-20 22:50:41 +00004333 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
4334 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00004335 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004336 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004337 EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
4338 getLineNumber(Loc), NA.getName());
David Blaikief121b932013-05-20 22:50:41 +00004339 else
David Blaikie551fb0a2014-04-06 06:30:03 +00004340 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00004341 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
Adrian Prantlddb8e062017-05-12 16:23:53 +00004342 getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
Adrian Prantl5649b0e2017-07-19 00:09:58 +00004343 getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004344 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00004345 return R;
4346}
4347
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004348llvm::DINamespace *
Adrian Prantlddb8e062017-05-12 16:23:53 +00004349CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) {
4350 // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued
4351 // if necessary, and this way multiple declarations of the same namespace in
4352 // different parent modules stay distinct.
4353 auto I = NamespaceCache.find(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004354 if (I != NamespaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004355 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00004356
Adrian Prantl6ec370a2015-09-10 18:39:45 +00004357 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Adrian Prantld8870552017-05-11 22:59:19 +00004358 // Don't trust the context if it is a DIModule (see comment above).
Adrian Prantlddb8e062017-05-12 16:23:53 +00004359 llvm::DINamespace *NS =
4360 DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
4361 NamespaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 return NS;
4363}
4364
Adrian Prantla5206ce2015-09-22 23:26:43 +00004365void CGDebugInfo::setDwoId(uint64_t Signature) {
4366 assert(TheCU && "no main compile unit");
4367 TheCU->setDWOId(Signature);
4368}
4369
Guy Benyei11169dd2012-12-18 14:30:41 +00004370void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00004371 // Creating types might create further types - invalidating the current
4372 // element and the size(), so don't cache/reference them.
4373 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
4374 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004375 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00004376 ? CreateTypeDefinition(E.Type, E.Unit)
4377 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004378 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00004379 }
4380
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004381 if (CGM.getCodeGenOpts().DwarfVersion >= 5) {
4382 // Add methods to interface.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004383 for (const auto &P : ObjCMethodCache) {
Jonas Devlieghered8ba8ae2018-06-27 17:31:59 +00004384 if (P.second.empty())
4385 continue;
4386
4387 QualType QTy(P.first->getTypeForDecl(), 0);
4388 auto It = TypeCache.find(QTy.getAsOpaquePtr());
4389 assert(It != TypeCache.end());
4390
4391 llvm::DICompositeType *InterfaceDecl =
4392 cast<llvm::DICompositeType>(It->second);
4393
4394 SmallVector<llvm::Metadata *, 16> EltTys;
4395 auto CurrenetElts = InterfaceDecl->getElements();
4396 EltTys.append(CurrenetElts.begin(), CurrenetElts.end());
4397 for (auto &MD : P.second)
4398 EltTys.push_back(MD);
4399 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
4400 DBuilder.replaceArrays(InterfaceDecl, Elements);
4401 }
4402 }
4403
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004404 for (const auto &P : ReplaceMap) {
4405 assert(P.second);
4406 auto *Ty = cast<llvm::DIType>(P.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00004407 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00004408
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004409 auto It = TypeCache.find(P.first);
4410 assert(It != TypeCache.end());
4411 assert(It->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00004412
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00004413 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004414 cast<llvm::DIType>(It->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00004415 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00004416
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004417 for (const auto &P : FwdDeclReplaceMap) {
4418 assert(P.second);
4419 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00004420 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00004421
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004422 auto It = DeclCache.find(P.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00004423 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00004424 // with ourselves, that will destroy the temporary MDNode and
4425 // replace it with a standard one, avoiding leaking memory.
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004426 if (It == DeclCache.end())
4427 Repl = P.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00004428 else
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004429 Repl = It->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00004430
Adrian Prantl5f4740d2016-12-20 02:10:02 +00004431 if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl))
4432 Repl = GVE->getVariable();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00004433 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00004434 }
4435
Adrian Prantl73409ce2013-03-11 18:33:46 +00004436 // We keep our own list of retained types, because we need to look
4437 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00004438 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00004439 if (auto MD = TypeCache[RT])
4440 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00004441
Guy Benyei11169dd2012-12-18 14:30:41 +00004442 DBuilder.finalize();
4443}
David Blaikie66088d52014-09-24 17:01:27 +00004444
4445void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
Benjamin Kramer8c305922016-02-02 11:06:51 +00004446 if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo)
David Blaikie66088d52014-09-24 17:01:27 +00004447 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004448
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00004449 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00004450 // Don't ignore in case of explicit cast where it is referenced indirectly.
4451 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00004452}
Amara Emerson652795d2016-11-10 14:44:30 +00004453
4454llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
4455 if (LexicalBlockStack.empty())
4456 return llvm::DebugLoc();
4457
4458 llvm::MDNode *Scope = LexicalBlockStack.back();
Jonas Devlieghere9ef49652018-06-28 10:56:40 +00004459 return llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc), Scope);
Amara Emerson652795d2016-11-10 14:44:30 +00004460}
Vedant Kumar5931b4e2018-10-05 20:37:17 +00004461
4462llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
4463 // Call site-related attributes are only useful in optimized programs, and
4464 // when there's a possibility of debugging backtraces.
4465 if (!CGM.getLangOpts().Optimize || DebugKind == codegenoptions::NoDebugInfo ||
4466 DebugKind == codegenoptions::LocTrackingOnly)
4467 return llvm::DINode::FlagZero;
4468
4469 // Call site-related attributes are available in DWARF v5. Some debuggers,
4470 // while not fully DWARF v5-compliant, may accept these attributes as if they
4471 // were part of DWARF v4.
4472 bool SupportsDWARFv4Ext =
4473 CGM.getCodeGenOpts().DwarfVersion == 4 &&
4474 CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB;
4475 if (!SupportsDWARFv4Ext && CGM.getCodeGenOpts().DwarfVersion < 5)
4476 return llvm::DINode::FlagZero;
4477
4478 return llvm::DINode::FlagAllCallsDescribed;
4479}