blob: 8c4b4b3d0617af1a7cefe058c6269904e087c2a1 [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) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000789 // We don't set size information, but do specify where the typedef was
790 // declared.
David Blaikiebe822ed2015-07-01 18:07:22 +0000791 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000792
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000793 // Typedefs are derived from some other type.
794 return DBuilder.createTypedef(
795 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
796 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
David Blaikiebe822ed2015-07-01 18:07:22 +0000797 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000798}
799
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000800llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
801 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000802 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000803
804 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000805 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000806
807 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000808 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000809 if (isa<FunctionNoProtoType>(Ty))
810 EltTys.push_back(DBuilder.createUnspecifiedParameter());
811 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000812 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
813 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000814 if (FPT->isVariadic())
815 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 }
817
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000818 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000819 return DBuilder.createSubroutineType(Unit, EltTypeArray);
820}
821
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000822/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000823/// As an optimization, return 0 if the access specifier equals the
824/// default for the containing type.
825static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
826 AccessSpecifier Default = clang::AS_none;
827 if (RD && RD->isClass())
828 Default = clang::AS_private;
829 else if (RD && (RD->isStruct() || RD->isUnion()))
830 Default = clang::AS_public;
831
832 if (Access == Default)
833 return 0;
834
Eric Christophere7b87e52014-10-26 23:40:33 +0000835 switch (Access) {
836 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000837 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000838 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000839 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000840 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000841 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000842 case clang::AS_none:
843 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000844 }
845 llvm_unreachable("unexpected access enumerator");
846}
Guy Benyei11169dd2012-12-18 14:30:41 +0000847
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000848llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000849 StringRef name, QualType type, uint64_t sizeInBitsOverride,
850 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000851 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
852 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000853
854 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000855 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 unsigned line = getLineNumber(loc);
857
David Majnemer34b57492014-07-30 01:30:47 +0000858 uint64_t SizeInBits = 0;
859 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000860 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000861 TypeInfo TI = CGM.getContext().getTypeInfo(type);
862 SizeInBits = TI.Width;
863 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000864
865 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000866 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000867 }
868
Adrian Prantl21361fb2014-08-29 22:44:27 +0000869 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000870 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
871 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000872}
873
Eric Christopher91a31902013-01-16 01:22:32 +0000874/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000875void CGDebugInfo::CollectRecordLambdaFields(
876 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000877 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000878 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
879 // has the name and the location of the variable so we should iterate over
880 // both concurrently.
881 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
882 RecordDecl::field_iterator Field = CXXDecl->field_begin();
883 unsigned fieldno = 0;
884 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000885 E = CXXDecl->captures_end();
886 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000887 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000888 if (C.capturesVariable()) {
889 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000890 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000891 StringRef VName = V->getName();
892 uint64_t SizeInBitsOverride = 0;
893 if (Field->isBitField()) {
894 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
895 assert(SizeInBitsOverride && "found named 0-width bitfield");
896 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000897 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000898 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
899 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
900 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000901 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000902 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000903 // TODO: Need to handle 'this' in some way by probably renaming the
904 // this of the lambda class and having a field member of 'this' or
905 // by using AT_object_pointer for the function and having that be
906 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000907 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000909 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000910 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000911 "this", type, 0, f->getLocation(), f->getAccess(),
912 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000913
914 elements.push_back(fieldType);
915 }
916 }
917}
918
David Blaikie6943dea2013-08-20 01:28:15 +0000919/// Helper for CollectRecordFields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000920llvm::DIDerivedType *
921CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000922 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000923 // Create the descriptor for the static variable, with or without
924 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000925 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000926 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
927 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000928
Eric Christopher91a31902013-01-16 01:22:32 +0000929 unsigned LineNumber = getLineNumber(Var->getLocation());
930 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000931 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000932 if (Var->getInit()) {
933 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000934 if (Value) {
935 if (Value->isInt())
936 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
937 if (Value->isFloat())
938 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
939 }
Eric Christopher91a31902013-01-16 01:22:32 +0000940 }
941
Adrian Prantl21361fb2014-08-29 22:44:27 +0000942 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000943 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000944 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000945 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000946 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000947}
948
949/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000950void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000951 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
952 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000953 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000954 StringRef name = field->getName();
955 QualType type = field->getType();
956
957 // Ignore unnamed fields unless they're anonymous structs/unions.
958 if (name.empty() && !type->isRecordType())
959 return;
960
961 uint64_t SizeInBitsOverride = 0;
962 if (field->isBitField()) {
963 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
964 assert(SizeInBitsOverride && "found named 0-width bitfield");
965 }
966
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000967 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000968 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
969 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000970
971 elements.push_back(fieldType);
972}
973
Guy Benyei11169dd2012-12-18 14:30:41 +0000974/// CollectRecordFields - A helper function to collect debug info for
975/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000976void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000977 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000978 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000979 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000980 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
981
Eric Christopher91a31902013-01-16 01:22:32 +0000982 if (CXXDecl && CXXDecl->isLambda())
983 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
984 else {
985 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000986
Eric Christopher91a31902013-01-16 01:22:32 +0000987 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000988 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000989
Eric Christopher91a31902013-01-16 01:22:32 +0000990 // Static and non-static members should appear in the same order as
991 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000992 for (const auto *I : record->decls())
993 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000994 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000995 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000996 if (MI != StaticDataMemberCache.end()) {
997 assert(MI->second &&
998 "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000999 elements.push_back(cast<llvm::DIDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +00001000 } else {
1001 auto Field = CreateRecordStaticField(V, RecordTy, record);
1002 elements.push_back(Field);
1003 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001004 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001005 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1006 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001007
1008 // Bump field number for next field.
1009 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001011 }
1012}
1013
1014/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1015/// function type is not updated to include implicit "this" pointer. Use this
1016/// routine to get a method type which includes "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001017llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001018CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001019 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001020 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001021 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001022 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001023 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001024 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1025 Func, Unit);
1026}
David Blaikie2aaf0652013-01-07 22:24:59 +00001027
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001028llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1029 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001030 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001031 llvm::DITypeRefArray Args(
1032 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001033 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001034 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001035
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001036 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001037
1038 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001039 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001040
David Blaikie2aaf0652013-01-07 22:24:59 +00001041 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001042 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001043 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1044 // Create pointer type directly in this case.
1045 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1046 QualType PointeeTy = ThisPtrTy->getPointeeType();
1047 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001048 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001049 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001050 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1051 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001052 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001053 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001054 // TODO: This and the artificial type below are misleading, the
1055 // types aren't artificial the argument is, but the current
1056 // metadata doesn't represent that.
1057 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1058 Elts.push_back(ThisPtrType);
1059 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001060 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001061 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001062 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1063 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001064 }
1065
1066 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001067 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1068 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001069
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001070 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001071
Adrian Prantl0630eb72013-12-18 21:48:18 +00001072 unsigned Flags = 0;
1073 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001074 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001075 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001076 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001077
1078 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001079}
1080
Eric Christopherb2a008c2013-05-16 00:45:12 +00001081/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001082/// inside a function.
1083static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1084 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1085 return isFunctionLocalClass(NRD);
1086 if (isa<FunctionDecl>(RD->getDeclContext()))
1087 return true;
1088 return false;
1089}
1090
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001091/// CreateCXXMemberFunction - A helper function to create a subprogram for
Guy Benyei11169dd2012-12-18 14:30:41 +00001092/// a single member function GlobalDecl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001093llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1094 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001095 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001096 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001097
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001099 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001100
1101 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1102 // make sense to give a single ctor/dtor a linkage name.
1103 StringRef MethodLinkageName;
1104 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1105 MethodLinkageName = CGM.getMangledName(Method);
1106
1107 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001108 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001109 unsigned MethodLine = 0;
1110 if (!Method->isImplicit()) {
1111 MethodDefUnit = getOrCreateFile(Method->getLocation());
1112 MethodLine = getLineNumber(Method->getLocation());
1113 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001114
1115 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001116 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001117 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001119
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 if (Method->isVirtual()) {
1121 if (Method->isPure())
1122 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1123 else
1124 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001125
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 // It doesn't make sense to give a virtual destructor a vtable index,
1127 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001128 // FIXME: Add proper support for debug info for virtual calls in
1129 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1130 // lookup if we have multiple or virtual inheritance.
1131 if (!isa<CXXDestructorDecl>(Method) &&
1132 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001133 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001134 ContainingType = RecordTy;
1135 }
1136
1137 unsigned Flags = 0;
1138 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001139 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001140 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001141 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1142 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001143 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001144 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001145 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001147 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 }
1149 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001151 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001152 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001153 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001154 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001155
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001156 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1157 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001158 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1159 MethodTy, /*isLocalToUnit=*/false,
1160 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001161 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001162
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001163 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001164
1165 return SP;
1166}
1167
1168/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001169/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001170/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001171void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001172 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1173 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001174
1175 // Since we want more than just the individual member decls if we
1176 // have templated functions iterate over every declaration to gather
1177 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001178 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001179 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1180 // If the member is implicit, don't add it to the member list. This avoids
1181 // the member being added to type units by LLVM, while still allowing it
1182 // to be emitted into the type declaration/reference inside the compile
1183 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001184 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001185 // FIXME: Handle Using(Shadow?)Decls here to create
1186 // DW_TAG_imported_declarations inside the class for base decls brought into
1187 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1188 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1189 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001190 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001191 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001192
1193 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1194 continue;
1195
David Blaikiefd580722014-10-06 05:18:55 +00001196 // Reuse the existing member function declaration if it exists.
1197 // It may be associated with the declaration of the type & should be
1198 // reused as we're building the definition.
1199 //
1200 // This situation can arise in the vtable-based debug info reduction where
1201 // implicit members are emitted in a non-vtable TU.
1202 auto MI = SPCache.find(Method->getCanonicalDecl());
1203 EltTys.push_back(MI == SPCache.end()
1204 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001205 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001206 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001207}
Guy Benyei11169dd2012-12-18 14:30:41 +00001208
Guy Benyei11169dd2012-12-18 14:30:41 +00001209/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001210/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001211/// a Record.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001213 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001214 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001216 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001217 unsigned BFlags = 0;
1218 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001219
Guy Benyei11169dd2012-12-18 14:30:41 +00001220 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001221 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001222
Aaron Ballman574705e2014-03-13 15:41:46 +00001223 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001224 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1225 // virtual base offset offset is -ve. The code generator emits dwarf
1226 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001227 BaseOffset = 0 - CGM.getItaniumVTableContext()
1228 .getVirtualBaseOffsetOffset(RD, Base)
1229 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001230 } else {
1231 // In the MS ABI, store the vbtable offset, which is analogous to the
1232 // vbase offset offset in Itanium.
1233 BaseOffset =
1234 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1235 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001236 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 } else
1238 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1239 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1240 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001241
Adrian Prantl21361fb2014-08-29 22:44:27 +00001242 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001243 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001244 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 EltTys.push_back(DTy);
1246 }
1247}
1248
1249/// CollectTemplateParams - A helper function to collect template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001250llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001251CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1252 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001253 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001254 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001255 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1256 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001257 StringRef Name;
1258 if (TPList)
1259 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001260 switch (TA.getKind()) {
1261 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001262 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001263 TemplateParams.push_back(
1264 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001265 } break;
1266 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001267 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001268 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1269 TheCU, Name, TTy,
1270 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001271 } break;
1272 case TemplateArgument::Declaration: {
1273 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001274 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001275 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001276 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001277 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001278 // Variable pointer template parameters have a value that is the address
1279 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001280 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001281 V = CGM.GetAddrOfGlobalVar(VD);
1282 // Member function pointers have special support for building them, though
1283 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001284 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001285 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001286 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001287 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001288 // Member data pointers have special handling too to compute the fixed
1289 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001290 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001291 // These five lines (& possibly the above member function pointer
1292 // handling) might be able to be refactored to use similar code in
1293 // CodeGenModule::getMemberPointerConstant
1294 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1295 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001296 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001297 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001298 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001299 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1300 TheCU, Name, TTy,
1301 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001302 } break;
1303 case TemplateArgument::NullPtr: {
1304 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001305 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001306 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001307 // Special case member data pointer null values since they're actually -1
1308 // instead of zero.
1309 if (const MemberPointerType *MPT =
1310 dyn_cast<MemberPointerType>(T.getTypePtr()))
1311 // But treat member function pointers as simple zero integers because
1312 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1313 // CodeGen grows handling for values of non-null member function
1314 // pointers then perhaps we could remove this special case and rely on
1315 // EmitNullMemberPointer for member function pointers.
1316 if (MPT->isMemberDataPointer())
1317 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1318 if (!V)
1319 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001320 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1321 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001322 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001323 case TemplateArgument::Template:
1324 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001325 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001326 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1327 break;
1328 case TemplateArgument::Pack:
1329 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1330 TheCU, Name, nullptr,
1331 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1332 break;
David Majnemer5559d472013-08-24 08:21:10 +00001333 case TemplateArgument::Expression: {
1334 const Expr *E = TA.getAsExpr();
1335 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001336 if (E->isGLValue())
1337 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001338 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001339 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001340 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001341 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1342 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001343 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001344 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001345 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001346 case TemplateArgument::Null:
1347 llvm_unreachable(
1348 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001349 }
1350 }
1351 return DBuilder.getOrCreateArray(TemplateParams);
1352}
1353
1354/// CollectFunctionTemplateParams - A helper function to collect debug
1355/// info for function template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001356llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001357CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001358 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001359 if (FD->getTemplatedKind() ==
1360 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001361 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1362 ->getTemplate()
1363 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001364 return CollectTemplateParams(
1365 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001366 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001367 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001368}
1369
1370/// CollectCXXTemplateParams - A helper function to collect debug info for
1371/// template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001372llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1373 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001374 // Always get the full list of parameters, not just the ones from
1375 // the specialization.
1376 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001377 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001378 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001379 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001380}
1381
1382/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001383llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001384 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 return VTablePtrType;
1386
1387 ASTContext &Context = CGM.getContext();
1388
1389 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001390 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001391 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1392 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001393 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001394 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001395 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001396 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1397 return VTablePtrType;
1398}
1399
1400/// getVTableName - Get vtable name for the given Class.
1401StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001402 // Copy the gdb compatible name on the side and use its reference.
1403 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001404}
1405
Guy Benyei11169dd2012-12-18 14:30:41 +00001406/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1407/// debug info entry in EltTys vector.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001408void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001409 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1411
1412 // If there is a primary base then it will hold vtable info.
1413 if (RL.getPrimaryBase())
1414 return;
1415
1416 // If this class is not dynamic then there is not any vtable info to collect.
1417 if (!RD->isDynamicClass())
1418 return;
1419
1420 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001421 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001422 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001423 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001424 EltTys.push_back(VPTR);
1425}
1426
Eric Christopherb2a008c2013-05-16 00:45:12 +00001427/// getOrCreateRecordType - Emit record type's standalone debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001428llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001429 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001430 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001431 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 return T;
1433}
1434
1435/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1436/// debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001437llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001438 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001439 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001440 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001441 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 return T;
1443}
1444
David Blaikie483a9da2014-05-06 18:35:21 +00001445void CGDebugInfo::completeType(const EnumDecl *ED) {
1446 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1447 return;
1448 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001449 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001450 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001451 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001452 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001453 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001454 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001455 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001456}
1457
David Blaikieb2e86eb2013-08-15 20:49:17 +00001458void CGDebugInfo::completeType(const RecordDecl *RD) {
1459 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1460 !CGM.getLangOpts().CPlusPlus)
1461 completeRequiredType(RD);
1462}
1463
1464void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001465 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1466 return;
1467
David Blaikie6943dea2013-08-20 01:28:15 +00001468 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1469 if (CXXDecl->isDynamicClass())
1470 return;
1471
David Blaikieb2e86eb2013-08-15 20:49:17 +00001472 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001473 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001474 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001475 completeClassData(RD);
1476}
1477
1478void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1479 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001480 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001481 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001482 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001483 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001484 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001485 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001486 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001487 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001488 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001489}
1490
David Blaikie0e716b42014-03-03 23:48:23 +00001491static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1492 CXXRecordDecl::method_iterator End) {
1493 for (; I != End; ++I)
1494 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001495 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1496 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001497 return true;
1498 return false;
1499}
1500
1501static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1502 const RecordDecl *RD,
1503 const LangOptions &LangOpts) {
1504 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1505 return false;
1506
1507 if (!LangOpts.CPlusPlus)
1508 return false;
1509
1510 if (!RD->isCompleteDefinitionRequired())
1511 return true;
1512
1513 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1514
1515 if (!CXXDecl)
1516 return false;
1517
1518 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1519 return true;
1520
1521 TemplateSpecializationKind Spec = TSK_Undeclared;
1522 if (const ClassTemplateSpecializationDecl *SD =
1523 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1524 Spec = SD->getSpecializationKind();
1525
1526 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1527 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1528 CXXDecl->method_end()))
1529 return true;
1530
1531 return false;
1532}
1533
Guy Benyei11169dd2012-12-18 14:30:41 +00001534/// CreateType - get structure or union type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001535llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001536 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001537 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001538 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001539 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001540 T = getOrCreateRecordFwdDecl(
1541 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001542 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001543 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001544
David Blaikieb2e86eb2013-08-15 20:49:17 +00001545 return CreateTypeDefinition(Ty);
1546}
1547
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001548llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001549 RecordDecl *RD = Ty->getDecl();
1550
Guy Benyei11169dd2012-12-18 14:30:41 +00001551 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001552 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001553
1554 // Records and classes and unions can all be recursive. To handle them, we
1555 // first generate a debug descriptor for the struct as a forward declaration.
1556 // Then (if it is a definition) we go through and get debug info for all of
1557 // its members. Finally, we create a descriptor for the complete type (which
1558 // may refer to the forward decl if the struct is recursive) and replace all
1559 // uses of the forward declaration with the final definition.
1560
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001561 auto *FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001562 cast<llvm::DICompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001563
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001564 const RecordDecl *D = RD->getDefinition();
1565 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001566 return FwdDecl;
1567
David Blaikieadfbf992013-08-18 16:55:33 +00001568 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1569 CollectContainingType(CXXDecl, FwdDecl);
1570
Guy Benyei11169dd2012-12-18 14:30:41 +00001571 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001572 LexicalBlockStack.emplace_back(&*FwdDecl);
1573 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001574
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001576 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001577 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001578
1579 // Note: The split of CXXDecl information here is intentional, the
1580 // gdb tests will depend on a certain ordering at printout. The debug
1581 // information offsets are still correct if we merge them all together
1582 // though.
1583 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1584 if (CXXDecl) {
1585 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1586 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1587 }
1588
Eric Christopher91a31902013-01-16 01:22:32 +00001589 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001591 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001593
1594 LexicalBlockStack.pop_back();
1595 RegionMap.erase(Ty->getDecl());
1596
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001597 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001598 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001599
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001600 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001601 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001602 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001603
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001604 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001605 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001606}
1607
1608/// CreateType - get objective-c object type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001609llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1610 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001611 // Ignore protocols.
1612 return getOrCreateType(Ty->getBaseType(), Unit);
1613}
1614
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001615/// \return true if Getter has the default name for the property PD.
1616static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1617 const ObjCMethodDecl *Getter) {
1618 assert(PD);
1619 if (!Getter)
1620 return true;
1621
1622 assert(Getter->getDeclName().isObjCZeroArgSelector());
1623 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001624 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001625}
1626
1627/// \return true if Setter has the default name for the property PD.
1628static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1629 const ObjCMethodDecl *Setter) {
1630 assert(PD);
1631 if (!Setter)
1632 return true;
1633
1634 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001635 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001636 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001637}
1638
Guy Benyei11169dd2012-12-18 14:30:41 +00001639/// CreateType - get objective-c interface type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001640llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1641 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 ObjCInterfaceDecl *ID = Ty->getDecl();
1643 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001644 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001645
1646 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001647 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001648 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001649 auto RuntimeLang =
1650 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001651
1652 // If this is just a forward declaration return a special forward-declaration
1653 // debug type since we won't be able to lay out the entire type.
1654 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001655 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001656 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001657 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1658 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001659 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 return FwdDecl;
1661 }
1662
David Blaikieef8a9512014-05-05 23:23:53 +00001663 return CreateTypeDefinition(Ty, Unit);
1664}
1665
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001666llvm::DIModule *
1667CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod) {
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001668 auto it = ModuleRefCache.find(Mod.Signature);
1669 if (it != ModuleRefCache.end())
Adrian Prantl2388ead2015-06-30 18:01:05 +00001670 return it->second;
1671
1672 // Macro definitions that were defined with "-D" on the command line.
1673 SmallString<128> ConfigMacros;
1674 {
1675 llvm::raw_svector_ostream OS(ConfigMacros);
1676 const auto &PPOpts = CGM.getPreprocessorOpts();
1677 unsigned I = 0;
1678 // Translate the macro definitions back into a commmand line.
1679 for (auto &M : PPOpts.Macros) {
1680 if (++I > 1)
1681 OS << " ";
1682 const std::string &Macro = M.first;
1683 bool Undef = M.second;
1684 OS << "\"-" << (Undef ? 'U' : 'D');
1685 for (char c : Macro)
1686 switch (c) {
1687 case '\\' : OS << "\\\\"; break;
1688 case '"' : OS << "\\\""; break;
1689 default: OS << c;
1690 }
1691 OS << '\"';
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001692 }
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001693 }
Adrian Prantl2388ead2015-06-30 18:01:05 +00001694 llvm::DIBuilder DIB(CGM.getModule());
1695 auto *CU = DIB.createCompileUnit(
1696 TheCU->getSourceLanguage(), internString(Mod.ModuleName),
1697 internString(Mod.Path), TheCU->getProducer(), true, StringRef(), 0,
1698 internString(Mod.ASTFile), llvm::DIBuilder::FullDebug, Mod.Signature);
1699 llvm::DIModule *ModuleRef =
1700 DIB.createModule(CU, Mod.ModuleName, ConfigMacros, internString(Mod.Path),
1701 internString(CGM.getHeaderSearchOpts().Sysroot));
1702 DIB.finalize();
1703 ModuleRefCache.insert(std::make_pair(Mod.Signature, ModuleRef));
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00001704 return ModuleRef;
1705}
1706
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001707llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1708 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001709 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001710 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001711 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001712 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001713
1714 // Bit size, align and offset of the type.
1715 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1716 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1717
1718 unsigned Flags = 0;
1719 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001720 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001721
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001722 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001723 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001724 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001725
David Blaikieef8a9512014-05-05 23:23:53 +00001726 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001727 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001728
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001729 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001730 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001731 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001732
1733 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001734 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001735
1736 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1737 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001738 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001739 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001740 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001741 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001742
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001743 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001744 EltTys.push_back(InhTag);
1745 }
1746
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001747 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001748 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001749 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001750 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001751 unsigned PLine = getLineNumber(Loc);
1752 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1753 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001754 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1755 PD->getName(), PUnit, PLine,
1756 hasDefaultGetterName(PD, Getter) ? ""
1757 : getSelectorName(PD->getGetterName()),
1758 hasDefaultSetterName(PD, Setter) ? ""
1759 : getSelectorName(PD->getSetterName()),
1760 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001761 EltTys.push_back(PropertyNode);
1762 }
1763
1764 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1765 unsigned FieldNo = 0;
1766 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1767 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001768 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001769 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001770 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001771
Guy Benyei11169dd2012-12-18 14:30:41 +00001772 StringRef FieldName = Field->getName();
1773
1774 // Ignore unnamed fields.
1775 if (FieldName.empty())
1776 continue;
1777
1778 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001779 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001780 unsigned FieldLine = getLineNumber(Field->getLocation());
1781 QualType FType = Field->getType();
1782 uint64_t FieldSize = 0;
1783 unsigned FieldAlign = 0;
1784
1785 if (!FType->isIncompleteArrayType()) {
1786
1787 // Bit size, align and offset of the type.
1788 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001789 ? Field->getBitWidthValue(CGM.getContext())
1790 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 FieldAlign = CGM.getContext().getTypeAlign(FType);
1792 }
1793
1794 uint64_t FieldOffset;
1795 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1796 // We don't know the runtime offset of an ivar if we're using the
1797 // non-fragile ABI. For bitfields, use the bit offset into the first
1798 // byte of storage of the bitfield. For other fields, use zero.
1799 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001800 FieldOffset =
1801 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001802 FieldOffset %= CGM.getContext().getCharWidth();
1803 } else {
1804 FieldOffset = 0;
1805 }
1806 } else {
1807 FieldOffset = RL.getFieldOffset(FieldNo);
1808 }
1809
1810 unsigned Flags = 0;
1811 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001812 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001813 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001814 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001815 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001816 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001817
Craig Topper8a13c412014-05-21 05:09:00 +00001818 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001819 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001820 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001821 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001823 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001824 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001825 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001826 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1827 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001828 PropertyNode = DBuilder.createObjCProperty(
1829 PD->getName(), PUnit, PLine,
1830 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1831 PD->getGetterName()),
1832 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1833 PD->getSetterName()),
1834 PD->getPropertyAttributes(),
1835 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 }
1837 }
1838 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001839 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1840 FieldSize, FieldAlign, FieldOffset, Flags,
1841 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001842 EltTys.push_back(FieldTy);
1843 }
1844
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001845 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001846 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001847
Guy Benyei11169dd2012-12-18 14:30:41 +00001848 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001849 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001850}
1851
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001852llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1853 llvm::DIFile *Unit) {
1854 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 int64_t Count = Ty->getNumElements();
1856 if (Count == 0)
1857 // If number of elements are not known then this is an unbounded array.
1858 // Use Count == -1 to express such arrays.
1859 Count = -1;
1860
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001861 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001862 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001863
1864 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1865 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1866
1867 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1868}
1869
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001870llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 uint64_t Size;
1872 uint64_t Align;
1873
1874 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1875 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1876 Size = 0;
1877 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001878 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001879 } else if (Ty->isIncompleteArrayType()) {
1880 Size = 0;
1881 if (Ty->getElementType()->isIncompleteType())
1882 Align = 0;
1883 else
1884 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001885 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 Size = 0;
1887 Align = 0;
1888 } else {
1889 // Size and align of the whole array, not the element type.
1890 Size = CGM.getContext().getTypeSize(Ty);
1891 Align = CGM.getContext().getTypeAlign(Ty);
1892 }
1893
1894 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1895 // interior arrays, do we care? Why aren't nested arrays represented the
1896 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001897 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001898 QualType EltTy(Ty, 0);
1899 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1900 // If the number of elements is known, then count is that number. Otherwise,
1901 // it's -1. This allows us to represent a subrange with an array of 0
1902 // elements, like this:
1903 //
1904 // struct foo {
1905 // int x[0];
1906 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001907 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001908 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1909 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001910
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 // FIXME: Verify this is right for VLAs.
1912 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1913 EltTy = Ty->getElementType();
1914 }
1915
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001916 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001917
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001918 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1919 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001920}
1921
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001922llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1923 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001924 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1925 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001926}
1927
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001928llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1929 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001930 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1931 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001932}
1933
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001934llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1935 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001936 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1937 ? CGM.getContext().getTypeSize(Ty)
1938 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001939 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001940 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001941 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001942 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001943
1944 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001945 Ty->getPointeeType()->getAs<FunctionProtoType>();
1946 return DBuilder.createMemberPointerType(
1947 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1948 Ty->getClass(), FPT->getTypeQuals())),
1949 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001950 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001951}
1952
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001953llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001954 // Ignore the atomic wrapping
1955 // FIXME: What is the correct representation?
1956 return getOrCreateType(Ty->getValueType(), U);
1957}
1958
1959/// CreateEnumType - get enumeration type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001960llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001961 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001962 uint64_t Size = 0;
1963 uint64_t Align = 0;
1964 if (!ED->getTypeForDecl()->isIncompleteType()) {
1965 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1966 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1967 }
1968
Manman Rene0064d82013-08-29 23:19:58 +00001969 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1970
Guy Benyei11169dd2012-12-18 14:30:41 +00001971 // If this is just a forward declaration, construct an appropriately
1972 // marked node and just return it.
1973 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001974 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001975 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001976 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 unsigned Line = getLineNumber(ED->getLocation());
1978 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001979 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001980 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001981 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001982 ReplaceMap.emplace_back(
1983 std::piecewise_construct, std::make_tuple(Ty),
1984 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001985 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001986 }
1987
David Blaikie483a9da2014-05-06 18:35:21 +00001988 return CreateTypeDefinition(Ty);
1989}
1990
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001991llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001992 const EnumDecl *ED = Ty->getDecl();
1993 uint64_t Size = 0;
1994 uint64_t Align = 0;
1995 if (!ED->getTypeForDecl()->isIncompleteType()) {
1996 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1997 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1998 }
1999
2000 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2001
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002002 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002003 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002005 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00002006 Enumerators.push_back(DBuilder.createEnumerator(
2007 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002008 }
2009
2010 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002011 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00002012
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002013 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002015 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00002016 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002017 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002018 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
2019 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
2020 Line, Size, Align, EltArray, ClassTy,
2021 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002022}
2023
David Blaikie05491062013-01-21 04:37:12 +00002024static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
2025 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 do {
Adrian Prantl179af902013-09-26 21:35:50 +00002027 Qualifiers InnerQuals = T.getLocalQualifiers();
2028 // Qualifiers::operator+() doesn't like it if you add a Qualifier
2029 // that is already there.
2030 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
2031 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00002032 QualType LastT = T;
2033 switch (T->getTypeClass()) {
2034 default:
David Blaikie05491062013-01-21 04:37:12 +00002035 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00002036 case Type::TemplateSpecialization: {
2037 const auto *Spec = cast<TemplateSpecializationType>(T);
2038 if (Spec->isTypeAlias())
2039 return C.getQualifiedType(T.getTypePtr(), Quals);
2040 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00002041 break;
2042 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 case Type::TypeOfExpr:
2044 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2045 break;
2046 case Type::TypeOf:
2047 T = cast<TypeOfType>(T)->getUnderlyingType();
2048 break;
2049 case Type::Decltype:
2050 T = cast<DecltypeType>(T)->getUnderlyingType();
2051 break;
2052 case Type::UnaryTransform:
2053 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2054 break;
2055 case Type::Attributed:
2056 T = cast<AttributedType>(T)->getEquivalentType();
2057 break;
2058 case Type::Elaborated:
2059 T = cast<ElaboratedType>(T)->getNamedType();
2060 break;
2061 case Type::Paren:
2062 T = cast<ParenType>(T)->getInnerType();
2063 break;
David Blaikie05491062013-01-21 04:37:12 +00002064 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002065 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002066 break;
2067 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002068 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002069 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002070 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002071 break;
2072 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002073
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002075 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 } while (true);
2077}
2078
Eric Christopher0fdcb312013-05-16 00:52:20 +00002079/// getType - Get the type from the cache or return null type if it doesn't
2080/// exist.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002081llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002082
2083 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002084 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002085
David Blaikief427b002014-05-06 03:42:01 +00002086 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002087 if (it != TypeCache.end()) {
2088 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002089 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002090 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002091 }
2092
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002093 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002094}
2095
David Blaikie0e716b42014-03-03 23:48:23 +00002096void CGDebugInfo::completeTemplateDefinition(
2097 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002098 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2099 return;
2100
David Blaikie0e716b42014-03-03 23:48:23 +00002101 completeClassData(&SD);
2102 // In case this type has no member function definitions being emitted, ensure
2103 // it is retained
2104 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2105}
2106
Guy Benyei11169dd2012-12-18 14:30:41 +00002107/// getOrCreateType - Get the type from the cache or create a new
2108/// one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002109llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002110 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002111 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002112
2113 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002114 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002115
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002116 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002117 return T;
2118
2119 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002120 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002121 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002122
2123 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002124 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002125
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 return Res;
2127}
2128
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002129/// Currently the checksum of an interface includes the number of
2130/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002131unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002132 // The assumption is that the number of ivars can only increase
2133 // monotonically, so it is safe to just use their current number as
2134 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002135 unsigned Sum = 0;
2136 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002137 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002138 ++Sum;
2139
2140 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002141}
2142
2143ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2144 switch (Ty->getTypeClass()) {
2145 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002146 return getObjCInterfaceDecl(
2147 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002148 case Type::ObjCInterface:
2149 return cast<ObjCInterfaceType>(Ty)->getDecl();
2150 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002151 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002152 }
2153}
2154
Guy Benyei11169dd2012-12-18 14:30:41 +00002155/// CreateTypeNode - Create a new debug type node.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002156llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002157 // Handle qualifiers, which recursively handles what they refer to.
2158 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002159 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002160
Guy Benyei11169dd2012-12-18 14:30:41 +00002161 // Work out details of type.
2162 switch (Ty->getTypeClass()) {
2163#define TYPE(Class, Base)
2164#define ABSTRACT_TYPE(Class, Base)
2165#define NON_CANONICAL_TYPE(Class, Base)
2166#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2167#include "clang/AST/TypeNodes.def"
2168 llvm_unreachable("Dependent types cannot show up in debug information");
2169
2170 case Type::ExtVector:
2171 case Type::Vector:
2172 return CreateType(cast<VectorType>(Ty), Unit);
2173 case Type::ObjCObjectPointer:
2174 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2175 case Type::ObjCObject:
2176 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2177 case Type::ObjCInterface:
2178 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2179 case Type::Builtin:
2180 return CreateType(cast<BuiltinType>(Ty));
2181 case Type::Complex:
2182 return CreateType(cast<ComplexType>(Ty));
2183 case Type::Pointer:
2184 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002185 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002186 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002187 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002188 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002189 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002190 case Type::BlockPointer:
2191 return CreateType(cast<BlockPointerType>(Ty), Unit);
2192 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002193 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002195 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002197 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 case Type::FunctionProto:
2199 case Type::FunctionNoProto:
2200 return CreateType(cast<FunctionType>(Ty), Unit);
2201 case Type::ConstantArray:
2202 case Type::VariableArray:
2203 case Type::IncompleteArray:
2204 return CreateType(cast<ArrayType>(Ty), Unit);
2205
2206 case Type::LValueReference:
2207 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2208 case Type::RValueReference:
2209 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2210
2211 case Type::MemberPointer:
2212 return CreateType(cast<MemberPointerType>(Ty), Unit);
2213
2214 case Type::Atomic:
2215 return CreateType(cast<AtomicType>(Ty), Unit);
2216
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002218 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2219
David Blaikie42edade2014-11-11 20:44:45 +00002220 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002221 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 case Type::Elaborated:
2223 case Type::Paren:
2224 case Type::SubstTemplateTypeParm:
2225 case Type::TypeOfExpr:
2226 case Type::TypeOf:
2227 case Type::Decltype:
2228 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002229 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002230 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002231 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002232
David Blaikie42edade2014-11-11 20:44:45 +00002233 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002234}
2235
2236/// getOrCreateLimitedType - Get the type from the cache or create a new
2237/// limited type if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002238llvm::DIType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2239 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002240 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002241
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002242 auto *T = cast_or_null<llvm::DICompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002243
2244 // We may have cached a forward decl when we could have created
2245 // a non-forward decl. Go ahead and create a non-forward decl
2246 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002247 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002248 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002249
2250 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002251 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002252
2253 // Propagate members from the declaration to the definition
2254 // CreateType(const RecordType*) will overwrite this with the members in the
2255 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002256 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002257
Guy Benyei11169dd2012-12-18 14:30:41 +00002258 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002259 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 return Res;
2261}
2262
2263// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002264llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002265 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002266
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002268 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002269 unsigned Line = getLineNumber(RD->getLocation());
2270 StringRef RDName = getClassName(RD);
2271
David Blaikiebe822ed2015-07-01 18:07:22 +00002272 llvm::DIScope *RDContext =
2273 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002274
David Blaikied2785892013-08-18 17:36:19 +00002275 // If we ended up creating the type during the context chain construction,
2276 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002277 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002278 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002279 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002280 return T;
David Blaikied2785892013-08-18 17:36:19 +00002281
Adrian Prantl381e7552014-02-04 21:29:50 +00002282 // If this is just a forward or incomplete declaration, construct an
2283 // appropriately marked node and just return it.
2284 const RecordDecl *D = RD->getDefinition();
2285 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002286 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002287
2288 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2289 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002290
Manman Rene0064d82013-08-29 23:19:58 +00002291 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2292
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002293 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002294 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2295 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002296
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002297 RegionMap[Ty->getDecl()].reset(RealDecl);
2298 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002299
David Blaikieadfbf992013-08-18 16:55:33 +00002300 if (const ClassTemplateSpecializationDecl *TSpecial =
2301 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002302 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002303 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002304 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002305}
2306
David Blaikieadfbf992013-08-18 16:55:33 +00002307void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002308 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002309 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002310 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002311 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2312 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002313 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002314 while (1) {
2315 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2316 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2317 if (PBT && !BRL.isPrimaryBaseVirtual())
2318 PBase = PBT;
2319 else
2320 break;
2321 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002322 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002323 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2324 getOrCreateFile(RD->getLocation())));
2325 } else if (RD->isDynamicClass())
2326 ContainingType = RealDecl;
2327
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002328 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002329}
2330
Guy Benyei11169dd2012-12-18 14:30:41 +00002331/// CreateMemberType - Create new member and increase Offset by FType's size.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002332llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002333 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002334 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2336 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002337 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002338 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 *Offset += FieldSize;
2340 return Ty;
2341}
2342
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002343void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002344 StringRef &Name,
2345 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002346 llvm::DIScope *&FDContext,
2347 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002348 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002349 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2350 Name = getFunctionName(FD);
2351 // Use mangled name as linkage name for C/C++ functions.
2352 if (FD->hasPrototype()) {
2353 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002354 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002355 }
2356 // No need to replicate the linkage name if it isn't different from the
2357 // subprogram name, no need to have it at all unless coverage is enabled or
2358 // debug is set to more than just line tables.
2359 if (LinkageName == Name ||
2360 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2361 !CGM.getCodeGenOpts().EmitGcovNotes &&
2362 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2363 LinkageName = StringRef();
2364
2365 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2366 if (const NamespaceDecl *NSDecl =
2367 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2368 FDContext = getOrCreateNameSpace(NSDecl);
2369 else if (const RecordDecl *RDecl =
2370 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2371 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2372 // Collect template parameters.
2373 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2374 }
2375}
2376
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002377void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002378 unsigned &LineNo, QualType &T,
2379 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002380 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002381 Unit = getOrCreateFile(VD->getLocation());
2382 LineNo = getLineNumber(VD->getLocation());
2383
2384 setLocation(VD->getLocation());
2385
2386 T = VD->getType();
2387 if (T->isIncompleteArrayType()) {
2388 // CodeGen turns int[] into int[1] so we'll do the same here.
2389 llvm::APInt ConstVal(32, 1);
2390 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2391
2392 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2393 ArrayType::Normal, 0);
2394 }
2395
2396 Name = VD->getName();
2397 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2398 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2399 LinkageName = CGM.getMangledName(VD);
2400 if (LinkageName == Name)
2401 LinkageName = StringRef();
2402
2403 // Since we emit declarations (DW_AT_members) for static members, place the
2404 // definition of those static members in the namespace they were declared in
2405 // in the source code (the lexical decl context).
2406 // FIXME: Generalize this for even non-member global variables where the
2407 // declaration and definition may have different lexical decl contexts, once
2408 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002409 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2410 : VD->getDeclContext();
2411 // When a record type contains an in-line initialization of a static data
2412 // member, and the record type is marked as __declspec(dllexport), an implicit
2413 // definition of the member will be created in the record context. DWARF
2414 // doesn't seem to have a nice way to describe this in a form that consumers
2415 // are likely to understand, so fake the "normal" situation of a definition
2416 // outside the class by putting it in the global scope.
2417 if (DC->isRecord())
2418 DC = CGM.getContext().getTranslationUnitDecl();
David Blaikiebe822ed2015-07-01 18:07:22 +00002419 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002420}
2421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002422llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002423CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002424 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002425 StringRef Name, LinkageName;
2426 unsigned Flags = 0;
2427 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002428 llvm::DIFile *Unit = getOrCreateFile(Loc);
2429 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002430 unsigned Line = getLineNumber(Loc);
2431
2432 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2433 TParamsArray, Flags);
2434 // Build function type.
2435 SmallVector<QualType, 16> ArgTypes;
2436 for (const ParmVarDecl *Parm: FD->parameters())
2437 ArgTypes.push_back(Parm->getType());
2438 QualType FnType =
2439 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2440 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002442 DContext, Name, LinkageName, Unit, Line,
2443 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2444 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2445 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002446 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002447 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2448 std::make_tuple(CanonDecl),
2449 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002450 return SP;
2451}
2452
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002453llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002454CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2455 QualType T;
2456 StringRef Name, LinkageName;
2457 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002458 llvm::DIFile *Unit = getOrCreateFile(Loc);
2459 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002460 unsigned Line = getLineNumber(Loc);
2461
2462 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002463 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2464 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2465 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002466 FwdDeclReplaceMap.emplace_back(
2467 std::piecewise_construct,
2468 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2469 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002470 return GV;
2471}
2472
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002473llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002474 // We only need a declaration (not a definition) of the type - so use whatever
2475 // we would otherwise do to get a type for a pointee. (forward declarations in
2476 // limited debug info, full definitions (if the type definition is available)
2477 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002478 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2479 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002480 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002481 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002482
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002483 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002484 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002485
2486 // No definition for now. Emit a forward definition that might be
2487 // merged with a potential upcoming definition.
2488 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2489 return getFunctionForwardDeclaration(FD);
2490 else if (const auto *VD = dyn_cast<VarDecl>(D))
2491 return getGlobalVariableForwardDeclaration(VD);
2492
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002493 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002494}
2495
Guy Benyei11169dd2012-12-18 14:30:41 +00002496/// getFunctionDeclaration - Return debug info descriptor to describe method
2497/// declaration for the given method definition.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002498llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002499 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002500 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002501
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002503 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002504 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002505
2506 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002507 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002508
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002509 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002510 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002511 if (const CXXMethodDecl *MD =
2512 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002513 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002514 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002515 }
2516 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002518 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002519 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002520 return SP;
2521 }
2522
Aaron Ballman86c93902014-03-06 23:45:36 +00002523 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002524 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002526 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002527 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002528 return SP;
2529 }
2530 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002531 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002532}
2533
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002534// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002535// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002536llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002537 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002538 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002539 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002540 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2541 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002542 return DBuilder.createSubroutineType(F,
2543 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002544
2545 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2546 return getOrCreateMethodType(Method, F);
2547 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2548 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002549 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002550
2551 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002552 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002553
2554 // Replace the instancetype keyword with the actual type.
2555 if (ResultTy == CGM.getContext().getObjCInstanceType())
2556 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002557 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002558
Adrian Prantl7bec9032013-05-10 21:08:31 +00002559 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002561 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002562 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002564 Elts.push_back(DBuilder.createArtificialType(
2565 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002567 for (const auto *PI : OMethod->params())
2568 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002569 // Variadic methods need a special marker at the end of the type list.
2570 if (OMethod->isVariadic())
2571 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002572
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002573 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 return DBuilder.createSubroutineType(F, EltTypeArray);
2575 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002576
Adrian Prantl800faef2014-02-25 23:42:18 +00002577 // Handle variadic function types; they need an additional
2578 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002579 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2580 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002581 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002582 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2583 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2584 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2585 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2586 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002587 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002588 return DBuilder.createSubroutineType(F, EltTypeArray);
2589 }
2590
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002591 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002592}
2593
2594/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002595void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2596 SourceLocation ScopeLoc, QualType FnType,
2597 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002598
2599 StringRef Name;
2600 StringRef LinkageName;
2601
2602 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2603
2604 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002605 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002606
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002608 llvm::DIFile *Unit = getOrCreateFile(Loc);
2609 llvm::DIScope *FDContext = Unit;
2610 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002611 if (!HasDecl) {
2612 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002613 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002615 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002616 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002618 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002619 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002620 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002621 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 return;
2623 }
2624 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002625 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2626 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002627 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2628 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002629 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 } else {
2631 // Use llvm function name.
2632 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002633 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 }
2635 if (!Name.empty() && Name[0] == '\01')
2636 Name = Name.substr(1);
2637
Adrian Prantl42d71b92014-04-10 23:21:53 +00002638 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002639 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002640 // Artificial functions without a location should not silently reuse CurLoc.
2641 if (Loc.isInvalid())
2642 CurLoc = SourceLocation();
2643 }
2644 unsigned LineNo = getLineNumber(Loc);
2645 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002646
Eric Christopher8018e412014-03-27 18:50:35 +00002647 // FIXME: The function declaration we're constructing here is mostly reusing
2648 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2649 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2650 // all subprograms instead of the actual context since subprogram definitions
2651 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002652 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002653 FDContext, Name, LinkageName, Unit, LineNo,
2654 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2655 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002656 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002657 // We might get here with a VarDecl in the case we're generating
2658 // code for the initialization of globals. Do not record these decls
2659 // as they will overwrite the actual VarDecl Decl in the cache.
2660 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002661 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002662
Adrian Prantlbebb8932014-03-21 21:01:58 +00002663 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002664 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002665
Guy Benyei11169dd2012-12-18 14:30:41 +00002666 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002667 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002668}
2669
2670/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002671/// information in the source file. If the location is invalid, the
2672/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002673void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002674 // Update our current location
2675 setLocation(Loc);
2676
Eric Christophere7b87e52014-10-26 23:40:33 +00002677 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2678 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002679
Adrian Prantle83b1302014-01-07 22:05:52 +00002680 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002681 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002682 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002683}
2684
2685/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2686/// the stack.
2687void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002688 llvm::MDNode *Back = nullptr;
2689 if (!LexicalBlockStack.empty())
2690 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002691 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002692 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002693 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002694}
2695
2696/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2697/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002698void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2699 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 // Set our current location.
2701 setLocation(Loc);
2702
Guy Benyei11169dd2012-12-18 14:30:41 +00002703 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002704 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2705 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002706
2707 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2708 return;
2709
2710 // Create a new lexical block and push it on the stack.
2711 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002712}
2713
2714/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2715/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002716void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2717 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2719
2720 // Provide an entry in the line table for the end of the block.
2721 EmitLocation(Builder, Loc);
2722
David Blaikie60a877b2014-10-22 19:34:33 +00002723 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2724 return;
2725
Guy Benyei11169dd2012-12-18 14:30:41 +00002726 LexicalBlockStack.pop_back();
2727}
2728
2729/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2730void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2731 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2732 unsigned RCount = FnBeginRegionCount.back();
2733 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2734
2735 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002736 while (LexicalBlockStack.size() != RCount) {
2737 // Provide an entry in the line table for the end of the block.
2738 EmitLocation(Builder, CurLoc);
2739 LexicalBlockStack.pop_back();
2740 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002741 FnBeginRegionCount.pop_back();
2742}
2743
Eric Christopherb2a008c2013-05-16 00:45:12 +00002744// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002745// See BuildByRefType.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002746llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002747 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002748
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002749 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002750 QualType FType;
2751 uint64_t FieldSize, FieldOffset;
2752 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002753
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002754 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002755 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002756
2757 FieldOffset = 0;
2758 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2759 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2760 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2761 FType = CGM.getContext().IntTy;
2762 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2763 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2764
2765 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2766 if (HasCopyAndDispose) {
2767 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002768 EltTys.push_back(
2769 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2770 EltTys.push_back(
2771 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 }
2773 bool HasByrefExtendedLayout;
2774 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002775 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2776 HasByrefExtendedLayout) &&
2777 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002778 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002779 EltTys.push_back(
2780 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002781 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002782
Guy Benyei11169dd2012-12-18 14:30:41 +00002783 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2784 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002785 CGM.getTarget().getPointerAlign(0))) {
2786 CharUnits FieldOffsetInBytes =
2787 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2788 CharUnits AlignedOffsetInBytes =
2789 FieldOffsetInBytes.RoundUpToAlignment(Align);
2790 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002791
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 if (NumPaddingBytes.isPositive()) {
2793 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2794 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2795 pad, ArrayType::Normal, 0);
2796 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2797 }
2798 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002799
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002801 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002802 FieldSize = CGM.getContext().getTypeSize(FType);
2803 FieldAlign = CGM.getContext().toBits(Align);
2804
Eric Christopherb2a008c2013-05-16 00:45:12 +00002805 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002806 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2807 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002808 EltTys.push_back(FieldTy);
2809 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002810
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002811 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002812
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002813 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002814
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002816 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002817}
2818
2819/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002820void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002821 llvm::Value *Storage, unsigned ArgNo,
2822 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002823 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2825
David Blaikie7fceebf2013-08-19 03:37:48 +00002826 bool Unwritten =
2827 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2828 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002829 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002830 if (!Unwritten)
2831 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002832 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 uint64_t XOffset = 0;
2834 if (VD->hasAttr<BlocksAttr>())
2835 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002836 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 Ty = getOrCreateType(VD->getType(), Unit);
2838
2839 // If there is no debug info for this type then do not emit debug info
2840 // for this variable.
2841 if (!Ty)
2842 return;
2843
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002845 unsigned Line = 0;
2846 unsigned Column = 0;
2847 if (!Unwritten) {
2848 Line = getLineNumber(VD->getLocation());
2849 Column = getColumnNumber(VD->getLocation());
2850 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002851 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002852 unsigned Flags = 0;
2853 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002854 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 // If this is the first argument and it is implicit then
2856 // give it an object pointer flag.
2857 // FIXME: There has to be a better way to do this, but for static
2858 // functions there won't be an implicit param at arg1 and
2859 // otherwise it is 'self' or 'this'.
2860 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002861 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002862 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002863 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2864 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002865 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002866
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002867 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002868
2869 StringRef Name = VD->getName();
2870 if (!Name.empty()) {
2871 if (VD->hasAttr<BlocksAttr>()) {
2872 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002873 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002874 // offset of __forwarding field
2875 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002876 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002877 Expr.push_back(offset.getQuantity());
2878 Expr.push_back(llvm::dwarf::DW_OP_deref);
2879 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 // offset of x field
2881 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002882 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002883
2884 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002885 auto *D = DBuilder.createLocalVariable(Tag, Scope, VD->getName(), Unit,
2886 Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002887
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002889 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2890 llvm::DebugLoc::get(Line, Column, Scope),
2891 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002892 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002893 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002894 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002895 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2896 // If VD is an anonymous union then Storage represents value for
2897 // all union fields.
2898 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2899 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002900 // GDB has trouble finding local variables in anonymous unions, so we emit
2901 // artifical local variables for each of the members.
2902 //
2903 // FIXME: Remove this code as soon as GDB supports this.
2904 // The debug info verifier in LLVM operates based on the assumption that a
2905 // variable has the same size as its storage and we had to disable the check
2906 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002907 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002908 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002909 StringRef FieldName = Field->getName();
2910
2911 // Ignore unnamed fields. Do not ignore unnamed records.
2912 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2913 continue;
2914
2915 // Use VarDecl's Tag, Scope and Line number.
2916 auto *D = DBuilder.createLocalVariable(
2917 Tag, Scope, FieldName, Unit, Line, FieldTy,
Adrian Prantl00820ab2015-04-29 16:52:31 +00002918 CGM.getLangOpts().Optimize, Flags | llvm::DINode::FlagArtificial,
2919 ArgNo);
Adrian Prantl0d820892015-04-29 15:05:50 +00002920
2921 // Insert an llvm.dbg.declare into the current block.
2922 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2923 llvm::DebugLoc::get(Line, Column, Scope),
2924 Builder.GetInsertBlock());
2925 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002926 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002928
2929 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002930 auto *D =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002931 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2932 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002933
2934 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002935 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2936 llvm::DebugLoc::get(Line, Column, Scope),
2937 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002938}
2939
2940void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2941 llvm::Value *Storage,
2942 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002943 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002944 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2945}
2946
Adrian Prantlde17db32013-03-29 19:20:29 +00002947/// Look up the completed type for a self pointer in the TypeCache and
2948/// create a copy of it with the ObjectPointer and Artificial flags
2949/// set. If the type is not cached, a new one is created. This should
2950/// never happen though, since creating a type for the implicit self
2951/// argument implies that we already parsed the interface definition
2952/// and the ivar declarations in the implementation.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002953llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2954 llvm::DIType *Ty) {
2955 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002956 if (CachedTy)
2957 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002958 return DBuilder.createObjectPointerType(Ty);
2959}
2960
Eric Christophere7b87e52014-10-26 23:40:33 +00002961void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2962 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002963 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002964 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002965 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002966
Craig Topper8a13c412014-05-21 05:09:00 +00002967 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002969
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002971
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002973 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2974 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002975 if (isByRef)
2976 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002977 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 Ty = getOrCreateType(VD->getType(), Unit);
2979
2980 // Self is passed along as an implicit non-arg variable in a
2981 // block. Mark it as the object pointer.
2982 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002983 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002984
2985 // Get location information.
2986 unsigned Line = getLineNumber(VD->getLocation());
2987 unsigned Column = getColumnNumber(VD->getLocation());
2988
2989 const llvm::DataLayout &target = CGM.getDataLayout();
2990
2991 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002992 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002993 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2994
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002995 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002996 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002997 addr.push_back(llvm::dwarf::DW_OP_deref);
2998 addr.push_back(llvm::dwarf::DW_OP_plus);
2999 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003000 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003001 addr.push_back(llvm::dwarf::DW_OP_deref);
3002 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00003004 offset =
3005 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003006 addr.push_back(offset.getQuantity());
3007 addr.push_back(llvm::dwarf::DW_OP_deref);
3008 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00003009 // offset of x field
3010 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00003011 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 }
3013
3014 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003015 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003016 llvm::dwarf::DW_TAG_auto_variable,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003017 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003018 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00003019
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003021 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
3022 if (InsertPoint)
3023 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3024 InsertPoint);
3025 else
3026 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
3027 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003028}
3029
3030/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
3031/// variable declaration.
3032void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
3033 unsigned ArgNo,
3034 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003035 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
3037}
3038
3039namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00003040struct BlockLayoutChunk {
3041 uint64_t OffsetInBits;
3042 const BlockDecl::Capture *Capture;
3043};
3044bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3045 return l.OffsetInBits < r.OffsetInBits;
3046}
Guy Benyei11169dd2012-12-18 14:30:41 +00003047}
3048
3049void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003050 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003051 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003052 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003054 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 ASTContext &C = CGM.getContext();
3056 const BlockDecl *blockDecl = block.getBlockDecl();
3057
3058 // Collect some general information about the block's location.
3059 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003060 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003061 unsigned line = getLineNumber(loc);
3062 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003063
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 // Build the debug-info type for the block literal.
3065 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3066
3067 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003068 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003069
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003070 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3072 blockLayout->getElementOffsetInBits(0),
3073 tunit, tunit));
3074 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3075 blockLayout->getElementOffsetInBits(1),
3076 tunit, tunit));
3077 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3078 blockLayout->getElementOffsetInBits(2),
3079 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003080 auto *FnTy = block.getBlockExpr()->getFunctionType();
3081 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3082 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 blockLayout->getElementOffsetInBits(3),
3084 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003085 fields.push_back(createFieldType(
3086 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3087 ? C.getBlockDescriptorExtendedType()
3088 : C.getBlockDescriptorType()),
3089 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003090
3091 // We want to sort the captures by offset, not because DWARF
3092 // requires this, but because we're paranoid about debuggers.
3093 SmallVector<BlockLayoutChunk, 8> chunks;
3094
3095 // 'this' capture.
3096 if (blockDecl->capturesCXXThis()) {
3097 BlockLayoutChunk chunk;
3098 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003099 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003100 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 chunks.push_back(chunk);
3102 }
3103
3104 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003105 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003106 const VarDecl *variable = capture.getVariable();
3107 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3108
3109 // Ignore constant captures.
3110 if (captureInfo.isConstant())
3111 continue;
3112
3113 BlockLayoutChunk chunk;
3114 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003115 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003116 chunk.Capture = &capture;
3117 chunks.push_back(chunk);
3118 }
3119
3120 // Sort by offset.
3121 llvm::array_pod_sort(chunks.begin(), chunks.end());
3122
Eric Christophere7b87e52014-10-26 23:40:33 +00003123 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3124 e = chunks.end();
3125 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003126 uint64_t offsetInBits = i->OffsetInBits;
3127 const BlockDecl::Capture *capture = i->Capture;
3128
3129 // If we have a null capture, this must be the C++ 'this' capture.
3130 if (!capture) {
3131 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003132 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 QualType type = method->getThisType(C);
3134
3135 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3136 offsetInBits, tunit, tunit));
3137 continue;
3138 }
3139
3140 const VarDecl *variable = capture->getVariable();
3141 StringRef name = variable->getName();
3142
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003143 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003145 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003146
3147 // FIXME: this creates a second copy of this type!
3148 uint64_t xoffset;
3149 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003150 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3151 fieldType =
3152 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3153 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003155 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3156 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003157 }
3158 fields.push_back(fieldType);
3159 }
3160
3161 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003162 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3163 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003164
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003165 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003166
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003167 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003168 tunit, typeName.str(), tunit, line,
3169 CGM.getContext().toBits(block.BlockSize),
3170 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003171 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3172
3173 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003174 unsigned flags = llvm::DINode::FlagArtificial;
3175 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003176
3177 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003178 auto *debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003179 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3180 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003181
Adrian Prantl616bef42013-03-14 21:52:59 +00003182 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003183 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003184 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003185 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003186 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003187 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003188
Adrian Prantl616bef42013-03-14 21:52:59 +00003189 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003190 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3191 llvm::DebugLoc::get(line, column, scope),
3192 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003193}
3194
David Blaikie6943dea2013-08-20 01:28:15 +00003195/// If D is an out-of-class definition of a static data member of a class, find
3196/// its corresponding in-class declaration.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003197llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003198CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3199 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003200 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003201
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003202 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003203 if (MI != StaticDataMemberCache.end()) {
3204 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003205 return cast<llvm::DIDerivedType>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003206 }
David Blaikiece763042013-08-20 21:49:21 +00003207
3208 // If the member wasn't found in the cache, lazily construct and add it to the
3209 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003210 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003211 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003212 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003213 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003214}
3215
Eric Christophercab9fae2014-04-10 05:20:00 +00003216/// Recursively collect all of the member fields of a global anonymous decl and
3217/// create static variables for them. The first time this is called it needs
3218/// 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 +00003219llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3220 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3221 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3222 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003223
3224 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003225 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003226 StringRef FieldName = Field->getName();
3227
3228 // Ignore unnamed fields, but recurse into anonymous records.
3229 if (FieldName.empty()) {
3230 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3231 if (RT)
3232 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3233 Var, DContext);
3234 continue;
3235 }
3236 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003237 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3238 LineNo, FieldTy,
3239 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003240 }
3241 return GV;
3242}
3243
Guy Benyei11169dd2012-12-18 14:30:41 +00003244/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003245void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003246 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003247 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003249 llvm::DIFile *Unit = nullptr;
3250 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003251 unsigned LineNo;
3252 StringRef DeclName, LinkageName;
3253 QualType T;
3254 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003255
3256 // Attempt to store one global variable for the declaration - even if we
3257 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003258 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003259
3260 // If this is an anonymous union then we'll want to emit a global
3261 // variable for each member of the anonymous union so that it's possible
3262 // to find the name of any field in the union.
3263 if (T->isUnionType() && DeclName.empty()) {
3264 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003265 assert(RD->isAnonymousStructOrUnion() &&
3266 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003267 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3268 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003269 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003270 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3271 Var->hasInternalLinkage(), Var,
3272 getOrCreateStaticDataMemberDeclarationOrNull(D));
3273 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003274 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003275}
3276
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003277/// EmitGlobalVariable - Emit global variable's debug info.
3278void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3279 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003280 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003281 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003282 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003283 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003284 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003285 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3286 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3287 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3288 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3289 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003290 // Do not use global variables for enums.
3291 //
3292 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003293 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003294 return;
David Blaikiea15565562014-04-04 20:56:17 +00003295 // Do not emit separate definitions for function local const/statics.
3296 if (isa<FunctionDecl>(VD->getDeclContext()))
3297 return;
David Blaikiebb113912014-04-05 07:23:17 +00003298 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003299 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003300 if (VarD->isStaticDataMember()) {
3301 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3302 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003303 // Ensure that the type is retained even though it's otherwise unreferenced.
3304 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003305 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003306 return;
3307 }
3308
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003309 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003310 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3311
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003312 auto &GV = DeclCache[VD];
3313 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003314 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003315 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003316 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003317 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003318}
3319
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003320llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003321 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003322 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003323 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003324}
3325
David Blaikie9f88fe82013-04-22 06:13:21 +00003326void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003327 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3328 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003329 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003330 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3331 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003332 getLineNumber(UD.getLocation()));
3333}
3334
David Blaikiebd483762013-05-20 04:58:53 +00003335void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3336 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3337 return;
3338 assert(UD.shadow_size() &&
3339 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3340 // Emitting one decl is sufficient - debuggers can detect that this is an
3341 // overloaded name & provide lookup for all the overloads.
3342 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003343 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003344 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003345 DBuilder.createImportedDeclaration(
3346 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3347 getLineNumber(USD.getLocation()));
3348}
3349
Adrian Prantlc4bb47e2015-06-30 17:39:51 +00003350void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
3351 auto *Reader = CGM.getContext().getExternalSource();
3352 auto Info = Reader->getSourceDescriptor(*ID.getImportedModule());
3353 DBuilder.createImportedDeclaration(
3354 getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
3355 getOrCreateModuleRef(Info),
3356 getLineNumber(ID.getLocation()));
3357}
3358
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003359llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003360CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3361 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003362 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003363 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003364 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003365 return cast<llvm::DIImportedEntity>(VH);
3366 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003367 if (const NamespaceAliasDecl *Underlying =
3368 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3369 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003370 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003371 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3372 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3373 NA.getName());
3374 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003375 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003376 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3377 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3378 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003379 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003380 return R;
3381}
3382
Guy Benyei11169dd2012-12-18 14:30:41 +00003383/// getOrCreateNamesSpace - Return namespace descriptor for the given
3384/// namespace decl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003385llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003386CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003387 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003388 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003389 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003390 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003391
Guy Benyei11169dd2012-12-18 14:30:41 +00003392 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003393 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3394 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003395 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003396 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003397 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003398 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003399 return NS;
3400}
3401
3402void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003403 // Creating types might create further types - invalidating the current
3404 // element and the size(), so don't cache/reference them.
3405 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3406 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003407 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003408 ? CreateTypeDefinition(E.Type, E.Unit)
3409 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003410 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003411 }
3412
David Blaikief427b002014-05-06 03:42:01 +00003413 for (auto p : ReplaceMap) {
3414 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003415 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003416 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003417
David Blaikief427b002014-05-06 03:42:01 +00003418 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003419 assert(it != TypeCache.end());
3420 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003421
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003422 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3423 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003424 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003425
Frederic Rissd253ed62014-11-18 03:40:51 +00003426 for (const auto &p : FwdDeclReplaceMap) {
3427 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003428 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003429 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003430
3431 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003432 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003433 // with ourselves, that will destroy the temporary MDNode and
3434 // replace it with a standard one, avoiding leaking memory.
3435 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003436 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003437 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003438 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003439
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003440 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003441 }
3442
Adrian Prantl73409ce2013-03-11 18:33:46 +00003443 // We keep our own list of retained types, because we need to look
3444 // up the final type in the type cache.
3445 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3446 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003447 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003448
Guy Benyei11169dd2012-12-18 14:30:41 +00003449 DBuilder.finalize();
3450}
David Blaikie66088d52014-09-24 17:01:27 +00003451
3452void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3453 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3454 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003455
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003456 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003457 // Don't ignore in case of explicit cast where it is referenced indirectly.
3458 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003459}