blob: 4305280a0c875fbe408ee42044f48d80c4c9e1ff [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"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/Version.h"
29#include "clang/Frontend/CodeGenOptions.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000030#include "clang/Lex/HeaderSearchOptions.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000031#include "clang/Lex/ModuleMap.h"
Adrian Prantlc4bb47e2015-06-30 17:39:51 +000032#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000033#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000035#include "llvm/IR/Constants.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/Intrinsics.h"
40#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000042#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043using namespace clang;
44using namespace clang::CodeGen;
45
46CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000047 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000048 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000049 DBuilder(CGM.getModule()) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +000050 for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap)
51 DebugPrefixMap[KV.first] = KV.second;
Guy Benyei11169dd2012-12-18 14:30:41 +000052 CreateCompileUnit();
53}
54
55CGDebugInfo::~CGDebugInfo() {
56 assert(LexicalBlockStack.empty() &&
57 "Region stack mismatch, stack not empty!");
58}
59
David Blaikie66e41972015-01-14 07:38:27 +000060ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000061 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000062 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000063 init(TemporaryLocation);
64}
65
Adrian Prantl39428e72015-02-03 18:40:42 +000066ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000067 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000068 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000069 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000070 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000071}
72
73void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000074 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000075 auto *DI = CGF->getDebugInfo();
76 if (!DI) {
77 CGF = nullptr;
78 return;
David Blaikie66e41972015-01-14 07:38:27 +000079 }
David Blaikied7057d92015-08-12 23:49:57 +000080
81 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
82 if (TemporaryLocation.isValid()) {
83 DI->EmitLocation(CGF->Builder, TemporaryLocation);
84 return;
85 }
86
87 if (DefaultToEmpty) {
88 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
89 return;
90 }
91
92 // Construct a location that has a valid scope, but no line info.
93 assert(!DI->LexicalBlockStack.empty());
94 CGF->Builder.SetCurrentDebugLocation(
95 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000096}
97
David Blaikie9b479662015-01-25 01:19:10 +000098ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000099 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +0000100 init(E->getExprLoc());
101}
102
David Blaikie66e41972015-01-14 07:38:27 +0000103ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000104 : CGF(&CGF) {
105 if (!CGF.getDebugInfo()) {
106 this->CGF = nullptr;
107 return;
David Blaikie66e41972015-01-14 07:38:27 +0000108 }
David Blaikied7057d92015-08-12 23:49:57 +0000109 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
110 if (Loc)
111 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000112}
113
114ApplyDebugLocation::~ApplyDebugLocation() {
115 // Query CGF so the location isn't overwritten when location updates are
116 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000117 if (CGF)
118 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000119}
120
Guy Benyei11169dd2012-12-18 14:30:41 +0000121void CGDebugInfo::setLocation(SourceLocation Loc) {
122 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000123 if (Loc.isInvalid())
124 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
126 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
127
128 // If we've changed files in the middle of a lexical scope go ahead
129 // and create a new lexical scope with file node if it's different
130 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000131 if (LexicalBlockStack.empty())
132 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000133
134 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000135 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000137
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000138 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 return;
140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000141 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000143 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
144 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000145 } else if (isa<llvm::DILexicalBlock>(Scope) ||
146 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000148 LexicalBlockStack.emplace_back(
149 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 }
151}
152
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000153llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000154 llvm::DIScope *Mod = getParentModuleOrNull(D);
155 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
156 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000157}
158
159llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
160 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000161 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000162 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000163
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000164 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000166 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000167 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 }
169
170 // Check namespace.
171 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000172 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000173
David Blaikiebfa52742013-04-19 06:56:38 +0000174 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
175 if (!RDecl->isDependentType())
176 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000177 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000178 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000179}
180
Guy Benyei11169dd2012-12-18 14:30:41 +0000181StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000182 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000184 FunctionTemplateSpecializationInfo *Info =
185 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000186 if (!Info && FII)
187 return FII->getName();
188
189 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000190 SmallString<128> NS;
191 llvm::raw_svector_ostream OS(NS);
192 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000193
194 // Add any template specialization args.
195 if (Info) {
196 const TemplateArgumentList *TArgs = Info->TemplateArguments;
197 const TemplateArgument *Args = TArgs->data();
198 unsigned NumArgs = TArgs->size();
199 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000200 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
201 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000202 }
203
204 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000205 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000206}
207
208StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
209 SmallString<256> MethodName;
210 llvm::raw_svector_ostream OS(MethodName);
211 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
212 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000213 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 dyn_cast<const ObjCImplementationDecl>(DC)) {
215 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000216 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 dyn_cast<const ObjCInterfaceDecl>(DC)) {
218 OS << OID->getName();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000219 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
220 if (OC->IsClassExtension()) {
221 OS << OC->getClassInterface()->getName();
222 } else {
223 OS << ((const NamedDecl *)OC)->getIdentifier()->getNameStart() << '('
224 << OC->getIdentifier()->getNameStart() << ')';
225 }
Eric Christopherb2a008c2013-05-16 00:45:12 +0000226 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000227 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
228 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
229 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000230 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000231 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000232 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000233 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000234 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000235 ClassTy.print(OS, PrintingPolicy(LangOptions()));
236 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000237 }
238 OS << ' ' << OMD->getSelector().getAsString() << ']';
239
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000240 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000241}
242
Guy Benyei11169dd2012-12-18 14:30:41 +0000243StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000244 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000245}
246
Eric Christophere7b87e52014-10-26 23:40:33 +0000247StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000248 // quick optimization to avoid having to intern strings that are already
249 // stored reliably elsewhere
250 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000251 return RD->getName();
252
David Blaikie65813a32014-04-02 18:21:09 +0000253 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000254 {
David Blaikie65813a32014-04-02 18:21:09 +0000255 llvm::raw_svector_ostream OS(Name);
256 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
257 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000258 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
260 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000261 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000262}
263
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000264llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000265 if (!Loc.isValid())
266 // If Location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000267 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
268 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000269
270 SourceManager &SM = CGM.getContext().getSourceManager();
271 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
272
273 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
274 // If the location is not valid then use main input file.
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000275 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
276 remapDIPath(TheCU->getDirectory()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000277
278 // Cache the results.
279 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000280 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000281
282 if (it != DIFileCache.end()) {
283 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000284 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000285 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000286 }
287
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000288 llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()),
289 remapDIPath(getCurrentDirname()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000290
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000291 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000292 return F;
293}
294
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000295llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000296 return DBuilder.createFile(remapDIPath(TheCU->getFilename()),
297 remapDIPath(TheCU->getDirectory()));
298}
299
300std::string CGDebugInfo::remapDIPath(StringRef Path) const {
301 for (const auto &Entry : DebugPrefixMap)
302 if (Path.startswith(Entry.first))
303 return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
304 return Path.str();
Guy Benyei11169dd2012-12-18 14:30:41 +0000305}
306
Guy Benyei11169dd2012-12-18 14:30:41 +0000307unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
308 if (Loc.isInvalid() && CurLoc.isInvalid())
309 return 0;
310 SourceManager &SM = CGM.getContext().getSourceManager();
311 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000312 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000313}
314
Adrian Prantlc7822422013-03-12 20:43:25 +0000315unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000316 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000317 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000318 return 0;
319
320 // If the location is invalid then use the current column.
321 if (Loc.isInvalid() && CurLoc.isInvalid())
322 return 0;
323 SourceManager &SM = CGM.getContext().getSourceManager();
324 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000325 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000326}
327
328StringRef CGDebugInfo::getCurrentDirname() {
329 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
330 return CGM.getCodeGenOpts().DebugCompilationDir;
331
332 if (!CWDName.empty())
333 return CWDName;
334 SmallString<256> CWD;
335 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000336 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000337}
338
Guy Benyei11169dd2012-12-18 14:30:41 +0000339void CGDebugInfo::CreateCompileUnit() {
340
David Blaikieaabde052014-05-14 00:29:00 +0000341 // Should we be asking the SourceManager for the main file name, instead of
342 // accepting it as an argument? This just causes the main file name to
343 // mismatch with source locations and create extra lexical scopes or
344 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
345 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
346 // because that's what the SourceManager says)
347
Guy Benyei11169dd2012-12-18 14:30:41 +0000348 // Get absolute path name.
349 SourceManager &SM = CGM.getContext().getSourceManager();
350 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
351 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000352 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000353
354 // The main file name provided via the "-main-file-name" option contains just
355 // the file name itself with no path information. This file name may have had
356 // a relative path, so we look into the actual file entry for the main
357 // file to determine the real absolute path for the file.
358 std::string MainFileDir;
359 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000360 MainFileDir = remapDIPath(MainFile->getDir()->getName());
Yaron Keren9fb7e902013-10-21 20:07:37 +0000361 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000362 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
363 llvm::sys::path::append(MainFileDirSS, MainFileName);
364 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000365 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000366 }
367
Ed Masteda706022014-05-07 12:49:30 +0000368 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000369 const LangOptions &LO = CGM.getLangOpts();
370 if (LO.CPlusPlus) {
371 if (LO.ObjC1)
372 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
373 else
374 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
375 } else if (LO.ObjC1) {
376 LangTag = llvm::dwarf::DW_LANG_ObjC;
377 } else if (LO.C99) {
378 LangTag = llvm::dwarf::DW_LANG_C99;
379 } else {
380 LangTag = llvm::dwarf::DW_LANG_C89;
381 }
382
383 std::string Producer = getClangFullVersion();
384
385 // Figure out which version of the ObjC runtime we have.
386 unsigned RuntimeVers = 0;
387 if (LO.ObjC1)
388 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
389
390 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000391 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000392 TheCU = DBuilder.createCompileUnit(
Saleem Abdulrasool436256a2015-10-12 20:21:08 +0000393 LangTag, remapDIPath(MainFileName), remapDIPath(getCurrentDirname()),
394 Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000395 CGM.getCodeGenOpts().SplitDwarfFile,
Diego Novillo913690c2014-06-24 17:02:17 +0000396 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000397 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000398 : llvm::DIBuilder::FullDebug,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000399 0 /* DWOid */, DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000400}
401
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000402llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000403 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000404 StringRef BTName;
405 switch (BT->getKind()) {
406#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000407#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000408#include "clang/AST/BuiltinTypes.def"
409 case BuiltinType::Dependent:
410 llvm_unreachable("Unexpected builtin type");
411 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000412 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000414 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000415 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000416 if (!ClassTy)
417 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
418 "objc_class", TheCU,
419 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000420 return ClassTy;
421 case BuiltinType::ObjCId: {
422 // typedef struct objc_class *Class;
423 // typedef struct objc_object {
424 // Class isa;
425 // } *id;
426
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000427 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000428 return ObjTy;
429
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000430 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000431 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
432 "objc_class", TheCU,
433 getOrCreateMainFile(), 0);
434
435 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000436
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000437 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000438
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000439 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000440 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000441 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000442
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000443 DBuilder.replaceArrays(
444 ObjTy,
445 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
446 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 return ObjTy;
448 }
449 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000450 if (!SelTy)
451 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
452 "objc_selector", TheCU,
453 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 return SelTy;
455 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000456
457 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000459 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000460 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000461 OCLImage1dArrayDITy);
462 case BuiltinType::OCLImage1dBuffer:
463 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
464 OCLImage1dBufferDITy);
465 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000466 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000467 case BuiltinType::OCLImage2dArray:
468 return getOrCreateStructPtrType("opencl_image2d_array_t",
469 OCLImage2dArrayDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000470 case BuiltinType::OCLImage2dDepth:
471 return getOrCreateStructPtrType("opencl_image2d_depth_t",
472 OCLImage2dDepthDITy);
473 case BuiltinType::OCLImage2dArrayDepth:
474 return getOrCreateStructPtrType("opencl_image2d_array_depth_t",
475 OCLImage2dArrayDepthDITy);
476 case BuiltinType::OCLImage2dMSAA:
477 return getOrCreateStructPtrType("opencl_image2d_msaa_t",
478 OCLImage2dMSAADITy);
479 case BuiltinType::OCLImage2dArrayMSAA:
480 return getOrCreateStructPtrType("opencl_image2d_array_msaa_t",
481 OCLImage2dArrayMSAADITy);
482 case BuiltinType::OCLImage2dMSAADepth:
483 return getOrCreateStructPtrType("opencl_image2d_msaa_depth_t",
484 OCLImage2dMSAADepthDITy);
485 case BuiltinType::OCLImage2dArrayMSAADepth:
486 return getOrCreateStructPtrType("opencl_image2d_array_msaa_depth_t",
487 OCLImage2dArrayMSAADepthDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000488 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000489 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000490 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000491 return DBuilder.createBasicType(
492 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
493 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000494 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000495 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000496 case BuiltinType::OCLClkEvent:
497 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
498 case BuiltinType::OCLQueue:
499 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
500 case BuiltinType::OCLNDRange:
501 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
502 case BuiltinType::OCLReserveID:
503 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000504
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000506 case BuiltinType::Char_U:
507 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
508 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000509 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000510 case BuiltinType::SChar:
511 Encoding = llvm::dwarf::DW_ATE_signed_char;
512 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000514 case BuiltinType::Char32:
515 Encoding = llvm::dwarf::DW_ATE_UTF;
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 case BuiltinType::UShort:
518 case BuiltinType::UInt:
519 case BuiltinType::UInt128:
520 case BuiltinType::ULong:
521 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000522 case BuiltinType::ULongLong:
523 Encoding = llvm::dwarf::DW_ATE_unsigned;
524 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 case BuiltinType::Short:
526 case BuiltinType::Int:
527 case BuiltinType::Int128:
528 case BuiltinType::Long:
529 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000530 case BuiltinType::LongLong:
531 Encoding = llvm::dwarf::DW_ATE_signed;
532 break;
533 case BuiltinType::Bool:
534 Encoding = llvm::dwarf::DW_ATE_boolean;
535 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000536 case BuiltinType::Half:
537 case BuiltinType::Float:
538 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000539 case BuiltinType::Double:
540 Encoding = llvm::dwarf::DW_ATE_float;
541 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 }
543
544 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000545 case BuiltinType::Long:
546 BTName = "long int";
547 break;
548 case BuiltinType::LongLong:
549 BTName = "long long int";
550 break;
551 case BuiltinType::ULong:
552 BTName = "long unsigned int";
553 break;
554 case BuiltinType::ULongLong:
555 BTName = "long long unsigned int";
556 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000557 default:
558 BTName = BT->getName(CGM.getLangOpts());
559 break;
560 }
561 // Bit size, align and offset of the type.
562 uint64_t Size = CGM.getContext().getTypeSize(BT);
563 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000564 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000565}
566
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000567llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000569 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000570 if (Ty->isComplexIntegerType())
571 Encoding = llvm::dwarf::DW_ATE_lo_user;
572
573 uint64_t Size = CGM.getContext().getTypeSize(Ty);
574 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000575 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000576}
577
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000578llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
579 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 QualifierCollector Qc;
581 const Type *T = Qc.strip(Ty);
582
583 // Ignore these qualifiers for now.
584 Qc.removeObjCGCAttr();
585 Qc.removeAddressSpace();
586 Qc.removeObjCLifetime();
587
588 // We will create one Derived type for one qualifier and recurse to handle any
589 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000590 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000591 if (Qc.hasConst()) {
592 Tag = llvm::dwarf::DW_TAG_const_type;
593 Qc.removeConst();
594 } else if (Qc.hasVolatile()) {
595 Tag = llvm::dwarf::DW_TAG_volatile_type;
596 Qc.removeVolatile();
597 } else if (Qc.hasRestrict()) {
598 Tag = llvm::dwarf::DW_TAG_restrict_type;
599 Qc.removeRestrict();
600 } else {
601 assert(Qc.empty() && "Unknown type qualifier for debug info");
602 return getOrCreateType(QualType(T, 0), Unit);
603 }
604
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000605 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000606
607 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
608 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000609 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000610}
611
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000612llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
613 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000614
615 // The frontend treats 'id' as a typedef to an ObjCObjectType,
616 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
617 // debug info, we want to emit 'id' in both cases.
618 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000619 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000620
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000621 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
622 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000623}
624
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000625llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
626 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000627 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000628 Ty->getPointeeType(), Unit);
629}
630
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000631/// \return whether a C++ mangling exists for the type defined by TD.
632static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
633 switch (TheCU->getSourceLanguage()) {
634 case llvm::dwarf::DW_LANG_C_plus_plus:
635 return true;
636 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
637 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
638 default:
639 return false;
640 }
641}
642
Manman Rene0064d82013-08-29 23:19:58 +0000643/// In C++ mode, types have linkage, so we can rely on the ODR and
644/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000645static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
646 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000647 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000648 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000649 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000650
651 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000652 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000653
Manman Rene0064d82013-08-29 23:19:58 +0000654 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
655 if (CGM.getTarget().getCXXABI().isMicrosoft())
656 return FullName;
657
658 // TODO: This is using the RTTI name. Is there a better way to get
659 // a unique string for a type?
660 llvm::raw_svector_ostream Out(FullName);
661 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000662 return FullName;
663}
664
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000665/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000666static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
667 llvm::dwarf::Tag Tag;
668 if (RD->isStruct() || RD->isInterface())
669 Tag = llvm::dwarf::DW_TAG_structure_type;
670 else if (RD->isUnion())
671 Tag = llvm::dwarf::DW_TAG_union_type;
672 else {
673 // FIXME: This could be a struct type giving a default visibility different
674 // than C++ class type, but needs llvm metadata changes first.
675 assert(RD->isClass());
676 Tag = llvm::dwarf::DW_TAG_class_type;
677 }
678 return Tag;
679}
680
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000681llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000682CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000683 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000684 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000685 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
686 return cast<llvm::DICompositeType>(T);
687 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000688 unsigned Line = getLineNumber(RD->getLocation());
689 StringRef RDName = getClassName(RD);
690
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000691 uint64_t Size = 0;
692 uint64_t Align = 0;
693
694 const RecordDecl *D = RD->getDefinition();
695 if (D && D->isCompleteDefinition()) {
696 Size = CGM.getContext().getTypeSize(Ty);
697 Align = CGM.getContext().getTypeAlign(Ty);
698 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000699
700 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000701 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000702 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000703 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000704 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000705 ReplaceMap.emplace_back(
706 std::piecewise_construct, std::make_tuple(Ty),
707 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000708 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000709}
710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000711llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000712 const Type *Ty,
713 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000714 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000715 // Bit size, align and offset of the type.
716 // Size is always the size of a pointer. We can't use getTypeSize here
717 // because that does not return the correct value for references.
718 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000719 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000720 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
721
Keno Fischer87842f32015-11-16 09:04:13 +0000722 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
723 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
724 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit),
725 Size, Align);
726 else
727 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
728 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000729}
730
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000731llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
732 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000733 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000734 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000735 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
736 TheCU, getOrCreateMainFile(), 0);
737 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
738 Cache = DBuilder.createPointerType(Cache, Size);
739 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000740}
741
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000742llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
743 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000744 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 QualType FType;
746 uint64_t FieldSize, FieldOffset;
747 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000748 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000749
750 FieldOffset = 0;
751 FType = CGM.getContext().UnsignedLongTy;
752 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
753 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
754
755 Elements = DBuilder.getOrCreateArray(EltTys);
756 EltTys.clear();
757
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000758 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000759 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000760
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000761 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000762 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000763 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000764
765 // Bit size, align and offset of the type.
766 uint64_t Size = CGM.getContext().getTypeSize(Ty);
767
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000768 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000769
770 FieldOffset = 0;
771 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
772 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
773 FType = CGM.getContext().IntTy;
774 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
775 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000776 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000777 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
778
779 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000780 FieldSize = CGM.getContext().getTypeSize(Ty);
781 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000782 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000783 FieldSize, FieldAlign, FieldOffset,
784 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000785
786 FieldOffset += FieldSize;
787 Elements = DBuilder.getOrCreateArray(EltTys);
788
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000789 // The __block_literal_generic structs are marked with a special
790 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
791 // the debugger needs to know about. To allow type uniquing, emit
792 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000793 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000794 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000795 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000796
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000797 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000798}
799
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000800llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
801 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000802 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000804
805 SmallString<128> NS;
806 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000807 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
808 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000809
810 TemplateSpecializationType::PrintTemplateArgumentList(
811 OS, Ty->getArgs(), Ty->getNumArgs(),
812 CGM.getContext().getPrintingPolicy());
813
Eric Christophere7b87e52014-10-26 23:40:33 +0000814 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
815 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000816
817 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000818 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
819 getLineNumber(Loc),
820 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000821}
822
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000823llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
824 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 // We don't set size information, but do specify where the typedef was
826 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000827 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000828
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000829 // Typedefs are derived from some other type.
830 return DBuilder.createTypedef(
831 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
832 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000833 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000834}
835
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000836llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
837 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000838 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000839
840 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000841 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000842
843 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000844 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 if (isa<FunctionNoProtoType>(Ty))
846 EltTys.push_back(DBuilder.createUnspecifiedParameter());
847 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000848 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
849 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000850 if (FPT->isVariadic())
851 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 }
853
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000854 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +0000855 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +0000856}
857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000858/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000859/// As an optimization, return 0 if the access specifier equals the
860/// default for the containing type.
861static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
862 AccessSpecifier Default = clang::AS_none;
863 if (RD && RD->isClass())
864 Default = clang::AS_private;
865 else if (RD && (RD->isStruct() || RD->isUnion()))
866 Default = clang::AS_public;
867
868 if (Access == Default)
869 return 0;
870
Eric Christophere7b87e52014-10-26 23:40:33 +0000871 switch (Access) {
872 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000873 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000874 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000876 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000878 case clang::AS_none:
879 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000880 }
881 llvm_unreachable("unexpected access enumerator");
882}
Guy Benyei11169dd2012-12-18 14:30:41 +0000883
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000884llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000885 StringRef name, QualType type, uint64_t sizeInBitsOverride,
886 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000887 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
888 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000889
890 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000891 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000892 unsigned line = getLineNumber(loc);
893
David Majnemer34b57492014-07-30 01:30:47 +0000894 uint64_t SizeInBits = 0;
895 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000896 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000897 TypeInfo TI = CGM.getContext().getTypeInfo(type);
898 SizeInBits = TI.Width;
899 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000900
901 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000902 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000903 }
904
Adrian Prantl21361fb2014-08-29 22:44:27 +0000905 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000906 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
907 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000908}
909
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000910void CGDebugInfo::CollectRecordLambdaFields(
911 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000912 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000913 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
914 // has the name and the location of the variable so we should iterate over
915 // both concurrently.
916 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
917 RecordDecl::field_iterator Field = CXXDecl->field_begin();
918 unsigned fieldno = 0;
919 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000920 E = CXXDecl->captures_end();
921 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000922 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000923 if (C.capturesVariable()) {
924 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000925 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000926 StringRef VName = V->getName();
927 uint64_t SizeInBitsOverride = 0;
928 if (Field->isBitField()) {
929 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
930 assert(SizeInBitsOverride && "found named 0-width bitfield");
931 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000932 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000933 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
934 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
935 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000936 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000937 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000938 // TODO: Need to handle 'this' in some way by probably renaming the
939 // this of the lambda class and having a field member of 'this' or
940 // by using AT_object_pointer for the function and having that be
941 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000942 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000943 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000944 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000945 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000946 "this", type, 0, f->getLocation(), f->getAccess(),
947 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000948
949 elements.push_back(fieldType);
950 }
951 }
952}
953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954llvm::DIDerivedType *
955CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000956 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000957 // Create the descriptor for the static variable, with or without
958 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000959 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000960 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
961 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000962
Eric Christopher91a31902013-01-16 01:22:32 +0000963 unsigned LineNumber = getLineNumber(Var->getLocation());
964 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000965 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000966 if (Var->getInit()) {
967 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000968 if (Value) {
969 if (Value->isInt())
970 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
971 if (Value->isFloat())
972 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
973 }
Eric Christopher91a31902013-01-16 01:22:32 +0000974 }
975
Adrian Prantl21361fb2014-08-29 22:44:27 +0000976 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000977 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000978 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000979 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000980 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000981}
982
Eric Christophere7b87e52014-10-26 23:40:33 +0000983void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000984 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
985 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000986 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000987 StringRef name = field->getName();
988 QualType type = field->getType();
989
990 // Ignore unnamed fields unless they're anonymous structs/unions.
991 if (name.empty() && !type->isRecordType())
992 return;
993
994 uint64_t SizeInBitsOverride = 0;
995 if (field->isBitField()) {
996 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
997 assert(SizeInBitsOverride && "found named 0-width bitfield");
998 }
999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001001 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
1002 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +00001003
1004 elements.push_back(fieldType);
1005}
1006
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001007void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001008 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001009 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001010 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
1012
Eric Christopher91a31902013-01-16 01:22:32 +00001013 if (CXXDecl && CXXDecl->isLambda())
1014 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
1015 else {
1016 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001017
Eric Christopher91a31902013-01-16 01:22:32 +00001018 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +00001019 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +00001020
Eric Christopher91a31902013-01-16 01:22:32 +00001021 // Static and non-static members should appear in the same order as
1022 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001023 for (const auto *I : record->decls())
1024 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001025 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001026 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001027 if (MI != StaticDataMemberCache.end()) {
1028 assert(MI->second &&
1029 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001030 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001031 } else {
1032 auto Field = CreateRecordStaticField(V, RecordTy, record);
1033 elements.push_back(Field);
1034 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001035 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001036 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1037 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001038
1039 // Bump field number for next field.
1040 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001041 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001042 }
1043}
1044
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001045llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001046CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001047 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001048 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001049 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001050 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001051 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001052 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1053 Func, Unit);
1054}
David Blaikie2aaf0652013-01-07 22:24:59 +00001055
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001056llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1057 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001059 llvm::DITypeRefArray Args(
1060 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001061 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001062 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001063
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001064 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001065
1066 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001067 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001068
David Blaikie2aaf0652013-01-07 22:24:59 +00001069 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001070 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001071 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1072 // Create pointer type directly in this case.
1073 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1074 QualType PointeeTy = ThisPtrTy->getPointeeType();
1075 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001076 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001077 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1079 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001080 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001081 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001082 // TODO: This and the artificial type below are misleading, the
1083 // types aren't artificial the argument is, but the current
1084 // metadata doesn't represent that.
1085 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1086 Elts.push_back(ThisPtrType);
1087 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001088 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001089 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001090 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1091 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001092 }
1093
1094 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001095 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1096 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001097
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001098 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001099
Adrian Prantl0630eb72013-12-18 21:48:18 +00001100 unsigned Flags = 0;
1101 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001103 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001105
Eric Christopher28a6db52015-10-15 06:56:08 +00001106 return DBuilder.createSubroutineType(EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001107}
1108
Eric Christopherb2a008c2013-05-16 00:45:12 +00001109/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001110/// inside a function.
1111static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1112 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1113 return isFunctionLocalClass(NRD);
1114 if (isa<FunctionDecl>(RD->getDeclContext()))
1115 return true;
1116 return false;
1117}
1118
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1120 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001121 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001122 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
1127 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1128 // make sense to give a single ctor/dtor a linkage name.
1129 StringRef MethodLinkageName;
1130 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1131 MethodLinkageName = CGM.getMangledName(Method);
1132
1133 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001134 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001135 unsigned MethodLine = 0;
1136 if (!Method->isImplicit()) {
1137 MethodDefUnit = getOrCreateFile(Method->getLocation());
1138 MethodLine = getLineNumber(Method->getLocation());
1139 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001140
1141 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001142 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001143 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001145
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 if (Method->isVirtual()) {
1147 if (Method->isPure())
1148 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1149 else
1150 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001151
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 // It doesn't make sense to give a virtual destructor a vtable index,
1153 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001154 // FIXME: Add proper support for debug info for virtual calls in
1155 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1156 // lookup if we have multiple or virtual inheritance.
1157 if (!isa<CXXDestructorDecl>(Method) &&
1158 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001159 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001160 ContainingType = RecordTy;
1161 }
1162
1163 unsigned Flags = 0;
1164 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001165 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001166 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001167 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1168 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001169 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001170 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001171 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001172 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001173 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 }
1175 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001176 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001177 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001178 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001179 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001180 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001181
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001182 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1183 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001184 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1185 MethodTy, /*isLocalToUnit=*/false,
1186 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00001187 CGM.getLangOpts().Optimize, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001188
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001189 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001190
1191 return SP;
1192}
1193
Eric Christophere7b87e52014-10-26 23:40:33 +00001194void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001195 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1196 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001197
1198 // Since we want more than just the individual member decls if we
1199 // have templated functions iterate over every declaration to gather
1200 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001201 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001202 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1203 // If the member is implicit, don't add it to the member list. This avoids
1204 // the member being added to type units by LLVM, while still allowing it
1205 // to be emitted into the type declaration/reference inside the compile
1206 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001207 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001208 // FIXME: Handle Using(Shadow?)Decls here to create
1209 // DW_TAG_imported_declarations inside the class for base decls brought into
1210 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1211 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1212 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001213 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001214 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001215
1216 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1217 continue;
1218
David Blaikiefd580722014-10-06 05:18:55 +00001219 // Reuse the existing member function declaration if it exists.
1220 // It may be associated with the declaration of the type & should be
1221 // reused as we're building the definition.
1222 //
1223 // This situation can arise in the vtable-based debug info reduction where
1224 // implicit members are emitted in a non-vtable TU.
1225 auto MI = SPCache.find(Method->getCanonicalDecl());
1226 EltTys.push_back(MI == SPCache.end()
1227 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001228 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001229 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001230}
Guy Benyei11169dd2012-12-18 14:30:41 +00001231
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001232void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001233 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001236 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 unsigned BFlags = 0;
1238 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001239
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001241 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001242
Aaron Ballman574705e2014-03-13 15:41:46 +00001243 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001244 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1245 // virtual base offset offset is -ve. The code generator emits dwarf
1246 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001247 BaseOffset = 0 - CGM.getItaniumVTableContext()
1248 .getVirtualBaseOffsetOffset(RD, Base)
1249 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001250 } else {
1251 // In the MS ABI, store the vbtable offset, which is analogous to the
1252 // vbase offset offset in Itanium.
1253 BaseOffset =
1254 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1255 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001256 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001257 } else
1258 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1259 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1260 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001261
Adrian Prantl21361fb2014-08-29 22:44:27 +00001262 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001263 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001264 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001265 EltTys.push_back(DTy);
1266 }
1267}
1268
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001270CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1271 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001272 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001273 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001274 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1275 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001276 StringRef Name;
1277 if (TPList)
1278 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001279 switch (TA.getKind()) {
1280 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001281 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001282 TemplateParams.push_back(
1283 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001284 } break;
1285 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001286 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001287 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1288 TheCU, Name, TTy,
1289 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001290 } break;
1291 case TemplateArgument::Declaration: {
1292 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001293 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001294 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001295 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001296 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001297 // Variable pointer template parameters have a value that is the address
1298 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001299 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001300 V = CGM.GetAddrOfGlobalVar(VD);
1301 // Member function pointers have special support for building them, though
1302 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001303 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001304 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001305 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001306 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001307 // Member data pointers have special handling too to compute the fixed
1308 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001309 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001310 // These five lines (& possibly the above member function pointer
1311 // handling) might be able to be refactored to use similar code in
1312 // CodeGenModule::getMemberPointerConstant
1313 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1314 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001315 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001316 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001317 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001318 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1319 TheCU, Name, TTy,
1320 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001321 } break;
1322 case TemplateArgument::NullPtr: {
1323 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001324 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001325 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001326 // Special case member data pointer null values since they're actually -1
1327 // instead of zero.
1328 if (const MemberPointerType *MPT =
1329 dyn_cast<MemberPointerType>(T.getTypePtr()))
1330 // But treat member function pointers as simple zero integers because
1331 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1332 // CodeGen grows handling for values of non-null member function
1333 // pointers then perhaps we could remove this special case and rely on
1334 // EmitNullMemberPointer for member function pointers.
1335 if (MPT->isMemberDataPointer())
1336 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1337 if (!V)
1338 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001339 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1340 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001341 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001342 case TemplateArgument::Template:
1343 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001344 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001345 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1346 break;
1347 case TemplateArgument::Pack:
1348 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1349 TheCU, Name, nullptr,
1350 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1351 break;
David Majnemer5559d472013-08-24 08:21:10 +00001352 case TemplateArgument::Expression: {
1353 const Expr *E = TA.getAsExpr();
1354 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001355 if (E->isGLValue())
1356 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001357 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001358 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001359 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001360 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1361 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001362 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001363 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001364 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001365 case TemplateArgument::Null:
1366 llvm_unreachable(
1367 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 }
1369 }
1370 return DBuilder.getOrCreateArray(TemplateParams);
1371}
1372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001373llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001374CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001375 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001376 if (FD->getTemplatedKind() ==
1377 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001378 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1379 ->getTemplate()
1380 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001381 return CollectTemplateParams(
1382 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001384 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001385}
1386
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1388 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001389 // Always get the full list of parameters, not just the ones from
1390 // the specialization.
1391 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001392 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001393 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001394 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001395}
1396
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001397llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001398 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001399 return VTablePtrType;
1400
1401 ASTContext &Context = CGM.getContext();
1402
1403 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001404 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001405 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
Eric Christopher28a6db52015-10-15 06:56:08 +00001406 llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001408 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001409 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1411 return VTablePtrType;
1412}
1413
Guy Benyei11169dd2012-12-18 14:30:41 +00001414StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001415 // Copy the gdb compatible name on the side and use its reference.
1416 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001417}
1418
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001419void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001420 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1422
1423 // If there is a primary base then it will hold vtable info.
1424 if (RL.getPrimaryBase())
1425 return;
1426
1427 // If this class is not dynamic then there is not any vtable info to collect.
1428 if (!RD->isDynamicClass())
1429 return;
1430
1431 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001432 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001433 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001434 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001435 EltTys.push_back(VPTR);
1436}
1437
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001438llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001439 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001440 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001441 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 return T;
1443}
1444
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001445llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001446 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001447 return getOrCreateStandaloneType(D, Loc);
1448}
1449
1450llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1451 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001452 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001453 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001454 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001455 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001456
1457 // Composite types with UIDs were already retained by DIBuilder
1458 // because they are only referenced by name in the IR.
1459 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1460 if (!CTy->getIdentifier().empty())
1461 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001462 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001463 return T;
1464}
1465
David Blaikie483a9da2014-05-06 18:35:21 +00001466void CGDebugInfo::completeType(const EnumDecl *ED) {
1467 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1468 return;
1469 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001470 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001471 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001472 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001473 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001474 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001475 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001476 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001477}
1478
David Blaikieb2e86eb2013-08-15 20:49:17 +00001479void CGDebugInfo::completeType(const RecordDecl *RD) {
1480 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1481 !CGM.getLangOpts().CPlusPlus)
1482 completeRequiredType(RD);
1483}
1484
1485void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001486 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1487 return;
1488
David Blaikie6943dea2013-08-20 01:28:15 +00001489 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1490 if (CXXDecl->isDynamicClass())
1491 return;
1492
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001493 if (DebugTypeExtRefs && RD->isFromASTFile())
1494 return;
1495
David Blaikieb2e86eb2013-08-15 20:49:17 +00001496 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001497 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001498 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001499 completeClassData(RD);
1500}
1501
1502void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1503 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001504 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001505 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001506 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001507 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001509 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001510 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001511 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001512 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001513}
1514
David Blaikie0e716b42014-03-03 23:48:23 +00001515static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1516 CXXRecordDecl::method_iterator End) {
1517 for (; I != End; ++I)
1518 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001519 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1520 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001521 return true;
1522 return false;
1523}
1524
1525static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001526 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001527 const RecordDecl *RD,
1528 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001529 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001530 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001531 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001532
David Blaikie0e716b42014-03-03 23:48:23 +00001533 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1534 return false;
1535
1536 if (!LangOpts.CPlusPlus)
1537 return false;
1538
1539 if (!RD->isCompleteDefinitionRequired())
1540 return true;
1541
1542 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1543
1544 if (!CXXDecl)
1545 return false;
1546
1547 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1548 return true;
1549
1550 TemplateSpecializationKind Spec = TSK_Undeclared;
1551 if (const ClassTemplateSpecializationDecl *SD =
1552 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1553 Spec = SD->getSpecializationKind();
1554
1555 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1556 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1557 CXXDecl->method_end()))
1558 return true;
1559
1560 return false;
1561}
1562
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001563llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001564 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001565 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001566 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1567 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001568 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001569 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001570 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001571 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
David Blaikieb2e86eb2013-08-15 20:49:17 +00001573 return CreateTypeDefinition(Ty);
1574}
1575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001576llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001577 RecordDecl *RD = Ty->getDecl();
1578
Guy Benyei11169dd2012-12-18 14:30:41 +00001579 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001580 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001581
1582 // Records and classes and unions can all be recursive. To handle them, we
1583 // first generate a debug descriptor for the struct as a forward declaration.
1584 // Then (if it is a definition) we go through and get debug info for all of
1585 // its members. Finally, we create a descriptor for the complete type (which
1586 // may refer to the forward decl if the struct is recursive) and replace all
1587 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001588 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001589
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001590 const RecordDecl *D = RD->getDefinition();
1591 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 return FwdDecl;
1593
David Blaikieadfbf992013-08-18 16:55:33 +00001594 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1595 CollectContainingType(CXXDecl, FwdDecl);
1596
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001598 LexicalBlockStack.emplace_back(&*FwdDecl);
1599 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001600
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001602 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001603 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001604
1605 // Note: The split of CXXDecl information here is intentional, the
1606 // gdb tests will depend on a certain ordering at printout. The debug
1607 // information offsets are still correct if we merge them all together
1608 // though.
1609 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1610 if (CXXDecl) {
1611 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1612 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1613 }
1614
Eric Christopher91a31902013-01-16 01:22:32 +00001615 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001616 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001617 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001618 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001619
1620 LexicalBlockStack.pop_back();
1621 RegionMap.erase(Ty->getDecl());
1622
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001623 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001624 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001625
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001626 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001627 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001628 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001629
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001630 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001631 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001632}
1633
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001634llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1635 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001636 // Ignore protocols.
1637 return getOrCreateType(Ty->getBaseType(), Unit);
1638}
1639
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001640/// \return true if Getter has the default name for the property PD.
1641static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1642 const ObjCMethodDecl *Getter) {
1643 assert(PD);
1644 if (!Getter)
1645 return true;
1646
1647 assert(Getter->getDeclName().isObjCZeroArgSelector());
1648 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001649 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001650}
1651
1652/// \return true if Setter has the default name for the property PD.
1653static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1654 const ObjCMethodDecl *Setter) {
1655 assert(PD);
1656 if (!Setter)
1657 return true;
1658
1659 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001660 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001661 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001662}
1663
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001664llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1665 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001666 ObjCInterfaceDecl *ID = Ty->getDecl();
1667 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001668 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001669
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001670 // Return a forward declaration if this type was imported from a clang module.
1671 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1672 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1673 ID->getName(),
1674 getDeclContextDescriptor(ID), Unit, 0);
1675
Guy Benyei11169dd2012-12-18 14:30:41 +00001676 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001678 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001679 auto RuntimeLang =
1680 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
1682 // If this is just a forward declaration return a special forward-declaration
1683 // debug type since we won't be able to lay out the entire type.
1684 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001685 if (!Def || !Def->getImplementation()) {
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001686 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001687 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
Adrian Prantl42ce2d32015-10-01 16:57:02 +00001688 llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
1689 DefUnit, Line, RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001690 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001691 return FwdDecl;
1692 }
1693
David Blaikieef8a9512014-05-05 23:23:53 +00001694 return CreateTypeDefinition(Ty, Unit);
1695}
1696
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001697llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001698CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1699 bool CreateSkeletonCU) {
Adrian Prantleb66a262015-09-24 16:10:04 +00001700 // Use the Module pointer as the key into the cache. This is a
1701 // nullptr if the "Module" is a PCH, which is safe because we don't
1702 // support chained PCH debug info, so there can only be a single PCH.
1703 const Module *M = Mod.getModuleOrNull();
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001704 auto ModRef = ModuleCache.find(M);
1705 if (ModRef != ModuleCache.end())
1706 return cast<llvm::DIModule>(ModRef->second);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001707
1708 // Macro definitions that were defined with "-D" on the command line.
1709 SmallString<128> ConfigMacros;
1710 {
1711 llvm::raw_svector_ostream OS(ConfigMacros);
1712 const auto &PPOpts = CGM.getPreprocessorOpts();
1713 unsigned I = 0;
1714 // Translate the macro definitions back into a commmand line.
1715 for (auto &M : PPOpts.Macros) {
1716 if (++I > 1)
1717 OS << " ";
1718 const std::string &Macro = M.first;
1719 bool Undef = M.second;
1720 OS << "\"-" << (Undef ? 'U' : 'D');
1721 for (char c : Macro)
1722 switch (c) {
1723 case '\\' : OS << "\\\\"; break;
1724 case '"' : OS << "\\\""; break;
1725 default: OS << c;
1726 }
1727 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001728 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001729 }
Adrian Prantl66689202015-09-18 23:01:45 +00001730
Adrian Prantl835e6632015-09-24 16:10:10 +00001731 bool IsRootModule = M ? !M->Parent : true;
1732 if (CreateSkeletonCU && IsRootModule) {
Adrian Prantl66689202015-09-18 23:01:45 +00001733 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl835e6632015-09-24 16:10:10 +00001734 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.getModuleName(),
Adrian Prantl6083b082015-09-24 16:10:00 +00001735 Mod.getPath(), TheCU->getProducer(), true,
1736 StringRef(), 0, Mod.getASTFile(),
1737 llvm::DIBuilder::FullDebug, Mod.getSignature());
Adrian Prantl66689202015-09-18 23:01:45 +00001738 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001739 }
Adrian Prantl835e6632015-09-24 16:10:10 +00001740 llvm::DIModule *Parent =
1741 IsRootModule ? nullptr
1742 : getOrCreateModuleRef(
1743 ExternalASTSource::ASTSourceDescriptor(*M->Parent),
1744 CreateSkeletonCU);
Adrian Prantleb66a262015-09-24 16:10:04 +00001745 llvm::DIModule *DIMod =
Adrian Prantl835e6632015-09-24 16:10:10 +00001746 DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
1747 Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl9e8ea352015-09-29 20:44:46 +00001748 ModuleCache[M].reset(DIMod);
Adrian Prantleb66a262015-09-24 16:10:04 +00001749 return DIMod;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001750}
1751
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001752llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1753 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001754 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001755 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001756 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001757 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001758
1759 // Bit size, align and offset of the type.
1760 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1761 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1762
1763 unsigned Flags = 0;
1764 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001765 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001766
Adrian Prantlfd696112015-10-01 00:48:51 +00001767 llvm::DIScope *Mod = getParentModuleOrNull(ID);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001768 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Adrian Prantlfd696112015-10-01 00:48:51 +00001769 Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
1770 nullptr, llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001771
David Blaikieef8a9512014-05-05 23:23:53 +00001772 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001773 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001774
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001775 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001776 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001777 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001778
1779 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001780 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001781
1782 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1783 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001784 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001785 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001786 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001787 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001788
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001789 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001790 EltTys.push_back(InhTag);
1791 }
1792
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001793 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001794 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001796 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001797 unsigned PLine = getLineNumber(Loc);
1798 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1799 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001800 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1801 PD->getName(), PUnit, PLine,
1802 hasDefaultGetterName(PD, Getter) ? ""
1803 : getSelectorName(PD->getGetterName()),
1804 hasDefaultSetterName(PD, Setter) ? ""
1805 : getSelectorName(PD->getSetterName()),
1806 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001807 EltTys.push_back(PropertyNode);
1808 }
1809
1810 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1811 unsigned FieldNo = 0;
1812 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1813 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001814 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001815 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001816 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001817
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 StringRef FieldName = Field->getName();
1819
1820 // Ignore unnamed fields.
1821 if (FieldName.empty())
1822 continue;
1823
1824 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001825 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 unsigned FieldLine = getLineNumber(Field->getLocation());
1827 QualType FType = Field->getType();
1828 uint64_t FieldSize = 0;
1829 unsigned FieldAlign = 0;
1830
1831 if (!FType->isIncompleteArrayType()) {
1832
1833 // Bit size, align and offset of the type.
1834 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001835 ? Field->getBitWidthValue(CGM.getContext())
1836 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001837 FieldAlign = CGM.getContext().getTypeAlign(FType);
1838 }
1839
1840 uint64_t FieldOffset;
1841 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1842 // We don't know the runtime offset of an ivar if we're using the
1843 // non-fragile ABI. For bitfields, use the bit offset into the first
1844 // byte of storage of the bitfield. For other fields, use zero.
1845 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001846 FieldOffset =
1847 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 FieldOffset %= CGM.getContext().getCharWidth();
1849 } else {
1850 FieldOffset = 0;
1851 }
1852 } else {
1853 FieldOffset = RL.getFieldOffset(FieldNo);
1854 }
1855
1856 unsigned Flags = 0;
1857 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001858 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001859 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001860 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001861 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001862 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001863
Craig Topper8a13c412014-05-21 05:09:00 +00001864 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001866 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001867 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001869 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001870 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001871 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1873 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001874 PropertyNode = DBuilder.createObjCProperty(
1875 PD->getName(), PUnit, PLine,
1876 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1877 PD->getGetterName()),
1878 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1879 PD->getSetterName()),
1880 PD->getPropertyAttributes(),
1881 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 }
1883 }
1884 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001885 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1886 FieldSize, FieldAlign, FieldOffset, Flags,
1887 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 EltTys.push_back(FieldTy);
1889 }
1890
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001891 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001892 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001893
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001895 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001896}
1897
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001898llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1899 llvm::DIFile *Unit) {
1900 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001901 int64_t Count = Ty->getNumElements();
1902 if (Count == 0)
1903 // If number of elements are not known then this is an unbounded array.
1904 // Use Count == -1 to express such arrays.
1905 Count = -1;
1906
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001907 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001908 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001909
1910 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1911 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1912
1913 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1914}
1915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001917 uint64_t Size;
1918 uint64_t Align;
1919
1920 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1921 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1922 Size = 0;
1923 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001924 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001925 } else if (Ty->isIncompleteArrayType()) {
1926 Size = 0;
1927 if (Ty->getElementType()->isIncompleteType())
1928 Align = 0;
1929 else
1930 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001931 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 Size = 0;
1933 Align = 0;
1934 } else {
1935 // Size and align of the whole array, not the element type.
1936 Size = CGM.getContext().getTypeSize(Ty);
1937 Align = CGM.getContext().getTypeAlign(Ty);
1938 }
1939
1940 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1941 // interior arrays, do we care? Why aren't nested arrays represented the
1942 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001943 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001944 QualType EltTy(Ty, 0);
1945 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1946 // If the number of elements is known, then count is that number. Otherwise,
1947 // it's -1. This allows us to represent a subrange with an array of 0
1948 // elements, like this:
1949 //
1950 // struct foo {
1951 // int x[0];
1952 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001953 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001954 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1955 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001956
Guy Benyei11169dd2012-12-18 14:30:41 +00001957 // FIXME: Verify this is right for VLAs.
1958 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1959 EltTy = Ty->getElementType();
1960 }
1961
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001962 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001963
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001964 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1965 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001966}
1967
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1969 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001970 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1971 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001972}
1973
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001974llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1975 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001976 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1977 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001978}
1979
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001980llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1981 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001982 uint64_t Size =
1983 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001984 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001985 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001986 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001987 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001988
1989 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001990 Ty->getPointeeType()->getAs<FunctionProtoType>();
1991 return DBuilder.createMemberPointerType(
1992 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1993 Ty->getClass(), FPT->getTypeQuals())),
1994 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001995 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001996}
1997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001998llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 // Ignore the atomic wrapping
2000 // FIXME: What is the correct representation?
2001 return getOrCreateType(Ty->getValueType(), U);
2002}
2003
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002004llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00002005 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002006
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 uint64_t Size = 0;
2008 uint64_t Align = 0;
2009 if (!ED->getTypeForDecl()->isIncompleteType()) {
2010 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2011 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2012 }
2013
Manman Rene0064d82013-08-29 23:19:58 +00002014 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2015
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002016 bool isImportedFromModule =
2017 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
2018
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 // If this is just a forward declaration, construct an appropriately
2020 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002021 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002022 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002023 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 unsigned Line = getLineNumber(ED->getLocation());
2025 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002026 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00002027 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002028 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002029 ReplaceMap.emplace_back(
2030 std::piecewise_construct, std::make_tuple(Ty),
2031 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002032 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 }
2034
David Blaikie483a9da2014-05-06 18:35:21 +00002035 return CreateTypeDefinition(Ty);
2036}
2037
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002038llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002039 const EnumDecl *ED = Ty->getDecl();
2040 uint64_t Size = 0;
2041 uint64_t Align = 0;
2042 if (!ED->getTypeForDecl()->isIncompleteType()) {
2043 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2044 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2045 }
2046
2047 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2048
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002049 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002050 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002051 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002052 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002053 Enumerators.push_back(DBuilder.createEnumerator(
2054 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 }
2056
2057 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002058 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002059
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002060 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002062 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002063 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002064 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2065 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2066 Line, Size, Align, EltArray, ClassTy,
2067 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002068}
2069
David Blaikie05491062013-01-21 04:37:12 +00002070static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2071 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002072 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002073 Qualifiers InnerQuals = T.getLocalQualifiers();
2074 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2075 // that is already there.
2076 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2077 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 QualType LastT = T;
2079 switch (T->getTypeClass()) {
2080 default:
David Blaikie05491062013-01-21 04:37:12 +00002081 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002082 case Type::TemplateSpecialization: {
2083 const auto *Spec = cast<TemplateSpecializationType>(T);
2084 if (Spec->isTypeAlias())
2085 return C.getQualifiedType(T.getTypePtr(), Quals);
2086 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002087 break;
2088 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 case Type::TypeOfExpr:
2090 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2091 break;
2092 case Type::TypeOf:
2093 T = cast<TypeOfType>(T)->getUnderlyingType();
2094 break;
2095 case Type::Decltype:
2096 T = cast<DecltypeType>(T)->getUnderlyingType();
2097 break;
2098 case Type::UnaryTransform:
2099 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2100 break;
2101 case Type::Attributed:
2102 T = cast<AttributedType>(T)->getEquivalentType();
2103 break;
2104 case Type::Elaborated:
2105 T = cast<ElaboratedType>(T)->getNamedType();
2106 break;
2107 case Type::Paren:
2108 T = cast<ParenType>(T)->getInnerType();
2109 break;
David Blaikie05491062013-01-21 04:37:12 +00002110 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002111 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002112 break;
2113 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002114 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002115 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002116 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002117 break;
2118 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002119
Guy Benyei11169dd2012-12-18 14:30:41 +00002120 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002121 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002122 } while (true);
2123}
2124
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002125llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002126
2127 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002128 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002129
David Blaikief427b002014-05-06 03:42:01 +00002130 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002131 if (it != TypeCache.end()) {
2132 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002133 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002134 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 }
2136
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002137 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002138}
2139
David Blaikie0e716b42014-03-03 23:48:23 +00002140void CGDebugInfo::completeTemplateDefinition(
2141 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002142 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2143 return;
2144
David Blaikie0e716b42014-03-03 23:48:23 +00002145 completeClassData(&SD);
2146 // In case this type has no member function definitions being emitted, ensure
2147 // it is retained
2148 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2149}
2150
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002151llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002152 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002153 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002154
2155 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002156 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002157
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002158 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 return T;
2160
Adrian Prantlca844182015-09-11 17:23:03 +00002161 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002162 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002163
2164 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002165 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002166
Guy Benyei11169dd2012-12-18 14:30:41 +00002167 return Res;
2168}
2169
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002170llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl335f5c72015-10-02 17:36:14 +00002171 // A forward declaration inside a module header does not belong to the module.
2172 if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition())
2173 return nullptr;
Adrian Prantl85d938a2015-09-21 17:48:37 +00002174 if (DebugTypeExtRefs && D->isFromASTFile()) {
2175 // Record a reference to an imported clang module or precompiled header.
2176 auto *Reader = CGM.getContext().getExternalSource();
2177 auto Idx = D->getOwningModuleID();
2178 auto Info = Reader->getSourceDescriptor(Idx);
2179 if (Info)
2180 return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true);
2181 } else if (ClangModuleMap) {
Adrian Prantl9402cef2015-09-20 16:51:35 +00002182 // We are building a clang module or a precompiled header.
2183 //
2184 // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies
2185 // and it wouldn't be necessary to specify the parent scope
2186 // because the type is already unique by definition (it would look
2187 // like the output of -fno-standalone-debug). On the other hand,
2188 // the parent scope helps a consumer to quickly locate the object
2189 // file where the type's definition is located, so it might be
2190 // best to make this behavior a command line or debugger tuning
2191 // option.
2192 FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager());
2193 if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) {
2194 auto Info = ExternalASTSource::ASTSourceDescriptor(*M);
2195 return getOrCreateModuleRef(Info, /*SkeletonCU=*/false);
2196 }
2197 }
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002198
Adrian Prantl9402cef2015-09-20 16:51:35 +00002199 return nullptr;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002200}
2201
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002202llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002203 // Handle qualifiers, which recursively handles what they refer to.
2204 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002205 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002206
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 // Work out details of type.
2208 switch (Ty->getTypeClass()) {
2209#define TYPE(Class, Base)
2210#define ABSTRACT_TYPE(Class, Base)
2211#define NON_CANONICAL_TYPE(Class, Base)
2212#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2213#include "clang/AST/TypeNodes.def"
2214 llvm_unreachable("Dependent types cannot show up in debug information");
2215
2216 case Type::ExtVector:
2217 case Type::Vector:
2218 return CreateType(cast<VectorType>(Ty), Unit);
2219 case Type::ObjCObjectPointer:
2220 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2221 case Type::ObjCObject:
2222 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2223 case Type::ObjCInterface:
2224 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2225 case Type::Builtin:
2226 return CreateType(cast<BuiltinType>(Ty));
2227 case Type::Complex:
2228 return CreateType(cast<ComplexType>(Ty));
2229 case Type::Pointer:
2230 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002231 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002232 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002233 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002234 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002235 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 case Type::BlockPointer:
2237 return CreateType(cast<BlockPointerType>(Ty), Unit);
2238 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002239 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002240 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002241 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002242 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002243 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 case Type::FunctionProto:
2245 case Type::FunctionNoProto:
2246 return CreateType(cast<FunctionType>(Ty), Unit);
2247 case Type::ConstantArray:
2248 case Type::VariableArray:
2249 case Type::IncompleteArray:
2250 return CreateType(cast<ArrayType>(Ty), Unit);
2251
2252 case Type::LValueReference:
2253 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2254 case Type::RValueReference:
2255 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2256
2257 case Type::MemberPointer:
2258 return CreateType(cast<MemberPointerType>(Ty), Unit);
2259
2260 case Type::Atomic:
2261 return CreateType(cast<AtomicType>(Ty), Unit);
2262
Guy Benyei11169dd2012-12-18 14:30:41 +00002263 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002264 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2265
David Blaikie42edade2014-11-11 20:44:45 +00002266 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002267 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002268 case Type::Elaborated:
2269 case Type::Paren:
2270 case Type::SubstTemplateTypeParm:
2271 case Type::TypeOfExpr:
2272 case Type::TypeOf:
2273 case Type::Decltype:
2274 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002275 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002276 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002277 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002278
David Blaikie42edade2014-11-11 20:44:45 +00002279 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002280}
2281
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002282llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2283 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002284 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002285
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002286 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002287
2288 // We may have cached a forward decl when we could have created
2289 // a non-forward decl. Go ahead and create a non-forward decl
2290 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002291 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002292 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002293
2294 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002295 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002296
2297 // Propagate members from the declaration to the definition
2298 // CreateType(const RecordType*) will overwrite this with the members in the
2299 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002301
Guy Benyei11169dd2012-12-18 14:30:41 +00002302 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002303 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002304 return Res;
2305}
2306
2307// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002308llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002310
Guy Benyei11169dd2012-12-18 14:30:41 +00002311 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002312 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 unsigned Line = getLineNumber(RD->getLocation());
2314 StringRef RDName = getClassName(RD);
2315
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002316 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
David Blaikied2785892013-08-18 17:36:19 +00002318 // If we ended up creating the type during the context chain construction,
2319 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002320 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002321 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002322 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002323 return T;
David Blaikied2785892013-08-18 17:36:19 +00002324
Adrian Prantl381e7552014-02-04 21:29:50 +00002325 // If this is just a forward or incomplete declaration, construct an
2326 // appropriately marked node and just return it.
2327 const RecordDecl *D = RD->getDefinition();
2328 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002329 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002330
2331 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2332 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002333
Manman Rene0064d82013-08-29 23:19:58 +00002334 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2335
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002336 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002337 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2338 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002339
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002340 RegionMap[Ty->getDecl()].reset(RealDecl);
2341 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002342
David Blaikieadfbf992013-08-18 16:55:33 +00002343 if (const ClassTemplateSpecializationDecl *TSpecial =
2344 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002345 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002346 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002347 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002348}
2349
David Blaikieadfbf992013-08-18 16:55:33 +00002350void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002351 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002352 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002353 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002354 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2355 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002356 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002357 while (1) {
2358 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2359 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2360 if (PBT && !BRL.isPrimaryBaseVirtual())
2361 PBase = PBT;
2362 else
2363 break;
2364 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002365 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002366 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2367 getOrCreateFile(RD->getLocation())));
2368 } else if (RD->isDynamicClass())
2369 ContainingType = RealDecl;
2370
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002371 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002372}
2373
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002374llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002375 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002376 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2378 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002379 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002380 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002381 *Offset += FieldSize;
2382 return Ty;
2383}
2384
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002385void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002386 StringRef &Name,
2387 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002388 llvm::DIScope *&FDContext,
2389 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002390 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002391 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2392 Name = getFunctionName(FD);
2393 // Use mangled name as linkage name for C/C++ functions.
2394 if (FD->hasPrototype()) {
2395 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002396 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002397 }
2398 // No need to replicate the linkage name if it isn't different from the
2399 // subprogram name, no need to have it at all unless coverage is enabled or
2400 // debug is set to more than just line tables.
2401 if (LinkageName == Name ||
2402 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2403 !CGM.getCodeGenOpts().EmitGcovNotes &&
2404 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2405 LinkageName = StringRef();
2406
2407 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2408 if (const NamespaceDecl *NSDecl =
2409 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2410 FDContext = getOrCreateNameSpace(NSDecl);
2411 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002412 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2413 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2414 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2415 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002416 // Collect template parameters.
2417 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2418 }
2419}
2420
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002421void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002422 unsigned &LineNo, QualType &T,
2423 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002424 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002425 Unit = getOrCreateFile(VD->getLocation());
2426 LineNo = getLineNumber(VD->getLocation());
2427
2428 setLocation(VD->getLocation());
2429
2430 T = VD->getType();
2431 if (T->isIncompleteArrayType()) {
2432 // CodeGen turns int[] into int[1] so we'll do the same here.
2433 llvm::APInt ConstVal(32, 1);
2434 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2435
2436 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2437 ArrayType::Normal, 0);
2438 }
2439
2440 Name = VD->getName();
2441 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2442 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2443 LinkageName = CGM.getMangledName(VD);
2444 if (LinkageName == Name)
2445 LinkageName = StringRef();
2446
2447 // Since we emit declarations (DW_AT_members) for static members, place the
2448 // definition of those static members in the namespace they were declared in
2449 // in the source code (the lexical decl context).
2450 // FIXME: Generalize this for even non-member global variables where the
2451 // declaration and definition may have different lexical decl contexts, once
2452 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002453 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2454 : VD->getDeclContext();
2455 // When a record type contains an in-line initialization of a static data
2456 // member, and the record type is marked as __declspec(dllexport), an implicit
2457 // definition of the member will be created in the record context. DWARF
2458 // doesn't seem to have a nice way to describe this in a form that consumers
2459 // are likely to understand, so fake the "normal" situation of a definition
2460 // outside the class by putting it in the global scope.
2461 if (DC->isRecord())
2462 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002463
2464 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2465 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002466}
2467
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002468llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002469CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002470 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002471 StringRef Name, LinkageName;
2472 unsigned Flags = 0;
2473 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002474 llvm::DIFile *Unit = getOrCreateFile(Loc);
2475 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002476 unsigned Line = getLineNumber(Loc);
2477
2478 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2479 TParamsArray, Flags);
2480 // Build function type.
2481 SmallVector<QualType, 16> ArgTypes;
2482 for (const ParmVarDecl *Parm: FD->parameters())
2483 ArgTypes.push_back(Parm->getType());
2484 QualType FnType =
2485 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2486 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002487 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002488 DContext, Name, LinkageName, Unit, Line,
2489 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002490 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002491 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002492 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002493 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2494 std::make_tuple(CanonDecl),
2495 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002496 return SP;
2497}
2498
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002499llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002500CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2501 QualType T;
2502 StringRef Name, LinkageName;
2503 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002504 llvm::DIFile *Unit = getOrCreateFile(Loc);
2505 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002506 unsigned Line = getLineNumber(Loc);
2507
2508 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002509 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2510 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2511 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002512 FwdDeclReplaceMap.emplace_back(
2513 std::piecewise_construct,
2514 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2515 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002516 return GV;
2517}
2518
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002519llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002520 // We only need a declaration (not a definition) of the type - so use whatever
2521 // we would otherwise do to get a type for a pointee. (forward declarations in
2522 // limited debug info, full definitions (if the type definition is available)
2523 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002524 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2525 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002526 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002527 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002528
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002529 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002531
2532 // No definition for now. Emit a forward definition that might be
2533 // merged with a potential upcoming definition.
2534 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2535 return getFunctionForwardDeclaration(FD);
2536 else if (const auto *VD = dyn_cast<VarDecl>(D))
2537 return getGlobalVariableForwardDeclaration(VD);
2538
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002539 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002540}
2541
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002542llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002543 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002544 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002545
Guy Benyei11169dd2012-12-18 14:30:41 +00002546 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002547 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002548 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002549
2550 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002551 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002552
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002553 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002554 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002555 if (const CXXMethodDecl *MD =
2556 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002557 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002558 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002559 }
2560 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002561 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002562 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002563 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 return SP;
2565 }
2566
Aaron Ballman86c93902014-03-06 23:45:36 +00002567 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002568 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002570 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002571 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 return SP;
2573 }
2574 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002575 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002576}
2577
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002578// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002579// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002580llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002581 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002582 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002583 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002584 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2585 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Eric Christopher28a6db52015-10-15 06:56:08 +00002586 return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002587
2588 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2589 return getOrCreateMethodType(Method, F);
2590 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2591 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002592 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002593
2594 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002595 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002596
2597 // Replace the instancetype keyword with the actual type.
2598 if (ResultTy == CGM.getContext().getObjCInstanceType())
2599 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002600 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002601
Adrian Prantl7bec9032013-05-10 21:08:31 +00002602 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002603 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002604 QualType SelfDeclTy;
2605 if (auto *SelfDecl = OMethod->getSelfDecl())
2606 SelfDeclTy = SelfDecl->getType();
2607 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2608 if (FPT->getNumParams() > 1)
2609 SelfDeclTy = FPT->getParamType(0);
2610 if (!SelfDeclTy.isNull())
2611 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002613 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002614 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002615 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002616 for (const auto *PI : OMethod->params())
2617 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002618 // Variadic methods need a special marker at the end of the type list.
2619 if (OMethod->isVariadic())
2620 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002621
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002622 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Eric Christopher28a6db52015-10-15 06:56:08 +00002623 return DBuilder.createSubroutineType(EltTypeArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002625
Adrian Prantl800faef2014-02-25 23:42:18 +00002626 // Handle variadic function types; they need an additional
2627 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002628 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2629 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002630 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002631 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2632 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2633 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2634 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2635 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002636 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Eric Christopher28a6db52015-10-15 06:56:08 +00002637 return DBuilder.createSubroutineType(EltTypeArray);
Adrian Prantld45ba252014-02-25 19:38:11 +00002638 }
2639
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002640 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002641}
2642
Eric Christophere7b87e52014-10-26 23:40:33 +00002643void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2644 SourceLocation ScopeLoc, QualType FnType,
2645 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002646
2647 StringRef Name;
2648 StringRef LinkageName;
2649
2650 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2651
2652 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002653 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002654
Guy Benyei11169dd2012-12-18 14:30:41 +00002655 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002656 llvm::DIFile *Unit = getOrCreateFile(Loc);
2657 llvm::DIScope *FDContext = Unit;
2658 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 if (!HasDecl) {
2660 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002661 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002662 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002663 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002664 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002665 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002666 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002667 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002668 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002669 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 return;
2671 }
2672 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002673 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2674 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2676 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002677 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002678 } else {
2679 // Use llvm function name.
2680 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002681 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 }
2683 if (!Name.empty() && Name[0] == '\01')
2684 Name = Name.substr(1);
2685
Adrian Prantl42d71b92014-04-10 23:21:53 +00002686 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002687 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002688 // Artificial functions without a location should not silently reuse CurLoc.
2689 if (Loc.isInvalid())
2690 CurLoc = SourceLocation();
2691 }
2692 unsigned LineNo = getLineNumber(Loc);
2693 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002694
Eric Christopher8018e412014-03-27 18:50:35 +00002695 // FIXME: The function declaration we're constructing here is mostly reusing
2696 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2697 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2698 // all subprograms instead of the actual context since subprogram definitions
2699 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002700 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002701 FDContext, Name, LinkageName, Unit, LineNo,
2702 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002703 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002704 TParamsArray.get(), getFunctionDeclaration(D));
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002705 Fn->setSubprogram(SP);
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002706 // We might get here with a VarDecl in the case we're generating
2707 // code for the initialization of globals. Do not record these decls
2708 // as they will overwrite the actual VarDecl Decl in the cache.
2709 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002710 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002711
Adrian Prantlbebb8932014-03-21 21:01:58 +00002712 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002713 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002714
Guy Benyei11169dd2012-12-18 14:30:41 +00002715 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002716 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002717}
2718
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002719void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2720 QualType FnType) {
2721 StringRef Name;
2722 StringRef LinkageName;
2723
2724 const Decl *D = GD.getDecl();
2725 if (!D)
2726 return;
2727
2728 unsigned Flags = 0;
2729 llvm::DIFile *Unit = getOrCreateFile(Loc);
Adrian Prantlf0cd6772015-10-04 23:23:04 +00002730 llvm::DIScope *FDContext = getDeclContextDescriptor(D);
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002731 llvm::DINodeArray TParamsArray;
2732 if (isa<FunctionDecl>(D)) {
2733 // If there is a DISubprogram for this function available then use it.
2734 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2735 TParamsArray, Flags);
2736 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2737 Name = getObjCMethodName(OMD);
2738 Flags |= llvm::DINode::FlagPrototyped;
2739 } else {
2740 llvm_unreachable("not a function or ObjC method");
2741 }
2742 if (!Name.empty() && Name[0] == '\01')
2743 Name = Name.substr(1);
2744
2745 if (D->isImplicit()) {
2746 Flags |= llvm::DINode::FlagArtificial;
2747 // Artificial functions without a location should not silently reuse CurLoc.
2748 if (Loc.isInvalid())
2749 CurLoc = SourceLocation();
2750 }
2751 unsigned LineNo = getLineNumber(Loc);
2752 unsigned ScopeLine = 0;
2753
2754 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2755 getOrCreateFunctionType(D, FnType, Unit),
2756 false /*internalLinkage*/, true /*definition*/,
Peter Collingbourne0900fe02015-11-05 22:04:14 +00002757 ScopeLine, Flags, CGM.getLangOpts().Optimize,
2758 TParamsArray.get(), getFunctionDeclaration(D));
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002759}
2760
David Blaikie835afb22015-01-21 23:08:17 +00002761void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 // Update our current location
2763 setLocation(Loc);
2764
Eric Christophere7b87e52014-10-26 23:40:33 +00002765 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2766 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002767
Adrian Prantle83b1302014-01-07 22:05:52 +00002768 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002769 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002770 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002771}
2772
Guy Benyei11169dd2012-12-18 14:30:41 +00002773void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002774 llvm::MDNode *Back = nullptr;
2775 if (!LexicalBlockStack.empty())
2776 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002777 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002778 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002779 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002780}
2781
Eric Christopher0fdcb312013-05-16 00:52:20 +00002782void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2783 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 // Set our current location.
2785 setLocation(Loc);
2786
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002788 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2789 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002790
2791 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2792 return;
2793
2794 // Create a new lexical block and push it on the stack.
2795 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002796}
2797
Eric Christopher0fdcb312013-05-16 00:52:20 +00002798void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2799 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2801
2802 // Provide an entry in the line table for the end of the block.
2803 EmitLocation(Builder, Loc);
2804
David Blaikie60a877b2014-10-22 19:34:33 +00002805 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2806 return;
2807
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 LexicalBlockStack.pop_back();
2809}
2810
Guy Benyei11169dd2012-12-18 14:30:41 +00002811void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2812 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2813 unsigned RCount = FnBeginRegionCount.back();
2814 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2815
2816 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002817 while (LexicalBlockStack.size() != RCount) {
2818 // Provide an entry in the line table for the end of the block.
2819 EmitLocation(Builder, CurLoc);
2820 LexicalBlockStack.pop_back();
2821 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002822 FnBeginRegionCount.pop_back();
2823}
2824
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002825llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002826 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002827
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002828 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002829 QualType FType;
2830 uint64_t FieldSize, FieldOffset;
2831 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002832
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002833 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002834 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002835
2836 FieldOffset = 0;
2837 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2838 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2839 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2840 FType = CGM.getContext().IntTy;
2841 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2842 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2843
2844 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2845 if (HasCopyAndDispose) {
2846 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002847 EltTys.push_back(
2848 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2849 EltTys.push_back(
2850 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 }
2852 bool HasByrefExtendedLayout;
2853 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002854 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2855 HasByrefExtendedLayout) &&
2856 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002857 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002858 EltTys.push_back(
2859 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002860 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002861
Guy Benyei11169dd2012-12-18 14:30:41 +00002862 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2863 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002864 CGM.getTarget().getPointerAlign(0))) {
2865 CharUnits FieldOffsetInBytes =
2866 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2867 CharUnits AlignedOffsetInBytes =
2868 FieldOffsetInBytes.RoundUpToAlignment(Align);
2869 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002870
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 if (NumPaddingBytes.isPositive()) {
2872 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2873 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2874 pad, ArrayType::Normal, 0);
2875 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2876 }
2877 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002878
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002880 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 FieldSize = CGM.getContext().getTypeSize(FType);
2882 FieldAlign = CGM.getContext().toBits(Align);
2883
Eric Christopherb2a008c2013-05-16 00:45:12 +00002884 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002885 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2886 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 EltTys.push_back(FieldTy);
2888 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002890 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002891
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002892 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002893
Guy Benyei11169dd2012-12-18 14:30:41 +00002894 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002895 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002896}
2897
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002898void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2899 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002900 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002901 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2903
David Blaikie7fceebf2013-08-19 03:37:48 +00002904 bool Unwritten =
2905 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2906 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002907 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002908 if (!Unwritten)
2909 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002910 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002911 uint64_t XOffset = 0;
2912 if (VD->hasAttr<BlocksAttr>())
2913 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002914 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002915 Ty = getOrCreateType(VD->getType(), Unit);
2916
2917 // If there is no debug info for this type then do not emit debug info
2918 // for this variable.
2919 if (!Ty)
2920 return;
2921
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002923 unsigned Line = 0;
2924 unsigned Column = 0;
2925 if (!Unwritten) {
2926 Line = getLineNumber(VD->getLocation());
2927 Column = getColumnNumber(VD->getLocation());
2928 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002929 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 unsigned Flags = 0;
2931 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002932 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002933 // If this is the first argument and it is implicit then
2934 // give it an object pointer flag.
2935 // FIXME: There has to be a better way to do this, but for static
2936 // functions there won't be an implicit param at arg1 and
2937 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002938 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002939 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002940 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002941 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2942 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002943 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002944
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002945 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002946
2947 StringRef Name = VD->getName();
2948 if (!Name.empty()) {
2949 if (VD->hasAttr<BlocksAttr>()) {
2950 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002951 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002952 // offset of __forwarding field
2953 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002954 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002955 Expr.push_back(offset.getQuantity());
2956 Expr.push_back(llvm::dwarf::DW_OP_deref);
2957 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002958 // offset of x field
2959 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002960 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002961
2962 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002963 auto *D = ArgNo
2964 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2965 *ArgNo, Unit, Line, Ty)
2966 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2967 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002968
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002970 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2971 llvm::DebugLoc::get(Line, Column, Scope),
2972 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002974 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002975 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002976 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2977 // If VD is an anonymous union then Storage represents value for
2978 // all union fields.
2979 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2980 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002981 // GDB has trouble finding local variables in anonymous unions, so we emit
2982 // artifical local variables for each of the members.
2983 //
2984 // FIXME: Remove this code as soon as GDB supports this.
2985 // The debug info verifier in LLVM operates based on the assumption that a
2986 // variable has the same size as its storage and we had to disable the check
2987 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002988 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002989 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002990 StringRef FieldName = Field->getName();
2991
2992 // Ignore unnamed fields. Do not ignore unnamed records.
2993 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2994 continue;
2995
2996 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002997 auto *D = DBuilder.createAutoVariable(
2998 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2999 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00003000
3001 // Insert an llvm.dbg.declare into the current block.
3002 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3003 llvm::DebugLoc::get(Line, Column, Scope),
3004 Builder.GetInsertBlock());
3005 }
Adrian Prantl0d820892015-04-29 15:05:50 +00003006 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003007 }
David Blaikiea76a7c92013-01-05 05:58:35 +00003008
3009 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003010 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003011 ArgNo
3012 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
3013 Ty, CGM.getLangOpts().Optimize,
3014 Flags)
3015 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
3016 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00003017
3018 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003019 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
3020 llvm::DebugLoc::get(Line, Column, Scope),
3021 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003022}
3023
3024void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
3025 llvm::Value *Storage,
3026 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003027 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003028 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003029}
3030
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003031llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3032 llvm::DIType *Ty) {
3033 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003034 if (CachedTy)
3035 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003036 return DBuilder.createObjectPointerType(Ty);
3037}
3038
Eric Christophere7b87e52014-10-26 23:40:33 +00003039void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3040 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003041 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003042 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003043 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003044
Craig Topper8a13c412014-05-21 05:09:00 +00003045 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003046 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003047
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003049
Guy Benyei11169dd2012-12-18 14:30:41 +00003050 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003051 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3052 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 if (isByRef)
3054 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003055 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003056 Ty = getOrCreateType(VD->getType(), Unit);
3057
3058 // Self is passed along as an implicit non-arg variable in a
3059 // block. Mark it as the object pointer.
3060 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003061 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003062
3063 // Get location information.
3064 unsigned Line = getLineNumber(VD->getLocation());
3065 unsigned Column = getColumnNumber(VD->getLocation());
3066
3067 const llvm::DataLayout &target = CGM.getDataLayout();
3068
3069 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003070 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3072
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003073 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003074 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003075 addr.push_back(llvm::dwarf::DW_OP_deref);
3076 addr.push_back(llvm::dwarf::DW_OP_plus);
3077 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003079 addr.push_back(llvm::dwarf::DW_OP_deref);
3080 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003082 offset =
3083 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003084 addr.push_back(offset.getQuantity());
3085 addr.push_back(llvm::dwarf::DW_OP_deref);
3086 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 // offset of x field
3088 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003089 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 }
3091
3092 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003093 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003094 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003095 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003096
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003098 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3099 if (InsertPoint)
3100 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3101 InsertPoint);
3102 else
3103 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3104 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003105}
3106
Guy Benyei11169dd2012-12-18 14:30:41 +00003107void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3108 unsigned ArgNo,
3109 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003110 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003111 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003112}
3113
3114namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003115struct BlockLayoutChunk {
3116 uint64_t OffsetInBits;
3117 const BlockDecl::Capture *Capture;
3118};
3119bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3120 return l.OffsetInBits < r.OffsetInBits;
3121}
Guy Benyei11169dd2012-12-18 14:30:41 +00003122}
3123
3124void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003125 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003126 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003127 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003129 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 ASTContext &C = CGM.getContext();
3131 const BlockDecl *blockDecl = block.getBlockDecl();
3132
3133 // Collect some general information about the block's location.
3134 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003135 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003136 unsigned line = getLineNumber(loc);
3137 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003138
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003140 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141
3142 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003143 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003144
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003145 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3147 blockLayout->getElementOffsetInBits(0),
3148 tunit, tunit));
3149 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3150 blockLayout->getElementOffsetInBits(1),
3151 tunit, tunit));
3152 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3153 blockLayout->getElementOffsetInBits(2),
3154 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003155 auto *FnTy = block.getBlockExpr()->getFunctionType();
3156 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3157 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 blockLayout->getElementOffsetInBits(3),
3159 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003160 fields.push_back(createFieldType(
3161 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3162 ? C.getBlockDescriptorExtendedType()
3163 : C.getBlockDescriptorType()),
3164 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003165
3166 // We want to sort the captures by offset, not because DWARF
3167 // requires this, but because we're paranoid about debuggers.
3168 SmallVector<BlockLayoutChunk, 8> chunks;
3169
3170 // 'this' capture.
3171 if (blockDecl->capturesCXXThis()) {
3172 BlockLayoutChunk chunk;
3173 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003174 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003175 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003176 chunks.push_back(chunk);
3177 }
3178
3179 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003180 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003181 const VarDecl *variable = capture.getVariable();
3182 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3183
3184 // Ignore constant captures.
3185 if (captureInfo.isConstant())
3186 continue;
3187
3188 BlockLayoutChunk chunk;
3189 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003190 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003191 chunk.Capture = &capture;
3192 chunks.push_back(chunk);
3193 }
3194
3195 // Sort by offset.
3196 llvm::array_pod_sort(chunks.begin(), chunks.end());
3197
Eric Christophere7b87e52014-10-26 23:40:33 +00003198 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3199 e = chunks.end();
3200 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 uint64_t offsetInBits = i->OffsetInBits;
3202 const BlockDecl::Capture *capture = i->Capture;
3203
3204 // If we have a null capture, this must be the C++ 'this' capture.
3205 if (!capture) {
3206 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003207 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003208 QualType type = method->getThisType(C);
3209
3210 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3211 offsetInBits, tunit, tunit));
3212 continue;
3213 }
3214
3215 const VarDecl *variable = capture->getVariable();
3216 StringRef name = variable->getName();
3217
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003218 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003219 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003220 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003221
3222 // FIXME: this creates a second copy of this type!
3223 uint64_t xoffset;
3224 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003225 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3226 fieldType =
3227 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3228 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003230 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3231 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 }
3233 fields.push_back(fieldType);
3234 }
3235
3236 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003237 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3238 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003239
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003240 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003241
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003242 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003243 tunit, typeName.str(), tunit, line,
3244 CGM.getContext().toBits(block.BlockSize),
3245 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003246 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3247
3248 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003249 unsigned flags = llvm::DINode::FlagArtificial;
3250 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003251
3252 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003253 auto *debugVar = DBuilder.createParameterVariable(
3254 scope, Arg->getName(), ArgNo, tunit, line, type,
3255 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003256
Adrian Prantl616bef42013-03-14 21:52:59 +00003257 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003258 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003259 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003260 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003261 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003262 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003263
Adrian Prantl616bef42013-03-14 21:52:59 +00003264 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003265 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3266 llvm::DebugLoc::get(line, column, scope),
3267 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003268}
3269
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003270llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003271CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3272 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003273 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003274
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003275 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003276 if (MI != StaticDataMemberCache.end()) {
3277 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003278 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003279 }
David Blaikiece763042013-08-20 21:49:21 +00003280
3281 // If the member wasn't found in the cache, lazily construct and add it to the
3282 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003283 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003284 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003285 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003286}
3287
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003288llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3289 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3290 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3291 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003292
3293 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003294 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003295 StringRef FieldName = Field->getName();
3296
3297 // Ignore unnamed fields, but recurse into anonymous records.
3298 if (FieldName.empty()) {
3299 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3300 if (RT)
3301 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3302 Var, DContext);
3303 continue;
3304 }
3305 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003306 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3307 LineNo, FieldTy,
3308 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003309 }
3310 return GV;
3311}
3312
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003313void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003315 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003316 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003317 llvm::DIFile *Unit = nullptr;
3318 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003319 unsigned LineNo;
3320 StringRef DeclName, LinkageName;
3321 QualType T;
3322 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003323
3324 // Attempt to store one global variable for the declaration - even if we
3325 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003326 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003327
3328 // If this is an anonymous union then we'll want to emit a global
3329 // variable for each member of the anonymous union so that it's possible
3330 // to find the name of any field in the union.
3331 if (T->isUnionType() && DeclName.empty()) {
Reid Kleckner43ecd7c2015-11-20 17:41:12 +00003332 const RecordDecl *RD = T->castAs<RecordType>()->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003333 assert(RD->isAnonymousStructOrUnion() &&
3334 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003335 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3336 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003337 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003338 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3339 Var->hasInternalLinkage(), Var,
3340 getOrCreateStaticDataMemberDeclarationOrNull(D));
3341 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003342 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003343}
3344
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003345void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3346 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003347 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003348 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003349 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003350 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003351 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003352 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3353 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3354 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3355 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3356 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003357 // Do not use global variables for enums.
3358 //
3359 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003360 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003361 return;
David Blaikiea15565562014-04-04 20:56:17 +00003362 // Do not emit separate definitions for function local const/statics.
3363 if (isa<FunctionDecl>(VD->getDeclContext()))
3364 return;
David Blaikiebb113912014-04-05 07:23:17 +00003365 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003366 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003367 if (VarD->isStaticDataMember()) {
3368 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003369 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003370 // Ensure that the type is retained even though it's otherwise unreferenced.
3371 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003372 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003373 return;
3374 }
3375
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003376 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003377
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003378 auto &GV = DeclCache[VD];
3379 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003380 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003381 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003382 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003383 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003384}
3385
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003386llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003387 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003388 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003389 llvm::DIScope *Mod = getParentModuleOrNull(D);
3390 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003391}
3392
David Blaikie9f88fe82013-04-22 06:13:21 +00003393void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003394 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3395 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003396 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003397 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3398 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003399 getLineNumber(UD.getLocation()));
3400}
3401
David Blaikiebd483762013-05-20 04:58:53 +00003402void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3403 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3404 return;
3405 assert(UD.shadow_size() &&
3406 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3407 // Emitting one decl is sufficient - debuggers can detect that this is an
3408 // overloaded name & provide lookup for all the overloads.
3409 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003410 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003411 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003412 DBuilder.createImportedDeclaration(
3413 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3414 getLineNumber(USD.getLocation()));
3415}
3416
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003417void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantlc6458d62015-09-19 00:10:32 +00003418 auto Info = ExternalASTSource::ASTSourceDescriptor(*ID.getImportedModule());
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003419 DBuilder.createImportedDeclaration(
Adrian Prantl66689202015-09-18 23:01:45 +00003420 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3421 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3422 getLineNumber(ID.getLocation()));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003423}
3424
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003425llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003426CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3427 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003428 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003429 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003430 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003431 return cast<llvm::DIImportedEntity>(VH);
3432 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003433 if (const NamespaceAliasDecl *Underlying =
3434 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3435 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003436 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003437 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3438 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3439 NA.getName());
3440 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003441 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003442 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3443 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3444 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003445 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003446 return R;
3447}
3448
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003449llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003450CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003451 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003452 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003453 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003454 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003455
Guy Benyei11169dd2012-12-18 14:30:41 +00003456 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003457 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003458 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003460 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003461 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003462 return NS;
3463}
3464
Adrian Prantla5206ce2015-09-22 23:26:43 +00003465void CGDebugInfo::setDwoId(uint64_t Signature) {
3466 assert(TheCU && "no main compile unit");
3467 TheCU->setDWOId(Signature);
3468}
3469
3470
Guy Benyei11169dd2012-12-18 14:30:41 +00003471void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003472 // Creating types might create further types - invalidating the current
3473 // element and the size(), so don't cache/reference them.
3474 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3475 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003476 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003477 ? CreateTypeDefinition(E.Type, E.Unit)
3478 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003479 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003480 }
3481
David Blaikief427b002014-05-06 03:42:01 +00003482 for (auto p : ReplaceMap) {
3483 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003484 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003485 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003486
David Blaikief427b002014-05-06 03:42:01 +00003487 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003488 assert(it != TypeCache.end());
3489 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003490
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003491 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3492 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003493 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003494
Frederic Rissd253ed62014-11-18 03:40:51 +00003495 for (const auto &p : FwdDeclReplaceMap) {
3496 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003497 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003498 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003499
3500 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003501 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003502 // with ourselves, that will destroy the temporary MDNode and
3503 // replace it with a standard one, avoiding leaking memory.
3504 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003505 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003506 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003507 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003508
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003509 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003510 }
3511
Adrian Prantl73409ce2013-03-11 18:33:46 +00003512 // We keep our own list of retained types, because we need to look
3513 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003514 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003515 if (auto MD = TypeCache[RT])
3516 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003517
Guy Benyei11169dd2012-12-18 14:30:41 +00003518 DBuilder.finalize();
3519}
David Blaikie66088d52014-09-24 17:01:27 +00003520
3521void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3522 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3523 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003524
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003525 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003526 // Don't ignore in case of explicit cast where it is referenced indirectly.
3527 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003528}