blob: e09e57a231e84c60fba7da971199e2ac3c5c35e9 [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/Dwarf.h"
41#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000042#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000043using namespace clang;
44using namespace clang::CodeGen;
45
46CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000047 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
48 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
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000150llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000151 if (!Context)
152 return TheCU;
153
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000154 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000155 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000156 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000157 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000158 }
159
160 // Check namespace.
161 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000162 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000163
David Blaikiebfa52742013-04-19 06:56:38 +0000164 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
165 if (!RDecl->isDependentType())
166 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000167 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 return TheCU;
169}
170
Guy Benyei11169dd2012-12-18 14:30:41 +0000171StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000172 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000173 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000174 FunctionTemplateSpecializationInfo *Info =
175 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000176 if (!Info && FII)
177 return FII->getName();
178
179 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000180 SmallString<128> NS;
181 llvm::raw_svector_ostream OS(NS);
182 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183
184 // Add any template specialization args.
185 if (Info) {
186 const TemplateArgumentList *TArgs = Info->TemplateArguments;
187 const TemplateArgument *Args = TArgs->data();
188 unsigned NumArgs = TArgs->size();
189 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000190 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
191 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000192 }
193
194 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000195 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000196}
197
198StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
199 SmallString<256> MethodName;
200 llvm::raw_svector_ostream OS(MethodName);
201 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
202 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000203 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000204 dyn_cast<const ObjCImplementationDecl>(DC)) {
205 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000206 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000207 dyn_cast<const ObjCInterfaceDecl>(DC)) {
208 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000209 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000210 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
211 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
212 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000213 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000214 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000215 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000216 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000217 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000218 ClassTy.print(OS, PrintingPolicy(LangOptions()));
219 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000220 }
221 OS << ' ' << OMD->getSelector().getAsString() << ']';
222
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000223 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000224}
225
Guy Benyei11169dd2012-12-18 14:30:41 +0000226StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000227 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000228}
229
Eric Christophere7b87e52014-10-26 23:40:33 +0000230StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000231 // quick optimization to avoid having to intern strings that are already
232 // stored reliably elsewhere
233 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000234 return RD->getName();
235
David Blaikie65813a32014-04-02 18:21:09 +0000236 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000237 {
David Blaikie65813a32014-04-02 18:21:09 +0000238 llvm::raw_svector_ostream OS(Name);
239 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
240 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000241 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000242
243 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000244 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000245}
246
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000247llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000248 if (!Loc.isValid())
249 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000250 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000251
252 SourceManager &SM = CGM.getContext().getSourceManager();
253 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
254
255 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
256 // If the 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 // Cache the results.
260 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000261 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000262
263 if (it != DIFileCache.end()) {
264 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000265 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000266 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000267 }
268
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000269 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000270 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000271
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000272 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000273 return F;
274}
275
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000276llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000277 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000278}
279
Guy Benyei11169dd2012-12-18 14:30:41 +0000280unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
281 if (Loc.isInvalid() && CurLoc.isInvalid())
282 return 0;
283 SourceManager &SM = CGM.getContext().getSourceManager();
284 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000285 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000286}
287
Adrian Prantlc7822422013-03-12 20:43:25 +0000288unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000289 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000290 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000291 return 0;
292
293 // If the location is invalid then use the current column.
294 if (Loc.isInvalid() && CurLoc.isInvalid())
295 return 0;
296 SourceManager &SM = CGM.getContext().getSourceManager();
297 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000298 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000299}
300
301StringRef CGDebugInfo::getCurrentDirname() {
302 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
303 return CGM.getCodeGenOpts().DebugCompilationDir;
304
305 if (!CWDName.empty())
306 return CWDName;
307 SmallString<256> CWD;
308 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000309 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000310}
311
Guy Benyei11169dd2012-12-18 14:30:41 +0000312void CGDebugInfo::CreateCompileUnit() {
313
David Blaikieaabde052014-05-14 00:29:00 +0000314 // Should we be asking the SourceManager for the main file name, instead of
315 // accepting it as an argument? This just causes the main file name to
316 // mismatch with source locations and create extra lexical scopes or
317 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
318 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
319 // because that's what the SourceManager says)
320
Guy Benyei11169dd2012-12-18 14:30:41 +0000321 // Get absolute path name.
322 SourceManager &SM = CGM.getContext().getSourceManager();
323 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
324 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000325 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000326
327 // The main file name provided via the "-main-file-name" option contains just
328 // the file name itself with no path information. This file name may have had
329 // a relative path, so we look into the actual file entry for the main
330 // file to determine the real absolute path for the file.
331 std::string MainFileDir;
332 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
333 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000334 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000335 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
336 llvm::sys::path::append(MainFileDirSS, MainFileName);
337 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000338 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000339 }
340
341 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000342 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000343
344 // Save split dwarf file string.
345 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000346 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000347
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(
373 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
374 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000375 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000376 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000377 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000378 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000379 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000380}
381
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000382llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000383 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 StringRef BTName;
385 switch (BT->getKind()) {
386#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000387#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000388#include "clang/AST/BuiltinTypes.def"
389 case BuiltinType::Dependent:
390 llvm_unreachable("Unexpected builtin type");
391 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000392 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000393 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000394 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000395 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000396 if (!ClassTy)
397 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
398 "objc_class", TheCU,
399 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 return ClassTy;
401 case BuiltinType::ObjCId: {
402 // typedef struct objc_class *Class;
403 // typedef struct objc_object {
404 // Class isa;
405 // } *id;
406
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000407 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return ObjTy;
409
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000410 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000411 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
412 "objc_class", TheCU,
413 getOrCreateMainFile(), 0);
414
415 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000416
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000417 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000418
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000419 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000420 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000421 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000422
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000423 DBuilder.replaceArrays(
424 ObjTy,
425 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
426 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000427 return ObjTy;
428 }
429 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000430 if (!SelTy)
431 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
432 "objc_selector", TheCU,
433 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000434 return SelTy;
435 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000436
437 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000438 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000439 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000440 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441 OCLImage1dArrayDITy);
442 case BuiltinType::OCLImage1dBuffer:
443 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
444 OCLImage1dBufferDITy);
445 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 case BuiltinType::OCLImage2dArray:
448 return getOrCreateStructPtrType("opencl_image2d_array_t",
449 OCLImage2dArrayDITy);
450 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000452 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000453 return DBuilder.createBasicType(
454 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
455 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000456 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000457 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000458
Guy Benyei11169dd2012-12-18 14:30:41 +0000459 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000460 case BuiltinType::Char_U:
461 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
462 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000464 case BuiltinType::SChar:
465 Encoding = llvm::dwarf::DW_ATE_signed_char;
466 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::Char32:
469 Encoding = llvm::dwarf::DW_ATE_UTF;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::UShort:
472 case BuiltinType::UInt:
473 case BuiltinType::UInt128:
474 case BuiltinType::ULong:
475 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::ULongLong:
477 Encoding = llvm::dwarf::DW_ATE_unsigned;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 case BuiltinType::Short:
480 case BuiltinType::Int:
481 case BuiltinType::Int128:
482 case BuiltinType::Long:
483 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::LongLong:
485 Encoding = llvm::dwarf::DW_ATE_signed;
486 break;
487 case BuiltinType::Bool:
488 Encoding = llvm::dwarf::DW_ATE_boolean;
489 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000490 case BuiltinType::Half:
491 case BuiltinType::Float:
492 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000493 case BuiltinType::Double:
494 Encoding = llvm::dwarf::DW_ATE_float;
495 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000496 }
497
498 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000499 case BuiltinType::Long:
500 BTName = "long int";
501 break;
502 case BuiltinType::LongLong:
503 BTName = "long long int";
504 break;
505 case BuiltinType::ULong:
506 BTName = "long unsigned int";
507 break;
508 case BuiltinType::ULongLong:
509 BTName = "long long unsigned int";
510 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000511 default:
512 BTName = BT->getName(CGM.getLangOpts());
513 break;
514 }
515 // Bit size, align and offset of the type.
516 uint64_t Size = CGM.getContext().getTypeSize(BT);
517 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000518 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000519}
520
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000521llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000522 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000523 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000524 if (Ty->isComplexIntegerType())
525 Encoding = llvm::dwarf::DW_ATE_lo_user;
526
527 uint64_t Size = CGM.getContext().getTypeSize(Ty);
528 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000529 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000530}
531
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000532llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
533 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000534 QualifierCollector Qc;
535 const Type *T = Qc.strip(Ty);
536
537 // Ignore these qualifiers for now.
538 Qc.removeObjCGCAttr();
539 Qc.removeAddressSpace();
540 Qc.removeObjCLifetime();
541
542 // We will create one Derived type for one qualifier and recurse to handle any
543 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000544 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 if (Qc.hasConst()) {
546 Tag = llvm::dwarf::DW_TAG_const_type;
547 Qc.removeConst();
548 } else if (Qc.hasVolatile()) {
549 Tag = llvm::dwarf::DW_TAG_volatile_type;
550 Qc.removeVolatile();
551 } else if (Qc.hasRestrict()) {
552 Tag = llvm::dwarf::DW_TAG_restrict_type;
553 Qc.removeRestrict();
554 } else {
555 assert(Qc.empty() && "Unknown type qualifier for debug info");
556 return getOrCreateType(QualType(T, 0), Unit);
557 }
558
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000559 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000560
561 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
562 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000563 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000564}
565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000566llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
567 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000568
569 // The frontend treats 'id' as a typedef to an ObjCObjectType,
570 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
571 // debug info, we want to emit 'id' in both cases.
572 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000573 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000574
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000575 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
576 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000577}
578
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000579llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
580 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000581 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000582 Ty->getPointeeType(), Unit);
583}
584
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000585/// \return whether a C++ mangling exists for the type defined by TD.
586static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
587 switch (TheCU->getSourceLanguage()) {
588 case llvm::dwarf::DW_LANG_C_plus_plus:
589 return true;
590 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
591 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
592 default:
593 return false;
594 }
595}
596
Manman Rene0064d82013-08-29 23:19:58 +0000597/// In C++ mode, types have linkage, so we can rely on the ODR and
598/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000599static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
600 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000601 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000602 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000603 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000604
605 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000606 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000607
Manman Rene0064d82013-08-29 23:19:58 +0000608 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
609 if (CGM.getTarget().getCXXABI().isMicrosoft())
610 return FullName;
611
612 // TODO: This is using the RTTI name. Is there a better way to get
613 // a unique string for a type?
614 llvm::raw_svector_ostream Out(FullName);
615 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
Manman Rene0064d82013-08-29 23:19:58 +0000616 return FullName;
617}
618
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000619/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000620static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
621 llvm::dwarf::Tag Tag;
622 if (RD->isStruct() || RD->isInterface())
623 Tag = llvm::dwarf::DW_TAG_structure_type;
624 else if (RD->isUnion())
625 Tag = llvm::dwarf::DW_TAG_union_type;
626 else {
627 // FIXME: This could be a struct type giving a default visibility different
628 // than C++ class type, but needs llvm metadata changes first.
629 assert(RD->isClass());
630 Tag = llvm::dwarf::DW_TAG_class_type;
631 }
632 return Tag;
633}
634
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000635llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000636CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000637 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000638 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000639 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
640 return cast<llvm::DICompositeType>(T);
641 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000642 unsigned Line = getLineNumber(RD->getLocation());
643 StringRef RDName = getClassName(RD);
644
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000645 uint64_t Size = 0;
646 uint64_t Align = 0;
647
648 const RecordDecl *D = RD->getDefinition();
649 if (D && D->isCompleteDefinition()) {
650 Size = CGM.getContext().getTypeSize(Ty);
651 Align = CGM.getContext().getTypeAlign(Ty);
652 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000653
654 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000655 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000656 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000657 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000658 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000659 ReplaceMap.emplace_back(
660 std::piecewise_construct, std::make_tuple(Ty),
661 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000662 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000663}
664
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000665llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000666 const Type *Ty,
667 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000668 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000669 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
670 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000671 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000672
Guy Benyei11169dd2012-12-18 14:30:41 +0000673 // Bit size, align and offset of the type.
674 // Size is always the size of a pointer. We can't use getTypeSize here
675 // because that does not return the correct value for references.
676 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000677 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
679
David Blaikie99dab3b2013-09-04 22:03:57 +0000680 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
681 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000682}
683
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000684llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
685 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000686 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000687 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000688 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
689 TheCU, getOrCreateMainFile(), 0);
690 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
691 Cache = DBuilder.createPointerType(Cache, Size);
692 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000693}
694
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
696 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000697 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000698 QualType FType;
699 uint64_t FieldSize, FieldOffset;
700 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000701 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702
703 FieldOffset = 0;
704 FType = CGM.getContext().UnsignedLongTy;
705 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
706 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
707
708 Elements = DBuilder.getOrCreateArray(EltTys);
709 EltTys.clear();
710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000711 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000712 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000713
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000714 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000715 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000716 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
718 // Bit size, align and offset of the type.
719 uint64_t Size = CGM.getContext().getTypeSize(Ty);
720
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000721 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722
723 FieldOffset = 0;
724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
725 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
726 FType = CGM.getContext().IntTy;
727 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
728 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000729 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
731
732 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000733 FieldSize = CGM.getContext().getTypeSize(Ty);
734 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000735 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000736 FieldSize, FieldAlign, FieldOffset,
737 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000738
739 FieldOffset += FieldSize;
740 Elements = DBuilder.getOrCreateArray(EltTys);
741
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000742 // The __block_literal_generic structs are marked with a special
743 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
744 // the debugger needs to know about. To allow type uniquing, emit
745 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000746 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000747 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000748 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000749
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000750 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000751}
752
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000753llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
754 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000755 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000756 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000757
758 SmallString<128> NS;
759 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000760 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
761 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000762
763 TemplateSpecializationType::PrintTemplateArgumentList(
764 OS, Ty->getArgs(), Ty->getNumArgs(),
765 CGM.getContext().getPrintingPolicy());
766
Eric Christophere7b87e52014-10-26 23:40:33 +0000767 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
768 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000769
770 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000771 return DBuilder.createTypedef(
772 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
773 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000774}
775
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000776llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
777 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000778 // We don't set size information, but do specify where the typedef was
779 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000780 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000781
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000782 // Typedefs are derived from some other type.
783 return DBuilder.createTypedef(
784 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
785 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
David Blaikiebe822ed2015-07-01 18:07:22 +0000786 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000787}
788
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000789llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
790 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000791 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000792
793 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000794 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000795
796 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000797 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000798 if (isa<FunctionNoProtoType>(Ty))
799 EltTys.push_back(DBuilder.createUnspecifiedParameter());
800 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000801 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
802 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000803 if (FPT->isVariadic())
804 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 }
806
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000807 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000808 return DBuilder.createSubroutineType(Unit, EltTypeArray);
809}
810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000811/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000812/// As an optimization, return 0 if the access specifier equals the
813/// default for the containing type.
814static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
815 AccessSpecifier Default = clang::AS_none;
816 if (RD && RD->isClass())
817 Default = clang::AS_private;
818 else if (RD && (RD->isStruct() || RD->isUnion()))
819 Default = clang::AS_public;
820
821 if (Access == Default)
822 return 0;
823
Eric Christophere7b87e52014-10-26 23:40:33 +0000824 switch (Access) {
825 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000826 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000827 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000828 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000829 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000830 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000831 case clang::AS_none:
832 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000833 }
834 llvm_unreachable("unexpected access enumerator");
835}
Guy Benyei11169dd2012-12-18 14:30:41 +0000836
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000837llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000838 StringRef name, QualType type, uint64_t sizeInBitsOverride,
839 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000840 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
841 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000842
843 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000844 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000845 unsigned line = getLineNumber(loc);
846
David Majnemer34b57492014-07-30 01:30:47 +0000847 uint64_t SizeInBits = 0;
848 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000850 TypeInfo TI = CGM.getContext().getTypeInfo(type);
851 SizeInBits = TI.Width;
852 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000853
854 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000855 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 }
857
Adrian Prantl21361fb2014-08-29 22:44:27 +0000858 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000859 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
860 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000861}
862
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000863void CGDebugInfo::CollectRecordLambdaFields(
864 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000865 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000866 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
867 // has the name and the location of the variable so we should iterate over
868 // both concurrently.
869 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
870 RecordDecl::field_iterator Field = CXXDecl->field_begin();
871 unsigned fieldno = 0;
872 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000873 E = CXXDecl->captures_end();
874 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000875 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000876 if (C.capturesVariable()) {
877 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000878 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000879 StringRef VName = V->getName();
880 uint64_t SizeInBitsOverride = 0;
881 if (Field->isBitField()) {
882 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
883 assert(SizeInBitsOverride && "found named 0-width bitfield");
884 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000885 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000886 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
887 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
888 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000889 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000890 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000891 // TODO: Need to handle 'this' in some way by probably renaming the
892 // this of the lambda class and having a field member of 'this' or
893 // by using AT_object_pointer for the function and having that be
894 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000895 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000896 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000897 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000898 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000899 "this", type, 0, f->getLocation(), f->getAccess(),
900 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000901
902 elements.push_back(fieldType);
903 }
904 }
905}
906
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000907llvm::DIDerivedType *
908CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000909 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000910 // Create the descriptor for the static variable, with or without
911 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000912 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000913 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
914 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000915
Eric Christopher91a31902013-01-16 01:22:32 +0000916 unsigned LineNumber = getLineNumber(Var->getLocation());
917 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000918 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000919 if (Var->getInit()) {
920 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000921 if (Value) {
922 if (Value->isInt())
923 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
924 if (Value->isFloat())
925 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
926 }
Eric Christopher91a31902013-01-16 01:22:32 +0000927 }
928
Adrian Prantl21361fb2014-08-29 22:44:27 +0000929 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000930 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000931 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000932 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000933 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000934}
935
Eric Christophere7b87e52014-10-26 23:40:33 +0000936void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000937 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
938 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000939 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000940 StringRef name = field->getName();
941 QualType type = field->getType();
942
943 // Ignore unnamed fields unless they're anonymous structs/unions.
944 if (name.empty() && !type->isRecordType())
945 return;
946
947 uint64_t SizeInBitsOverride = 0;
948 if (field->isBitField()) {
949 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
950 assert(SizeInBitsOverride && "found named 0-width bitfield");
951 }
952
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000953 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000954 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
955 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000956
957 elements.push_back(fieldType);
958}
959
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000960void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000961 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000962 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000963 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000964 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
965
Eric Christopher91a31902013-01-16 01:22:32 +0000966 if (CXXDecl && CXXDecl->isLambda())
967 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
968 else {
969 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000970
Eric Christopher91a31902013-01-16 01:22:32 +0000971 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000972 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000973
Eric Christopher91a31902013-01-16 01:22:32 +0000974 // Static and non-static members should appear in the same order as
975 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000976 for (const auto *I : record->decls())
977 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000978 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000979 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000980 if (MI != StaticDataMemberCache.end()) {
981 assert(MI->second &&
982 "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +0000983 elements.push_back(MI->second);
Adrian Prantl21361fb2014-08-29 22:44:27 +0000984 } else {
985 auto Field = CreateRecordStaticField(V, RecordTy, record);
986 elements.push_back(Field);
987 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000988 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000989 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
990 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000991
992 // Bump field number for next field.
993 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000994 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000995 }
996}
997
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000998llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +0000999CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001000 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001001 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001002 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001003 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001004 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001005 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1006 Func, Unit);
1007}
David Blaikie2aaf0652013-01-07 22:24:59 +00001008
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001009llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1010 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001012 llvm::DITypeRefArray Args(
1013 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001014 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001015 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001016
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001017 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001018
1019 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001020 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
David Blaikie2aaf0652013-01-07 22:24:59 +00001022 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001023 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001024 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1025 // Create pointer type directly in this case.
1026 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1027 QualType PointeeTy = ThisPtrTy->getPointeeType();
1028 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001029 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001030 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1032 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001033 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001034 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 // TODO: This and the artificial type below are misleading, the
1036 // types aren't artificial the argument is, but the current
1037 // metadata doesn't represent that.
1038 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1039 Elts.push_back(ThisPtrType);
1040 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001041 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001042 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001043 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1044 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001045 }
1046
1047 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001048 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1049 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001051 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001052
Adrian Prantl0630eb72013-12-18 21:48:18 +00001053 unsigned Flags = 0;
1054 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001055 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001056 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001057 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001058
1059 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001060}
1061
Eric Christopherb2a008c2013-05-16 00:45:12 +00001062/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001063/// inside a function.
1064static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1065 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1066 return isFunctionLocalClass(NRD);
1067 if (isa<FunctionDecl>(RD->getDeclContext()))
1068 return true;
1069 return false;
1070}
1071
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001072llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1073 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001074 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001075 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001076
Guy Benyei11169dd2012-12-18 14:30:41 +00001077 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001078 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
1080 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1081 // make sense to give a single ctor/dtor a linkage name.
1082 StringRef MethodLinkageName;
1083 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1084 MethodLinkageName = CGM.getMangledName(Method);
1085
1086 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001087 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001088 unsigned MethodLine = 0;
1089 if (!Method->isImplicit()) {
1090 MethodDefUnit = getOrCreateFile(Method->getLocation());
1091 MethodLine = getLineNumber(Method->getLocation());
1092 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001093
1094 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001095 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001096 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001098
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 if (Method->isVirtual()) {
1100 if (Method->isPure())
1101 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1102 else
1103 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001104
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 // It doesn't make sense to give a virtual destructor a vtable index,
1106 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001107 // FIXME: Add proper support for debug info for virtual calls in
1108 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1109 // lookup if we have multiple or virtual inheritance.
1110 if (!isa<CXXDestructorDecl>(Method) &&
1111 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001112 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 ContainingType = RecordTy;
1114 }
1115
1116 unsigned Flags = 0;
1117 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001118 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001119 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1121 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001122 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001124 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001126 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 }
1128 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001129 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001130 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001131 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001132 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001133 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001134
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001135 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1136 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001137 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1138 MethodTy, /*isLocalToUnit=*/false,
1139 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001140 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001141
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001142 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001143
1144 return SP;
1145}
1146
Eric Christophere7b87e52014-10-26 23:40:33 +00001147void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001148 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1149 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001150
1151 // Since we want more than just the individual member decls if we
1152 // have templated functions iterate over every declaration to gather
1153 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001154 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001155 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1156 // If the member is implicit, don't add it to the member list. This avoids
1157 // the member being added to type units by LLVM, while still allowing it
1158 // to be emitted into the type declaration/reference inside the compile
1159 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001160 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001161 // FIXME: Handle Using(Shadow?)Decls here to create
1162 // DW_TAG_imported_declarations inside the class for base decls brought into
1163 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1164 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1165 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001166 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001167 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001168
1169 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1170 continue;
1171
David Blaikiefd580722014-10-06 05:18:55 +00001172 // Reuse the existing member function declaration if it exists.
1173 // It may be associated with the declaration of the type & should be
1174 // reused as we're building the definition.
1175 //
1176 // This situation can arise in the vtable-based debug info reduction where
1177 // implicit members are emitted in a non-vtable TU.
1178 auto MI = SPCache.find(Method->getCanonicalDecl());
1179 EltTys.push_back(MI == SPCache.end()
1180 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001181 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001183}
Guy Benyei11169dd2012-12-18 14:30:41 +00001184
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001185void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001186 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001187 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001188 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001189 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001190 unsigned BFlags = 0;
1191 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001192
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001194 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001195
Aaron Ballman574705e2014-03-13 15:41:46 +00001196 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001197 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1198 // virtual base offset offset is -ve. The code generator emits dwarf
1199 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001200 BaseOffset = 0 - CGM.getItaniumVTableContext()
1201 .getVirtualBaseOffsetOffset(RD, Base)
1202 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001203 } else {
1204 // In the MS ABI, store the vbtable offset, which is analogous to the
1205 // vbase offset offset in Itanium.
1206 BaseOffset =
1207 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1208 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001209 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001210 } else
1211 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1212 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1213 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001214
Adrian Prantl21361fb2014-08-29 22:44:27 +00001215 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001216 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001217 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 EltTys.push_back(DTy);
1219 }
1220}
1221
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001222llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001223CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1224 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001225 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001226 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1228 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001229 StringRef Name;
1230 if (TPList)
1231 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001232 switch (TA.getKind()) {
1233 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001235 TemplateParams.push_back(
1236 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001237 } break;
1238 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001240 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1241 TheCU, Name, TTy,
1242 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001243 } break;
1244 case TemplateArgument::Declaration: {
1245 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001246 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001247 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001248 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001249 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001250 // Variable pointer template parameters have a value that is the address
1251 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001252 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001253 V = CGM.GetAddrOfGlobalVar(VD);
1254 // Member function pointers have special support for building them, though
1255 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001256 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001257 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001258 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001259 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001260 // Member data pointers have special handling too to compute the fixed
1261 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001262 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001263 // These five lines (& possibly the above member function pointer
1264 // handling) might be able to be refactored to use similar code in
1265 // CodeGenModule::getMemberPointerConstant
1266 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1267 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001268 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001269 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001270 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001271 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1272 TheCU, Name, TTy,
1273 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001274 } break;
1275 case TemplateArgument::NullPtr: {
1276 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001277 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001278 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001279 // Special case member data pointer null values since they're actually -1
1280 // instead of zero.
1281 if (const MemberPointerType *MPT =
1282 dyn_cast<MemberPointerType>(T.getTypePtr()))
1283 // But treat member function pointers as simple zero integers because
1284 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1285 // CodeGen grows handling for values of non-null member function
1286 // pointers then perhaps we could remove this special case and rely on
1287 // EmitNullMemberPointer for member function pointers.
1288 if (MPT->isMemberDataPointer())
1289 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1290 if (!V)
1291 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001292 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1293 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001294 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001295 case TemplateArgument::Template:
1296 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001297 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001298 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1299 break;
1300 case TemplateArgument::Pack:
1301 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1302 TheCU, Name, nullptr,
1303 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1304 break;
David Majnemer5559d472013-08-24 08:21:10 +00001305 case TemplateArgument::Expression: {
1306 const Expr *E = TA.getAsExpr();
1307 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001308 if (E->isGLValue())
1309 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001310 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001311 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001312 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001313 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1314 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001315 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001316 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001317 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001318 case TemplateArgument::Null:
1319 llvm_unreachable(
1320 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001321 }
1322 }
1323 return DBuilder.getOrCreateArray(TemplateParams);
1324}
1325
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001326llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001327CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001328 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001329 if (FD->getTemplatedKind() ==
1330 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001331 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1332 ->getTemplate()
1333 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001334 return CollectTemplateParams(
1335 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001336 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001337 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001338}
1339
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001340llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1341 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001342 // Always get the full list of parameters, not just the ones from
1343 // the specialization.
1344 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001345 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001346 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001347 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001348}
1349
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001350llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001351 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 return VTablePtrType;
1353
1354 ASTContext &Context = CGM.getContext();
1355
1356 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001357 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001358 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1359 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001362 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1364 return VTablePtrType;
1365}
1366
Guy Benyei11169dd2012-12-18 14:30:41 +00001367StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001368 // Copy the gdb compatible name on the side and use its reference.
1369 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001370}
1371
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001372void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001373 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001374 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1375
1376 // If there is a primary base then it will hold vtable info.
1377 if (RL.getPrimaryBase())
1378 return;
1379
1380 // If this class is not dynamic then there is not any vtable info to collect.
1381 if (!RD->isDynamicClass())
1382 return;
1383
1384 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001385 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001386 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001387 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 EltTys.push_back(VPTR);
1389}
1390
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001391llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001392 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001393 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001394 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 return T;
1396}
1397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001398llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001399 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001400 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001401 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001402 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 return T;
1404}
1405
David Blaikie483a9da2014-05-06 18:35:21 +00001406void CGDebugInfo::completeType(const EnumDecl *ED) {
1407 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1408 return;
1409 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001410 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001411 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001412 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001413 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001414 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001415 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001416 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001417}
1418
David Blaikieb2e86eb2013-08-15 20:49:17 +00001419void CGDebugInfo::completeType(const RecordDecl *RD) {
1420 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1421 !CGM.getLangOpts().CPlusPlus)
1422 completeRequiredType(RD);
1423}
1424
1425void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001426 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1427 return;
1428
David Blaikie6943dea2013-08-20 01:28:15 +00001429 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1430 if (CXXDecl->isDynamicClass())
1431 return;
1432
David Blaikieb2e86eb2013-08-15 20:49:17 +00001433 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001434 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001435 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001436 completeClassData(RD);
1437}
1438
1439void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1440 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001441 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001442 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001443 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001444 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001445 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001446 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001447 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001448 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001449 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001450}
1451
David Blaikie0e716b42014-03-03 23:48:23 +00001452static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1453 CXXRecordDecl::method_iterator End) {
1454 for (; I != End; ++I)
1455 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001456 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1457 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001458 return true;
1459 return false;
1460}
1461
1462static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1463 const RecordDecl *RD,
1464 const LangOptions &LangOpts) {
1465 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1466 return false;
1467
1468 if (!LangOpts.CPlusPlus)
1469 return false;
1470
1471 if (!RD->isCompleteDefinitionRequired())
1472 return true;
1473
1474 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1475
1476 if (!CXXDecl)
1477 return false;
1478
1479 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1480 return true;
1481
1482 TemplateSpecializationKind Spec = TSK_Undeclared;
1483 if (const ClassTemplateSpecializationDecl *SD =
1484 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1485 Spec = SD->getSpecializationKind();
1486
1487 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1488 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1489 CXXDecl->method_end()))
1490 return true;
1491
1492 return false;
1493}
1494
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001495llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001496 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001497 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001498 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001499 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001500 T = getOrCreateRecordFwdDecl(
1501 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001502 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001503 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001504
David Blaikieb2e86eb2013-08-15 20:49:17 +00001505 return CreateTypeDefinition(Ty);
1506}
1507
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001508llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001509 RecordDecl *RD = Ty->getDecl();
1510
Guy Benyei11169dd2012-12-18 14:30:41 +00001511 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001512 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001513
1514 // Records and classes and unions can all be recursive. To handle them, we
1515 // first generate a debug descriptor for the struct as a forward declaration.
1516 // Then (if it is a definition) we go through and get debug info for all of
1517 // its members. Finally, we create a descriptor for the complete type (which
1518 // may refer to the forward decl if the struct is recursive) and replace all
1519 // uses of the forward declaration with the final definition.
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00001520 llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001521
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001522 const RecordDecl *D = RD->getDefinition();
1523 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001524 return FwdDecl;
1525
David Blaikieadfbf992013-08-18 16:55:33 +00001526 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1527 CollectContainingType(CXXDecl, FwdDecl);
1528
Guy Benyei11169dd2012-12-18 14:30:41 +00001529 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001530 LexicalBlockStack.emplace_back(&*FwdDecl);
1531 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001532
Guy Benyei11169dd2012-12-18 14:30:41 +00001533 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001534 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001535 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001536
1537 // Note: The split of CXXDecl information here is intentional, the
1538 // gdb tests will depend on a certain ordering at printout. The debug
1539 // information offsets are still correct if we merge them all together
1540 // though.
1541 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1542 if (CXXDecl) {
1543 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1544 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1545 }
1546
Eric Christopher91a31902013-01-16 01:22:32 +00001547 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001549 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001551
1552 LexicalBlockStack.pop_back();
1553 RegionMap.erase(Ty->getDecl());
1554
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001555 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001556 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001557
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001558 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001559 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001560 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001561
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001562 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001563 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001564}
1565
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001566llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1567 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001568 // Ignore protocols.
1569 return getOrCreateType(Ty->getBaseType(), Unit);
1570}
1571
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001572/// \return true if Getter has the default name for the property PD.
1573static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1574 const ObjCMethodDecl *Getter) {
1575 assert(PD);
1576 if (!Getter)
1577 return true;
1578
1579 assert(Getter->getDeclName().isObjCZeroArgSelector());
1580 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001581 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001582}
1583
1584/// \return true if Setter has the default name for the property PD.
1585static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1586 const ObjCMethodDecl *Setter) {
1587 assert(PD);
1588 if (!Setter)
1589 return true;
1590
1591 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001592 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001593 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001594}
1595
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001596llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1597 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 ObjCInterfaceDecl *ID = Ty->getDecl();
1599 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001600 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001601
1602 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001603 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001604 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001605 auto RuntimeLang =
1606 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001607
1608 // If this is just a forward declaration return a special forward-declaration
1609 // debug type since we won't be able to lay out the entire type.
1610 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001611 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001612 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001613 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1614 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001615 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001616 return FwdDecl;
1617 }
1618
David Blaikieef8a9512014-05-05 23:23:53 +00001619 return CreateTypeDefinition(Ty, Unit);
1620}
1621
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001622llvm::DIModule *
1623CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001624 auto it = ModuleRefCache.find(Mod.Signature);
1625 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001626 return it->second;
1627
1628 // Macro definitions that were defined with "-D" on the command line.
1629 SmallString<128> ConfigMacros;
1630 {
1631 llvm::raw_svector_ostream OS(ConfigMacros);
1632 const auto &PPOpts = CGM.getPreprocessorOpts();
1633 unsigned I = 0;
1634 // Translate the macro definitions back into a commmand line.
1635 for (auto &M : PPOpts.Macros) {
1636 if (++I > 1)
1637 OS << " ";
1638 const std::string &Macro = M.first;
1639 bool Undef = M.second;
1640 OS << "\"-" << (Undef ? 'U' : 'D');
1641 for (char c : Macro)
1642 switch (c) {
1643 case '\\' : OS << "\\\\"; break;
1644 case '"' : OS << "\\\""; break;
1645 default: OS << c;
1646 }
1647 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001648 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001649 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001650 llvm::DIBuilder DIB(CGM.getModule());
1651 auto *CU = DIB.createCompileUnit(
1652 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1653 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1654 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1655 llvm::DIModule *ModuleRef =
1656 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1657 internString(CGM.getHeaderSearchOpts().Sysroot));
1658 DIB.finalize();
1659 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001660 return ModuleRef;
1661}
1662
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001663llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1664 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001665 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001666 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001667 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001668 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001669
1670 // Bit size, align and offset of the type.
1671 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1672 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1673
1674 unsigned Flags = 0;
1675 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001676 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001677
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001678 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001679 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001680 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001681
David Blaikieef8a9512014-05-05 23:23:53 +00001682 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001683 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001685 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001686 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001687 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001688
1689 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001690 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001691
1692 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1693 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001694 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001695 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001696 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001697 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001698
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001699 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 EltTys.push_back(InhTag);
1701 }
1702
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001703 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001704 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001706 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001707 unsigned PLine = getLineNumber(Loc);
1708 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1709 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001710 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1711 PD->getName(), PUnit, PLine,
1712 hasDefaultGetterName(PD, Getter) ? ""
1713 : getSelectorName(PD->getGetterName()),
1714 hasDefaultSetterName(PD, Setter) ? ""
1715 : getSelectorName(PD->getSetterName()),
1716 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 EltTys.push_back(PropertyNode);
1718 }
1719
1720 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1721 unsigned FieldNo = 0;
1722 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1723 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001724 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001725 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001726 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001727
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 StringRef FieldName = Field->getName();
1729
1730 // Ignore unnamed fields.
1731 if (FieldName.empty())
1732 continue;
1733
1734 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001735 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 unsigned FieldLine = getLineNumber(Field->getLocation());
1737 QualType FType = Field->getType();
1738 uint64_t FieldSize = 0;
1739 unsigned FieldAlign = 0;
1740
1741 if (!FType->isIncompleteArrayType()) {
1742
1743 // Bit size, align and offset of the type.
1744 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001745 ? Field->getBitWidthValue(CGM.getContext())
1746 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001747 FieldAlign = CGM.getContext().getTypeAlign(FType);
1748 }
1749
1750 uint64_t FieldOffset;
1751 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1752 // We don't know the runtime offset of an ivar if we're using the
1753 // non-fragile ABI. For bitfields, use the bit offset into the first
1754 // byte of storage of the bitfield. For other fields, use zero.
1755 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001756 FieldOffset =
1757 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001758 FieldOffset %= CGM.getContext().getCharWidth();
1759 } else {
1760 FieldOffset = 0;
1761 }
1762 } else {
1763 FieldOffset = RL.getFieldOffset(FieldNo);
1764 }
1765
1766 unsigned Flags = 0;
1767 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001768 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001770 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001771 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001772 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001773
Craig Topper8a13c412014-05-21 05:09:00 +00001774 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001775 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001776 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001777 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001778 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001779 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001780 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001781 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1783 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001784 PropertyNode = DBuilder.createObjCProperty(
1785 PD->getName(), PUnit, PLine,
1786 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1787 PD->getGetterName()),
1788 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1789 PD->getSetterName()),
1790 PD->getPropertyAttributes(),
1791 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 }
1793 }
1794 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001795 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1796 FieldSize, FieldAlign, FieldOffset, Flags,
1797 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 EltTys.push_back(FieldTy);
1799 }
1800
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001801 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001802 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001803
Guy Benyei11169dd2012-12-18 14:30:41 +00001804 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001805 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001806}
1807
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001808llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1809 llvm::DIFile *Unit) {
1810 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001811 int64_t Count = Ty->getNumElements();
1812 if (Count == 0)
1813 // If number of elements are not known then this is an unbounded array.
1814 // Use Count == -1 to express such arrays.
1815 Count = -1;
1816
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001817 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001818 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001819
1820 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1821 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1822
1823 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1824}
1825
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001826llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001827 uint64_t Size;
1828 uint64_t Align;
1829
1830 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1831 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1832 Size = 0;
1833 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001834 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 } else if (Ty->isIncompleteArrayType()) {
1836 Size = 0;
1837 if (Ty->getElementType()->isIncompleteType())
1838 Align = 0;
1839 else
1840 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001841 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 Size = 0;
1843 Align = 0;
1844 } else {
1845 // Size and align of the whole array, not the element type.
1846 Size = CGM.getContext().getTypeSize(Ty);
1847 Align = CGM.getContext().getTypeAlign(Ty);
1848 }
1849
1850 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1851 // interior arrays, do we care? Why aren't nested arrays represented the
1852 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001853 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001854 QualType EltTy(Ty, 0);
1855 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1856 // If the number of elements is known, then count is that number. Otherwise,
1857 // it's -1. This allows us to represent a subrange with an array of 0
1858 // elements, like this:
1859 //
1860 // struct foo {
1861 // int x[0];
1862 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001863 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001864 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1865 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001866
Guy Benyei11169dd2012-12-18 14:30:41 +00001867 // FIXME: Verify this is right for VLAs.
1868 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1869 EltTy = Ty->getElementType();
1870 }
1871
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001872 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001873
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001874 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1875 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001876}
1877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001878llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1879 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001880 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1881 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001882}
1883
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001884llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1885 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001886 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1887 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001888}
1889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1891 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001892 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1893 ? CGM.getContext().getTypeSize(Ty)
1894 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001895 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001896 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001897 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001898 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001899
1900 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001901 Ty->getPointeeType()->getAs<FunctionProtoType>();
1902 return DBuilder.createMemberPointerType(
1903 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1904 Ty->getClass(), FPT->getTypeQuals())),
1905 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001906 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001907}
1908
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001909llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001910 // Ignore the atomic wrapping
1911 // FIXME: What is the correct representation?
1912 return getOrCreateType(Ty->getValueType(), U);
1913}
1914
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001915llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001916 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001917 uint64_t Size = 0;
1918 uint64_t Align = 0;
1919 if (!ED->getTypeForDecl()->isIncompleteType()) {
1920 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1921 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1922 }
1923
Manman Rene0064d82013-08-29 23:19:58 +00001924 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1925
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 // If this is just a forward declaration, construct an appropriately
1927 // marked node and just return it.
1928 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001929 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001930 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 unsigned Line = getLineNumber(ED->getLocation());
1933 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001934 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001935 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001936 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001937 ReplaceMap.emplace_back(
1938 std::piecewise_construct, std::make_tuple(Ty),
1939 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001940 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 }
1942
David Blaikie483a9da2014-05-06 18:35:21 +00001943 return CreateTypeDefinition(Ty);
1944}
1945
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001946llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001947 const EnumDecl *ED = Ty->getDecl();
1948 uint64_t Size = 0;
1949 uint64_t Align = 0;
1950 if (!ED->getTypeForDecl()->isIncompleteType()) {
1951 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1952 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1953 }
1954
1955 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1956
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001957 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001958 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001959 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001960 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001961 Enumerators.push_back(DBuilder.createEnumerator(
1962 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 }
1964
1965 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001966 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001967
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001969 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001970 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001971 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001972 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001973 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1974 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1975 Line, Size, Align, EltArray, ClassTy,
1976 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001977}
1978
David Blaikie05491062013-01-21 04:37:12 +00001979static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1980 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001981 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001982 Qualifiers InnerQuals = T.getLocalQualifiers();
1983 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1984 // that is already there.
1985 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1986 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001987 QualType LastT = T;
1988 switch (T->getTypeClass()) {
1989 default:
David Blaikie05491062013-01-21 04:37:12 +00001990 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001991 case Type::TemplateSpecialization: {
1992 const auto *Spec = cast<TemplateSpecializationType>(T);
1993 if (Spec->isTypeAlias())
1994 return C.getQualifiedType(T.getTypePtr(), Quals);
1995 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001996 break;
1997 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 case Type::TypeOfExpr:
1999 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2000 break;
2001 case Type::TypeOf:
2002 T = cast<TypeOfType>(T)->getUnderlyingType();
2003 break;
2004 case Type::Decltype:
2005 T = cast<DecltypeType>(T)->getUnderlyingType();
2006 break;
2007 case Type::UnaryTransform:
2008 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2009 break;
2010 case Type::Attributed:
2011 T = cast<AttributedType>(T)->getEquivalentType();
2012 break;
2013 case Type::Elaborated:
2014 T = cast<ElaboratedType>(T)->getNamedType();
2015 break;
2016 case Type::Paren:
2017 T = cast<ParenType>(T)->getInnerType();
2018 break;
David Blaikie05491062013-01-21 04:37:12 +00002019 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002021 break;
2022 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002023 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002024 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002025 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 break;
2027 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002028
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002030 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 } while (true);
2032}
2033
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002034llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002035
2036 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002037 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002038
David Blaikief427b002014-05-06 03:42:01 +00002039 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 if (it != TypeCache.end()) {
2041 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002042 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002043 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 }
2045
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002046 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002047}
2048
David Blaikie0e716b42014-03-03 23:48:23 +00002049void CGDebugInfo::completeTemplateDefinition(
2050 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002051 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2052 return;
2053
David Blaikie0e716b42014-03-03 23:48:23 +00002054 completeClassData(&SD);
2055 // In case this type has no member function definitions being emitted, ensure
2056 // it is retained
2057 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2058}
2059
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002060llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002062 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002063
2064 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002065 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002066
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002067 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002068 return T;
2069
2070 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002071 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002072 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002073
2074 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002075 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002076
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 return Res;
2078}
2079
Eric Christopher1ecc5632013-06-07 22:54:39 +00002080unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002081 // The assumption is that the number of ivars can only increase
2082 // monotonically, so it is safe to just use their current number as
2083 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002084 unsigned Sum = 0;
2085 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002086 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002087 ++Sum;
2088
2089 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002090}
2091
2092ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2093 switch (Ty->getTypeClass()) {
2094 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002095 return getObjCInterfaceDecl(
2096 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002097 case Type::ObjCInterface:
2098 return cast<ObjCInterfaceType>(Ty)->getDecl();
2099 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002100 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002101 }
2102}
2103
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002104llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002105 // Handle qualifiers, which recursively handles what they refer to.
2106 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002107 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002108
Guy Benyei11169dd2012-12-18 14:30:41 +00002109 // Work out details of type.
2110 switch (Ty->getTypeClass()) {
2111#define TYPE(Class, Base)
2112#define ABSTRACT_TYPE(Class, Base)
2113#define NON_CANONICAL_TYPE(Class, Base)
2114#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2115#include "clang/AST/TypeNodes.def"
2116 llvm_unreachable("Dependent types cannot show up in debug information");
2117
2118 case Type::ExtVector:
2119 case Type::Vector:
2120 return CreateType(cast<VectorType>(Ty), Unit);
2121 case Type::ObjCObjectPointer:
2122 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2123 case Type::ObjCObject:
2124 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2125 case Type::ObjCInterface:
2126 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2127 case Type::Builtin:
2128 return CreateType(cast<BuiltinType>(Ty));
2129 case Type::Complex:
2130 return CreateType(cast<ComplexType>(Ty));
2131 case Type::Pointer:
2132 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002133 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002134 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002135 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002136 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002137 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 case Type::BlockPointer:
2139 return CreateType(cast<BlockPointerType>(Ty), Unit);
2140 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002141 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002142 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002143 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002145 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 case Type::FunctionProto:
2147 case Type::FunctionNoProto:
2148 return CreateType(cast<FunctionType>(Ty), Unit);
2149 case Type::ConstantArray:
2150 case Type::VariableArray:
2151 case Type::IncompleteArray:
2152 return CreateType(cast<ArrayType>(Ty), Unit);
2153
2154 case Type::LValueReference:
2155 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2156 case Type::RValueReference:
2157 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2158
2159 case Type::MemberPointer:
2160 return CreateType(cast<MemberPointerType>(Ty), Unit);
2161
2162 case Type::Atomic:
2163 return CreateType(cast<AtomicType>(Ty), Unit);
2164
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002166 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2167
David Blaikie42edade2014-11-11 20:44:45 +00002168 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002169 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002170 case Type::Elaborated:
2171 case Type::Paren:
2172 case Type::SubstTemplateTypeParm:
2173 case Type::TypeOfExpr:
2174 case Type::TypeOf:
2175 case Type::Decltype:
2176 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002177 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002178 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002180
David Blaikie42edade2014-11-11 20:44:45 +00002181 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002182}
2183
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002184llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2185 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002186 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002187
Duncan P. N. Exon Smithbd210e62015-07-24 20:34:41 +00002188 auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002189
2190 // We may have cached a forward decl when we could have created
2191 // a non-forward decl. Go ahead and create a non-forward decl
2192 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002193 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002194 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002195
2196 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002197 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002198
2199 // Propagate members from the declaration to the definition
2200 // CreateType(const RecordType*) will overwrite this with the members in the
2201 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002202 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002203
Guy Benyei11169dd2012-12-18 14:30:41 +00002204 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002205 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 return Res;
2207}
2208
2209// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002210llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002211 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002212
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002214 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 unsigned Line = getLineNumber(RD->getLocation());
2216 StringRef RDName = getClassName(RD);
2217
David Blaikiebe822ed2015-07-01 18:07:22 +00002218 llvm::DIScope *RDContext =
2219 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002220
David Blaikied2785892013-08-18 17:36:19 +00002221 // If we ended up creating the type during the context chain construction,
2222 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002223 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002224 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002225 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002226 return T;
David Blaikied2785892013-08-18 17:36:19 +00002227
Adrian Prantl381e7552014-02-04 21:29:50 +00002228 // If this is just a forward or incomplete declaration, construct an
2229 // appropriately marked node and just return it.
2230 const RecordDecl *D = RD->getDefinition();
2231 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002232 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002233
2234 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2235 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002236
Manman Rene0064d82013-08-29 23:19:58 +00002237 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002239 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002240 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2241 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002242
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002243 RegionMap[Ty->getDecl()].reset(RealDecl);
2244 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002245
David Blaikieadfbf992013-08-18 16:55:33 +00002246 if (const ClassTemplateSpecializationDecl *TSpecial =
2247 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002248 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002249 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002250 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002251}
2252
David Blaikieadfbf992013-08-18 16:55:33 +00002253void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002254 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002255 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002256 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002257 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2258 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002259 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002260 while (1) {
2261 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2262 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2263 if (PBT && !BRL.isPrimaryBaseVirtual())
2264 PBase = PBT;
2265 else
2266 break;
2267 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002268 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002269 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2270 getOrCreateFile(RD->getLocation())));
2271 } else if (RD->isDynamicClass())
2272 ContainingType = RealDecl;
2273
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002274 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002275}
2276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002277llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002278 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002279 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002280 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2281 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002282 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002283 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 *Offset += FieldSize;
2285 return Ty;
2286}
2287
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002288void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002289 StringRef &Name,
2290 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002291 llvm::DIScope *&FDContext,
2292 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002293 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002294 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2295 Name = getFunctionName(FD);
2296 // Use mangled name as linkage name for C/C++ functions.
2297 if (FD->hasPrototype()) {
2298 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002299 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002300 }
2301 // No need to replicate the linkage name if it isn't different from the
2302 // subprogram name, no need to have it at all unless coverage is enabled or
2303 // debug is set to more than just line tables.
2304 if (LinkageName == Name ||
2305 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2306 !CGM.getCodeGenOpts().EmitGcovNotes &&
2307 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2308 LinkageName = StringRef();
2309
2310 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2311 if (const NamespaceDecl *NSDecl =
2312 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2313 FDContext = getOrCreateNameSpace(NSDecl);
2314 else if (const RecordDecl *RDecl =
2315 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2316 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2317 // Collect template parameters.
2318 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2319 }
2320}
2321
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002322void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002323 unsigned &LineNo, QualType &T,
2324 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002325 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002326 Unit = getOrCreateFile(VD->getLocation());
2327 LineNo = getLineNumber(VD->getLocation());
2328
2329 setLocation(VD->getLocation());
2330
2331 T = VD->getType();
2332 if (T->isIncompleteArrayType()) {
2333 // CodeGen turns int[] into int[1] so we'll do the same here.
2334 llvm::APInt ConstVal(32, 1);
2335 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2336
2337 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2338 ArrayType::Normal, 0);
2339 }
2340
2341 Name = VD->getName();
2342 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2343 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2344 LinkageName = CGM.getMangledName(VD);
2345 if (LinkageName == Name)
2346 LinkageName = StringRef();
2347
2348 // Since we emit declarations (DW_AT_members) for static members, place the
2349 // definition of those static members in the namespace they were declared in
2350 // in the source code (the lexical decl context).
2351 // FIXME: Generalize this for even non-member global variables where the
2352 // declaration and definition may have different lexical decl contexts, once
2353 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002354 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2355 : VD->getDeclContext();
2356 // When a record type contains an in-line initialization of a static data
2357 // member, and the record type is marked as __declspec(dllexport), an implicit
2358 // definition of the member will be created in the record context. DWARF
2359 // doesn't seem to have a nice way to describe this in a form that consumers
2360 // are likely to understand, so fake the "normal" situation of a definition
2361 // outside the class by putting it in the global scope.
2362 if (DC->isRecord())
2363 DC = CGM.getContext().getTranslationUnitDecl();
David Blaikiebe822ed2015-07-01 18:07:22 +00002364 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002365}
2366
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002367llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002368CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002369 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002370 StringRef Name, LinkageName;
2371 unsigned Flags = 0;
2372 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002373 llvm::DIFile *Unit = getOrCreateFile(Loc);
2374 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002375 unsigned Line = getLineNumber(Loc);
2376
2377 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2378 TParamsArray, Flags);
2379 // Build function type.
2380 SmallVector<QualType, 16> ArgTypes;
2381 for (const ParmVarDecl *Parm: FD->parameters())
2382 ArgTypes.push_back(Parm->getType());
2383 QualType FnType =
2384 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2385 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002386 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002387 DContext, Name, LinkageName, Unit, Line,
2388 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
Duncan P. N. Exon Smith31d38f72015-08-26 22:21:09 +00002389 /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002390 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002391 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002392 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2393 std::make_tuple(CanonDecl),
2394 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002395 return SP;
2396}
2397
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002398llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002399CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2400 QualType T;
2401 StringRef Name, LinkageName;
2402 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002403 llvm::DIFile *Unit = getOrCreateFile(Loc);
2404 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002405 unsigned Line = getLineNumber(Loc);
2406
2407 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002408 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2409 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2410 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002411 FwdDeclReplaceMap.emplace_back(
2412 std::piecewise_construct,
2413 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2414 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002415 return GV;
2416}
2417
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002418llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002419 // We only need a declaration (not a definition) of the type - so use whatever
2420 // we would otherwise do to get a type for a pointee. (forward declarations in
2421 // limited debug info, full definitions (if the type definition is available)
2422 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002423 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2424 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002425 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002426 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002427
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002428 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002429 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002430
2431 // No definition for now. Emit a forward definition that might be
2432 // merged with a potential upcoming definition.
2433 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2434 return getFunctionForwardDeclaration(FD);
2435 else if (const auto *VD = dyn_cast<VarDecl>(D))
2436 return getGlobalVariableForwardDeclaration(VD);
2437
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002438 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002439}
2440
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002442 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002443 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002444
Guy Benyei11169dd2012-12-18 14:30:41 +00002445 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002446 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002447 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002448
2449 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002450 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002451
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002452 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002453 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002454 if (const CXXMethodDecl *MD =
2455 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002456 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002457 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002458 }
2459 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002460 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002461 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002462 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 return SP;
2464 }
2465
Aaron Ballman86c93902014-03-06 23:45:36 +00002466 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002467 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002468 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002469 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002470 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002471 return SP;
2472 }
2473 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002474 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002475}
2476
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002477// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002478// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002479llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002480 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002481 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002482 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002483 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2484 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002485 return DBuilder.createSubroutineType(F,
2486 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002487
2488 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2489 return getOrCreateMethodType(Method, F);
2490 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2491 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002492 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002493
2494 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002495 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002496
2497 // Replace the instancetype keyword with the actual type.
2498 if (ResultTy == CGM.getContext().getObjCInstanceType())
2499 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002500 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002501
Adrian Prantl7bec9032013-05-10 21:08:31 +00002502 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002504 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002505 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002507 Elts.push_back(DBuilder.createArtificialType(
2508 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002509 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002510 for (const auto *PI : OMethod->params())
2511 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002512 // Variadic methods need a special marker at the end of the type list.
2513 if (OMethod->isVariadic())
2514 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002515
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002516 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 return DBuilder.createSubroutineType(F, EltTypeArray);
2518 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002519
Adrian Prantl800faef2014-02-25 23:42:18 +00002520 // Handle variadic function types; they need an additional
2521 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002522 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2523 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002524 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002525 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2526 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2527 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2528 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2529 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002531 return DBuilder.createSubroutineType(F, EltTypeArray);
2532 }
2533
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002534 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002535}
2536
Eric Christophere7b87e52014-10-26 23:40:33 +00002537void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2538 SourceLocation ScopeLoc, QualType FnType,
2539 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002540
2541 StringRef Name;
2542 StringRef LinkageName;
2543
2544 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2545
2546 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002547 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002548
Guy Benyei11169dd2012-12-18 14:30:41 +00002549 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002550 llvm::DIFile *Unit = getOrCreateFile(Loc);
2551 llvm::DIScope *FDContext = Unit;
2552 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 if (!HasDecl) {
2554 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002555 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002556 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002557 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002558 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002560 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002561 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002562 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002563 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 return;
2565 }
2566 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002567 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2568 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2570 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002571 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 } else {
2573 // Use llvm function name.
2574 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002575 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 }
2577 if (!Name.empty() && Name[0] == '\01')
2578 Name = Name.substr(1);
2579
Adrian Prantl42d71b92014-04-10 23:21:53 +00002580 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002581 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002582 // Artificial functions without a location should not silently reuse CurLoc.
2583 if (Loc.isInvalid())
2584 CurLoc = SourceLocation();
2585 }
2586 unsigned LineNo = getLineNumber(Loc);
2587 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002588
Eric Christopher8018e412014-03-27 18:50:35 +00002589 // FIXME: The function declaration we're constructing here is mostly reusing
2590 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2591 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2592 // all subprograms instead of the actual context since subprogram definitions
2593 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002594 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002595 FDContext, Name, LinkageName, Unit, LineNo,
2596 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2597 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002598 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002599 // We might get here with a VarDecl in the case we're generating
2600 // code for the initialization of globals. Do not record these decls
2601 // as they will overwrite the actual VarDecl Decl in the cache.
2602 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002603 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002604
Adrian Prantlbebb8932014-03-21 21:01:58 +00002605 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002606 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002607
Guy Benyei11169dd2012-12-18 14:30:41 +00002608 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002609 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002610}
2611
David Blaikie835afb22015-01-21 23:08:17 +00002612void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 // Update our current location
2614 setLocation(Loc);
2615
Eric Christophere7b87e52014-10-26 23:40:33 +00002616 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2617 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002618
Adrian Prantle83b1302014-01-07 22:05:52 +00002619 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002620 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002621 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002622}
2623
Guy Benyei11169dd2012-12-18 14:30:41 +00002624void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002625 llvm::MDNode *Back = nullptr;
2626 if (!LexicalBlockStack.empty())
2627 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002628 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002629 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002630 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002631}
2632
Eric Christopher0fdcb312013-05-16 00:52:20 +00002633void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2634 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002635 // Set our current location.
2636 setLocation(Loc);
2637
Guy Benyei11169dd2012-12-18 14:30:41 +00002638 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002639 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2640 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002641
2642 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2643 return;
2644
2645 // Create a new lexical block and push it on the stack.
2646 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002647}
2648
Eric Christopher0fdcb312013-05-16 00:52:20 +00002649void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2650 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002651 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2652
2653 // Provide an entry in the line table for the end of the block.
2654 EmitLocation(Builder, Loc);
2655
David Blaikie60a877b2014-10-22 19:34:33 +00002656 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2657 return;
2658
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 LexicalBlockStack.pop_back();
2660}
2661
Guy Benyei11169dd2012-12-18 14:30:41 +00002662void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2663 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2664 unsigned RCount = FnBeginRegionCount.back();
2665 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2666
2667 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002668 while (LexicalBlockStack.size() != RCount) {
2669 // Provide an entry in the line table for the end of the block.
2670 EmitLocation(Builder, CurLoc);
2671 LexicalBlockStack.pop_back();
2672 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002673 FnBeginRegionCount.pop_back();
2674}
2675
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002676llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002677 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002678
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002679 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 QualType FType;
2681 uint64_t FieldSize, FieldOffset;
2682 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002683
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002684 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002685 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002686
2687 FieldOffset = 0;
2688 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2689 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2690 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2691 FType = CGM.getContext().IntTy;
2692 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2693 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2694
2695 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2696 if (HasCopyAndDispose) {
2697 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002698 EltTys.push_back(
2699 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2700 EltTys.push_back(
2701 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002702 }
2703 bool HasByrefExtendedLayout;
2704 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002705 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2706 HasByrefExtendedLayout) &&
2707 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002708 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002709 EltTys.push_back(
2710 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002711 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002712
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2714 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002715 CGM.getTarget().getPointerAlign(0))) {
2716 CharUnits FieldOffsetInBytes =
2717 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2718 CharUnits AlignedOffsetInBytes =
2719 FieldOffsetInBytes.RoundUpToAlignment(Align);
2720 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002721
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 if (NumPaddingBytes.isPositive()) {
2723 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2724 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2725 pad, ArrayType::Normal, 0);
2726 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2727 }
2728 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002729
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002731 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002732 FieldSize = CGM.getContext().getTypeSize(FType);
2733 FieldAlign = CGM.getContext().toBits(Align);
2734
Eric Christopherb2a008c2013-05-16 00:45:12 +00002735 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002736 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2737 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002738 EltTys.push_back(FieldTy);
2739 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002740
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002741 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002742
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002743 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002744
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002746 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002747}
2748
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002749void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage,
2750 llvm::Optional<unsigned> ArgNo,
Eric Christophere7b87e52014-10-26 23:40:33 +00002751 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002752 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002753 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2754
David Blaikie7fceebf2013-08-19 03:37:48 +00002755 bool Unwritten =
2756 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2757 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002758 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002759 if (!Unwritten)
2760 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002761 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 uint64_t XOffset = 0;
2763 if (VD->hasAttr<BlocksAttr>())
2764 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002765 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002766 Ty = getOrCreateType(VD->getType(), Unit);
2767
2768 // If there is no debug info for this type then do not emit debug info
2769 // for this variable.
2770 if (!Ty)
2771 return;
2772
Guy Benyei11169dd2012-12-18 14:30:41 +00002773 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002774 unsigned Line = 0;
2775 unsigned Column = 0;
2776 if (!Unwritten) {
2777 Line = getLineNumber(VD->getLocation());
2778 Column = getColumnNumber(VD->getLocation());
2779 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002780 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 unsigned Flags = 0;
2782 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002783 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 // If this is the first argument and it is implicit then
2785 // give it an object pointer flag.
2786 // FIXME: There has to be a better way to do this, but for static
2787 // functions there won't be an implicit param at arg1 and
2788 // otherwise it is 'self' or 'this'.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002789 if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002790 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002791 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002792 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2793 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002794 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002795
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002796 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002797
2798 StringRef Name = VD->getName();
2799 if (!Name.empty()) {
2800 if (VD->hasAttr<BlocksAttr>()) {
2801 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002802 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 // offset of __forwarding field
2804 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002805 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002806 Expr.push_back(offset.getQuantity());
2807 Expr.push_back(llvm::dwarf::DW_OP_deref);
2808 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 // offset of x field
2810 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002811 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002812
2813 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002814 auto *D = ArgNo
2815 ? DBuilder.createParameterVariable(Scope, VD->getName(),
2816 *ArgNo, Unit, Line, Ty)
2817 : DBuilder.createAutoVariable(Scope, VD->getName(), Unit,
2818 Line, Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002819
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002821 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2822 llvm::DebugLoc::get(Line, Column, Scope),
2823 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002825 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002826 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002827 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2828 // If VD is an anonymous union then Storage represents value for
2829 // all union fields.
2830 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2831 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002832 // GDB has trouble finding local variables in anonymous unions, so we emit
2833 // artifical local variables for each of the members.
2834 //
2835 // FIXME: Remove this code as soon as GDB supports this.
2836 // The debug info verifier in LLVM operates based on the assumption that a
2837 // variable has the same size as its storage and we had to disable the check
2838 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002839 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002840 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002841 StringRef FieldName = Field->getName();
2842
2843 // Ignore unnamed fields. Do not ignore unnamed records.
2844 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2845 continue;
2846
2847 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002848 auto *D = DBuilder.createAutoVariable(
2849 Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
2850 Flags | llvm::DINode::FlagArtificial);
Adrian Prantl0d820892015-04-29 15:05:50 +00002851
2852 // Insert an llvm.dbg.declare into the current block.
2853 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2854 llvm::DebugLoc::get(Line, Column, Scope),
2855 Builder.GetInsertBlock());
2856 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002857 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002859
2860 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002861 auto *D =
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002862 ArgNo
2863 ? DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line,
2864 Ty, CGM.getLangOpts().Optimize,
2865 Flags)
2866 : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty,
2867 CGM.getLangOpts().Optimize, Flags);
David Blaikiea76a7c92013-01-05 05:58:35 +00002868
2869 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002870 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2871 llvm::DebugLoc::get(Line, Column, Scope),
2872 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002873}
2874
2875void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2876 llvm::Value *Storage,
2877 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002878 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002879 EmitDeclare(VD, Storage, llvm::None, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002880}
2881
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002882llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2883 llvm::DIType *Ty) {
2884 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002885 if (CachedTy)
2886 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002887 return DBuilder.createObjectPointerType(Ty);
2888}
2889
Eric Christophere7b87e52014-10-26 23:40:33 +00002890void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2891 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002892 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002893 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002894 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002895
Craig Topper8a13c412014-05-21 05:09:00 +00002896 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002897 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002898
Guy Benyei11169dd2012-12-18 14:30:41 +00002899 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002900
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002902 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2903 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 if (isByRef)
2905 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002906 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 Ty = getOrCreateType(VD->getType(), Unit);
2908
2909 // Self is passed along as an implicit non-arg variable in a
2910 // block. Mark it as the object pointer.
2911 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002912 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002913
2914 // Get location information.
2915 unsigned Line = getLineNumber(VD->getLocation());
2916 unsigned Column = getColumnNumber(VD->getLocation());
2917
2918 const llvm::DataLayout &target = CGM.getDataLayout();
2919
2920 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002921 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2923
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002924 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002925 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002926 addr.push_back(llvm::dwarf::DW_OP_deref);
2927 addr.push_back(llvm::dwarf::DW_OP_plus);
2928 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002929 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002930 addr.push_back(llvm::dwarf::DW_OP_deref);
2931 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002933 offset =
2934 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002935 addr.push_back(offset.getQuantity());
2936 addr.push_back(llvm::dwarf::DW_OP_deref);
2937 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002938 // offset of x field
2939 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002940 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 }
2942
2943 // Create the descriptor for the variable.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002944 auto *D = DBuilder.createAutoVariable(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002945 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002946 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002947
Guy Benyei11169dd2012-12-18 14:30:41 +00002948 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002949 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2950 if (InsertPoint)
2951 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2952 InsertPoint);
2953 else
2954 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2955 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002956}
2957
Guy Benyei11169dd2012-12-18 14:30:41 +00002958void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2959 unsigned ArgNo,
2960 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002961 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00002962 EmitDeclare(VD, AI, ArgNo, Builder);
Guy Benyei11169dd2012-12-18 14:30:41 +00002963}
2964
2965namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002966struct BlockLayoutChunk {
2967 uint64_t OffsetInBits;
2968 const BlockDecl::Capture *Capture;
2969};
2970bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2971 return l.OffsetInBits < r.OffsetInBits;
2972}
Guy Benyei11169dd2012-12-18 14:30:41 +00002973}
2974
2975void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002976 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002977 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002978 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002980 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002981 ASTContext &C = CGM.getContext();
2982 const BlockDecl *blockDecl = block.getBlockDecl();
2983
2984 // Collect some general information about the block's location.
2985 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002986 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002987 unsigned line = getLineNumber(loc);
2988 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002989
Guy Benyei11169dd2012-12-18 14:30:41 +00002990 // Build the debug-info type for the block literal.
2991 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2992
2993 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002994 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00002995
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002996 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00002997 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2998 blockLayout->getElementOffsetInBits(0),
2999 tunit, tunit));
3000 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3001 blockLayout->getElementOffsetInBits(1),
3002 tunit, tunit));
3003 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3004 blockLayout->getElementOffsetInBits(2),
3005 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003006 auto *FnTy = block.getBlockExpr()->getFunctionType();
3007 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3008 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003009 blockLayout->getElementOffsetInBits(3),
3010 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003011 fields.push_back(createFieldType(
3012 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3013 ? C.getBlockDescriptorExtendedType()
3014 : C.getBlockDescriptorType()),
3015 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003016
3017 // We want to sort the captures by offset, not because DWARF
3018 // requires this, but because we're paranoid about debuggers.
3019 SmallVector<BlockLayoutChunk, 8> chunks;
3020
3021 // 'this' capture.
3022 if (blockDecl->capturesCXXThis()) {
3023 BlockLayoutChunk chunk;
3024 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003025 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003026 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 chunks.push_back(chunk);
3028 }
3029
3030 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003031 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 const VarDecl *variable = capture.getVariable();
3033 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3034
3035 // Ignore constant captures.
3036 if (captureInfo.isConstant())
3037 continue;
3038
3039 BlockLayoutChunk chunk;
3040 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003041 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 chunk.Capture = &capture;
3043 chunks.push_back(chunk);
3044 }
3045
3046 // Sort by offset.
3047 llvm::array_pod_sort(chunks.begin(), chunks.end());
3048
Eric Christophere7b87e52014-10-26 23:40:33 +00003049 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3050 e = chunks.end();
3051 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 uint64_t offsetInBits = i->OffsetInBits;
3053 const BlockDecl::Capture *capture = i->Capture;
3054
3055 // If we have a null capture, this must be the C++ 'this' capture.
3056 if (!capture) {
3057 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003058 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 QualType type = method->getThisType(C);
3060
3061 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3062 offsetInBits, tunit, tunit));
3063 continue;
3064 }
3065
3066 const VarDecl *variable = capture->getVariable();
3067 StringRef name = variable->getName();
3068
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003069 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003070 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003071 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003072
3073 // FIXME: this creates a second copy of this type!
3074 uint64_t xoffset;
3075 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003076 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3077 fieldType =
3078 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3079 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003080 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003081 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3082 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 }
3084 fields.push_back(fieldType);
3085 }
3086
3087 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003088 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3089 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003090
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003091 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003092
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003093 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003094 tunit, typeName.str(), tunit, line,
3095 CGM.getContext().toBits(block.BlockSize),
3096 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3098
3099 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003100 unsigned flags = llvm::DINode::FlagArtificial;
3101 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003102
3103 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithe4306542015-07-31 17:56:14 +00003104 auto *debugVar = DBuilder.createParameterVariable(
3105 scope, Arg->getName(), ArgNo, tunit, line, type,
3106 CGM.getLangOpts().Optimize, flags);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003107
Adrian Prantl616bef42013-03-14 21:52:59 +00003108 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003109 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003110 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003111 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003112 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003113 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003114
Adrian Prantl616bef42013-03-14 21:52:59 +00003115 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003116 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3117 llvm::DebugLoc::get(line, column, scope),
3118 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003119}
3120
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003121llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003122CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3123 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003124 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003125
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003126 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003127 if (MI != StaticDataMemberCache.end()) {
3128 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithac346ba2015-07-24 18:05:58 +00003129 return MI->second;
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003130 }
David Blaikiece763042013-08-20 21:49:21 +00003131
3132 // If the member wasn't found in the cache, lazily construct and add it to the
3133 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003134 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003135 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003136 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003137 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003138}
3139
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003140llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3141 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3142 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3143 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003144
3145 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003146 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003147 StringRef FieldName = Field->getName();
3148
3149 // Ignore unnamed fields, but recurse into anonymous records.
3150 if (FieldName.empty()) {
3151 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3152 if (RT)
3153 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3154 Var, DContext);
3155 continue;
3156 }
3157 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003158 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3159 LineNo, FieldTy,
3160 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003161 }
3162 return GV;
3163}
3164
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003165void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003167 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003168 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 llvm::DIFile *Unit = nullptr;
3170 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003171 unsigned LineNo;
3172 StringRef DeclName, LinkageName;
3173 QualType T;
3174 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003175
3176 // Attempt to store one global variable for the declaration - even if we
3177 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003178 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003179
3180 // If this is an anonymous union then we'll want to emit a global
3181 // variable for each member of the anonymous union so that it's possible
3182 // to find the name of any field in the union.
3183 if (T->isUnionType() && DeclName.empty()) {
3184 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003185 assert(RD->isAnonymousStructOrUnion() &&
3186 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003187 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3188 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003189 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003190 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3191 Var->hasInternalLinkage(), Var,
3192 getOrCreateStaticDataMemberDeclarationOrNull(D));
3193 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003194 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003195}
3196
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003197void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3198 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003199 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003200 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003201 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003202 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003203 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003204 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3205 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3206 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3207 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3208 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003209 // Do not use global variables for enums.
3210 //
3211 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003212 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003213 return;
David Blaikiea15565562014-04-04 20:56:17 +00003214 // Do not emit separate definitions for function local const/statics.
3215 if (isa<FunctionDecl>(VD->getDeclContext()))
3216 return;
David Blaikiebb113912014-04-05 07:23:17 +00003217 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003218 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003219 if (VarD->isStaticDataMember()) {
3220 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3221 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003222 // Ensure that the type is retained even though it's otherwise unreferenced.
3223 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003224 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003225 return;
3226 }
3227
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003228 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003229 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3230
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003231 auto &GV = DeclCache[VD];
3232 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003233 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003234 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003235 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003236 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003237}
3238
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003239llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003240 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003241 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003242 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003243}
3244
David Blaikie9f88fe82013-04-22 06:13:21 +00003245void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003246 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3247 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003248 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003249 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3250 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003251 getLineNumber(UD.getLocation()));
3252}
3253
David Blaikiebd483762013-05-20 04:58:53 +00003254void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3255 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3256 return;
3257 assert(UD.shadow_size() &&
3258 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3259 // Emitting one decl is sufficient - debuggers can detect that this is an
3260 // overloaded name & provide lookup for all the overloads.
3261 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003262 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003263 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003264 DBuilder.createImportedDeclaration(
3265 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3266 getLineNumber(USD.getLocation()));
3267}
3268
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003269void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3270 auto *Reader = CGM.getContext().getExternalSource();
3271 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3272 DBuilder.createImportedDeclaration(
3273 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3274 getOrCreateModuleRef(Info),
3275 getLineNumber(ID.getLocation()));
3276}
3277
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003278llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003279CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3280 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003281 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003282 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003283 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003284 return cast<llvm::DIImportedEntity>(VH);
3285 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003286 if (const NamespaceAliasDecl *Underlying =
3287 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3288 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003289 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003290 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3291 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3292 NA.getName());
3293 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003294 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003295 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3296 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3297 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003298 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003299 return R;
3300}
3301
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003302llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003303CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003304 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003305 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003306 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003307 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003308
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003310 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3311 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003312 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003314 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003315 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003316 return NS;
3317}
3318
3319void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003320 // Creating types might create further types - invalidating the current
3321 // element and the size(), so don't cache/reference them.
3322 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3323 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003324 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003325 ? CreateTypeDefinition(E.Type, E.Unit)
3326 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003327 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003328 }
3329
David Blaikief427b002014-05-06 03:42:01 +00003330 for (auto p : ReplaceMap) {
3331 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003332 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003333 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003334
David Blaikief427b002014-05-06 03:42:01 +00003335 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003336 assert(it != TypeCache.end());
3337 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003338
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003339 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3340 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003341 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003342
Frederic Rissd253ed62014-11-18 03:40:51 +00003343 for (const auto &p : FwdDeclReplaceMap) {
3344 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003345 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003346 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003347
3348 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003349 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003350 // with ourselves, that will destroy the temporary MDNode and
3351 // replace it with a standard one, avoiding leaking memory.
3352 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003353 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003354 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003355 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003356
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003357 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003358 }
3359
Adrian Prantl73409ce2013-03-11 18:33:46 +00003360 // We keep our own list of retained types, because we need to look
3361 // up the final type in the type cache.
3362 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3363 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003364 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003365
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 DBuilder.finalize();
3367}
David Blaikie66088d52014-09-24 17:01:27 +00003368
3369void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3370 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3371 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003372
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003373 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003374 // Don't ignore in case of explicit cast where it is referenced indirectly.
3375 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003376}