blob: c0a3db4d61b7288573b7fe69b592ea84e8d3caa5 [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);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000450 case BuiltinType::OCLImage2dDepth:
451 return getOrCreateStructPtrType("opencl_image2d_depth_t",
452 OCLImage2dDepthDITy);
453 case BuiltinType::OCLImage2dArrayDepth:
454 return getOrCreateStructPtrType("opencl_image2d_array_depth_t",
455 OCLImage2dArrayDepthDITy);
456 case BuiltinType::OCLImage2dMSAA:
457 return getOrCreateStructPtrType("opencl_image2d_msaa_t",
458 OCLImage2dMSAADITy);
459 case BuiltinType::OCLImage2dArrayMSAA:
460 return getOrCreateStructPtrType("opencl_image2d_array_msaa_t",
461 OCLImage2dArrayMSAADITy);
462 case BuiltinType::OCLImage2dMSAADepth:
463 return getOrCreateStructPtrType("opencl_image2d_msaa_depth_t",
464 OCLImage2dMSAADepthDITy);
465 case BuiltinType::OCLImage2dArrayMSAADepth:
466 return getOrCreateStructPtrType("opencl_image2d_array_msaa_depth_t",
467 OCLImage2dArrayMSAADepthDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000468 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000469 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000470 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000471 return DBuilder.createBasicType(
472 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
473 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000474 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000475 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Alexey Bader9c8453f2015-09-15 11:18:52 +0000476 case BuiltinType::OCLClkEvent:
477 return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
478 case BuiltinType::OCLQueue:
479 return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
480 case BuiltinType::OCLNDRange:
481 return getOrCreateStructPtrType("opencl_ndrange_t", OCLNDRangeDITy);
482 case BuiltinType::OCLReserveID:
483 return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000484
Guy Benyei11169dd2012-12-18 14:30:41 +0000485 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000486 case BuiltinType::Char_U:
487 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
488 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000489 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000490 case BuiltinType::SChar:
491 Encoding = llvm::dwarf::DW_ATE_signed_char;
492 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000493 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000494 case BuiltinType::Char32:
495 Encoding = llvm::dwarf::DW_ATE_UTF;
496 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000497 case BuiltinType::UShort:
498 case BuiltinType::UInt:
499 case BuiltinType::UInt128:
500 case BuiltinType::ULong:
501 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000502 case BuiltinType::ULongLong:
503 Encoding = llvm::dwarf::DW_ATE_unsigned;
504 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 case BuiltinType::Short:
506 case BuiltinType::Int:
507 case BuiltinType::Int128:
508 case BuiltinType::Long:
509 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000510 case BuiltinType::LongLong:
511 Encoding = llvm::dwarf::DW_ATE_signed;
512 break;
513 case BuiltinType::Bool:
514 Encoding = llvm::dwarf::DW_ATE_boolean;
515 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516 case BuiltinType::Half:
517 case BuiltinType::Float:
518 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000519 case BuiltinType::Double:
520 Encoding = llvm::dwarf::DW_ATE_float;
521 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 }
523
524 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000525 case BuiltinType::Long:
526 BTName = "long int";
527 break;
528 case BuiltinType::LongLong:
529 BTName = "long long int";
530 break;
531 case BuiltinType::ULong:
532 BTName = "long unsigned int";
533 break;
534 case BuiltinType::ULongLong:
535 BTName = "long long unsigned int";
536 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 default:
538 BTName = BT->getName(CGM.getLangOpts());
539 break;
540 }
541 // Bit size, align and offset of the type.
542 uint64_t Size = CGM.getContext().getTypeSize(BT);
543 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000544 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000545}
546
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000547llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000548 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000549 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000550 if (Ty->isComplexIntegerType())
551 Encoding = llvm::dwarf::DW_ATE_lo_user;
552
553 uint64_t Size = CGM.getContext().getTypeSize(Ty);
554 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000555 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000556}
557
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000558llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
559 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000560 QualifierCollector Qc;
561 const Type *T = Qc.strip(Ty);
562
563 // Ignore these qualifiers for now.
564 Qc.removeObjCGCAttr();
565 Qc.removeAddressSpace();
566 Qc.removeObjCLifetime();
567
568 // We will create one Derived type for one qualifier and recurse to handle any
569 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000570 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 if (Qc.hasConst()) {
572 Tag = llvm::dwarf::DW_TAG_const_type;
573 Qc.removeConst();
574 } else if (Qc.hasVolatile()) {
575 Tag = llvm::dwarf::DW_TAG_volatile_type;
576 Qc.removeVolatile();
577 } else if (Qc.hasRestrict()) {
578 Tag = llvm::dwarf::DW_TAG_restrict_type;
579 Qc.removeRestrict();
580 } else {
581 assert(Qc.empty() && "Unknown type qualifier for debug info");
582 return getOrCreateType(QualType(T, 0), Unit);
583 }
584
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000585 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586
587 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
588 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000589 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000590}
591
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000592llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
593 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000594
595 // The frontend treats 'id' as a typedef to an ObjCObjectType,
596 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
597 // debug info, we want to emit 'id' in both cases.
598 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000599 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000600
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000601 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
602 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000603}
604
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000605llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
606 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000607 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000608 Ty->getPointeeType(), Unit);
609}
610
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000611/// \return whether a C++ mangling exists for the type defined by TD.
612static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
613 switch (TheCU->getSourceLanguage()) {
614 case llvm::dwarf::DW_LANG_C_plus_plus:
615 return true;
616 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
617 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
618 default:
619 return false;
620 }
621}
622
Manman Rene0064d82013-08-29 23:19:58 +0000623/// In C++ mode, types have linkage, so we can rely on the ODR and
624/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000625static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
626 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000627 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000628 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000629 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000630
631 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000632 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000633
Manman Rene0064d82013-08-29 23:19:58 +0000634 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
635 if (CGM.getTarget().getCXXABI().isMicrosoft())
636 return FullName;
637
638 // TODO: This is using the RTTI name. Is there a better way to get
639 // a unique string for a type?
640 llvm::raw_svector_ostream Out(FullName);
641 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000642 return FullName;
643}
644
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000645/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000646static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
647 llvm::dwarf::Tag Tag;
648 if (RD->isStruct() || RD->isInterface())
649 Tag = llvm::dwarf::DW_TAG_structure_type;
650 else if (RD->isUnion())
651 Tag = llvm::dwarf::DW_TAG_union_type;
652 else {
653 // FIXME: This could be a struct type giving a default visibility different
654 // than C++ class type, but needs llvm metadata changes first.
655 assert(RD->isClass());
656 Tag = llvm::dwarf::DW_TAG_class_type;
657 }
658 return Tag;
659}
660
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000661llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000662CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000663 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000664 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000665 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
666 return cast<llvm::DICompositeType>(T);
667 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000668 unsigned Line = getLineNumber(RD->getLocation());
669 StringRef RDName = getClassName(RD);
670
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000671 uint64_t Size = 0;
672 uint64_t Align = 0;
673
674 const RecordDecl *D = RD->getDefinition();
675 if (D && D->isCompleteDefinition()) {
676 Size = CGM.getContext().getTypeSize(Ty);
677 Align = CGM.getContext().getTypeAlign(Ty);
678 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000679
680 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000681 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000682 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000683 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000684 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000685 ReplaceMap.emplace_back(
686 std::piecewise_construct, std::make_tuple(Ty),
687 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000688 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000689}
690
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000691llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000692 const Type *Ty,
693 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000694 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
696 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000697 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000698
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 // Bit size, align and offset of the type.
700 // Size is always the size of a pointer. We can't use getTypeSize here
701 // because that does not return the correct value for references.
702 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000703 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
705
David Blaikie99dab3b2013-09-04 22:03:57 +0000706 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
707 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000708}
709
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000710llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
711 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000712 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000713 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000714 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
715 TheCU, getOrCreateMainFile(), 0);
716 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
717 Cache = DBuilder.createPointerType(Cache, Size);
718 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000719}
720
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000721llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
722 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000723 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000724 QualType FType;
725 uint64_t FieldSize, FieldOffset;
726 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000727 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000728
729 FieldOffset = 0;
730 FType = CGM.getContext().UnsignedLongTy;
731 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
732 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
733
734 Elements = DBuilder.getOrCreateArray(EltTys);
735 EltTys.clear();
736
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000737 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000738 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000739
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000740 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000741 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000742 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000743
744 // Bit size, align and offset of the type.
745 uint64_t Size = CGM.getContext().getTypeSize(Ty);
746
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000747 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000748
749 FieldOffset = 0;
750 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
751 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
752 FType = CGM.getContext().IntTy;
753 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
754 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000755 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000756 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
757
758 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000759 FieldSize = CGM.getContext().getTypeSize(Ty);
760 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000761 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000762 FieldSize, FieldAlign, FieldOffset,
763 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000764
765 FieldOffset += FieldSize;
766 Elements = DBuilder.getOrCreateArray(EltTys);
767
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000768 // The __block_literal_generic structs are marked with a special
769 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
770 // the debugger needs to know about. To allow type uniquing, emit
771 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000772 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000773 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000774 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000775
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000776 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000777}
778
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000779llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
780 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000781 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000782 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000783
784 SmallString<128> NS;
785 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000786 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
787 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000788
789 TemplateSpecializationType::PrintTemplateArgumentList(
790 OS, Ty->getArgs(), Ty->getNumArgs(),
791 CGM.getContext().getPrintingPolicy());
792
Eric Christophere7b87e52014-10-26 23:40:33 +0000793 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
794 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000795
796 SourceLocation Loc = AliasDecl->getLocation();
Adrian Prantlb67dbce2015-09-11 18:45:02 +0000797 return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
798 getLineNumber(Loc),
799 getDeclContextDescriptor(AliasDecl));
David Blaikief1b382e2014-04-06 17:14:06 +0000800}
801
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000802llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
803 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000804 // We don't set size information, but do specify where the typedef was
805 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000806 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000807
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000808 // Typedefs are derived from some other type.
809 return DBuilder.createTypedef(
810 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
811 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Adrian Prantl6ec370a2015-09-10 18:39:45 +0000812 getDeclContextDescriptor(Ty->getDecl()));
Guy Benyei11169dd2012-12-18 14:30:41 +0000813}
814
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000815llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
816 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000817 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000818
819 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000820 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000821
822 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000823 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 if (isa<FunctionNoProtoType>(Ty))
825 EltTys.push_back(DBuilder.createUnspecifiedParameter());
826 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000827 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
828 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000829 if (FPT->isVariadic())
830 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000831 }
832
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000833 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000834 return DBuilder.createSubroutineType(Unit, EltTypeArray);
835}
836
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000837/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000838/// As an optimization, return 0 if the access specifier equals the
839/// default for the containing type.
840static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
841 AccessSpecifier Default = clang::AS_none;
842 if (RD && RD->isClass())
843 Default = clang::AS_private;
844 else if (RD && (RD->isStruct() || RD->isUnion()))
845 Default = clang::AS_public;
846
847 if (Access == Default)
848 return 0;
849
Eric Christophere7b87e52014-10-26 23:40:33 +0000850 switch (Access) {
851 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000852 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000853 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000854 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000855 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000856 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000857 case clang::AS_none:
858 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000859 }
860 llvm_unreachable("unexpected access enumerator");
861}
Guy Benyei11169dd2012-12-18 14:30:41 +0000862
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000863llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000864 StringRef name, QualType type, uint64_t sizeInBitsOverride,
865 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000866 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
867 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000868
869 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000870 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000871 unsigned line = getLineNumber(loc);
872
David Majnemer34b57492014-07-30 01:30:47 +0000873 uint64_t SizeInBits = 0;
874 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000875 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000876 TypeInfo TI = CGM.getContext().getTypeInfo(type);
877 SizeInBits = TI.Width;
878 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000879
880 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000881 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000882 }
883
Adrian Prantl21361fb2014-08-29 22:44:27 +0000884 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000885 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
886 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000887}
888
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000889void CGDebugInfo::CollectRecordLambdaFields(
890 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000891 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000892 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
893 // has the name and the location of the variable so we should iterate over
894 // both concurrently.
895 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
896 RecordDecl::field_iterator Field = CXXDecl->field_begin();
897 unsigned fieldno = 0;
898 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000899 E = CXXDecl->captures_end();
900 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000901 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000902 if (C.capturesVariable()) {
903 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000904 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000905 StringRef VName = V->getName();
906 uint64_t SizeInBitsOverride = 0;
907 if (Field->isBitField()) {
908 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
909 assert(SizeInBitsOverride && "found named 0-width bitfield");
910 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000911 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000912 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
913 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
914 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000915 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000916 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000917 // TODO: Need to handle 'this' in some way by probably renaming the
918 // this of the lambda class and having a field member of 'this' or
919 // by using AT_object_pointer for the function and having that be
920 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000921 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000922 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000923 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000924 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000925 "this", type, 0, f->getLocation(), f->getAccess(),
926 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000927
928 elements.push_back(fieldType);
929 }
930 }
931}
932
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000933llvm::DIDerivedType *
934CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000935 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000936 // Create the descriptor for the static variable, with or without
937 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000938 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000939 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
940 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000941
Eric Christopher91a31902013-01-16 01:22:32 +0000942 unsigned LineNumber = getLineNumber(Var->getLocation());
943 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000944 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000945 if (Var->getInit()) {
946 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000947 if (Value) {
948 if (Value->isInt())
949 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
950 if (Value->isFloat())
951 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
952 }
Eric Christopher91a31902013-01-16 01:22:32 +0000953 }
954
Adrian Prantl21361fb2014-08-29 22:44:27 +0000955 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000956 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000957 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000958 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000959 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000960}
961
Eric Christophere7b87e52014-10-26 23:40:33 +0000962void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000963 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
964 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000965 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000966 StringRef name = field->getName();
967 QualType type = field->getType();
968
969 // Ignore unnamed fields unless they're anonymous structs/unions.
970 if (name.empty() && !type->isRecordType())
971 return;
972
973 uint64_t SizeInBitsOverride = 0;
974 if (field->isBitField()) {
975 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
976 assert(SizeInBitsOverride && "found named 0-width bitfield");
977 }
978
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000979 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000980 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
981 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000982
983 elements.push_back(fieldType);
984}
985
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000986void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000987 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000988 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000989 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000990 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
991
Eric Christopher91a31902013-01-16 01:22:32 +0000992 if (CXXDecl && CXXDecl->isLambda())
993 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
994 else {
995 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000996
Eric Christopher91a31902013-01-16 01:22:32 +0000997 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000998 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000999
Eric Christopher91a31902013-01-16 01:22:32 +00001000 // Static and non-static members should appear in the same order as
1001 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +00001002 for (const auto *I : record->decls())
1003 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +00001004 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001005 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +00001006 if (MI != StaticDataMemberCache.end()) {
1007 assert(MI->second &&
1008 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00001009 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +00001010 } else {
1011 auto Field = CreateRecordStaticField(V, RecordTy, record);
1012 elements.push_back(Field);
1013 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001014 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001015 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1016 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001017
1018 // Bump field number for next field.
1019 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001020 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001021 }
1022}
1023
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001024llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001025CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001026 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001027 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001028 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001029 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001030 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001031 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1032 Func, Unit);
1033}
David Blaikie2aaf0652013-01-07 22:24:59 +00001034
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001035llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1036 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001037 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001038 llvm::DITypeRefArray Args(
1039 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001040 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001041 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001042
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001043 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
1045 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001046 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001047
David Blaikie2aaf0652013-01-07 22:24:59 +00001048 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001049 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001050 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1051 // Create pointer type directly in this case.
1052 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1053 QualType PointeeTy = ThisPtrTy->getPointeeType();
1054 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001055 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001056 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1058 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001059 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001060 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001061 // TODO: This and the artificial type below are misleading, the
1062 // types aren't artificial the argument is, but the current
1063 // metadata doesn't represent that.
1064 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1065 Elts.push_back(ThisPtrType);
1066 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001067 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001068 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001069 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1070 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001071 }
1072
1073 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001074 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1075 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001077 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001078
Adrian Prantl0630eb72013-12-18 21:48:18 +00001079 unsigned Flags = 0;
1080 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001081 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001082 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001083 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001084
1085 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001086}
1087
Eric Christopherb2a008c2013-05-16 00:45:12 +00001088/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001089/// inside a function.
1090static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1091 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1092 return isFunctionLocalClass(NRD);
1093 if (isa<FunctionDecl>(RD->getDeclContext()))
1094 return true;
1095 return false;
1096}
1097
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001098llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1099 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001100 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001101 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001102
Guy Benyei11169dd2012-12-18 14:30:41 +00001103 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001104 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001105
1106 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1107 // make sense to give a single ctor/dtor a linkage name.
1108 StringRef MethodLinkageName;
1109 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1110 MethodLinkageName = CGM.getMangledName(Method);
1111
1112 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001113 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001114 unsigned MethodLine = 0;
1115 if (!Method->isImplicit()) {
1116 MethodDefUnit = getOrCreateFile(Method->getLocation());
1117 MethodLine = getLineNumber(Method->getLocation());
1118 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001119
1120 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001121 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001122 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001123 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001124
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 if (Method->isVirtual()) {
1126 if (Method->isPure())
1127 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1128 else
1129 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001130
Guy Benyei11169dd2012-12-18 14:30:41 +00001131 // It doesn't make sense to give a virtual destructor a vtable index,
1132 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001133 // FIXME: Add proper support for debug info for virtual calls in
1134 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1135 // lookup if we have multiple or virtual inheritance.
1136 if (!isa<CXXDestructorDecl>(Method) &&
1137 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001138 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 ContainingType = RecordTy;
1140 }
1141
1142 unsigned Flags = 0;
1143 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001144 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001145 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1147 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001148 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001149 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001150 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001151 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001152 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001153 }
1154 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001155 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001156 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001158 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001159 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001160
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001161 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1162 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001163 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1164 MethodTy, /*isLocalToUnit=*/false,
1165 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001166 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001167
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001168 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001169
1170 return SP;
1171}
1172
Eric Christophere7b87e52014-10-26 23:40:33 +00001173void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001174 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1175 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001176
1177 // Since we want more than just the individual member decls if we
1178 // have templated functions iterate over every declaration to gather
1179 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001180 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001181 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1182 // If the member is implicit, don't add it to the member list. This avoids
1183 // the member being added to type units by LLVM, while still allowing it
1184 // to be emitted into the type declaration/reference inside the compile
1185 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001186 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001187 // FIXME: Handle Using(Shadow?)Decls here to create
1188 // DW_TAG_imported_declarations inside the class for base decls brought into
1189 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1190 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1191 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001192 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001193 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001194
1195 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1196 continue;
1197
David Blaikiefd580722014-10-06 05:18:55 +00001198 // Reuse the existing member function declaration if it exists.
1199 // It may be associated with the declaration of the type & should be
1200 // reused as we're building the definition.
1201 //
1202 // This situation can arise in the vtable-based debug info reduction where
1203 // implicit members are emitted in a non-vtable TU.
1204 auto MI = SPCache.find(Method->getCanonicalDecl());
1205 EltTys.push_back(MI == SPCache.end()
1206 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001207 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001208 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001209}
Guy Benyei11169dd2012-12-18 14:30:41 +00001210
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001211void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001212 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001213 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001214 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 unsigned BFlags = 0;
1217 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001218
Guy Benyei11169dd2012-12-18 14:30:41 +00001219 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001220 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001221
Aaron Ballman574705e2014-03-13 15:41:46 +00001222 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001223 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1224 // virtual base offset offset is -ve. The code generator emits dwarf
1225 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001226 BaseOffset = 0 - CGM.getItaniumVTableContext()
1227 .getVirtualBaseOffsetOffset(RD, Base)
1228 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001229 } else {
1230 // In the MS ABI, store the vbtable offset, which is analogous to the
1231 // vbase offset offset in Itanium.
1232 BaseOffset =
1233 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1234 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001235 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001236 } else
1237 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1238 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1239 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001240
Adrian Prantl21361fb2014-08-29 22:44:27 +00001241 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001242 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001243 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001244 EltTys.push_back(DTy);
1245 }
1246}
1247
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001248llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001249CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1250 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001251 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001252 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1254 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001255 StringRef Name;
1256 if (TPList)
1257 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001258 switch (TA.getKind()) {
1259 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001261 TemplateParams.push_back(
1262 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001263 } break;
1264 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001266 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1267 TheCU, Name, TTy,
1268 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001269 } break;
1270 case TemplateArgument::Declaration: {
1271 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001272 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001273 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001274 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001275 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001276 // Variable pointer template parameters have a value that is the address
1277 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001278 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001279 V = CGM.GetAddrOfGlobalVar(VD);
1280 // Member function pointers have special support for building them, though
1281 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001282 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001283 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001284 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001285 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001286 // Member data pointers have special handling too to compute the fixed
1287 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001288 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001289 // These five lines (& possibly the above member function pointer
1290 // handling) might be able to be refactored to use similar code in
1291 // CodeGenModule::getMemberPointerConstant
1292 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1293 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001294 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001295 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001296 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001297 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1298 TheCU, Name, TTy,
1299 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001300 } break;
1301 case TemplateArgument::NullPtr: {
1302 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001303 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001304 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001305 // Special case member data pointer null values since they're actually -1
1306 // instead of zero.
1307 if (const MemberPointerType *MPT =
1308 dyn_cast<MemberPointerType>(T.getTypePtr()))
1309 // But treat member function pointers as simple zero integers because
1310 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1311 // CodeGen grows handling for values of non-null member function
1312 // pointers then perhaps we could remove this special case and rely on
1313 // EmitNullMemberPointer for member function pointers.
1314 if (MPT->isMemberDataPointer())
1315 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1316 if (!V)
1317 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001318 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1319 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001320 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001321 case TemplateArgument::Template:
1322 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001323 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001324 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1325 break;
1326 case TemplateArgument::Pack:
1327 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1328 TheCU, Name, nullptr,
1329 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1330 break;
David Majnemer5559d472013-08-24 08:21:10 +00001331 case TemplateArgument::Expression: {
1332 const Expr *E = TA.getAsExpr();
1333 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001334 if (E->isGLValue())
1335 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001336 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001337 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001338 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001339 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1340 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001341 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001342 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001344 case TemplateArgument::Null:
1345 llvm_unreachable(
1346 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 }
1348 }
1349 return DBuilder.getOrCreateArray(TemplateParams);
1350}
1351
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001352llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001353CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001354 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 if (FD->getTemplatedKind() ==
1356 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001357 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1358 ->getTemplate()
1359 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001360 return CollectTemplateParams(
1361 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001363 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001364}
1365
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001366llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1367 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001368 // Always get the full list of parameters, not just the ones from
1369 // the specialization.
1370 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001371 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001372 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001373 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001374}
1375
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001376llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001377 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001378 return VTablePtrType;
1379
1380 ASTContext &Context = CGM.getContext();
1381
1382 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001383 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001384 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1385 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001386 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001388 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001389 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1390 return VTablePtrType;
1391}
1392
Guy Benyei11169dd2012-12-18 14:30:41 +00001393StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001394 // Copy the gdb compatible name on the side and use its reference.
1395 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001396}
1397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001399 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1401
1402 // If there is a primary base then it will hold vtable info.
1403 if (RL.getPrimaryBase())
1404 return;
1405
1406 // If this class is not dynamic then there is not any vtable info to collect.
1407 if (!RD->isDynamicClass())
1408 return;
1409
1410 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001411 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001412 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001413 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001414 EltTys.push_back(VPTR);
1415}
1416
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001417llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001418 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001419 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001420 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 return T;
1422}
1423
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001425 SourceLocation Loc) {
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001426 return getOrCreateStandaloneType(D, Loc);
1427}
1428
1429llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
1430 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001431 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001432 assert(!D.isNull() && "null type");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001433 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantlad9a195e2015-08-27 21:21:19 +00001434 assert(T && "could not create debug info for type");
Adrian Prantl3a884fa2015-08-27 22:56:46 +00001435
1436 // Composite types with UIDs were already retained by DIBuilder
1437 // because they are only referenced by name in the IR.
1438 if (auto *CTy = dyn_cast<llvm::DICompositeType>(T))
1439 if (!CTy->getIdentifier().empty())
1440 return T;
Adrian Prantl73409ce2013-03-11 18:33:46 +00001441 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 return T;
1443}
1444
David Blaikie483a9da2014-05-06 18:35:21 +00001445void CGDebugInfo::completeType(const EnumDecl *ED) {
1446 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1447 return;
1448 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001449 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001450 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001451 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001452 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001453 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001454 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001455 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001456}
1457
David Blaikieb2e86eb2013-08-15 20:49:17 +00001458void CGDebugInfo::completeType(const RecordDecl *RD) {
1459 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1460 !CGM.getLangOpts().CPlusPlus)
1461 completeRequiredType(RD);
1462}
1463
1464void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001465 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1466 return;
1467
David Blaikie6943dea2013-08-20 01:28:15 +00001468 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1469 if (CXXDecl->isDynamicClass())
1470 return;
1471
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001472 if (DebugTypeExtRefs && RD->isFromASTFile())
1473 return;
1474
David Blaikieb2e86eb2013-08-15 20:49:17 +00001475 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001476 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001477 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001478 completeClassData(RD);
1479}
1480
1481void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1482 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001483 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001484 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001485 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001486 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001487 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001488 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001489 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001490 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001491 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001492}
1493
David Blaikie0e716b42014-03-03 23:48:23 +00001494static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1495 CXXRecordDecl::method_iterator End) {
1496 for (; I != End; ++I)
1497 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001498 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1499 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001500 return true;
1501 return false;
1502}
1503
1504static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001505 bool DebugTypeExtRefs,
David Blaikie0e716b42014-03-03 23:48:23 +00001506 const RecordDecl *RD,
1507 const LangOptions &LangOpts) {
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001508 // Does the type exist in an imported clang module?
Adrian Prantl5d66c6b2015-09-11 18:54:28 +00001509 if (DebugTypeExtRefs && RD->isFromASTFile() && RD->getDefinition())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001510 return true;
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001511
David Blaikie0e716b42014-03-03 23:48:23 +00001512 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1513 return false;
1514
1515 if (!LangOpts.CPlusPlus)
1516 return false;
1517
1518 if (!RD->isCompleteDefinitionRequired())
1519 return true;
1520
1521 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1522
1523 if (!CXXDecl)
1524 return false;
1525
1526 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1527 return true;
1528
1529 TemplateSpecializationKind Spec = TSK_Undeclared;
1530 if (const ClassTemplateSpecializationDecl *SD =
1531 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1532 Spec = SD->getSpecializationKind();
1533
1534 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1535 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1536 CXXDecl->method_end()))
1537 return true;
1538
1539 return false;
1540}
1541
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001542llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001544 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001545 if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
1546 CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001547 if (!T)
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001548 T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001549 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001550 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001551
David Blaikieb2e86eb2013-08-15 20:49:17 +00001552 return CreateTypeDefinition(Ty);
1553}
1554
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001555llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001556 RecordDecl *RD = Ty->getDecl();
1557
Guy Benyei11169dd2012-12-18 14:30:41 +00001558 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001559 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001560
1561 // Records and classes and unions can all be recursive. To handle them, we
1562 // first generate a debug descriptor for the struct as a forward declaration.
1563 // Then (if it is a definition) we go through and get debug info for all of
1564 // its members. Finally, we create a descriptor for the complete type (which
1565 // may refer to the forward decl if the struct is recursive) and replace all
1566 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001567 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001568
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001569 const RecordDecl *D = RD->getDefinition();
1570 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 return FwdDecl;
1572
David Blaikieadfbf992013-08-18 16:55:33 +00001573 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1574 CollectContainingType(CXXDecl, FwdDecl);
1575
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001577 LexicalBlockStack.emplace_back(&*FwdDecl);
1578 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001579
Guy Benyei11169dd2012-12-18 14:30:41 +00001580 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001581 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001582 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
1584 // Note: The split of CXXDecl information here is intentional, the
1585 // gdb tests will depend on a certain ordering at printout. The debug
1586 // information offsets are still correct if we merge them all together
1587 // though.
1588 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1589 if (CXXDecl) {
1590 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1591 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1592 }
1593
Eric Christopher91a31902013-01-16 01:22:32 +00001594 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001596 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001597 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
1599 LexicalBlockStack.pop_back();
1600 RegionMap.erase(Ty->getDecl());
1601
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001602 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001603 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001604
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001605 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001606 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001607 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001608
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001609 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001610 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001611}
1612
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001613llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1614 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 // Ignore protocols.
1616 return getOrCreateType(Ty->getBaseType(), Unit);
1617}
1618
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001619/// \return true if Getter has the default name for the property PD.
1620static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1621 const ObjCMethodDecl *Getter) {
1622 assert(PD);
1623 if (!Getter)
1624 return true;
1625
1626 assert(Getter->getDeclName().isObjCZeroArgSelector());
1627 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001628 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001629}
1630
1631/// \return true if Setter has the default name for the property PD.
1632static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1633 const ObjCMethodDecl *Setter) {
1634 assert(PD);
1635 if (!Setter)
1636 return true;
1637
1638 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001639 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001640 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001641}
1642
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001643llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1644 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 ObjCInterfaceDecl *ID = Ty->getDecl();
1646 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001647 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001648
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001649 // Return a forward declaration if this type was imported from a clang module.
1650 if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition())
1651 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1652 ID->getName(),
1653 getDeclContextDescriptor(ID), Unit, 0);
1654
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001656 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001657 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001658 auto RuntimeLang =
1659 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001660
1661 // If this is just a forward declaration return a special forward-declaration
1662 // debug type since we won't be able to lay out the entire type.
1663 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001664 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001665 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001666 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1667 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001668 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001669 return FwdDecl;
1670 }
1671
David Blaikieef8a9512014-05-05 23:23:53 +00001672 return CreateTypeDefinition(Ty, Unit);
1673}
1674
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001675llvm::DIModule *
Adrian Prantl66689202015-09-18 23:01:45 +00001676CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
1677 bool CreateSkeletonCU) {
Adrian Prantl2d3d6df2015-09-19 00:10:37 +00001678 auto &ModRef = ModuleRefCache[Mod.FullModuleName];
Adrian Prantld0929a12015-09-11 01:03:19 +00001679 if (ModRef)
1680 return cast<llvm::DIModule>(ModRef);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001681
1682 // Macro definitions that were defined with "-D" on the command line.
1683 SmallString<128> ConfigMacros;
1684 {
1685 llvm::raw_svector_ostream OS(ConfigMacros);
1686 const auto &PPOpts = CGM.getPreprocessorOpts();
1687 unsigned I = 0;
1688 // Translate the macro definitions back into a commmand line.
1689 for (auto &M : PPOpts.Macros) {
1690 if (++I > 1)
1691 OS << " ";
1692 const std::string &Macro = M.first;
1693 bool Undef = M.second;
1694 OS << "\"-" << (Undef ? 'U' : 'D');
1695 for (char c : Macro)
1696 switch (c) {
1697 case '\\' : OS << "\\\\"; break;
1698 case '"' : OS << "\\\""; break;
1699 default: OS << c;
1700 }
1701 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001702 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001703 }
Adrian Prantl66689202015-09-18 23:01:45 +00001704
Adrian Prantl66689202015-09-18 23:01:45 +00001705 if (CreateSkeletonCU) {
1706 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001707 DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.FullModuleName,
1708 Mod.Path, TheCU->getProducer(), true, StringRef(), 0,
1709 Mod.ASTFile, llvm::DIBuilder::FullDebug,
1710 Mod.Signature);
Adrian Prantl66689202015-09-18 23:01:45 +00001711 DIB.finalize();
Adrian Prantl2f957ac2015-09-19 00:59:22 +00001712 }
1713 llvm::DIModule *M =
1714 DBuilder.createModule(TheCU, Mod.FullModuleName, ConfigMacros, Mod.Path,
1715 CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantld0929a12015-09-11 01:03:19 +00001716 ModRef.reset(M);
1717 return M;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001718}
1719
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001720llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1721 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001722 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001723 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001724 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001725 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001726
1727 // Bit size, align and offset of the type.
1728 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1729 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1730
1731 unsigned Flags = 0;
1732 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001733 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001734
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001735 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001736 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001737 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001738
David Blaikieef8a9512014-05-05 23:23:53 +00001739 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001740 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001741
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001742 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001743 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001744 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001745
1746 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001747 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001748
1749 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1750 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001751 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001752 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001753 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001754 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001755
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001756 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001757 EltTys.push_back(InhTag);
1758 }
1759
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001760 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001761 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001762 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001763 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 unsigned PLine = getLineNumber(Loc);
1765 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1766 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001767 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1768 PD->getName(), PUnit, PLine,
1769 hasDefaultGetterName(PD, Getter) ? ""
1770 : getSelectorName(PD->getGetterName()),
1771 hasDefaultSetterName(PD, Setter) ? ""
1772 : getSelectorName(PD->getSetterName()),
1773 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001774 EltTys.push_back(PropertyNode);
1775 }
1776
1777 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1778 unsigned FieldNo = 0;
1779 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1780 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001782 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001783 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001784
Guy Benyei11169dd2012-12-18 14:30:41 +00001785 StringRef FieldName = Field->getName();
1786
1787 // Ignore unnamed fields.
1788 if (FieldName.empty())
1789 continue;
1790
1791 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001792 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 unsigned FieldLine = getLineNumber(Field->getLocation());
1794 QualType FType = Field->getType();
1795 uint64_t FieldSize = 0;
1796 unsigned FieldAlign = 0;
1797
1798 if (!FType->isIncompleteArrayType()) {
1799
1800 // Bit size, align and offset of the type.
1801 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001802 ? Field->getBitWidthValue(CGM.getContext())
1803 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 FieldAlign = CGM.getContext().getTypeAlign(FType);
1805 }
1806
1807 uint64_t FieldOffset;
1808 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1809 // We don't know the runtime offset of an ivar if we're using the
1810 // non-fragile ABI. For bitfields, use the bit offset into the first
1811 // byte of storage of the bitfield. For other fields, use zero.
1812 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001813 FieldOffset =
1814 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001815 FieldOffset %= CGM.getContext().getCharWidth();
1816 } else {
1817 FieldOffset = 0;
1818 }
1819 } else {
1820 FieldOffset = RL.getFieldOffset(FieldNo);
1821 }
1822
1823 unsigned Flags = 0;
1824 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001825 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001827 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001828 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001829 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001830
Craig Topper8a13c412014-05-21 05:09:00 +00001831 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001833 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001834 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001836 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001837 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001838 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1840 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001841 PropertyNode = DBuilder.createObjCProperty(
1842 PD->getName(), PUnit, PLine,
1843 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1844 PD->getGetterName()),
1845 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1846 PD->getSetterName()),
1847 PD->getPropertyAttributes(),
1848 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001849 }
1850 }
1851 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001852 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1853 FieldSize, FieldAlign, FieldOffset, Flags,
1854 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 EltTys.push_back(FieldTy);
1856 }
1857
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001858 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001859 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001860
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001862 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001863}
1864
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001865llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1866 llvm::DIFile *Unit) {
1867 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 int64_t Count = Ty->getNumElements();
1869 if (Count == 0)
1870 // If number of elements are not known then this is an unbounded array.
1871 // Use Count == -1 to express such arrays.
1872 Count = -1;
1873
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001874 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001875 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001876
1877 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1878 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1879
1880 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1881}
1882
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001883llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 uint64_t Size;
1885 uint64_t Align;
1886
1887 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1888 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1889 Size = 0;
1890 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001891 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001892 } else if (Ty->isIncompleteArrayType()) {
1893 Size = 0;
1894 if (Ty->getElementType()->isIncompleteType())
1895 Align = 0;
1896 else
1897 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001898 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 Size = 0;
1900 Align = 0;
1901 } else {
1902 // Size and align of the whole array, not the element type.
1903 Size = CGM.getContext().getTypeSize(Ty);
1904 Align = CGM.getContext().getTypeAlign(Ty);
1905 }
1906
1907 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1908 // interior arrays, do we care? Why aren't nested arrays represented the
1909 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001910 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 QualType EltTy(Ty, 0);
1912 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1913 // If the number of elements is known, then count is that number. Otherwise,
1914 // it's -1. This allows us to represent a subrange with an array of 0
1915 // elements, like this:
1916 //
1917 // struct foo {
1918 // int x[0];
1919 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001920 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1922 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001923
Guy Benyei11169dd2012-12-18 14:30:41 +00001924 // FIXME: Verify this is right for VLAs.
1925 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1926 EltTy = Ty->getElementType();
1927 }
1928
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001930
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001931 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1932 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001933}
1934
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001935llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1936 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001937 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1938 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001939}
1940
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001941llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1942 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001943 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1944 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001945}
1946
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001947llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1948 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001949 uint64_t Size =
1950 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001951 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001952 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001953 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001954 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001955
1956 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001957 Ty->getPointeeType()->getAs<FunctionProtoType>();
1958 return DBuilder.createMemberPointerType(
1959 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1960 Ty->getClass(), FPT->getTypeQuals())),
1961 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001962 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001963}
1964
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001965llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001966 // Ignore the atomic wrapping
1967 // FIXME: What is the correct representation?
1968 return getOrCreateType(Ty->getValueType(), U);
1969}
1970
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001971llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001972 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001973
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 uint64_t Size = 0;
1975 uint64_t Align = 0;
1976 if (!ED->getTypeForDecl()->isIncompleteType()) {
1977 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1978 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1979 }
1980
Manman Rene0064d82013-08-29 23:19:58 +00001981 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1982
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001983 bool isImportedFromModule =
1984 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
1985
Guy Benyei11169dd2012-12-18 14:30:41 +00001986 // If this is just a forward declaration, construct an appropriately
1987 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001988 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001989 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001990 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 unsigned Line = getLineNumber(ED->getLocation());
1992 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001993 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001994 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001995 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001996 ReplaceMap.emplace_back(
1997 std::piecewise_construct, std::make_tuple(Ty),
1998 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001999 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 }
2001
David Blaikie483a9da2014-05-06 18:35:21 +00002002 return CreateTypeDefinition(Ty);
2003}
2004
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002005llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002006 const EnumDecl *ED = Ty->getDecl();
2007 uint64_t Size = 0;
2008 uint64_t Align = 0;
2009 if (!ED->getTypeForDecl()->isIncompleteType()) {
2010 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2011 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2012 }
2013
2014 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2015
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002016 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002017 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002019 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002020 Enumerators.push_back(DBuilder.createEnumerator(
2021 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 }
2023
2024 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002025 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002026
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002027 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002029 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002030 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002031 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2032 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2033 Line, Size, Align, EltArray, ClassTy,
2034 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002035}
2036
David Blaikie05491062013-01-21 04:37:12 +00002037static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2038 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002039 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002040 Qualifiers InnerQuals = T.getLocalQualifiers();
2041 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2042 // that is already there.
2043 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2044 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002045 QualType LastT = T;
2046 switch (T->getTypeClass()) {
2047 default:
David Blaikie05491062013-01-21 04:37:12 +00002048 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002049 case Type::TemplateSpecialization: {
2050 const auto *Spec = cast<TemplateSpecializationType>(T);
2051 if (Spec->isTypeAlias())
2052 return C.getQualifiedType(T.getTypePtr(), Quals);
2053 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002054 break;
2055 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002056 case Type::TypeOfExpr:
2057 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2058 break;
2059 case Type::TypeOf:
2060 T = cast<TypeOfType>(T)->getUnderlyingType();
2061 break;
2062 case Type::Decltype:
2063 T = cast<DecltypeType>(T)->getUnderlyingType();
2064 break;
2065 case Type::UnaryTransform:
2066 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2067 break;
2068 case Type::Attributed:
2069 T = cast<AttributedType>(T)->getEquivalentType();
2070 break;
2071 case Type::Elaborated:
2072 T = cast<ElaboratedType>(T)->getNamedType();
2073 break;
2074 case Type::Paren:
2075 T = cast<ParenType>(T)->getInnerType();
2076 break;
David Blaikie05491062013-01-21 04:37:12 +00002077 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002078 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002079 break;
2080 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002081 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002082 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002083 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002084 break;
2085 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002086
Guy Benyei11169dd2012-12-18 14:30:41 +00002087 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002088 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 } while (true);
2090}
2091
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002092llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002093
2094 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002095 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002096
David Blaikief427b002014-05-06 03:42:01 +00002097 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 if (it != TypeCache.end()) {
2099 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002100 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002101 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 }
2103
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002104 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002105}
2106
David Blaikie0e716b42014-03-03 23:48:23 +00002107void CGDebugInfo::completeTemplateDefinition(
2108 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002109 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2110 return;
2111
David Blaikie0e716b42014-03-03 23:48:23 +00002112 completeClassData(&SD);
2113 // In case this type has no member function definitions being emitted, ensure
2114 // it is retained
2115 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2116}
2117
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002118llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002119 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002120 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002121
2122 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002123 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002124
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002125 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 return T;
2127
Adrian Prantlca844182015-09-11 17:23:03 +00002128 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002129 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002130
2131 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002132 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002133
Guy Benyei11169dd2012-12-18 14:30:41 +00002134 return Res;
2135}
2136
Eric Christopher1ecc5632013-06-07 22:54:39 +00002137unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002138 // The assumption is that the number of ivars can only increase
2139 // monotonically, so it is safe to just use their current number as
2140 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002141 unsigned Sum = 0;
2142 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002143 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002144 ++Sum;
2145
2146 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002147}
2148
2149ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2150 switch (Ty->getTypeClass()) {
2151 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002152 return getObjCInterfaceDecl(
2153 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002154 case Type::ObjCInterface:
2155 return cast<ObjCInterfaceType>(Ty)->getDecl();
2156 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002157 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002158 }
2159}
2160
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002161llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl453cdf02015-09-11 18:54:31 +00002162 if (!DebugTypeExtRefs || !D->isFromASTFile())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002163 return nullptr;
2164
Adrian Prantl66689202015-09-18 23:01:45 +00002165 // Record a reference to an imported clang module or precompiled header.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002166 llvm::DIModule *ModuleRef = nullptr;
2167 auto *Reader = CGM.getContext().getExternalSource();
2168 auto Idx = D->getOwningModuleID();
2169 auto Info = Reader->getSourceDescriptor(Idx);
2170 if (Info)
Adrian Prantl66689202015-09-18 23:01:45 +00002171 ModuleRef = getOrCreateModuleRef(*Info, true);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002172 return ModuleRef;
2173}
2174
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002175llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 // Handle qualifiers, which recursively handles what they refer to.
2177 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002178 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002179
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 // Work out details of type.
2181 switch (Ty->getTypeClass()) {
2182#define TYPE(Class, Base)
2183#define ABSTRACT_TYPE(Class, Base)
2184#define NON_CANONICAL_TYPE(Class, Base)
2185#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2186#include "clang/AST/TypeNodes.def"
2187 llvm_unreachable("Dependent types cannot show up in debug information");
2188
2189 case Type::ExtVector:
2190 case Type::Vector:
2191 return CreateType(cast<VectorType>(Ty), Unit);
2192 case Type::ObjCObjectPointer:
2193 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2194 case Type::ObjCObject:
2195 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2196 case Type::ObjCInterface:
2197 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2198 case Type::Builtin:
2199 return CreateType(cast<BuiltinType>(Ty));
2200 case Type::Complex:
2201 return CreateType(cast<ComplexType>(Ty));
2202 case Type::Pointer:
2203 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002204 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002205 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002206 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002207 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002208 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 case Type::BlockPointer:
2210 return CreateType(cast<BlockPointerType>(Ty), Unit);
2211 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002212 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002214 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002216 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 case Type::FunctionProto:
2218 case Type::FunctionNoProto:
2219 return CreateType(cast<FunctionType>(Ty), Unit);
2220 case Type::ConstantArray:
2221 case Type::VariableArray:
2222 case Type::IncompleteArray:
2223 return CreateType(cast<ArrayType>(Ty), Unit);
2224
2225 case Type::LValueReference:
2226 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2227 case Type::RValueReference:
2228 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2229
2230 case Type::MemberPointer:
2231 return CreateType(cast<MemberPointerType>(Ty), Unit);
2232
2233 case Type::Atomic:
2234 return CreateType(cast<AtomicType>(Ty), Unit);
2235
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002237 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2238
David Blaikie42edade2014-11-11 20:44:45 +00002239 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002240 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 case Type::Elaborated:
2242 case Type::Paren:
2243 case Type::SubstTemplateTypeParm:
2244 case Type::TypeOfExpr:
2245 case Type::TypeOf:
2246 case Type::Decltype:
2247 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002248 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002249 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002250 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002251
David Blaikie42edade2014-11-11 20:44:45 +00002252 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002253}
2254
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002255llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2256 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002257 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002258
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002259 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002260
2261 // We may have cached a forward decl when we could have created
2262 // a non-forward decl. Go ahead and create a non-forward decl
2263 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002264 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002265 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002266
2267 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002268 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002269
2270 // Propagate members from the declaration to the definition
2271 // CreateType(const RecordType*) will overwrite this with the members in the
2272 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002273 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002274
Guy Benyei11169dd2012-12-18 14:30:41 +00002275 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002276 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002277 return Res;
2278}
2279
2280// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002281llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002282 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002283
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002285 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002286 unsigned Line = getLineNumber(RD->getLocation());
2287 StringRef RDName = getClassName(RD);
2288
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002289 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002290
David Blaikied2785892013-08-18 17:36:19 +00002291 // If we ended up creating the type during the context chain construction,
2292 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002294 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002295 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002296 return T;
David Blaikied2785892013-08-18 17:36:19 +00002297
Adrian Prantl381e7552014-02-04 21:29:50 +00002298 // If this is just a forward or incomplete declaration, construct an
2299 // appropriately marked node and just return it.
2300 const RecordDecl *D = RD->getDefinition();
2301 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002302 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002303
2304 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2305 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002306
Manman Rene0064d82013-08-29 23:19:58 +00002307 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2308
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002309 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002310 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2311 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002312
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002313 RegionMap[Ty->getDecl()].reset(RealDecl);
2314 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002315
David Blaikieadfbf992013-08-18 16:55:33 +00002316 if (const ClassTemplateSpecializationDecl *TSpecial =
2317 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002318 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002319 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002320 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002321}
2322
David Blaikieadfbf992013-08-18 16:55:33 +00002323void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002324 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002325 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002326 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002327 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2328 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002329 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002330 while (1) {
2331 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2332 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2333 if (PBT && !BRL.isPrimaryBaseVirtual())
2334 PBase = PBT;
2335 else
2336 break;
2337 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002338 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002339 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2340 getOrCreateFile(RD->getLocation())));
2341 } else if (RD->isDynamicClass())
2342 ContainingType = RealDecl;
2343
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002344 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002345}
2346
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002347llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002348 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002349 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002350 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2351 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002352 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002353 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 *Offset += FieldSize;
2355 return Ty;
2356}
2357
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002358void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002359 StringRef &Name,
2360 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002361 llvm::DIScope *&FDContext,
2362 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002363 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002364 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2365 Name = getFunctionName(FD);
2366 // Use mangled name as linkage name for C/C++ functions.
2367 if (FD->hasPrototype()) {
2368 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002370 }
2371 // No need to replicate the linkage name if it isn't different from the
2372 // subprogram name, no need to have it at all unless coverage is enabled or
2373 // debug is set to more than just line tables.
2374 if (LinkageName == Name ||
2375 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2376 !CGM.getCodeGenOpts().EmitGcovNotes &&
2377 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2378 LinkageName = StringRef();
2379
2380 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2381 if (const NamespaceDecl *NSDecl =
2382 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2383 FDContext = getOrCreateNameSpace(NSDecl);
2384 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002385 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2386 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2387 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2388 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002389 // Collect template parameters.
2390 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2391 }
2392}
2393
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002394void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002395 unsigned &LineNo, QualType &T,
2396 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002397 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002398 Unit = getOrCreateFile(VD->getLocation());
2399 LineNo = getLineNumber(VD->getLocation());
2400
2401 setLocation(VD->getLocation());
2402
2403 T = VD->getType();
2404 if (T->isIncompleteArrayType()) {
2405 // CodeGen turns int[] into int[1] so we'll do the same here.
2406 llvm::APInt ConstVal(32, 1);
2407 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2408
2409 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2410 ArrayType::Normal, 0);
2411 }
2412
2413 Name = VD->getName();
2414 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2415 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2416 LinkageName = CGM.getMangledName(VD);
2417 if (LinkageName == Name)
2418 LinkageName = StringRef();
2419
2420 // Since we emit declarations (DW_AT_members) for static members, place the
2421 // definition of those static members in the namespace they were declared in
2422 // in the source code (the lexical decl context).
2423 // FIXME: Generalize this for even non-member global variables where the
2424 // declaration and definition may have different lexical decl contexts, once
2425 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002426 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2427 : VD->getDeclContext();
2428 // When a record type contains an in-line initialization of a static data
2429 // member, and the record type is marked as __declspec(dllexport), an implicit
2430 // definition of the member will be created in the record context. DWARF
2431 // doesn't seem to have a nice way to describe this in a form that consumers
2432 // are likely to understand, so fake the "normal" situation of a definition
2433 // outside the class by putting it in the global scope.
2434 if (DC->isRecord())
2435 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002436
2437 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2438 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002439}
2440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002442CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002443 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002444 StringRef Name, LinkageName;
2445 unsigned Flags = 0;
2446 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002447 llvm::DIFile *Unit = getOrCreateFile(Loc);
2448 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002449 unsigned Line = getLineNumber(Loc);
2450
2451 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2452 TParamsArray, Flags);
2453 // Build function type.
2454 SmallVector<QualType, 16> ArgTypes;
2455 for (const ParmVarDecl *Parm: FD->parameters())
2456 ArgTypes.push_back(Parm->getType());
2457 QualType FnType =
2458 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2459 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002460 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002461 DContext, Name, LinkageName, Unit, Line,
2462 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002463 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002464 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002465 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002466 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2467 std::make_tuple(CanonDecl),
2468 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002469 return SP;
2470}
2471
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002473CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2474 QualType T;
2475 StringRef Name, LinkageName;
2476 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002477 llvm::DIFile *Unit = getOrCreateFile(Loc);
2478 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002479 unsigned Line = getLineNumber(Loc);
2480
2481 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002482 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2483 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2484 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002485 FwdDeclReplaceMap.emplace_back(
2486 std::piecewise_construct,
2487 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2488 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002489 return GV;
2490}
2491
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002492llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002493 // We only need a declaration (not a definition) of the type - so use whatever
2494 // we would otherwise do to get a type for a pointee. (forward declarations in
2495 // limited debug info, full definitions (if the type definition is available)
2496 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002497 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2498 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002499 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002500 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002501
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002502 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002503 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002504
2505 // No definition for now. Emit a forward definition that might be
2506 // merged with a potential upcoming definition.
2507 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2508 return getFunctionForwardDeclaration(FD);
2509 else if (const auto *VD = dyn_cast<VarDecl>(D))
2510 return getGlobalVariableForwardDeclaration(VD);
2511
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002512 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002513}
2514
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002515llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002516 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002517 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002518
Guy Benyei11169dd2012-12-18 14:30:41 +00002519 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002520 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002521 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002522
2523 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002524 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002525
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002526 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002527 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002528 if (const CXXMethodDecl *MD =
2529 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002530 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002531 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002532 }
2533 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002534 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002535 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002536 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002537 return SP;
2538 }
2539
Aaron Ballman86c93902014-03-06 23:45:36 +00002540 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002541 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002542 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002543 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002544 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002545 return SP;
2546 }
2547 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002548 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002549}
2550
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002551// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002552// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002553llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002554 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002555 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002556 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002557 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2558 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002559 return DBuilder.createSubroutineType(F,
2560 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002561
2562 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2563 return getOrCreateMethodType(Method, F);
2564 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2565 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002566 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002567
2568 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002569 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002570
2571 // Replace the instancetype keyword with the actual type.
2572 if (ResultTy == CGM.getContext().getObjCInstanceType())
2573 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002574 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002575
Adrian Prantl7bec9032013-05-10 21:08:31 +00002576 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002578 QualType SelfDeclTy;
2579 if (auto *SelfDecl = OMethod->getSelfDecl())
2580 SelfDeclTy = SelfDecl->getType();
2581 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2582 if (FPT->getNumParams() > 1)
2583 SelfDeclTy = FPT->getParamType(0);
2584 if (!SelfDeclTy.isNull())
2585 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002587 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002588 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002590 for (const auto *PI : OMethod->params())
2591 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002592 // Variadic methods need a special marker at the end of the type list.
2593 if (OMethod->isVariadic())
2594 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002595
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002596 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002597 return DBuilder.createSubroutineType(F, EltTypeArray);
2598 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002599
Adrian Prantl800faef2014-02-25 23:42:18 +00002600 // Handle variadic function types; they need an additional
2601 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002602 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2603 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002604 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002605 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2606 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2607 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2608 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2609 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002610 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002611 return DBuilder.createSubroutineType(F, EltTypeArray);
2612 }
2613
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002614 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002615}
2616
Eric Christophere7b87e52014-10-26 23:40:33 +00002617void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2618 SourceLocation ScopeLoc, QualType FnType,
2619 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002620
2621 StringRef Name;
2622 StringRef LinkageName;
2623
2624 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2625
2626 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002627 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002628
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002630 llvm::DIFile *Unit = getOrCreateFile(Loc);
2631 llvm::DIScope *FDContext = Unit;
2632 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 if (!HasDecl) {
2634 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002635 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002637 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002638 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002640 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002641 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002642 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002643 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 return;
2645 }
2646 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002647 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2648 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002649 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2650 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002651 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 } else {
2653 // Use llvm function name.
2654 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002655 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 }
2657 if (!Name.empty() && Name[0] == '\01')
2658 Name = Name.substr(1);
2659
Adrian Prantl42d71b92014-04-10 23:21:53 +00002660 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002661 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002662 // Artificial functions without a location should not silently reuse CurLoc.
2663 if (Loc.isInvalid())
2664 CurLoc = SourceLocation();
2665 }
2666 unsigned LineNo = getLineNumber(Loc);
2667 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002668
Eric Christopher8018e412014-03-27 18:50:35 +00002669 // FIXME: The function declaration we're constructing here is mostly reusing
2670 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2671 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2672 // all subprograms instead of the actual context since subprogram definitions
2673 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002674 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002675 FDContext, Name, LinkageName, Unit, LineNo,
2676 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2677 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002678 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002679 // We might get here with a VarDecl in the case we're generating
2680 // code for the initialization of globals. Do not record these decls
2681 // as they will overwrite the actual VarDecl Decl in the cache.
2682 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002683 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002684
Adrian Prantlbebb8932014-03-21 21:01:58 +00002685 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002686 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002687
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002689 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002690}
2691
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002692void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2693 QualType FnType) {
2694 StringRef Name;
2695 StringRef LinkageName;
2696
2697 const Decl *D = GD.getDecl();
2698 if (!D)
2699 return;
2700
2701 unsigned Flags = 0;
2702 llvm::DIFile *Unit = getOrCreateFile(Loc);
2703 llvm::DIScope *FDContext = Unit;
2704 llvm::DINodeArray TParamsArray;
2705 if (isa<FunctionDecl>(D)) {
2706 // If there is a DISubprogram for this function available then use it.
2707 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2708 TParamsArray, Flags);
2709 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2710 Name = getObjCMethodName(OMD);
2711 Flags |= llvm::DINode::FlagPrototyped;
2712 } else {
2713 llvm_unreachable("not a function or ObjC method");
2714 }
2715 if (!Name.empty() && Name[0] == '\01')
2716 Name = Name.substr(1);
2717
2718 if (D->isImplicit()) {
2719 Flags |= llvm::DINode::FlagArtificial;
2720 // Artificial functions without a location should not silently reuse CurLoc.
2721 if (Loc.isInvalid())
2722 CurLoc = SourceLocation();
2723 }
2724 unsigned LineNo = getLineNumber(Loc);
2725 unsigned ScopeLine = 0;
2726
2727 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2728 getOrCreateFunctionType(D, FnType, Unit),
2729 false /*internalLinkage*/, true /*definition*/,
2730 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2731 TParamsArray.get(),
2732 getFunctionDeclaration(D));
2733}
2734
David Blaikie835afb22015-01-21 23:08:17 +00002735void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 // Update our current location
2737 setLocation(Loc);
2738
Eric Christophere7b87e52014-10-26 23:40:33 +00002739 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2740 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002741
Adrian Prantle83b1302014-01-07 22:05:52 +00002742 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002743 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002744 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002745}
2746
Guy Benyei11169dd2012-12-18 14:30:41 +00002747void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002748 llvm::MDNode *Back = nullptr;
2749 if (!LexicalBlockStack.empty())
2750 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002751 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002752 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002753 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002754}
2755
Eric Christopher0fdcb312013-05-16 00:52:20 +00002756void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2757 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002758 // Set our current location.
2759 setLocation(Loc);
2760
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002762 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2763 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002764
2765 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2766 return;
2767
2768 // Create a new lexical block and push it on the stack.
2769 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002770}
2771
Eric Christopher0fdcb312013-05-16 00:52:20 +00002772void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2773 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002774 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2775
2776 // Provide an entry in the line table for the end of the block.
2777 EmitLocation(Builder, Loc);
2778
David Blaikie60a877b2014-10-22 19:34:33 +00002779 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2780 return;
2781
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 LexicalBlockStack.pop_back();
2783}
2784
Guy Benyei11169dd2012-12-18 14:30:41 +00002785void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2786 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2787 unsigned RCount = FnBeginRegionCount.back();
2788 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2789
2790 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002791 while (LexicalBlockStack.size() != RCount) {
2792 // Provide an entry in the line table for the end of the block.
2793 EmitLocation(Builder, CurLoc);
2794 LexicalBlockStack.pop_back();
2795 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002796 FnBeginRegionCount.pop_back();
2797}
2798
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002799llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002800 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002801
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002802 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 QualType FType;
2804 uint64_t FieldSize, FieldOffset;
2805 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002807 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002808 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002809
2810 FieldOffset = 0;
2811 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2812 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2813 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2814 FType = CGM.getContext().IntTy;
2815 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2816 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2817
2818 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2819 if (HasCopyAndDispose) {
2820 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002821 EltTys.push_back(
2822 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2823 EltTys.push_back(
2824 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002825 }
2826 bool HasByrefExtendedLayout;
2827 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002828 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2829 HasByrefExtendedLayout) &&
2830 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002831 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002832 EltTys.push_back(
2833 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002834 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002835
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2837 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002838 CGM.getTarget().getPointerAlign(0))) {
2839 CharUnits FieldOffsetInBytes =
2840 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2841 CharUnits AlignedOffsetInBytes =
2842 FieldOffsetInBytes.RoundUpToAlignment(Align);
2843 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002844
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 if (NumPaddingBytes.isPositive()) {
2846 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2847 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2848 pad, ArrayType::Normal, 0);
2849 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2850 }
2851 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002852
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002854 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 FieldSize = CGM.getContext().getTypeSize(FType);
2856 FieldAlign = CGM.getContext().toBits(Align);
2857
Eric Christopherb2a008c2013-05-16 00:45:12 +00002858 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002859 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2860 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002861 EltTys.push_back(FieldTy);
2862 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002863
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002864 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002865
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002866 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002867
Guy Benyei11169dd2012-12-18 14:30:41 +00002868 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002869 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002870}
2871
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002872void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2873 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002874 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002875 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2877
David Blaikie7fceebf2013-08-19 03:37:48 +00002878 bool Unwritten =
2879 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2880 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002881 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002882 if (!Unwritten)
2883 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002884 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 uint64_t XOffset = 0;
2886 if (VD->hasAttr<BlocksAttr>())
2887 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002888 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 Ty = getOrCreateType(VD->getType(), Unit);
2890
2891 // If there is no debug info for this type then do not emit debug info
2892 // for this variable.
2893 if (!Ty)
2894 return;
2895
Guy Benyei11169dd2012-12-18 14:30:41 +00002896 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002897 unsigned Line = 0;
2898 unsigned Column = 0;
2899 if (!Unwritten) {
2900 Line = getLineNumber(VD->getLocation());
2901 Column = getColumnNumber(VD->getLocation());
2902 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002903 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 unsigned Flags = 0;
2905 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002906 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 // If this is the first argument and it is implicit then
2908 // give it an object pointer flag.
2909 // FIXME: There has to be a better way to do this, but for static
2910 // functions there won't be an implicit param at arg1 and
2911 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002912 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002913 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002914 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002915 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2916 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002917 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002918
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002919 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002920
2921 StringRef Name = VD->getName();
2922 if (!Name.empty()) {
2923 if (VD->hasAttr<BlocksAttr>()) {
2924 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002925 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 // offset of __forwarding field
2927 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002928 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002929 Expr.push_back(offset.getQuantity());
2930 Expr.push_back(llvm::dwarf::DW_OP_deref);
2931 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 // offset of x field
2933 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002934 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002935
2936 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002937 auto *D = ArgNo
2938 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2939 *ArgNo, Unit, Line, Ty)
2940 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2941 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002942
Guy Benyei11169dd2012-12-18 14:30:41 +00002943 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002944 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2945 llvm::DebugLoc::get(Line, Column, Scope),
2946 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002947 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002948 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002949 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002950 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2951 // If VD is an anonymous union then Storage represents value for
2952 // all union fields.
2953 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2954 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002955 // GDB has trouble finding local variables in anonymous unions, so we emit
2956 // artifical local variables for each of the members.
2957 //
2958 // FIXME: Remove this code as soon as GDB supports this.
2959 // The debug info verifier in LLVM operates based on the assumption that a
2960 // variable has the same size as its storage and we had to disable the check
2961 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002962 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002963 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002964 StringRef FieldName = Field->getName();
2965
2966 // Ignore unnamed fields. Do not ignore unnamed records.
2967 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2968 continue;
2969
2970 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002971 auto *D = DBuilder.createAutoVariable(
2972 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2973 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002974
2975 // Insert an llvm.dbg.declare into the current block.
2976 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2977 llvm::DebugLoc::get(Line, Column, Scope),
2978 Builder.GetInsertBlock());
2979 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002980 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002982
2983 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002984 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002985 ArgNo
2986 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2987 Ty, CGM.getLangOpts().Optimize,
2988 Flags)
2989 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2990 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002991
2992 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002993 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2994 llvm::DebugLoc::get(Line, Column, Scope),
2995 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002996}
2997
2998void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2999 llvm::Value *Storage,
3000 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003001 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003002 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003003}
3004
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003005llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3006 llvm::DIType *Ty) {
3007 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003008 if (CachedTy)
3009 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003010 return DBuilder.createObjectPointerType(Ty);
3011}
3012
Eric Christophere7b87e52014-10-26 23:40:33 +00003013void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3014 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003015 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003016 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003018
Craig Topper8a13c412014-05-21 05:09:00 +00003019 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003021
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003023
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003025 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3026 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 if (isByRef)
3028 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003029 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 Ty = getOrCreateType(VD->getType(), Unit);
3031
3032 // Self is passed along as an implicit non-arg variable in a
3033 // block. Mark it as the object pointer.
3034 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003035 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003036
3037 // Get location information.
3038 unsigned Line = getLineNumber(VD->getLocation());
3039 unsigned Column = getColumnNumber(VD->getLocation());
3040
3041 const llvm::DataLayout &target = CGM.getDataLayout();
3042
3043 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003044 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3046
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003047 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003048 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003049 addr.push_back(llvm::dwarf::DW_OP_deref);
3050 addr.push_back(llvm::dwarf::DW_OP_plus);
3051 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003053 addr.push_back(llvm::dwarf::DW_OP_deref);
3054 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003056 offset =
3057 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003058 addr.push_back(offset.getQuantity());
3059 addr.push_back(llvm::dwarf::DW_OP_deref);
3060 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 // offset of x field
3062 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003063 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 }
3065
3066 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003067 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003068 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003069 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003070
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003072 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3073 if (InsertPoint)
3074 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3075 InsertPoint);
3076 else
3077 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3078 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003079}
3080
Guy Benyei11169dd2012-12-18 14:30:41 +00003081void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3082 unsigned ArgNo,
3083 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003084 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003085 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003086}
3087
3088namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003089struct BlockLayoutChunk {
3090 uint64_t OffsetInBits;
3091 const BlockDecl::Capture *Capture;
3092};
3093bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3094 return l.OffsetInBits < r.OffsetInBits;
3095}
Guy Benyei11169dd2012-12-18 14:30:41 +00003096}
3097
3098void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003099 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003100 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003101 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003103 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 ASTContext &C = CGM.getContext();
3105 const BlockDecl *blockDecl = block.getBlockDecl();
3106
3107 // Collect some general information about the block's location.
3108 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003109 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003110 unsigned line = getLineNumber(loc);
3111 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003112
Guy Benyei11169dd2012-12-18 14:30:41 +00003113 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003114 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003115
3116 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003117 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003118
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003119 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3121 blockLayout->getElementOffsetInBits(0),
3122 tunit, tunit));
3123 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3124 blockLayout->getElementOffsetInBits(1),
3125 tunit, tunit));
3126 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3127 blockLayout->getElementOffsetInBits(2),
3128 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003129 auto *FnTy = block.getBlockExpr()->getFunctionType();
3130 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3131 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 blockLayout->getElementOffsetInBits(3),
3133 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003134 fields.push_back(createFieldType(
3135 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3136 ? C.getBlockDescriptorExtendedType()
3137 : C.getBlockDescriptorType()),
3138 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003139
3140 // We want to sort the captures by offset, not because DWARF
3141 // requires this, but because we're paranoid about debuggers.
3142 SmallVector<BlockLayoutChunk, 8> chunks;
3143
3144 // 'this' capture.
3145 if (blockDecl->capturesCXXThis()) {
3146 BlockLayoutChunk chunk;
3147 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003148 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003149 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 chunks.push_back(chunk);
3151 }
3152
3153 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003154 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 const VarDecl *variable = capture.getVariable();
3156 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3157
3158 // Ignore constant captures.
3159 if (captureInfo.isConstant())
3160 continue;
3161
3162 BlockLayoutChunk chunk;
3163 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003164 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003165 chunk.Capture = &capture;
3166 chunks.push_back(chunk);
3167 }
3168
3169 // Sort by offset.
3170 llvm::array_pod_sort(chunks.begin(), chunks.end());
3171
Eric Christophere7b87e52014-10-26 23:40:33 +00003172 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3173 e = chunks.end();
3174 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 uint64_t offsetInBits = i->OffsetInBits;
3176 const BlockDecl::Capture *capture = i->Capture;
3177
3178 // If we have a null capture, this must be the C++ 'this' capture.
3179 if (!capture) {
3180 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003181 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003182 QualType type = method->getThisType(C);
3183
3184 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3185 offsetInBits, tunit, tunit));
3186 continue;
3187 }
3188
3189 const VarDecl *variable = capture->getVariable();
3190 StringRef name = variable->getName();
3191
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003192 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003193 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003194 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003195
3196 // FIXME: this creates a second copy of this type!
3197 uint64_t xoffset;
3198 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003199 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3200 fieldType =
3201 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3202 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003204 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3205 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003206 }
3207 fields.push_back(fieldType);
3208 }
3209
3210 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003211 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3212 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003213
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003214 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003215
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003216 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003217 tunit, typeName.str(), tunit, line,
3218 CGM.getContext().toBits(block.BlockSize),
3219 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3221
3222 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003223 unsigned flags = llvm::DINode::FlagArtificial;
3224 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003225
3226 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003227 auto *debugVar = DBuilder.createParameterVariable(
3228 scope, Arg->getName(), ArgNo, tunit, line, type,
3229 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003230
Adrian Prantl616bef42013-03-14 21:52:59 +00003231 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003232 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003233 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003234 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003235 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003236 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003237
Adrian Prantl616bef42013-03-14 21:52:59 +00003238 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003239 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3240 llvm::DebugLoc::get(line, column, scope),
3241 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003242}
3243
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003244llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003245CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3246 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003247 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003248
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003249 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003250 if (MI != StaticDataMemberCache.end()) {
3251 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003252 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003253 }
David Blaikiece763042013-08-20 21:49:21 +00003254
3255 // If the member wasn't found in the cache, lazily construct and add it to the
3256 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003257 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003258 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003259 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003260}
3261
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003262llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3263 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3264 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3265 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003266
3267 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003268 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003269 StringRef FieldName = Field->getName();
3270
3271 // Ignore unnamed fields, but recurse into anonymous records.
3272 if (FieldName.empty()) {
3273 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3274 if (RT)
3275 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3276 Var, DContext);
3277 continue;
3278 }
3279 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003280 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3281 LineNo, FieldTy,
3282 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003283 }
3284 return GV;
3285}
3286
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003287void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003289 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003290 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003291 llvm::DIFile *Unit = nullptr;
3292 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003293 unsigned LineNo;
3294 StringRef DeclName, LinkageName;
3295 QualType T;
3296 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003297
3298 // Attempt to store one global variable for the declaration - even if we
3299 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003300 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003301
3302 // If this is an anonymous union then we'll want to emit a global
3303 // variable for each member of the anonymous union so that it's possible
3304 // to find the name of any field in the union.
3305 if (T->isUnionType() && DeclName.empty()) {
3306 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003307 assert(RD->isAnonymousStructOrUnion() &&
3308 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003309 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3310 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003311 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003312 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3313 Var->hasInternalLinkage(), Var,
3314 getOrCreateStaticDataMemberDeclarationOrNull(D));
3315 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003316 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003317}
3318
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003319void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3320 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003321 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003322 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003323 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003324 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003325 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003326 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3327 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3328 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3329 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3330 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003331 // Do not use global variables for enums.
3332 //
3333 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003334 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003335 return;
David Blaikiea15565562014-04-04 20:56:17 +00003336 // Do not emit separate definitions for function local const/statics.
3337 if (isa<FunctionDecl>(VD->getDeclContext()))
3338 return;
David Blaikiebb113912014-04-05 07:23:17 +00003339 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003340 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003341 if (VarD->isStaticDataMember()) {
3342 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003343 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003344 // Ensure that the type is retained even though it's otherwise unreferenced.
3345 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003346 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003347 return;
3348 }
3349
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003350 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003351
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003352 auto &GV = DeclCache[VD];
3353 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003354 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003355 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003356 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003357 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003358}
3359
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003360llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003361 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003362 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003363 llvm::DIScope *Mod = getParentModuleOrNull(D);
3364 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003365}
3366
David Blaikie9f88fe82013-04-22 06:13:21 +00003367void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003368 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3369 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003370 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003371 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3372 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003373 getLineNumber(UD.getLocation()));
3374}
3375
David Blaikiebd483762013-05-20 04:58:53 +00003376void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3377 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3378 return;
3379 assert(UD.shadow_size() &&
3380 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3381 // Emitting one decl is sufficient - debuggers can detect that this is an
3382 // overloaded name & provide lookup for all the overloads.
3383 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003384 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003385 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003386 DBuilder.createImportedDeclaration(
3387 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3388 getLineNumber(USD.getLocation()));
3389}
3390
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003391void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
Adrian Prantlc6458d62015-09-19 00:10:32 +00003392 auto Info = ExternalASTSource::ASTSourceDescriptor(*ID.getImportedModule());
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003393 DBuilder.createImportedDeclaration(
Adrian Prantl66689202015-09-18 23:01:45 +00003394 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3395 getOrCreateModuleRef(Info, DebugTypeExtRefs),
3396 getLineNumber(ID.getLocation()));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003397}
3398
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003399llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003400CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3401 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003402 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003403 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003404 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003405 return cast<llvm::DIImportedEntity>(VH);
3406 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003407 if (const NamespaceAliasDecl *Underlying =
3408 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3409 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003410 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003411 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3412 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3413 NA.getName());
3414 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003415 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003416 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3417 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3418 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003419 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003420 return R;
3421}
3422
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003423llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003424CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003425 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003426 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003427 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003428 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003429
Guy Benyei11169dd2012-12-18 14:30:41 +00003430 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003431 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003432 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003433 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003434 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003435 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003436 return NS;
3437}
3438
3439void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003440 // Creating types might create further types - invalidating the current
3441 // element and the size(), so don't cache/reference them.
3442 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3443 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003444 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003445 ? CreateTypeDefinition(E.Type, E.Unit)
3446 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003447 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003448 }
3449
David Blaikief427b002014-05-06 03:42:01 +00003450 for (auto p : ReplaceMap) {
3451 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003452 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003453 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003454
David Blaikief427b002014-05-06 03:42:01 +00003455 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003456 assert(it != TypeCache.end());
3457 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003458
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003459 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3460 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003462
Frederic Rissd253ed62014-11-18 03:40:51 +00003463 for (const auto &p : FwdDeclReplaceMap) {
3464 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003465 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003466 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003467
3468 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003469 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003470 // with ourselves, that will destroy the temporary MDNode and
3471 // replace it with a standard one, avoiding leaking memory.
3472 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003473 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003474 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003475 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003476
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003477 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003478 }
3479
Adrian Prantl73409ce2013-03-11 18:33:46 +00003480 // We keep our own list of retained types, because we need to look
3481 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003482 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003483 if (auto MD = TypeCache[RT])
3484 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003485
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 DBuilder.finalize();
3487}
David Blaikie66088d52014-09-24 17:01:27 +00003488
3489void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3490 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3491 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003492
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003493 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003494 // Don't ignore in case of explicit cast where it is referenced indirectly.
3495 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003496}