blob: 93a2287b1e012eb0b625a5e1a7b3a14dbc71cfbe [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 Blaikie66e41972015-01-14 07:38:27 +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)
66 : 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 Blaikie66e41972015-01-14 07:38:27 +000072 if (auto *DI = CGF.getDebugInfo()) {
73 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Adrian Prantl39428e72015-02-03 18:40:42 +000074 if (TemporaryLocation.isInvalid()) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000075 if (DefaultToEmpty)
Adrian Prantl39428e72015-02-03 18:40:42 +000076 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc());
77 else {
78 // Construct a location that has a valid scope, but no line info.
79 assert(!DI->LexicalBlockStack.empty());
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +000080 CGF.Builder.SetCurrentDebugLocation(
81 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl39428e72015-02-03 18:40:42 +000082 }
83 } else
David Blaikie835afb22015-01-21 23:08:17 +000084 DI->EmitLocation(CGF.Builder, TemporaryLocation);
David Blaikie66e41972015-01-14 07:38:27 +000085 }
Adrian Prantl2e0637f2013-07-18 00:28:02 +000086}
87
David Blaikie9b479662015-01-25 01:19:10 +000088ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
89 : CGF(CGF) {
90 init(E->getExprLoc());
91}
92
David Blaikie66e41972015-01-14 07:38:27 +000093ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
94 : CGF(CGF) {
95 if (CGF.getDebugInfo()) {
96 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Duncan P. N. Exon Smith2809cc72015-03-30 20:01:41 +000097 if (Loc)
Benjamin Kramer03278662015-02-07 13:15:54 +000098 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +000099 }
100}
101
102ApplyDebugLocation::~ApplyDebugLocation() {
103 // Query CGF so the location isn't overwritten when location updates are
104 // temporarily disabled (for C++ default function arguments)
105 if (CGF.getDebugInfo())
Benjamin Kramer03278662015-02-07 13:15:54 +0000106 CGF.Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000107}
108
Guy Benyei11169dd2012-12-18 14:30:41 +0000109void CGDebugInfo::setLocation(SourceLocation Loc) {
110 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000111 if (Loc.isInvalid())
112 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000113
114 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
115
116 // If we've changed files in the middle of a lexical scope go ahead
117 // and create a new lexical scope with file node if it's different
118 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000119 if (LexicalBlockStack.empty())
120 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000121
122 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000123 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000124 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000126 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 return;
128
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000129 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000130 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000131 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
132 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000133 } else if (isa<llvm::DILexicalBlock>(Scope) ||
134 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000135 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000136 LexicalBlockStack.emplace_back(
137 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000138 }
139}
140
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000141llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 if (!Context)
143 return TheCU;
144
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000145 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000146 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000147 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000148 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000149 }
150
151 // Check namespace.
152 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000153 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000154
David Blaikiebfa52742013-04-19 06:56:38 +0000155 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
156 if (!RDecl->isDependentType())
157 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000158 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000159 return TheCU;
160}
161
Guy Benyei11169dd2012-12-18 14:30:41 +0000162StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000163 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000164 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000165 FunctionTemplateSpecializationInfo *Info =
166 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000167 if (!Info && FII)
168 return FII->getName();
169
170 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000171 SmallString<128> NS;
172 llvm::raw_svector_ostream OS(NS);
173 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000174
175 // Add any template specialization args.
176 if (Info) {
177 const TemplateArgumentList *TArgs = Info->TemplateArguments;
178 const TemplateArgument *Args = TArgs->data();
179 unsigned NumArgs = TArgs->size();
180 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000181 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
182 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 }
184
185 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000186 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000187}
188
189StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
190 SmallString<256> MethodName;
191 llvm::raw_svector_ostream OS(MethodName);
192 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
193 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000194 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000195 dyn_cast<const ObjCImplementationDecl>(DC)) {
196 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000197 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000198 dyn_cast<const ObjCInterfaceDecl>(DC)) {
199 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000200 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000201 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
202 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
203 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000204 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000205 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000206 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000207 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000208 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000209 ClassTy.print(OS, PrintingPolicy(LangOptions()));
210 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000211 }
212 OS << ' ' << OMD->getSelector().getAsString() << ']';
213
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000214 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000215}
216
Guy Benyei11169dd2012-12-18 14:30:41 +0000217StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000218 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000219}
220
Eric Christophere7b87e52014-10-26 23:40:33 +0000221StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000222 // quick optimization to avoid having to intern strings that are already
223 // stored reliably elsewhere
224 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000225 return RD->getName();
226
David Blaikie65813a32014-04-02 18:21:09 +0000227 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000228 {
David Blaikie65813a32014-04-02 18:21:09 +0000229 llvm::raw_svector_ostream OS(Name);
230 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
231 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000232 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000233
234 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000235 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000236}
237
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000238llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000239 if (!Loc.isValid())
240 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000241 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000242
243 SourceManager &SM = CGM.getContext().getSourceManager();
244 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
245
246 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
247 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000248 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000249
250 // Cache the results.
251 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000252 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000253
254 if (it != DIFileCache.end()) {
255 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000256 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000257 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000258 }
259
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000260 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000261 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000262
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000263 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000264 return F;
265}
266
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000267llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000268 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000269}
270
Guy Benyei11169dd2012-12-18 14:30:41 +0000271unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
272 if (Loc.isInvalid() && CurLoc.isInvalid())
273 return 0;
274 SourceManager &SM = CGM.getContext().getSourceManager();
275 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000276 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000277}
278
Adrian Prantlc7822422013-03-12 20:43:25 +0000279unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000280 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000281 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000282 return 0;
283
284 // If the location is invalid then use the current column.
285 if (Loc.isInvalid() && CurLoc.isInvalid())
286 return 0;
287 SourceManager &SM = CGM.getContext().getSourceManager();
288 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000289 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000290}
291
292StringRef CGDebugInfo::getCurrentDirname() {
293 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
294 return CGM.getCodeGenOpts().DebugCompilationDir;
295
296 if (!CWDName.empty())
297 return CWDName;
298 SmallString<256> CWD;
299 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000300 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000301}
302
Guy Benyei11169dd2012-12-18 14:30:41 +0000303void CGDebugInfo::CreateCompileUnit() {
304
David Blaikieaabde052014-05-14 00:29:00 +0000305 // Should we be asking the SourceManager for the main file name, instead of
306 // accepting it as an argument? This just causes the main file name to
307 // mismatch with source locations and create extra lexical scopes or
308 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
309 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
310 // because that's what the SourceManager says)
311
Guy Benyei11169dd2012-12-18 14:30:41 +0000312 // Get absolute path name.
313 SourceManager &SM = CGM.getContext().getSourceManager();
314 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
315 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000316 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000317
318 // The main file name provided via the "-main-file-name" option contains just
319 // the file name itself with no path information. This file name may have had
320 // a relative path, so we look into the actual file entry for the main
321 // file to determine the real absolute path for the file.
322 std::string MainFileDir;
323 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
324 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000325 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000326 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
327 llvm::sys::path::append(MainFileDirSS, MainFileName);
328 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000329 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000330 }
331
332 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000333 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000334
335 // Save split dwarf file string.
336 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000337 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000338
Ed Masteda706022014-05-07 12:49:30 +0000339 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000340 const LangOptions &LO = CGM.getLangOpts();
341 if (LO.CPlusPlus) {
342 if (LO.ObjC1)
343 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
344 else
345 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
346 } else if (LO.ObjC1) {
347 LangTag = llvm::dwarf::DW_LANG_ObjC;
348 } else if (LO.C99) {
349 LangTag = llvm::dwarf::DW_LANG_C99;
350 } else {
351 LangTag = llvm::dwarf::DW_LANG_C89;
352 }
353
354 std::string Producer = getClangFullVersion();
355
356 // Figure out which version of the ObjC runtime we have.
357 unsigned RuntimeVers = 0;
358 if (LO.ObjC1)
359 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
360
361 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000362 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000363 TheCU = DBuilder.createCompileUnit(
364 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
365 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000366 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000367 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000368 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000369 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000370 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000371}
372
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000373llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000374 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000375 StringRef BTName;
376 switch (BT->getKind()) {
377#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000378#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000379#include "clang/AST/BuiltinTypes.def"
380 case BuiltinType::Dependent:
381 llvm_unreachable("Unexpected builtin type");
382 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000383 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000384 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000385 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000386 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000387 if (!ClassTy)
388 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
389 "objc_class", TheCU,
390 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000391 return ClassTy;
392 case BuiltinType::ObjCId: {
393 // typedef struct objc_class *Class;
394 // typedef struct objc_object {
395 // Class isa;
396 // } *id;
397
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000398 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000399 return ObjTy;
400
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000401 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000402 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
403 "objc_class", TheCU,
404 getOrCreateMainFile(), 0);
405
406 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000407
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000408 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000409
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000410 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000411 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000412 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000413
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000414 DBuilder.replaceArrays(
415 ObjTy,
416 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
417 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000418 return ObjTy;
419 }
420 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000421 if (!SelTy)
422 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
423 "objc_selector", TheCU,
424 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000425 return SelTy;
426 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000427
428 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000429 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000430 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000431 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000432 OCLImage1dArrayDITy);
433 case BuiltinType::OCLImage1dBuffer:
434 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
435 OCLImage1dBufferDITy);
436 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000437 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000438 case BuiltinType::OCLImage2dArray:
439 return getOrCreateStructPtrType("opencl_image2d_array_t",
440 OCLImage2dArrayDITy);
441 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000442 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000443 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000444 return DBuilder.createBasicType(
445 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
446 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000447 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000448 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449
Guy Benyei11169dd2012-12-18 14:30:41 +0000450 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 case BuiltinType::Char_U:
452 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
453 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000454 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000455 case BuiltinType::SChar:
456 Encoding = llvm::dwarf::DW_ATE_signed_char;
457 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000458 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 case BuiltinType::Char32:
460 Encoding = llvm::dwarf::DW_ATE_UTF;
461 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000462 case BuiltinType::UShort:
463 case BuiltinType::UInt:
464 case BuiltinType::UInt128:
465 case BuiltinType::ULong:
466 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000467 case BuiltinType::ULongLong:
468 Encoding = llvm::dwarf::DW_ATE_unsigned;
469 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000470 case BuiltinType::Short:
471 case BuiltinType::Int:
472 case BuiltinType::Int128:
473 case BuiltinType::Long:
474 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000475 case BuiltinType::LongLong:
476 Encoding = llvm::dwarf::DW_ATE_signed;
477 break;
478 case BuiltinType::Bool:
479 Encoding = llvm::dwarf::DW_ATE_boolean;
480 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 case BuiltinType::Half:
482 case BuiltinType::Float:
483 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::Double:
485 Encoding = llvm::dwarf::DW_ATE_float;
486 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 }
488
489 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000490 case BuiltinType::Long:
491 BTName = "long int";
492 break;
493 case BuiltinType::LongLong:
494 BTName = "long long int";
495 break;
496 case BuiltinType::ULong:
497 BTName = "long unsigned int";
498 break;
499 case BuiltinType::ULongLong:
500 BTName = "long long unsigned int";
501 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 default:
503 BTName = BT->getName(CGM.getLangOpts());
504 break;
505 }
506 // Bit size, align and offset of the type.
507 uint64_t Size = CGM.getContext().getTypeSize(BT);
508 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000509 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000510}
511
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000512llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000513 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000514 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000515 if (Ty->isComplexIntegerType())
516 Encoding = llvm::dwarf::DW_ATE_lo_user;
517
518 uint64_t Size = CGM.getContext().getTypeSize(Ty);
519 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000520 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000521}
522
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000523llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
524 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 QualifierCollector Qc;
526 const Type *T = Qc.strip(Ty);
527
528 // Ignore these qualifiers for now.
529 Qc.removeObjCGCAttr();
530 Qc.removeAddressSpace();
531 Qc.removeObjCLifetime();
532
533 // We will create one Derived type for one qualifier and recurse to handle any
534 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000535 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000536 if (Qc.hasConst()) {
537 Tag = llvm::dwarf::DW_TAG_const_type;
538 Qc.removeConst();
539 } else if (Qc.hasVolatile()) {
540 Tag = llvm::dwarf::DW_TAG_volatile_type;
541 Qc.removeVolatile();
542 } else if (Qc.hasRestrict()) {
543 Tag = llvm::dwarf::DW_TAG_restrict_type;
544 Qc.removeRestrict();
545 } else {
546 assert(Qc.empty() && "Unknown type qualifier for debug info");
547 return getOrCreateType(QualType(T, 0), Unit);
548 }
549
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000550 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000551
552 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
553 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000554 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000555}
556
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000557llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
558 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000559
560 // The frontend treats 'id' as a typedef to an ObjCObjectType,
561 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
562 // debug info, we want to emit 'id' in both cases.
563 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000564 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000565
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000566 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
567 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568}
569
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000570llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
571 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000572 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000573 Ty->getPointeeType(), Unit);
574}
575
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000576/// \return whether a C++ mangling exists for the type defined by TD.
577static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
578 switch (TheCU->getSourceLanguage()) {
579 case llvm::dwarf::DW_LANG_C_plus_plus:
580 return true;
581 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
582 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
583 default:
584 return false;
585 }
586}
587
Manman Rene0064d82013-08-29 23:19:58 +0000588/// In C++ mode, types have linkage, so we can rely on the ODR and
589/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000590static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
591 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000592 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000593 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000594 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000595
596 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000597 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000598
Manman Rene0064d82013-08-29 23:19:58 +0000599 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
600 if (CGM.getTarget().getCXXABI().isMicrosoft())
601 return FullName;
602
603 // TODO: This is using the RTTI name. Is there a better way to get
604 // a unique string for a type?
605 llvm::raw_svector_ostream Out(FullName);
606 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
607 Out.flush();
608 return FullName;
609}
610
Adrian Prantl3e8bad42015-07-08 21:18:34 +0000611/// \return the approproate DWARF tag for a composite type.
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000612static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
613 llvm::dwarf::Tag Tag;
614 if (RD->isStruct() || RD->isInterface())
615 Tag = llvm::dwarf::DW_TAG_structure_type;
616 else if (RD->isUnion())
617 Tag = llvm::dwarf::DW_TAG_union_type;
618 else {
619 // FIXME: This could be a struct type giving a default visibility different
620 // than C++ class type, but needs llvm metadata changes first.
621 assert(RD->isClass());
622 Tag = llvm::dwarf::DW_TAG_class_type;
623 }
624 return Tag;
625}
626
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000627llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000628CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000629 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000630 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000631 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
632 return cast<llvm::DICompositeType>(T);
633 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000634 unsigned Line = getLineNumber(RD->getLocation());
635 StringRef RDName = getClassName(RD);
636
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000637 uint64_t Size = 0;
638 uint64_t Align = 0;
639
640 const RecordDecl *D = RD->getDefinition();
641 if (D && D->isCompleteDefinition()) {
642 Size = CGM.getContext().getTypeSize(Ty);
643 Align = CGM.getContext().getTypeAlign(Ty);
644 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000645
646 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000647 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000648 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000649 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000650 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000651 ReplaceMap.emplace_back(
652 std::piecewise_construct, std::make_tuple(Ty),
653 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000654 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000655}
656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000657llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000658 const Type *Ty,
659 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000660 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000661 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
662 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000663 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000664
Guy Benyei11169dd2012-12-18 14:30:41 +0000665 // Bit size, align and offset of the type.
666 // Size is always the size of a pointer. We can't use getTypeSize here
667 // because that does not return the correct value for references.
668 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000669 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
671
David Blaikie99dab3b2013-09-04 22:03:57 +0000672 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
673 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000674}
675
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000676llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
677 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000678 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000679 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000680 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
681 TheCU, getOrCreateMainFile(), 0);
682 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
683 Cache = DBuilder.createPointerType(Cache, Size);
684 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000685}
686
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000687llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
688 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000689 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000690 QualType FType;
691 uint64_t FieldSize, FieldOffset;
692 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000693 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000694
695 FieldOffset = 0;
696 FType = CGM.getContext().UnsignedLongTy;
697 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
698 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
699
700 Elements = DBuilder.getOrCreateArray(EltTys);
701 EltTys.clear();
702
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000703 unsigned Flags = llvm::DINode::FlagAppleBlock;
Adrian Prantl498fff62015-07-06 21:31:35 +0000704 unsigned LineNo = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000705
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000706 auto *EltTy =
Adrian Prantl498fff62015-07-06 21:31:35 +0000707 DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000708 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000709
710 // Bit size, align and offset of the type.
711 uint64_t Size = CGM.getContext().getTypeSize(Ty);
712
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000713 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000714
715 FieldOffset = 0;
716 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
717 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
718 FType = CGM.getContext().IntTy;
719 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
720 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000721 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
723
724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000725 FieldSize = CGM.getContext().getTypeSize(Ty);
726 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Adrian Prantl498fff62015-07-06 21:31:35 +0000727 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000728 FieldSize, FieldAlign, FieldOffset,
729 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000730
731 FieldOffset += FieldSize;
732 Elements = DBuilder.getOrCreateArray(EltTys);
733
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000734 // The __block_literal_generic structs are marked with a special
735 // DW_AT_APPLE_BLOCK attribute and are an implementation detail only
736 // the debugger needs to know about. To allow type uniquing, emit
737 // them without a name or a location.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000738 EltTy =
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000739 DBuilder.createStructType(Unit, "", nullptr, LineNo,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000740 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000741
Adrian Prantl3d2c0512015-07-07 00:49:35 +0000742 return DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000743}
744
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000745llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
746 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000747 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000748 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000749
750 SmallString<128> NS;
751 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000752 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
753 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000754
755 TemplateSpecializationType::PrintTemplateArgumentList(
756 OS, Ty->getArgs(), Ty->getNumArgs(),
757 CGM.getContext().getPrintingPolicy());
758
Eric Christophere7b87e52014-10-26 23:40:33 +0000759 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
760 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000761
762 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000763 return DBuilder.createTypedef(
764 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
765 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000766}
767
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000768llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
769 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000770 // We don't set size information, but do specify where the typedef was
771 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000772 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000773
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000774 // Typedefs are derived from some other type.
775 return DBuilder.createTypedef(
776 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
777 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
David Blaikiebe822ed2015-07-01 18:07:22 +0000778 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000779}
780
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000781llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
782 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000783 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000784
785 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000786 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000787
788 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000789 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000790 if (isa<FunctionNoProtoType>(Ty))
791 EltTys.push_back(DBuilder.createUnspecifiedParameter());
792 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000793 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
794 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000795 if (FPT->isVariadic())
796 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000797 }
798
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000799 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000800 return DBuilder.createSubroutineType(Unit, EltTypeArray);
801}
802
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000804/// As an optimization, return 0 if the access specifier equals the
805/// default for the containing type.
806static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
807 AccessSpecifier Default = clang::AS_none;
808 if (RD && RD->isClass())
809 Default = clang::AS_private;
810 else if (RD && (RD->isStruct() || RD->isUnion()))
811 Default = clang::AS_public;
812
813 if (Access == Default)
814 return 0;
815
Eric Christophere7b87e52014-10-26 23:40:33 +0000816 switch (Access) {
817 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000818 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000819 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000820 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000821 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000822 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000823 case clang::AS_none:
824 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000825 }
826 llvm_unreachable("unexpected access enumerator");
827}
Guy Benyei11169dd2012-12-18 14:30:41 +0000828
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000829llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000830 StringRef name, QualType type, uint64_t sizeInBitsOverride,
831 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000832 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
833 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000834
835 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000836 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 unsigned line = getLineNumber(loc);
838
David Majnemer34b57492014-07-30 01:30:47 +0000839 uint64_t SizeInBits = 0;
840 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000841 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000842 TypeInfo TI = CGM.getContext().getTypeInfo(type);
843 SizeInBits = TI.Width;
844 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000845
846 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000847 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000848 }
849
Adrian Prantl21361fb2014-08-29 22:44:27 +0000850 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000851 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
852 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000853}
854
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000855void CGDebugInfo::CollectRecordLambdaFields(
856 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000857 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000858 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
859 // has the name and the location of the variable so we should iterate over
860 // both concurrently.
861 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
862 RecordDecl::field_iterator Field = CXXDecl->field_begin();
863 unsigned fieldno = 0;
864 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000865 E = CXXDecl->captures_end();
866 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000867 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000868 if (C.capturesVariable()) {
869 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000870 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000871 StringRef VName = V->getName();
872 uint64_t SizeInBitsOverride = 0;
873 if (Field->isBitField()) {
874 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
875 assert(SizeInBitsOverride && "found named 0-width bitfield");
876 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000878 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
879 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
880 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000881 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000882 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000883 // TODO: Need to handle 'this' in some way by probably renaming the
884 // this of the lambda class and having a field member of 'this' or
885 // by using AT_object_pointer for the function and having that be
886 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000887 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000888 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000889 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000890 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000891 "this", type, 0, f->getLocation(), f->getAccess(),
892 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000893
894 elements.push_back(fieldType);
895 }
896 }
897}
898
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000899llvm::DIDerivedType *
900CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000901 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000902 // Create the descriptor for the static variable, with or without
903 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000904 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000905 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
906 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000907
Eric Christopher91a31902013-01-16 01:22:32 +0000908 unsigned LineNumber = getLineNumber(Var->getLocation());
909 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000910 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000911 if (Var->getInit()) {
912 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000913 if (Value) {
914 if (Value->isInt())
915 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
916 if (Value->isFloat())
917 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
918 }
Eric Christopher91a31902013-01-16 01:22:32 +0000919 }
920
Adrian Prantl21361fb2014-08-29 22:44:27 +0000921 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000922 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000923 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000924 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000925 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000926}
927
Eric Christophere7b87e52014-10-26 23:40:33 +0000928void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000929 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
930 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000931 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000932 StringRef name = field->getName();
933 QualType type = field->getType();
934
935 // Ignore unnamed fields unless they're anonymous structs/unions.
936 if (name.empty() && !type->isRecordType())
937 return;
938
939 uint64_t SizeInBitsOverride = 0;
940 if (field->isBitField()) {
941 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
942 assert(SizeInBitsOverride && "found named 0-width bitfield");
943 }
944
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000945 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000946 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
947 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000948
949 elements.push_back(fieldType);
950}
951
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000952void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000953 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000954 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000955 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
957
Eric Christopher91a31902013-01-16 01:22:32 +0000958 if (CXXDecl && CXXDecl->isLambda())
959 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
960 else {
961 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000962
Eric Christopher91a31902013-01-16 01:22:32 +0000963 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000964 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000965
Eric Christopher91a31902013-01-16 01:22:32 +0000966 // Static and non-static members should appear in the same order as
967 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000968 for (const auto *I : record->decls())
969 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000970 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000971 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000972 if (MI != StaticDataMemberCache.end()) {
973 assert(MI->second &&
974 "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000975 elements.push_back(cast<llvm::DIDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000976 } else {
977 auto Field = CreateRecordStaticField(V, RecordTy, record);
978 elements.push_back(Field);
979 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000980 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000981 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
982 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000983
984 // Bump field number for next field.
985 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000986 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000987 }
988}
989
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000990llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +0000991CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000992 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +0000993 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +0000994 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000995 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000996 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +0000997 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
998 Func, Unit);
999}
David Blaikie2aaf0652013-01-07 22:24:59 +00001000
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001001llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1002 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001003 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001004 llvm::DITypeRefArray Args(
1005 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001006 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001007 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001008
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001009 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001010
1011 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001012 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001013
David Blaikie2aaf0652013-01-07 22:24:59 +00001014 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001015 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001016 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1017 // Create pointer type directly in this case.
1018 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1019 QualType PointeeTy = ThisPtrTy->getPointeeType();
1020 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001021 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001022 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001023 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1024 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001025 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001026 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 // TODO: This and the artificial type below are misleading, the
1028 // types aren't artificial the argument is, but the current
1029 // metadata doesn't represent that.
1030 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1031 Elts.push_back(ThisPtrType);
1032 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001033 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001034 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1036 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001037 }
1038
1039 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001040 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1041 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001042
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001043 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001044
Adrian Prantl0630eb72013-12-18 21:48:18 +00001045 unsigned Flags = 0;
1046 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001047 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001048 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001049 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001050
1051 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001052}
1053
Eric Christopherb2a008c2013-05-16 00:45:12 +00001054/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001055/// inside a function.
1056static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1057 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1058 return isFunctionLocalClass(NRD);
1059 if (isa<FunctionDecl>(RD->getDeclContext()))
1060 return true;
1061 return false;
1062}
1063
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001064llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1065 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001066 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001067 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001068
Guy Benyei11169dd2012-12-18 14:30:41 +00001069 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001070 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001071
1072 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1073 // make sense to give a single ctor/dtor a linkage name.
1074 StringRef MethodLinkageName;
1075 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1076 MethodLinkageName = CGM.getMangledName(Method);
1077
1078 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001079 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001080 unsigned MethodLine = 0;
1081 if (!Method->isImplicit()) {
1082 MethodDefUnit = getOrCreateFile(Method->getLocation());
1083 MethodLine = getLineNumber(Method->getLocation());
1084 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001085
1086 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001087 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001088 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001089 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001090
Guy Benyei11169dd2012-12-18 14:30:41 +00001091 if (Method->isVirtual()) {
1092 if (Method->isPure())
1093 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1094 else
1095 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001096
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 // It doesn't make sense to give a virtual destructor a vtable index,
1098 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001099 // FIXME: Add proper support for debug info for virtual calls in
1100 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1101 // lookup if we have multiple or virtual inheritance.
1102 if (!isa<CXXDestructorDecl>(Method) &&
1103 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001104 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 ContainingType = RecordTy;
1106 }
1107
1108 unsigned Flags = 0;
1109 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001110 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001111 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1113 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001114 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001115 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001116 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001117 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001118 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001119 }
1120 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001121 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001122 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001123 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001124 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001125 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001126
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001127 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1128 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001129 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1130 MethodTy, /*isLocalToUnit=*/false,
1131 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001132 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001133
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001134 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001135
1136 return SP;
1137}
1138
Eric Christophere7b87e52014-10-26 23:40:33 +00001139void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001140 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1141 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001142
1143 // Since we want more than just the individual member decls if we
1144 // have templated functions iterate over every declaration to gather
1145 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001146 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001147 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1148 // If the member is implicit, don't add it to the member list. This avoids
1149 // the member being added to type units by LLVM, while still allowing it
1150 // to be emitted into the type declaration/reference inside the compile
1151 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001152 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001153 // FIXME: Handle Using(Shadow?)Decls here to create
1154 // DW_TAG_imported_declarations inside the class for base decls brought into
1155 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1156 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1157 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001158 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001159 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001160
1161 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1162 continue;
1163
David Blaikiefd580722014-10-06 05:18:55 +00001164 // Reuse the existing member function declaration if it exists.
1165 // It may be associated with the declaration of the type & should be
1166 // reused as we're building the definition.
1167 //
1168 // This situation can arise in the vtable-based debug info reduction where
1169 // implicit members are emitted in a non-vtable TU.
1170 auto MI = SPCache.find(Method->getCanonicalDecl());
1171 EltTys.push_back(MI == SPCache.end()
1172 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001173 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001175}
Guy Benyei11169dd2012-12-18 14:30:41 +00001176
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001177void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001178 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001179 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001180 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001181 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001182 unsigned BFlags = 0;
1183 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001184
Guy Benyei11169dd2012-12-18 14:30:41 +00001185 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001186 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001187
Aaron Ballman574705e2014-03-13 15:41:46 +00001188 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001189 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1190 // virtual base offset offset is -ve. The code generator emits dwarf
1191 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001192 BaseOffset = 0 - CGM.getItaniumVTableContext()
1193 .getVirtualBaseOffsetOffset(RD, Base)
1194 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001195 } else {
1196 // In the MS ABI, store the vbtable offset, which is analogous to the
1197 // vbase offset offset in Itanium.
1198 BaseOffset =
1199 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1200 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001201 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001202 } else
1203 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1204 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1205 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001206
Adrian Prantl21361fb2014-08-29 22:44:27 +00001207 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001208 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001209 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001210 EltTys.push_back(DTy);
1211 }
1212}
1213
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001215CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1216 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001218 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001219 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1220 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001221 StringRef Name;
1222 if (TPList)
1223 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001224 switch (TA.getKind()) {
1225 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001226 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001227 TemplateParams.push_back(
1228 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001229 } break;
1230 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001231 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001232 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1233 TheCU, Name, TTy,
1234 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001235 } break;
1236 case TemplateArgument::Declaration: {
1237 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001238 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001240 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001241 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001242 // Variable pointer template parameters have a value that is the address
1243 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001244 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001245 V = CGM.GetAddrOfGlobalVar(VD);
1246 // Member function pointers have special support for building them, though
1247 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001248 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001249 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001250 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001251 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001252 // Member data pointers have special handling too to compute the fixed
1253 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001254 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001255 // These five lines (& possibly the above member function pointer
1256 // handling) might be able to be refactored to use similar code in
1257 // CodeGenModule::getMemberPointerConstant
1258 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1259 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001260 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001261 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001262 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001263 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1264 TheCU, Name, TTy,
1265 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001266 } break;
1267 case TemplateArgument::NullPtr: {
1268 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001269 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001270 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001271 // Special case member data pointer null values since they're actually -1
1272 // instead of zero.
1273 if (const MemberPointerType *MPT =
1274 dyn_cast<MemberPointerType>(T.getTypePtr()))
1275 // But treat member function pointers as simple zero integers because
1276 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1277 // CodeGen grows handling for values of non-null member function
1278 // pointers then perhaps we could remove this special case and rely on
1279 // EmitNullMemberPointer for member function pointers.
1280 if (MPT->isMemberDataPointer())
1281 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1282 if (!V)
1283 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001284 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1285 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001286 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001287 case TemplateArgument::Template:
1288 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001289 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001290 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1291 break;
1292 case TemplateArgument::Pack:
1293 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1294 TheCU, Name, nullptr,
1295 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1296 break;
David Majnemer5559d472013-08-24 08:21:10 +00001297 case TemplateArgument::Expression: {
1298 const Expr *E = TA.getAsExpr();
1299 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001300 if (E->isGLValue())
1301 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001302 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001303 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001304 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001305 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1306 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001307 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001308 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001309 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001310 case TemplateArgument::Null:
1311 llvm_unreachable(
1312 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 }
1314 }
1315 return DBuilder.getOrCreateArray(TemplateParams);
1316}
1317
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001318llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001319CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001320 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001321 if (FD->getTemplatedKind() ==
1322 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001323 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1324 ->getTemplate()
1325 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001326 return CollectTemplateParams(
1327 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001329 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001330}
1331
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001332llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1333 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001334 // Always get the full list of parameters, not just the ones from
1335 // the specialization.
1336 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001337 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001338 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001339 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001340}
1341
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001342llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001343 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001344 return VTablePtrType;
1345
1346 ASTContext &Context = CGM.getContext();
1347
1348 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001349 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001350 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1351 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001353 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001354 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1356 return VTablePtrType;
1357}
1358
Guy Benyei11169dd2012-12-18 14:30:41 +00001359StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001360 // Copy the gdb compatible name on the side and use its reference.
1361 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001362}
1363
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001364void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001365 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001366 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1367
1368 // If there is a primary base then it will hold vtable info.
1369 if (RL.getPrimaryBase())
1370 return;
1371
1372 // If this class is not dynamic then there is not any vtable info to collect.
1373 if (!RD->isDynamicClass())
1374 return;
1375
1376 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001377 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001378 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001379 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 EltTys.push_back(VPTR);
1381}
1382
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001383llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001384 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001385 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001387 return T;
1388}
1389
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001390llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001391 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001392 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001393 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001394 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 return T;
1396}
1397
David Blaikie483a9da2014-05-06 18:35:21 +00001398void CGDebugInfo::completeType(const EnumDecl *ED) {
1399 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1400 return;
1401 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001402 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001403 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001404 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001405 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001406 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001407 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001408 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001409}
1410
David Blaikieb2e86eb2013-08-15 20:49:17 +00001411void CGDebugInfo::completeType(const RecordDecl *RD) {
1412 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1413 !CGM.getLangOpts().CPlusPlus)
1414 completeRequiredType(RD);
1415}
1416
1417void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001418 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1419 return;
1420
David Blaikie6943dea2013-08-20 01:28:15 +00001421 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1422 if (CXXDecl->isDynamicClass())
1423 return;
1424
David Blaikieb2e86eb2013-08-15 20:49:17 +00001425 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001427 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001428 completeClassData(RD);
1429}
1430
1431void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1432 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001433 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001434 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001435 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001436 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001437 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001438 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001439 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001440 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001441 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001442}
1443
David Blaikie0e716b42014-03-03 23:48:23 +00001444static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1445 CXXRecordDecl::method_iterator End) {
1446 for (; I != End; ++I)
1447 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001448 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1449 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001450 return true;
1451 return false;
1452}
1453
1454static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1455 const RecordDecl *RD,
1456 const LangOptions &LangOpts) {
1457 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1458 return false;
1459
1460 if (!LangOpts.CPlusPlus)
1461 return false;
1462
1463 if (!RD->isCompleteDefinitionRequired())
1464 return true;
1465
1466 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1467
1468 if (!CXXDecl)
1469 return false;
1470
1471 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1472 return true;
1473
1474 TemplateSpecializationKind Spec = TSK_Undeclared;
1475 if (const ClassTemplateSpecializationDecl *SD =
1476 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1477 Spec = SD->getSpecializationKind();
1478
1479 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1480 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1481 CXXDecl->method_end()))
1482 return true;
1483
1484 return false;
1485}
1486
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001487llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001488 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001489 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001490 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001491 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001492 T = getOrCreateRecordFwdDecl(
1493 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001494 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001495 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001496
David Blaikieb2e86eb2013-08-15 20:49:17 +00001497 return CreateTypeDefinition(Ty);
1498}
1499
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001500llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001501 RecordDecl *RD = Ty->getDecl();
1502
Guy Benyei11169dd2012-12-18 14:30:41 +00001503 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001504 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001505
1506 // Records and classes and unions can all be recursive. To handle them, we
1507 // first generate a debug descriptor for the struct as a forward declaration.
1508 // Then (if it is a definition) we go through and get debug info for all of
1509 // its members. Finally, we create a descriptor for the complete type (which
1510 // may refer to the forward decl if the struct is recursive) and replace all
1511 // uses of the forward declaration with the final definition.
1512
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001513 auto *FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001514 cast<llvm::DICompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001515
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001516 const RecordDecl *D = RD->getDefinition();
1517 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001518 return FwdDecl;
1519
David Blaikieadfbf992013-08-18 16:55:33 +00001520 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1521 CollectContainingType(CXXDecl, FwdDecl);
1522
Guy Benyei11169dd2012-12-18 14:30:41 +00001523 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001524 LexicalBlockStack.emplace_back(&*FwdDecl);
1525 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001526
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001528 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001529 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001530
1531 // Note: The split of CXXDecl information here is intentional, the
1532 // gdb tests will depend on a certain ordering at printout. The debug
1533 // information offsets are still correct if we merge them all together
1534 // though.
1535 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1536 if (CXXDecl) {
1537 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1538 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1539 }
1540
Eric Christopher91a31902013-01-16 01:22:32 +00001541 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001542 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001543 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001544 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001545
1546 LexicalBlockStack.pop_back();
1547 RegionMap.erase(Ty->getDecl());
1548
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001549 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001550 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001551
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001552 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001553 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001554 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001555
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001556 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001557 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001558}
1559
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001560llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1561 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001562 // Ignore protocols.
1563 return getOrCreateType(Ty->getBaseType(), Unit);
1564}
1565
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001566/// \return true if Getter has the default name for the property PD.
1567static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1568 const ObjCMethodDecl *Getter) {
1569 assert(PD);
1570 if (!Getter)
1571 return true;
1572
1573 assert(Getter->getDeclName().isObjCZeroArgSelector());
1574 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001575 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001576}
1577
1578/// \return true if Setter has the default name for the property PD.
1579static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1580 const ObjCMethodDecl *Setter) {
1581 assert(PD);
1582 if (!Setter)
1583 return true;
1584
1585 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001586 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001587 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001588}
1589
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001590llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1591 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 ObjCInterfaceDecl *ID = Ty->getDecl();
1593 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001594 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001595
1596 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001597 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001599 auto RuntimeLang =
1600 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001601
1602 // If this is just a forward declaration return a special forward-declaration
1603 // debug type since we won't be able to lay out the entire type.
1604 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001605 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001606 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001607 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1608 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001609 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001610 return FwdDecl;
1611 }
1612
David Blaikieef8a9512014-05-05 23:23:53 +00001613 return CreateTypeDefinition(Ty, Unit);
1614}
1615
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001616llvm::DIModule *
1617CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001618 auto it = ModuleRefCache.find(Mod.Signature);
1619 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001620 return it->second;
1621
1622 // Macro definitions that were defined with "-D" on the command line.
1623 SmallString<128> ConfigMacros;
1624 {
1625 llvm::raw_svector_ostream OS(ConfigMacros);
1626 const auto &PPOpts = CGM.getPreprocessorOpts();
1627 unsigned I = 0;
1628 // Translate the macro definitions back into a commmand line.
1629 for (auto &M : PPOpts.Macros) {
1630 if (++I > 1)
1631 OS << " ";
1632 const std::string &Macro = M.first;
1633 bool Undef = M.second;
1634 OS << "\"-" << (Undef ? 'U' : 'D');
1635 for (char c : Macro)
1636 switch (c) {
1637 case '\\' : OS << "\\\\"; break;
1638 case '"' : OS << "\\\""; break;
1639 default: OS << c;
1640 }
1641 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001642 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001643 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001644 llvm::DIBuilder DIB(CGM.getModule());
1645 auto *CU = DIB.createCompileUnit(
1646 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1647 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1648 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1649 llvm::DIModule *ModuleRef =
1650 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1651 internString(CGM.getHeaderSearchOpts().Sysroot));
1652 DIB.finalize();
1653 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001654 return ModuleRef;
1655}
1656
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001657llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1658 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001659 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001660 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001661 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001662 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001663
1664 // Bit size, align and offset of the type.
1665 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1666 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1667
1668 unsigned Flags = 0;
1669 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001670 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001671
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001672 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001673 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001674 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001675
David Blaikieef8a9512014-05-05 23:23:53 +00001676 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001677 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001679 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001680 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001681 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001682
1683 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001684 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
1686 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1687 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001688 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001689 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001690 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001691 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001692
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001693 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 EltTys.push_back(InhTag);
1695 }
1696
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001697 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001698 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001699 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001700 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 unsigned PLine = getLineNumber(Loc);
1702 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1703 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001704 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1705 PD->getName(), PUnit, PLine,
1706 hasDefaultGetterName(PD, Getter) ? ""
1707 : getSelectorName(PD->getGetterName()),
1708 hasDefaultSetterName(PD, Setter) ? ""
1709 : getSelectorName(PD->getSetterName()),
1710 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 EltTys.push_back(PropertyNode);
1712 }
1713
1714 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1715 unsigned FieldNo = 0;
1716 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1717 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001718 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001719 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001720 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001721
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 StringRef FieldName = Field->getName();
1723
1724 // Ignore unnamed fields.
1725 if (FieldName.empty())
1726 continue;
1727
1728 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001729 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 unsigned FieldLine = getLineNumber(Field->getLocation());
1731 QualType FType = Field->getType();
1732 uint64_t FieldSize = 0;
1733 unsigned FieldAlign = 0;
1734
1735 if (!FType->isIncompleteArrayType()) {
1736
1737 // Bit size, align and offset of the type.
1738 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001739 ? Field->getBitWidthValue(CGM.getContext())
1740 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001741 FieldAlign = CGM.getContext().getTypeAlign(FType);
1742 }
1743
1744 uint64_t FieldOffset;
1745 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1746 // We don't know the runtime offset of an ivar if we're using the
1747 // non-fragile ABI. For bitfields, use the bit offset into the first
1748 // byte of storage of the bitfield. For other fields, use zero.
1749 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001750 FieldOffset =
1751 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001752 FieldOffset %= CGM.getContext().getCharWidth();
1753 } else {
1754 FieldOffset = 0;
1755 }
1756 } else {
1757 FieldOffset = RL.getFieldOffset(FieldNo);
1758 }
1759
1760 unsigned Flags = 0;
1761 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001762 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001763 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001764 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001765 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001766 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001767
Craig Topper8a13c412014-05-21 05:09:00 +00001768 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001770 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001771 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001773 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001774 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001775 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1777 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001778 PropertyNode = DBuilder.createObjCProperty(
1779 PD->getName(), PUnit, PLine,
1780 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1781 PD->getGetterName()),
1782 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1783 PD->getSetterName()),
1784 PD->getPropertyAttributes(),
1785 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 }
1787 }
1788 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001789 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1790 FieldSize, FieldAlign, FieldOffset, Flags,
1791 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 EltTys.push_back(FieldTy);
1793 }
1794
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001795 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001796 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001797
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001799 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001800}
1801
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001802llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1803 llvm::DIFile *Unit) {
1804 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001805 int64_t Count = Ty->getNumElements();
1806 if (Count == 0)
1807 // If number of elements are not known then this is an unbounded array.
1808 // Use Count == -1 to express such arrays.
1809 Count = -1;
1810
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001811 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001812 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001813
1814 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1815 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1816
1817 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1818}
1819
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001820llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 uint64_t Size;
1822 uint64_t Align;
1823
1824 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1825 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1826 Size = 0;
1827 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001828 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 } else if (Ty->isIncompleteArrayType()) {
1830 Size = 0;
1831 if (Ty->getElementType()->isIncompleteType())
1832 Align = 0;
1833 else
1834 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001835 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 Size = 0;
1837 Align = 0;
1838 } else {
1839 // Size and align of the whole array, not the element type.
1840 Size = CGM.getContext().getTypeSize(Ty);
1841 Align = CGM.getContext().getTypeAlign(Ty);
1842 }
1843
1844 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1845 // interior arrays, do we care? Why aren't nested arrays represented the
1846 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001847 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 QualType EltTy(Ty, 0);
1849 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1850 // If the number of elements is known, then count is that number. Otherwise,
1851 // it's -1. This allows us to represent a subrange with an array of 0
1852 // elements, like this:
1853 //
1854 // struct foo {
1855 // int x[0];
1856 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001857 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001858 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1859 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001860
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 // FIXME: Verify this is right for VLAs.
1862 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1863 EltTy = Ty->getElementType();
1864 }
1865
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001866 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001867
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001868 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1869 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001870}
1871
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001872llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1873 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001874 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1875 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001876}
1877
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001878llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1879 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001880 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_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 MemberPointerType *Ty,
1885 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001886 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1887 ? CGM.getContext().getTypeSize(Ty)
1888 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001889 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001890 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001891 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001892 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001893
1894 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001895 Ty->getPointeeType()->getAs<FunctionProtoType>();
1896 return DBuilder.createMemberPointerType(
1897 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1898 Ty->getClass(), FPT->getTypeQuals())),
1899 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001900 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001901}
1902
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001903llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001904 // Ignore the atomic wrapping
1905 // FIXME: What is the correct representation?
1906 return getOrCreateType(Ty->getValueType(), U);
1907}
1908
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001909llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001910 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 uint64_t Size = 0;
1912 uint64_t Align = 0;
1913 if (!ED->getTypeForDecl()->isIncompleteType()) {
1914 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1915 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1916 }
1917
Manman Rene0064d82013-08-29 23:19:58 +00001918 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // If this is just a forward declaration, construct an appropriately
1921 // marked node and just return it.
1922 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001923 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001924 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001925 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 unsigned Line = getLineNumber(ED->getLocation());
1927 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001928 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001929 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001930 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001931 ReplaceMap.emplace_back(
1932 std::piecewise_construct, std::make_tuple(Ty),
1933 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001934 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 }
1936
David Blaikie483a9da2014-05-06 18:35:21 +00001937 return CreateTypeDefinition(Ty);
1938}
1939
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001940llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001941 const EnumDecl *ED = Ty->getDecl();
1942 uint64_t Size = 0;
1943 uint64_t Align = 0;
1944 if (!ED->getTypeForDecl()->isIncompleteType()) {
1945 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1946 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1947 }
1948
1949 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1950
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001951 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001952 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001953 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001954 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001955 Enumerators.push_back(DBuilder.createEnumerator(
1956 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001957 }
1958
1959 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001961
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001962 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001964 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001965 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001966 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001967 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1968 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1969 Line, Size, Align, EltArray, ClassTy,
1970 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001971}
1972
David Blaikie05491062013-01-21 04:37:12 +00001973static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1974 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001975 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001976 Qualifiers InnerQuals = T.getLocalQualifiers();
1977 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1978 // that is already there.
1979 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1980 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001981 QualType LastT = T;
1982 switch (T->getTypeClass()) {
1983 default:
David Blaikie05491062013-01-21 04:37:12 +00001984 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001985 case Type::TemplateSpecialization: {
1986 const auto *Spec = cast<TemplateSpecializationType>(T);
1987 if (Spec->isTypeAlias())
1988 return C.getQualifiedType(T.getTypePtr(), Quals);
1989 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001990 break;
1991 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 case Type::TypeOfExpr:
1993 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1994 break;
1995 case Type::TypeOf:
1996 T = cast<TypeOfType>(T)->getUnderlyingType();
1997 break;
1998 case Type::Decltype:
1999 T = cast<DecltypeType>(T)->getUnderlyingType();
2000 break;
2001 case Type::UnaryTransform:
2002 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2003 break;
2004 case Type::Attributed:
2005 T = cast<AttributedType>(T)->getEquivalentType();
2006 break;
2007 case Type::Elaborated:
2008 T = cast<ElaboratedType>(T)->getNamedType();
2009 break;
2010 case Type::Paren:
2011 T = cast<ParenType>(T)->getInnerType();
2012 break;
David Blaikie05491062013-01-21 04:37:12 +00002013 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002015 break;
2016 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002017 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002018 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002019 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 break;
2021 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002022
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002024 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002025 } while (true);
2026}
2027
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002028llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002029
2030 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002031 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002032
David Blaikief427b002014-05-06 03:42:01 +00002033 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 if (it != TypeCache.end()) {
2035 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002036 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002037 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 }
2039
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002040 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002041}
2042
David Blaikie0e716b42014-03-03 23:48:23 +00002043void CGDebugInfo::completeTemplateDefinition(
2044 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002045 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2046 return;
2047
David Blaikie0e716b42014-03-03 23:48:23 +00002048 completeClassData(&SD);
2049 // In case this type has no member function definitions being emitted, ensure
2050 // it is retained
2051 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2052}
2053
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002054llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002056 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002057
2058 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002059 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002060
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002061 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002062 return T;
2063
2064 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002065 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002066 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002067
2068 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002069 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002070
Guy Benyei11169dd2012-12-18 14:30:41 +00002071 return Res;
2072}
2073
Eric Christopher1ecc5632013-06-07 22:54:39 +00002074unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002075 // The assumption is that the number of ivars can only increase
2076 // monotonically, so it is safe to just use their current number as
2077 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002078 unsigned Sum = 0;
2079 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002080 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002081 ++Sum;
2082
2083 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002084}
2085
2086ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2087 switch (Ty->getTypeClass()) {
2088 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002089 return getObjCInterfaceDecl(
2090 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002091 case Type::ObjCInterface:
2092 return cast<ObjCInterfaceType>(Ty)->getDecl();
2093 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002094 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002095 }
2096}
2097
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002098llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002099 // Handle qualifiers, which recursively handles what they refer to.
2100 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002101 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002102
Guy Benyei11169dd2012-12-18 14:30:41 +00002103 // Work out details of type.
2104 switch (Ty->getTypeClass()) {
2105#define TYPE(Class, Base)
2106#define ABSTRACT_TYPE(Class, Base)
2107#define NON_CANONICAL_TYPE(Class, Base)
2108#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2109#include "clang/AST/TypeNodes.def"
2110 llvm_unreachable("Dependent types cannot show up in debug information");
2111
2112 case Type::ExtVector:
2113 case Type::Vector:
2114 return CreateType(cast<VectorType>(Ty), Unit);
2115 case Type::ObjCObjectPointer:
2116 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2117 case Type::ObjCObject:
2118 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2119 case Type::ObjCInterface:
2120 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2121 case Type::Builtin:
2122 return CreateType(cast<BuiltinType>(Ty));
2123 case Type::Complex:
2124 return CreateType(cast<ComplexType>(Ty));
2125 case Type::Pointer:
2126 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002127 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002128 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002129 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002130 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002131 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 case Type::BlockPointer:
2133 return CreateType(cast<BlockPointerType>(Ty), Unit);
2134 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002135 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002136 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002137 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002139 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002140 case Type::FunctionProto:
2141 case Type::FunctionNoProto:
2142 return CreateType(cast<FunctionType>(Ty), Unit);
2143 case Type::ConstantArray:
2144 case Type::VariableArray:
2145 case Type::IncompleteArray:
2146 return CreateType(cast<ArrayType>(Ty), Unit);
2147
2148 case Type::LValueReference:
2149 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2150 case Type::RValueReference:
2151 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2152
2153 case Type::MemberPointer:
2154 return CreateType(cast<MemberPointerType>(Ty), Unit);
2155
2156 case Type::Atomic:
2157 return CreateType(cast<AtomicType>(Ty), Unit);
2158
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002160 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2161
David Blaikie42edade2014-11-11 20:44:45 +00002162 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002163 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 case Type::Elaborated:
2165 case Type::Paren:
2166 case Type::SubstTemplateTypeParm:
2167 case Type::TypeOfExpr:
2168 case Type::TypeOf:
2169 case Type::Decltype:
2170 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002171 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002172 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002173 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002174
David Blaikie42edade2014-11-11 20:44:45 +00002175 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002176}
2177
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002178llvm::DIType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2179 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002180 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002181
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002182 auto *T = cast_or_null<llvm::DICompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002183
2184 // We may have cached a forward decl when we could have created
2185 // a non-forward decl. Go ahead and create a non-forward decl
2186 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002187 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002188 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002189
2190 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002191 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002192
2193 // Propagate members from the declaration to the definition
2194 // CreateType(const RecordType*) will overwrite this with the members in the
2195 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002196 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002197
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002199 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002200 return Res;
2201}
2202
2203// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002204llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002205 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002206
Guy Benyei11169dd2012-12-18 14:30:41 +00002207 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002208 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 unsigned Line = getLineNumber(RD->getLocation());
2210 StringRef RDName = getClassName(RD);
2211
David Blaikiebe822ed2015-07-01 18:07:22 +00002212 llvm::DIScope *RDContext =
2213 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002214
David Blaikied2785892013-08-18 17:36:19 +00002215 // If we ended up creating the type during the context chain construction,
2216 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002217 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002218 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002219 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002220 return T;
David Blaikied2785892013-08-18 17:36:19 +00002221
Adrian Prantl381e7552014-02-04 21:29:50 +00002222 // If this is just a forward or incomplete declaration, construct an
2223 // appropriately marked node and just return it.
2224 const RecordDecl *D = RD->getDefinition();
2225 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002226 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002227
2228 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2229 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002230
Manman Rene0064d82013-08-29 23:19:58 +00002231 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2232
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002233 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002234 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2235 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002236
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002237 RegionMap[Ty->getDecl()].reset(RealDecl);
2238 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002239
David Blaikieadfbf992013-08-18 16:55:33 +00002240 if (const ClassTemplateSpecializationDecl *TSpecial =
2241 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002242 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002243 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002244 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002245}
2246
David Blaikieadfbf992013-08-18 16:55:33 +00002247void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002248 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002249 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002250 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002251 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2252 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002253 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002254 while (1) {
2255 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2256 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2257 if (PBT && !BRL.isPrimaryBaseVirtual())
2258 PBase = PBT;
2259 else
2260 break;
2261 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002262 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002263 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2264 getOrCreateFile(RD->getLocation())));
2265 } else if (RD->isDynamicClass())
2266 ContainingType = RealDecl;
2267
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002268 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002269}
2270
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002271llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002272 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002273 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002274 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2275 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002276 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002277 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 *Offset += FieldSize;
2279 return Ty;
2280}
2281
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002282void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002283 StringRef &Name,
2284 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002285 llvm::DIScope *&FDContext,
2286 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002287 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002288 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2289 Name = getFunctionName(FD);
2290 // Use mangled name as linkage name for C/C++ functions.
2291 if (FD->hasPrototype()) {
2292 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002294 }
2295 // No need to replicate the linkage name if it isn't different from the
2296 // subprogram name, no need to have it at all unless coverage is enabled or
2297 // debug is set to more than just line tables.
2298 if (LinkageName == Name ||
2299 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2300 !CGM.getCodeGenOpts().EmitGcovNotes &&
2301 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2302 LinkageName = StringRef();
2303
2304 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2305 if (const NamespaceDecl *NSDecl =
2306 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2307 FDContext = getOrCreateNameSpace(NSDecl);
2308 else if (const RecordDecl *RDecl =
2309 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2310 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2311 // Collect template parameters.
2312 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2313 }
2314}
2315
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002316void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002317 unsigned &LineNo, QualType &T,
2318 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002319 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002320 Unit = getOrCreateFile(VD->getLocation());
2321 LineNo = getLineNumber(VD->getLocation());
2322
2323 setLocation(VD->getLocation());
2324
2325 T = VD->getType();
2326 if (T->isIncompleteArrayType()) {
2327 // CodeGen turns int[] into int[1] so we'll do the same here.
2328 llvm::APInt ConstVal(32, 1);
2329 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2330
2331 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2332 ArrayType::Normal, 0);
2333 }
2334
2335 Name = VD->getName();
2336 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2337 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2338 LinkageName = CGM.getMangledName(VD);
2339 if (LinkageName == Name)
2340 LinkageName = StringRef();
2341
2342 // Since we emit declarations (DW_AT_members) for static members, place the
2343 // definition of those static members in the namespace they were declared in
2344 // in the source code (the lexical decl context).
2345 // FIXME: Generalize this for even non-member global variables where the
2346 // declaration and definition may have different lexical decl contexts, once
2347 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002348 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2349 : VD->getDeclContext();
2350 // When a record type contains an in-line initialization of a static data
2351 // member, and the record type is marked as __declspec(dllexport), an implicit
2352 // definition of the member will be created in the record context. DWARF
2353 // doesn't seem to have a nice way to describe this in a form that consumers
2354 // are likely to understand, so fake the "normal" situation of a definition
2355 // outside the class by putting it in the global scope.
2356 if (DC->isRecord())
2357 DC = CGM.getContext().getTranslationUnitDecl();
David Blaikiebe822ed2015-07-01 18:07:22 +00002358 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002359}
2360
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002361llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002362CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002363 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002364 StringRef Name, LinkageName;
2365 unsigned Flags = 0;
2366 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002367 llvm::DIFile *Unit = getOrCreateFile(Loc);
2368 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002369 unsigned Line = getLineNumber(Loc);
2370
2371 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2372 TParamsArray, Flags);
2373 // Build function type.
2374 SmallVector<QualType, 16> ArgTypes;
2375 for (const ParmVarDecl *Parm: FD->parameters())
2376 ArgTypes.push_back(Parm->getType());
2377 QualType FnType =
2378 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2379 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002380 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002381 DContext, Name, LinkageName, Unit, Line,
2382 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2383 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2384 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002385 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002386 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2387 std::make_tuple(CanonDecl),
2388 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002389 return SP;
2390}
2391
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002392llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002393CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2394 QualType T;
2395 StringRef Name, LinkageName;
2396 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002397 llvm::DIFile *Unit = getOrCreateFile(Loc);
2398 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002399 unsigned Line = getLineNumber(Loc);
2400
2401 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002402 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2403 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2404 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002405 FwdDeclReplaceMap.emplace_back(
2406 std::piecewise_construct,
2407 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2408 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002409 return GV;
2410}
2411
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002412llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002413 // We only need a declaration (not a definition) of the type - so use whatever
2414 // we would otherwise do to get a type for a pointee. (forward declarations in
2415 // limited debug info, full definitions (if the type definition is available)
2416 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002417 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2418 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002419 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002420 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002421
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002422 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002423 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002424
2425 // No definition for now. Emit a forward definition that might be
2426 // merged with a potential upcoming definition.
2427 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2428 return getFunctionForwardDeclaration(FD);
2429 else if (const auto *VD = dyn_cast<VarDecl>(D))
2430 return getGlobalVariableForwardDeclaration(VD);
2431
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002432 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002433}
2434
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002435llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002436 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002437 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002438
Guy Benyei11169dd2012-12-18 14:30:41 +00002439 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002440 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002441 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002442
2443 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002444 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002445
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002446 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002447 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002448 if (const CXXMethodDecl *MD =
2449 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002450 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002451 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002452 }
2453 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002456 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002457 return SP;
2458 }
2459
Aaron Ballman86c93902014-03-06 23:45:36 +00002460 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002461 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002463 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002464 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002465 return SP;
2466 }
2467 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002468 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002469}
2470
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002471// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002472// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002473llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002474 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002475 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002476 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002477 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2478 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002479 return DBuilder.createSubroutineType(F,
2480 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002481
2482 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2483 return getOrCreateMethodType(Method, F);
2484 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2485 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002486 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002487
2488 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002489 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002490
2491 // Replace the instancetype keyword with the actual type.
2492 if (ResultTy == CGM.getContext().getObjCInstanceType())
2493 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002494 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002495
Adrian Prantl7bec9032013-05-10 21:08:31 +00002496 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002498 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002499 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002500 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002501 Elts.push_back(DBuilder.createArtificialType(
2502 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002504 for (const auto *PI : OMethod->params())
2505 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002506 // Variadic methods need a special marker at the end of the type list.
2507 if (OMethod->isVariadic())
2508 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002509
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002510 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002511 return DBuilder.createSubroutineType(F, EltTypeArray);
2512 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002513
Adrian Prantl800faef2014-02-25 23:42:18 +00002514 // Handle variadic function types; they need an additional
2515 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002516 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2517 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002518 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002519 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2520 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2521 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2522 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2523 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002524 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002525 return DBuilder.createSubroutineType(F, EltTypeArray);
2526 }
2527
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002528 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002529}
2530
Eric Christophere7b87e52014-10-26 23:40:33 +00002531void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2532 SourceLocation ScopeLoc, QualType FnType,
2533 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002534
2535 StringRef Name;
2536 StringRef LinkageName;
2537
2538 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2539
2540 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002541 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002542
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002544 llvm::DIFile *Unit = getOrCreateFile(Loc);
2545 llvm::DIScope *FDContext = Unit;
2546 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002547 if (!HasDecl) {
2548 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002549 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002551 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002552 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002554 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002555 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002556 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002557 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002558 return;
2559 }
2560 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002561 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2562 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2564 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002565 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 } else {
2567 // Use llvm function name.
2568 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002569 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 }
2571 if (!Name.empty() && Name[0] == '\01')
2572 Name = Name.substr(1);
2573
Adrian Prantl42d71b92014-04-10 23:21:53 +00002574 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002575 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002576 // Artificial functions without a location should not silently reuse CurLoc.
2577 if (Loc.isInvalid())
2578 CurLoc = SourceLocation();
2579 }
2580 unsigned LineNo = getLineNumber(Loc);
2581 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002582
Eric Christopher8018e412014-03-27 18:50:35 +00002583 // FIXME: The function declaration we're constructing here is mostly reusing
2584 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2585 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2586 // all subprograms instead of the actual context since subprogram definitions
2587 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002588 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002589 FDContext, Name, LinkageName, Unit, LineNo,
2590 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2591 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002592 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002593 // We might get here with a VarDecl in the case we're generating
2594 // code for the initialization of globals. Do not record these decls
2595 // as they will overwrite the actual VarDecl Decl in the cache.
2596 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002597 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002598
Adrian Prantlbebb8932014-03-21 21:01:58 +00002599 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002600 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002601
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002603 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002604}
2605
David Blaikie835afb22015-01-21 23:08:17 +00002606void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 // Update our current location
2608 setLocation(Loc);
2609
Eric Christophere7b87e52014-10-26 23:40:33 +00002610 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2611 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002612
Adrian Prantle83b1302014-01-07 22:05:52 +00002613 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002614 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002615 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002616}
2617
Guy Benyei11169dd2012-12-18 14:30:41 +00002618void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002619 llvm::MDNode *Back = nullptr;
2620 if (!LexicalBlockStack.empty())
2621 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002622 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002623 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002624 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002625}
2626
Eric Christopher0fdcb312013-05-16 00:52:20 +00002627void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2628 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002629 // Set our current location.
2630 setLocation(Loc);
2631
Guy Benyei11169dd2012-12-18 14:30:41 +00002632 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002633 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2634 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002635
2636 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2637 return;
2638
2639 // Create a new lexical block and push it on the stack.
2640 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002641}
2642
Eric Christopher0fdcb312013-05-16 00:52:20 +00002643void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2644 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002645 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2646
2647 // Provide an entry in the line table for the end of the block.
2648 EmitLocation(Builder, Loc);
2649
David Blaikie60a877b2014-10-22 19:34:33 +00002650 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2651 return;
2652
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 LexicalBlockStack.pop_back();
2654}
2655
Guy Benyei11169dd2012-12-18 14:30:41 +00002656void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2657 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2658 unsigned RCount = FnBeginRegionCount.back();
2659 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2660
2661 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002662 while (LexicalBlockStack.size() != RCount) {
2663 // Provide an entry in the line table for the end of the block.
2664 EmitLocation(Builder, CurLoc);
2665 LexicalBlockStack.pop_back();
2666 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 FnBeginRegionCount.pop_back();
2668}
2669
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002670llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002671 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002672
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002673 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 QualType FType;
2675 uint64_t FieldSize, FieldOffset;
2676 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002677
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002678 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002679 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002680
2681 FieldOffset = 0;
2682 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2683 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2684 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2685 FType = CGM.getContext().IntTy;
2686 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2687 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2688
2689 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2690 if (HasCopyAndDispose) {
2691 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002692 EltTys.push_back(
2693 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2694 EltTys.push_back(
2695 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002696 }
2697 bool HasByrefExtendedLayout;
2698 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002699 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2700 HasByrefExtendedLayout) &&
2701 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002702 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002703 EltTys.push_back(
2704 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002705 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002706
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2708 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002709 CGM.getTarget().getPointerAlign(0))) {
2710 CharUnits FieldOffsetInBytes =
2711 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2712 CharUnits AlignedOffsetInBytes =
2713 FieldOffsetInBytes.RoundUpToAlignment(Align);
2714 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002715
Guy Benyei11169dd2012-12-18 14:30:41 +00002716 if (NumPaddingBytes.isPositive()) {
2717 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2718 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2719 pad, ArrayType::Normal, 0);
2720 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2721 }
2722 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002723
Guy Benyei11169dd2012-12-18 14:30:41 +00002724 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002725 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 FieldSize = CGM.getContext().getTypeSize(FType);
2727 FieldAlign = CGM.getContext().toBits(Align);
2728
Eric Christopherb2a008c2013-05-16 00:45:12 +00002729 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002730 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2731 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002732 EltTys.push_back(FieldTy);
2733 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002734
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002735 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002736
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002737 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002738
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002740 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002741}
2742
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002743void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002744 llvm::Value *Storage, unsigned ArgNo,
2745 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002746 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2748
David Blaikie7fceebf2013-08-19 03:37:48 +00002749 bool Unwritten =
2750 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2751 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002752 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002753 if (!Unwritten)
2754 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002755 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 uint64_t XOffset = 0;
2757 if (VD->hasAttr<BlocksAttr>())
2758 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002759 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002760 Ty = getOrCreateType(VD->getType(), Unit);
2761
2762 // If there is no debug info for this type then do not emit debug info
2763 // for this variable.
2764 if (!Ty)
2765 return;
2766
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002768 unsigned Line = 0;
2769 unsigned Column = 0;
2770 if (!Unwritten) {
2771 Line = getLineNumber(VD->getLocation());
2772 Column = getColumnNumber(VD->getLocation());
2773 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002774 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 unsigned Flags = 0;
2776 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002777 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 // If this is the first argument and it is implicit then
2779 // give it an object pointer flag.
2780 // FIXME: There has to be a better way to do this, but for static
2781 // functions there won't be an implicit param at arg1 and
2782 // otherwise it is 'self' or 'this'.
2783 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002784 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002785 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002786 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2787 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002788 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002789
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002790 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002791
2792 StringRef Name = VD->getName();
2793 if (!Name.empty()) {
2794 if (VD->hasAttr<BlocksAttr>()) {
2795 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002796 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002797 // offset of __forwarding field
2798 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002799 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002800 Expr.push_back(offset.getQuantity());
2801 Expr.push_back(llvm::dwarf::DW_OP_deref);
2802 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002803 // offset of x field
2804 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002805 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002806
2807 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002808 auto *D = DBuilder.createLocalVariable(Tag, Scope, VD->getName(), Unit,
2809 Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002810
Guy Benyei11169dd2012-12-18 14:30:41 +00002811 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002812 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2813 llvm::DebugLoc::get(Line, Column, Scope),
2814 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002816 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002817 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002818 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2819 // If VD is an anonymous union then Storage represents value for
2820 // all union fields.
2821 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2822 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002823 // GDB has trouble finding local variables in anonymous unions, so we emit
2824 // artifical local variables for each of the members.
2825 //
2826 // FIXME: Remove this code as soon as GDB supports this.
2827 // The debug info verifier in LLVM operates based on the assumption that a
2828 // variable has the same size as its storage and we had to disable the check
2829 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002830 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002831 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002832 StringRef FieldName = Field->getName();
2833
2834 // Ignore unnamed fields. Do not ignore unnamed records.
2835 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2836 continue;
2837
2838 // Use VarDecl's Tag, Scope and Line number.
2839 auto *D = DBuilder.createLocalVariable(
2840 Tag, Scope, FieldName, Unit, Line, FieldTy,
Adrian Prantl00820ab2015-04-29 16:52:31 +00002841 CGM.getLangOpts().Optimize, Flags | llvm::DINode::FlagArtificial,
2842 ArgNo);
Adrian Prantl0d820892015-04-29 15:05:50 +00002843
2844 // Insert an llvm.dbg.declare into the current block.
2845 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2846 llvm::DebugLoc::get(Line, Column, Scope),
2847 Builder.GetInsertBlock());
2848 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002849 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002851
2852 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002853 auto *D =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002854 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2855 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002856
2857 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002858 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2859 llvm::DebugLoc::get(Line, Column, Scope),
2860 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002861}
2862
2863void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2864 llvm::Value *Storage,
2865 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002866 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002867 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2868}
2869
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002870llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2871 llvm::DIType *Ty) {
2872 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002873 if (CachedTy)
2874 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002875 return DBuilder.createObjectPointerType(Ty);
2876}
2877
Eric Christophere7b87e52014-10-26 23:40:33 +00002878void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2879 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002880 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002881 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002883
Craig Topper8a13c412014-05-21 05:09:00 +00002884 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002886
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002888
Guy Benyei11169dd2012-12-18 14:30:41 +00002889 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002890 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2891 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 if (isByRef)
2893 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002894 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002895 Ty = getOrCreateType(VD->getType(), Unit);
2896
2897 // Self is passed along as an implicit non-arg variable in a
2898 // block. Mark it as the object pointer.
2899 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002900 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002901
2902 // Get location information.
2903 unsigned Line = getLineNumber(VD->getLocation());
2904 unsigned Column = getColumnNumber(VD->getLocation());
2905
2906 const llvm::DataLayout &target = CGM.getDataLayout();
2907
2908 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002909 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2911
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002912 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002913 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002914 addr.push_back(llvm::dwarf::DW_OP_deref);
2915 addr.push_back(llvm::dwarf::DW_OP_plus);
2916 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002917 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002918 addr.push_back(llvm::dwarf::DW_OP_deref);
2919 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002920 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002921 offset =
2922 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002923 addr.push_back(offset.getQuantity());
2924 addr.push_back(llvm::dwarf::DW_OP_deref);
2925 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 // offset of x field
2927 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002928 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002929 }
2930
2931 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002932 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002933 llvm::dwarf::DW_TAG_auto_variable,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002934 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002935 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002936
Guy Benyei11169dd2012-12-18 14:30:41 +00002937 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002938 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2939 if (InsertPoint)
2940 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2941 InsertPoint);
2942 else
2943 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2944 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002945}
2946
Guy Benyei11169dd2012-12-18 14:30:41 +00002947void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2948 unsigned ArgNo,
2949 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002950 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2952}
2953
2954namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002955struct BlockLayoutChunk {
2956 uint64_t OffsetInBits;
2957 const BlockDecl::Capture *Capture;
2958};
2959bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2960 return l.OffsetInBits < r.OffsetInBits;
2961}
Guy Benyei11169dd2012-12-18 14:30:41 +00002962}
2963
2964void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002965 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002966 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002967 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002969 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 ASTContext &C = CGM.getContext();
2971 const BlockDecl *blockDecl = block.getBlockDecl();
2972
2973 // Collect some general information about the block's location.
2974 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002975 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 unsigned line = getLineNumber(loc);
2977 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002978
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 // Build the debug-info type for the block literal.
2980 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2981
2982 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002983 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00002984
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002985 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2987 blockLayout->getElementOffsetInBits(0),
2988 tunit, tunit));
2989 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2990 blockLayout->getElementOffsetInBits(1),
2991 tunit, tunit));
2992 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2993 blockLayout->getElementOffsetInBits(2),
2994 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00002995 auto *FnTy = block.getBlockExpr()->getFunctionType();
2996 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
2997 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00002998 blockLayout->getElementOffsetInBits(3),
2999 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003000 fields.push_back(createFieldType(
3001 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3002 ? C.getBlockDescriptorExtendedType()
3003 : C.getBlockDescriptorType()),
3004 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003005
3006 // We want to sort the captures by offset, not because DWARF
3007 // requires this, but because we're paranoid about debuggers.
3008 SmallVector<BlockLayoutChunk, 8> chunks;
3009
3010 // 'this' capture.
3011 if (blockDecl->capturesCXXThis()) {
3012 BlockLayoutChunk chunk;
3013 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003014 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003015 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003016 chunks.push_back(chunk);
3017 }
3018
3019 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003020 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003021 const VarDecl *variable = capture.getVariable();
3022 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3023
3024 // Ignore constant captures.
3025 if (captureInfo.isConstant())
3026 continue;
3027
3028 BlockLayoutChunk chunk;
3029 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003030 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 chunk.Capture = &capture;
3032 chunks.push_back(chunk);
3033 }
3034
3035 // Sort by offset.
3036 llvm::array_pod_sort(chunks.begin(), chunks.end());
3037
Eric Christophere7b87e52014-10-26 23:40:33 +00003038 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3039 e = chunks.end();
3040 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003041 uint64_t offsetInBits = i->OffsetInBits;
3042 const BlockDecl::Capture *capture = i->Capture;
3043
3044 // If we have a null capture, this must be the C++ 'this' capture.
3045 if (!capture) {
3046 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003047 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 QualType type = method->getThisType(C);
3049
3050 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3051 offsetInBits, tunit, tunit));
3052 continue;
3053 }
3054
3055 const VarDecl *variable = capture->getVariable();
3056 StringRef name = variable->getName();
3057
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003058 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003060 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003061
3062 // FIXME: this creates a second copy of this type!
3063 uint64_t xoffset;
3064 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003065 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3066 fieldType =
3067 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3068 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003069 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003070 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3071 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 }
3073 fields.push_back(fieldType);
3074 }
3075
3076 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003077 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3078 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003079
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003080 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003082 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003083 tunit, typeName.str(), tunit, line,
3084 CGM.getContext().toBits(block.BlockSize),
3085 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003086 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3087
3088 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003089 unsigned flags = llvm::DINode::FlagArtificial;
3090 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003091
3092 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003093 auto *debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003094 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3095 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003096
Adrian Prantl616bef42013-03-14 21:52:59 +00003097 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003098 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003099 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003100 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003101 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003102 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003103
Adrian Prantl616bef42013-03-14 21:52:59 +00003104 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003105 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3106 llvm::DebugLoc::get(line, column, scope),
3107 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003108}
3109
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003110llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003111CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3112 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003113 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003114
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003115 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003116 if (MI != StaticDataMemberCache.end()) {
3117 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003118 return cast<llvm::DIDerivedType>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003119 }
David Blaikiece763042013-08-20 21:49:21 +00003120
3121 // If the member wasn't found in the cache, lazily construct and add it to the
3122 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003123 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003124 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003125 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003126 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003127}
3128
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003129llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3130 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3131 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3132 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003133
3134 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003135 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003136 StringRef FieldName = Field->getName();
3137
3138 // Ignore unnamed fields, but recurse into anonymous records.
3139 if (FieldName.empty()) {
3140 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3141 if (RT)
3142 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3143 Var, DContext);
3144 continue;
3145 }
3146 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003147 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3148 LineNo, FieldTy,
3149 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003150 }
3151 return GV;
3152}
3153
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003154void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003156 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003158 llvm::DIFile *Unit = nullptr;
3159 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003160 unsigned LineNo;
3161 StringRef DeclName, LinkageName;
3162 QualType T;
3163 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003164
3165 // Attempt to store one global variable for the declaration - even if we
3166 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003167 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003168
3169 // If this is an anonymous union then we'll want to emit a global
3170 // variable for each member of the anonymous union so that it's possible
3171 // to find the name of any field in the union.
3172 if (T->isUnionType() && DeclName.empty()) {
3173 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003174 assert(RD->isAnonymousStructOrUnion() &&
3175 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003176 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3177 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003178 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003179 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3180 Var->hasInternalLinkage(), Var,
3181 getOrCreateStaticDataMemberDeclarationOrNull(D));
3182 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003183 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003184}
3185
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003186void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3187 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003188 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003189 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003190 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003191 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003192 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003193 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3194 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3195 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3196 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3197 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003198 // Do not use global variables for enums.
3199 //
3200 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003201 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003202 return;
David Blaikiea15565562014-04-04 20:56:17 +00003203 // Do not emit separate definitions for function local const/statics.
3204 if (isa<FunctionDecl>(VD->getDeclContext()))
3205 return;
David Blaikiebb113912014-04-05 07:23:17 +00003206 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003207 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003208 if (VarD->isStaticDataMember()) {
3209 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3210 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003211 // Ensure that the type is retained even though it's otherwise unreferenced.
3212 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003213 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003214 return;
3215 }
3216
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003217 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003218 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3219
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003220 auto &GV = DeclCache[VD];
3221 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003222 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003223 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003224 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003225 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003226}
3227
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003228llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003229 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003230 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003231 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003232}
3233
David Blaikie9f88fe82013-04-22 06:13:21 +00003234void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003235 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3236 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003237 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003238 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3239 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003240 getLineNumber(UD.getLocation()));
3241}
3242
David Blaikiebd483762013-05-20 04:58:53 +00003243void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3244 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3245 return;
3246 assert(UD.shadow_size() &&
3247 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3248 // Emitting one decl is sufficient - debuggers can detect that this is an
3249 // overloaded name & provide lookup for all the overloads.
3250 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003251 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003252 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003253 DBuilder.createImportedDeclaration(
3254 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3255 getLineNumber(USD.getLocation()));
3256}
3257
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003258void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3259 auto *Reader = CGM.getContext().getExternalSource();
3260 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3261 DBuilder.createImportedDeclaration(
3262 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3263 getOrCreateModuleRef(Info),
3264 getLineNumber(ID.getLocation()));
3265}
3266
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003267llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003268CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3269 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003270 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003271 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003272 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003273 return cast<llvm::DIImportedEntity>(VH);
3274 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003275 if (const NamespaceAliasDecl *Underlying =
3276 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3277 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003278 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003279 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3280 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3281 NA.getName());
3282 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003283 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003284 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3285 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3286 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003287 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003288 return R;
3289}
3290
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003291llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003292CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003293 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003294 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003295 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003296 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003297
Guy Benyei11169dd2012-12-18 14:30:41 +00003298 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003299 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3300 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003301 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003302 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003303 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003304 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003305 return NS;
3306}
3307
3308void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003309 // Creating types might create further types - invalidating the current
3310 // element and the size(), so don't cache/reference them.
3311 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3312 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003314 ? CreateTypeDefinition(E.Type, E.Unit)
3315 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003316 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003317 }
3318
David Blaikief427b002014-05-06 03:42:01 +00003319 for (auto p : ReplaceMap) {
3320 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003321 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003322 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003323
David Blaikief427b002014-05-06 03:42:01 +00003324 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003325 assert(it != TypeCache.end());
3326 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003327
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003328 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3329 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003330 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003331
Frederic Rissd253ed62014-11-18 03:40:51 +00003332 for (const auto &p : FwdDeclReplaceMap) {
3333 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003334 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003335 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003336
3337 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003338 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003339 // with ourselves, that will destroy the temporary MDNode and
3340 // replace it with a standard one, avoiding leaking memory.
3341 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003342 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003343 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003344 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003345
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003346 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003347 }
3348
Adrian Prantl73409ce2013-03-11 18:33:46 +00003349 // We keep our own list of retained types, because we need to look
3350 // up the final type in the type cache.
3351 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3352 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003353 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003354
Guy Benyei11169dd2012-12-18 14:30:41 +00003355 DBuilder.finalize();
3356}
David Blaikie66088d52014-09-24 17:01:27 +00003357
3358void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3359 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3360 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003361
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003362 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003363 // Don't ignore in case of explicit cast where it is referenced indirectly.
3364 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003365}