blob: b8cf83ee48c66d269f4278cb89bf63bfb3252148 [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 *
1676CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantld0929a12015-09-11 01:03:19 +00001677 auto &ModRef = ModuleRefCache[Mod.Signature];
1678 if (ModRef)
1679 return cast<llvm::DIModule>(ModRef);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001680
1681 // Macro definitions that were defined with "-D" on the command line.
1682 SmallString<128> ConfigMacros;
1683 {
1684 llvm::raw_svector_ostream OS(ConfigMacros);
1685 const auto &PPOpts = CGM.getPreprocessorOpts();
1686 unsigned I = 0;
1687 // Translate the macro definitions back into a commmand line.
1688 for (auto &M : PPOpts.Macros) {
1689 if (++I > 1)
1690 OS << " ";
1691 const std::string &Macro = M.first;
1692 bool Undef = M.second;
1693 OS << "\"-" << (Undef ? 'U' : 'D');
1694 for (char c : Macro)
1695 switch (c) {
1696 case '\\' : OS << "\\\\"; break;
1697 case '"' : OS << "\\\""; break;
1698 default: OS << c;
1699 }
1700 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001701 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001702 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001703 llvm::DIBuilder DIB(CGM.getModule());
Adrian Prantlb67dbce2015-09-11 18:45:02 +00001704 auto *CU = DIB.createCompileUnit(TheCU->getSourceLanguage(), Mod.ModuleName,
1705 Mod.Path, TheCU->getProducer(), true,
1706 StringRef(), 0, Mod.ASTFile,
1707 llvm::DIBuilder::FullDebug, Mod.Signature);
1708 llvm::DIModule *M =
1709 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, Mod.Path,
1710 CGM.getHeaderSearchOpts().Sysroot);
Adrian Prantl2388ead2015-06-30 18:01:05 +00001711 DIB.finalize();
Adrian Prantld0929a12015-09-11 01:03:19 +00001712 ModRef.reset(M);
1713 return M;
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001714}
1715
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001716llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1717 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001718 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001719 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001720 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001721 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001722
1723 // Bit size, align and offset of the type.
1724 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1725 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1726
1727 unsigned Flags = 0;
1728 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001730
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001731 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001732 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001733 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001734
David Blaikieef8a9512014-05-05 23:23:53 +00001735 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001736 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001737
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001738 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001739 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001740 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001741
1742 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001743 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001744
1745 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1746 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001747 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001748 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001749 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001750 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001751
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001752 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001753 EltTys.push_back(InhTag);
1754 }
1755
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001756 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001757 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001758 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001759 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 unsigned PLine = getLineNumber(Loc);
1761 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1762 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001763 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1764 PD->getName(), PUnit, PLine,
1765 hasDefaultGetterName(PD, Getter) ? ""
1766 : getSelectorName(PD->getGetterName()),
1767 hasDefaultSetterName(PD, Setter) ? ""
1768 : getSelectorName(PD->getSetterName()),
1769 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 EltTys.push_back(PropertyNode);
1771 }
1772
1773 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1774 unsigned FieldNo = 0;
1775 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1776 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001777 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001778 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001779 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001780
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 StringRef FieldName = Field->getName();
1782
1783 // Ignore unnamed fields.
1784 if (FieldName.empty())
1785 continue;
1786
1787 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001788 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 unsigned FieldLine = getLineNumber(Field->getLocation());
1790 QualType FType = Field->getType();
1791 uint64_t FieldSize = 0;
1792 unsigned FieldAlign = 0;
1793
1794 if (!FType->isIncompleteArrayType()) {
1795
1796 // Bit size, align and offset of the type.
1797 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001798 ? Field->getBitWidthValue(CGM.getContext())
1799 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 FieldAlign = CGM.getContext().getTypeAlign(FType);
1801 }
1802
1803 uint64_t FieldOffset;
1804 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1805 // We don't know the runtime offset of an ivar if we're using the
1806 // non-fragile ABI. For bitfields, use the bit offset into the first
1807 // byte of storage of the bitfield. For other fields, use zero.
1808 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001809 FieldOffset =
1810 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001811 FieldOffset %= CGM.getContext().getCharWidth();
1812 } else {
1813 FieldOffset = 0;
1814 }
1815 } else {
1816 FieldOffset = RL.getFieldOffset(FieldNo);
1817 }
1818
1819 unsigned Flags = 0;
1820 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001821 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001823 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001824 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001825 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001826
Craig Topper8a13c412014-05-21 05:09:00 +00001827 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001829 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001830 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001831 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001832 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001833 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001834 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1836 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001837 PropertyNode = DBuilder.createObjCProperty(
1838 PD->getName(), PUnit, PLine,
1839 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1840 PD->getGetterName()),
1841 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1842 PD->getSetterName()),
1843 PD->getPropertyAttributes(),
1844 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001845 }
1846 }
1847 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001848 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1849 FieldSize, FieldAlign, FieldOffset, Flags,
1850 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001851 EltTys.push_back(FieldTy);
1852 }
1853
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001854 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001855 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001856
Guy Benyei11169dd2012-12-18 14:30:41 +00001857 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001858 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001859}
1860
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001861llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1862 llvm::DIFile *Unit) {
1863 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 int64_t Count = Ty->getNumElements();
1865 if (Count == 0)
1866 // If number of elements are not known then this is an unbounded array.
1867 // Use Count == -1 to express such arrays.
1868 Count = -1;
1869
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001870 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001871 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001872
1873 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1874 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1875
1876 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1877}
1878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001879llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 uint64_t Size;
1881 uint64_t Align;
1882
1883 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1884 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1885 Size = 0;
1886 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001887 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 } else if (Ty->isIncompleteArrayType()) {
1889 Size = 0;
1890 if (Ty->getElementType()->isIncompleteType())
1891 Align = 0;
1892 else
1893 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001894 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001895 Size = 0;
1896 Align = 0;
1897 } else {
1898 // Size and align of the whole array, not the element type.
1899 Size = CGM.getContext().getTypeSize(Ty);
1900 Align = CGM.getContext().getTypeAlign(Ty);
1901 }
1902
1903 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1904 // interior arrays, do we care? Why aren't nested arrays represented the
1905 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001906 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 QualType EltTy(Ty, 0);
1908 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1909 // If the number of elements is known, then count is that number. Otherwise,
1910 // it's -1. This allows us to represent a subrange with an array of 0
1911 // elements, like this:
1912 //
1913 // struct foo {
1914 // int x[0];
1915 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001916 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001917 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1918 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // FIXME: Verify this is right for VLAs.
1921 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1922 EltTy = Ty->getElementType();
1923 }
1924
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001925 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001926
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001927 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1928 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001929}
1930
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1932 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001933 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1934 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001935}
1936
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001937llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1938 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001939 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1940 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001941}
1942
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001943llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1944 llvm::DIFile *U) {
David Majnemer9df56372015-09-10 21:52:00 +00001945 uint64_t Size =
1946 !Ty->isIncompleteType() ? CGM.getContext().getTypeSize(Ty) : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001947 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001948 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001949 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001950 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001951
1952 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001953 Ty->getPointeeType()->getAs<FunctionProtoType>();
1954 return DBuilder.createMemberPointerType(
1955 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1956 Ty->getClass(), FPT->getTypeQuals())),
1957 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001958 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001959}
1960
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001961llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001962 // Ignore the atomic wrapping
1963 // FIXME: What is the correct representation?
1964 return getOrCreateType(Ty->getValueType(), U);
1965}
1966
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001967llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001968 const EnumDecl *ED = Ty->getDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001969
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 uint64_t Size = 0;
1971 uint64_t Align = 0;
1972 if (!ED->getTypeForDecl()->isIncompleteType()) {
1973 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1974 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1975 }
1976
Manman Rene0064d82013-08-29 23:19:58 +00001977 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1978
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001979 bool isImportedFromModule =
1980 DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition();
1981
Guy Benyei11169dd2012-12-18 14:30:41 +00001982 // If this is just a forward declaration, construct an appropriately
1983 // marked node and just return it.
Adrian Prantl5c8bd882015-09-11 17:23:08 +00001984 if (isImportedFromModule || !ED->getDefinition()) {
Adrian Prantl6ec370a2015-09-10 18:39:45 +00001985 llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001986 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 unsigned Line = getLineNumber(ED->getLocation());
1988 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001989 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001990 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001991 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001992 ReplaceMap.emplace_back(
1993 std::piecewise_construct, std::make_tuple(Ty),
1994 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001995 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 }
1997
David Blaikie483a9da2014-05-06 18:35:21 +00001998 return CreateTypeDefinition(Ty);
1999}
2000
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002001llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002002 const EnumDecl *ED = Ty->getDecl();
2003 uint64_t Size = 0;
2004 uint64_t Align = 0;
2005 if (!ED->getTypeForDecl()->isIncompleteType()) {
2006 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2007 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2008 }
2009
2010 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2011
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002012 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002013 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002015 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002016 Enumerators.push_back(DBuilder.createEnumerator(
2017 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 }
2019
2020 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002021 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002022
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002023 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 unsigned Line = getLineNumber(ED->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002025 llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002026 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002027 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2028 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2029 Line, Size, Align, EltArray, ClassTy,
2030 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002031}
2032
David Blaikie05491062013-01-21 04:37:12 +00002033static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2034 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002036 Qualifiers InnerQuals = T.getLocalQualifiers();
2037 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2038 // that is already there.
2039 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2040 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 QualType LastT = T;
2042 switch (T->getTypeClass()) {
2043 default:
David Blaikie05491062013-01-21 04:37:12 +00002044 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002045 case Type::TemplateSpecialization: {
2046 const auto *Spec = cast<TemplateSpecializationType>(T);
2047 if (Spec->isTypeAlias())
2048 return C.getQualifiedType(T.getTypePtr(), Quals);
2049 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002050 break;
2051 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002052 case Type::TypeOfExpr:
2053 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2054 break;
2055 case Type::TypeOf:
2056 T = cast<TypeOfType>(T)->getUnderlyingType();
2057 break;
2058 case Type::Decltype:
2059 T = cast<DecltypeType>(T)->getUnderlyingType();
2060 break;
2061 case Type::UnaryTransform:
2062 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2063 break;
2064 case Type::Attributed:
2065 T = cast<AttributedType>(T)->getEquivalentType();
2066 break;
2067 case Type::Elaborated:
2068 T = cast<ElaboratedType>(T)->getNamedType();
2069 break;
2070 case Type::Paren:
2071 T = cast<ParenType>(T)->getInnerType();
2072 break;
David Blaikie05491062013-01-21 04:37:12 +00002073 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002075 break;
2076 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002077 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002078 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002079 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002080 break;
2081 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002082
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002084 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002085 } while (true);
2086}
2087
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002088llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002089
2090 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002091 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002092
David Blaikief427b002014-05-06 03:42:01 +00002093 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 if (it != TypeCache.end()) {
2095 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002096 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002097 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 }
2099
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002100 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002101}
2102
David Blaikie0e716b42014-03-03 23:48:23 +00002103void CGDebugInfo::completeTemplateDefinition(
2104 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002105 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2106 return;
2107
David Blaikie0e716b42014-03-03 23:48:23 +00002108 completeClassData(&SD);
2109 // In case this type has no member function definitions being emitted, ensure
2110 // it is retained
2111 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2112}
2113
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002114llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002115 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002116 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002117
2118 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002119 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002120
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002121 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002122 return T;
2123
Adrian Prantlca844182015-09-11 17:23:03 +00002124 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002125 void* TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002126
2127 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002128 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002129
Guy Benyei11169dd2012-12-18 14:30:41 +00002130 return Res;
2131}
2132
Eric Christopher1ecc5632013-06-07 22:54:39 +00002133unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002134 // The assumption is that the number of ivars can only increase
2135 // monotonically, so it is safe to just use their current number as
2136 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002137 unsigned Sum = 0;
2138 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002139 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002140 ++Sum;
2141
2142 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002143}
2144
2145ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2146 switch (Ty->getTypeClass()) {
2147 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002148 return getObjCInterfaceDecl(
2149 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002150 case Type::ObjCInterface:
2151 return cast<ObjCInterfaceType>(Ty)->getDecl();
2152 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002153 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002154 }
2155}
2156
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002157llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
Adrian Prantl453cdf02015-09-11 18:54:31 +00002158 if (!DebugTypeExtRefs || !D->isFromASTFile())
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002159 return nullptr;
2160
2161 llvm::DIModule *ModuleRef = nullptr;
2162 auto *Reader = CGM.getContext().getExternalSource();
2163 auto Idx = D->getOwningModuleID();
2164 auto Info = Reader->getSourceDescriptor(Idx);
2165 if (Info)
2166 ModuleRef = getOrCreateModuleRef(*Info);
2167 return ModuleRef;
2168}
2169
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002170llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 // Handle qualifiers, which recursively handles what they refer to.
2172 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002173 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002174
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 // Work out details of type.
2176 switch (Ty->getTypeClass()) {
2177#define TYPE(Class, Base)
2178#define ABSTRACT_TYPE(Class, Base)
2179#define NON_CANONICAL_TYPE(Class, Base)
2180#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2181#include "clang/AST/TypeNodes.def"
2182 llvm_unreachable("Dependent types cannot show up in debug information");
2183
2184 case Type::ExtVector:
2185 case Type::Vector:
2186 return CreateType(cast<VectorType>(Ty), Unit);
2187 case Type::ObjCObjectPointer:
2188 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2189 case Type::ObjCObject:
2190 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2191 case Type::ObjCInterface:
2192 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2193 case Type::Builtin:
2194 return CreateType(cast<BuiltinType>(Ty));
2195 case Type::Complex:
2196 return CreateType(cast<ComplexType>(Ty));
2197 case Type::Pointer:
2198 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002199 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002200 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002201 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002202 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002203 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002204 case Type::BlockPointer:
2205 return CreateType(cast<BlockPointerType>(Ty), Unit);
2206 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002207 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002208 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002209 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002211 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002212 case Type::FunctionProto:
2213 case Type::FunctionNoProto:
2214 return CreateType(cast<FunctionType>(Ty), Unit);
2215 case Type::ConstantArray:
2216 case Type::VariableArray:
2217 case Type::IncompleteArray:
2218 return CreateType(cast<ArrayType>(Ty), Unit);
2219
2220 case Type::LValueReference:
2221 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2222 case Type::RValueReference:
2223 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2224
2225 case Type::MemberPointer:
2226 return CreateType(cast<MemberPointerType>(Ty), Unit);
2227
2228 case Type::Atomic:
2229 return CreateType(cast<AtomicType>(Ty), Unit);
2230
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002232 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2233
David Blaikie42edade2014-11-11 20:44:45 +00002234 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002235 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 case Type::Elaborated:
2237 case Type::Paren:
2238 case Type::SubstTemplateTypeParm:
2239 case Type::TypeOfExpr:
2240 case Type::TypeOf:
2241 case Type::Decltype:
2242 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002243 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002244 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002246
David Blaikie42edade2014-11-11 20:44:45 +00002247 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002248}
2249
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002250llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2251 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002252 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002253
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002254 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002255
2256 // We may have cached a forward decl when we could have created
2257 // a non-forward decl. Go ahead and create a non-forward decl
2258 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002259 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002260 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002261
2262 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002263 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002264
2265 // Propagate members from the declaration to the definition
2266 // CreateType(const RecordType*) will overwrite this with the members in the
2267 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002268 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002269
Guy Benyei11169dd2012-12-18 14:30:41 +00002270 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002271 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 return Res;
2273}
2274
2275// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002276llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002277 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002278
Guy Benyei11169dd2012-12-18 14:30:41 +00002279 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002280 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002281 unsigned Line = getLineNumber(RD->getLocation());
2282 StringRef RDName = getClassName(RD);
2283
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002284 llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
Guy Benyei11169dd2012-12-18 14:30:41 +00002285
David Blaikied2785892013-08-18 17:36:19 +00002286 // If we ended up creating the type during the context chain construction,
2287 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002288 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002289 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002290 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002291 return T;
David Blaikied2785892013-08-18 17:36:19 +00002292
Adrian Prantl381e7552014-02-04 21:29:50 +00002293 // If this is just a forward or incomplete declaration, construct an
2294 // appropriately marked node and just return it.
2295 const RecordDecl *D = RD->getDefinition();
2296 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002297 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002298
2299 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2300 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002301
Manman Rene0064d82013-08-29 23:19:58 +00002302 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2303
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002304 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002305 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2306 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002307
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002308 RegionMap[Ty->getDecl()].reset(RealDecl);
2309 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002310
David Blaikieadfbf992013-08-18 16:55:33 +00002311 if (const ClassTemplateSpecializationDecl *TSpecial =
2312 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002313 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002314 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002315 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002316}
2317
David Blaikieadfbf992013-08-18 16:55:33 +00002318void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002319 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002320 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002321 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002322 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2323 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002324 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002325 while (1) {
2326 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2327 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2328 if (PBT && !BRL.isPrimaryBaseVirtual())
2329 PBase = PBT;
2330 else
2331 break;
2332 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002333 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002334 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2335 getOrCreateFile(RD->getLocation())));
2336 } else if (RD->isDynamicClass())
2337 ContainingType = RealDecl;
2338
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002339 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002340}
2341
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002342llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002343 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002344 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002345 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2346 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002347 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002348 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 *Offset += FieldSize;
2350 return Ty;
2351}
2352
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002353void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002354 StringRef &Name,
2355 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002356 llvm::DIScope *&FDContext,
2357 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002358 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002359 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2360 Name = getFunctionName(FD);
2361 // Use mangled name as linkage name for C/C++ functions.
2362 if (FD->hasPrototype()) {
2363 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002364 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002365 }
2366 // No need to replicate the linkage name if it isn't different from the
2367 // subprogram name, no need to have it at all unless coverage is enabled or
2368 // debug is set to more than just line tables.
2369 if (LinkageName == Name ||
2370 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2371 !CGM.getCodeGenOpts().EmitGcovNotes &&
2372 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2373 LinkageName = StringRef();
2374
2375 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2376 if (const NamespaceDecl *NSDecl =
2377 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2378 FDContext = getOrCreateNameSpace(NSDecl);
2379 else if (const RecordDecl *RDecl =
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002380 dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
2381 llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
2382 FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU);
2383 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002384 // Collect template parameters.
2385 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2386 }
2387}
2388
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002389void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002390 unsigned &LineNo, QualType &T,
2391 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002393 Unit = getOrCreateFile(VD->getLocation());
2394 LineNo = getLineNumber(VD->getLocation());
2395
2396 setLocation(VD->getLocation());
2397
2398 T = VD->getType();
2399 if (T->isIncompleteArrayType()) {
2400 // CodeGen turns int[] into int[1] so we'll do the same here.
2401 llvm::APInt ConstVal(32, 1);
2402 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2403
2404 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2405 ArrayType::Normal, 0);
2406 }
2407
2408 Name = VD->getName();
2409 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2410 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2411 LinkageName = CGM.getMangledName(VD);
2412 if (LinkageName == Name)
2413 LinkageName = StringRef();
2414
2415 // Since we emit declarations (DW_AT_members) for static members, place the
2416 // definition of those static members in the namespace they were declared in
2417 // in the source code (the lexical decl context).
2418 // FIXME: Generalize this for even non-member global variables where the
2419 // declaration and definition may have different lexical decl contexts, once
2420 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002421 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2422 : VD->getDeclContext();
2423 // When a record type contains an in-line initialization of a static data
2424 // member, and the record type is marked as __declspec(dllexport), an implicit
2425 // definition of the member will be created in the record context. DWARF
2426 // doesn't seem to have a nice way to describe this in a form that consumers
2427 // are likely to understand, so fake the "normal" situation of a definition
2428 // outside the class by putting it in the global scope.
2429 if (DC->isRecord())
2430 DC = CGM.getContext().getTranslationUnitDecl();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00002431
2432 llvm::DIScope *Mod = getParentModuleOrNull(VD);
2433 VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
Frederic Riss9db79f12014-11-18 03:40:46 +00002434}
2435
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002436llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002437CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002438 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002439 StringRef Name, LinkageName;
2440 unsigned Flags = 0;
2441 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002442 llvm::DIFile *Unit = getOrCreateFile(Loc);
2443 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002444 unsigned Line = getLineNumber(Loc);
2445
2446 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2447 TParamsArray, Flags);
2448 // Build function type.
2449 SmallVector<QualType, 16> ArgTypes;
2450 for (const ParmVarDecl *Parm: FD->parameters())
2451 ArgTypes.push_back(Parm->getType());
2452 QualType FnType =
2453 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2454 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002456 DContext, Name, LinkageName, Unit, Line,
2457 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002458 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002459 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002460 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002461 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2462 std::make_tuple(CanonDecl),
2463 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002464 return SP;
2465}
2466
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002468CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2469 QualType T;
2470 StringRef Name, LinkageName;
2471 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002472 llvm::DIFile *Unit = getOrCreateFile(Loc);
2473 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002474 unsigned Line = getLineNumber(Loc);
2475
2476 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002477 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2478 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2479 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002480 FwdDeclReplaceMap.emplace_back(
2481 std::piecewise_construct,
2482 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2483 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002484 return GV;
2485}
2486
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002487llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002488 // We only need a declaration (not a definition) of the type - so use whatever
2489 // we would otherwise do to get a type for a pointee. (forward declarations in
2490 // limited debug info, full definitions (if the type definition is available)
2491 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002492 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2493 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002494 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002495 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002496
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002497 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002498 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002499
2500 // No definition for now. Emit a forward definition that might be
2501 // merged with a potential upcoming definition.
2502 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2503 return getFunctionForwardDeclaration(FD);
2504 else if (const auto *VD = dyn_cast<VarDecl>(D))
2505 return getGlobalVariableForwardDeclaration(VD);
2506
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002507 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002508}
2509
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002510llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002511 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002512 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002513
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002515 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002516 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002517
2518 // Setup context.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00002519 auto *S = getDeclContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00002520
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002521 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002522 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002523 if (const CXXMethodDecl *MD =
2524 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002525 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002526 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002527 }
2528 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002531 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002532 return SP;
2533 }
2534
Aaron Ballman86c93902014-03-06 23:45:36 +00002535 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002536 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002537 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002538 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002539 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002540 return SP;
2541 }
2542 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002543 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002544}
2545
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002546// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002547// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002548llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002549 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002551 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002552 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2553 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002554 return DBuilder.createSubroutineType(F,
2555 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002556
2557 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2558 return getOrCreateMethodType(Method, F);
2559 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2560 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002561 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002562
2563 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002564 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002565
2566 // Replace the instancetype keyword with the actual type.
2567 if (ResultTy == CGM.getContext().getObjCInstanceType())
2568 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002569 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002570
Adrian Prantl7bec9032013-05-10 21:08:31 +00002571 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 // "self" pointer is always first argument.
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002573 QualType SelfDeclTy;
2574 if (auto *SelfDecl = OMethod->getSelfDecl())
2575 SelfDeclTy = SelfDecl->getType();
2576 else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType))
2577 if (FPT->getNumParams() > 1)
2578 SelfDeclTy = FPT->getParamType(0);
2579 if (!SelfDeclTy.isNull())
2580 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002581 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002582 Elts.push_back(DBuilder.createArtificialType(
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002583 getOrCreateType(CGM.getContext().getObjCSelType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002585 for (const auto *PI : OMethod->params())
2586 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002587 // Variadic methods need a special marker at the end of the type list.
2588 if (OMethod->isVariadic())
2589 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002591 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 return DBuilder.createSubroutineType(F, EltTypeArray);
2593 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002594
Adrian Prantl800faef2014-02-25 23:42:18 +00002595 // Handle variadic function types; they need an additional
2596 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002597 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2598 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002599 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002600 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2601 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2602 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2603 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2604 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002605 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002606 return DBuilder.createSubroutineType(F, EltTypeArray);
2607 }
2608
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002609 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002610}
2611
Eric Christophere7b87e52014-10-26 23:40:33 +00002612void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2613 SourceLocation ScopeLoc, QualType FnType,
2614 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002615
2616 StringRef Name;
2617 StringRef LinkageName;
2618
2619 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2620
2621 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002622 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002623
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002625 llvm::DIFile *Unit = getOrCreateFile(Loc);
2626 llvm::DIScope *FDContext = Unit;
2627 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 if (!HasDecl) {
2629 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002630 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002632 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002633 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002635 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002636 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002637 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002638 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 return;
2640 }
2641 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002642 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2643 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2645 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002646 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002647 } else {
2648 // Use llvm function name.
2649 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002650 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002651 }
2652 if (!Name.empty() && Name[0] == '\01')
2653 Name = Name.substr(1);
2654
Adrian Prantl42d71b92014-04-10 23:21:53 +00002655 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002656 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002657 // Artificial functions without a location should not silently reuse CurLoc.
2658 if (Loc.isInvalid())
2659 CurLoc = SourceLocation();
2660 }
2661 unsigned LineNo = getLineNumber(Loc);
2662 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002663
Eric Christopher8018e412014-03-27 18:50:35 +00002664 // FIXME: The function declaration we're constructing here is mostly reusing
2665 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2666 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2667 // all subprograms instead of the actual context since subprogram definitions
2668 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002669 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002670 FDContext, Name, LinkageName, Unit, LineNo,
2671 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2672 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002673 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002674 // We might get here with a VarDecl in the case we're generating
2675 // code for the initialization of globals. Do not record these decls
2676 // as they will overwrite the actual VarDecl Decl in the cache.
2677 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002678 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002679
Adrian Prantlbebb8932014-03-21 21:01:58 +00002680 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002681 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002682
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002684 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002685}
2686
Adrian Prantl748a6cd2015-09-08 20:41:52 +00002687void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
2688 QualType FnType) {
2689 StringRef Name;
2690 StringRef LinkageName;
2691
2692 const Decl *D = GD.getDecl();
2693 if (!D)
2694 return;
2695
2696 unsigned Flags = 0;
2697 llvm::DIFile *Unit = getOrCreateFile(Loc);
2698 llvm::DIScope *FDContext = Unit;
2699 llvm::DINodeArray TParamsArray;
2700 if (isa<FunctionDecl>(D)) {
2701 // If there is a DISubprogram for this function available then use it.
2702 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2703 TParamsArray, Flags);
2704 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2705 Name = getObjCMethodName(OMD);
2706 Flags |= llvm::DINode::FlagPrototyped;
2707 } else {
2708 llvm_unreachable("not a function or ObjC method");
2709 }
2710 if (!Name.empty() && Name[0] == '\01')
2711 Name = Name.substr(1);
2712
2713 if (D->isImplicit()) {
2714 Flags |= llvm::DINode::FlagArtificial;
2715 // Artificial functions without a location should not silently reuse CurLoc.
2716 if (Loc.isInvalid())
2717 CurLoc = SourceLocation();
2718 }
2719 unsigned LineNo = getLineNumber(Loc);
2720 unsigned ScopeLine = 0;
2721
2722 DBuilder.createFunction(FDContext, Name, LinkageName, Unit, LineNo,
2723 getOrCreateFunctionType(D, FnType, Unit),
2724 false /*internalLinkage*/, true /*definition*/,
2725 ScopeLine, Flags, CGM.getLangOpts().Optimize, nullptr,
2726 TParamsArray.get(),
2727 getFunctionDeclaration(D));
2728}
2729
David Blaikie835afb22015-01-21 23:08:17 +00002730void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002731 // Update our current location
2732 setLocation(Loc);
2733
Eric Christophere7b87e52014-10-26 23:40:33 +00002734 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2735 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002736
Adrian Prantle83b1302014-01-07 22:05:52 +00002737 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002738 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002739 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002740}
2741
Guy Benyei11169dd2012-12-18 14:30:41 +00002742void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002743 llvm::MDNode *Back = nullptr;
2744 if (!LexicalBlockStack.empty())
2745 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002746 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002747 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002748 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002749}
2750
Eric Christopher0fdcb312013-05-16 00:52:20 +00002751void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2752 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002753 // Set our current location.
2754 setLocation(Loc);
2755
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002757 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2758 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002759
2760 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2761 return;
2762
2763 // Create a new lexical block and push it on the stack.
2764 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002765}
2766
Eric Christopher0fdcb312013-05-16 00:52:20 +00002767void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2768 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002769 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2770
2771 // Provide an entry in the line table for the end of the block.
2772 EmitLocation(Builder, Loc);
2773
David Blaikie60a877b2014-10-22 19:34:33 +00002774 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2775 return;
2776
Guy Benyei11169dd2012-12-18 14:30:41 +00002777 LexicalBlockStack.pop_back();
2778}
2779
Guy Benyei11169dd2012-12-18 14:30:41 +00002780void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2781 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2782 unsigned RCount = FnBeginRegionCount.back();
2783 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2784
2785 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002786 while (LexicalBlockStack.size() != RCount) {
2787 // Provide an entry in the line table for the end of the block.
2788 EmitLocation(Builder, CurLoc);
2789 LexicalBlockStack.pop_back();
2790 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002791 FnBeginRegionCount.pop_back();
2792}
2793
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002794llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002795 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002796
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002797 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002798 QualType FType;
2799 uint64_t FieldSize, FieldOffset;
2800 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002801
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002802 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002803 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002804
2805 FieldOffset = 0;
2806 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2807 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2808 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2809 FType = CGM.getContext().IntTy;
2810 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2811 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2812
2813 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2814 if (HasCopyAndDispose) {
2815 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002816 EltTys.push_back(
2817 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2818 EltTys.push_back(
2819 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 }
2821 bool HasByrefExtendedLayout;
2822 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002823 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2824 HasByrefExtendedLayout) &&
2825 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002826 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002827 EltTys.push_back(
2828 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002829 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002830
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2832 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002833 CGM.getTarget().getPointerAlign(0))) {
2834 CharUnits FieldOffsetInBytes =
2835 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2836 CharUnits AlignedOffsetInBytes =
2837 FieldOffsetInBytes.RoundUpToAlignment(Align);
2838 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002839
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 if (NumPaddingBytes.isPositive()) {
2841 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2842 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2843 pad, ArrayType::Normal, 0);
2844 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2845 }
2846 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002847
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002849 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 FieldSize = CGM.getContext().getTypeSize(FType);
2851 FieldAlign = CGM.getContext().toBits(Align);
2852
Eric Christopherb2a008c2013-05-16 00:45:12 +00002853 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002854 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2855 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 EltTys.push_back(FieldTy);
2857 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002858
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002859 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002860
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002861 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002862
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002864 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002865}
2866
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002867void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2868 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002869 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002870 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002871 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2872
David Blaikie7fceebf2013-08-19 03:37:48 +00002873 bool Unwritten =
2874 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2875 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002876 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002877 if (!Unwritten)
2878 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002879 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 uint64_t XOffset = 0;
2881 if (VD->hasAttr<BlocksAttr>())
2882 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002883 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 Ty = getOrCreateType(VD->getType(), Unit);
2885
2886 // If there is no debug info for this type then do not emit debug info
2887 // for this variable.
2888 if (!Ty)
2889 return;
2890
Guy Benyei11169dd2012-12-18 14:30:41 +00002891 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002892 unsigned Line = 0;
2893 unsigned Column = 0;
2894 if (!Unwritten) {
2895 Line = getLineNumber(VD->getLocation());
2896 Column = getColumnNumber(VD->getLocation());
2897 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002898 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002899 unsigned Flags = 0;
2900 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002901 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002902 // If this is the first argument and it is implicit then
2903 // give it an object pointer flag.
2904 // FIXME: There has to be a better way to do this, but for static
2905 // functions there won't be an implicit param at arg1 and
2906 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002907 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002908 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002909 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002910 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2911 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002912 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002913
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002914 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002915
2916 StringRef Name = VD->getName();
2917 if (!Name.empty()) {
2918 if (VD->hasAttr<BlocksAttr>()) {
2919 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002920 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 // offset of __forwarding field
2922 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002923 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002924 Expr.push_back(offset.getQuantity());
2925 Expr.push_back(llvm::dwarf::DW_OP_deref);
2926 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 // offset of x field
2928 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002929 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002930
2931 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002932 auto *D = ArgNo
2933 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2934 *ArgNo, Unit, Line, Ty)
2935 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2936 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002937
Guy Benyei11169dd2012-12-18 14:30:41 +00002938 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002939 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2940 llvm::DebugLoc::get(Line, Column, Scope),
2941 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002942 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002943 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002944 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002945 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2946 // If VD is an anonymous union then Storage represents value for
2947 // all union fields.
2948 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2949 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002950 // GDB has trouble finding local variables in anonymous unions, so we emit
2951 // artifical local variables for each of the members.
2952 //
2953 // FIXME: Remove this code as soon as GDB supports this.
2954 // The debug info verifier in LLVM operates based on the assumption that a
2955 // variable has the same size as its storage and we had to disable the check
2956 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002957 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002958 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002959 StringRef FieldName = Field->getName();
2960
2961 // Ignore unnamed fields. Do not ignore unnamed records.
2962 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2963 continue;
2964
2965 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002966 auto *D = DBuilder.createAutoVariable(
2967 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2968 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002969
2970 // Insert an llvm.dbg.declare into the current block.
2971 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2972 llvm::DebugLoc::get(Line, Column, Scope),
2973 Builder.GetInsertBlock());
2974 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002975 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002977
2978 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002979 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002980 ArgNo
2981 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2982 Ty, CGM.getLangOpts().Optimize,
2983 Flags)
2984 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2985 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002986
2987 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002988 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2989 llvm::DebugLoc::get(Line, Column, Scope),
2990 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002991}
2992
2993void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2994 llvm::Value *Storage,
2995 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002996 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002997 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002998}
2999
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003000llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
3001 llvm::DIType *Ty) {
3002 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00003003 if (CachedTy)
3004 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00003005 return DBuilder.createObjectPointerType(Ty);
3006}
3007
Eric Christophere7b87e52014-10-26 23:40:33 +00003008void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
3009 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00003010 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00003011 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00003013
Craig Topper8a13c412014-05-21 05:09:00 +00003014 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00003015 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00003016
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00003018
Guy Benyei11169dd2012-12-18 14:30:41 +00003019 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003020 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3021 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 if (isByRef)
3023 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003024 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 Ty = getOrCreateType(VD->getType(), Unit);
3026
3027 // Self is passed along as an implicit non-arg variable in a
3028 // block. Mark it as the object pointer.
3029 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003030 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003031
3032 // Get location information.
3033 unsigned Line = getLineNumber(VD->getLocation());
3034 unsigned Column = getColumnNumber(VD->getLocation());
3035
3036 const llvm::DataLayout &target = CGM.getDataLayout();
3037
3038 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003039 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3041
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003042 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003043 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003044 addr.push_back(llvm::dwarf::DW_OP_deref);
3045 addr.push_back(llvm::dwarf::DW_OP_plus);
3046 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003047 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003048 addr.push_back(llvm::dwarf::DW_OP_deref);
3049 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003050 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003051 offset =
3052 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003053 addr.push_back(offset.getQuantity());
3054 addr.push_back(llvm::dwarf::DW_OP_deref);
3055 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003056 // offset of x field
3057 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003058 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 }
3060
3061 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003062 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003063 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003064 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003065
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003067 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3068 if (InsertPoint)
3069 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3070 InsertPoint);
3071 else
3072 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3073 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003074}
3075
Guy Benyei11169dd2012-12-18 14:30:41 +00003076void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3077 unsigned ArgNo,
3078 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003079 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003080 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081}
3082
3083namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003084struct BlockLayoutChunk {
3085 uint64_t OffsetInBits;
3086 const BlockDecl::Capture *Capture;
3087};
3088bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3089 return l.OffsetInBits < r.OffsetInBits;
3090}
Guy Benyei11169dd2012-12-18 14:30:41 +00003091}
3092
3093void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003094 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003095 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003096 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003098 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003099 ASTContext &C = CGM.getContext();
3100 const BlockDecl *blockDecl = block.getBlockDecl();
3101
3102 // Collect some general information about the block's location.
3103 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003104 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003105 unsigned line = getLineNumber(loc);
3106 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003107
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 // Build the debug-info type for the block literal.
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003109 getDeclContextDescriptor(blockDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003110
3111 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003112 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003113
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003114 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3116 blockLayout->getElementOffsetInBits(0),
3117 tunit, tunit));
3118 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3119 blockLayout->getElementOffsetInBits(1),
3120 tunit, tunit));
3121 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3122 blockLayout->getElementOffsetInBits(2),
3123 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003124 auto *FnTy = block.getBlockExpr()->getFunctionType();
3125 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3126 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 blockLayout->getElementOffsetInBits(3),
3128 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003129 fields.push_back(createFieldType(
3130 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3131 ? C.getBlockDescriptorExtendedType()
3132 : C.getBlockDescriptorType()),
3133 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003134
3135 // We want to sort the captures by offset, not because DWARF
3136 // requires this, but because we're paranoid about debuggers.
3137 SmallVector<BlockLayoutChunk, 8> chunks;
3138
3139 // 'this' capture.
3140 if (blockDecl->capturesCXXThis()) {
3141 BlockLayoutChunk chunk;
3142 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003143 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003144 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003145 chunks.push_back(chunk);
3146 }
3147
3148 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003149 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 const VarDecl *variable = capture.getVariable();
3151 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3152
3153 // Ignore constant captures.
3154 if (captureInfo.isConstant())
3155 continue;
3156
3157 BlockLayoutChunk chunk;
3158 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003159 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003160 chunk.Capture = &capture;
3161 chunks.push_back(chunk);
3162 }
3163
3164 // Sort by offset.
3165 llvm::array_pod_sort(chunks.begin(), chunks.end());
3166
Eric Christophere7b87e52014-10-26 23:40:33 +00003167 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3168 e = chunks.end();
3169 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 uint64_t offsetInBits = i->OffsetInBits;
3171 const BlockDecl::Capture *capture = i->Capture;
3172
3173 // If we have a null capture, this must be the C++ 'this' capture.
3174 if (!capture) {
3175 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003176 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 QualType type = method->getThisType(C);
3178
3179 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3180 offsetInBits, tunit, tunit));
3181 continue;
3182 }
3183
3184 const VarDecl *variable = capture->getVariable();
3185 StringRef name = variable->getName();
3186
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003187 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003188 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003189 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003190
3191 // FIXME: this creates a second copy of this type!
3192 uint64_t xoffset;
3193 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003194 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3195 fieldType =
3196 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3197 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003198 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003199 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3200 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 }
3202 fields.push_back(fieldType);
3203 }
3204
3205 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003206 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3207 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003208
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003209 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003210
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003211 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003212 tunit, typeName.str(), tunit, line,
3213 CGM.getContext().toBits(block.BlockSize),
3214 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003215 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3216
3217 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003218 unsigned flags = llvm::DINode::FlagArtificial;
3219 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003220
3221 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003222 auto *debugVar = DBuilder.createParameterVariable(
3223 scope, Arg->getName(), ArgNo, tunit, line, type,
3224 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003225
Adrian Prantl616bef42013-03-14 21:52:59 +00003226 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003227 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003228 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003229 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003230 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003231 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003232
Adrian Prantl616bef42013-03-14 21:52:59 +00003233 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003234 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3235 llvm::DebugLoc::get(line, column, scope),
3236 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003237}
3238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003239llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003240CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3241 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003242 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003243
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003244 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003245 if (MI != StaticDataMemberCache.end()) {
3246 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003247 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003248 }
David Blaikiece763042013-08-20 21:49:21 +00003249
3250 // If the member wasn't found in the cache, lazily construct and add it to the
3251 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003252 auto DC = D->getDeclContext();
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003253 auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003254 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003255}
3256
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003257llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3258 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3259 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3260 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003261
3262 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003263 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003264 StringRef FieldName = Field->getName();
3265
3266 // Ignore unnamed fields, but recurse into anonymous records.
3267 if (FieldName.empty()) {
3268 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3269 if (RT)
3270 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3271 Var, DContext);
3272 continue;
3273 }
3274 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003275 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3276 LineNo, FieldTy,
3277 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003278 }
3279 return GV;
3280}
3281
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003282void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003283 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003284 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003285 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003286 llvm::DIFile *Unit = nullptr;
3287 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003288 unsigned LineNo;
3289 StringRef DeclName, LinkageName;
3290 QualType T;
3291 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003292
3293 // Attempt to store one global variable for the declaration - even if we
3294 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003295 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003296
3297 // If this is an anonymous union then we'll want to emit a global
3298 // variable for each member of the anonymous union so that it's possible
3299 // to find the name of any field in the union.
3300 if (T->isUnionType() && DeclName.empty()) {
3301 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003302 assert(RD->isAnonymousStructOrUnion() &&
3303 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003304 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3305 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003306 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003307 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3308 Var->hasInternalLinkage(), Var,
3309 getOrCreateStaticDataMemberDeclarationOrNull(D));
3310 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003311 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003312}
3313
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003314void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3315 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003316 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003317 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003318 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003319 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003320 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003321 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3322 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3323 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3324 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3325 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003326 // Do not use global variables for enums.
3327 //
3328 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003329 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003330 return;
David Blaikiea15565562014-04-04 20:56:17 +00003331 // Do not emit separate definitions for function local const/statics.
3332 if (isa<FunctionDecl>(VD->getDeclContext()))
3333 return;
David Blaikiebb113912014-04-05 07:23:17 +00003334 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003335 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003336 if (VarD->isStaticDataMember()) {
3337 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003338 getDeclContextDescriptor(VarD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003339 // Ensure that the type is retained even though it's otherwise unreferenced.
3340 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003341 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003342 return;
3343 }
3344
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003345 llvm::DIScope *DContext = getDeclContextDescriptor(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003346
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003347 auto &GV = DeclCache[VD];
3348 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003349 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003350 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003351 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003352 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003353}
3354
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003355llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003356 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003357 return LexicalBlockStack.back();
Adrian Prantl5c8bd882015-09-11 17:23:08 +00003358 llvm::DIScope *Mod = getParentModuleOrNull(D);
3359 return getContextDescriptor(D, Mod ? Mod : TheCU);
Guy Benyei11169dd2012-12-18 14:30:41 +00003360}
3361
David Blaikie9f88fe82013-04-22 06:13:21 +00003362void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003363 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3364 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003365 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003366 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3367 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003368 getLineNumber(UD.getLocation()));
3369}
3370
David Blaikiebd483762013-05-20 04:58:53 +00003371void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3372 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3373 return;
3374 assert(UD.shadow_size() &&
3375 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3376 // Emitting one decl is sufficient - debuggers can detect that this is an
3377 // overloaded name & provide lookup for all the overloads.
3378 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003379 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003380 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003381 DBuilder.createImportedDeclaration(
3382 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3383 getLineNumber(USD.getLocation()));
3384}
3385
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003386void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3387 auto *Reader = CGM.getContext().getExternalSource();
3388 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3389 DBuilder.createImportedDeclaration(
3390 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3391 getOrCreateModuleRef(Info),
3392 getLineNumber(ID.getLocation()));
3393}
3394
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003395llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003396CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3397 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003398 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003399 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003400 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003401 return cast<llvm::DIImportedEntity>(VH);
3402 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003403 if (const NamespaceAliasDecl *Underlying =
3404 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3405 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003406 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003407 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3408 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3409 NA.getName());
3410 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003411 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003412 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3413 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3414 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003415 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003416 return R;
3417}
3418
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003419llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003420CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003421 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003422 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003423 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003424 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003425
Guy Benyei11169dd2012-12-18 14:30:41 +00003426 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003427 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
Adrian Prantl6ec370a2015-09-10 18:39:45 +00003428 llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003429 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003430 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003431 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003432 return NS;
3433}
3434
3435void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003436 // Creating types might create further types - invalidating the current
3437 // element and the size(), so don't cache/reference them.
3438 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3439 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003440 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003441 ? CreateTypeDefinition(E.Type, E.Unit)
3442 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003443 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003444 }
3445
David Blaikief427b002014-05-06 03:42:01 +00003446 for (auto p : ReplaceMap) {
3447 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003448 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003449 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003450
David Blaikief427b002014-05-06 03:42:01 +00003451 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003452 assert(it != TypeCache.end());
3453 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003454
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003455 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3456 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003457 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003458
Frederic Rissd253ed62014-11-18 03:40:51 +00003459 for (const auto &p : FwdDeclReplaceMap) {
3460 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003461 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003462 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003463
3464 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003465 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003466 // with ourselves, that will destroy the temporary MDNode and
3467 // replace it with a standard one, avoiding leaking memory.
3468 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003469 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003470 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003471 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003472
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003473 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003474 }
3475
Adrian Prantl73409ce2013-03-11 18:33:46 +00003476 // We keep our own list of retained types, because we need to look
3477 // up the final type in the type cache.
Adrian Prantl3a884fa2015-08-27 22:56:46 +00003478 for (auto &RT : RetainedTypes)
Adrian Prantlad9a195e2015-08-27 21:21:19 +00003479 if (auto MD = TypeCache[RT])
3480 DBuilder.retainType(cast<llvm::DIType>(MD));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003481
Guy Benyei11169dd2012-12-18 14:30:41 +00003482 DBuilder.finalize();
3483}
David Blaikie66088d52014-09-24 17:01:27 +00003484
3485void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3486 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3487 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003488
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003489 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003490 // Don't ignore in case of explicit cast where it is referenced indirectly.
3491 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003492}