blob: a139ce0e07363871ea6f5de6a0658fa06f0159b1 [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"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Module.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000038#include "llvm/Support/Dwarf.h"
39#include "llvm/Support/FileSystem.h"
Adrian Prantl0630eb72013-12-18 21:48:18 +000040#include "llvm/Support/Path.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041using namespace clang;
42using namespace clang::CodeGen;
43
44CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Eric Christopher324bbbd2013-07-14 21:12:44 +000045 : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
46 DBuilder(CGM.getModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +000047 CreateCompileUnit();
48}
49
50CGDebugInfo::~CGDebugInfo() {
51 assert(LexicalBlockStack.empty() &&
52 "Region stack mismatch, stack not empty!");
53}
54
David Blaikie66e41972015-01-14 07:38:27 +000055ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
David Blaikie835afb22015-01-21 23:08:17 +000056 SourceLocation TemporaryLocation)
David Blaikie66e41972015-01-14 07:38:27 +000057 : CGF(CGF) {
David Blaikie9b479662015-01-25 01:19:10 +000058 init(TemporaryLocation);
59}
60
Adrian Prantl39428e72015-02-03 18:40:42 +000061ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
Adrian Prantl95b24e92015-02-03 20:00:54 +000062 bool DefaultToEmpty,
Adrian Prantl39428e72015-02-03 18:40:42 +000063 SourceLocation TemporaryLocation)
64 : CGF(CGF) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000065 init(TemporaryLocation, DefaultToEmpty);
Adrian Prantl39428e72015-02-03 18:40:42 +000066}
67
68void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
Adrian Prantl95b24e92015-02-03 20:00:54 +000069 bool DefaultToEmpty) {
David Blaikie66e41972015-01-14 07:38:27 +000070 if (auto *DI = CGF.getDebugInfo()) {
71 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Adrian Prantl39428e72015-02-03 18:40:42 +000072 if (TemporaryLocation.isInvalid()) {
Adrian Prantl95b24e92015-02-03 20:00:54 +000073 if (DefaultToEmpty)
Adrian Prantl39428e72015-02-03 18:40:42 +000074 CGF.Builder.SetCurrentDebugLocation(llvm::DebugLoc());
75 else {
76 // Construct a location that has a valid scope, but no line info.
77 assert(!DI->LexicalBlockStack.empty());
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +000078 CGF.Builder.SetCurrentDebugLocation(
79 llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back()));
Adrian Prantl39428e72015-02-03 18:40:42 +000080 }
81 } else
David Blaikie835afb22015-01-21 23:08:17 +000082 DI->EmitLocation(CGF.Builder, TemporaryLocation);
David Blaikie66e41972015-01-14 07:38:27 +000083 }
Adrian Prantl2e0637f2013-07-18 00:28:02 +000084}
85
David Blaikie9b479662015-01-25 01:19:10 +000086ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
87 : CGF(CGF) {
88 init(E->getExprLoc());
89}
90
David Blaikie66e41972015-01-14 07:38:27 +000091ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
92 : CGF(CGF) {
93 if (CGF.getDebugInfo()) {
94 OriginalLocation = CGF.Builder.getCurrentDebugLocation();
Duncan P. N. Exon Smith2809cc72015-03-30 20:01:41 +000095 if (Loc)
Benjamin Kramer03278662015-02-07 13:15:54 +000096 CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
David Blaikie66e41972015-01-14 07:38:27 +000097 }
98}
99
100ApplyDebugLocation::~ApplyDebugLocation() {
101 // Query CGF so the location isn't overwritten when location updates are
102 // temporarily disabled (for C++ default function arguments)
103 if (CGF.getDebugInfo())
Benjamin Kramer03278662015-02-07 13:15:54 +0000104 CGF.Builder.SetCurrentDebugLocation(std::move(OriginalLocation));
David Blaikie66e41972015-01-14 07:38:27 +0000105}
106
107/// ArtificialLocation - An RAII object that temporarily switches to
108/// an artificial debug location that has a valid scope, but no line
Guy Benyei11169dd2012-12-18 14:30:41 +0000109void CGDebugInfo::setLocation(SourceLocation Loc) {
110 // If the new location isn't valid return.
Eric Christophere7b87e52014-10-26 23:40:33 +0000111 if (Loc.isInvalid())
112 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000113
114 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
115
116 // If we've changed files in the middle of a lexical scope go ahead
117 // and create a new lexical scope with file node if it's different
118 // from the one in the scope.
Eric Christophere7b87e52014-10-26 23:40:33 +0000119 if (LexicalBlockStack.empty())
120 return;
Guy Benyei11169dd2012-12-18 14:30:41 +0000121
122 SourceManager &SM = CGM.getContext().getSourceManager();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000123 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +0000124 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
Duncan P. N. Exon Smith373ee852015-04-16 01:36:36 +0000126 if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())
Guy Benyei11169dd2012-12-18 14:30:41 +0000127 return;
128
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000129 if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000130 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000131 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
132 LBF->getScope(), getOrCreateFile(CurLoc)));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000133 } else if (isa<llvm::DILexicalBlock>(Scope) ||
134 isa<llvm::DISubprogram>(Scope)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000135 LexicalBlockStack.pop_back();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000136 LexicalBlockStack.emplace_back(
137 DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000138 }
139}
140
141/// getContextDescriptor - Get context info for the decl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000142llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000143 if (!Context)
144 return TheCU;
145
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000146 auto I = RegionMap.find(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 if (I != RegionMap.end()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000148 llvm::Metadata *V = I->second;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000149 return dyn_cast_or_null<llvm::DIScope>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000150 }
151
152 // Check namespace.
153 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
David Blaikiebfa52742013-04-19 06:56:38 +0000154 return getOrCreateNameSpace(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +0000155
David Blaikiebfa52742013-04-19 06:56:38 +0000156 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context))
157 if (!RDecl->isDependentType())
158 return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Eric Christophere7b87e52014-10-26 23:40:33 +0000159 getOrCreateMainFile());
Guy Benyei11169dd2012-12-18 14:30:41 +0000160 return TheCU;
161}
162
163/// getFunctionName - Get function name for the given FunctionDecl. If the
Benjamin Kramer60509af2013-09-09 14:48:42 +0000164/// name is constructed on demand (e.g. C++ destructor) then the name
Guy Benyei11169dd2012-12-18 14:30:41 +0000165/// is stored on the side.
166StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000167 assert(FD && "Invalid FunctionDecl!");
Guy Benyei11169dd2012-12-18 14:30:41 +0000168 IdentifierInfo *FII = FD->getIdentifier();
Eric Christophere7b87e52014-10-26 23:40:33 +0000169 FunctionTemplateSpecializationInfo *Info =
170 FD->getTemplateSpecializationInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000171 if (!Info && FII)
172 return FII->getName();
173
174 // Otherwise construct human readable name for debug info.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000175 SmallString<128> NS;
176 llvm::raw_svector_ostream OS(NS);
177 FD->printName(OS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000178
179 // Add any template specialization args.
180 if (Info) {
181 const TemplateArgumentList *TArgs = Info->TemplateArguments;
182 const TemplateArgument *Args = TArgs->data();
183 unsigned NumArgs = TArgs->size();
184 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer9170e912013-02-22 15:46:01 +0000185 TemplateSpecializationType::PrintTemplateArgumentList(OS, Args, NumArgs,
186 Policy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000187 }
188
189 // Copy this name on the side and use its reference.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000190 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000191}
192
193StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
194 SmallString<256> MethodName;
195 llvm::raw_svector_ostream OS(MethodName);
196 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
197 const DeclContext *DC = OMD->getDeclContext();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000198 if (const ObjCImplementationDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000199 dyn_cast<const ObjCImplementationDecl>(DC)) {
200 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000201 } else if (const ObjCInterfaceDecl *OID =
Eric Christophere7b87e52014-10-26 23:40:33 +0000202 dyn_cast<const ObjCInterfaceDecl>(DC)) {
203 OS << OID->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000204 } else if (const ObjCCategoryImplDecl *OCD =
Eric Christophere7b87e52014-10-26 23:40:33 +0000205 dyn_cast<const ObjCCategoryImplDecl>(DC)) {
206 OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '('
207 << OCD->getIdentifier()->getNameStart() << ')';
Adrian Prantlb39fc142013-05-17 23:58:45 +0000208 } else if (isa<ObjCProtocolDecl>(DC)) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000209 // We can extract the type of the class from the self pointer.
Eric Christophere7b87e52014-10-26 23:40:33 +0000210 if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000211 QualType ClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +0000212 cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
Adrian Prantl6e785ec2013-05-17 23:49:10 +0000213 ClassTy.print(OS, PrintingPolicy(LangOptions()));
214 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000215 }
216 OS << ' ' << OMD->getSelector().getAsString() << ']';
217
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000218 return internString(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000219}
220
221/// getSelectorName - Return selector name. This is used for debugging
222/// info.
223StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000224 return internString(S.getAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +0000225}
226
227/// getClassName - Get class name including template argument list.
Eric Christophere7b87e52014-10-26 23:40:33 +0000228StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
David Blaikie65813a32014-04-02 18:21:09 +0000229 // quick optimization to avoid having to intern strings that are already
230 // stored reliably elsewhere
231 if (!isa<ClassTemplateSpecializationDecl>(RD))
Guy Benyei11169dd2012-12-18 14:30:41 +0000232 return RD->getName();
233
David Blaikie65813a32014-04-02 18:21:09 +0000234 SmallString<128> Name;
Benjamin Kramer9170e912013-02-22 15:46:01 +0000235 {
David Blaikie65813a32014-04-02 18:21:09 +0000236 llvm::raw_svector_ostream OS(Name);
237 RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(),
238 /*Qualified*/ false);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000239 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000240
241 // Copy this name on the side and use its reference.
David Blaikie65813a32014-04-02 18:21:09 +0000242 return internString(Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000243}
244
245/// getOrCreateFile - Get the file debug info descriptor for the input location.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000246llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000247 if (!Loc.isValid())
248 // If Location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000249 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000250
251 SourceManager &SM = CGM.getContext().getSourceManager();
252 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
253
254 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
255 // If the location is not valid then use main input file.
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000256 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000257
258 // Cache the results.
259 const char *fname = PLoc.getFilename();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000260 auto it = DIFileCache.find(fname);
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
262 if (it != DIFileCache.end()) {
263 // Verify that the information still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000264 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000265 return cast<llvm::DIFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000266 }
267
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000268 llvm::DIFile *F =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000269 DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Guy Benyei11169dd2012-12-18 14:30:41 +0000270
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000271 DIFileCache[fname].reset(F);
Guy Benyei11169dd2012-12-18 14:30:41 +0000272 return F;
273}
274
275/// getOrCreateMainFile - Get the file info for main compile unit.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000276llvm::DIFile *CGDebugInfo::getOrCreateMainFile() {
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000277 return DBuilder.createFile(TheCU->getFilename(), TheCU->getDirectory());
Guy Benyei11169dd2012-12-18 14:30:41 +0000278}
279
280/// getLineNumber - Get line number for the location. If location is invalid
281/// then use current location.
282unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
283 if (Loc.isInvalid() && CurLoc.isInvalid())
284 return 0;
285 SourceManager &SM = CGM.getContext().getSourceManager();
286 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000287 return PLoc.isValid() ? PLoc.getLine() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000288}
289
290/// getColumnNumber - Get column number for the location.
Adrian Prantlc7822422013-03-12 20:43:25 +0000291unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000292 // We may not want column information at all.
Adrian Prantlc7822422013-03-12 20:43:25 +0000293 if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
Guy Benyei11169dd2012-12-18 14:30:41 +0000294 return 0;
295
296 // If the location is invalid then use the current column.
297 if (Loc.isInvalid() && CurLoc.isInvalid())
298 return 0;
299 SourceManager &SM = CGM.getContext().getSourceManager();
300 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Eric Christophere7b87e52014-10-26 23:40:33 +0000301 return PLoc.isValid() ? PLoc.getColumn() : 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000302}
303
304StringRef CGDebugInfo::getCurrentDirname() {
305 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
306 return CGM.getCodeGenOpts().DebugCompilationDir;
307
308 if (!CWDName.empty())
309 return CWDName;
310 SmallString<256> CWD;
311 llvm::sys::fs::current_path(CWD);
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000312 return CWDName = internString(CWD);
Guy Benyei11169dd2012-12-18 14:30:41 +0000313}
314
315/// CreateCompileUnit - Create new compile unit.
316void CGDebugInfo::CreateCompileUnit() {
317
David Blaikieaabde052014-05-14 00:29:00 +0000318 // Should we be asking the SourceManager for the main file name, instead of
319 // accepting it as an argument? This just causes the main file name to
320 // mismatch with source locations and create extra lexical scopes or
321 // mismatched debug info (a CU with a DW_AT_file of "-", because that's what
322 // the driver passed, but functions/other things have DW_AT_file of "<stdin>"
323 // because that's what the SourceManager says)
324
Guy Benyei11169dd2012-12-18 14:30:41 +0000325 // Get absolute path name.
326 SourceManager &SM = CGM.getContext().getSourceManager();
327 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
328 if (MainFileName.empty())
David Blaikieaabde052014-05-14 00:29:00 +0000329 MainFileName = "<stdin>";
Guy Benyei11169dd2012-12-18 14:30:41 +0000330
331 // The main file name provided via the "-main-file-name" option contains just
332 // the file name itself with no path information. This file name may have had
333 // a relative path, so we look into the actual file entry for the main
334 // file to determine the real absolute path for the file.
335 std::string MainFileDir;
336 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
337 MainFileDir = MainFile->getDir()->getName();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000338 if (MainFileDir != ".") {
Eric Christopher0a1301f2014-02-26 02:49:36 +0000339 llvm::SmallString<1024> MainFileDirSS(MainFileDir);
340 llvm::sys::path::append(MainFileDirSS, MainFileName);
341 MainFileName = MainFileDirSS.str();
Yaron Keren9fb7e902013-10-21 20:07:37 +0000342 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000343 }
344
345 // Save filename string.
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000346 StringRef Filename = internString(MainFileName);
Eric Christopherf1545832013-02-22 23:50:16 +0000347
348 // Save split dwarf file string.
349 std::string SplitDwarfFile = CGM.getCodeGenOpts().SplitDwarfFile;
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +0000350 StringRef SplitDwarfFilename = internString(SplitDwarfFile);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000351
Ed Masteda706022014-05-07 12:49:30 +0000352 llvm::dwarf::SourceLanguage LangTag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000353 const LangOptions &LO = CGM.getLangOpts();
354 if (LO.CPlusPlus) {
355 if (LO.ObjC1)
356 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
357 else
358 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
359 } else if (LO.ObjC1) {
360 LangTag = llvm::dwarf::DW_LANG_ObjC;
361 } else if (LO.C99) {
362 LangTag = llvm::dwarf::DW_LANG_C99;
363 } else {
364 LangTag = llvm::dwarf::DW_LANG_C89;
365 }
366
367 std::string Producer = getClangFullVersion();
368
369 // Figure out which version of the ObjC runtime we have.
370 unsigned RuntimeVers = 0;
371 if (LO.ObjC1)
372 RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1;
373
374 // Create new compile unit.
Guy Benyei11169dd2012-12-18 14:30:41 +0000375 // FIXME - Eliminate TheCU.
Eric Christophere4200a22014-02-27 01:25:08 +0000376 TheCU = DBuilder.createCompileUnit(
377 LangTag, Filename, getCurrentDirname(), Producer, LO.Optimize,
378 CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, SplitDwarfFilename,
Diego Novillo913690c2014-06-24 17:02:17 +0000379 DebugKind <= CodeGenOptions::DebugLineTablesOnly
Eric Christophere4200a22014-02-27 01:25:08 +0000380 ? llvm::DIBuilder::LineTablesOnly
Diego Novillo913690c2014-06-24 17:02:17 +0000381 : llvm::DIBuilder::FullDebug,
Adrian Prantlbe81cf52015-05-21 20:37:26 +0000382 0 /* DWOid */,
Diego Novillo913690c2014-06-24 17:02:17 +0000383 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000384}
385
386/// CreateType - Get the Basic type from the cache or create a new
387/// one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000388llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000389 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000390 StringRef BTName;
391 switch (BT->getKind()) {
392#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000393#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000394#include "clang/AST/BuiltinTypes.def"
395 case BuiltinType::Dependent:
396 llvm_unreachable("Unexpected builtin type");
397 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000398 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000399 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000400 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000401 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000402 if (!ClassTy)
403 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
404 "objc_class", TheCU,
405 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000406 return ClassTy;
407 case BuiltinType::ObjCId: {
408 // typedef struct objc_class *Class;
409 // typedef struct objc_object {
410 // Class isa;
411 // } *id;
412
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000413 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000414 return ObjTy;
415
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000416 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000417 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
418 "objc_class", TheCU,
419 getOrCreateMainFile(), 0);
420
421 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000422
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000423 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000424
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000425 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000426 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000427 0, 0, 0, 0, nullptr, llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000428
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000429 DBuilder.replaceArrays(
430 ObjTy,
431 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
432 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000433 return ObjTy;
434 }
435 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000436 if (!SelTy)
437 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
438 "objc_selector", TheCU,
439 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 return SelTy;
441 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000442
443 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000444 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000445 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000446 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000447 OCLImage1dArrayDITy);
448 case BuiltinType::OCLImage1dBuffer:
449 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
450 OCLImage1dBufferDITy);
451 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000452 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000453 case BuiltinType::OCLImage2dArray:
454 return getOrCreateStructPtrType("opencl_image2d_array_t",
455 OCLImage2dArrayDITy);
456 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000457 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000458 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000459 return DBuilder.createBasicType(
460 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
461 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000462 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000463 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000464
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000466 case BuiltinType::Char_U:
467 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
468 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000469 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000470 case BuiltinType::SChar:
471 Encoding = llvm::dwarf::DW_ATE_signed_char;
472 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000473 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000474 case BuiltinType::Char32:
475 Encoding = llvm::dwarf::DW_ATE_UTF;
476 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000477 case BuiltinType::UShort:
478 case BuiltinType::UInt:
479 case BuiltinType::UInt128:
480 case BuiltinType::ULong:
481 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000482 case BuiltinType::ULongLong:
483 Encoding = llvm::dwarf::DW_ATE_unsigned;
484 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000485 case BuiltinType::Short:
486 case BuiltinType::Int:
487 case BuiltinType::Int128:
488 case BuiltinType::Long:
489 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000490 case BuiltinType::LongLong:
491 Encoding = llvm::dwarf::DW_ATE_signed;
492 break;
493 case BuiltinType::Bool:
494 Encoding = llvm::dwarf::DW_ATE_boolean;
495 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000496 case BuiltinType::Half:
497 case BuiltinType::Float:
498 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000499 case BuiltinType::Double:
500 Encoding = llvm::dwarf::DW_ATE_float;
501 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000502 }
503
504 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000505 case BuiltinType::Long:
506 BTName = "long int";
507 break;
508 case BuiltinType::LongLong:
509 BTName = "long long int";
510 break;
511 case BuiltinType::ULong:
512 BTName = "long unsigned int";
513 break;
514 case BuiltinType::ULongLong:
515 BTName = "long long unsigned int";
516 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000517 default:
518 BTName = BT->getName(CGM.getLangOpts());
519 break;
520 }
521 // Bit size, align and offset of the type.
522 uint64_t Size = CGM.getContext().getTypeSize(BT);
523 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000524 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000525}
526
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000527llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000528 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000529 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000530 if (Ty->isComplexIntegerType())
531 Encoding = llvm::dwarf::DW_ATE_lo_user;
532
533 uint64_t Size = CGM.getContext().getTypeSize(Ty);
534 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000535 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000536}
537
538/// CreateCVRType - Get the qualified type from the cache or create
539/// a new one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000540llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
541 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000542 QualifierCollector Qc;
543 const Type *T = Qc.strip(Ty);
544
545 // Ignore these qualifiers for now.
546 Qc.removeObjCGCAttr();
547 Qc.removeAddressSpace();
548 Qc.removeObjCLifetime();
549
550 // We will create one Derived type for one qualifier and recurse to handle any
551 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000552 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000553 if (Qc.hasConst()) {
554 Tag = llvm::dwarf::DW_TAG_const_type;
555 Qc.removeConst();
556 } else if (Qc.hasVolatile()) {
557 Tag = llvm::dwarf::DW_TAG_volatile_type;
558 Qc.removeVolatile();
559 } else if (Qc.hasRestrict()) {
560 Tag = llvm::dwarf::DW_TAG_restrict_type;
561 Qc.removeRestrict();
562 } else {
563 assert(Qc.empty() && "Unknown type qualifier for debug info");
564 return getOrCreateType(QualType(T, 0), Unit);
565 }
566
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000567 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000568
569 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
570 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000571 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000572}
573
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000574llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
575 llvm::DIFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000576
577 // The frontend treats 'id' as a typedef to an ObjCObjectType,
578 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
579 // debug info, we want to emit 'id' in both cases.
580 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000581 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000582
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000583 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
584 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000585}
586
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000587llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
588 llvm::DIFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000589 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000590 Ty->getPointeeType(), Unit);
591}
592
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000593/// \return whether a C++ mangling exists for the type defined by TD.
594static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) {
595 switch (TheCU->getSourceLanguage()) {
596 case llvm::dwarf::DW_LANG_C_plus_plus:
597 return true;
598 case llvm::dwarf::DW_LANG_ObjC_plus_plus:
599 return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD);
600 default:
601 return false;
602 }
603}
604
Manman Rene0064d82013-08-29 23:19:58 +0000605/// In C++ mode, types have linkage, so we can rely on the ODR and
606/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000607static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
608 CodeGenModule &CGM,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000609 llvm::DICompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000610 SmallString<256> FullName;
Manman Rene0064d82013-08-29 23:19:58 +0000611 const TagDecl *TD = Ty->getDecl();
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000612
613 if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible())
Manman Rene0064d82013-08-29 23:19:58 +0000614 return FullName;
Adrian Prantlc6f91a22015-06-15 23:18:16 +0000615
Manman Rene0064d82013-08-29 23:19:58 +0000616 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
617 if (CGM.getTarget().getCXXABI().isMicrosoft())
618 return FullName;
619
620 // TODO: This is using the RTTI name. Is there a better way to get
621 // a unique string for a type?
622 llvm::raw_svector_ostream Out(FullName);
623 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
624 Out.flush();
625 return FullName;
626}
627
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000628static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
629 llvm::dwarf::Tag Tag;
630 if (RD->isStruct() || RD->isInterface())
631 Tag = llvm::dwarf::DW_TAG_structure_type;
632 else if (RD->isUnion())
633 Tag = llvm::dwarf::DW_TAG_union_type;
634 else {
635 // FIXME: This could be a struct type giving a default visibility different
636 // than C++ class type, but needs llvm metadata changes first.
637 assert(RD->isClass());
638 Tag = llvm::dwarf::DW_TAG_class_type;
639 }
640 return Tag;
641}
642
Guy Benyei11169dd2012-12-18 14:30:41 +0000643// Creates a forward declaration for a RecordDecl in the given context.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000644llvm::DICompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000645CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000646 llvm::DIScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000647 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000648 if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
649 return cast<llvm::DICompositeType>(T);
650 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 unsigned Line = getLineNumber(RD->getLocation());
652 StringRef RDName = getClassName(RD);
653
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000654 uint64_t Size = 0;
655 uint64_t Align = 0;
656
657 const RecordDecl *D = RD->getDefinition();
658 if (D && D->isCompleteDefinition()) {
659 Size = CGM.getContext().getTypeSize(Ty);
660 Align = CGM.getContext().getTypeAlign(Ty);
661 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000662
663 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000664 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000665 llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000666 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000667 llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000668 ReplaceMap.emplace_back(
669 std::piecewise_construct, std::make_tuple(Ty),
670 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000671 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000672}
673
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000674llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000675 const Type *Ty,
676 QualType PointeeTy,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000677 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
679 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000680 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000681
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 // Bit size, align and offset of the type.
683 // Size is always the size of a pointer. We can't use getTypeSize here
684 // because that does not return the correct value for references.
685 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000686 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000687 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
688
David Blaikie99dab3b2013-09-04 22:03:57 +0000689 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
690 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000691}
692
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000693llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
694 llvm::DIType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000695 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000696 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000697 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
698 TheCU, getOrCreateMainFile(), 0);
699 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
700 Cache = DBuilder.createPointerType(Cache, Size);
701 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000702}
703
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000704llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
705 llvm::DIFile *Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000706 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 return BlockLiteralGeneric;
708
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000709 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000710 QualType FType;
711 uint64_t FieldSize, FieldOffset;
712 unsigned FieldAlign;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000713 llvm::DINodeArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000714
715 FieldOffset = 0;
716 FType = CGM.getContext().UnsignedLongTy;
717 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
718 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
719
720 Elements = DBuilder.getOrCreateArray(EltTys);
721 EltTys.clear();
722
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000723 unsigned Flags = llvm::DINode::FlagAppleBlock;
Guy Benyei11169dd2012-12-18 14:30:41 +0000724 unsigned LineNo = getLineNumber(CurLoc);
725
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000726 auto *EltTy =
727 DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
728 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000729
730 // Bit size, align and offset of the type.
731 uint64_t Size = CGM.getContext().getTypeSize(Ty);
732
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000733 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000734
735 FieldOffset = 0;
736 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
737 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
738 FType = CGM.getContext().IntTy;
739 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
740 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000741 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000742 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
743
744 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 FieldSize = CGM.getContext().getTypeSize(Ty);
746 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000747 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo,
748 FieldSize, FieldAlign, FieldOffset,
749 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000750
751 FieldOffset += FieldSize;
752 Elements = DBuilder.getOrCreateArray(EltTys);
753
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000754 EltTy =
755 DBuilder.createStructType(Unit, "__block_literal_generic", Unit, LineNo,
756 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000757
Guy Benyei11169dd2012-12-18 14:30:41 +0000758 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
759 return BlockLiteralGeneric;
760}
761
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000762llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
763 llvm::DIFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000764 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000765 llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000766
767 SmallString<128> NS;
768 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000769 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
770 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000771
772 TemplateSpecializationType::PrintTemplateArgumentList(
773 OS, Ty->getArgs(), Ty->getNumArgs(),
774 CGM.getContext().getPrintingPolicy());
775
Eric Christophere7b87e52014-10-26 23:40:33 +0000776 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
777 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000778
779 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000780 return DBuilder.createTypedef(
781 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
782 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000783}
784
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000785llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
786 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000787 // We don't set size information, but do specify where the typedef was
788 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000789 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000790
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000791 // Typedefs are derived from some other type.
792 return DBuilder.createTypedef(
793 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
794 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
795 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000796}
797
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000798llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
799 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000800 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000801
802 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000803 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000804
805 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000806 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 if (isa<FunctionNoProtoType>(Ty))
808 EltTys.push_back(DBuilder.createUnspecifiedParameter());
809 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000810 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
811 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000812 if (FPT->isVariadic())
813 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000814 }
815
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000816 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000817 return DBuilder.createSubroutineType(Unit, EltTypeArray);
818}
819
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000820/// Convert an AccessSpecifier into the corresponding DINode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000821/// As an optimization, return 0 if the access specifier equals the
822/// default for the containing type.
823static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
824 AccessSpecifier Default = clang::AS_none;
825 if (RD && RD->isClass())
826 Default = clang::AS_private;
827 else if (RD && (RD->isStruct() || RD->isUnion()))
828 Default = clang::AS_public;
829
830 if (Access == Default)
831 return 0;
832
Eric Christophere7b87e52014-10-26 23:40:33 +0000833 switch (Access) {
834 case clang::AS_private:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000835 return llvm::DINode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000836 case clang::AS_protected:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000837 return llvm::DINode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000838 case clang::AS_public:
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000839 return llvm::DINode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000840 case clang::AS_none:
841 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000842 }
843 llvm_unreachable("unexpected access enumerator");
844}
Guy Benyei11169dd2012-12-18 14:30:41 +0000845
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000846llvm::DIType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000847 StringRef name, QualType type, uint64_t sizeInBitsOverride,
848 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000849 llvm::DIFile *tunit, llvm::DIScope *scope, const RecordDecl *RD) {
850 llvm::DIType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000851
852 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000853 llvm::DIFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 unsigned line = getLineNumber(loc);
855
David Majnemer34b57492014-07-30 01:30:47 +0000856 uint64_t SizeInBits = 0;
857 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000858 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000859 TypeInfo TI = CGM.getContext().getTypeInfo(type);
860 SizeInBits = TI.Width;
861 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000862
863 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000864 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000865 }
866
Adrian Prantl21361fb2014-08-29 22:44:27 +0000867 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000868 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
869 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000870}
871
Eric Christopher91a31902013-01-16 01:22:32 +0000872/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000873void CGDebugInfo::CollectRecordLambdaFields(
874 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000875 llvm::DIType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000876 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
877 // has the name and the location of the variable so we should iterate over
878 // both concurrently.
879 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
880 RecordDecl::field_iterator Field = CXXDecl->field_begin();
881 unsigned fieldno = 0;
882 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000883 E = CXXDecl->captures_end();
884 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000885 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000886 if (C.capturesVariable()) {
887 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000888 llvm::DIFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000889 StringRef VName = V->getName();
890 uint64_t SizeInBitsOverride = 0;
891 if (Field->isBitField()) {
892 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
893 assert(SizeInBitsOverride && "found named 0-width bitfield");
894 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000895 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000896 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
897 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
898 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000899 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000900 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000901 // TODO: Need to handle 'this' in some way by probably renaming the
902 // this of the lambda class and having a field member of 'this' or
903 // by using AT_object_pointer for the function and having that be
904 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000905 FieldDecl *f = *Field;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000906 llvm::DIFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000907 QualType type = f->getType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000908 llvm::DIType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000909 "this", type, 0, f->getLocation(), f->getAccess(),
910 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000911
912 elements.push_back(fieldType);
913 }
914 }
915}
916
David Blaikie6943dea2013-08-20 01:28:15 +0000917/// Helper for CollectRecordFields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000918llvm::DIDerivedType *
919CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000920 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000921 // Create the descriptor for the static variable, with or without
922 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000923 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000924 llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation());
925 llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000926
Eric Christopher91a31902013-01-16 01:22:32 +0000927 unsigned LineNumber = getLineNumber(Var->getLocation());
928 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000929 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000930 if (Var->getInit()) {
931 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000932 if (Value) {
933 if (Value->isInt())
934 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
935 if (Value->isFloat())
936 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
937 }
Eric Christopher91a31902013-01-16 01:22:32 +0000938 }
939
Adrian Prantl21361fb2014-08-29 22:44:27 +0000940 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000941 llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000942 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000943 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000944 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000945}
946
947/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000948void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000949 const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit,
950 SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000951 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000952 StringRef name = field->getName();
953 QualType type = field->getType();
954
955 // Ignore unnamed fields unless they're anonymous structs/unions.
956 if (name.empty() && !type->isRecordType())
957 return;
958
959 uint64_t SizeInBitsOverride = 0;
960 if (field->isBitField()) {
961 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
962 assert(SizeInBitsOverride && "found named 0-width bitfield");
963 }
964
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000965 llvm::DIType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000966 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
967 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000968
969 elements.push_back(fieldType);
970}
971
Guy Benyei11169dd2012-12-18 14:30:41 +0000972/// CollectRecordFields - A helper function to collect debug info for
973/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000974void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000975 const RecordDecl *record, llvm::DIFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000976 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000977 llvm::DICompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000978 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
979
Eric Christopher91a31902013-01-16 01:22:32 +0000980 if (CXXDecl && CXXDecl->isLambda())
981 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
982 else {
983 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000984
Eric Christopher91a31902013-01-16 01:22:32 +0000985 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000986 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000987
Eric Christopher91a31902013-01-16 01:22:32 +0000988 // Static and non-static members should appear in the same order as
989 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000990 for (const auto *I : record->decls())
991 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000992 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000993 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000994 if (MI != StaticDataMemberCache.end()) {
995 assert(MI->second &&
996 "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +0000997 elements.push_back(cast<llvm::DIDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000998 } else {
999 auto Field = CreateRecordStaticField(V, RecordTy, record);
1000 elements.push_back(Field);
1001 }
Aaron Ballman629afae2014-03-07 19:56:05 +00001002 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001003 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
1004 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +00001005
1006 // Bump field number for next field.
1007 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +00001008 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001009 }
1010}
1011
1012/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1013/// function type is not updated to include implicit "this" pointer. Use this
1014/// routine to get a method type which includes "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001015llvm::DISubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001016CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001017 llvm::DIFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001018 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001019 if (Method->isStatic())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001020 return cast_or_null<llvm::DISubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001021 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001022 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1023 Func, Unit);
1024}
David Blaikie2aaf0652013-01-07 22:24:59 +00001025
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001026llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
1027 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001028 // Add "this" pointer.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001029 llvm::DITypeRefArray Args(
1030 cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001031 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001032 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001033
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001034 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001035
1036 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001037 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001038
David Blaikie2aaf0652013-01-07 22:24:59 +00001039 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001040 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001041 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1042 // Create pointer type directly in this case.
1043 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1044 QualType PointeeTy = ThisPtrTy->getPointeeType();
1045 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001046 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001047 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001048 llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
1049 llvm::DIType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001050 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001051 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001052 // TODO: This and the artificial type below are misleading, the
1053 // types aren't artificial the argument is, but the current
1054 // metadata doesn't represent that.
1055 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1056 Elts.push_back(ThisPtrType);
1057 } else {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001058 llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001059 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001060 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1061 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001062 }
1063
1064 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001065 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1066 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001067
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001068 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001069
Adrian Prantl0630eb72013-12-18 21:48:18 +00001070 unsigned Flags = 0;
1071 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001072 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001073 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001074 Flags |= llvm::DINode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001075
1076 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001077}
1078
Eric Christopherb2a008c2013-05-16 00:45:12 +00001079/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001080/// inside a function.
1081static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1082 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1083 return isFunctionLocalClass(NRD);
1084 if (isa<FunctionDecl>(RD->getDeclContext()))
1085 return true;
1086 return false;
1087}
1088
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001089/// CreateCXXMemberFunction - A helper function to create a subprogram for
Guy Benyei11169dd2012-12-18 14:30:41 +00001090/// a single member function GlobalDecl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001091llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
1092 const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001093 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001094 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001095
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001097 llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001098
1099 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1100 // make sense to give a single ctor/dtor a linkage name.
1101 StringRef MethodLinkageName;
1102 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1103 MethodLinkageName = CGM.getMangledName(Method);
1104
1105 // Get the location for the method.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001106 llvm::DIFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001107 unsigned MethodLine = 0;
1108 if (!Method->isImplicit()) {
1109 MethodDefUnit = getOrCreateFile(Method->getLocation());
1110 MethodLine = getLineNumber(Method->getLocation());
1111 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001112
1113 // Collect virtual method info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001114 llvm::DIType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001115 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001116 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001117
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 if (Method->isVirtual()) {
1119 if (Method->isPure())
1120 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1121 else
1122 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001123
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 // It doesn't make sense to give a virtual destructor a vtable index,
1125 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001126 // FIXME: Add proper support for debug info for virtual calls in
1127 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1128 // lookup if we have multiple or virtual inheritance.
1129 if (!isa<CXXDestructorDecl>(Method) &&
1130 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001131 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 ContainingType = RecordTy;
1133 }
1134
1135 unsigned Flags = 0;
1136 if (Method->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001137 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001138 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1140 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001141 Flags |= llvm::DINode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001142 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001143 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 if (CXXC->isExplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001145 Flags |= llvm::DINode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001146 }
1147 if (Method->hasPrototype())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001148 Flags |= llvm::DINode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001149 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001150 Flags |= llvm::DINode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001151 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001152 Flags |= llvm::DINode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001153
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001154 llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
1155 llvm::DISubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001156 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1157 MethodTy, /*isLocalToUnit=*/false,
1158 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001159 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001160
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001161 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001162
1163 return SP;
1164}
1165
1166/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001167/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001168/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001169void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001170 const CXXRecordDecl *RD, llvm::DIFile *Unit,
1171 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001172
1173 // Since we want more than just the individual member decls if we
1174 // have templated functions iterate over every declaration to gather
1175 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001176 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001177 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1178 // If the member is implicit, don't add it to the member list. This avoids
1179 // the member being added to type units by LLVM, while still allowing it
1180 // to be emitted into the type declaration/reference inside the compile
1181 // unit.
Paul Robinson6a7511b2015-06-25 17:50:43 +00001182 // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp.
David Blaikie6dddfe32014-10-06 05:52:27 +00001183 // FIXME: Handle Using(Shadow?)Decls here to create
1184 // DW_TAG_imported_declarations inside the class for base decls brought into
1185 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1186 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1187 // referenced)
Paul Robinson6a7511b2015-06-25 17:50:43 +00001188 if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>())
David Blaikiefd580722014-10-06 05:18:55 +00001189 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001190
1191 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1192 continue;
1193
David Blaikiefd580722014-10-06 05:18:55 +00001194 // Reuse the existing member function declaration if it exists.
1195 // It may be associated with the declaration of the type & should be
1196 // reused as we're building the definition.
1197 //
1198 // This situation can arise in the vtable-based debug info reduction where
1199 // implicit members are emitted in a non-vtable TU.
1200 auto MI = SPCache.find(Method->getCanonicalDecl());
1201 EltTys.push_back(MI == SPCache.end()
1202 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001203 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001204 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001205}
Guy Benyei11169dd2012-12-18 14:30:41 +00001206
Guy Benyei11169dd2012-12-18 14:30:41 +00001207/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001208/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001209/// a Record.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001210void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001211 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001212 llvm::DIType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001213 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001214 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001215 unsigned BFlags = 0;
1216 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001217
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001219 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001220
Aaron Ballman574705e2014-03-13 15:41:46 +00001221 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001222 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1223 // virtual base offset offset is -ve. The code generator emits dwarf
1224 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001225 BaseOffset = 0 - CGM.getItaniumVTableContext()
1226 .getVirtualBaseOffsetOffset(RD, Base)
1227 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001228 } else {
1229 // In the MS ABI, store the vbtable offset, which is analogous to the
1230 // vbase offset offset in Itanium.
1231 BaseOffset =
1232 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1233 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001234 BFlags = llvm::DINode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 } else
1236 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1237 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1238 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001239
Adrian Prantl21361fb2014-08-29 22:44:27 +00001240 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001241 llvm::DIType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001242 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 EltTys.push_back(DTy);
1244 }
1245}
1246
1247/// CollectTemplateParams - A helper function to collect template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001248llvm::DINodeArray
Eric Christophere7b87e52014-10-26 23:40:33 +00001249CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1250 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001251 llvm::DIFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001252 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1254 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001255 StringRef Name;
1256 if (TPList)
1257 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001258 switch (TA.getKind()) {
1259 case TemplateArgument::Type: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001260 llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001261 TemplateParams.push_back(
1262 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001263 } break;
1264 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001265 llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001266 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1267 TheCU, Name, TTy,
1268 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001269 } break;
1270 case TemplateArgument::Declaration: {
1271 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001272 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001273 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001274 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001275 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001276 // Variable pointer template parameters have a value that is the address
1277 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001278 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001279 V = CGM.GetAddrOfGlobalVar(VD);
1280 // Member function pointers have special support for building them, though
1281 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001282 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Majnemere2be95b2015-06-23 07:31:01 +00001283 V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001284 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001285 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001286 // Member data pointers have special handling too to compute the fixed
1287 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001288 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001289 // These five lines (& possibly the above member function pointer
1290 // handling) might be able to be refactored to use similar code in
1291 // CodeGenModule::getMemberPointerConstant
1292 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1293 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001294 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001295 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001296 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001297 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1298 TheCU, Name, TTy,
1299 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001300 } break;
1301 case TemplateArgument::NullPtr: {
1302 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001303 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001304 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001305 // Special case member data pointer null values since they're actually -1
1306 // instead of zero.
1307 if (const MemberPointerType *MPT =
1308 dyn_cast<MemberPointerType>(T.getTypePtr()))
1309 // But treat member function pointers as simple zero integers because
1310 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1311 // CodeGen grows handling for values of non-null member function
1312 // pointers then perhaps we could remove this special case and rely on
1313 // EmitNullMemberPointer for member function pointers.
1314 if (MPT->isMemberDataPointer())
1315 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1316 if (!V)
1317 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001318 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1319 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001320 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001321 case TemplateArgument::Template:
1322 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001323 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001324 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1325 break;
1326 case TemplateArgument::Pack:
1327 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1328 TheCU, Name, nullptr,
1329 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1330 break;
David Majnemer5559d472013-08-24 08:21:10 +00001331 case TemplateArgument::Expression: {
1332 const Expr *E = TA.getAsExpr();
1333 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001334 if (E->isGLValue())
1335 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001336 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001337 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001338 llvm::DIType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001339 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1340 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001341 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001342 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001343 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001344 case TemplateArgument::Null:
1345 llvm_unreachable(
1346 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001347 }
1348 }
1349 return DBuilder.getOrCreateArray(TemplateParams);
1350}
1351
1352/// CollectFunctionTemplateParams - A helper function to collect debug
1353/// info for function template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001354llvm::DINodeArray
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001355CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001356 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 if (FD->getTemplatedKind() ==
1358 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001359 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1360 ->getTemplate()
1361 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001362 return CollectTemplateParams(
1363 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001365 return llvm::DINodeArray();
Guy Benyei11169dd2012-12-18 14:30:41 +00001366}
1367
1368/// CollectCXXTemplateParams - A helper function to collect debug info for
1369/// template parameters.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001370llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
1371 const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001372 // Always get the full list of parameters, not just the ones from
1373 // the specialization.
1374 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001375 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001376 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001377 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001378}
1379
1380/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001381llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001382 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 return VTablePtrType;
1384
1385 ASTContext &Context = CGM.getContext();
1386
1387 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001388 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001389 llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
1390 llvm::DIType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001391 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001392 llvm::DIType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001393 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1395 return VTablePtrType;
1396}
1397
1398/// getVTableName - Get vtable name for the given Class.
1399StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001400 // Copy the gdb compatible name on the side and use its reference.
1401 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001402}
1403
Guy Benyei11169dd2012-12-18 14:30:41 +00001404/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1405/// debug info entry in EltTys vector.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001406void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001407 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1409
1410 // If there is a primary base then it will hold vtable info.
1411 if (RL.getPrimaryBase())
1412 return;
1413
1414 // If this class is not dynamic then there is not any vtable info to collect.
1415 if (!RD->isDynamicClass())
1416 return;
1417
1418 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001419 llvm::DIType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001420 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001421 llvm::DINode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 EltTys.push_back(VPTR);
1423}
1424
Eric Christopherb2a008c2013-05-16 00:45:12 +00001425/// getOrCreateRecordType - Emit record type's standalone debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001426llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001427 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001428 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001429 llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001430 return T;
1431}
1432
1433/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1434/// debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001435llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001436 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001437 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001438 llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001439 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 return T;
1441}
1442
David Blaikie483a9da2014-05-06 18:35:21 +00001443void CGDebugInfo::completeType(const EnumDecl *ED) {
1444 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1445 return;
1446 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001447 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001448 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001449 if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001450 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001451 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001452 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001453 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001454}
1455
David Blaikieb2e86eb2013-08-15 20:49:17 +00001456void CGDebugInfo::completeType(const RecordDecl *RD) {
1457 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1458 !CGM.getLangOpts().CPlusPlus)
1459 completeRequiredType(RD);
1460}
1461
1462void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001463 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1464 return;
1465
David Blaikie6943dea2013-08-20 01:28:15 +00001466 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1467 if (CXXDecl->isDynamicClass())
1468 return;
1469
David Blaikieb2e86eb2013-08-15 20:49:17 +00001470 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001471 llvm::DIType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001472 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001473 completeClassData(RD);
1474}
1475
1476void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1477 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001478 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001479 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001480 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001481 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001482 if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001483 return;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001484 llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001485 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001486 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001487}
1488
David Blaikie0e716b42014-03-03 23:48:23 +00001489static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1490 CXXRecordDecl::method_iterator End) {
1491 for (; I != End; ++I)
1492 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001493 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1494 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001495 return true;
1496 return false;
1497}
1498
1499static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1500 const RecordDecl *RD,
1501 const LangOptions &LangOpts) {
1502 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1503 return false;
1504
1505 if (!LangOpts.CPlusPlus)
1506 return false;
1507
1508 if (!RD->isCompleteDefinitionRequired())
1509 return true;
1510
1511 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1512
1513 if (!CXXDecl)
1514 return false;
1515
1516 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1517 return true;
1518
1519 TemplateSpecializationKind Spec = TSK_Undeclared;
1520 if (const ClassTemplateSpecializationDecl *SD =
1521 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1522 Spec = SD->getSpecializationKind();
1523
1524 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1525 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1526 CXXDecl->method_end()))
1527 return true;
1528
1529 return false;
1530}
1531
Guy Benyei11169dd2012-12-18 14:30:41 +00001532/// CreateType - get structure or union type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001533llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001535 llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001536 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001537 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001538 T = getOrCreateRecordFwdDecl(
1539 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001540 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001541 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001542
David Blaikieb2e86eb2013-08-15 20:49:17 +00001543 return CreateTypeDefinition(Ty);
1544}
1545
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001546llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001547 RecordDecl *RD = Ty->getDecl();
1548
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001550 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001551
1552 // Records and classes and unions can all be recursive. To handle them, we
1553 // first generate a debug descriptor for the struct as a forward declaration.
1554 // Then (if it is a definition) we go through and get debug info for all of
1555 // its members. Finally, we create a descriptor for the complete type (which
1556 // may refer to the forward decl if the struct is recursive) and replace all
1557 // uses of the forward declaration with the final definition.
1558
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001559 auto *FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001560 cast<llvm::DICompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001561
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001562 const RecordDecl *D = RD->getDefinition();
1563 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001564 return FwdDecl;
1565
David Blaikieadfbf992013-08-18 16:55:33 +00001566 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1567 CollectContainingType(CXXDecl, FwdDecl);
1568
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001570 LexicalBlockStack.emplace_back(&*FwdDecl);
1571 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001574 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001575 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001576
1577 // Note: The split of CXXDecl information here is intentional, the
1578 // gdb tests will depend on a certain ordering at printout. The debug
1579 // information offsets are still correct if we merge them all together
1580 // though.
1581 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1582 if (CXXDecl) {
1583 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1584 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1585 }
1586
Eric Christopher91a31902013-01-16 01:22:32 +00001587 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001588 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001589 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001591
1592 LexicalBlockStack.pop_back();
1593 RegionMap.erase(Ty->getDecl());
1594
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001595 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001596 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001597
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001598 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001599 FwdDecl =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001600 llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001601
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001602 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001603 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001604}
1605
1606/// CreateType - get objective-c object type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001607llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1608 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001609 // Ignore protocols.
1610 return getOrCreateType(Ty->getBaseType(), Unit);
1611}
1612
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001613/// \return true if Getter has the default name for the property PD.
1614static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1615 const ObjCMethodDecl *Getter) {
1616 assert(PD);
1617 if (!Getter)
1618 return true;
1619
1620 assert(Getter->getDeclName().isObjCZeroArgSelector());
1621 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001622 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001623}
1624
1625/// \return true if Setter has the default name for the property PD.
1626static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1627 const ObjCMethodDecl *Setter) {
1628 assert(PD);
1629 if (!Setter)
1630 return true;
1631
1632 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001633 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001634 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001635}
1636
Guy Benyei11169dd2012-12-18 14:30:41 +00001637/// CreateType - get objective-c interface type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001638llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
1639 llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001640 ObjCInterfaceDecl *ID = Ty->getDecl();
1641 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001642 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001643
1644 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001645 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001646 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001647 auto RuntimeLang =
1648 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001649
1650 // If this is just a forward declaration return a special forward-declaration
1651 // debug type since we won't be able to lay out the entire type.
1652 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001653 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001654 llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001655 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1656 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001657 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001658 return FwdDecl;
1659 }
1660
David Blaikieef8a9512014-05-05 23:23:53 +00001661 return CreateTypeDefinition(Ty, Unit);
1662}
1663
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001664llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
1665 llvm::DIFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001666 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001667 llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001668 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001669 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001670
1671 // Bit size, align and offset of the type.
1672 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1673 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1674
1675 unsigned Flags = 0;
1676 if (ID->getImplementation())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001677 Flags |= llvm::DINode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001679 llvm::DICompositeType *RealDecl = DBuilder.createStructType(
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00001680 Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, nullptr,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001681 llvm::DINodeArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001682
David Blaikieef8a9512014-05-05 23:23:53 +00001683 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001684 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001686 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001687 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001688 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001689
1690 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001691 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001692
1693 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1694 if (SClass) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001695 llvm::DIType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001696 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001697 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001698 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001699
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001700 llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 EltTys.push_back(InhTag);
1702 }
1703
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001704 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001705 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001707 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 unsigned PLine = getLineNumber(Loc);
1709 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1710 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001711 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1712 PD->getName(), PUnit, PLine,
1713 hasDefaultGetterName(PD, Getter) ? ""
1714 : getSelectorName(PD->getGetterName()),
1715 hasDefaultSetterName(PD, Setter) ? ""
1716 : getSelectorName(PD->getSetterName()),
1717 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 EltTys.push_back(PropertyNode);
1719 }
1720
1721 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1722 unsigned FieldNo = 0;
1723 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1724 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001725 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001726 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001727 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001728
Guy Benyei11169dd2012-12-18 14:30:41 +00001729 StringRef FieldName = Field->getName();
1730
1731 // Ignore unnamed fields.
1732 if (FieldName.empty())
1733 continue;
1734
1735 // Get the location for the field.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001736 llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 unsigned FieldLine = getLineNumber(Field->getLocation());
1738 QualType FType = Field->getType();
1739 uint64_t FieldSize = 0;
1740 unsigned FieldAlign = 0;
1741
1742 if (!FType->isIncompleteArrayType()) {
1743
1744 // Bit size, align and offset of the type.
1745 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001746 ? Field->getBitWidthValue(CGM.getContext())
1747 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 FieldAlign = CGM.getContext().getTypeAlign(FType);
1749 }
1750
1751 uint64_t FieldOffset;
1752 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1753 // We don't know the runtime offset of an ivar if we're using the
1754 // non-fragile ABI. For bitfields, use the bit offset into the first
1755 // byte of storage of the bitfield. For other fields, use zero.
1756 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001757 FieldOffset =
1758 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001759 FieldOffset %= CGM.getContext().getCharWidth();
1760 } else {
1761 FieldOffset = 0;
1762 }
1763 } else {
1764 FieldOffset = RL.getFieldOffset(FieldNo);
1765 }
1766
1767 unsigned Flags = 0;
1768 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001769 Flags = llvm::DINode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001770 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001771 Flags = llvm::DINode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001772 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001773 Flags = llvm::DINode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001774
Craig Topper8a13c412014-05-21 05:09:00 +00001775 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001777 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001778 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001780 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001781 llvm::DIFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001782 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001783 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1784 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001785 PropertyNode = DBuilder.createObjCProperty(
1786 PD->getName(), PUnit, PLine,
1787 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1788 PD->getGetterName()),
1789 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1790 PD->getSetterName()),
1791 PD->getPropertyAttributes(),
1792 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 }
1794 }
1795 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001796 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1797 FieldSize, FieldAlign, FieldOffset, Flags,
1798 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001799 EltTys.push_back(FieldTy);
1800 }
1801
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001802 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001803 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001804
Guy Benyei11169dd2012-12-18 14:30:41 +00001805 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001806 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001807}
1808
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001809llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
1810 llvm::DIFile *Unit) {
1811 llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001812 int64_t Count = Ty->getNumElements();
1813 if (Count == 0)
1814 // If number of elements are not known then this is an unbounded array.
1815 // Use Count == -1 to express such arrays.
1816 Count = -1;
1817
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001818 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001819 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Guy Benyei11169dd2012-12-18 14:30:41 +00001820
1821 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1822 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1823
1824 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1825}
1826
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001827llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001828 uint64_t Size;
1829 uint64_t Align;
1830
1831 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1832 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1833 Size = 0;
1834 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001835 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001836 } else if (Ty->isIncompleteArrayType()) {
1837 Size = 0;
1838 if (Ty->getElementType()->isIncompleteType())
1839 Align = 0;
1840 else
1841 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001842 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001843 Size = 0;
1844 Align = 0;
1845 } else {
1846 // Size and align of the whole array, not the element type.
1847 Size = CGM.getContext().getTypeSize(Ty);
1848 Align = CGM.getContext().getTypeAlign(Ty);
1849 }
1850
1851 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1852 // interior arrays, do we care? Why aren't nested arrays represented the
1853 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001854 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001855 QualType EltTy(Ty, 0);
1856 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1857 // If the number of elements is known, then count is that number. Otherwise,
1858 // it's -1. This allows us to represent a subrange with an array of 0
1859 // elements, like this:
1860 //
1861 // struct foo {
1862 // int x[0];
1863 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001864 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1866 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001867
Guy Benyei11169dd2012-12-18 14:30:41 +00001868 // FIXME: Verify this is right for VLAs.
1869 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1870 EltTy = Ty->getElementType();
1871 }
1872
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001873 llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001874
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001875 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1876 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001877}
1878
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001879llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1880 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001881 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1882 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001883}
1884
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001885llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1886 llvm::DIFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001887 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1888 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001889}
1890
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001891llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
1892 llvm::DIFile *U) {
David Majnemer34568bc2015-05-26 21:54:24 +00001893 uint64_t Size = CGM.getCXXABI().isTypeInfoCalculable(QualType(Ty, 0))
1894 ? CGM.getContext().getTypeSize(Ty)
1895 : 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001896 llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Majnemer5fd33e02015-04-24 01:25:08 +00001897 if (Ty->isMemberDataPointerType())
David Blaikie2c705ca2013-01-19 19:20:56 +00001898 return DBuilder.createMemberPointerType(
David Majnemer34568bc2015-05-26 21:54:24 +00001899 getOrCreateType(Ty->getPointeeType(), U), ClassType, Size);
Adrian Prantl0866acd2013-12-19 01:38:47 +00001900
1901 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001902 Ty->getPointeeType()->getAs<FunctionProtoType>();
1903 return DBuilder.createMemberPointerType(
1904 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1905 Ty->getClass(), FPT->getTypeQuals())),
1906 FPT, U),
David Majnemer34568bc2015-05-26 21:54:24 +00001907 ClassType, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +00001908}
1909
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001910llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001911 // Ignore the atomic wrapping
1912 // FIXME: What is the correct representation?
1913 return getOrCreateType(Ty->getValueType(), U);
1914}
1915
1916/// CreateEnumType - get enumeration type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001917llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001918 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001919 uint64_t Size = 0;
1920 uint64_t Align = 0;
1921 if (!ED->getTypeForDecl()->isIncompleteType()) {
1922 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1923 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1924 }
1925
Manman Rene0064d82013-08-29 23:19:58 +00001926 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1927
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 // If this is just a forward declaration, construct an appropriately
1929 // marked node and just return it.
1930 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001931 llvm::DIScope *EDContext =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001932 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001933 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 unsigned Line = getLineNumber(ED->getLocation());
1935 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001936 llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001937 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001938 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001939 ReplaceMap.emplace_back(
1940 std::piecewise_construct, std::make_tuple(Ty),
1941 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001942 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001943 }
1944
David Blaikie483a9da2014-05-06 18:35:21 +00001945 return CreateTypeDefinition(Ty);
1946}
1947
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001948llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001949 const EnumDecl *ED = Ty->getDecl();
1950 uint64_t Size = 0;
1951 uint64_t Align = 0;
1952 if (!ED->getTypeForDecl()->isIncompleteType()) {
1953 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1954 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1955 }
1956
1957 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1958
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001959 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001960 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001961 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001962 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001963 Enumerators.push_back(DBuilder.createEnumerator(
1964 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001965 }
1966
1967 // Return a CompositeType for the enum itself.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001968 llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Guy Benyei11169dd2012-12-18 14:30:41 +00001969
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001970 llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001971 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001972 llvm::DIScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001973 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00001974 llvm::DIType *ClassTy =
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001975 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1976 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1977 Line, Size, Align, EltArray, ClassTy,
1978 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001979}
1980
David Blaikie05491062013-01-21 04:37:12 +00001981static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1982 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001983 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001984 Qualifiers InnerQuals = T.getLocalQualifiers();
1985 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1986 // that is already there.
1987 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1988 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 QualType LastT = T;
1990 switch (T->getTypeClass()) {
1991 default:
David Blaikie05491062013-01-21 04:37:12 +00001992 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001993 case Type::TemplateSpecialization: {
1994 const auto *Spec = cast<TemplateSpecializationType>(T);
1995 if (Spec->isTypeAlias())
1996 return C.getQualifiedType(T.getTypePtr(), Quals);
1997 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001998 break;
1999 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 case Type::TypeOfExpr:
2001 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
2002 break;
2003 case Type::TypeOf:
2004 T = cast<TypeOfType>(T)->getUnderlyingType();
2005 break;
2006 case Type::Decltype:
2007 T = cast<DecltypeType>(T)->getUnderlyingType();
2008 break;
2009 case Type::UnaryTransform:
2010 T = cast<UnaryTransformType>(T)->getUnderlyingType();
2011 break;
2012 case Type::Attributed:
2013 T = cast<AttributedType>(T)->getEquivalentType();
2014 break;
2015 case Type::Elaborated:
2016 T = cast<ElaboratedType>(T)->getNamedType();
2017 break;
2018 case Type::Paren:
2019 T = cast<ParenType>(T)->getInnerType();
2020 break;
David Blaikie05491062013-01-21 04:37:12 +00002021 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 break;
2024 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002025 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002026 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002027 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 break;
2029 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002030
Guy Benyei11169dd2012-12-18 14:30:41 +00002031 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002032 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 } while (true);
2034}
2035
Eric Christopher0fdcb312013-05-16 00:52:20 +00002036/// getType - Get the type from the cache or return null type if it doesn't
2037/// exist.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002038llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002039
2040 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002041 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002042
David Blaikief427b002014-05-06 03:42:01 +00002043 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002044 if (it != TypeCache.end()) {
2045 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002046 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002047 return cast<llvm::DIType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 }
2049
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002050 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002051}
2052
David Blaikie0e716b42014-03-03 23:48:23 +00002053void CGDebugInfo::completeTemplateDefinition(
2054 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002055 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2056 return;
2057
David Blaikie0e716b42014-03-03 23:48:23 +00002058 completeClassData(&SD);
2059 // In case this type has no member function definitions being emitted, ensure
2060 // it is retained
2061 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2062}
2063
Guy Benyei11169dd2012-12-18 14:30:41 +00002064/// getOrCreateType - Get the type from the cache or create a new
2065/// one if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002066llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002067 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002068 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002069
2070 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002071 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002072
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002073 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002074 return T;
2075
2076 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002077 llvm::DIType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002078 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002079
2080 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002081 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002082
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 return Res;
2084}
2085
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002086/// Currently the checksum of an interface includes the number of
2087/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002088unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002089 // The assumption is that the number of ivars can only increase
2090 // monotonically, so it is safe to just use their current number as
2091 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002092 unsigned Sum = 0;
2093 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002094 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002095 ++Sum;
2096
2097 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002098}
2099
2100ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2101 switch (Ty->getTypeClass()) {
2102 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002103 return getObjCInterfaceDecl(
2104 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002105 case Type::ObjCInterface:
2106 return cast<ObjCInterfaceType>(Ty)->getDecl();
2107 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002108 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002109 }
2110}
2111
Guy Benyei11169dd2012-12-18 14:30:41 +00002112/// CreateTypeNode - Create a new debug type node.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002113llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002114 // Handle qualifiers, which recursively handles what they refer to.
2115 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002116 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002117
Guy Benyei11169dd2012-12-18 14:30:41 +00002118 // Work out details of type.
2119 switch (Ty->getTypeClass()) {
2120#define TYPE(Class, Base)
2121#define ABSTRACT_TYPE(Class, Base)
2122#define NON_CANONICAL_TYPE(Class, Base)
2123#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2124#include "clang/AST/TypeNodes.def"
2125 llvm_unreachable("Dependent types cannot show up in debug information");
2126
2127 case Type::ExtVector:
2128 case Type::Vector:
2129 return CreateType(cast<VectorType>(Ty), Unit);
2130 case Type::ObjCObjectPointer:
2131 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2132 case Type::ObjCObject:
2133 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2134 case Type::ObjCInterface:
2135 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2136 case Type::Builtin:
2137 return CreateType(cast<BuiltinType>(Ty));
2138 case Type::Complex:
2139 return CreateType(cast<ComplexType>(Ty));
2140 case Type::Pointer:
2141 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002142 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002143 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002144 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002145 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002146 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002147 case Type::BlockPointer:
2148 return CreateType(cast<BlockPointerType>(Ty), Unit);
2149 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002150 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002152 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002154 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002155 case Type::FunctionProto:
2156 case Type::FunctionNoProto:
2157 return CreateType(cast<FunctionType>(Ty), Unit);
2158 case Type::ConstantArray:
2159 case Type::VariableArray:
2160 case Type::IncompleteArray:
2161 return CreateType(cast<ArrayType>(Ty), Unit);
2162
2163 case Type::LValueReference:
2164 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2165 case Type::RValueReference:
2166 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2167
2168 case Type::MemberPointer:
2169 return CreateType(cast<MemberPointerType>(Ty), Unit);
2170
2171 case Type::Atomic:
2172 return CreateType(cast<AtomicType>(Ty), Unit);
2173
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002175 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2176
David Blaikie42edade2014-11-11 20:44:45 +00002177 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002178 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 case Type::Elaborated:
2180 case Type::Paren:
2181 case Type::SubstTemplateTypeParm:
2182 case Type::TypeOfExpr:
2183 case Type::TypeOf:
2184 case Type::Decltype:
2185 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002186 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002187 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002189
David Blaikie42edade2014-11-11 20:44:45 +00002190 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002191}
2192
2193/// getOrCreateLimitedType - Get the type from the cache or create a new
2194/// limited type if necessary.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002195llvm::DIType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
2196 llvm::DIFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002197 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002198
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002199 auto *T = cast_or_null<llvm::DICompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002200
2201 // We may have cached a forward decl when we could have created
2202 // a non-forward decl. Go ahead and create a non-forward decl
2203 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002204 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002205 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002206
2207 // Otherwise create the type.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002208 llvm::DICompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002209
2210 // Propagate members from the declaration to the definition
2211 // CreateType(const RecordType*) will overwrite this with the members in the
2212 // correct order if the full type is needed.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002213 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002214
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002216 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 return Res;
2218}
2219
2220// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002221llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002223
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002225 llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002226 unsigned Line = getLineNumber(RD->getLocation());
2227 StringRef RDName = getClassName(RD);
2228
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002229 llvm::DIScope *RDContext =
Eric Christopher07429ff2013-10-15 21:22:34 +00002230 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002231
David Blaikied2785892013-08-18 17:36:19 +00002232 // If we ended up creating the type during the context chain construction,
2233 // just return that.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002234 auto *T = cast_or_null<llvm::DICompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002235 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002236 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002237 return T;
David Blaikied2785892013-08-18 17:36:19 +00002238
Adrian Prantl381e7552014-02-04 21:29:50 +00002239 // If this is just a forward or incomplete declaration, construct an
2240 // appropriately marked node and just return it.
2241 const RecordDecl *D = RD->getDefinition();
2242 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002243 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002244
2245 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2246 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002247
Manman Rene0064d82013-08-29 23:19:58 +00002248 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2249
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002250 llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002251 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2252 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002253
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002254 RegionMap[Ty->getDecl()].reset(RealDecl);
2255 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002256
David Blaikieadfbf992013-08-18 16:55:33 +00002257 if (const ClassTemplateSpecializationDecl *TSpecial =
2258 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002259 DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002260 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002261 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002262}
2263
David Blaikieadfbf992013-08-18 16:55:33 +00002264void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002265 llvm::DICompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002266 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002267 llvm::DICompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002268 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2269 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002270 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002271 while (1) {
2272 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2273 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2274 if (PBT && !BRL.isPrimaryBaseVirtual())
2275 PBase = PBT;
2276 else
2277 break;
2278 }
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002279 ContainingType = cast<llvm::DICompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002280 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2281 getOrCreateFile(RD->getLocation())));
2282 } else if (RD->isDynamicClass())
2283 ContainingType = RealDecl;
2284
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002285 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002286}
2287
Guy Benyei11169dd2012-12-18 14:30:41 +00002288/// CreateMemberType - Create new member and increase Offset by FType's size.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002289llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002290 StringRef Name, uint64_t *Offset) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002291 llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2293 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002294 llvm::DIType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002295 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002296 *Offset += FieldSize;
2297 return Ty;
2298}
2299
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002300void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002301 StringRef &Name,
2302 StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002303 llvm::DIScope *&FDContext,
2304 llvm::DINodeArray &TParamsArray,
Duncan P. N. Exon Smith8e47da42015-04-21 20:07:29 +00002305 unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002306 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2307 Name = getFunctionName(FD);
2308 // Use mangled name as linkage name for C/C++ functions.
2309 if (FD->hasPrototype()) {
2310 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002311 Flags |= llvm::DINode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002312 }
2313 // No need to replicate the linkage name if it isn't different from the
2314 // subprogram name, no need to have it at all unless coverage is enabled or
2315 // debug is set to more than just line tables.
2316 if (LinkageName == Name ||
2317 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2318 !CGM.getCodeGenOpts().EmitGcovNotes &&
2319 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2320 LinkageName = StringRef();
2321
2322 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2323 if (const NamespaceDecl *NSDecl =
2324 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2325 FDContext = getOrCreateNameSpace(NSDecl);
2326 else if (const RecordDecl *RDecl =
2327 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2328 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2329 // Collect template parameters.
2330 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2331 }
2332}
2333
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002334void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002335 unsigned &LineNo, QualType &T,
2336 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002337 llvm::DIScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002338 Unit = getOrCreateFile(VD->getLocation());
2339 LineNo = getLineNumber(VD->getLocation());
2340
2341 setLocation(VD->getLocation());
2342
2343 T = VD->getType();
2344 if (T->isIncompleteArrayType()) {
2345 // CodeGen turns int[] into int[1] so we'll do the same here.
2346 llvm::APInt ConstVal(32, 1);
2347 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2348
2349 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2350 ArrayType::Normal, 0);
2351 }
2352
2353 Name = VD->getName();
2354 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2355 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2356 LinkageName = CGM.getMangledName(VD);
2357 if (LinkageName == Name)
2358 LinkageName = StringRef();
2359
2360 // Since we emit declarations (DW_AT_members) for static members, place the
2361 // definition of those static members in the namespace they were declared in
2362 // in the source code (the lexical decl context).
2363 // FIXME: Generalize this for even non-member global variables where the
2364 // declaration and definition may have different lexical decl contexts, once
2365 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002366 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2367 : VD->getDeclContext();
2368 // When a record type contains an in-line initialization of a static data
2369 // member, and the record type is marked as __declspec(dllexport), an implicit
2370 // definition of the member will be created in the record context. DWARF
2371 // doesn't seem to have a nice way to describe this in a form that consumers
2372 // are likely to understand, so fake the "normal" situation of a definition
2373 // outside the class by putting it in the global scope.
2374 if (DC->isRecord())
2375 DC = CGM.getContext().getTranslationUnitDecl();
2376 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002377}
2378
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002379llvm::DISubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002380CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002381 llvm::DINodeArray TParamsArray;
Frederic Rissd253ed62014-11-18 03:40:51 +00002382 StringRef Name, LinkageName;
2383 unsigned Flags = 0;
2384 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002385 llvm::DIFile *Unit = getOrCreateFile(Loc);
2386 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002387 unsigned Line = getLineNumber(Loc);
2388
2389 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2390 TParamsArray, Flags);
2391 // Build function type.
2392 SmallVector<QualType, 16> ArgTypes;
2393 for (const ParmVarDecl *Parm: FD->parameters())
2394 ArgTypes.push_back(Parm->getType());
2395 QualType FnType =
2396 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2397 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002398 llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002399 DContext, Name, LinkageName, Unit, Line,
2400 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2401 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2402 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002403 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002404 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2405 std::make_tuple(CanonDecl),
2406 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002407 return SP;
2408}
2409
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002410llvm::DIGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002411CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2412 QualType T;
2413 StringRef Name, LinkageName;
2414 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002415 llvm::DIFile *Unit = getOrCreateFile(Loc);
2416 llvm::DIScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002417 unsigned Line = getLineNumber(Loc);
2418
2419 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002420 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2421 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2422 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002423 FwdDeclReplaceMap.emplace_back(
2424 std::piecewise_construct,
2425 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2426 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002427 return GV;
2428}
2429
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002430llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002431 // We only need a declaration (not a definition) of the type - so use whatever
2432 // we would otherwise do to get a type for a pointee. (forward declarations in
2433 // limited debug info, full definitions (if the type definition is available)
2434 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002435 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2436 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002437 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002438 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002439
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002440 if (I != DeclCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002441 return dyn_cast_or_null<llvm::DINode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002442
2443 // No definition for now. Emit a forward definition that might be
2444 // merged with a potential upcoming definition.
2445 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2446 return getFunctionForwardDeclaration(FD);
2447 else if (const auto *VD = dyn_cast<VarDecl>(D))
2448 return getGlobalVariableForwardDeclaration(VD);
2449
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002450 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002451}
2452
Guy Benyei11169dd2012-12-18 14:30:41 +00002453/// getFunctionDeclaration - Return debug info descriptor to describe method
2454/// declaration for the given method definition.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002455llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002456 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002457 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002458
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002460 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002461 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002462
2463 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002464 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002465
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002466 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002467 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002468 if (const CXXMethodDecl *MD =
2469 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002470 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002471 cast<llvm::DICompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002472 }
2473 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002475 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002476 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 return SP;
2478 }
2479
Aaron Ballman86c93902014-03-06 23:45:36 +00002480 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002481 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002483 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002484 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 return SP;
2486 }
2487 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002488 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002489}
2490
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002491// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002492// implicit parameter "this".
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002493llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002494 QualType FnType,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002495 llvm::DIFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002496 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002497 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2498 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002499 return DBuilder.createSubroutineType(F,
2500 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002501
2502 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2503 return getOrCreateMethodType(Method, F);
2504 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2505 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002506 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002507
2508 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002509 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002510
2511 // Replace the instancetype keyword with the actual type.
2512 if (ResultTy == CGM.getContext().getObjCInstanceType())
2513 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002514 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002515
Adrian Prantl7bec9032013-05-10 21:08:31 +00002516 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002518 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002519 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002520 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002521 Elts.push_back(DBuilder.createArtificialType(
2522 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002524 for (const auto *PI : OMethod->params())
2525 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002526 // Variadic methods need a special marker at the end of the type list.
2527 if (OMethod->isVariadic())
2528 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002529
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002530 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 return DBuilder.createSubroutineType(F, EltTypeArray);
2532 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002533
Adrian Prantl800faef2014-02-25 23:42:18 +00002534 // Handle variadic function types; they need an additional
2535 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002536 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2537 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002538 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002539 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2540 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2541 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2542 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2543 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002544 llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002545 return DBuilder.createSubroutineType(F, EltTypeArray);
2546 }
2547
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002548 return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002549}
2550
2551/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002552void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2553 SourceLocation ScopeLoc, QualType FnType,
2554 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002555
2556 StringRef Name;
2557 StringRef LinkageName;
2558
2559 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2560
2561 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002562 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002563
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 unsigned Flags = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002565 llvm::DIFile *Unit = getOrCreateFile(Loc);
2566 llvm::DIScope *FDContext = Unit;
2567 llvm::DINodeArray TParamsArray;
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 if (!HasDecl) {
2569 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002570 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002572 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002573 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002574 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002575 auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002576 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002577 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002578 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 return;
2580 }
2581 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002582 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2583 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2585 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002586 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 } else {
2588 // Use llvm function name.
2589 Name = Fn->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002590 Flags |= llvm::DINode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002591 }
2592 if (!Name.empty() && Name[0] == '\01')
2593 Name = Name.substr(1);
2594
Adrian Prantl42d71b92014-04-10 23:21:53 +00002595 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002596 Flags |= llvm::DINode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002597 // Artificial functions without a location should not silently reuse CurLoc.
2598 if (Loc.isInvalid())
2599 CurLoc = SourceLocation();
2600 }
2601 unsigned LineNo = getLineNumber(Loc);
2602 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002603
Eric Christopher8018e412014-03-27 18:50:35 +00002604 // FIXME: The function declaration we're constructing here is mostly reusing
2605 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2606 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2607 // all subprograms instead of the actual context since subprogram definitions
2608 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002609 llvm::DISubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002610 FDContext, Name, LinkageName, Unit, LineNo,
2611 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2612 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002613 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002614 // We might get here with a VarDecl in the case we're generating
2615 // code for the initialization of globals. Do not record these decls
2616 // as they will overwrite the actual VarDecl Decl in the cache.
2617 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002618 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002619
Adrian Prantlbebb8932014-03-21 21:01:58 +00002620 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002621 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002622
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002624 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002625}
2626
2627/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002628/// information in the source file. If the location is invalid, the
2629/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002630void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002631 // Update our current location
2632 setLocation(Loc);
2633
Eric Christophere7b87e52014-10-26 23:40:33 +00002634 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2635 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002636
Adrian Prantle83b1302014-01-07 22:05:52 +00002637 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002638 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002639 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002640}
2641
2642/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2643/// the stack.
2644void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002645 llvm::MDNode *Back = nullptr;
2646 if (!LexicalBlockStack.empty())
2647 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002648 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002649 cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002650 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002651}
2652
2653/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2654/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002655void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2656 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002657 // Set our current location.
2658 setLocation(Loc);
2659
Guy Benyei11169dd2012-12-18 14:30:41 +00002660 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002661 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2662 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002663
2664 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2665 return;
2666
2667 // Create a new lexical block and push it on the stack.
2668 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002669}
2670
2671/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2672/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002673void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2674 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002675 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2676
2677 // Provide an entry in the line table for the end of the block.
2678 EmitLocation(Builder, Loc);
2679
David Blaikie60a877b2014-10-22 19:34:33 +00002680 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2681 return;
2682
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 LexicalBlockStack.pop_back();
2684}
2685
2686/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2687void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2688 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2689 unsigned RCount = FnBeginRegionCount.back();
2690 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2691
2692 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002693 while (LexicalBlockStack.size() != RCount) {
2694 // Provide an entry in the line table for the end of the block.
2695 EmitLocation(Builder, CurLoc);
2696 LexicalBlockStack.pop_back();
2697 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002698 FnBeginRegionCount.pop_back();
2699}
2700
Eric Christopherb2a008c2013-05-16 00:45:12 +00002701// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002702// See BuildByRefType.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002703llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002704 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002705
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002706 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002707 QualType FType;
2708 uint64_t FieldSize, FieldOffset;
2709 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002710
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002711 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002712 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002713
2714 FieldOffset = 0;
2715 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2716 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2717 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2718 FType = CGM.getContext().IntTy;
2719 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2720 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2721
2722 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2723 if (HasCopyAndDispose) {
2724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002725 EltTys.push_back(
2726 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2727 EltTys.push_back(
2728 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002729 }
2730 bool HasByrefExtendedLayout;
2731 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002732 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2733 HasByrefExtendedLayout) &&
2734 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002735 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002736 EltTys.push_back(
2737 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002738 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002739
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2741 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002742 CGM.getTarget().getPointerAlign(0))) {
2743 CharUnits FieldOffsetInBytes =
2744 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2745 CharUnits AlignedOffsetInBytes =
2746 FieldOffsetInBytes.RoundUpToAlignment(Align);
2747 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002748
Guy Benyei11169dd2012-12-18 14:30:41 +00002749 if (NumPaddingBytes.isPositive()) {
2750 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2751 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2752 pad, ArrayType::Normal, 0);
2753 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2754 }
2755 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002756
Guy Benyei11169dd2012-12-18 14:30:41 +00002757 FType = Type;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002758 llvm::DIType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002759 FieldSize = CGM.getContext().getTypeSize(FType);
2760 FieldAlign = CGM.getContext().toBits(Align);
2761
Eric Christopherb2a008c2013-05-16 00:45:12 +00002762 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002763 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2764 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 EltTys.push_back(FieldTy);
2766 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002767
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002768 llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002769
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002770 unsigned Flags = llvm::DINode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002771
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002773 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002774}
2775
2776/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002777void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002778 llvm::Value *Storage, unsigned ArgNo,
2779 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002780 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2782
David Blaikie7fceebf2013-08-19 03:37:48 +00002783 bool Unwritten =
2784 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2785 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002786 llvm::DIFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002787 if (!Unwritten)
2788 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002789 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 uint64_t XOffset = 0;
2791 if (VD->hasAttr<BlocksAttr>())
2792 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002793 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002794 Ty = getOrCreateType(VD->getType(), Unit);
2795
2796 // If there is no debug info for this type then do not emit debug info
2797 // for this variable.
2798 if (!Ty)
2799 return;
2800
Guy Benyei11169dd2012-12-18 14:30:41 +00002801 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002802 unsigned Line = 0;
2803 unsigned Column = 0;
2804 if (!Unwritten) {
2805 Line = getLineNumber(VD->getLocation());
2806 Column = getColumnNumber(VD->getLocation());
2807 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002808 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 unsigned Flags = 0;
2810 if (VD->isImplicit())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002811 Flags |= llvm::DINode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002812 // If this is the first argument and it is implicit then
2813 // give it an object pointer flag.
2814 // FIXME: There has to be a better way to do this, but for static
2815 // functions there won't be an implicit param at arg1 and
2816 // otherwise it is 'self' or 'this'.
2817 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002818 Flags |= llvm::DINode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002819 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002820 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2821 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002822 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002823
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002824 auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002825
2826 StringRef Name = VD->getName();
2827 if (!Name.empty()) {
2828 if (VD->hasAttr<BlocksAttr>()) {
2829 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002830 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 // offset of __forwarding field
2832 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002833 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002834 Expr.push_back(offset.getQuantity());
2835 Expr.push_back(llvm::dwarf::DW_OP_deref);
2836 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 // offset of x field
2838 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002839 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002840
2841 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002842 auto *D = DBuilder.createLocalVariable(Tag, Scope, VD->getName(), Unit,
2843 Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002844
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002846 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2847 llvm::DebugLoc::get(Line, Column, Scope),
2848 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002850 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002851 Expr.push_back(llvm::dwarf::DW_OP_deref);
Adrian Prantl0d820892015-04-29 15:05:50 +00002852 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2853 // If VD is an anonymous union then Storage represents value for
2854 // all union fields.
2855 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2856 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Adrian Prantl00820ab2015-04-29 16:52:31 +00002857 // GDB has trouble finding local variables in anonymous unions, so we emit
2858 // artifical local variables for each of the members.
2859 //
2860 // FIXME: Remove this code as soon as GDB supports this.
2861 // The debug info verifier in LLVM operates based on the assumption that a
2862 // variable has the same size as its storage and we had to disable the check
2863 // for artificial variables.
Adrian Prantl0d820892015-04-29 15:05:50 +00002864 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002865 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Adrian Prantl0d820892015-04-29 15:05:50 +00002866 StringRef FieldName = Field->getName();
2867
2868 // Ignore unnamed fields. Do not ignore unnamed records.
2869 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2870 continue;
2871
2872 // Use VarDecl's Tag, Scope and Line number.
2873 auto *D = DBuilder.createLocalVariable(
2874 Tag, Scope, FieldName, Unit, Line, FieldTy,
Adrian Prantl00820ab2015-04-29 16:52:31 +00002875 CGM.getLangOpts().Optimize, Flags | llvm::DINode::FlagArtificial,
2876 ArgNo);
Adrian Prantl0d820892015-04-29 15:05:50 +00002877
2878 // Insert an llvm.dbg.declare into the current block.
2879 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2880 llvm::DebugLoc::get(Line, Column, Scope),
2881 Builder.GetInsertBlock());
2882 }
Adrian Prantl0d820892015-04-29 15:05:50 +00002883 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002885
2886 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002887 auto *D =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002888 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2889 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002890
2891 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002892 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2893 llvm::DebugLoc::get(Line, Column, Scope),
2894 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002895}
2896
2897void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2898 llvm::Value *Storage,
2899 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002900 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2902}
2903
Adrian Prantlde17db32013-03-29 19:20:29 +00002904/// Look up the completed type for a self pointer in the TypeCache and
2905/// create a copy of it with the ObjectPointer and Artificial flags
2906/// set. If the type is not cached, a new one is created. This should
2907/// never happen though, since creating a type for the implicit self
2908/// argument implies that we already parsed the interface definition
2909/// and the ivar declarations in the implementation.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002910llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2911 llvm::DIType *Ty) {
2912 llvm::DIType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002913 if (CachedTy)
2914 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002915 return DBuilder.createObjectPointerType(Ty);
2916}
2917
Eric Christophere7b87e52014-10-26 23:40:33 +00002918void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2919 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002920 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002921 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002923
Craig Topper8a13c412014-05-21 05:09:00 +00002924 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002925 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002926
Guy Benyei11169dd2012-12-18 14:30:41 +00002927 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002928
Guy Benyei11169dd2012-12-18 14:30:41 +00002929 uint64_t XOffset = 0;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002930 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
2931 llvm::DIType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 if (isByRef)
2933 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002934 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002935 Ty = getOrCreateType(VD->getType(), Unit);
2936
2937 // Self is passed along as an implicit non-arg variable in a
2938 // block. Mark it as the object pointer.
2939 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002940 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002941
2942 // Get location information.
2943 unsigned Line = getLineNumber(VD->getLocation());
2944 unsigned Column = getColumnNumber(VD->getLocation());
2945
2946 const llvm::DataLayout &target = CGM.getDataLayout();
2947
2948 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002949 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002950 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2951
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002952 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002953 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002954 addr.push_back(llvm::dwarf::DW_OP_deref);
2955 addr.push_back(llvm::dwarf::DW_OP_plus);
2956 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002958 addr.push_back(llvm::dwarf::DW_OP_deref);
2959 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002961 offset =
2962 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002963 addr.push_back(offset.getQuantity());
2964 addr.push_back(llvm::dwarf::DW_OP_deref);
2965 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002966 // offset of x field
2967 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002968 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 }
2970
2971 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002972 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002973 llvm::dwarf::DW_TAG_auto_variable,
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00002974 cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002975 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002976
Guy Benyei11169dd2012-12-18 14:30:41 +00002977 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002978 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2979 if (InsertPoint)
2980 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2981 InsertPoint);
2982 else
2983 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2984 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002985}
2986
2987/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2988/// variable declaration.
2989void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2990 unsigned ArgNo,
2991 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002992 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002993 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2994}
2995
2996namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002997struct BlockLayoutChunk {
2998 uint64_t OffsetInBits;
2999 const BlockDecl::Capture *Capture;
3000};
3001bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
3002 return l.OffsetInBits < r.OffsetInBits;
3003}
Guy Benyei11169dd2012-12-18 14:30:41 +00003004}
3005
3006void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003007 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00003008 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00003009 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00003011 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 ASTContext &C = CGM.getContext();
3013 const BlockDecl *blockDecl = block.getBlockDecl();
3014
3015 // Collect some general information about the block's location.
3016 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003017 llvm::DIFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003018 unsigned line = getLineNumber(loc);
3019 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003020
Guy Benyei11169dd2012-12-18 14:30:41 +00003021 // Build the debug-info type for the block literal.
3022 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
3023
3024 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00003025 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003026
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003027 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3029 blockLayout->getElementOffsetInBits(0),
3030 tunit, tunit));
3031 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3032 blockLayout->getElementOffsetInBits(1),
3033 tunit, tunit));
3034 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3035 blockLayout->getElementOffsetInBits(2),
3036 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003037 auto *FnTy = block.getBlockExpr()->getFunctionType();
3038 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3039 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 blockLayout->getElementOffsetInBits(3),
3041 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003042 fields.push_back(createFieldType(
3043 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3044 ? C.getBlockDescriptorExtendedType()
3045 : C.getBlockDescriptorType()),
3046 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003047
3048 // We want to sort the captures by offset, not because DWARF
3049 // requires this, but because we're paranoid about debuggers.
3050 SmallVector<BlockLayoutChunk, 8> chunks;
3051
3052 // 'this' capture.
3053 if (blockDecl->capturesCXXThis()) {
3054 BlockLayoutChunk chunk;
3055 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003056 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003057 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 chunks.push_back(chunk);
3059 }
3060
3061 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003062 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003063 const VarDecl *variable = capture.getVariable();
3064 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3065
3066 // Ignore constant captures.
3067 if (captureInfo.isConstant())
3068 continue;
3069
3070 BlockLayoutChunk chunk;
3071 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003072 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 chunk.Capture = &capture;
3074 chunks.push_back(chunk);
3075 }
3076
3077 // Sort by offset.
3078 llvm::array_pod_sort(chunks.begin(), chunks.end());
3079
Eric Christophere7b87e52014-10-26 23:40:33 +00003080 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3081 e = chunks.end();
3082 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003083 uint64_t offsetInBits = i->OffsetInBits;
3084 const BlockDecl::Capture *capture = i->Capture;
3085
3086 // If we have a null capture, this must be the C++ 'this' capture.
3087 if (!capture) {
3088 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003089 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003090 QualType type = method->getThisType(C);
3091
3092 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3093 offsetInBits, tunit, tunit));
3094 continue;
3095 }
3096
3097 const VarDecl *variable = capture->getVariable();
3098 StringRef name = variable->getName();
3099
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003100 llvm::DIType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003102 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003103
3104 // FIXME: this creates a second copy of this type!
3105 uint64_t xoffset;
3106 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003107 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3108 fieldType =
3109 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3110 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003112 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3113 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 }
3115 fields.push_back(fieldType);
3116 }
3117
3118 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003119 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3120 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003121
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003122 llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields);
Guy Benyei11169dd2012-12-18 14:30:41 +00003123
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003124 llvm::DIType *type = DBuilder.createStructType(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003125 tunit, typeName.str(), tunit, line,
3126 CGM.getContext().toBits(block.BlockSize),
3127 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3129
3130 // Get overall information about the block.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003131 unsigned flags = llvm::DINode::FlagArtificial;
3132 auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003133
3134 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003135 auto *debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003136 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3137 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003138
Adrian Prantl616bef42013-03-14 21:52:59 +00003139 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003140 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003141 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003142 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003143 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003144 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003145
Adrian Prantl616bef42013-03-14 21:52:59 +00003146 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003147 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3148 llvm::DebugLoc::get(line, column, scope),
3149 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003150}
3151
David Blaikie6943dea2013-08-20 01:28:15 +00003152/// If D is an out-of-class definition of a static data member of a class, find
3153/// its corresponding in-class declaration.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003154llvm::DIDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003155CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3156 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003157 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003158
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003159 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003160 if (MI != StaticDataMemberCache.end()) {
3161 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003162 return cast<llvm::DIDerivedType>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003163 }
David Blaikiece763042013-08-20 21:49:21 +00003164
3165 // If the member wasn't found in the cache, lazily construct and add it to the
3166 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003167 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003168 auto *Ctxt =
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003169 cast<llvm::DICompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003170 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003171}
3172
Eric Christophercab9fae2014-04-10 05:20:00 +00003173/// Recursively collect all of the member fields of a global anonymous decl and
3174/// create static variables for them. The first time this is called it needs
3175/// 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 +00003176llvm::DIGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
3177 const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo,
3178 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) {
3179 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003180
3181 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003182 llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003183 StringRef FieldName = Field->getName();
3184
3185 // Ignore unnamed fields, but recurse into anonymous records.
3186 if (FieldName.empty()) {
3187 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3188 if (RT)
3189 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3190 Var, DContext);
3191 continue;
3192 }
3193 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003194 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3195 LineNo, FieldTy,
3196 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003197 }
3198 return GV;
3199}
3200
Guy Benyei11169dd2012-12-18 14:30:41 +00003201/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003202void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003204 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 // Create global variable debug descriptor.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003206 llvm::DIFile *Unit = nullptr;
3207 llvm::DIScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003208 unsigned LineNo;
3209 StringRef DeclName, LinkageName;
3210 QualType T;
3211 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003212
3213 // Attempt to store one global variable for the declaration - even if we
3214 // emit a lot of fields.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003215 llvm::DIGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003216
3217 // If this is an anonymous union then we'll want to emit a global
3218 // variable for each member of the anonymous union so that it's possible
3219 // to find the name of any field in the union.
3220 if (T->isUnionType() && DeclName.empty()) {
3221 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003222 assert(RD->isAnonymousStructOrUnion() &&
3223 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003224 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3225 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003226 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003227 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3228 Var->hasInternalLinkage(), Var,
3229 getOrCreateStaticDataMemberDeclarationOrNull(D));
3230 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003231 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003232}
3233
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003234/// EmitGlobalVariable - Emit global variable's debug info.
3235void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3236 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003237 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003238 // Create the descriptor for the variable.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003239 llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003240 StringRef Name = VD->getName();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003241 llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003242 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3243 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3244 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3245 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3246 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003247 // Do not use global variables for enums.
3248 //
3249 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003250 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003251 return;
David Blaikiea15565562014-04-04 20:56:17 +00003252 // Do not emit separate definitions for function local const/statics.
3253 if (isa<FunctionDecl>(VD->getDeclContext()))
3254 return;
David Blaikiebb113912014-04-05 07:23:17 +00003255 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003256 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003257 if (VarD->isStaticDataMember()) {
3258 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3259 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003260 // Ensure that the type is retained even though it's otherwise unreferenced.
3261 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003262 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003263 return;
3264 }
3265
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003266 llvm::DIScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003267 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3268
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003269 auto &GV = DeclCache[VD];
3270 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003271 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003272 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003273 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003274 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003275}
3276
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003277llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003278 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003279 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003280 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003281}
3282
David Blaikie9f88fe82013-04-22 06:13:21 +00003283void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003284 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3285 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003286 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003287 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3288 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003289 getLineNumber(UD.getLocation()));
3290}
3291
David Blaikiebd483762013-05-20 04:58:53 +00003292void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3293 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3294 return;
3295 assert(UD.shadow_size() &&
3296 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3297 // Emitting one decl is sufficient - debuggers can detect that this is an
3298 // overloaded name & provide lookup for all the overloads.
3299 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003300 if (llvm::DINode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003301 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003302 DBuilder.createImportedDeclaration(
3303 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3304 getLineNumber(USD.getLocation()));
3305}
3306
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003307llvm::DIImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003308CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3309 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003310 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003311 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003312 if (VH)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003313 return cast<llvm::DIImportedEntity>(VH);
3314 llvm::DIImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003315 if (const NamespaceAliasDecl *Underlying =
3316 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3317 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003318 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003319 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3320 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3321 NA.getName());
3322 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003323 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003324 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3325 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3326 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003327 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003328 return R;
3329}
3330
Guy Benyei11169dd2012-12-18 14:30:41 +00003331/// getOrCreateNamesSpace - Return namespace descriptor for the given
3332/// namespace decl.
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003333llvm::DINamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003334CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003335 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003336 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003337 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003338 return cast<llvm::DINamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003339
Guy Benyei11169dd2012-12-18 14:30:41 +00003340 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003341 llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation());
3342 llvm::DIScope *Context =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003343 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003344 llvm::DINamespace *NS =
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003345 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003346 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003347 return NS;
3348}
3349
3350void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003351 // Creating types might create further types - invalidating the current
3352 // element and the size(), so don't cache/reference them.
3353 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3354 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003355 llvm::DIType *Ty = E.Type->getDecl()->getDefinition()
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003356 ? CreateTypeDefinition(E.Type, E.Unit)
3357 : E.Decl;
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003358 DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003359 }
3360
David Blaikief427b002014-05-06 03:42:01 +00003361 for (auto p : ReplaceMap) {
3362 assert(p.second);
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003363 auto *Ty = cast<llvm::DIType>(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003364 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003365
David Blaikief427b002014-05-06 03:42:01 +00003366 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003367 assert(it != TypeCache.end());
3368 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003369
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003370 DBuilder.replaceTemporary(llvm::TempDIType(Ty),
3371 cast<llvm::DIType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003373
Frederic Rissd253ed62014-11-18 03:40:51 +00003374 for (const auto &p : FwdDeclReplaceMap) {
3375 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003376 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003377 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003378
3379 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003380 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003381 // with ourselves, that will destroy the temporary MDNode and
3382 // replace it with a standard one, avoiding leaking memory.
3383 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003384 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003385 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003386 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003387
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003388 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003389 }
3390
Adrian Prantl73409ce2013-03-11 18:33:46 +00003391 // We keep our own list of retained types, because we need to look
3392 // up the final type in the type cache.
3393 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3394 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smith9dd4e4e2015-04-29 16:40:08 +00003395 DBuilder.retainType(cast<llvm::DIType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003396
Guy Benyei11169dd2012-12-18 14:30:41 +00003397 DBuilder.finalize();
3398}
David Blaikie66088d52014-09-24 17:01:27 +00003399
3400void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3401 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3402 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003403
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003404 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003405 // Don't ignore in case of explicit cast where it is referenced indirectly.
3406 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003407}