blob: 7368dc69f5175f4112a5a57ea232cc04d19e108e [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
109/// ArtificialLocation - An RAII object that temporarily switches to
110/// an artificial debug location that has a valid scope, but no line
Guy Benyei11169dd2012-12-18 14:30:41 +0000111void CGDebugInfo::setLocation(SourceLocation Loc) {
112 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000113 if (Loc.isInvalid())
114 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000115
116 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
117
118 // If we've changed files in the middle of a lexical scope go ahead
119 // and create a new lexical scope with file node if it's different
120 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000121 if (LexicalBlockStack.empty())
122 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000123
124 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000125 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000126 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000127
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000128 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000129 return;
130
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000131 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000132 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000133 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
134 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000135 } else if (isa<llvm::DILexicalBlock>(Scope) ||
136 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000137 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000138 LexicalBlockStack.emplace_back(
139 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000140 }
141}
142
143/// getContextDescriptor - Get context info for the decl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000144llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000145 if (!Context)
146 return TheCU;
147
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000148 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000149 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000150 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000151 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000152 }
153
154 // Check namespace.
155 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000156 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000157
David Blaikiebfa52742013-04-19 06:56:38 +0000158 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
159 if (!RDecl->isDependentType())
160 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000161 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 return TheCU;
163}
164
165/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000166/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000167/// is stored on the side.
168StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000169 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000170 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000171 FunctionTemplateSpecializationInfo *Info =
172 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000173 if (!Info && FII)
174 return FII->getName();
175
176 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000177 SmallString<128> NS;
178 llvm::raw_svector_ostream OS(NS);
179 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000180
181 // Add any template specialization args.
182 if (Info) {
183 const TemplateArgumentList *TArgs = Info->TemplateArguments;
184 const TemplateArgument *Args = TArgs->data();
185 unsigned NumArgs = TArgs->size();
186 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000187 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
188 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000189 }
190
191 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000192 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000193}
194
195StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
196 SmallString<256> MethodName;
197 llvm::raw_svector_ostream OS(MethodName);
198 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
199 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000200 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000201 dyn_cast<const ObjCImplementationDecl>(DC)) {
202 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000203 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000204 dyn_cast<const ObjCInterfaceDecl>(DC)) {
205 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000206 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000207 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
208 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
209 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000210 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000211 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000212 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000213 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000214 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000215 ClassTy.print(OS, PrintingPolicy(LangOptions()));
216 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000217 }
218 OS << ' ' << OMD->getSelector().getAsString() << ']';
219
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000220 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000221}
222
223/// getSelectorName - Return selector name. This is used for debugging
224/// info.
225StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000226 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000227}
228
229/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000230StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000231 // quick optimization to avoid having to intern strings that are already
232 // stored reliably elsewhere
233 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000234 return RD->getName();
235
David Blaikie65813a32014-04-02 18:21:09 +0000236 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000237 {
David Blaikie65813a32014-04-02 18:21:09 +0000238 llvm::raw_svector_ostream OS(Name);
239 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
240 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000241 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000242
243 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000244 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000245}
246
247/// getOrCreateFile - Get the file debug info descriptor for the input location.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000248llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000249 if (!Loc.isValid())
250 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000251 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000252
253 SourceManager &SM = CGM.getContext().getSourceManager();
254 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
255
256 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
257 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000258 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000259
260 // Cache the results.
261 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000262 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000263
264 if (it != DIFileCache.end()) {
265 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000266 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000267 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000268 }
269
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000270 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000271 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000272
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000273 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000274 return F;
275}
276
277/// getOrCreateMainFile - Get the file info for main compile unit.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000278llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000279 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000280}
281
282/// getLineNumber - Get line number for the location. If location is invalid
283/// then use current location.
284unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
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.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000290}
291
292/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000293unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000294 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000295 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000296 return 0;
297
298 // If the location is invalid then use the current column.
299 if (Loc.isInvalid() && CurLoc.isInvalid())
300 return 0;
301 SourceManager &SM = CGM.getContext().getSourceManager();
302 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000303 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000304}
305
306StringRef CGDebugInfo::getCurrentDirname() {
307 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
308 return CGM.getCodeGenOpts().DebugCompilationDir;
309
310 if (!CWDName.empty())
311 return CWDName;
312 SmallString<256> CWD;
313 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000314 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000315}
316
317/// CreateCompileUnit - Create new compile unit.
318void CGDebugInfo::CreateCompileUnit() {
319
David Blaikieaabde052014-05-14 00:29:00 +0000320 // Should we be asking the SourceManager for the main file name, instead of
321 // accepting it as an argument? This just causes the main file name to
322 // mismatch with source locations and create extra lexical scopes or
323 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
324 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
325 // because that's what the SourceManager says)
326
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 // Get absolute path name.
328 SourceManager &SM = CGM.getContext().getSourceManager();
329 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
330 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000331 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000332
333 // The main file name provided via the "-main-file-name" option contains just
334 // the file name itself with no path information. This file name may have had
335 // a relative path, so we look into the actual file entry for the main
336 // file to determine the real absolute path for the file.
337 std::string MainFileDir;
338 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
339 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000340 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000341 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
342 llvm::sys::path::append(MainFileDirSS, MainFileName);
343 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000344 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000345 }
346
347 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000348 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000349
350 // Save split dwarf file string.
351 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000352 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000353
Ed Masteda706022014-05-07 12:49:30 +0000354 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000355 const LangOptions &LO = CGM.getLangOpts();
356 if (LO.CPlusPlus) {
357 if (LO.ObjC1)
358 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
359 else
360 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
361 } else if (LO.ObjC1) {
362 LangTag = llvm::dwarf::DW_LANG_ObjC;
363 } else if (LO.C99) {
364 LangTag = llvm::dwarf::DW_LANG_C99;
365 } else {
366 LangTag = llvm::dwarf::DW_LANG_C89;
367 }
368
369 std::string Producer = getClangFullVersion();
370
371 // Figure out which version of the ObjC runtime we have.
372 unsigned RuntimeVers = 0;
373 if (LO.ObjC1)
374 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
375
376 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000377 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000378 TheCU = DBuilder.createCompileUnit(
379 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
380 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000381 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000382 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000383 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000384 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000385 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000386}
387
388/// CreateType - Get the Basic type from the cache or create a new
389/// one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000390llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000391 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000392 StringRef BTName;
393 switch (BT->getKind()) {
394#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000395#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000396#include "clang/AST/BuiltinTypes.def"
397 case BuiltinType::Dependent:
398 llvm_unreachable("Unexpected builtin type");
399 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000400 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000402 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000403 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000404 if (!ClassTy)
405 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
406 "objc_class", TheCU,
407 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000408 return ClassTy;
409 case BuiltinType::ObjCId: {
410 // typedef struct objc_class *Class;
411 // typedef struct objc_object {
412 // Class isa;
413 // } *id;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 return ObjTy;
417
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000418 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
420 "objc_class", TheCU,
421 getOrCreateMainFile(), 0);
422
423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000424
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000425 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000426
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000427 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000428 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000429 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000430
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000431 DBuilder.replaceArrays(
432 ObjTy,
433 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
434 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000435 return ObjTy;
436 }
437 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000438 if (!SelTy)
439 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
440 "objc_selector", TheCU,
441 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 return SelTy;
443 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444
445 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000446 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000448 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000449 OCLImage1dArrayDITy);
450 case BuiltinType::OCLImage1dBuffer:
451 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
452 OCLImage1dBufferDITy);
453 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000454 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000455 case BuiltinType::OCLImage2dArray:
456 return getOrCreateStructPtrType("opencl_image2d_array_t",
457 OCLImage2dArrayDITy);
458 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000460 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000461 return DBuilder.createBasicType(
462 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
463 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000464 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000466
Guy Benyei11169dd2012-12-18 14:30:41 +0000467 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000468 case BuiltinType::Char_U:
469 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000471 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000472 case BuiltinType::SChar:
473 Encoding = llvm::dwarf::DW_ATE_signed_char;
474 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000475 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000476 case BuiltinType::Char32:
477 Encoding = llvm::dwarf::DW_ATE_UTF;
478 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000479 case BuiltinType::UShort:
480 case BuiltinType::UInt:
481 case BuiltinType::UInt128:
482 case BuiltinType::ULong:
483 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000484 case BuiltinType::ULongLong:
485 Encoding = llvm::dwarf::DW_ATE_unsigned;
486 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000487 case BuiltinType::Short:
488 case BuiltinType::Int:
489 case BuiltinType::Int128:
490 case BuiltinType::Long:
491 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000492 case BuiltinType::LongLong:
493 Encoding = llvm::dwarf::DW_ATE_signed;
494 break;
495 case BuiltinType::Bool:
496 Encoding = llvm::dwarf::DW_ATE_boolean;
497 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000498 case BuiltinType::Half:
499 case BuiltinType::Float:
500 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000501 case BuiltinType::Double:
502 Encoding = llvm::dwarf::DW_ATE_float;
503 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000504 }
505
506 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000507 case BuiltinType::Long:
508 BTName = "long int";
509 break;
510 case BuiltinType::LongLong:
511 BTName = "long long int";
512 break;
513 case BuiltinType::ULong:
514 BTName = "long unsigned int";
515 break;
516 case BuiltinType::ULongLong:
517 BTName = "long long unsigned int";
518 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000519 default:
520 BTName = BT->getName(CGM.getLangOpts());
521 break;
522 }
523 // Bit size, align and offset of the type.
524 uint64_t Size = CGM.getContext().getTypeSize(BT);
525 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000526 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000527}
528
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000529llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000530 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000531 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000532 if (Ty->isComplexIntegerType())
533 Encoding = llvm::dwarf::DW_ATE_lo_user;
534
535 uint64_t Size = CGM.getContext().getTypeSize(Ty);
536 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000537 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000538}
539
540/// CreateCVRType - Get the qualified type from the cache or create
541/// a new one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000542llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
543 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000544 QualifierCollector Qc;
545 const Type *T = Qc.strip(Ty);
546
547 // Ignore these qualifiers for now.
548 Qc.removeObjCGCAttr();
549 Qc.removeAddressSpace();
550 Qc.removeObjCLifetime();
551
552 // We will create one Derived type for one qualifier and recurse to handle any
553 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000554 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 if (Qc.hasConst()) {
556 Tag = llvm::dwarf::DW_TAG_const_type;
557 Qc.removeConst();
558 } else if (Qc.hasVolatile()) {
559 Tag = llvm::dwarf::DW_TAG_volatile_type;
560 Qc.removeVolatile();
561 } else if (Qc.hasRestrict()) {
562 Tag = llvm::dwarf::DW_TAG_restrict_type;
563 Qc.removeRestrict();
564 } else {
565 assert(Qc.empty() && "Unknown type qualifier for debug info");
566 return getOrCreateType(QualType(T, 0), Unit);
567 }
568
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000569 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000570
571 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
572 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000573 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000574}
575
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000576llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
577 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000578
579 // The frontend treats 'id' as a typedef to an ObjCObjectType,
580 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
581 // debug info, we want to emit 'id' in both cases.
582 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000583 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000584
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000585 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
586 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000587}
588
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000589llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
590 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000591 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 Ty->getPointeeType(), Unit);
593}
594
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000595/// \return whether a C++ mangling exists for the type defined by TD.
596static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
597 switch (TheCU->getSourceLanguage()) {
598 case llvm::dwarf::DW_LANG_C_plus_plus:
599 return true;
600 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
601 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
602 default:
603 return false;
604 }
605}
606
Manman Rene0064d82013-08-29 23:19:58 +0000607/// In C++ mode, types have linkage, so we can rely on the ODR and
608/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000609static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
610 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000611 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000612 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000613 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000614
615 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000616 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000617
Manman Rene0064d82013-08-29 23:19:58 +0000618 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
619 if (CGM.getTarget().getCXXABI().isMicrosoft())
620 return FullName;
621
622 // TODO: This is using the RTTI name. Is there a better way to get
623 // a unique string for a type?
624 llvm::raw_svector_ostream Out(FullName);
625 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
626 Out.flush();
627 return FullName;
628}
629
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000630static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
631 llvm::dwarf::Tag Tag;
632 if (RD->isStruct() || RD->isInterface())
633 Tag = llvm::dwarf::DW_TAG_structure_type;
634 else if (RD->isUnion())
635 Tag = llvm::dwarf::DW_TAG_union_type;
636 else {
637 // FIXME: This could be a struct type giving a default visibility different
638 // than C++ class type, but needs llvm metadata changes first.
639 assert(RD->isClass());
640 Tag = llvm::dwarf::DW_TAG_class_type;
641 }
642 return Tag;
643}
644
Guy Benyei11169dd2012-12-18 14:30:41 +0000645// Creates a forward declaration for a RecordDecl in the given context.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000646llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000647CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000648 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000649 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000650 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
651 return cast<llvm::DICompositeType>(T);
652 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000653 unsigned Line = getLineNumber(RD->getLocation());
654 StringRef RDName = getClassName(RD);
655
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000656 uint64_t Size = 0;
657 uint64_t Align = 0;
658
659 const RecordDecl *D = RD->getDefinition();
660 if (D && D->isCompleteDefinition()) {
661 Size = CGM.getContext().getTypeSize(Ty);
662 Align = CGM.getContext().getTypeAlign(Ty);
663 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000664
665 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000666 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000667 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000668 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000669 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000670 ReplaceMap.emplace_back(
671 std::piecewise_construct, std::make_tuple(Ty),
672 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000673 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000674}
675
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000676llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000677 const Type *Ty,
678 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000679 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000680 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
681 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000682 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000683
Guy Benyei11169dd2012-12-18 14:30:41 +0000684 // Bit size, align and offset of the type.
685 // Size is always the size of a pointer. We can't use getTypeSize here
686 // because that does not return the correct value for references.
687 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000688 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
690
David Blaikie99dab3b2013-09-04 22:03:57 +0000691 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
692 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000693}
694
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000695llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
696 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000697 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000698 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000699 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
700 TheCU, getOrCreateMainFile(), 0);
701 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
702 Cache = DBuilder.createPointerType(Cache, Size);
703 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000704}
705
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000706llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
707 llvm::DIFile *Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000708 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000709 return BlockLiteralGeneric;
710
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000711 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 QualType FType;
713 uint64_t FieldSize, FieldOffset;
714 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000715 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000716
717 FieldOffset = 0;
718 FType = CGM.getContext().UnsignedLongTy;
719 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
720 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
721
722 Elements = DBuilder.getOrCreateArray(EltTys);
723 EltTys.clear();
724
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000725 unsigned Flags = llvm::DINode::FlagAppleBlock;
Guy Benyei11169dd2012-12-18 14:30:41 +0000726 unsigned LineNo = getLineNumber(CurLoc);
727
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000728 auto *EltTy =
729 DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
730 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000731
732 // Bit size, align and offset of the type.
733 uint64_t Size = CGM.getContext().getTypeSize(Ty);
734
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000735 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000736
737 FieldOffset = 0;
738 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
739 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
740 FType = CGM.getContext().IntTy;
741 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
742 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000743 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000744 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
745
746 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000747 FieldSize = CGM.getContext().getTypeSize(Ty);
748 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000749 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo,
750 FieldSize, FieldAlign, FieldOffset,
751 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000752
753 FieldOffset += FieldSize;
754 Elements = DBuilder.getOrCreateArray(EltTys);
755
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000756 EltTy =
757 DBuilder.createStructType(Unit, "__block_literal_generic", Unit, LineNo,
758 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000759
Guy Benyei11169dd2012-12-18 14:30:41 +0000760 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
761 return BlockLiteralGeneric;
762}
763
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000764llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
765 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000766 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000767 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000768
769 SmallString<128> NS;
770 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000771 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
772 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000773
774 TemplateSpecializationType::PrintTemplateArgumentList(
775 OS, Ty->getArgs(), Ty->getNumArgs(),
776 CGM.getContext().getPrintingPolicy());
777
Eric Christophere7b87e52014-10-26 23:40:33 +0000778 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
779 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000780
781 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000782 return DBuilder.createTypedef(
783 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
784 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000785}
786
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000787llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
788 llvm::DIFile *Unit) {
Michael Kupersteindef554d2015-07-01 12:34:39 +0000789 TypedefNameDecl *TD = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +0000790 // We don't set size information, but do specify where the typedef was
791 // declared.
Michael Kupersteindef554d2015-07-01 12:34:39 +0000792 SourceLocation Loc = TD->getLocation();
793
794 llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0));
Eric Christopherb2a008c2013-05-16 00:45:12 +0000795
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000796 // Typedefs are derived from some other type.
797 return DBuilder.createTypedef(
798 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
799 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
Michael Kupersteindef554d2015-07-01 12:34:39 +0000800 TDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +0000801}
802
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000803llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
804 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000805 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000806
807 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000808 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000809
810 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000811 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000812 if (isa<FunctionNoProtoType>(Ty))
813 EltTys.push_back(DBuilder.createUnspecifiedParameter());
814 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000815 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
816 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000817 if (FPT->isVariadic())
818 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 }
820
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000821 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000822 return DBuilder.createSubroutineType(Unit, EltTypeArray);
823}
824
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000825/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000826/// As an optimization, return 0 if the access specifier equals the
827/// default for the containing type.
828static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
829 AccessSpecifier Default = clang::AS_none;
830 if (RD && RD->isClass())
831 Default = clang::AS_private;
832 else if (RD && (RD->isStruct() || RD->isUnion()))
833 Default = clang::AS_public;
834
835 if (Access == Default)
836 return 0;
837
Eric Christophere7b87e52014-10-26 23:40:33 +0000838 switch (Access) {
839 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000840 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000841 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000842 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000843 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000844 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000845 case clang::AS_none:
846 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000847 }
848 llvm_unreachable("unexpected access enumerator");
849}
Guy Benyei11169dd2012-12-18 14:30:41 +0000850
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000851llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000852 StringRef name, QualType type, uint64_t sizeInBitsOverride,
853 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000854 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
855 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000856
857 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000858 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 unsigned line = getLineNumber(loc);
860
David Majnemer34b57492014-07-30 01:30:47 +0000861 uint64_t SizeInBits = 0;
862 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000863 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000864 TypeInfo TI = CGM.getContext().getTypeInfo(type);
865 SizeInBits = TI.Width;
866 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000867
868 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000869 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000870 }
871
Adrian Prantl21361fb2014-08-29 22:44:27 +0000872 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000873 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
874 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000875}
876
Eric Christopher91a31902013-01-16 01:22:32 +0000877/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000878void CGDebugInfo::CollectRecordLambdaFields(
879 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000880 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000881 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
882 // has the name and the location of the variable so we should iterate over
883 // both concurrently.
884 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
885 RecordDecl::field_iterator Field = CXXDecl->field_begin();
886 unsigned fieldno = 0;
887 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000888 E = CXXDecl->captures_end();
889 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000890 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000891 if (C.capturesVariable()) {
892 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000893 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000894 StringRef VName = V->getName();
895 uint64_t SizeInBitsOverride = 0;
896 if (Field->isBitField()) {
897 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
898 assert(SizeInBitsOverride && "found named 0-width bitfield");
899 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000900 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000901 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
902 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
903 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000904 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000905 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000906 // TODO: Need to handle 'this' in some way by probably renaming the
907 // this of the lambda class and having a field member of 'this' or
908 // by using AT_object_pointer for the function and having that be
909 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000910 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000911 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000912 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000913 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000914 "this", type, 0, f->getLocation(), f->getAccess(),
915 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000916
917 elements.push_back(fieldType);
918 }
919 }
920}
921
David Blaikie6943dea2013-08-20 01:28:15 +0000922/// Helper for CollectRecordFields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000923llvm::DIDerivedType *
924CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000925 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000926 // Create the descriptor for the static variable, with or without
927 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000928 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000929 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
930 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000931
Eric Christopher91a31902013-01-16 01:22:32 +0000932 unsigned LineNumber = getLineNumber(Var->getLocation());
933 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000934 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000935 if (Var->getInit()) {
936 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000937 if (Value) {
938 if (Value->isInt())
939 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
940 if (Value->isFloat())
941 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
942 }
Eric Christopher91a31902013-01-16 01:22:32 +0000943 }
944
Adrian Prantl21361fb2014-08-29 22:44:27 +0000945 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000946 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000947 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000948 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000949 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000950}
951
952/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000953void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000954 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
955 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000956 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000957 StringRef name = field->getName();
958 QualType type = field->getType();
959
960 // Ignore unnamed fields unless they're anonymous structs/unions.
961 if (name.empty() && !type->isRecordType())
962 return;
963
964 uint64_t SizeInBitsOverride = 0;
965 if (field->isBitField()) {
966 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
967 assert(SizeInBitsOverride && "found named 0-width bitfield");
968 }
969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000970 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000971 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
972 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000973
974 elements.push_back(fieldType);
975}
976
Guy Benyei11169dd2012-12-18 14:30:41 +0000977/// CollectRecordFields - A helper function to collect debug info for
978/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000979void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000980 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000981 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000982 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000983 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
984
Eric Christopher91a31902013-01-16 01:22:32 +0000985 if (CXXDecl && CXXDecl->isLambda())
986 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
987 else {
988 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000989
Eric Christopher91a31902013-01-16 01:22:32 +0000990 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000991 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000992
Eric Christopher91a31902013-01-16 01:22:32 +0000993 // Static and non-static members should appear in the same order as
994 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000995 for (const auto *I : record->decls())
996 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000997 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000998 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000999 if (MI != StaticDataMemberCache.end()) {
1000 assert(MI->second &&
1001 "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001002 elements.push_back(cast<llvm::DIDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +00001003 } else {
1004 auto Field = CreateRecordStaticField(V, RecordTy, record);
1005 elements.push_back(Field);
1006 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001007 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001008 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1009 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001010
1011 // Bump field number for next field.
1012 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001013 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001014 }
1015}
1016
1017/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1018/// function type is not updated to include implicit "this" pointer. Use this
1019/// routine to get a method type which includes "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001020llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001021CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001022 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001023 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001024 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001025 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001026 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001027 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1028 Func, Unit);
1029}
David Blaikie2aaf0652013-01-07 22:24:59 +00001030
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1032 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001033 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001034 llvm::DITypeRefArray Args(
1035 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001036 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001037 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001038
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001039 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001040
1041 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001042 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001043
David Blaikie2aaf0652013-01-07 22:24:59 +00001044 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001045 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001046 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1047 // Create pointer type directly in this case.
1048 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1049 QualType PointeeTy = ThisPtrTy->getPointeeType();
1050 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001051 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001052 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001053 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1054 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001055 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001056 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001057 // TODO: This and the artificial type below are misleading, the
1058 // types aren't artificial the argument is, but the current
1059 // metadata doesn't represent that.
1060 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1061 Elts.push_back(ThisPtrType);
1062 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001063 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001064 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001065 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1066 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001067 }
1068
1069 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001070 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1071 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001073 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001074
Adrian Prantl0630eb72013-12-18 21:48:18 +00001075 unsigned Flags = 0;
1076 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001077 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001078 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001079 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001080
1081 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001082}
1083
Eric Christopherb2a008c2013-05-16 00:45:12 +00001084/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001085/// inside a function.
1086static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1087 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1088 return isFunctionLocalClass(NRD);
1089 if (isa<FunctionDecl>(RD->getDeclContext()))
1090 return true;
1091 return false;
1092}
1093
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001094/// CreateCXXMemberFunction - A helper function to create a subprogram for
Guy Benyei11169dd2012-12-18 14:30:41 +00001095/// a single member function GlobalDecl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001096llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1097 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001098 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001099 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001100
Guy Benyei11169dd2012-12-18 14:30:41 +00001101 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001102 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001103
1104 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1105 // make sense to give a single ctor/dtor a linkage name.
1106 StringRef MethodLinkageName;
1107 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1108 MethodLinkageName = CGM.getMangledName(Method);
1109
1110 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001111 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001112 unsigned MethodLine = 0;
1113 if (!Method->isImplicit()) {
1114 MethodDefUnit = getOrCreateFile(Method->getLocation());
1115 MethodLine = getLineNumber(Method->getLocation());
1116 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001117
1118 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001119 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001120 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001121 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001122
Guy Benyei11169dd2012-12-18 14:30:41 +00001123 if (Method->isVirtual()) {
1124 if (Method->isPure())
1125 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1126 else
1127 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001128
Guy Benyei11169dd2012-12-18 14:30:41 +00001129 // It doesn't make sense to give a virtual destructor a vtable index,
1130 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001131 // FIXME: Add proper support for debug info for virtual calls in
1132 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1133 // lookup if we have multiple or virtual inheritance.
1134 if (!isa<CXXDestructorDecl>(Method) &&
1135 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001136 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001137 ContainingType = RecordTy;
1138 }
1139
1140 unsigned Flags = 0;
1141 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001142 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001143 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1145 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001146 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001147 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001148 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001151 }
1152 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001153 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001154 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001155 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001156 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001157 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001158
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001159 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1160 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001161 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1162 MethodTy, /*isLocalToUnit=*/false,
1163 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001164 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001165
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001166 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001167
1168 return SP;
1169}
1170
1171/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001172/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001173/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001174void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001175 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1176 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001177
1178 // Since we want more than just the individual member decls if we
1179 // have templated functions iterate over every declaration to gather
1180 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001181 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001182 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1183 // If the member is implicit, don't add it to the member list. This avoids
1184 // the member being added to type units by LLVM, while still allowing it
1185 // to be emitted into the type declaration/reference inside the compile
1186 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001187 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001188 // FIXME: Handle Using(Shadow?)Decls here to create
1189 // DW_TAG_imported_declarations inside the class for base decls brought into
1190 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1191 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1192 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001193 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001194 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001195
1196 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1197 continue;
1198
David Blaikiefd580722014-10-06 05:18:55 +00001199 // Reuse the existing member function declaration if it exists.
1200 // It may be associated with the declaration of the type & should be
1201 // reused as we're building the definition.
1202 //
1203 // This situation can arise in the vtable-based debug info reduction where
1204 // implicit members are emitted in a non-vtable TU.
1205 auto MI = SPCache.find(Method->getCanonicalDecl());
1206 EltTys.push_back(MI == SPCache.end()
1207 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001208 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001209 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001210}
Guy Benyei11169dd2012-12-18 14:30:41 +00001211
Guy Benyei11169dd2012-12-18 14:30:41 +00001212/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001213/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001214/// a Record.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001215void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001216 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001217 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001219 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001220 unsigned BFlags = 0;
1221 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001222
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001224 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001225
Aaron Ballman574705e2014-03-13 15:41:46 +00001226 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001227 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1228 // virtual base offset offset is -ve. The code generator emits dwarf
1229 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001230 BaseOffset = 0 - CGM.getItaniumVTableContext()
1231 .getVirtualBaseOffsetOffset(RD, Base)
1232 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001233 } else {
1234 // In the MS ABI, store the vbtable offset, which is analogous to the
1235 // vbase offset offset in Itanium.
1236 BaseOffset =
1237 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1238 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001239 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 } else
1241 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1242 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1243 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001244
Adrian Prantl21361fb2014-08-29 22:44:27 +00001245 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001246 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001247 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 EltTys.push_back(DTy);
1249 }
1250}
1251
1252/// CollectTemplateParams - A helper function to collect template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001253llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001254CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1255 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001256 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001257 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001258 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1259 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001260 StringRef Name;
1261 if (TPList)
1262 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001263 switch (TA.getKind()) {
1264 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001266 TemplateParams.push_back(
1267 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001268 } break;
1269 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001270 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001271 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1272 TheCU, Name, TTy,
1273 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001274 } break;
1275 case TemplateArgument::Declaration: {
1276 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001277 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001278 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001279 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001280 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001281 // Variable pointer template parameters have a value that is the address
1282 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001283 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001284 V = CGM.GetAddrOfGlobalVar(VD);
1285 // Member function pointers have special support for building them, though
1286 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001287 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001288 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001289 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001290 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001291 // Member data pointers have special handling too to compute the fixed
1292 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001293 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001294 // These five lines (& possibly the above member function pointer
1295 // handling) might be able to be refactored to use similar code in
1296 // CodeGenModule::getMemberPointerConstant
1297 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1298 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001299 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001300 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001301 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001302 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1303 TheCU, Name, TTy,
1304 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001305 } break;
1306 case TemplateArgument::NullPtr: {
1307 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001308 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001309 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001310 // Special case member data pointer null values since they're actually -1
1311 // instead of zero.
1312 if (const MemberPointerType *MPT =
1313 dyn_cast<MemberPointerType>(T.getTypePtr()))
1314 // But treat member function pointers as simple zero integers because
1315 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1316 // CodeGen grows handling for values of non-null member function
1317 // pointers then perhaps we could remove this special case and rely on
1318 // EmitNullMemberPointer for member function pointers.
1319 if (MPT->isMemberDataPointer())
1320 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1321 if (!V)
1322 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001323 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1324 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001325 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001326 case TemplateArgument::Template:
1327 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001328 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001329 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1330 break;
1331 case TemplateArgument::Pack:
1332 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1333 TheCU, Name, nullptr,
1334 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1335 break;
David Majnemer5559d472013-08-24 08:21:10 +00001336 case TemplateArgument::Expression: {
1337 const Expr *E = TA.getAsExpr();
1338 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001339 if (E->isGLValue())
1340 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001341 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001342 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001343 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001344 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1345 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001346 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001347 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001348 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001349 case TemplateArgument::Null:
1350 llvm_unreachable(
1351 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001352 }
1353 }
1354 return DBuilder.getOrCreateArray(TemplateParams);
1355}
1356
1357/// CollectFunctionTemplateParams - A helper function to collect debug
1358/// info for function template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001359llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001360CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001361 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 if (FD->getTemplatedKind() ==
1363 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001364 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1365 ->getTemplate()
1366 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001367 return CollectTemplateParams(
1368 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001370 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001371}
1372
1373/// CollectCXXTemplateParams - A helper function to collect debug info for
1374/// template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001375llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1376 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001377 // Always get the full list of parameters, not just the ones from
1378 // the specialization.
1379 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001380 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001381 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001382 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001383}
1384
1385/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001386llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001387 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001388 return VTablePtrType;
1389
1390 ASTContext &Context = CGM.getContext();
1391
1392 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001393 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001394 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1395 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001396 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001397 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001398 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001399 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1400 return VTablePtrType;
1401}
1402
1403/// getVTableName - Get vtable name for the given Class.
1404StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001405 // Copy the gdb compatible name on the side and use its reference.
1406 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001407}
1408
Guy Benyei11169dd2012-12-18 14:30:41 +00001409/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1410/// debug info entry in EltTys vector.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001411void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001412 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001413 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1414
1415 // If there is a primary base then it will hold vtable info.
1416 if (RL.getPrimaryBase())
1417 return;
1418
1419 // If this class is not dynamic then there is not any vtable info to collect.
1420 if (!RD->isDynamicClass())
1421 return;
1422
1423 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001424 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001425 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001427 EltTys.push_back(VPTR);
1428}
1429
Eric Christopherb2a008c2013-05-16 00:45:12 +00001430/// getOrCreateRecordType - Emit record type's standalone debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001431llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001432 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001433 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001434 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001435 return T;
1436}
1437
1438/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1439/// debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001440llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001441 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001442 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001443 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001444 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 return T;
1446}
1447
Michael Kupersteindef554d2015-07-01 12:34:39 +00001448void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {
1449 assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&
1450 "D is already mapped to lexical block scope");
1451 if (!LexicalBlockStack.empty())
1452 LexicalBlockMap[&D] = LexicalBlockStack.back();
1453}
1454
1455llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D,
1456 QualType Ty) {
1457 auto I = LexicalBlockMap.find(&D);
1458 if (I != LexicalBlockMap.end()) {
1459 RetainedTypes.push_back(Ty.getAsOpaquePtr());
1460 return I->second;
1461 } else
1462 return getContextDescriptor(cast<Decl>(D.getDeclContext()));
1463}
1464
David Blaikie483a9da2014-05-06 18:35:21 +00001465void CGDebugInfo::completeType(const EnumDecl *ED) {
1466 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1467 return;
1468 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001469 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001470 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001471 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001472 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001473 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001474 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001475 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001476}
1477
David Blaikieb2e86eb2013-08-15 20:49:17 +00001478void CGDebugInfo::completeType(const RecordDecl *RD) {
1479 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1480 !CGM.getLangOpts().CPlusPlus)
1481 completeRequiredType(RD);
1482}
1483
1484void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001485 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1486 return;
1487
David Blaikie6943dea2013-08-20 01:28:15 +00001488 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1489 if (CXXDecl->isDynamicClass())
1490 return;
1491
David Blaikieb2e86eb2013-08-15 20:49:17 +00001492 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001493 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001494 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001495 completeClassData(RD);
1496}
1497
1498void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1499 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001500 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001501 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001502 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001503 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001504 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001505 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001506 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001507 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001508 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001509}
1510
David Blaikie0e716b42014-03-03 23:48:23 +00001511static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1512 CXXRecordDecl::method_iterator End) {
1513 for (; I != End; ++I)
1514 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001515 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1516 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001517 return true;
1518 return false;
1519}
1520
1521static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1522 const RecordDecl *RD,
1523 const LangOptions &LangOpts) {
1524 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1525 return false;
1526
1527 if (!LangOpts.CPlusPlus)
1528 return false;
1529
1530 if (!RD->isCompleteDefinitionRequired())
1531 return true;
1532
1533 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1534
1535 if (!CXXDecl)
1536 return false;
1537
1538 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1539 return true;
1540
1541 TemplateSpecializationKind Spec = TSK_Undeclared;
1542 if (const ClassTemplateSpecializationDecl *SD =
1543 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1544 Spec = SD->getSpecializationKind();
1545
1546 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1547 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1548 CXXDecl->method_end()))
1549 return true;
1550
1551 return false;
1552}
1553
Guy Benyei11169dd2012-12-18 14:30:41 +00001554/// CreateType - get structure or union type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001555llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001557 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001558 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001559 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001560 T = getOrCreateRecordFwdDecl(
1561 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001562 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001563 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001564
David Blaikieb2e86eb2013-08-15 20:49:17 +00001565 return CreateTypeDefinition(Ty);
1566}
1567
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001568llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001569 RecordDecl *RD = Ty->getDecl();
1570
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001572 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001573
1574 // Records and classes and unions can all be recursive. To handle them, we
1575 // first generate a debug descriptor for the struct as a forward declaration.
1576 // Then (if it is a definition) we go through and get debug info for all of
1577 // its members. Finally, we create a descriptor for the complete type (which
1578 // may refer to the forward decl if the struct is recursive) and replace all
1579 // uses of the forward declaration with the final definition.
1580
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001581 auto *FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001582 cast<llvm::DICompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001584 const RecordDecl *D = RD->getDefinition();
1585 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001586 return FwdDecl;
1587
David Blaikieadfbf992013-08-18 16:55:33 +00001588 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1589 CollectContainingType(CXXDecl, FwdDecl);
1590
Guy Benyei11169dd2012-12-18 14:30:41 +00001591 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001592 LexicalBlockStack.emplace_back(&*FwdDecl);
1593 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001594
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001596 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001597 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001598
1599 // Note: The split of CXXDecl information here is intentional, the
1600 // gdb tests will depend on a certain ordering at printout. The debug
1601 // information offsets are still correct if we merge them all together
1602 // though.
1603 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1604 if (CXXDecl) {
1605 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1606 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1607 }
1608
Eric Christopher91a31902013-01-16 01:22:32 +00001609 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001610 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001611 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001612 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001613
1614 LexicalBlockStack.pop_back();
1615 RegionMap.erase(Ty->getDecl());
1616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001617 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001618 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001619
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001620 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001621 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001622 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001623
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001624 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001625 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001626}
1627
1628/// CreateType - get objective-c object type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001629llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1630 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 // Ignore protocols.
1632 return getOrCreateType(Ty->getBaseType(), Unit);
1633}
1634
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001635/// \return true if Getter has the default name for the property PD.
1636static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1637 const ObjCMethodDecl *Getter) {
1638 assert(PD);
1639 if (!Getter)
1640 return true;
1641
1642 assert(Getter->getDeclName().isObjCZeroArgSelector());
1643 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001644 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001645}
1646
1647/// \return true if Setter has the default name for the property PD.
1648static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1649 const ObjCMethodDecl *Setter) {
1650 assert(PD);
1651 if (!Setter)
1652 return true;
1653
1654 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001655 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001656 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001657}
1658
Guy Benyei11169dd2012-12-18 14:30:41 +00001659/// CreateType - get objective-c interface type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001660llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1661 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001662 ObjCInterfaceDecl *ID = Ty->getDecl();
1663 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001664 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001665
1666 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001667 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001668 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001669 auto RuntimeLang =
1670 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001671
1672 // If this is just a forward declaration return a special forward-declaration
1673 // debug type since we won't be able to lay out the entire type.
1674 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001675 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001676 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001677 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1678 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001679 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001680 return FwdDecl;
1681 }
1682
David Blaikieef8a9512014-05-05 23:23:53 +00001683 return CreateTypeDefinition(Ty, Unit);
1684}
1685
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001686llvm::DIModule *
1687CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001688 auto it = ModuleRefCache.find(Mod.Signature);
1689 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001690 return it->second;
1691
1692 // Macro definitions that were defined with "-D" on the command line.
1693 SmallString<128> ConfigMacros;
1694 {
1695 llvm::raw_svector_ostream OS(ConfigMacros);
1696 const auto &PPOpts = CGM.getPreprocessorOpts();
1697 unsigned I = 0;
1698 // Translate the macro definitions back into a commmand line.
1699 for (auto &M : PPOpts.Macros) {
1700 if (++I > 1)
1701 OS << " ";
1702 const std::string &Macro = M.first;
1703 bool Undef = M.second;
1704 OS << "\"-" << (Undef ? 'U' : 'D');
1705 for (char c : Macro)
1706 switch (c) {
1707 case '\\' : OS << "\\\\"; break;
1708 case '"' : OS << "\\\""; break;
1709 default: OS << c;
1710 }
1711 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001712 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001713 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001714 llvm::DIBuilder DIB(CGM.getModule());
1715 auto *CU = DIB.createCompileUnit(
1716 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1717 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1718 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1719 llvm::DIModule *ModuleRef =
1720 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1721 internString(CGM.getHeaderSearchOpts().Sysroot));
1722 DIB.finalize();
1723 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001724 return ModuleRef;
1725}
1726
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001727llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1728 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001729 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001730 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001731 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001732 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001733
1734 // Bit size, align and offset of the type.
1735 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1736 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1737
1738 unsigned Flags = 0;
1739 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001740 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001741
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001742 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001743 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001744 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001745
David Blaikieef8a9512014-05-05 23:23:53 +00001746 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001747 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001748
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001749 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001750 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001751 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001752
1753 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001754 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001755
1756 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1757 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001758 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001759 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001760 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001761 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001762
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001763 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 EltTys.push_back(InhTag);
1765 }
1766
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001767 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001768 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001770 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001771 unsigned PLine = getLineNumber(Loc);
1772 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1773 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001774 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1775 PD->getName(), PUnit, PLine,
1776 hasDefaultGetterName(PD, Getter) ? ""
1777 : getSelectorName(PD->getGetterName()),
1778 hasDefaultSetterName(PD, Setter) ? ""
1779 : getSelectorName(PD->getSetterName()),
1780 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 EltTys.push_back(PropertyNode);
1782 }
1783
1784 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1785 unsigned FieldNo = 0;
1786 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1787 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001788 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001789 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001790 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001791
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 StringRef FieldName = Field->getName();
1793
1794 // Ignore unnamed fields.
1795 if (FieldName.empty())
1796 continue;
1797
1798 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001799 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 unsigned FieldLine = getLineNumber(Field->getLocation());
1801 QualType FType = Field->getType();
1802 uint64_t FieldSize = 0;
1803 unsigned FieldAlign = 0;
1804
1805 if (!FType->isIncompleteArrayType()) {
1806
1807 // Bit size, align and offset of the type.
1808 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001809 ? Field->getBitWidthValue(CGM.getContext())
1810 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001811 FieldAlign = CGM.getContext().getTypeAlign(FType);
1812 }
1813
1814 uint64_t FieldOffset;
1815 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1816 // We don't know the runtime offset of an ivar if we're using the
1817 // non-fragile ABI. For bitfields, use the bit offset into the first
1818 // byte of storage of the bitfield. For other fields, use zero.
1819 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001820 FieldOffset =
1821 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 FieldOffset %= CGM.getContext().getCharWidth();
1823 } else {
1824 FieldOffset = 0;
1825 }
1826 } else {
1827 FieldOffset = RL.getFieldOffset(FieldNo);
1828 }
1829
1830 unsigned Flags = 0;
1831 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001832 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001833 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001834 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001835 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001836 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001837
Craig Topper8a13c412014-05-21 05:09:00 +00001838 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001839 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001840 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001841 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001843 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001844 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001845 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001846 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1847 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001848 PropertyNode = DBuilder.createObjCProperty(
1849 PD->getName(), PUnit, PLine,
1850 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1851 PD->getGetterName()),
1852 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1853 PD->getSetterName()),
1854 PD->getPropertyAttributes(),
1855 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 }
1857 }
1858 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001859 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1860 FieldSize, FieldAlign, FieldOffset, Flags,
1861 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 EltTys.push_back(FieldTy);
1863 }
1864
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001865 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001866 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001867
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001869 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001870}
1871
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001872llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1873 llvm::DIFile *Unit) {
1874 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 int64_t Count = Ty->getNumElements();
1876 if (Count == 0)
1877 // If number of elements are not known then this is an unbounded array.
1878 // Use Count == -1 to express such arrays.
1879 Count = -1;
1880
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001881 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001882 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883
1884 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1885 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1886
1887 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1888}
1889
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001890llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001891 uint64_t Size;
1892 uint64_t Align;
1893
1894 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1895 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1896 Size = 0;
1897 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001898 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 } else if (Ty->isIncompleteArrayType()) {
1900 Size = 0;
1901 if (Ty->getElementType()->isIncompleteType())
1902 Align = 0;
1903 else
1904 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001905 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 Size = 0;
1907 Align = 0;
1908 } else {
1909 // Size and align of the whole array, not the element type.
1910 Size = CGM.getContext().getTypeSize(Ty);
1911 Align = CGM.getContext().getTypeAlign(Ty);
1912 }
1913
1914 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1915 // interior arrays, do we care? Why aren't nested arrays represented the
1916 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001917 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 QualType EltTy(Ty, 0);
1919 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1920 // If the number of elements is known, then count is that number. Otherwise,
1921 // it's -1. This allows us to represent a subrange with an array of 0
1922 // elements, like this:
1923 //
1924 // struct foo {
1925 // int x[0];
1926 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001927 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1929 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001930
Guy Benyei11169dd2012-12-18 14:30:41 +00001931 // FIXME: Verify this is right for VLAs.
1932 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1933 EltTy = Ty->getElementType();
1934 }
1935
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001936 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001937
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001938 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1939 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001940}
1941
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001942llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1943 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001944 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1945 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001946}
1947
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001948llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1949 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001950 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1951 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001952}
1953
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001954llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1955 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001956 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1957 ? CGM.getContext().getTypeSize(Ty)
1958 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001959 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001960 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001961 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001962 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001963
1964 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001965 Ty->getPointeeType()->getAs<FunctionProtoType>();
1966 return DBuilder.createMemberPointerType(
1967 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1968 Ty->getClass(), FPT->getTypeQuals())),
1969 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001970 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001971}
1972
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001973llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001974 // Ignore the atomic wrapping
1975 // FIXME: What is the correct representation?
1976 return getOrCreateType(Ty->getValueType(), U);
1977}
1978
1979/// CreateEnumType - get enumeration type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001980llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001981 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001982 uint64_t Size = 0;
1983 uint64_t Align = 0;
1984 if (!ED->getTypeForDecl()->isIncompleteType()) {
1985 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1986 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1987 }
1988
Manman Rene0064d82013-08-29 23:19:58 +00001989 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1990
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 // If this is just a forward declaration, construct an appropriately
1992 // marked node and just return it.
1993 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001994 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001995 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001996 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 unsigned Line = getLineNumber(ED->getLocation());
1998 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001999 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00002000 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002001 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002002 ReplaceMap.emplace_back(
2003 std::piecewise_construct, std::make_tuple(Ty),
2004 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00002005 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00002006 }
2007
David Blaikie483a9da2014-05-06 18:35:21 +00002008 return CreateTypeDefinition(Ty);
2009}
2010
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002011llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00002012 const EnumDecl *ED = Ty->getDecl();
2013 uint64_t Size = 0;
2014 uint64_t Align = 0;
2015 if (!ED->getTypeForDecl()->isIncompleteType()) {
2016 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
2017 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
2018 }
2019
2020 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2021
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002022 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002023 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002025 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002026 Enumerators.push_back(DBuilder.createEnumerator(
2027 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 }
2029
2030 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002031 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002032
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002033 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002035 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00002036 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002037 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002038 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2039 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2040 Line, Size, Align, EltArray, ClassTy,
2041 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002042}
2043
David Blaikie05491062013-01-21 04:37:12 +00002044static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2045 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002047 Qualifiers InnerQuals = T.getLocalQualifiers();
2048 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2049 // that is already there.
2050 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2051 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002052 QualType LastT = T;
2053 switch (T->getTypeClass()) {
2054 default:
David Blaikie05491062013-01-21 04:37:12 +00002055 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002056 case Type::TemplateSpecialization: {
2057 const auto *Spec = cast<TemplateSpecializationType>(T);
2058 if (Spec->isTypeAlias())
2059 return C.getQualifiedType(T.getTypePtr(), Quals);
2060 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002061 break;
2062 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002063 case Type::TypeOfExpr:
2064 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2065 break;
2066 case Type::TypeOf:
2067 T = cast<TypeOfType>(T)->getUnderlyingType();
2068 break;
2069 case Type::Decltype:
2070 T = cast<DecltypeType>(T)->getUnderlyingType();
2071 break;
2072 case Type::UnaryTransform:
2073 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2074 break;
2075 case Type::Attributed:
2076 T = cast<AttributedType>(T)->getEquivalentType();
2077 break;
2078 case Type::Elaborated:
2079 T = cast<ElaboratedType>(T)->getNamedType();
2080 break;
2081 case Type::Paren:
2082 T = cast<ParenType>(T)->getInnerType();
2083 break;
David Blaikie05491062013-01-21 04:37:12 +00002084 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002085 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002086 break;
2087 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002088 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002089 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002090 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002091 break;
2092 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002093
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002095 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002096 } while (true);
2097}
2098
Eric Christopher0fdcb312013-05-16 00:52:20 +00002099/// getType - Get the type from the cache or return null type if it doesn't
2100/// exist.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002101llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002102
2103 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002104 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002105
David Blaikief427b002014-05-06 03:42:01 +00002106 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002107 if (it != TypeCache.end()) {
2108 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002109 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002110 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002111 }
2112
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002113 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002114}
2115
David Blaikie0e716b42014-03-03 23:48:23 +00002116void CGDebugInfo::completeTemplateDefinition(
2117 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002118 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2119 return;
2120
David Blaikie0e716b42014-03-03 23:48:23 +00002121 completeClassData(&SD);
2122 // In case this type has no member function definitions being emitted, ensure
2123 // it is retained
2124 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2125}
2126
Guy Benyei11169dd2012-12-18 14:30:41 +00002127/// getOrCreateType - Get the type from the cache or create a new
2128/// one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002129llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002130 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002131 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002132
2133 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002134 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002135
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002136 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002137 return T;
2138
2139 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002140 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002141 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002142
2143 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002144 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002145
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 return Res;
2147}
2148
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002149/// Currently the checksum of an interface includes the number of
2150/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002151unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002152 // The assumption is that the number of ivars can only increase
2153 // monotonically, so it is safe to just use their current number as
2154 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002155 unsigned Sum = 0;
2156 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002157 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002158 ++Sum;
2159
2160 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002161}
2162
2163ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2164 switch (Ty->getTypeClass()) {
2165 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002166 return getObjCInterfaceDecl(
2167 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002168 case Type::ObjCInterface:
2169 return cast<ObjCInterfaceType>(Ty)->getDecl();
2170 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002171 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002172 }
2173}
2174
Guy Benyei11169dd2012-12-18 14:30:41 +00002175/// CreateTypeNode - Create a new debug type node.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002176llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 // Handle qualifiers, which recursively handles what they refer to.
2178 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002179 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002180
Guy Benyei11169dd2012-12-18 14:30:41 +00002181 // Work out details of type.
2182 switch (Ty->getTypeClass()) {
2183#define TYPE(Class, Base)
2184#define ABSTRACT_TYPE(Class, Base)
2185#define NON_CANONICAL_TYPE(Class, Base)
2186#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2187#include "clang/AST/TypeNodes.def"
2188 llvm_unreachable("Dependent types cannot show up in debug information");
2189
2190 case Type::ExtVector:
2191 case Type::Vector:
2192 return CreateType(cast<VectorType>(Ty), Unit);
2193 case Type::ObjCObjectPointer:
2194 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2195 case Type::ObjCObject:
2196 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2197 case Type::ObjCInterface:
2198 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2199 case Type::Builtin:
2200 return CreateType(cast<BuiltinType>(Ty));
2201 case Type::Complex:
2202 return CreateType(cast<ComplexType>(Ty));
2203 case Type::Pointer:
2204 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002205 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002206 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002207 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002208 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002209 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 case Type::BlockPointer:
2211 return CreateType(cast<BlockPointerType>(Ty), Unit);
2212 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002213 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002214 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002215 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002217 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 case Type::FunctionProto:
2219 case Type::FunctionNoProto:
2220 return CreateType(cast<FunctionType>(Ty), Unit);
2221 case Type::ConstantArray:
2222 case Type::VariableArray:
2223 case Type::IncompleteArray:
2224 return CreateType(cast<ArrayType>(Ty), Unit);
2225
2226 case Type::LValueReference:
2227 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2228 case Type::RValueReference:
2229 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2230
2231 case Type::MemberPointer:
2232 return CreateType(cast<MemberPointerType>(Ty), Unit);
2233
2234 case Type::Atomic:
2235 return CreateType(cast<AtomicType>(Ty), Unit);
2236
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002238 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2239
David Blaikie42edade2014-11-11 20:44:45 +00002240 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002241 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002242 case Type::Elaborated:
2243 case Type::Paren:
2244 case Type::SubstTemplateTypeParm:
2245 case Type::TypeOfExpr:
2246 case Type::TypeOf:
2247 case Type::Decltype:
2248 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002249 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002250 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002251 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002252
David Blaikie42edade2014-11-11 20:44:45 +00002253 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002254}
2255
2256/// getOrCreateLimitedType - Get the type from the cache or create a new
2257/// limited type if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002258llvm::DIType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2259 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002260 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002261
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002262 auto *T = cast_or_null<llvm::DICompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002263
2264 // We may have cached a forward decl when we could have created
2265 // a non-forward decl. Go ahead and create a non-forward decl
2266 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002267 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002268 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002269
2270 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002271 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002272
2273 // Propagate members from the declaration to the definition
2274 // CreateType(const RecordType*) will overwrite this with the members in the
2275 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002276 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002277
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002279 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002280 return Res;
2281}
2282
2283// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002284llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002288 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 unsigned Line = getLineNumber(RD->getLocation());
2290 StringRef RDName = getClassName(RD);
2291
Michael Kupersteindef554d2015-07-01 12:34:39 +00002292 llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002293
David Blaikied2785892013-08-18 17:36:19 +00002294 // If we ended up creating the type during the context chain construction,
2295 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002296 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002297 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002298 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002299 return T;
David Blaikied2785892013-08-18 17:36:19 +00002300
Adrian Prantl381e7552014-02-04 21:29:50 +00002301 // If this is just a forward or incomplete declaration, construct an
2302 // appropriately marked node and just return it.
2303 const RecordDecl *D = RD->getDefinition();
2304 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002305 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002306
2307 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2308 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002309
Manman Rene0064d82013-08-29 23:19:58 +00002310 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2311
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002312 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002313 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2314 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002315
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002316 RegionMap[Ty->getDecl()].reset(RealDecl);
2317 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002318
David Blaikieadfbf992013-08-18 16:55:33 +00002319 if (const ClassTemplateSpecializationDecl *TSpecial =
2320 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002321 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002322 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002323 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002324}
2325
David Blaikieadfbf992013-08-18 16:55:33 +00002326void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002327 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002328 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002329 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002330 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2331 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002332 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002333 while (1) {
2334 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2335 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2336 if (PBT && !BRL.isPrimaryBaseVirtual())
2337 PBase = PBT;
2338 else
2339 break;
2340 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002341 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002342 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2343 getOrCreateFile(RD->getLocation())));
2344 } else if (RD->isDynamicClass())
2345 ContainingType = RealDecl;
2346
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002347 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002348}
2349
Guy Benyei11169dd2012-12-18 14:30:41 +00002350/// CreateMemberType - Create new member and increase Offset by FType's size.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002351llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002352 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002353 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2355 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002356 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002357 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002358 *Offset += FieldSize;
2359 return Ty;
2360}
2361
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002362void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002363 StringRef &Name,
2364 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002365 llvm::DIScope *&FDContext,
2366 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002367 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002368 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2369 Name = getFunctionName(FD);
2370 // Use mangled name as linkage name for C/C++ functions.
2371 if (FD->hasPrototype()) {
2372 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002373 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002374 }
2375 // No need to replicate the linkage name if it isn't different from the
2376 // subprogram name, no need to have it at all unless coverage is enabled or
2377 // debug is set to more than just line tables.
2378 if (LinkageName == Name ||
2379 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2380 !CGM.getCodeGenOpts().EmitGcovNotes &&
2381 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2382 LinkageName = StringRef();
2383
2384 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2385 if (const NamespaceDecl *NSDecl =
2386 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2387 FDContext = getOrCreateNameSpace(NSDecl);
2388 else if (const RecordDecl *RDecl =
2389 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2390 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2391 // Collect template parameters.
2392 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2393 }
2394}
2395
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002396void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002397 unsigned &LineNo, QualType &T,
2398 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002399 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002400 Unit = getOrCreateFile(VD->getLocation());
2401 LineNo = getLineNumber(VD->getLocation());
2402
2403 setLocation(VD->getLocation());
2404
2405 T = VD->getType();
2406 if (T->isIncompleteArrayType()) {
2407 // CodeGen turns int[] into int[1] so we'll do the same here.
2408 llvm::APInt ConstVal(32, 1);
2409 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2410
2411 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2412 ArrayType::Normal, 0);
2413 }
2414
2415 Name = VD->getName();
2416 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2417 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2418 LinkageName = CGM.getMangledName(VD);
2419 if (LinkageName == Name)
2420 LinkageName = StringRef();
2421
2422 // Since we emit declarations (DW_AT_members) for static members, place the
2423 // definition of those static members in the namespace they were declared in
2424 // in the source code (the lexical decl context).
2425 // FIXME: Generalize this for even non-member global variables where the
2426 // declaration and definition may have different lexical decl contexts, once
2427 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002428 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2429 : VD->getDeclContext();
2430 // When a record type contains an in-line initialization of a static data
2431 // member, and the record type is marked as __declspec(dllexport), an implicit
2432 // definition of the member will be created in the record context. DWARF
2433 // doesn't seem to have a nice way to describe this in a form that consumers
2434 // are likely to understand, so fake the "normal" situation of a definition
2435 // outside the class by putting it in the global scope.
2436 if (DC->isRecord())
2437 DC = CGM.getContext().getTranslationUnitDecl();
Michael Kupersteindef554d2015-07-01 12:34:39 +00002438
2439 if (VD->isStaticLocal()) {
2440 // Get context for static locals (that are technically globals) the same way
2441 // we do for "local" locals -- by using current lexical block.
2442 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2443 VDContext = LexicalBlockStack.back();
2444 } else
2445 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002446}
2447
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002448llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002449CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002450 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002451 StringRef Name, LinkageName;
2452 unsigned Flags = 0;
2453 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002454 llvm::DIFile *Unit = getOrCreateFile(Loc);
2455 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002456 unsigned Line = getLineNumber(Loc);
2457
2458 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2459 TParamsArray, Flags);
2460 // Build function type.
2461 SmallVector<QualType, 16> ArgTypes;
2462 for (const ParmVarDecl *Parm: FD->parameters())
2463 ArgTypes.push_back(Parm->getType());
2464 QualType FnType =
2465 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2466 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002467 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002468 DContext, Name, LinkageName, Unit, Line,
2469 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2470 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2471 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002472 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002473 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2474 std::make_tuple(CanonDecl),
2475 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002476 return SP;
2477}
2478
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002479llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002480CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2481 QualType T;
2482 StringRef Name, LinkageName;
2483 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484 llvm::DIFile *Unit = getOrCreateFile(Loc);
2485 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002486 unsigned Line = getLineNumber(Loc);
2487
2488 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002489 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2490 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2491 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002492 FwdDeclReplaceMap.emplace_back(
2493 std::piecewise_construct,
2494 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2495 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002496 return GV;
2497}
2498
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002499llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002500 // We only need a declaration (not a definition) of the type - so use whatever
2501 // we would otherwise do to get a type for a pointee. (forward declarations in
2502 // limited debug info, full definitions (if the type definition is available)
2503 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002504 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2505 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002506 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002507 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002508
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002509 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002510 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002511
2512 // No definition for now. Emit a forward definition that might be
2513 // merged with a potential upcoming definition.
2514 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2515 return getFunctionForwardDeclaration(FD);
2516 else if (const auto *VD = dyn_cast<VarDecl>(D))
2517 return getGlobalVariableForwardDeclaration(VD);
2518
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002519 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002520}
2521
Guy Benyei11169dd2012-12-18 14:30:41 +00002522/// getFunctionDeclaration - Return debug info descriptor to describe method
2523/// declaration for the given method definition.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002524llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002525 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002526 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002527
Guy Benyei11169dd2012-12-18 14:30:41 +00002528 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002529 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002530 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002531
2532 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002533 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002534
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002535 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002536 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002537 if (const CXXMethodDecl *MD =
2538 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002539 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002540 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002541 }
2542 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002544 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002545 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002546 return SP;
2547 }
2548
Aaron Ballman86c93902014-03-06 23:45:36 +00002549 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002550 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002551 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002552 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002553 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 return SP;
2555 }
2556 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002557 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002558}
2559
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002560// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002561// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002562llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002563 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002564 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002565 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002566 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2567 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002568 return DBuilder.createSubroutineType(F,
2569 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002570
2571 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2572 return getOrCreateMethodType(Method, F);
2573 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2574 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002575 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002576
2577 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002578 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002579
2580 // Replace the instancetype keyword with the actual type.
2581 if (ResultTy == CGM.getContext().getObjCInstanceType())
2582 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002583 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002584
Adrian Prantl7bec9032013-05-10 21:08:31 +00002585 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002587 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002588 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002590 Elts.push_back(DBuilder.createArtificialType(
2591 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002592 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002593 for (const auto *PI : OMethod->params())
2594 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002595 // Variadic methods need a special marker at the end of the type list.
2596 if (OMethod->isVariadic())
2597 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002598
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002599 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 return DBuilder.createSubroutineType(F, EltTypeArray);
2601 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002602
Adrian Prantl800faef2014-02-25 23:42:18 +00002603 // Handle variadic function types; they need an additional
2604 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002605 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2606 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002607 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002608 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2609 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2610 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2611 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2612 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002613 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002614 return DBuilder.createSubroutineType(F, EltTypeArray);
2615 }
2616
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002617 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002618}
2619
2620/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002621void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2622 SourceLocation ScopeLoc, QualType FnType,
2623 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002624
2625 StringRef Name;
2626 StringRef LinkageName;
2627
2628 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2629
2630 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002631 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002632
Guy Benyei11169dd2012-12-18 14:30:41 +00002633 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002634 llvm::DIFile *Unit = getOrCreateFile(Loc);
2635 llvm::DIScope *FDContext = Unit;
2636 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002637 if (!HasDecl) {
2638 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002639 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002640 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002641 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002642 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002644 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002645 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002646 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002647 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 return;
2649 }
2650 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002651 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2652 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2654 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002655 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 } else {
2657 // Use llvm function name.
2658 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002659 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002660 }
2661 if (!Name.empty() && Name[0] == '\01')
2662 Name = Name.substr(1);
2663
Adrian Prantl42d71b92014-04-10 23:21:53 +00002664 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002665 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002666 // Artificial functions without a location should not silently reuse CurLoc.
2667 if (Loc.isInvalid())
2668 CurLoc = SourceLocation();
2669 }
2670 unsigned LineNo = getLineNumber(Loc);
2671 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002672
Eric Christopher8018e412014-03-27 18:50:35 +00002673 // FIXME: The function declaration we're constructing here is mostly reusing
2674 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2675 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2676 // all subprograms instead of the actual context since subprogram definitions
2677 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002678 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002679 FDContext, Name, LinkageName, Unit, LineNo,
2680 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2681 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002682 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002683 // We might get here with a VarDecl in the case we're generating
2684 // code for the initialization of globals. Do not record these decls
2685 // as they will overwrite the actual VarDecl Decl in the cache.
2686 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002687 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002688
Adrian Prantlbebb8932014-03-21 21:01:58 +00002689 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002690 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002691
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002693 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002694}
2695
2696/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002697/// information in the source file. If the location is invalid, the
2698/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002699void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 // Update our current location
2701 setLocation(Loc);
2702
Eric Christophere7b87e52014-10-26 23:40:33 +00002703 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2704 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002705
Adrian Prantle83b1302014-01-07 22:05:52 +00002706 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002707 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002708 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002709}
2710
2711/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2712/// the stack.
2713void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002714 llvm::MDNode *Back = nullptr;
2715 if (!LexicalBlockStack.empty())
2716 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002717 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002718 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002719 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002720}
2721
2722/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2723/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002724void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2725 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 // Set our current location.
2727 setLocation(Loc);
2728
Guy Benyei11169dd2012-12-18 14:30:41 +00002729 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002730 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2731 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002732
2733 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2734 return;
2735
2736 // Create a new lexical block and push it on the stack.
2737 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002738}
2739
2740/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2741/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002742void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2743 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002744 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2745
2746 // Provide an entry in the line table for the end of the block.
2747 EmitLocation(Builder, Loc);
2748
David Blaikie60a877b2014-10-22 19:34:33 +00002749 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2750 return;
2751
Guy Benyei11169dd2012-12-18 14:30:41 +00002752 LexicalBlockStack.pop_back();
2753}
2754
2755/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2756void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2757 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2758 unsigned RCount = FnBeginRegionCount.back();
2759 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2760
2761 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002762 while (LexicalBlockStack.size() != RCount) {
2763 // Provide an entry in the line table for the end of the block.
2764 EmitLocation(Builder, CurLoc);
2765 LexicalBlockStack.pop_back();
2766 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 FnBeginRegionCount.pop_back();
2768}
2769
Eric Christopherb2a008c2013-05-16 00:45:12 +00002770// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002771// See BuildByRefType.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002772llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002773 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002774
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002775 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 QualType FType;
2777 uint64_t FieldSize, FieldOffset;
2778 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002779
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002780 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002781 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002782
2783 FieldOffset = 0;
2784 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2785 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2786 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2787 FType = CGM.getContext().IntTy;
2788 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2789 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2790
2791 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2792 if (HasCopyAndDispose) {
2793 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002794 EltTys.push_back(
2795 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2796 EltTys.push_back(
2797 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002798 }
2799 bool HasByrefExtendedLayout;
2800 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002801 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2802 HasByrefExtendedLayout) &&
2803 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002804 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002805 EltTys.push_back(
2806 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002807 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002808
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2810 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002811 CGM.getTarget().getPointerAlign(0))) {
2812 CharUnits FieldOffsetInBytes =
2813 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2814 CharUnits AlignedOffsetInBytes =
2815 FieldOffsetInBytes.RoundUpToAlignment(Align);
2816 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002817
Guy Benyei11169dd2012-12-18 14:30:41 +00002818 if (NumPaddingBytes.isPositive()) {
2819 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2820 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2821 pad, ArrayType::Normal, 0);
2822 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2823 }
2824 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002825
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002827 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 FieldSize = CGM.getContext().getTypeSize(FType);
2829 FieldAlign = CGM.getContext().toBits(Align);
2830
Eric Christopherb2a008c2013-05-16 00:45:12 +00002831 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002832 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2833 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002834 EltTys.push_back(FieldTy);
2835 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002836
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002837 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002838
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002839 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002840
Guy Benyei11169dd2012-12-18 14:30:41 +00002841 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002842 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002843}
2844
2845/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002846void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002847 llvm::Value *Storage, unsigned ArgNo,
2848 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002849 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2851
David Blaikie7fceebf2013-08-19 03:37:48 +00002852 bool Unwritten =
2853 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2854 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002855 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002856 if (!Unwritten)
2857 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002858 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002859 uint64_t XOffset = 0;
2860 if (VD->hasAttr<BlocksAttr>())
2861 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002862 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 Ty = getOrCreateType(VD->getType(), Unit);
2864
2865 // If there is no debug info for this type then do not emit debug info
2866 // for this variable.
2867 if (!Ty)
2868 return;
2869
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002871 unsigned Line = 0;
2872 unsigned Column = 0;
2873 if (!Unwritten) {
2874 Line = getLineNumber(VD->getLocation());
2875 Column = getColumnNumber(VD->getLocation());
2876 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002877 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002878 unsigned Flags = 0;
2879 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002880 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002881 // If this is the first argument and it is implicit then
2882 // give it an object pointer flag.
2883 // FIXME: There has to be a better way to do this, but for static
2884 // functions there won't be an implicit param at arg1 and
2885 // otherwise it is 'self' or 'this'.
2886 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002887 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002888 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002889 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2890 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002891 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002892
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002893 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002894
2895 StringRef Name = VD->getName();
2896 if (!Name.empty()) {
2897 if (VD->hasAttr<BlocksAttr>()) {
2898 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002899 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002900 // offset of __forwarding field
2901 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002902 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002903 Expr.push_back(offset.getQuantity());
2904 Expr.push_back(llvm::dwarf::DW_OP_deref);
2905 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 // offset of x field
2907 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002908 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002909
2910 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002911 auto *D = DBuilder.createLocalVariable(Tag, Scope, VD->getName(), Unit,
2912 Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002913
Guy Benyei11169dd2012-12-18 14:30:41 +00002914 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002915 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2916 llvm::DebugLoc::get(Line, Column, Scope),
2917 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002919 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002920 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002921 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2922 // If VD is an anonymous union then Storage represents value for
2923 // all union fields.
2924 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2925 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002926 // GDB has trouble finding local variables in anonymous unions, so we emit
2927 // artifical local variables for each of the members.
2928 //
2929 // FIXME: Remove this code as soon as GDB supports this.
2930 // The debug info verifier in LLVM operates based on the assumption that a
2931 // variable has the same size as its storage and we had to disable the check
2932 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002933 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002934 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002935 StringRef FieldName = Field->getName();
2936
2937 // Ignore unnamed fields. Do not ignore unnamed records.
2938 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2939 continue;
2940
2941 // Use VarDecl's Tag, Scope and Line number.
2942 auto *D = DBuilder.createLocalVariable(
2943 Tag, Scope, FieldName, Unit, Line, FieldTy,
Adrian Prantl00820ab2015-04-29 16:52:31 +00002944 CGM.getLangOpts().Optimize, Flags | llvm::DINode::FlagArtificial,
2945 ArgNo);
Adrian Prantl0d820892015-04-29 15:05:50 +00002946
2947 // Insert an llvm.dbg.declare into the current block.
2948 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2949 llvm::DebugLoc::get(Line, Column, Scope),
2950 Builder.GetInsertBlock());
2951 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002952 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002953 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002954
2955 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002956 auto *D =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002957 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2958 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002959
2960 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002961 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2962 llvm::DebugLoc::get(Line, Column, Scope),
2963 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002964}
2965
2966void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2967 llvm::Value *Storage,
2968 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002969 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2971}
2972
Adrian Prantlde17db32013-03-29 19:20:29 +00002973/// Look up the completed type for a self pointer in the TypeCache and
2974/// create a copy of it with the ObjectPointer and Artificial flags
2975/// set. If the type is not cached, a new one is created. This should
2976/// never happen though, since creating a type for the implicit self
2977/// argument implies that we already parsed the interface definition
2978/// and the ivar declarations in the implementation.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002979llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2980 llvm::DIType *Ty) {
2981 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002982 if (CachedTy)
2983 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002984 return DBuilder.createObjectPointerType(Ty);
2985}
2986
Eric Christophere7b87e52014-10-26 23:40:33 +00002987void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2988 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002989 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002990 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002992
Craig Topper8a13c412014-05-21 05:09:00 +00002993 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002994 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002995
Guy Benyei11169dd2012-12-18 14:30:41 +00002996 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002997
Guy Benyei11169dd2012-12-18 14:30:41 +00002998 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002999 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
3000 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00003001 if (isByRef)
3002 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003003 else
Guy Benyei11169dd2012-12-18 14:30:41 +00003004 Ty = getOrCreateType(VD->getType(), Unit);
3005
3006 // Self is passed along as an implicit non-arg variable in a
3007 // block. Mark it as the object pointer.
3008 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00003009 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00003010
3011 // Get location information.
3012 unsigned Line = getLineNumber(VD->getLocation());
3013 unsigned Column = getColumnNumber(VD->getLocation());
3014
3015 const llvm::DataLayout &target = CGM.getDataLayout();
3016
3017 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00003018 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00003019 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
3020
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003021 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00003022 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003023 addr.push_back(llvm::dwarf::DW_OP_deref);
3024 addr.push_back(llvm::dwarf::DW_OP_plus);
3025 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003027 addr.push_back(llvm::dwarf::DW_OP_deref);
3028 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003030 offset =
3031 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003032 addr.push_back(offset.getQuantity());
3033 addr.push_back(llvm::dwarf::DW_OP_deref);
3034 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003035 // offset of x field
3036 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003037 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 }
3039
3040 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003041 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003042 llvm::dwarf::DW_TAG_auto_variable,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003043 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003044 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003045
Guy Benyei11169dd2012-12-18 14:30:41 +00003046 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003047 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3048 if (InsertPoint)
3049 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3050 InsertPoint);
3051 else
3052 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3053 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003054}
3055
3056/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3057/// variable declaration.
3058void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3059 unsigned ArgNo,
3060 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003061 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3063}
3064
3065namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003066struct BlockLayoutChunk {
3067 uint64_t OffsetInBits;
3068 const BlockDecl::Capture *Capture;
3069};
3070bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3071 return l.OffsetInBits < r.OffsetInBits;
3072}
Guy Benyei11169dd2012-12-18 14:30:41 +00003073}
3074
3075void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003076 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003077 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003078 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003080 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 ASTContext &C = CGM.getContext();
3082 const BlockDecl *blockDecl = block.getBlockDecl();
3083
3084 // Collect some general information about the block's location.
3085 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003086 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003087 unsigned line = getLineNumber(loc);
3088 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003089
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 // Build the debug-info type for the block literal.
3091 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3092
3093 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003094 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003095
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003096 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003097 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3098 blockLayout->getElementOffsetInBits(0),
3099 tunit, tunit));
3100 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3101 blockLayout->getElementOffsetInBits(1),
3102 tunit, tunit));
3103 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3104 blockLayout->getElementOffsetInBits(2),
3105 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003106 auto *FnTy = block.getBlockExpr()->getFunctionType();
3107 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3108 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003109 blockLayout->getElementOffsetInBits(3),
3110 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003111 fields.push_back(createFieldType(
3112 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3113 ? C.getBlockDescriptorExtendedType()
3114 : C.getBlockDescriptorType()),
3115 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003116
3117 // We want to sort the captures by offset, not because DWARF
3118 // requires this, but because we're paranoid about debuggers.
3119 SmallVector<BlockLayoutChunk, 8> chunks;
3120
3121 // 'this' capture.
3122 if (blockDecl->capturesCXXThis()) {
3123 BlockLayoutChunk chunk;
3124 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003125 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003126 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 chunks.push_back(chunk);
3128 }
3129
3130 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003131 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 const VarDecl *variable = capture.getVariable();
3133 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3134
3135 // Ignore constant captures.
3136 if (captureInfo.isConstant())
3137 continue;
3138
3139 BlockLayoutChunk chunk;
3140 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003141 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003142 chunk.Capture = &capture;
3143 chunks.push_back(chunk);
3144 }
3145
3146 // Sort by offset.
3147 llvm::array_pod_sort(chunks.begin(), chunks.end());
3148
Eric Christophere7b87e52014-10-26 23:40:33 +00003149 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3150 e = chunks.end();
3151 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003152 uint64_t offsetInBits = i->OffsetInBits;
3153 const BlockDecl::Capture *capture = i->Capture;
3154
3155 // If we have a null capture, this must be the C++ 'this' capture.
3156 if (!capture) {
3157 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003158 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 QualType type = method->getThisType(C);
3160
3161 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3162 offsetInBits, tunit, tunit));
3163 continue;
3164 }
3165
3166 const VarDecl *variable = capture->getVariable();
3167 StringRef name = variable->getName();
3168
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003171 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003172
3173 // FIXME: this creates a second copy of this type!
3174 uint64_t xoffset;
3175 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003176 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3177 fieldType =
3178 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3179 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003180 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003181 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3182 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003183 }
3184 fields.push_back(fieldType);
3185 }
3186
3187 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003188 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3189 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003190
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003191 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003192
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003193 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003194 tunit, typeName.str(), tunit, line,
3195 CGM.getContext().toBits(block.BlockSize),
3196 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003197 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3198
3199 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003200 unsigned flags = llvm::DINode::FlagArtificial;
3201 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003202
3203 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003204 auto *debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003205 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3206 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003207
Adrian Prantl616bef42013-03-14 21:52:59 +00003208 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003209 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003210 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003211 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003212 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003213 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003214
Adrian Prantl616bef42013-03-14 21:52:59 +00003215 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003216 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3217 llvm::DebugLoc::get(line, column, scope),
3218 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003219}
3220
David Blaikie6943dea2013-08-20 01:28:15 +00003221/// If D is an out-of-class definition of a static data member of a class, find
3222/// its corresponding in-class declaration.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003223llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003224CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3225 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003226 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003227
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003228 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003229 if (MI != StaticDataMemberCache.end()) {
3230 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003231 return cast<llvm::DIDerivedType>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003232 }
David Blaikiece763042013-08-20 21:49:21 +00003233
3234 // If the member wasn't found in the cache, lazily construct and add it to the
3235 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003236 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003237 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003238 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003239 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003240}
3241
Eric Christophercab9fae2014-04-10 05:20:00 +00003242/// Recursively collect all of the member fields of a global anonymous decl and
3243/// create static variables for them. The first time this is called it needs
3244/// to be on a union and then from there we can have additional unnamed fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003245llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3246 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3247 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3248 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003249
3250 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003251 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003252 StringRef FieldName = Field->getName();
3253
3254 // Ignore unnamed fields, but recurse into anonymous records.
3255 if (FieldName.empty()) {
3256 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3257 if (RT)
3258 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3259 Var, DContext);
3260 continue;
3261 }
3262 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003263 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3264 LineNo, FieldTy,
3265 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003266 }
3267 return GV;
3268}
3269
Guy Benyei11169dd2012-12-18 14:30:41 +00003270/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003271void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003272 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003273 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003274 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003275 llvm::DIFile *Unit = nullptr;
3276 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003277 unsigned LineNo;
3278 StringRef DeclName, LinkageName;
3279 QualType T;
3280 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003281
3282 // Attempt to store one global variable for the declaration - even if we
3283 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003284 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003285
3286 // If this is an anonymous union then we'll want to emit a global
3287 // variable for each member of the anonymous union so that it's possible
3288 // to find the name of any field in the union.
3289 if (T->isUnionType() && DeclName.empty()) {
3290 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003291 assert(RD->isAnonymousStructOrUnion() &&
3292 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003293 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3294 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003295 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003296 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3297 Var->hasInternalLinkage(), Var,
3298 getOrCreateStaticDataMemberDeclarationOrNull(D));
3299 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003300 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003301}
3302
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003303/// EmitGlobalVariable - Emit global variable's debug info.
3304void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3305 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003306 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003307 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003308 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003309 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003310 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003311 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3312 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3313 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3314 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3315 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003316 // Do not use global variables for enums.
3317 //
3318 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003319 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003320 return;
David Blaikiea15565562014-04-04 20:56:17 +00003321 // Do not emit separate definitions for function local const/statics.
3322 if (isa<FunctionDecl>(VD->getDeclContext()))
3323 return;
David Blaikiebb113912014-04-05 07:23:17 +00003324 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003325 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003326 if (VarD->isStaticDataMember()) {
3327 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3328 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003329 // Ensure that the type is retained even though it's otherwise unreferenced.
3330 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003331 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003332 return;
3333 }
3334
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003335 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003336 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3337
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003338 auto &GV = DeclCache[VD];
3339 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003340 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003341 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003342 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003343 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003344}
3345
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003346llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003347 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003348 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003349 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003350}
3351
David Blaikie9f88fe82013-04-22 06:13:21 +00003352void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003353 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3354 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003355 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003356 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3357 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003358 getLineNumber(UD.getLocation()));
3359}
3360
David Blaikiebd483762013-05-20 04:58:53 +00003361void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3362 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3363 return;
3364 assert(UD.shadow_size() &&
3365 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3366 // Emitting one decl is sufficient - debuggers can detect that this is an
3367 // overloaded name & provide lookup for all the overloads.
3368 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003369 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003370 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003371 DBuilder.createImportedDeclaration(
3372 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3373 getLineNumber(USD.getLocation()));
3374}
3375
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003376void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3377 auto *Reader = CGM.getContext().getExternalSource();
3378 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3379 DBuilder.createImportedDeclaration(
3380 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3381 getOrCreateModuleRef(Info),
3382 getLineNumber(ID.getLocation()));
3383}
3384
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003385llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003386CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3387 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003388 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003389 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003390 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003391 return cast<llvm::DIImportedEntity>(VH);
3392 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003393 if (const NamespaceAliasDecl *Underlying =
3394 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3395 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003396 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003397 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3398 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3399 NA.getName());
3400 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003401 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003402 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3403 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3404 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003405 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003406 return R;
3407}
3408
Guy Benyei11169dd2012-12-18 14:30:41 +00003409/// getOrCreateNamesSpace - Return namespace descriptor for the given
3410/// namespace decl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003411llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003412CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003413 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003414 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003415 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003416 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003417
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003419 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3420 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003421 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003422 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003423 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003424 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003425 return NS;
3426}
3427
3428void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003429 // Creating types might create further types - invalidating the current
3430 // element and the size(), so don't cache/reference them.
3431 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3432 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003433 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003434 ? CreateTypeDefinition(E.Type, E.Unit)
3435 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003436 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003437 }
3438
David Blaikief427b002014-05-06 03:42:01 +00003439 for (auto p : ReplaceMap) {
3440 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003441 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003442 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003443
David Blaikief427b002014-05-06 03:42:01 +00003444 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003445 assert(it != TypeCache.end());
3446 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003447
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003448 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3449 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003450 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003451
Frederic Rissd253ed62014-11-18 03:40:51 +00003452 for (const auto &p : FwdDeclReplaceMap) {
3453 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003454 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003455 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003456
3457 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003458 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003459 // with ourselves, that will destroy the temporary MDNode and
3460 // replace it with a standard one, avoiding leaking memory.
3461 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003462 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003463 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003464 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003465
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003466 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003467 }
3468
Adrian Prantl73409ce2013-03-11 18:33:46 +00003469 // We keep our own list of retained types, because we need to look
3470 // up the final type in the type cache.
3471 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3472 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003473 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003474
Guy Benyei11169dd2012-12-18 14:30:41 +00003475 DBuilder.finalize();
3476}
David Blaikie66088d52014-09-24 17:01:27 +00003477
3478void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3479 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3480 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003481
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003482 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003483 // Don't ignore in case of explicit cast where it is referenced indirectly.
3484 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003485}