blob: 50a8ffb4953d2648ec92c30f919fb69f10ddeb7e [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"
31#include "clang/Lex/PreprocessorOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000032#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000034#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DerivedTypes.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/Intrinsics.h"
39#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000040#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000041#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042using namespace clang;
43using namespace clang::CodeGen;
44
45CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000046 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
Adrian Prantl6b21ab22015-08-27 19:46:20 +000047 DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
Eric Christopher324bbbd2013-07-14 21:12:44 +000048 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000049 CreateCompileUnit();
50}
51
52CGDebugInfo::~CGDebugInfo() {
53 assert(LexicalBlockStack.empty() &&
54 "Region stack mismatch, stack not empty!");
55}
56
David Blaikie66e41972015-01-14 07:38:27 +000057ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000058 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000059 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000060 init(TemporaryLocation);
61}
62
Adrian Prantl39428e72015-02-03 18:40:42 +000063ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000064 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000065 SourceLocation TemporaryLocation)
David Blaikied7057d92015-08-12 23:49:57 +000066 : CGF(&CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000067 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000068}
69
70void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000071 bool DefaultToEmpty) {
David Blaikied7057d92015-08-12 23:49:57 +000072 auto *DI = CGF->getDebugInfo();
73 if (!DI) {
74 CGF = nullptr;
75 return;
David Blaikie66e41972015-01-14 07:38:27 +000076 }
David Blaikied7057d92015-08-12 23:49:57 +000077
78 OriginalLocation = CGF->Builder.getCurrentDebugLocation();
79 if (TemporaryLocation.isValid()) {
80 DI->EmitLocation(CGF->Builder, TemporaryLocation);
81 return;
82 }
83
84 if (DefaultToEmpty) {
85 CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc());
86 return;
87 }
88
89 // Construct a location that has a valid scope, but no line info.
90 assert(!DI->LexicalBlockStack.empty());
91 CGF->Builder.SetCurrentDebugLocation(
92 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl2e0637f2013-07-18 00:28:02 +000093}
94
David Blaikie9b479662015-01-25 01:19:10 +000095ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
David Blaikied7057d92015-08-12 23:49:57 +000096 : CGF(&CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000097 init(E->getExprLoc());
98}
99
David Blaikie66e41972015-01-14 07:38:27 +0000100ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
David Blaikied7057d92015-08-12 23:49:57 +0000101 : CGF(&CGF) {
102 if (!CGF.getDebugInfo()) {
103 this->CGF = nullptr;
104 return;
David Blaikie66e41972015-01-14 07:38:27 +0000105 }
David Blaikied7057d92015-08-12 23:49:57 +0000106 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
107 if (Loc)
108 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +0000109}
110
111ApplyDebugLocation::~ApplyDebugLocation() {
112 // Query CGF so the location isn't overwritten when location updates are
113 // temporarily disabled (for C++ default function arguments)
David Blaikied7057d92015-08-12 23:49:57 +0000114 if (CGF)
115 CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000116}
117
Guy Benyei11169dd2012-12-18 14:30:41 +0000118void CGDebugInfo::setLocation(SourceLocation Loc) {
119 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000120 if (Loc.isInvalid())
121 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000122
123 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
124
125 // If we've changed files in the middle of a lexical scope go ahead
126 // and create a new lexical scope with file node if it's different
127 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000128 if (LexicalBlockStack.empty())
129 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000130
131 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000132 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000133 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000134
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000135 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 return;
137
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000138 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000139 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000140 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
141 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000142 } else if (isa<llvm::DILexicalBlock>(Scope) ||
143 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000144 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000145 LexicalBlockStack.emplace_back(
146 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 }
148}
149
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000150llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +0000151 llvm::DIScope *Mod = getParentModuleOrNull(D);
152 return getContextDescriptor(cast<Decl>(D->getDeclContext()),
153 Mod ? Mod : TheCU);
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000154}
155
156llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
157 llvm::DIScope *Default) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 if (!Context)
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000159 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000160
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000161 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000163 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000164 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000165 }
166
167 // Check namespace.
168 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000169 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000170
David Blaikiebfa52742013-04-19 06:56:38 +0000171 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
172 if (!RDecl->isDependentType())
173 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000174 getOrCreateMainFile());
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000175 return Default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000176}
177
Guy Benyei11169dd2012-12-18 14:30:41 +0000178StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000179 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000180 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000181 FunctionTemplateSpecializationInfo *Info =
182 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 if (!Info && FII)
184 return FII->getName();
185
186 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000187 SmallString<128> NS;
188 llvm::raw_svector_ostream OS(NS);
189 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000190
191 // Add any template specialization args.
192 if (Info) {
193 const TemplateArgumentList *TArgs = Info->TemplateArguments;
194 const TemplateArgument *Args = TArgs->data();
195 unsigned NumArgs = TArgs->size();
196 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000197 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
198 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 }
200
201 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000202 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000203}
204
205StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
206 SmallString<256> MethodName;
207 llvm::raw_svector_ostream OS(MethodName);
208 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
209 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000210 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000211 dyn_cast<const ObjCImplementationDecl>(DC)) {
212 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000213 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 dyn_cast<const ObjCInterfaceDecl>(DC)) {
215 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000216 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
218 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
219 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000220 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000221 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000222 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000223 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000224 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000225 ClassTy.print(OS, PrintingPolicy(LangOptions()));
226 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000227 }
228 OS << ' ' << OMD->getSelector().getAsString() << ']';
229
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000230 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000231}
232
Guy Benyei11169dd2012-12-18 14:30:41 +0000233StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000234 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000235}
236
Eric Christophere7b87e52014-10-26 23:40:33 +0000237StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000238 // quick optimization to avoid having to intern strings that are already
239 // stored reliably elsewhere
240 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000241 return RD->getName();
242
David Blaikie65813a32014-04-02 18:21:09 +0000243 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000244 {
David Blaikie65813a32014-04-02 18:21:09 +0000245 llvm::raw_svector_ostream OS(Name);
246 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
247 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000248 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000249
250 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000251 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000252}
253
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000254llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000255 if (!Loc.isValid())
256 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000257 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000258
259 SourceManager &SM = CGM.getContext().getSourceManager();
260 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
261
262 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
263 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000264 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000265
266 // Cache the results.
267 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000268 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000269
270 if (it != DIFileCache.end()) {
271 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000272 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000273 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000274 }
275
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000276 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000277 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000278
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000279 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000280 return F;
281}
282
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000283llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000284 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000285}
286
Guy Benyei11169dd2012-12-18 14:30:41 +0000287unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
288 if (Loc.isInvalid() && CurLoc.isInvalid())
289 return 0;
290 SourceManager &SM = CGM.getContext().getSourceManager();
291 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000292 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000293}
294
Adrian Prantlc7822422013-03-12 20:43:25 +0000295unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000296 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000297 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000298 return 0;
299
300 // If the location is invalid then use the current column.
301 if (Loc.isInvalid() && CurLoc.isInvalid())
302 return 0;
303 SourceManager &SM = CGM.getContext().getSourceManager();
304 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000305 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000306}
307
308StringRef CGDebugInfo::getCurrentDirname() {
309 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
310 return CGM.getCodeGenOpts().DebugCompilationDir;
311
312 if (!CWDName.empty())
313 return CWDName;
314 SmallString<256> CWD;
315 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000316 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000317}
318
Guy Benyei11169dd2012-12-18 14:30:41 +0000319void CGDebugInfo::CreateCompileUnit() {
320
David Blaikieaabde052014-05-14 00:29:00 +0000321 // Should we be asking the SourceManager for the main file name, instead of
322 // accepting it as an argument? This just causes the main file name to
323 // mismatch with source locations and create extra lexical scopes or
324 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
325 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
326 // because that's what the SourceManager says)
327
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 // Get absolute path name.
329 SourceManager &SM = CGM.getContext().getSourceManager();
330 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
331 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000332 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000333
334 // The main file name provided via the "-main-file-name" option contains just
335 // the file name itself with no path information. This file name may have had
336 // a relative path, so we look into the actual file entry for the main
337 // file to determine the real absolute path for the file.
338 std::string MainFileDir;
339 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
340 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000341 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000342 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
343 llvm::sys::path::append(MainFileDirSS, MainFileName);
344 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000345 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000346 }
347
Ed Masteda706022014-05-07 12:49:30 +0000348 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000349 const LangOptions &LO = CGM.getLangOpts();
350 if (LO.CPlusPlus) {
351 if (LO.ObjC1)
352 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
353 else
354 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
355 } else if (LO.ObjC1) {
356 LangTag = llvm::dwarf::DW_LANG_ObjC;
357 } else if (LO.C99) {
358 LangTag = llvm::dwarf::DW_LANG_C99;
359 } else {
360 LangTag = llvm::dwarf::DW_LANG_C89;
361 }
362
363 std::string Producer = getClangFullVersion();
364
365 // Figure out which version of the ObjC runtime we have.
366 unsigned RuntimeVers = 0;
367 if (LO.ObjC1)
368 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
369
370 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000371 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000372 TheCU = DBuilder.createCompileUnit(
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000373 LangTag, MainFileName, getCurrentDirname(), Producer, LO.Optimize,
374 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers,
375 CGM.getCodeGenOpts().SplitDwarfFile,
Diego Novillo913690c2014-06-24 17:02:17 +0000376 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000377 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000378 : llvm::DIBuilder::FullDebug,
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000379 0 /* DWOid */, DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000380}
381
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000382llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000383 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 StringRef BTName;
385 switch (BT->getKind()) {
386#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000387#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000388#include "clang/AST/BuiltinTypes.def"
389 case BuiltinType::Dependent:
390 llvm_unreachable("Unexpected builtin type");
391 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000392 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000393 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000394 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000395 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000396 if (!ClassTy)
397 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
398 "objc_class", TheCU,
399 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 return ClassTy;
401 case BuiltinType::ObjCId: {
402 // typedef struct objc_class *Class;
403 // typedef struct objc_object {
404 // Class isa;
405 // } *id;
406
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000407 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return ObjTy;
409
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000410 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000411 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
412 "objc_class", TheCU,
413 getOrCreateMainFile(), 0);
414
415 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000416
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000417 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000418
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000419 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000420 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000421 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000422
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000423 DBuilder.replaceArrays(
424 ObjTy,
425 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
426 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000427 return ObjTy;
428 }
429 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000430 if (!SelTy)
431 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
432 "objc_selector", TheCU,
433 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 return SelTy;
435 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000436
437 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000438 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000439 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000440 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441 OCLImage1dArrayDITy);
442 case BuiltinType::OCLImage1dBuffer:
443 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
444 OCLImage1dBufferDITy);
445 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 case BuiltinType::OCLImage2dArray:
448 return getOrCreateStructPtrType("opencl_image2d_array_t",
449 OCLImage2dArrayDITy);
450 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000452 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000453 return DBuilder.createBasicType(
454 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
455 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000456 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000457 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000458
Guy Benyei11169dd2012-12-18 14:30:41 +0000459 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000460 case BuiltinType::Char_U:
461 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
462 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000464 case BuiltinType::SChar:
465 Encoding = llvm::dwarf::DW_ATE_signed_char;
466 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::Char32:
469 Encoding = llvm::dwarf::DW_ATE_UTF;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::UShort:
472 case BuiltinType::UInt:
473 case BuiltinType::UInt128:
474 case BuiltinType::ULong:
475 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::ULongLong:
477 Encoding = llvm::dwarf::DW_ATE_unsigned;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 case BuiltinType::Short:
480 case BuiltinType::Int:
481 case BuiltinType::Int128:
482 case BuiltinType::Long:
483 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::LongLong:
485 Encoding = llvm::dwarf::DW_ATE_signed;
486 break;
487 case BuiltinType::Bool:
488 Encoding = llvm::dwarf::DW_ATE_boolean;
489 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 case BuiltinType::Half:
491 case BuiltinType::Float:
492 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000493 case BuiltinType::Double:
494 Encoding = llvm::dwarf::DW_ATE_float;
495 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000496 }
497
498 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000499 case BuiltinType::Long:
500 BTName = "long int";
501 break;
502 case BuiltinType::LongLong:
503 BTName = "long long int";
504 break;
505 case BuiltinType::ULong:
506 BTName = "long unsigned int";
507 break;
508 case BuiltinType::ULongLong:
509 BTName = "long long unsigned int";
510 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000511 default:
512 BTName = BT->getName(CGM.getLangOpts());
513 break;
514 }
515 // Bit size, align and offset of the type.
516 uint64_t Size = CGM.getContext().getTypeSize(BT);
517 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000518 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000519}
520
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000521llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000523 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000524 if (Ty->isComplexIntegerType())
525 Encoding = llvm::dwarf::DW_ATE_lo_user;
526
527 uint64_t Size = CGM.getContext().getTypeSize(Ty);
528 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000529 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000530}
531
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000532llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
533 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 QualifierCollector Qc;
535 const Type *T = Qc.strip(Ty);
536
537 // Ignore these qualifiers for now.
538 Qc.removeObjCGCAttr();
539 Qc.removeAddressSpace();
540 Qc.removeObjCLifetime();
541
542 // We will create one Derived type for one qualifier and recurse to handle any
543 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000544 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 if (Qc.hasConst()) {
546 Tag = llvm::dwarf::DW_TAG_const_type;
547 Qc.removeConst();
548 } else if (Qc.hasVolatile()) {
549 Tag = llvm::dwarf::DW_TAG_volatile_type;
550 Qc.removeVolatile();
551 } else if (Qc.hasRestrict()) {
552 Tag = llvm::dwarf::DW_TAG_restrict_type;
553 Qc.removeRestrict();
554 } else {
555 assert(Qc.empty() && "Unknown type qualifier for debug info");
556 return getOrCreateType(QualType(T, 0), Unit);
557 }
558
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000559 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000560
561 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
562 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000563 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000564}
565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000566llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
567 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000568
569 // The frontend treats 'id' as a typedef to an ObjCObjectType,
570 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
571 // debug info, we want to emit 'id' in both cases.
572 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000573 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000574
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000575 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
576 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000577}
578
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000579llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
580 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000581 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 Ty->getPointeeType(), Unit);
583}
584
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000585/// \return whether a C++ mangling exists for the type defined by TD.
586static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
587 switch (TheCU->getSourceLanguage()) {
588 case llvm::dwarf::DW_LANG_C_plus_plus:
589 return true;
590 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
591 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
592 default:
593 return false;
594 }
595}
596
Manman Rene0064d82013-08-29 23:19:58 +0000597/// In C++ mode, types have linkage, so we can rely on the ODR and
598/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000599static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
600 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000601 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000602 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000603 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000604
605 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000606 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000607
Manman Rene0064d82013-08-29 23:19:58 +0000608 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
609 if (CGM.getTarget().getCXXABI().isMicrosoft())
610 return FullName;
611
612 // TODO: This is using the RTTI name. Is there a better way to get
613 // a unique string for a type?
614 llvm::raw_svector_ostream Out(FullName);
615 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000616 return FullName;
617}
618
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000619/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000620static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
621 llvm::dwarf::Tag Tag;
622 if (RD->isStruct() || RD->isInterface())
623 Tag = llvm::dwarf::DW_TAG_structure_type;
624 else if (RD->isUnion())
625 Tag = llvm::dwarf::DW_TAG_union_type;
626 else {
627 // FIXME: This could be a struct type giving a default visibility different
628 // than C++ class type, but needs llvm metadata changes first.
629 assert(RD->isClass());
630 Tag = llvm::dwarf::DW_TAG_class_type;
631 }
632 return Tag;
633}
634
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000635llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000636CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000637 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000638 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000639 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
640 return cast<llvm::DICompositeType>(T);
641 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000642 unsigned Line = getLineNumber(RD->getLocation());
643 StringRef RDName = getClassName(RD);
644
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000645 uint64_t Size = 0;
646 uint64_t Align = 0;
647
648 const RecordDecl *D = RD->getDefinition();
649 if (D && D->isCompleteDefinition()) {
650 Size = CGM.getContext().getTypeSize(Ty);
651 Align = CGM.getContext().getTypeAlign(Ty);
652 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000653
654 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000655 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000656 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000657 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000658 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000659 ReplaceMap.emplace_back(
660 std::piecewise_construct, std::make_tuple(Ty),
661 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000662 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000663}
664
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000665llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000666 const Type *Ty,
667 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000668 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
670 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000671 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000672
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 // Bit size, align and offset of the type.
674 // Size is always the size of a pointer. We can't use getTypeSize here
675 // because that does not return the correct value for references.
676 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000677 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
679
David Blaikie99dab3b2013-09-04 22:03:57 +0000680 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
681 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000682}
683
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000684llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
685 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000686 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000687 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000688 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
689 TheCU, getOrCreateMainFile(), 0);
690 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
691 Cache = DBuilder.createPointerType(Cache, Size);
692 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000693}
694
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
696 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000697 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000698 QualType FType;
699 uint64_t FieldSize, FieldOffset;
700 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000701 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702
703 FieldOffset = 0;
704 FType = CGM.getContext().UnsignedLongTy;
705 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
706 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
707
708 Elements = DBuilder.getOrCreateArray(EltTys);
709 EltTys.clear();
710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000711 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000712 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000713
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000714 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000715 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000716 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
718 // Bit size, align and offset of the type.
719 uint64_t Size = CGM.getContext().getTypeSize(Ty);
720
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000721 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722
723 FieldOffset = 0;
724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
725 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
726 FType = CGM.getContext().IntTy;
727 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
728 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000729 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
731
732 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000733 FieldSize = CGM.getContext().getTypeSize(Ty);
734 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000735 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000736 FieldSize, FieldAlign, FieldOffset,
737 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000738
739 FieldOffset += FieldSize;
740 Elements = DBuilder.getOrCreateArray(EltTys);
741
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000742 // The __block_literal_generic structs are marked with a special
743 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
744 // the debugger needs to know about. To allow type uniquing, emit
745 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000746 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000747 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000748 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000749
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000750 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000751}
752
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000753llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
754 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000755 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000756 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000757
758 SmallString<128> NS;
759 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000760 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
761 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000762
763 TemplateSpecializationType::PrintTemplateArgumentList(
764 OS, Ty->getArgs(), Ty->getNumArgs(),
765 CGM.getContext().getPrintingPolicy());
766
Eric Christophere7b87e52014-10-26 23:40:33 +0000767 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
768 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000769
770 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000771 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
772 getLineNumber(Loc),
773 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000774}
775
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000776llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
777 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 // We don't set size information, but do specify where the typedef was
779 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000780 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000781
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000782 // Typedefs are derived from some other type.
783 return DBuilder.createTypedef(
784 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
785 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000786 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000787}
788
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000789llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
790 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000791 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000792
793 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000794 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000795
796 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000797 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000798 if (isa<FunctionNoProtoType>(Ty))
799 EltTys.push_back(DBuilder.createUnspecifiedParameter());
800 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000801 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
802 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000803 if (FPT->isVariadic())
804 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 }
806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000807 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 return DBuilder.createSubroutineType(Unit, EltTypeArray);
809}
810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000811/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000812/// As an optimization, return 0 if the access specifier equals the
813/// default for the containing type.
814static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
815 AccessSpecifier Default = clang::AS_none;
816 if (RD && RD->isClass())
817 Default = clang::AS_private;
818 else if (RD && (RD->isStruct() || RD->isUnion()))
819 Default = clang::AS_public;
820
821 if (Access == Default)
822 return 0;
823
Eric Christophere7b87e52014-10-26 23:40:33 +0000824 switch (Access) {
825 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000826 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000827 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000828 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000829 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000830 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000831 case clang::AS_none:
832 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000833 }
834 llvm_unreachable("unexpected access enumerator");
835}
Guy Benyei11169dd2012-12-18 14:30:41 +0000836
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000837llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000838 StringRef name, QualType type, uint64_t sizeInBitsOverride,
839 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000840 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
841 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000842
843 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000844 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 unsigned line = getLineNumber(loc);
846
David Majnemer34b57492014-07-30 01:30:47 +0000847 uint64_t SizeInBits = 0;
848 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000850 TypeInfo TI = CGM.getContext().getTypeInfo(type);
851 SizeInBits = TI.Width;
852 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000853
854 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000855 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 }
857
Adrian Prantl21361fb2014-08-29 22:44:27 +0000858 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000859 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
860 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000861}
862
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000863void CGDebugInfo::CollectRecordLambdaFields(
864 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000865 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000866 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
867 // has the name and the location of the variable so we should iterate over
868 // both concurrently.
869 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
870 RecordDecl::field_iterator Field = CXXDecl->field_begin();
871 unsigned fieldno = 0;
872 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000873 E = CXXDecl->captures_end();
874 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000875 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000876 if (C.capturesVariable()) {
877 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000878 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000879 StringRef VName = V->getName();
880 uint64_t SizeInBitsOverride = 0;
881 if (Field->isBitField()) {
882 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
883 assert(SizeInBitsOverride && "found named 0-width bitfield");
884 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000885 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000886 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
887 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
888 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000889 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000890 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000891 // TODO: Need to handle 'this' in some way by probably renaming the
892 // this of the lambda class and having a field member of 'this' or
893 // by using AT_object_pointer for the function and having that be
894 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000895 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000896 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000897 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000898 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000899 "this", type, 0, f->getLocation(), f->getAccess(),
900 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000901
902 elements.push_back(fieldType);
903 }
904 }
905}
906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000907llvm::DIDerivedType *
908CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000909 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000910 // Create the descriptor for the static variable, with or without
911 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000912 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000913 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
914 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000915
Eric Christopher91a31902013-01-16 01:22:32 +0000916 unsigned LineNumber = getLineNumber(Var->getLocation());
917 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000918 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000919 if (Var->getInit()) {
920 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000921 if (Value) {
922 if (Value->isInt())
923 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
924 if (Value->isFloat())
925 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
926 }
Eric Christopher91a31902013-01-16 01:22:32 +0000927 }
928
Adrian Prantl21361fb2014-08-29 22:44:27 +0000929 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000930 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000931 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000932 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000933 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000934}
935
Eric Christophere7b87e52014-10-26 23:40:33 +0000936void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000937 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
938 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000939 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000940 StringRef name = field->getName();
941 QualType type = field->getType();
942
943 // Ignore unnamed fields unless they're anonymous structs/unions.
944 if (name.empty() && !type->isRecordType())
945 return;
946
947 uint64_t SizeInBitsOverride = 0;
948 if (field->isBitField()) {
949 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
950 assert(SizeInBitsOverride && "found named 0-width bitfield");
951 }
952
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000953 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000954 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
955 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000956
957 elements.push_back(fieldType);
958}
959
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000960void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000961 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000962 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000963 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000964 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
965
Eric Christopher91a31902013-01-16 01:22:32 +0000966 if (CXXDecl && CXXDecl->isLambda())
967 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
968 else {
969 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000970
Eric Christopher91a31902013-01-16 01:22:32 +0000971 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000972 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000973
Eric Christopher91a31902013-01-16 01:22:32 +0000974 // Static and non-static members should appear in the same order as
975 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000976 for (const auto *I : record->decls())
977 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000978 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000979 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000980 if (MI != StaticDataMemberCache.end()) {
981 assert(MI->second &&
982 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +0000983 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +0000984 } else {
985 auto Field = CreateRecordStaticField(V, RecordTy, record);
986 elements.push_back(Field);
987 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000988 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000989 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
990 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000991
992 // Bump field number for next field.
993 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000994 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000995 }
996}
997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000998llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +0000999CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001001 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001002 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001003 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001004 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001005 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1006 Func, Unit);
1007}
David Blaikie2aaf0652013-01-07 22:24:59 +00001008
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001009llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1010 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001012 llvm::DITypeRefArray Args(
1013 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001014 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001015 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001016
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001017 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001018
1019 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001020 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
David Blaikie2aaf0652013-01-07 22:24:59 +00001022 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001023 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001024 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1025 // Create pointer type directly in this case.
1026 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1027 QualType PointeeTy = ThisPtrTy->getPointeeType();
1028 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001029 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001030 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1032 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001033 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001034 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 // TODO: This and the artificial type below are misleading, the
1036 // types aren't artificial the argument is, but the current
1037 // metadata doesn't represent that.
1038 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1039 Elts.push_back(ThisPtrType);
1040 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001041 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001042 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001043 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1044 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001045 }
1046
1047 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001048 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1049 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001051 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001052
Adrian Prantl0630eb72013-12-18 21:48:18 +00001053 unsigned Flags = 0;
1054 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001055 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001056 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001058
1059 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001060}
1061
Eric Christopherb2a008c2013-05-16 00:45:12 +00001062/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001063/// inside a function.
1064static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1065 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1066 return isFunctionLocalClass(NRD);
1067 if (isa<FunctionDecl>(RD->getDeclContext()))
1068 return true;
1069 return false;
1070}
1071
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001072llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1073 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001074 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001075 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001076
Guy Benyei11169dd2012-12-18 14:30:41 +00001077 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
1080 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1081 // make sense to give a single ctor/dtor a linkage name.
1082 StringRef MethodLinkageName;
1083 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1084 MethodLinkageName = CGM.getMangledName(Method);
1085
1086 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001087 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001088 unsigned MethodLine = 0;
1089 if (!Method->isImplicit()) {
1090 MethodDefUnit = getOrCreateFile(Method->getLocation());
1091 MethodLine = getLineNumber(Method->getLocation());
1092 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001093
1094 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001095 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001096 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001098
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 if (Method->isVirtual()) {
1100 if (Method->isPure())
1101 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1102 else
1103 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 // It doesn't make sense to give a virtual destructor a vtable index,
1106 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001107 // FIXME: Add proper support for debug info for virtual calls in
1108 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1109 // lookup if we have multiple or virtual inheritance.
1110 if (!isa<CXXDestructorDecl>(Method) &&
1111 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001112 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 ContainingType = RecordTy;
1114 }
1115
1116 unsigned Flags = 0;
1117 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001118 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001119 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1121 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001124 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001126 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 }
1128 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001129 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001130 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001131 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001132 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001133 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001134
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1136 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001137 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1138 MethodTy, /*isLocalToUnit=*/false,
1139 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001140 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001141
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001142 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001143
1144 return SP;
1145}
1146
Eric Christophere7b87e52014-10-26 23:40:33 +00001147void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001148 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1149 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001150
1151 // Since we want more than just the individual member decls if we
1152 // have templated functions iterate over every declaration to gather
1153 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001154 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001155 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1156 // If the member is implicit, don't add it to the member list. This avoids
1157 // the member being added to type units by LLVM, while still allowing it
1158 // to be emitted into the type declaration/reference inside the compile
1159 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001160 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001161 // FIXME: Handle Using(Shadow?)Decls here to create
1162 // DW_TAG_imported_declarations inside the class for base decls brought into
1163 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1164 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1165 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001166 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001167 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001168
1169 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1170 continue;
1171
David Blaikiefd580722014-10-06 05:18:55 +00001172 // Reuse the existing member function declaration if it exists.
1173 // It may be associated with the declaration of the type & should be
1174 // reused as we're building the definition.
1175 //
1176 // This situation can arise in the vtable-based debug info reduction where
1177 // implicit members are emitted in a non-vtable TU.
1178 auto MI = SPCache.find(Method->getCanonicalDecl());
1179 EltTys.push_back(MI == SPCache.end()
1180 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001181 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001183}
Guy Benyei11169dd2012-12-18 14:30:41 +00001184
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001185void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001186 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001188 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001189 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001190 unsigned BFlags = 0;
1191 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001192
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001194 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001195
Aaron Ballman574705e2014-03-13 15:41:46 +00001196 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001197 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1198 // virtual base offset offset is -ve. The code generator emits dwarf
1199 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001200 BaseOffset = 0 - CGM.getItaniumVTableContext()
1201 .getVirtualBaseOffsetOffset(RD, Base)
1202 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001203 } else {
1204 // In the MS ABI, store the vbtable offset, which is analogous to the
1205 // vbase offset offset in Itanium.
1206 BaseOffset =
1207 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1208 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001209 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001210 } else
1211 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1212 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1213 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001214
Adrian Prantl21361fb2014-08-29 22:44:27 +00001215 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001216 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001217 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 EltTys.push_back(DTy);
1219 }
1220}
1221
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001222llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001223CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1224 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001225 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001226 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1228 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001229 StringRef Name;
1230 if (TPList)
1231 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001232 switch (TA.getKind()) {
1233 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001235 TemplateParams.push_back(
1236 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001237 } break;
1238 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001240 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1241 TheCU, Name, TTy,
1242 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001243 } break;
1244 case TemplateArgument::Declaration: {
1245 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001246 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001247 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001248 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001249 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001250 // Variable pointer template parameters have a value that is the address
1251 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001252 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001253 V = CGM.GetAddrOfGlobalVar(VD);
1254 // Member function pointers have special support for building them, though
1255 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001256 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001257 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001258 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001259 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001260 // Member data pointers have special handling too to compute the fixed
1261 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001262 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001263 // These five lines (& possibly the above member function pointer
1264 // handling) might be able to be refactored to use similar code in
1265 // CodeGenModule::getMemberPointerConstant
1266 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1267 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001268 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001269 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001270 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001271 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1272 TheCU, Name, TTy,
1273 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001274 } break;
1275 case TemplateArgument::NullPtr: {
1276 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001277 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001278 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001279 // Special case member data pointer null values since they're actually -1
1280 // instead of zero.
1281 if (const MemberPointerType *MPT =
1282 dyn_cast<MemberPointerType>(T.getTypePtr()))
1283 // But treat member function pointers as simple zero integers because
1284 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1285 // CodeGen grows handling for values of non-null member function
1286 // pointers then perhaps we could remove this special case and rely on
1287 // EmitNullMemberPointer for member function pointers.
1288 if (MPT->isMemberDataPointer())
1289 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1290 if (!V)
1291 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001292 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1293 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001294 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001295 case TemplateArgument::Template:
1296 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001297 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001298 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1299 break;
1300 case TemplateArgument::Pack:
1301 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1302 TheCU, Name, nullptr,
1303 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1304 break;
David Majnemer5559d472013-08-24 08:21:10 +00001305 case TemplateArgument::Expression: {
1306 const Expr *E = TA.getAsExpr();
1307 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001308 if (E->isGLValue())
1309 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001310 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001311 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001312 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001313 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1314 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001315 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001316 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001317 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001318 case TemplateArgument::Null:
1319 llvm_unreachable(
1320 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001321 }
1322 }
1323 return DBuilder.getOrCreateArray(TemplateParams);
1324}
1325
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001326llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001327CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001329 if (FD->getTemplatedKind() ==
1330 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001331 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1332 ->getTemplate()
1333 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001334 return CollectTemplateParams(
1335 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001337 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001338}
1339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001340llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1341 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001342 // Always get the full list of parameters, not just the ones from
1343 // the specialization.
1344 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001345 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001346 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001347 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001348}
1349
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001350llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001351 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 return VTablePtrType;
1353
1354 ASTContext &Context = CGM.getContext();
1355
1356 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001357 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001358 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1359 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001362 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1364 return VTablePtrType;
1365}
1366
Guy Benyei11169dd2012-12-18 14:30:41 +00001367StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001368 // Copy the gdb compatible name on the side and use its reference.
1369 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001370}
1371
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001372void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001373 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001374 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1375
1376 // If there is a primary base then it will hold vtable info.
1377 if (RL.getPrimaryBase())
1378 return;
1379
1380 // If this class is not dynamic then there is not any vtable info to collect.
1381 if (!RD->isDynamicClass())
1382 return;
1383
1384 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001386 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 EltTys.push_back(VPTR);
1389}
1390
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001391llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001392 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001393 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001394 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 return T;
1396}
1397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001399 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001400 return getOrCreateStandaloneType(D, Loc);
1401}
1402
1403llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1404 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001405 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001406 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001407 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001408 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001409
1410 // Composite types with UIDs were already retained by DIBuilder
1411 // because they are only referenced by name in the IR.
1412 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1413 if (!CTy->getIdentifier().empty())
1414 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001415 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001416 return T;
1417}
1418
David Blaikie483a9da2014-05-06 18:35:21 +00001419void CGDebugInfo::completeType(const EnumDecl *ED) {
1420 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1421 return;
1422 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001423 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001424 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001425 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001426 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001427 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001428 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001429 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001430}
1431
David Blaikieb2e86eb2013-08-15 20:49:17 +00001432void CGDebugInfo::completeType(const RecordDecl *RD) {
1433 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1434 !CGM.getLangOpts().CPlusPlus)
1435 completeRequiredType(RD);
1436}
1437
1438void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001439 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1440 return;
1441
David Blaikie6943dea2013-08-20 01:28:15 +00001442 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1443 if (CXXDecl->isDynamicClass())
1444 return;
1445
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001446 if (DebugTypeExtRefs && RD->isFromASTFile())
1447 return;
1448
David Blaikieb2e86eb2013-08-15 20:49:17 +00001449 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001450 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001451 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001452 completeClassData(RD);
1453}
1454
1455void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1456 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001457 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001458 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001459 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001460 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001461 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001462 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001463 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001464 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001465 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001466}
1467
David Blaikie0e716b42014-03-03 23:48:23 +00001468static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1469 CXXRecordDecl::method_iterator End) {
1470 for (; I != End; ++I)
1471 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001472 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1473 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001474 return true;
1475 return false;
1476}
1477
1478static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001479 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001480 const RecordDecl *RD,
1481 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001482 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001483 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001484 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001485
David Blaikie0e716b42014-03-03 23:48:23 +00001486 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1487 return false;
1488
1489 if (!LangOpts.CPlusPlus)
1490 return false;
1491
1492 if (!RD->isCompleteDefinitionRequired())
1493 return true;
1494
1495 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1496
1497 if (!CXXDecl)
1498 return false;
1499
1500 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1501 return true;
1502
1503 TemplateSpecializationKind Spec = TSK_Undeclared;
1504 if (const ClassTemplateSpecializationDecl *SD =
1505 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1506 Spec = SD->getSpecializationKind();
1507
1508 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1509 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1510 CXXDecl->method_end()))
1511 return true;
1512
1513 return false;
1514}
1515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001516llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001517 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001518 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001519 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1520 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001521 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001522 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001523 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001524 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001525
David Blaikieb2e86eb2013-08-15 20:49:17 +00001526 return CreateTypeDefinition(Ty);
1527}
1528
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001529llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001530 RecordDecl *RD = Ty->getDecl();
1531
Guy Benyei11169dd2012-12-18 14:30:41 +00001532 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001533 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001534
1535 // Records and classes and unions can all be recursive. To handle them, we
1536 // first generate a debug descriptor for the struct as a forward declaration.
1537 // Then (if it is a definition) we go through and get debug info for all of
1538 // its members. Finally, we create a descriptor for the complete type (which
1539 // may refer to the forward decl if the struct is recursive) and replace all
1540 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001541 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001542
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001543 const RecordDecl *D = RD->getDefinition();
1544 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 return FwdDecl;
1546
David Blaikieadfbf992013-08-18 16:55:33 +00001547 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1548 CollectContainingType(CXXDecl, FwdDecl);
1549
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001551 LexicalBlockStack.emplace_back(&*FwdDecl);
1552 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001553
Guy Benyei11169dd2012-12-18 14:30:41 +00001554 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001555 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001556 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001557
1558 // Note: The split of CXXDecl information here is intentional, the
1559 // gdb tests will depend on a certain ordering at printout. The debug
1560 // information offsets are still correct if we merge them all together
1561 // though.
1562 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1563 if (CXXDecl) {
1564 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1565 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1566 }
1567
Eric Christopher91a31902013-01-16 01:22:32 +00001568 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001570 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
1573 LexicalBlockStack.pop_back();
1574 RegionMap.erase(Ty->getDecl());
1575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001576 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001577 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001578
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001579 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001580 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001581 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001582
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001583 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001584 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001585}
1586
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001587llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1588 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001589 // Ignore protocols.
1590 return getOrCreateType(Ty->getBaseType(), Unit);
1591}
1592
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001593/// \return true if Getter has the default name for the property PD.
1594static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1595 const ObjCMethodDecl *Getter) {
1596 assert(PD);
1597 if (!Getter)
1598 return true;
1599
1600 assert(Getter->getDeclName().isObjCZeroArgSelector());
1601 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001602 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001603}
1604
1605/// \return true if Setter has the default name for the property PD.
1606static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1607 const ObjCMethodDecl *Setter) {
1608 assert(PD);
1609 if (!Setter)
1610 return true;
1611
1612 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001613 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001614 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001615}
1616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001617llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1618 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001619 ObjCInterfaceDecl *ID = Ty->getDecl();
1620 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001621 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001622
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001623 // Return a forward declaration if this type was imported from a clang module.
1624 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1625 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1626 ID->getName(),
1627 getDeclContextDescriptor(ID), Unit, 0);
1628
Guy Benyei11169dd2012-12-18 14:30:41 +00001629 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001630 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001632 auto RuntimeLang =
1633 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001634
1635 // If this is just a forward declaration return a special forward-declaration
1636 // debug type since we won't be able to lay out the entire type.
1637 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001638 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001639 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001640 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1641 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001642 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 return FwdDecl;
1644 }
1645
David Blaikieef8a9512014-05-05 23:23:53 +00001646 return CreateTypeDefinition(Ty, Unit);
1647}
1648
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001649llvm::DIModule *
1650CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantld0929a12015-09-11 01:03:19 +00001651 auto &ModRef = ModuleRefCache[Mod.Signature];
1652 if (ModRef)
1653 return cast<llvm::DIModule>(ModRef);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001654
1655 // Macro definitions that were defined with "-D" on the command line.
1656 SmallString<128> ConfigMacros;
1657 {
1658 llvm::raw_svector_ostream OS(ConfigMacros);
1659 const auto &PPOpts = CGM.getPreprocessorOpts();
1660 unsigned I = 0;
1661 // Translate the macro definitions back into a commmand line.
1662 for (auto &M : PPOpts.Macros) {
1663 if (++I > 1)
1664 OS << " ";
1665 const std::string &Macro = M.first;
1666 bool Undef = M.second;
1667 OS << "\"-" << (Undef ? 'U' : 'D');
1668 for (char c : Macro)
1669 switch (c) {
1670 case '\\' : OS << "\\\\"; break;
1671 case '"' : OS << "\\\""; break;
1672 default: OS << c;
1673 }
1674 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001675 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001676 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001677 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantlb67dbce2015-09-11 18:45:02 +00001678 auto *CU = DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.ModuleName,
1679 Mod.Path, TheCU->getProducer(), true,
1680 StringRef(), 0, Mod.ASTFile,
1681 llvm::DIBuilder::FullDebug, Mod.Signature);
1682 llvm::DIModule *M =
1683 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, Mod.Path,
1684 CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001685 DIB.finalize();
Adrian Prantld0929a12015-09-11 01:03:19 +00001686 ModRef.reset(M);
1687 return M;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001688}
1689
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001690llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1691 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001692 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001694 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001695 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001696
1697 // Bit size, align and offset of the type.
1698 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1699 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1700
1701 unsigned Flags = 0;
1702 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001703 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001704
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001705 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001706 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001707 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001708
David Blaikieef8a9512014-05-05 23:23:53 +00001709 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001710 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001711
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001712 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001713 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001714 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001715
1716 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001717 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001718
1719 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1720 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001721 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001722 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001723 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001724 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001725
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001726 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001727 EltTys.push_back(InhTag);
1728 }
1729
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001730 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001731 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001732 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001733 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 unsigned PLine = getLineNumber(Loc);
1735 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1736 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001737 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1738 PD->getName(), PUnit, PLine,
1739 hasDefaultGetterName(PD, Getter) ? ""
1740 : getSelectorName(PD->getGetterName()),
1741 hasDefaultSetterName(PD, Setter) ? ""
1742 : getSelectorName(PD->getSetterName()),
1743 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001744 EltTys.push_back(PropertyNode);
1745 }
1746
1747 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1748 unsigned FieldNo = 0;
1749 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1750 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001751 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001752 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001753 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001754
Guy Benyei11169dd2012-12-18 14:30:41 +00001755 StringRef FieldName = Field->getName();
1756
1757 // Ignore unnamed fields.
1758 if (FieldName.empty())
1759 continue;
1760
1761 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001762 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001763 unsigned FieldLine = getLineNumber(Field->getLocation());
1764 QualType FType = Field->getType();
1765 uint64_t FieldSize = 0;
1766 unsigned FieldAlign = 0;
1767
1768 if (!FType->isIncompleteArrayType()) {
1769
1770 // Bit size, align and offset of the type.
1771 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001772 ? Field->getBitWidthValue(CGM.getContext())
1773 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 FieldAlign = CGM.getContext().getTypeAlign(FType);
1775 }
1776
1777 uint64_t FieldOffset;
1778 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1779 // We don't know the runtime offset of an ivar if we're using the
1780 // non-fragile ABI. For bitfields, use the bit offset into the first
1781 // byte of storage of the bitfield. For other fields, use zero.
1782 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001783 FieldOffset =
1784 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001785 FieldOffset %= CGM.getContext().getCharWidth();
1786 } else {
1787 FieldOffset = 0;
1788 }
1789 } else {
1790 FieldOffset = RL.getFieldOffset(FieldNo);
1791 }
1792
1793 unsigned Flags = 0;
1794 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001795 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001796 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001797 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001798 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001799 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001800
Craig Topper8a13c412014-05-21 05:09:00 +00001801 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001802 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001803 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001804 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001805 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001806 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001807 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001808 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1810 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001811 PropertyNode = DBuilder.createObjCProperty(
1812 PD->getName(), PUnit, PLine,
1813 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1814 PD->getGetterName()),
1815 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1816 PD->getSetterName()),
1817 PD->getPropertyAttributes(),
1818 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001819 }
1820 }
1821 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001822 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1823 FieldSize, FieldAlign, FieldOffset, Flags,
1824 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001825 EltTys.push_back(FieldTy);
1826 }
1827
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001828 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001829 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001830
Guy Benyei11169dd2012-12-18 14:30:41 +00001831 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001832 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001833}
1834
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001835llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1836 llvm::DIFile *Unit) {
1837 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001838 int64_t Count = Ty->getNumElements();
1839 if (Count == 0)
1840 // If number of elements are not known then this is an unbounded array.
1841 // Use Count == -1 to express such arrays.
1842 Count = -1;
1843
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001844 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001845 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001846
1847 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1848 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1849
1850 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1851}
1852
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001853llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001854 uint64_t Size;
1855 uint64_t Align;
1856
1857 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1858 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1859 Size = 0;
1860 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001861 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 } else if (Ty->isIncompleteArrayType()) {
1863 Size = 0;
1864 if (Ty->getElementType()->isIncompleteType())
1865 Align = 0;
1866 else
1867 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001868 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001869 Size = 0;
1870 Align = 0;
1871 } else {
1872 // Size and align of the whole array, not the element type.
1873 Size = CGM.getContext().getTypeSize(Ty);
1874 Align = CGM.getContext().getTypeAlign(Ty);
1875 }
1876
1877 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1878 // interior arrays, do we care? Why aren't nested arrays represented the
1879 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001880 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001881 QualType EltTy(Ty, 0);
1882 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1883 // If the number of elements is known, then count is that number. Otherwise,
1884 // it's -1. This allows us to represent a subrange with an array of 0
1885 // elements, like this:
1886 //
1887 // struct foo {
1888 // int x[0];
1889 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001890 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1892 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001893
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 // FIXME: Verify this is right for VLAs.
1895 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1896 EltTy = Ty->getElementType();
1897 }
1898
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001899 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001900
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001901 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1902 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001903}
1904
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001905llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1906 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001907 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1908 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001909}
1910
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001911llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1912 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001913 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1914 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001915}
1916
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001917llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1918 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001919 uint64_t Size =
1920 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001921 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001922 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001923 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001924 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001925
1926 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001927 Ty->getPointeeType()->getAs<FunctionProtoType>();
1928 return DBuilder.createMemberPointerType(
1929 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1930 Ty->getClass(), FPT->getTypeQuals())),
1931 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001932 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001933}
1934
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001935llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001936 // Ignore the atomic wrapping
1937 // FIXME: What is the correct representation?
1938 return getOrCreateType(Ty->getValueType(), U);
1939}
1940
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001941llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001942 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001943
Guy Benyei11169dd2012-12-18 14:30:41 +00001944 uint64_t Size = 0;
1945 uint64_t Align = 0;
1946 if (!ED->getTypeForDecl()->isIncompleteType()) {
1947 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1948 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1949 }
1950
Manman Rene0064d82013-08-29 23:19:58 +00001951 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1952
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001953 bool isImportedFromModule =
1954 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
1955
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 // If this is just a forward declaration, construct an appropriately
1957 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001958 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001959 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 unsigned Line = getLineNumber(ED->getLocation());
1962 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001963 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001964 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001965 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001966 ReplaceMap.emplace_back(
1967 std::piecewise_construct, std::make_tuple(Ty),
1968 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001969 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 }
1971
David Blaikie483a9da2014-05-06 18:35:21 +00001972 return CreateTypeDefinition(Ty);
1973}
1974
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001975llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001976 const EnumDecl *ED = Ty->getDecl();
1977 uint64_t Size = 0;
1978 uint64_t Align = 0;
1979 if (!ED->getTypeForDecl()->isIncompleteType()) {
1980 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1981 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1982 }
1983
1984 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1985
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001986 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001987 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001988 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001989 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001990 Enumerators.push_back(DBuilder.createEnumerator(
1991 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 }
1993
1994 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001995 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001996
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001997 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001999 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002000 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002001 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2002 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2003 Line, Size, Align, EltArray, ClassTy,
2004 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002005}
2006
David Blaikie05491062013-01-21 04:37:12 +00002007static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2008 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002009 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002010 Qualifiers InnerQuals = T.getLocalQualifiers();
2011 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2012 // that is already there.
2013 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2014 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002015 QualType LastT = T;
2016 switch (T->getTypeClass()) {
2017 default:
David Blaikie05491062013-01-21 04:37:12 +00002018 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002019 case Type::TemplateSpecialization: {
2020 const auto *Spec = cast<TemplateSpecializationType>(T);
2021 if (Spec->isTypeAlias())
2022 return C.getQualifiedType(T.getTypePtr(), Quals);
2023 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002024 break;
2025 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 case Type::TypeOfExpr:
2027 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2028 break;
2029 case Type::TypeOf:
2030 T = cast<TypeOfType>(T)->getUnderlyingType();
2031 break;
2032 case Type::Decltype:
2033 T = cast<DecltypeType>(T)->getUnderlyingType();
2034 break;
2035 case Type::UnaryTransform:
2036 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2037 break;
2038 case Type::Attributed:
2039 T = cast<AttributedType>(T)->getEquivalentType();
2040 break;
2041 case Type::Elaborated:
2042 T = cast<ElaboratedType>(T)->getNamedType();
2043 break;
2044 case Type::Paren:
2045 T = cast<ParenType>(T)->getInnerType();
2046 break;
David Blaikie05491062013-01-21 04:37:12 +00002047 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002049 break;
2050 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002051 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002052 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002053 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 break;
2055 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002056
Guy Benyei11169dd2012-12-18 14:30:41 +00002057 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002058 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 } while (true);
2060}
2061
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002062llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002063
2064 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002065 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002066
David Blaikief427b002014-05-06 03:42:01 +00002067 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 if (it != TypeCache.end()) {
2069 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002070 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002071 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002072 }
2073
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002074 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002075}
2076
David Blaikie0e716b42014-03-03 23:48:23 +00002077void CGDebugInfo::completeTemplateDefinition(
2078 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002079 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2080 return;
2081
David Blaikie0e716b42014-03-03 23:48:23 +00002082 completeClassData(&SD);
2083 // In case this type has no member function definitions being emitted, ensure
2084 // it is retained
2085 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2086}
2087
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002088llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002090 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002091
2092 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002093 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002094
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002095 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002096 return T;
2097
Adrian Prantlca844182015-09-11 17:23:03 +00002098 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002099 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002100
2101 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002102 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002103
Guy Benyei11169dd2012-12-18 14:30:41 +00002104 return Res;
2105}
2106
Eric Christopher1ecc5632013-06-07 22:54:39 +00002107unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002108 // The assumption is that the number of ivars can only increase
2109 // monotonically, so it is safe to just use their current number as
2110 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002111 unsigned Sum = 0;
2112 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002113 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002114 ++Sum;
2115
2116 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002117}
2118
2119ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2120 switch (Ty->getTypeClass()) {
2121 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002122 return getObjCInterfaceDecl(
2123 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002124 case Type::ObjCInterface:
2125 return cast<ObjCInterfaceType>(Ty)->getDecl();
2126 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002127 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002128 }
2129}
2130
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002131llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
2132 if (!DebugTypeExtRefs || !D || !D->isFromASTFile())
2133 return nullptr;
2134
2135 llvm::DIModule *ModuleRef = nullptr;
2136 auto *Reader = CGM.getContext().getExternalSource();
2137 auto Idx = D->getOwningModuleID();
2138 auto Info = Reader->getSourceDescriptor(Idx);
2139 if (Info)
2140 ModuleRef = getOrCreateModuleRef(*Info);
2141 return ModuleRef;
2142}
2143
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002144llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002145 // Handle qualifiers, which recursively handles what they refer to.
2146 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002147 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002148
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 // Work out details of type.
2150 switch (Ty->getTypeClass()) {
2151#define TYPE(Class, Base)
2152#define ABSTRACT_TYPE(Class, Base)
2153#define NON_CANONICAL_TYPE(Class, Base)
2154#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2155#include "clang/AST/TypeNodes.def"
2156 llvm_unreachable("Dependent types cannot show up in debug information");
2157
2158 case Type::ExtVector:
2159 case Type::Vector:
2160 return CreateType(cast<VectorType>(Ty), Unit);
2161 case Type::ObjCObjectPointer:
2162 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2163 case Type::ObjCObject:
2164 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2165 case Type::ObjCInterface:
2166 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2167 case Type::Builtin:
2168 return CreateType(cast<BuiltinType>(Ty));
2169 case Type::Complex:
2170 return CreateType(cast<ComplexType>(Ty));
2171 case Type::Pointer:
2172 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002173 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002174 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002175 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002176 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002177 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002178 case Type::BlockPointer:
2179 return CreateType(cast<BlockPointerType>(Ty), Unit);
2180 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002181 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002182 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002183 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002184 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002185 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 case Type::FunctionProto:
2187 case Type::FunctionNoProto:
2188 return CreateType(cast<FunctionType>(Ty), Unit);
2189 case Type::ConstantArray:
2190 case Type::VariableArray:
2191 case Type::IncompleteArray:
2192 return CreateType(cast<ArrayType>(Ty), Unit);
2193
2194 case Type::LValueReference:
2195 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2196 case Type::RValueReference:
2197 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2198
2199 case Type::MemberPointer:
2200 return CreateType(cast<MemberPointerType>(Ty), Unit);
2201
2202 case Type::Atomic:
2203 return CreateType(cast<AtomicType>(Ty), Unit);
2204
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002206 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2207
David Blaikie42edade2014-11-11 20:44:45 +00002208 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002209 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 case Type::Elaborated:
2211 case Type::Paren:
2212 case Type::SubstTemplateTypeParm:
2213 case Type::TypeOfExpr:
2214 case Type::TypeOf:
2215 case Type::Decltype:
2216 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002217 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002218 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002219 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002220
David Blaikie42edade2014-11-11 20:44:45 +00002221 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002222}
2223
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002224llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2225 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002226 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002228 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002229
2230 // We may have cached a forward decl when we could have created
2231 // a non-forward decl. Go ahead and create a non-forward decl
2232 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002233 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002234 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002235
2236 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002237 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002238
2239 // Propagate members from the declaration to the definition
2240 // CreateType(const RecordType*) will overwrite this with the members in the
2241 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002242 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002243
Guy Benyei11169dd2012-12-18 14:30:41 +00002244 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002245 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 return Res;
2247}
2248
2249// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002250llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002251 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002252
Guy Benyei11169dd2012-12-18 14:30:41 +00002253 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002254 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 unsigned Line = getLineNumber(RD->getLocation());
2256 StringRef RDName = getClassName(RD);
2257
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002258 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002259
David Blaikied2785892013-08-18 17:36:19 +00002260 // If we ended up creating the type during the context chain construction,
2261 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002262 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002263 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002264 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002265 return T;
David Blaikied2785892013-08-18 17:36:19 +00002266
Adrian Prantl381e7552014-02-04 21:29:50 +00002267 // If this is just a forward or incomplete declaration, construct an
2268 // appropriately marked node and just return it.
2269 const RecordDecl *D = RD->getDefinition();
2270 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002271 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002272
2273 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2274 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002275
Manman Rene0064d82013-08-29 23:19:58 +00002276 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002278 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002279 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2280 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002281
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002282 RegionMap[Ty->getDecl()].reset(RealDecl);
2283 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002284
David Blaikieadfbf992013-08-18 16:55:33 +00002285 if (const ClassTemplateSpecializationDecl *TSpecial =
2286 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002287 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002288 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002289 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002290}
2291
David Blaikieadfbf992013-08-18 16:55:33 +00002292void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002294 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002295 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002296 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2297 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002298 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002299 while (1) {
2300 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2301 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2302 if (PBT && !BRL.isPrimaryBaseVirtual())
2303 PBase = PBT;
2304 else
2305 break;
2306 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002307 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002308 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2309 getOrCreateFile(RD->getLocation())));
2310 } else if (RD->isDynamicClass())
2311 ContainingType = RealDecl;
2312
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002313 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002314}
2315
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002317 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002318 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002319 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2320 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002321 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002322 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002323 *Offset += FieldSize;
2324 return Ty;
2325}
2326
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002327void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002328 StringRef &Name,
2329 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002330 llvm::DIScope *&FDContext,
2331 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002332 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002333 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2334 Name = getFunctionName(FD);
2335 // Use mangled name as linkage name for C/C++ functions.
2336 if (FD->hasPrototype()) {
2337 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002338 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002339 }
2340 // No need to replicate the linkage name if it isn't different from the
2341 // subprogram name, no need to have it at all unless coverage is enabled or
2342 // debug is set to more than just line tables.
2343 if (LinkageName == Name ||
2344 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2345 !CGM.getCodeGenOpts().EmitGcovNotes &&
2346 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2347 LinkageName = StringRef();
2348
2349 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2350 if (const NamespaceDecl *NSDecl =
2351 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2352 FDContext = getOrCreateNameSpace(NSDecl);
2353 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002354 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2355 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2356 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2357 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002358 // Collect template parameters.
2359 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2360 }
2361}
2362
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002363void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002364 unsigned &LineNo, QualType &T,
2365 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002366 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002367 Unit = getOrCreateFile(VD->getLocation());
2368 LineNo = getLineNumber(VD->getLocation());
2369
2370 setLocation(VD->getLocation());
2371
2372 T = VD->getType();
2373 if (T->isIncompleteArrayType()) {
2374 // CodeGen turns int[] into int[1] so we'll do the same here.
2375 llvm::APInt ConstVal(32, 1);
2376 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2377
2378 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2379 ArrayType::Normal, 0);
2380 }
2381
2382 Name = VD->getName();
2383 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2384 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2385 LinkageName = CGM.getMangledName(VD);
2386 if (LinkageName == Name)
2387 LinkageName = StringRef();
2388
2389 // Since we emit declarations (DW_AT_members) for static members, place the
2390 // definition of those static members in the namespace they were declared in
2391 // in the source code (the lexical decl context).
2392 // FIXME: Generalize this for even non-member global variables where the
2393 // declaration and definition may have different lexical decl contexts, once
2394 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002395 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2396 : VD->getDeclContext();
2397 // When a record type contains an in-line initialization of a static data
2398 // member, and the record type is marked as __declspec(dllexport), an implicit
2399 // definition of the member will be created in the record context. DWARF
2400 // doesn't seem to have a nice way to describe this in a form that consumers
2401 // are likely to understand, so fake the "normal" situation of a definition
2402 // outside the class by putting it in the global scope.
2403 if (DC->isRecord())
2404 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002405
2406 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2407 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002408}
2409
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002410llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002411CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002413 StringRef Name, LinkageName;
2414 unsigned Flags = 0;
2415 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002416 llvm::DIFile *Unit = getOrCreateFile(Loc);
2417 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002418 unsigned Line = getLineNumber(Loc);
2419
2420 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2421 TParamsArray, Flags);
2422 // Build function type.
2423 SmallVector<QualType, 16> ArgTypes;
2424 for (const ParmVarDecl *Parm: FD->parameters())
2425 ArgTypes.push_back(Parm->getType());
2426 QualType FnType =
2427 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2428 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002429 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002430 DContext, Name, LinkageName, Unit, Line,
2431 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002432 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002433 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002434 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002435 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2436 std::make_tuple(CanonDecl),
2437 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002438 return SP;
2439}
2440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002442CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2443 QualType T;
2444 StringRef Name, LinkageName;
2445 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002446 llvm::DIFile *Unit = getOrCreateFile(Loc);
2447 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002448 unsigned Line = getLineNumber(Loc);
2449
2450 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002451 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2452 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2453 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002454 FwdDeclReplaceMap.emplace_back(
2455 std::piecewise_construct,
2456 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2457 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002458 return GV;
2459}
2460
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002462 // We only need a declaration (not a definition) of the type - so use whatever
2463 // we would otherwise do to get a type for a pointee. (forward declarations in
2464 // limited debug info, full definitions (if the type definition is available)
2465 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002466 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2467 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002468 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002469 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002470
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002471 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002473
2474 // No definition for now. Emit a forward definition that might be
2475 // merged with a potential upcoming definition.
2476 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2477 return getFunctionForwardDeclaration(FD);
2478 else if (const auto *VD = dyn_cast<VarDecl>(D))
2479 return getGlobalVariableForwardDeclaration(VD);
2480
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002481 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002482}
2483
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002485 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002486 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002487
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002489 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002490 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002491
2492 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002493 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002494
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002495 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002496 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002497 if (const CXXMethodDecl *MD =
2498 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002499 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002500 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002501 }
2502 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002504 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002505 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 return SP;
2507 }
2508
Aaron Ballman86c93902014-03-06 23:45:36 +00002509 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002510 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002511 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002512 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002513 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 return SP;
2515 }
2516 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002517 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002518}
2519
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002520// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002521// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002522llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002523 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002524 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002525 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002526 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2527 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002528 return DBuilder.createSubroutineType(F,
2529 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002530
2531 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2532 return getOrCreateMethodType(Method, F);
2533 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2534 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002535 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002536
2537 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002538 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002539
2540 // Replace the instancetype keyword with the actual type.
2541 if (ResultTy == CGM.getContext().getObjCInstanceType())
2542 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002543 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002544
Adrian Prantl7bec9032013-05-10 21:08:31 +00002545 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002546 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002547 QualType SelfDeclTy;
2548 if (auto *SelfDecl = OMethod->getSelfDecl())
2549 SelfDeclTy = SelfDecl->getType();
2550 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2551 if (FPT->getNumParams() > 1)
2552 SelfDeclTy = FPT->getParamType(0);
2553 if (!SelfDeclTy.isNull())
2554 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002556 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002557 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002558 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002559 for (const auto *PI : OMethod->params())
2560 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002561 // Variadic methods need a special marker at the end of the type list.
2562 if (OMethod->isVariadic())
2563 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002564
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002565 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 return DBuilder.createSubroutineType(F, EltTypeArray);
2567 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002568
Adrian Prantl800faef2014-02-25 23:42:18 +00002569 // Handle variadic function types; they need an additional
2570 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002571 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2572 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002573 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002574 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2575 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2576 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2577 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2578 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002579 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002580 return DBuilder.createSubroutineType(F, EltTypeArray);
2581 }
2582
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002583 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002584}
2585
Eric Christophere7b87e52014-10-26 23:40:33 +00002586void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2587 SourceLocation ScopeLoc, QualType FnType,
2588 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002589
2590 StringRef Name;
2591 StringRef LinkageName;
2592
2593 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2594
2595 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002596 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002597
Guy Benyei11169dd2012-12-18 14:30:41 +00002598 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002599 llvm::DIFile *Unit = getOrCreateFile(Loc);
2600 llvm::DIScope *FDContext = Unit;
2601 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 if (!HasDecl) {
2603 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002604 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002605 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002606 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002607 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002609 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002610 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002611 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002612 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 return;
2614 }
2615 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002616 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2617 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002618 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2619 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002620 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002621 } else {
2622 // Use llvm function name.
2623 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002624 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 }
2626 if (!Name.empty() && Name[0] == '\01')
2627 Name = Name.substr(1);
2628
Adrian Prantl42d71b92014-04-10 23:21:53 +00002629 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002630 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002631 // Artificial functions without a location should not silently reuse CurLoc.
2632 if (Loc.isInvalid())
2633 CurLoc = SourceLocation();
2634 }
2635 unsigned LineNo = getLineNumber(Loc);
2636 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002637
Eric Christopher8018e412014-03-27 18:50:35 +00002638 // FIXME: The function declaration we're constructing here is mostly reusing
2639 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2640 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2641 // all subprograms instead of the actual context since subprogram definitions
2642 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002643 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002644 FDContext, Name, LinkageName, Unit, LineNo,
2645 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2646 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002647 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002648 // We might get here with a VarDecl in the case we're generating
2649 // code for the initialization of globals. Do not record these decls
2650 // as they will overwrite the actual VarDecl Decl in the cache.
2651 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002652 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002653
Adrian Prantlbebb8932014-03-21 21:01:58 +00002654 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002655 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002656
Guy Benyei11169dd2012-12-18 14:30:41 +00002657 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002658 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002659}
2660
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002661void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2662 QualType FnType) {
2663 StringRef Name;
2664 StringRef LinkageName;
2665
2666 const Decl *D = GD.getDecl();
2667 if (!D)
2668 return;
2669
2670 unsigned Flags = 0;
2671 llvm::DIFile *Unit = getOrCreateFile(Loc);
2672 llvm::DIScope *FDContext = Unit;
2673 llvm::DINodeArray TParamsArray;
2674 if (isa<FunctionDecl>(D)) {
2675 // If there is a DISubprogram for this function available then use it.
2676 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2677 TParamsArray, Flags);
2678 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2679 Name = getObjCMethodName(OMD);
2680 Flags |= llvm::DINode::FlagPrototyped;
2681 } else {
2682 llvm_unreachable("not a function or ObjC method");
2683 }
2684 if (!Name.empty() && Name[0] == '\01')
2685 Name = Name.substr(1);
2686
2687 if (D->isImplicit()) {
2688 Flags |= llvm::DINode::FlagArtificial;
2689 // Artificial functions without a location should not silently reuse CurLoc.
2690 if (Loc.isInvalid())
2691 CurLoc = SourceLocation();
2692 }
2693 unsigned LineNo = getLineNumber(Loc);
2694 unsigned ScopeLine = 0;
2695
2696 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2697 getOrCreateFunctionType(D, FnType, Unit),
2698 false /*internalLinkage*/, true /*definition*/,
2699 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2700 TParamsArray.get(),
2701 getFunctionDeclaration(D));
2702}
2703
David Blaikie835afb22015-01-21 23:08:17 +00002704void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 // Update our current location
2706 setLocation(Loc);
2707
Eric Christophere7b87e52014-10-26 23:40:33 +00002708 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2709 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002710
Adrian Prantle83b1302014-01-07 22:05:52 +00002711 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002712 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002713 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002714}
2715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002717 llvm::MDNode *Back = nullptr;
2718 if (!LexicalBlockStack.empty())
2719 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002720 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002721 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002722 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002723}
2724
Eric Christopher0fdcb312013-05-16 00:52:20 +00002725void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2726 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002727 // Set our current location.
2728 setLocation(Loc);
2729
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002731 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2732 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002733
2734 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2735 return;
2736
2737 // Create a new lexical block and push it on the stack.
2738 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002739}
2740
Eric Christopher0fdcb312013-05-16 00:52:20 +00002741void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2742 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2744
2745 // Provide an entry in the line table for the end of the block.
2746 EmitLocation(Builder, Loc);
2747
David Blaikie60a877b2014-10-22 19:34:33 +00002748 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2749 return;
2750
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 LexicalBlockStack.pop_back();
2752}
2753
Guy Benyei11169dd2012-12-18 14:30:41 +00002754void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2755 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2756 unsigned RCount = FnBeginRegionCount.back();
2757 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2758
2759 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002760 while (LexicalBlockStack.size() != RCount) {
2761 // Provide an entry in the line table for the end of the block.
2762 EmitLocation(Builder, CurLoc);
2763 LexicalBlockStack.pop_back();
2764 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 FnBeginRegionCount.pop_back();
2766}
2767
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002769 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002770
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002771 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 QualType FType;
2773 uint64_t FieldSize, FieldOffset;
2774 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002775
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002776 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002777 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002778
2779 FieldOffset = 0;
2780 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2781 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2782 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2783 FType = CGM.getContext().IntTy;
2784 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2785 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2786
2787 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2788 if (HasCopyAndDispose) {
2789 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002790 EltTys.push_back(
2791 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2792 EltTys.push_back(
2793 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 }
2795 bool HasByrefExtendedLayout;
2796 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002797 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2798 HasByrefExtendedLayout) &&
2799 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002800 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002801 EltTys.push_back(
2802 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002803 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002804
Guy Benyei11169dd2012-12-18 14:30:41 +00002805 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2806 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002807 CGM.getTarget().getPointerAlign(0))) {
2808 CharUnits FieldOffsetInBytes =
2809 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2810 CharUnits AlignedOffsetInBytes =
2811 FieldOffsetInBytes.RoundUpToAlignment(Align);
2812 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002813
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 if (NumPaddingBytes.isPositive()) {
2815 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2816 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2817 pad, ArrayType::Normal, 0);
2818 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2819 }
2820 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002821
Guy Benyei11169dd2012-12-18 14:30:41 +00002822 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002823 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 FieldSize = CGM.getContext().getTypeSize(FType);
2825 FieldAlign = CGM.getContext().toBits(Align);
2826
Eric Christopherb2a008c2013-05-16 00:45:12 +00002827 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002828 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2829 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002830 EltTys.push_back(FieldTy);
2831 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002832
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002833 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002834
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002835 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002836
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002838 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002839}
2840
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002841void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2842 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002843 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002844 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2846
David Blaikie7fceebf2013-08-19 03:37:48 +00002847 bool Unwritten =
2848 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2849 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002850 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002851 if (!Unwritten)
2852 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002853 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 uint64_t XOffset = 0;
2855 if (VD->hasAttr<BlocksAttr>())
2856 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002857 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 Ty = getOrCreateType(VD->getType(), Unit);
2859
2860 // If there is no debug info for this type then do not emit debug info
2861 // for this variable.
2862 if (!Ty)
2863 return;
2864
Guy Benyei11169dd2012-12-18 14:30:41 +00002865 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002866 unsigned Line = 0;
2867 unsigned Column = 0;
2868 if (!Unwritten) {
2869 Line = getLineNumber(VD->getLocation());
2870 Column = getColumnNumber(VD->getLocation());
2871 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002872 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002873 unsigned Flags = 0;
2874 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002875 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 // If this is the first argument and it is implicit then
2877 // give it an object pointer flag.
2878 // FIXME: There has to be a better way to do this, but for static
2879 // functions there won't be an implicit param at arg1 and
2880 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002881 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002882 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002883 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002884 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2885 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002886 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002887
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002888 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002889
2890 StringRef Name = VD->getName();
2891 if (!Name.empty()) {
2892 if (VD->hasAttr<BlocksAttr>()) {
2893 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002894 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 // offset of __forwarding field
2896 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002897 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002898 Expr.push_back(offset.getQuantity());
2899 Expr.push_back(llvm::dwarf::DW_OP_deref);
2900 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 // offset of x field
2902 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002903 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002904
2905 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002906 auto *D = ArgNo
2907 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2908 *ArgNo, Unit, Line, Ty)
2909 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2910 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002911
Guy Benyei11169dd2012-12-18 14:30:41 +00002912 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002913 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2914 llvm::DebugLoc::get(Line, Column, Scope),
2915 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002916 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002917 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002918 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002919 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2920 // If VD is an anonymous union then Storage represents value for
2921 // all union fields.
2922 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2923 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002924 // GDB has trouble finding local variables in anonymous unions, so we emit
2925 // artifical local variables for each of the members.
2926 //
2927 // FIXME: Remove this code as soon as GDB supports this.
2928 // The debug info verifier in LLVM operates based on the assumption that a
2929 // variable has the same size as its storage and we had to disable the check
2930 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002931 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002932 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002933 StringRef FieldName = Field->getName();
2934
2935 // Ignore unnamed fields. Do not ignore unnamed records.
2936 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2937 continue;
2938
2939 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002940 auto *D = DBuilder.createAutoVariable(
2941 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2942 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002943
2944 // Insert an llvm.dbg.declare into the current block.
2945 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2946 llvm::DebugLoc::get(Line, Column, Scope),
2947 Builder.GetInsertBlock());
2948 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002949 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002951
2952 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002953 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002954 ArgNo
2955 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2956 Ty, CGM.getLangOpts().Optimize,
2957 Flags)
2958 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2959 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002960
2961 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002962 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2963 llvm::DebugLoc::get(Line, Column, Scope),
2964 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002965}
2966
2967void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2968 llvm::Value *Storage,
2969 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002970 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002971 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002972}
2973
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002974llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2975 llvm::DIType *Ty) {
2976 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002977 if (CachedTy)
2978 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002979 return DBuilder.createObjectPointerType(Ty);
2980}
2981
Eric Christophere7b87e52014-10-26 23:40:33 +00002982void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2983 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002984 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002985 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002987
Craig Topper8a13c412014-05-21 05:09:00 +00002988 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002990
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002992
Guy Benyei11169dd2012-12-18 14:30:41 +00002993 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002994 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2995 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002996 if (isByRef)
2997 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002998 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 Ty = getOrCreateType(VD->getType(), Unit);
3000
3001 // Self is passed along as an implicit non-arg variable in a
3002 // block. Mark it as the object pointer.
3003 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003004 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003005
3006 // Get location information.
3007 unsigned Line = getLineNumber(VD->getLocation());
3008 unsigned Column = getColumnNumber(VD->getLocation());
3009
3010 const llvm::DataLayout &target = CGM.getDataLayout();
3011
3012 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003013 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3015
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003016 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003017 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003018 addr.push_back(llvm::dwarf::DW_OP_deref);
3019 addr.push_back(llvm::dwarf::DW_OP_plus);
3020 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003021 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003022 addr.push_back(llvm::dwarf::DW_OP_deref);
3023 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003025 offset =
3026 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003027 addr.push_back(offset.getQuantity());
3028 addr.push_back(llvm::dwarf::DW_OP_deref);
3029 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 // offset of x field
3031 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003032 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 }
3034
3035 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003036 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003037 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003038 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003039
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003041 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3042 if (InsertPoint)
3043 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3044 InsertPoint);
3045 else
3046 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3047 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003048}
3049
Guy Benyei11169dd2012-12-18 14:30:41 +00003050void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3051 unsigned ArgNo,
3052 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003053 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003054 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003055}
3056
3057namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003058struct BlockLayoutChunk {
3059 uint64_t OffsetInBits;
3060 const BlockDecl::Capture *Capture;
3061};
3062bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3063 return l.OffsetInBits < r.OffsetInBits;
3064}
Guy Benyei11169dd2012-12-18 14:30:41 +00003065}
3066
3067void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003068 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003069 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003070 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003072 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 ASTContext &C = CGM.getContext();
3074 const BlockDecl *blockDecl = block.getBlockDecl();
3075
3076 // Collect some general information about the block's location.
3077 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003078 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 unsigned line = getLineNumber(loc);
3080 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003081
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003083 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003084
3085 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003086 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003087
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003088 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3090 blockLayout->getElementOffsetInBits(0),
3091 tunit, tunit));
3092 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3093 blockLayout->getElementOffsetInBits(1),
3094 tunit, tunit));
3095 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3096 blockLayout->getElementOffsetInBits(2),
3097 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003098 auto *FnTy = block.getBlockExpr()->getFunctionType();
3099 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3100 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 blockLayout->getElementOffsetInBits(3),
3102 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003103 fields.push_back(createFieldType(
3104 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3105 ? C.getBlockDescriptorExtendedType()
3106 : C.getBlockDescriptorType()),
3107 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003108
3109 // We want to sort the captures by offset, not because DWARF
3110 // requires this, but because we're paranoid about debuggers.
3111 SmallVector<BlockLayoutChunk, 8> chunks;
3112
3113 // 'this' capture.
3114 if (blockDecl->capturesCXXThis()) {
3115 BlockLayoutChunk chunk;
3116 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003117 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003118 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003119 chunks.push_back(chunk);
3120 }
3121
3122 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003123 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 const VarDecl *variable = capture.getVariable();
3125 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3126
3127 // Ignore constant captures.
3128 if (captureInfo.isConstant())
3129 continue;
3130
3131 BlockLayoutChunk chunk;
3132 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003133 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003134 chunk.Capture = &capture;
3135 chunks.push_back(chunk);
3136 }
3137
3138 // Sort by offset.
3139 llvm::array_pod_sort(chunks.begin(), chunks.end());
3140
Eric Christophere7b87e52014-10-26 23:40:33 +00003141 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3142 e = chunks.end();
3143 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 uint64_t offsetInBits = i->OffsetInBits;
3145 const BlockDecl::Capture *capture = i->Capture;
3146
3147 // If we have a null capture, this must be the C++ 'this' capture.
3148 if (!capture) {
3149 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003150 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 QualType type = method->getThisType(C);
3152
3153 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3154 offsetInBits, tunit, tunit));
3155 continue;
3156 }
3157
3158 const VarDecl *variable = capture->getVariable();
3159 StringRef name = variable->getName();
3160
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003161 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003162 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003163 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003164
3165 // FIXME: this creates a second copy of this type!
3166 uint64_t xoffset;
3167 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003168 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3169 fieldType =
3170 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3171 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003173 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3174 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 }
3176 fields.push_back(fieldType);
3177 }
3178
3179 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003180 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3181 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003182
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003183 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003185 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003186 tunit, typeName.str(), tunit, line,
3187 CGM.getContext().toBits(block.BlockSize),
3188 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003189 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3190
3191 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003192 unsigned flags = llvm::DINode::FlagArtificial;
3193 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003194
3195 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003196 auto *debugVar = DBuilder.createParameterVariable(
3197 scope, Arg->getName(), ArgNo, tunit, line, type,
3198 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003199
Adrian Prantl616bef42013-03-14 21:52:59 +00003200 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003201 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003202 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003203 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003204 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003205 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003206
Adrian Prantl616bef42013-03-14 21:52:59 +00003207 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003208 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3209 llvm::DebugLoc::get(line, column, scope),
3210 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003211}
3212
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003213llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003214CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3215 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003216 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003217
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003218 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003219 if (MI != StaticDataMemberCache.end()) {
3220 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003221 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003222 }
David Blaikiece763042013-08-20 21:49:21 +00003223
3224 // If the member wasn't found in the cache, lazily construct and add it to the
3225 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003226 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003227 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003228 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003229}
3230
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003231llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3232 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3233 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3234 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003235
3236 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003237 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003238 StringRef FieldName = Field->getName();
3239
3240 // Ignore unnamed fields, but recurse into anonymous records.
3241 if (FieldName.empty()) {
3242 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3243 if (RT)
3244 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3245 Var, DContext);
3246 continue;
3247 }
3248 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003249 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3250 LineNo, FieldTy,
3251 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003252 }
3253 return GV;
3254}
3255
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003256void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003257 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003258 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003259 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003260 llvm::DIFile *Unit = nullptr;
3261 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003262 unsigned LineNo;
3263 StringRef DeclName, LinkageName;
3264 QualType T;
3265 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003266
3267 // Attempt to store one global variable for the declaration - even if we
3268 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003269 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003270
3271 // If this is an anonymous union then we'll want to emit a global
3272 // variable for each member of the anonymous union so that it's possible
3273 // to find the name of any field in the union.
3274 if (T->isUnionType() && DeclName.empty()) {
3275 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003276 assert(RD->isAnonymousStructOrUnion() &&
3277 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003278 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3279 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003280 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003281 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3282 Var->hasInternalLinkage(), Var,
3283 getOrCreateStaticDataMemberDeclarationOrNull(D));
3284 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003285 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003286}
3287
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003288void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3289 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003290 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003291 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003292 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003293 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003294 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003295 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3296 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3297 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3298 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3299 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003300 // Do not use global variables for enums.
3301 //
3302 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003303 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003304 return;
David Blaikiea15565562014-04-04 20:56:17 +00003305 // Do not emit separate definitions for function local const/statics.
3306 if (isa<FunctionDecl>(VD->getDeclContext()))
3307 return;
David Blaikiebb113912014-04-05 07:23:17 +00003308 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003309 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003310 if (VarD->isStaticDataMember()) {
3311 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003312 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003313 // Ensure that the type is retained even though it's otherwise unreferenced.
3314 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003315 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003316 return;
3317 }
3318
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003319 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003320
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003321 auto &GV = DeclCache[VD];
3322 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003323 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003324 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003325 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003326 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003327}
3328
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003329llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003330 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003331 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003332 llvm::DIScope *Mod = getParentModuleOrNull(D);
3333 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003334}
3335
David Blaikie9f88fe82013-04-22 06:13:21 +00003336void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003337 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3338 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003339 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003340 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3341 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003342 getLineNumber(UD.getLocation()));
3343}
3344
David Blaikiebd483762013-05-20 04:58:53 +00003345void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3346 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3347 return;
3348 assert(UD.shadow_size() &&
3349 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3350 // Emitting one decl is sufficient - debuggers can detect that this is an
3351 // overloaded name & provide lookup for all the overloads.
3352 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003353 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003354 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003355 DBuilder.createImportedDeclaration(
3356 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3357 getLineNumber(USD.getLocation()));
3358}
3359
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003360void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3361 auto *Reader = CGM.getContext().getExternalSource();
3362 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3363 DBuilder.createImportedDeclaration(
3364 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3365 getOrCreateModuleRef(Info),
3366 getLineNumber(ID.getLocation()));
3367}
3368
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003369llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003370CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3371 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003372 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003373 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003374 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003375 return cast<llvm::DIImportedEntity>(VH);
3376 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003377 if (const NamespaceAliasDecl *Underlying =
3378 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3379 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003380 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003381 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3382 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3383 NA.getName());
3384 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003385 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003386 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3387 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3388 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003389 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003390 return R;
3391}
3392
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003393llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003394CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003395 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003396 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003397 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003398 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003399
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003401 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003402 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003403 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003404 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003405 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003406 return NS;
3407}
3408
3409void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003410 // Creating types might create further types - invalidating the current
3411 // element and the size(), so don't cache/reference them.
3412 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3413 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003414 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003415 ? CreateTypeDefinition(E.Type, E.Unit)
3416 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003417 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003418 }
3419
David Blaikief427b002014-05-06 03:42:01 +00003420 for (auto p : ReplaceMap) {
3421 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003422 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003423 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003424
David Blaikief427b002014-05-06 03:42:01 +00003425 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003426 assert(it != TypeCache.end());
3427 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003428
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003429 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3430 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003431 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003432
Frederic Rissd253ed62014-11-18 03:40:51 +00003433 for (const auto &p : FwdDeclReplaceMap) {
3434 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003435 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003436 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003437
3438 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003439 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003440 // with ourselves, that will destroy the temporary MDNode and
3441 // replace it with a standard one, avoiding leaking memory.
3442 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003443 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003444 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003445 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003446
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003447 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003448 }
3449
Adrian Prantl73409ce2013-03-11 18:33:46 +00003450 // We keep our own list of retained types, because we need to look
3451 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003452 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003453 if (auto MD = TypeCache[RT])
3454 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003455
Guy Benyei11169dd2012-12-18 14:30:41 +00003456 DBuilder.finalize();
3457}
David Blaikie66088d52014-09-24 17:01:27 +00003458
3459void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3460 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3461 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003462
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003463 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003464 // Don't ignore in case of explicit cast where it is referenced indirectly.
3465 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003466}