blob: d105d48ba7eb96e4e0d040f5e86abc94966d4b1c [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 Smith373ee852015-04-16 01:36:36 +0000123 auto *Scope = cast<llvm::MDScope>(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 Smith87afdeb2015-04-14 03:24:14 +0000129 if (auto *LBF = dyn_cast<llvm::MDLexicalBlockFile>(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 Smithc7551282015-04-06 23:21:33 +0000133 } else if (isa<llvm::MDLexicalBlock>(Scope) ||
134 isa<llvm::MDSubprogram>(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 Smithfc8d9d92015-04-20 18:32:15 +0000142llvm::MDScope *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 Smithc7551282015-04-06 23:21:33 +0000149 return dyn_cast_or_null<llvm::MDScope>(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 Smitha7fbcbf2015-04-20 22:09:57 +0000246llvm::MDFile *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 Smithc7551282015-04-06 23:21:33 +0000265 return cast<llvm::MDFile>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +0000266 }
267
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000268 llvm::MDFile *F =
269 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 Smitha7fbcbf2015-04-20 22:09:57 +0000276llvm::MDFile *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,
382 DebugKind != CodeGenOptions::LocTrackingOnly);
Guy Benyei11169dd2012-12-18 14:30:41 +0000383}
384
385/// CreateType - Get the Basic type from the cache or create a new
386/// one if necessary.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000387llvm::MDType *CGDebugInfo::CreateType(const BuiltinType *BT) {
Ed Masteda706022014-05-07 12:49:30 +0000388 llvm::dwarf::TypeKind Encoding;
Guy Benyei11169dd2012-12-18 14:30:41 +0000389 StringRef BTName;
390 switch (BT->getKind()) {
391#define BUILTIN_TYPE(Id, SingletonId)
Eric Christophere7b87e52014-10-26 23:40:33 +0000392#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
Guy Benyei11169dd2012-12-18 14:30:41 +0000393#include "clang/AST/BuiltinTypes.def"
394 case BuiltinType::Dependent:
395 llvm_unreachable("Unexpected builtin type");
396 case BuiltinType::NullPtr:
Peter Collingbourne5c5e6172013-06-27 22:51:01 +0000397 return DBuilder.createNullPtrType();
Guy Benyei11169dd2012-12-18 14:30:41 +0000398 case BuiltinType::Void:
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000399 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +0000400 case BuiltinType::ObjCClass:
David Blaikief427b002014-05-06 03:42:01 +0000401 if (!ClassTy)
402 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
403 "objc_class", TheCU,
404 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000405 return ClassTy;
406 case BuiltinType::ObjCId: {
407 // typedef struct objc_class *Class;
408 // typedef struct objc_object {
409 // Class isa;
410 // } *id;
411
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000412 if (ObjTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000413 return ObjTy;
414
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000415 if (!ClassTy)
Guy Benyei11169dd2012-12-18 14:30:41 +0000416 ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
417 "objc_class", TheCU,
418 getOrCreateMainFile(), 0);
419
420 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Eric Christopherb2a008c2013-05-16 00:45:12 +0000421
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000422 auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000423
Eric Christopher5c7ee8b2013-04-02 22:59:11 +0000424 ObjTy =
David Blaikie6d4fe152013-02-25 01:07:08 +0000425 DBuilder.createStructType(TheCU, "objc_object", getOrCreateMainFile(),
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000426 0, 0, 0, 0, nullptr, llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +0000427
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +0000428 DBuilder.replaceArrays(
429 ObjTy,
430 DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
431 ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, 0, ISATy)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000432 return ObjTy;
433 }
434 case BuiltinType::ObjCSel: {
David Blaikief427b002014-05-06 03:42:01 +0000435 if (!SelTy)
436 SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
437 "objc_selector", TheCU,
438 getOrCreateMainFile(), 0);
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 return SelTy;
440 }
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000441
442 case BuiltinType::OCLImage1d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000443 return getOrCreateStructPtrType("opencl_image1d_t", OCLImage1dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000444 case BuiltinType::OCLImage1dArray:
Eric Christopherb2a008c2013-05-16 00:45:12 +0000445 return getOrCreateStructPtrType("opencl_image1d_array_t",
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000446 OCLImage1dArrayDITy);
447 case BuiltinType::OCLImage1dBuffer:
448 return getOrCreateStructPtrType("opencl_image1d_buffer_t",
449 OCLImage1dBufferDITy);
450 case BuiltinType::OCLImage2d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000451 return getOrCreateStructPtrType("opencl_image2d_t", OCLImage2dDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000452 case BuiltinType::OCLImage2dArray:
453 return getOrCreateStructPtrType("opencl_image2d_array_t",
454 OCLImage2dArrayDITy);
455 case BuiltinType::OCLImage3d:
Eric Christophere7b87e52014-10-26 23:40:33 +0000456 return getOrCreateStructPtrType("opencl_image3d_t", OCLImage3dDITy);
Guy Benyei61054192013-02-07 10:55:47 +0000457 case BuiltinType::OCLSampler:
Eric Christophere7b87e52014-10-26 23:40:33 +0000458 return DBuilder.createBasicType(
459 "opencl_sampler_t", CGM.getContext().getTypeSize(BT),
460 CGM.getContext().getTypeAlign(BT), llvm::dwarf::DW_ATE_unsigned);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +0000461 case BuiltinType::OCLEvent:
Eric Christophere7b87e52014-10-26 23:40:33 +0000462 return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000463
Guy Benyei11169dd2012-12-18 14:30:41 +0000464 case BuiltinType::UChar:
Eric Christophere7b87e52014-10-26 23:40:33 +0000465 case BuiltinType::Char_U:
466 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
467 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000468 case BuiltinType::Char_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000469 case BuiltinType::SChar:
470 Encoding = llvm::dwarf::DW_ATE_signed_char;
471 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000472 case BuiltinType::Char16:
Eric Christophere7b87e52014-10-26 23:40:33 +0000473 case BuiltinType::Char32:
474 Encoding = llvm::dwarf::DW_ATE_UTF;
475 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000476 case BuiltinType::UShort:
477 case BuiltinType::UInt:
478 case BuiltinType::UInt128:
479 case BuiltinType::ULong:
480 case BuiltinType::WChar_U:
Eric Christophere7b87e52014-10-26 23:40:33 +0000481 case BuiltinType::ULongLong:
482 Encoding = llvm::dwarf::DW_ATE_unsigned;
483 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000484 case BuiltinType::Short:
485 case BuiltinType::Int:
486 case BuiltinType::Int128:
487 case BuiltinType::Long:
488 case BuiltinType::WChar_S:
Eric Christophere7b87e52014-10-26 23:40:33 +0000489 case BuiltinType::LongLong:
490 Encoding = llvm::dwarf::DW_ATE_signed;
491 break;
492 case BuiltinType::Bool:
493 Encoding = llvm::dwarf::DW_ATE_boolean;
494 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000495 case BuiltinType::Half:
496 case BuiltinType::Float:
497 case BuiltinType::LongDouble:
Eric Christophere7b87e52014-10-26 23:40:33 +0000498 case BuiltinType::Double:
499 Encoding = llvm::dwarf::DW_ATE_float;
500 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000501 }
502
503 switch (BT->getKind()) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000504 case BuiltinType::Long:
505 BTName = "long int";
506 break;
507 case BuiltinType::LongLong:
508 BTName = "long long int";
509 break;
510 case BuiltinType::ULong:
511 BTName = "long unsigned int";
512 break;
513 case BuiltinType::ULongLong:
514 BTName = "long long unsigned int";
515 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000516 default:
517 BTName = BT->getName(CGM.getLangOpts());
518 break;
519 }
520 // Bit size, align and offset of the type.
521 uint64_t Size = CGM.getContext().getTypeSize(BT);
522 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000523 return DBuilder.createBasicType(BTName, Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000524}
525
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000526llvm::MDType *CGDebugInfo::CreateType(const ComplexType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000527 // Bit size, align and offset of the type.
Ed Masteda706022014-05-07 12:49:30 +0000528 llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float;
Guy Benyei11169dd2012-12-18 14:30:41 +0000529 if (Ty->isComplexIntegerType())
530 Encoding = llvm::dwarf::DW_ATE_lo_user;
531
532 uint64_t Size = CGM.getContext().getTypeSize(Ty);
533 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000534 return DBuilder.createBasicType("complex", Size, Align, Encoding);
Guy Benyei11169dd2012-12-18 14:30:41 +0000535}
536
537/// CreateCVRType - Get the qualified type from the cache or create
538/// a new one if necessary.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000539llvm::MDType *CGDebugInfo::CreateQualifiedType(QualType Ty,
540 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000541 QualifierCollector Qc;
542 const Type *T = Qc.strip(Ty);
543
544 // Ignore these qualifiers for now.
545 Qc.removeObjCGCAttr();
546 Qc.removeAddressSpace();
547 Qc.removeObjCLifetime();
548
549 // We will create one Derived type for one qualifier and recurse to handle any
550 // additional ones.
Ed Masteda706022014-05-07 12:49:30 +0000551 llvm::dwarf::Tag Tag;
Guy Benyei11169dd2012-12-18 14:30:41 +0000552 if (Qc.hasConst()) {
553 Tag = llvm::dwarf::DW_TAG_const_type;
554 Qc.removeConst();
555 } else if (Qc.hasVolatile()) {
556 Tag = llvm::dwarf::DW_TAG_volatile_type;
557 Qc.removeVolatile();
558 } else if (Qc.hasRestrict()) {
559 Tag = llvm::dwarf::DW_TAG_restrict_type;
560 Qc.removeRestrict();
561 } else {
562 assert(Qc.empty() && "Unknown type qualifier for debug info");
563 return getOrCreateType(QualType(T, 0), Unit);
564 }
565
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000566 auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000567
568 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
569 // CVR derived types.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000570 return DBuilder.createQualifiedType(Tag, FromTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000571}
572
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000573llvm::MDType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000574 llvm::MDFile *Unit) {
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000575
576 // The frontend treats 'id' as a typedef to an ObjCObjectType,
577 // whereas 'id<protocol>' is treated as an ObjCPointerType. For the
578 // debug info, we want to emit 'id' in both cases.
579 if (Ty->isObjCQualifiedIdType())
Eric Christophere7b87e52014-10-26 23:40:33 +0000580 return getOrCreateType(CGM.getContext().getObjCIdType(), Unit);
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000581
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000582 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
583 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000584}
585
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000586llvm::MDType *CGDebugInfo::CreateType(const PointerType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000587 llvm::MDFile *Unit) {
Eric Christopherb2a008c2013-05-16 00:45:12 +0000588 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
Guy Benyei11169dd2012-12-18 14:30:41 +0000589 Ty->getPointeeType(), Unit);
590}
591
Manman Rene0064d82013-08-29 23:19:58 +0000592/// In C++ mode, types have linkage, so we can rely on the ODR and
593/// on their mangled names, if they're external.
Eric Christophere7b87e52014-10-26 23:40:33 +0000594static SmallString<256> getUniqueTagTypeName(const TagType *Ty,
595 CodeGenModule &CGM,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000596 llvm::MDCompileUnit *TheCU) {
Manman Rene0064d82013-08-29 23:19:58 +0000597 SmallString<256> FullName;
598 // FIXME: ODR should apply to ObjC++ exactly the same wasy it does to C++.
599 // For now, only apply ODR with C++.
600 const TagDecl *TD = Ty->getDecl();
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +0000601 if (TheCU->getSourceLanguage() != llvm::dwarf::DW_LANG_C_plus_plus ||
Manman Rene0064d82013-08-29 23:19:58 +0000602 !TD->isExternallyVisible())
603 return FullName;
604 // Microsoft Mangler does not have support for mangleCXXRTTIName yet.
605 if (CGM.getTarget().getCXXABI().isMicrosoft())
606 return FullName;
607
608 // TODO: This is using the RTTI name. Is there a better way to get
609 // a unique string for a type?
610 llvm::raw_svector_ostream Out(FullName);
611 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out);
612 Out.flush();
613 return FullName;
614}
615
Adrian Prantl5f66bae2015-02-11 17:45:15 +0000616static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) {
617 llvm::dwarf::Tag Tag;
618 if (RD->isStruct() || RD->isInterface())
619 Tag = llvm::dwarf::DW_TAG_structure_type;
620 else if (RD->isUnion())
621 Tag = llvm::dwarf::DW_TAG_union_type;
622 else {
623 // FIXME: This could be a struct type giving a default visibility different
624 // than C++ class type, but needs llvm metadata changes first.
625 assert(RD->isClass());
626 Tag = llvm::dwarf::DW_TAG_class_type;
627 }
628 return Tag;
629}
630
Guy Benyei11169dd2012-12-18 14:30:41 +0000631// Creates a forward declaration for a RecordDecl in the given context.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000632llvm::MDCompositeType *
Manman Ren1b457022013-08-28 21:20:28 +0000633CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000634 llvm::MDScope *Ctx) {
Manman Ren1b457022013-08-28 21:20:28 +0000635 const RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000636 if (llvm::MDType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
637 return cast<llvm::MDCompositeType>(T);
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000638 llvm::MDFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +0000639 unsigned Line = getLineNumber(RD->getLocation());
640 StringRef RDName = getClassName(RD);
641
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000642 uint64_t Size = 0;
643 uint64_t Align = 0;
644
645 const RecordDecl *D = RD->getDefinition();
646 if (D && D->isCompleteDefinition()) {
647 Size = CGM.getContext().getTypeSize(Ty);
648 Align = CGM.getContext().getTypeAlign(Ty);
649 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000650
651 // Create the type.
Manman Rene0064d82013-08-29 23:19:58 +0000652 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000653 llvm::MDCompositeType *RetTy = DBuilder.createReplaceableCompositeType(
Peter Collingbourned251b0a2015-03-01 22:07:04 +0000654 getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000655 llvm::DebugNode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000656 ReplaceMap.emplace_back(
657 std::piecewise_construct, std::make_tuple(Ty),
658 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +0000659 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +0000660}
661
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000662llvm::MDType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag,
663 const Type *Ty,
664 QualType PointeeTy,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000665 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000666 if (Tag == llvm::dwarf::DW_TAG_reference_type ||
667 Tag == llvm::dwarf::DW_TAG_rvalue_reference_type)
David Blaikie99dab3b2013-09-04 22:03:57 +0000668 return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit));
Fariborz Jahanian65f1fa12013-02-21 20:42:11 +0000669
Guy Benyei11169dd2012-12-18 14:30:41 +0000670 // Bit size, align and offset of the type.
671 // Size is always the size of a pointer. We can't use getTypeSize here
672 // because that does not return the correct value for references.
673 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +0000674 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
Guy Benyei11169dd2012-12-18 14:30:41 +0000675 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
676
David Blaikie99dab3b2013-09-04 22:03:57 +0000677 return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
678 Align);
Guy Benyei11169dd2012-12-18 14:30:41 +0000679}
680
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000681llvm::MDType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
682 llvm::MDType *&Cache) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000683 if (Cache)
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000684 return Cache;
David Blaikiefefc7f72013-05-21 17:58:54 +0000685 Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
686 TheCU, getOrCreateMainFile(), 0);
687 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
688 Cache = DBuilder.createPointerType(Cache, Size);
689 return Cache;
Guy Benyeid8a08ea2012-12-18 14:38:23 +0000690}
691
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000692llvm::MDType *CGDebugInfo::CreateType(const BlockPointerType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000693 llvm::MDFile *Unit) {
Eric Christopherf8bc4d82013-07-18 00:52:50 +0000694 if (BlockLiteralGeneric)
Guy Benyei11169dd2012-12-18 14:30:41 +0000695 return BlockLiteralGeneric;
696
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000697 SmallVector<llvm::Metadata *, 8> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000698 QualType FType;
699 uint64_t FieldSize, FieldOffset;
700 unsigned FieldAlign;
701 llvm::DIArray Elements;
Guy Benyei11169dd2012-12-18 14:30:41 +0000702
703 FieldOffset = 0;
704 FType = CGM.getContext().UnsignedLongTy;
705 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
706 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
707
708 Elements = DBuilder.getOrCreateArray(EltTys);
709 EltTys.clear();
710
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000711 unsigned Flags = llvm::DebugNode::FlagAppleBlock;
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 unsigned LineNo = getLineNumber(CurLoc);
713
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000714 auto *EltTy =
715 DBuilder.createStructType(Unit, "__block_descriptor", Unit, LineNo,
716 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000717
718 // Bit size, align and offset of the type.
719 uint64_t Size = CGM.getContext().getTypeSize(Ty);
720
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000721 auto *DescTy = DBuilder.createPointerType(EltTy, Size);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722
723 FieldOffset = 0;
724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
725 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
726 FType = CGM.getContext().IntTy;
727 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
728 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Adrian Prantl65d5d002014-11-05 01:01:30 +0000729 FType = CGM.getContext().getPointerType(Ty->getPointeeType());
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
731
732 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Guy Benyei11169dd2012-12-18 14:30:41 +0000733 FieldSize = CGM.getContext().getTypeSize(Ty);
734 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000735 EltTys.push_back(DBuilder.createMemberType(Unit, "__descriptor", Unit, LineNo,
736 FieldSize, FieldAlign, FieldOffset,
737 0, DescTy));
Guy Benyei11169dd2012-12-18 14:30:41 +0000738
739 FieldOffset += FieldSize;
740 Elements = DBuilder.getOrCreateArray(EltTys);
741
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000742 EltTy =
743 DBuilder.createStructType(Unit, "__block_literal_generic", Unit, LineNo,
744 FieldOffset, 0, Flags, nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745
Guy Benyei11169dd2012-12-18 14:30:41 +0000746 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
747 return BlockLiteralGeneric;
748}
749
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000750llvm::MDType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000751 llvm::MDFile *Unit) {
David Blaikief1b382e2014-04-06 17:14:06 +0000752 assert(Ty->isTypeAlias());
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000753 llvm::MDType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
David Blaikief1b382e2014-04-06 17:14:06 +0000754
755 SmallString<128> NS;
756 llvm::raw_svector_ostream OS(NS);
Eric Christophere7b87e52014-10-26 23:40:33 +0000757 Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(),
758 /*qualified*/ false);
David Blaikief1b382e2014-04-06 17:14:06 +0000759
760 TemplateSpecializationType::PrintTemplateArgumentList(
761 OS, Ty->getArgs(), Ty->getNumArgs(),
762 CGM.getContext().getPrintingPolicy());
763
Eric Christophere7b87e52014-10-26 23:40:33 +0000764 TypeAliasDecl *AliasDecl = cast<TypeAliasTemplateDecl>(
765 Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl();
David Blaikief1b382e2014-04-06 17:14:06 +0000766
767 SourceLocation Loc = AliasDecl->getLocation();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000768 return DBuilder.createTypedef(
769 Src, internString(OS.str()), getOrCreateFile(Loc), getLineNumber(Loc),
770 getContextDescriptor(cast<Decl>(AliasDecl->getDeclContext())));
David Blaikief1b382e2014-04-06 17:14:06 +0000771}
772
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000773llvm::MDType *CGDebugInfo::CreateType(const TypedefType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000774 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000775 // We don't set size information, but do specify where the typedef was
776 // declared.
Adrian Prantl3eff2252014-01-21 18:42:27 +0000777 SourceLocation Loc = Ty->getDecl()->getLocation();
Eric Christopherb2a008c2013-05-16 00:45:12 +0000778
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +0000779 // Typedefs are derived from some other type.
780 return DBuilder.createTypedef(
781 getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
782 Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
783 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
Guy Benyei11169dd2012-12-18 14:30:41 +0000784}
785
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000786llvm::MDType *CGDebugInfo::CreateType(const FunctionType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000787 llvm::MDFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000788 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +0000789
790 // Add the result type at least.
Alp Toker314cc812014-01-25 16:55:45 +0000791 EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +0000792
793 // Set up remainder of arguments if there is a prototype.
Adrian Prantl800faef2014-02-25 23:42:18 +0000794 // otherwise emit it as a variadic function.
Guy Benyei11169dd2012-12-18 14:30:41 +0000795 if (isa<FunctionNoProtoType>(Ty))
796 EltTys.push_back(DBuilder.createUnspecifiedParameter());
797 else if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
Alp Toker9cacbab2014-01-20 20:26:09 +0000798 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
799 EltTys.push_back(getOrCreateType(FPT->getParamType(i), Unit));
Adrian Prantld45ba252014-02-25 19:38:11 +0000800 if (FPT->isVariadic())
801 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +0000802 }
803
Manman Ren67f005e2014-07-28 22:24:34 +0000804 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 return DBuilder.createSubroutineType(Unit, EltTypeArray);
806}
807
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +0000808/// Convert an AccessSpecifier into the corresponding DebugNode flag.
Adrian Prantl21361fb2014-08-29 22:44:27 +0000809/// As an optimization, return 0 if the access specifier equals the
810/// default for the containing type.
811static unsigned getAccessFlag(AccessSpecifier Access, const RecordDecl *RD) {
812 AccessSpecifier Default = clang::AS_none;
813 if (RD && RD->isClass())
814 Default = clang::AS_private;
815 else if (RD && (RD->isStruct() || RD->isUnion()))
816 Default = clang::AS_public;
817
818 if (Access == Default)
819 return 0;
820
Eric Christophere7b87e52014-10-26 23:40:33 +0000821 switch (Access) {
822 case clang::AS_private:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000823 return llvm::DebugNode::FlagPrivate;
Eric Christophere7b87e52014-10-26 23:40:33 +0000824 case clang::AS_protected:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000825 return llvm::DebugNode::FlagProtected;
Eric Christophere7b87e52014-10-26 23:40:33 +0000826 case clang::AS_public:
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +0000827 return llvm::DebugNode::FlagPublic;
Eric Christophere7b87e52014-10-26 23:40:33 +0000828 case clang::AS_none:
829 return 0;
Adrian Prantl21361fb2014-08-29 22:44:27 +0000830 }
831 llvm_unreachable("unexpected access enumerator");
832}
Guy Benyei11169dd2012-12-18 14:30:41 +0000833
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000834llvm::MDType *CGDebugInfo::createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000835 StringRef name, QualType type, uint64_t sizeInBitsOverride,
836 SourceLocation loc, AccessSpecifier AS, uint64_t offsetInBits,
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +0000837 llvm::MDFile *tunit, llvm::MDScope *scope, const RecordDecl *RD) {
838 llvm::MDType *debugType = getOrCreateType(type, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +0000839
840 // Get the location for the field.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +0000841 llvm::MDFile *file = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +0000842 unsigned line = getLineNumber(loc);
843
David Majnemer34b57492014-07-30 01:30:47 +0000844 uint64_t SizeInBits = 0;
845 unsigned AlignInBits = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000846 if (!type->isIncompleteArrayType()) {
David Majnemer34b57492014-07-30 01:30:47 +0000847 TypeInfo TI = CGM.getContext().getTypeInfo(type);
848 SizeInBits = TI.Width;
849 AlignInBits = TI.Align;
Guy Benyei11169dd2012-12-18 14:30:41 +0000850
851 if (sizeInBitsOverride)
David Majnemer34b57492014-07-30 01:30:47 +0000852 SizeInBits = sizeInBitsOverride;
Guy Benyei11169dd2012-12-18 14:30:41 +0000853 }
854
Adrian Prantl21361fb2014-08-29 22:44:27 +0000855 unsigned flags = getAccessFlag(AS, RD);
David Majnemer34b57492014-07-30 01:30:47 +0000856 return DBuilder.createMemberType(scope, name, file, line, SizeInBits,
857 AlignInBits, offsetInBits, flags, debugType);
Guy Benyei11169dd2012-12-18 14:30:41 +0000858}
859
Eric Christopher91a31902013-01-16 01:22:32 +0000860/// CollectRecordLambdaFields - Helper for CollectRecordFields.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000861void CGDebugInfo::CollectRecordLambdaFields(
862 const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000863 llvm::MDType *RecordTy) {
Eric Christopher91a31902013-01-16 01:22:32 +0000864 // For C++11 Lambdas a Field will be the same as a Capture, but the Capture
865 // has the name and the location of the variable so we should iterate over
866 // both concurrently.
867 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl);
868 RecordDecl::field_iterator Field = CXXDecl->field_begin();
869 unsigned fieldno = 0;
870 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
Eric Christophere7b87e52014-10-26 23:40:33 +0000871 E = CXXDecl->captures_end();
872 I != E; ++I, ++Field, ++fieldno) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000873 const LambdaCapture &C = *I;
Eric Christopher91a31902013-01-16 01:22:32 +0000874 if (C.capturesVariable()) {
875 VarDecl *V = C.getCapturedVar();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000876 llvm::MDFile *VUnit = getOrCreateFile(C.getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000877 StringRef VName = V->getName();
878 uint64_t SizeInBitsOverride = 0;
879 if (Field->isBitField()) {
880 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
881 assert(SizeInBitsOverride && "found named 0-width bitfield");
882 }
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000883 llvm::MDType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000884 VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
885 Field->getAccess(), layout.getFieldOffset(fieldno), VUnit, RecordTy,
886 CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000887 elements.push_back(fieldType);
Alexey Bataev39c81e22014-08-28 04:28:19 +0000888 } else if (C.capturesThis()) {
Eric Christopher91a31902013-01-16 01:22:32 +0000889 // TODO: Need to handle 'this' in some way by probably renaming the
890 // this of the lambda class and having a field member of 'this' or
891 // by using AT_object_pointer for the function and having that be
892 // used as 'this' for semantic references.
Eric Christopher91a31902013-01-16 01:22:32 +0000893 FieldDecl *f = *Field;
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000894 llvm::MDFile *VUnit = getOrCreateFile(f->getLocation());
Eric Christopher91a31902013-01-16 01:22:32 +0000895 QualType type = f->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000896 llvm::MDType *fieldType = createFieldType(
Eric Christophere7b87e52014-10-26 23:40:33 +0000897 "this", type, 0, f->getLocation(), f->getAccess(),
898 layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
Eric Christopher91a31902013-01-16 01:22:32 +0000899
900 elements.push_back(fieldType);
901 }
902 }
903}
904
David Blaikie6943dea2013-08-20 01:28:15 +0000905/// Helper for CollectRecordFields.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000906llvm::MDDerivedType *
907CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::MDType *RecordTy,
908 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000909 // Create the descriptor for the static variable, with or without
910 // constant initializers.
David Blaikie8e707bb2014-10-14 22:22:17 +0000911 Var = Var->getCanonicalDecl();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000912 llvm::MDFile *VUnit = getOrCreateFile(Var->getLocation());
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000913 llvm::MDType *VTy = getOrCreateType(Var->getType(), VUnit);
Eric Christopher91a31902013-01-16 01:22:32 +0000914
Eric Christopher91a31902013-01-16 01:22:32 +0000915 unsigned LineNumber = getLineNumber(Var->getLocation());
916 StringRef VName = Var->getName();
Craig Topper8a13c412014-05-21 05:09:00 +0000917 llvm::Constant *C = nullptr;
Eric Christopher91a31902013-01-16 01:22:32 +0000918 if (Var->getInit()) {
919 const APValue *Value = Var->evaluateValue();
David Blaikied42917f2013-01-20 01:19:17 +0000920 if (Value) {
921 if (Value->isInt())
922 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
923 if (Value->isFloat())
924 C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
925 }
Eric Christopher91a31902013-01-16 01:22:32 +0000926 }
927
Adrian Prantl21361fb2014-08-29 22:44:27 +0000928 unsigned Flags = getAccessFlag(Var->getAccess(), RD);
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000929 llvm::MDDerivedType *GV = DBuilder.createStaticMemberType(
David Blaikieae019462013-08-15 22:50:29 +0000930 RecordTy, VName, VUnit, LineNumber, VTy, Flags, C);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000931 StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
David Blaikieae019462013-08-15 22:50:29 +0000932 return GV;
Eric Christopher91a31902013-01-16 01:22:32 +0000933}
934
935/// CollectRecordNormalField - Helper for CollectRecordFields.
Eric Christophere7b87e52014-10-26 23:40:33 +0000936void CGDebugInfo::CollectRecordNormalField(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000937 const FieldDecl *field, uint64_t OffsetInBits, llvm::MDFile *tunit,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000938 SmallVectorImpl<llvm::Metadata *> &elements, llvm::MDType *RecordTy,
Eric Christophere7b87e52014-10-26 23:40:33 +0000939 const RecordDecl *RD) {
Eric Christopher91a31902013-01-16 01:22:32 +0000940 StringRef name = field->getName();
941 QualType type = field->getType();
942
943 // Ignore unnamed fields unless they're anonymous structs/unions.
944 if (name.empty() && !type->isRecordType())
945 return;
946
947 uint64_t SizeInBitsOverride = 0;
948 if (field->isBitField()) {
949 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
950 assert(SizeInBitsOverride && "found named 0-width bitfield");
951 }
952
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +0000953 llvm::MDType *fieldType =
Eric Christophere7b87e52014-10-26 23:40:33 +0000954 createFieldType(name, type, SizeInBitsOverride, field->getLocation(),
955 field->getAccess(), OffsetInBits, tunit, RecordTy, RD);
Eric Christopher91a31902013-01-16 01:22:32 +0000956
957 elements.push_back(fieldType);
958}
959
Guy Benyei11169dd2012-12-18 14:30:41 +0000960/// CollectRecordFields - A helper function to collect debug info for
961/// record fields. This is used while creating debug info entry for a Record.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000962void CGDebugInfo::CollectRecordFields(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +0000963 const RecordDecl *record, llvm::MDFile *tunit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000964 SmallVectorImpl<llvm::Metadata *> &elements,
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +0000965 llvm::MDCompositeType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
967
Eric Christopher91a31902013-01-16 01:22:32 +0000968 if (CXXDecl && CXXDecl->isLambda())
969 CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
970 else {
971 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Guy Benyei11169dd2012-12-18 14:30:41 +0000972
Eric Christopher91a31902013-01-16 01:22:32 +0000973 // Field number for non-static fields.
Eric Christopher0f7594372013-01-04 17:59:07 +0000974 unsigned fieldNo = 0;
Eric Christopher91a31902013-01-16 01:22:32 +0000975
Eric Christopher91a31902013-01-16 01:22:32 +0000976 // Static and non-static members should appear in the same order as
977 // the corresponding declarations in the source program.
Aaron Ballman629afae2014-03-07 19:56:05 +0000978 for (const auto *I : record->decls())
979 if (const auto *V = dyn_cast<VarDecl>(I)) {
David Blaikiece763042013-08-20 21:49:21 +0000980 // Reuse the existing static member declaration if one exists
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000981 auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
David Blaikiece763042013-08-20 21:49:21 +0000982 if (MI != StaticDataMemberCache.end()) {
983 assert(MI->second &&
984 "Static data member declaration should still exist");
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +0000985 elements.push_back(cast<llvm::MDDerivedTypeBase>(MI->second));
Adrian Prantl21361fb2014-08-29 22:44:27 +0000986 } else {
987 auto Field = CreateRecordStaticField(V, RecordTy, record);
988 elements.push_back(Field);
989 }
Aaron Ballman629afae2014-03-07 19:56:05 +0000990 } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
Eric Christophere7b87e52014-10-26 23:40:33 +0000991 CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
992 elements, RecordTy, record);
Eric Christopher91a31902013-01-16 01:22:32 +0000993
994 // Bump field number for next field.
995 ++fieldNo;
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000997 }
998}
999
1000/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
1001/// function type is not updated to include implicit "this" pointer. Use this
1002/// routine to get a method type which includes "this" pointer.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001003llvm::MDSubroutineType *
Guy Benyei11169dd2012-12-18 14:30:41 +00001004CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001005 llvm::MDFile *Unit) {
David Blaikie7eb06852013-01-07 23:06:35 +00001006 const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>();
David Blaikie2aaf0652013-01-07 22:24:59 +00001007 if (Method->isStatic())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001008 return cast_or_null<llvm::MDSubroutineType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001009 getOrCreateType(QualType(Func, 0), Unit));
David Blaikie7eb06852013-01-07 23:06:35 +00001010 return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()),
1011 Func, Unit);
1012}
David Blaikie2aaf0652013-01-07 22:24:59 +00001013
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001014llvm::MDSubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001015 QualType ThisPtr, const FunctionProtoType *Func, llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001016 // Add "this" pointer.
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00001017 llvm::DITypeArray Args(
1018 cast<llvm::MDSubroutineType>(getOrCreateType(QualType(Func, 0), Unit))
1019 ->getTypeArray());
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001020 assert(Args.size() && "Invalid number of arguments!");
Guy Benyei11169dd2012-12-18 14:30:41 +00001021
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001022 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001023
1024 // First element is always return type. For 'void' functions it is NULL.
Duncan P. N. Exon Smith37328582015-04-07 18:41:26 +00001025 Elts.push_back(Args[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001026
David Blaikie2aaf0652013-01-07 22:24:59 +00001027 // "this" pointer is always first argument.
David Blaikie7eb06852013-01-07 23:06:35 +00001028 const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
David Blaikie2aaf0652013-01-07 22:24:59 +00001029 if (isa<ClassTemplateSpecializationDecl>(RD)) {
1030 // Create pointer type directly in this case.
1031 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1032 QualType PointeeTy = ThisPtrTy->getPointeeType();
1033 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
John McCallc8e01702013-04-16 22:48:15 +00001034 uint64_t Size = CGM.getTarget().getPointerWidth(AS);
David Blaikie2aaf0652013-01-07 22:24:59 +00001035 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001036 llvm::MDType *PointeeType = getOrCreateType(PointeeTy, Unit);
1037 llvm::MDType *ThisPtrType =
Eric Christophere7b87e52014-10-26 23:40:33 +00001038 DBuilder.createPointerType(PointeeType, Size, Align);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001039 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001040 // TODO: This and the artificial type below are misleading, the
1041 // types aren't artificial the argument is, but the current
1042 // metadata doesn't represent that.
1043 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1044 Elts.push_back(ThisPtrType);
1045 } else {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001046 llvm::MDType *ThisPtrType = getOrCreateType(ThisPtr, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001047 TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
David Blaikie2aaf0652013-01-07 22:24:59 +00001048 ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType);
1049 Elts.push_back(ThisPtrType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 }
1051
1052 // Copy rest of the arguments.
Duncan P. N. Exon Smitha98fac62015-04-07 04:14:45 +00001053 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1054 Elts.push_back(Args[i]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001055
Manman Ren67f005e2014-07-28 22:24:34 +00001056 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00001057
Adrian Prantl0630eb72013-12-18 21:48:18 +00001058 unsigned Flags = 0;
1059 if (Func->getExtProtoInfo().RefQualifier == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001060 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001061 if (Func->getExtProtoInfo().RefQualifier == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001062 Flags |= llvm::DebugNode::FlagRValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001063
1064 return DBuilder.createSubroutineType(Unit, EltTypeArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001065}
1066
Eric Christopherb2a008c2013-05-16 00:45:12 +00001067/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
Guy Benyei11169dd2012-12-18 14:30:41 +00001068/// inside a function.
1069static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
1070 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
1071 return isFunctionLocalClass(NRD);
1072 if (isa<FunctionDecl>(RD->getDeclContext()))
1073 return true;
1074 return false;
1075}
1076
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001077/// CreateCXXMemberFunction - A helper function to create a subprogram for
Guy Benyei11169dd2012-12-18 14:30:41 +00001078/// a single member function GlobalDecl.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001079llvm::MDSubprogram *CGDebugInfo::CreateCXXMemberFunction(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001080 const CXXMethodDecl *Method, llvm::MDFile *Unit, llvm::MDType *RecordTy) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001081 bool IsCtorOrDtor =
Eric Christophere7b87e52014-10-26 23:40:33 +00001082 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
Eric Christopherb2a008c2013-05-16 00:45:12 +00001083
Guy Benyei11169dd2012-12-18 14:30:41 +00001084 StringRef MethodName = getFunctionName(Method);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001085 llvm::MDSubroutineType *MethodTy = getOrCreateMethodType(Method, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001086
1087 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
1088 // make sense to give a single ctor/dtor a linkage name.
1089 StringRef MethodLinkageName;
1090 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
1091 MethodLinkageName = CGM.getMangledName(Method);
1092
1093 // Get the location for the method.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001094 llvm::MDFile *MethodDefUnit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00001095 unsigned MethodLine = 0;
1096 if (!Method->isImplicit()) {
1097 MethodDefUnit = getOrCreateFile(Method->getLocation());
1098 MethodLine = getLineNumber(Method->getLocation());
1099 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001100
1101 // Collect virtual method info.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001102 llvm::MDType *ContainingType = nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001103 unsigned Virtuality = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 unsigned VIndex = 0;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001105
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 if (Method->isVirtual()) {
1107 if (Method->isPure())
1108 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
1109 else
1110 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001111
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 // It doesn't make sense to give a virtual destructor a vtable index,
1113 // since a single destructor has two entries in the vtable.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001114 // FIXME: Add proper support for debug info for virtual calls in
1115 // the Microsoft ABI, where we may use multiple vptrs to make a vftable
1116 // lookup if we have multiple or virtual inheritance.
1117 if (!isa<CXXDestructorDecl>(Method) &&
1118 !CGM.getTarget().getCXXABI().isMicrosoft())
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001119 VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method);
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 ContainingType = RecordTy;
1121 }
1122
1123 unsigned Flags = 0;
1124 if (Method->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001125 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001126 Flags |= getAccessFlag(Method->getAccess(), Method->getParent());
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
1128 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001129 Flags |= llvm::DebugNode::FlagExplicit;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001130 } else if (const CXXConversionDecl *CXXC =
Eric Christophere7b87e52014-10-26 23:40:33 +00001131 dyn_cast<CXXConversionDecl>(Method)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001132 if (CXXC->isExplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001133 Flags |= llvm::DebugNode::FlagExplicit;
Guy Benyei11169dd2012-12-18 14:30:41 +00001134 }
1135 if (Method->hasPrototype())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001136 Flags |= llvm::DebugNode::FlagPrototyped;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001137 if (Method->getRefQualifier() == RQ_LValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001138 Flags |= llvm::DebugNode::FlagLValueReference;
Adrian Prantl0630eb72013-12-18 21:48:18 +00001139 if (Method->getRefQualifier() == RQ_RValue)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001140 Flags |= llvm::DebugNode::FlagRValueReference;
Guy Benyei11169dd2012-12-18 14:30:41 +00001141
1142 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001143 llvm::MDSubprogram *SP = DBuilder.createMethod(
Eric Christophere7b87e52014-10-26 23:40:33 +00001144 RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
1145 MethodTy, /*isLocalToUnit=*/false,
1146 /* isDefinition=*/false, Virtuality, VIndex, ContainingType, Flags,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00001147 CGM.getLangOpts().Optimize, nullptr, TParamsArray.get());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001148
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001149 SPCache[Method->getCanonicalDecl()].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00001150
1151 return SP;
1152}
1153
1154/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001155/// C++ member functions. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001156/// a Record.
Eric Christophere7b87e52014-10-26 23:40:33 +00001157void CGDebugInfo::CollectCXXMemberFunctions(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001158 const CXXRecordDecl *RD, llvm::MDFile *Unit,
1159 SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::MDType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001160
1161 // Since we want more than just the individual member decls if we
1162 // have templated functions iterate over every declaration to gather
1163 // the functions.
Eric Christophere7b87e52014-10-26 23:40:33 +00001164 for (const auto *I : RD->decls()) {
David Blaikiefd580722014-10-06 05:18:55 +00001165 const auto *Method = dyn_cast<CXXMethodDecl>(I);
1166 // If the member is implicit, don't add it to the member list. This avoids
1167 // the member being added to type units by LLVM, while still allowing it
1168 // to be emitted into the type declaration/reference inside the compile
1169 // unit.
David Blaikie6dddfe32014-10-06 05:52:27 +00001170 // FIXME: Handle Using(Shadow?)Decls here to create
1171 // DW_TAG_imported_declarations inside the class for base decls brought into
1172 // derived classes. GDB doesn't seem to notice/leverage these when I tried
1173 // it, so I'm not rushing to fix this. (GCC seems to produce them, if
1174 // referenced)
David Blaikiefd580722014-10-06 05:18:55 +00001175 if (!Method || Method->isImplicit())
1176 continue;
David Blaikie42edade2014-11-11 20:44:45 +00001177
1178 if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType())
1179 continue;
1180
David Blaikiefd580722014-10-06 05:18:55 +00001181 // Reuse the existing member function declaration if it exists.
1182 // It may be associated with the declaration of the type & should be
1183 // reused as we're building the definition.
1184 //
1185 // This situation can arise in the vtable-based debug info reduction where
1186 // implicit members are emitted in a non-vtable TU.
1187 auto MI = SPCache.find(Method->getCanonicalDecl());
1188 EltTys.push_back(MI == SPCache.end()
1189 ? CreateCXXMemberFunction(Method, Unit, RecordTy)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001190 : static_cast<llvm::Metadata *>(MI->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00001192}
Guy Benyei11169dd2012-12-18 14:30:41 +00001193
Guy Benyei11169dd2012-12-18 14:30:41 +00001194/// CollectCXXBases - A helper function to collect debug info for
Eric Christopherb2a008c2013-05-16 00:45:12 +00001195/// C++ base classes. This is used while creating debug info entry for
Guy Benyei11169dd2012-12-18 14:30:41 +00001196/// a Record.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001197void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::MDFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001198 SmallVectorImpl<llvm::Metadata *> &EltTys,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001199 llvm::MDType *RecordTy) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001200 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Aaron Ballman574705e2014-03-13 15:41:46 +00001201 for (const auto &BI : RD->bases()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001202 unsigned BFlags = 0;
1203 uint64_t BaseOffset;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001204
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 const CXXRecordDecl *Base =
Eric Christophere7b87e52014-10-26 23:40:33 +00001206 cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00001207
Aaron Ballman574705e2014-03-13 15:41:46 +00001208 if (BI.isVirtual()) {
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001209 if (CGM.getTarget().getCXXABI().isItaniumFamily()) {
1210 // virtual base offset offset is -ve. The code generator emits dwarf
1211 // expression where it expects +ve number.
Eric Christophere7b87e52014-10-26 23:40:33 +00001212 BaseOffset = 0 - CGM.getItaniumVTableContext()
1213 .getVirtualBaseOffsetOffset(RD, Base)
1214 .getQuantity();
Reid Klecknerd3b23d62014-08-07 21:29:25 +00001215 } else {
1216 // In the MS ABI, store the vbtable offset, which is analogous to the
1217 // vbase offset offset in Itanium.
1218 BaseOffset =
1219 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base);
1220 }
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001221 BFlags = llvm::DebugNode::FlagVirtual;
Guy Benyei11169dd2012-12-18 14:30:41 +00001222 } else
1223 BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base));
1224 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1225 // BI->isVirtual() and bits when not.
Eric Christopherb2a008c2013-05-16 00:45:12 +00001226
Adrian Prantl21361fb2014-08-29 22:44:27 +00001227 BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001228 llvm::MDType *DTy = DBuilder.createInheritance(
Eric Christophere7b87e52014-10-26 23:40:33 +00001229 RecordTy, getOrCreateType(BI.getType(), Unit), BaseOffset, BFlags);
Guy Benyei11169dd2012-12-18 14:30:41 +00001230 EltTys.push_back(DTy);
1231 }
1232}
1233
1234/// CollectTemplateParams - A helper function to collect template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001235llvm::DIArray
1236CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList,
1237 ArrayRef<TemplateArgument> TAList,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001238 llvm::MDFile *Unit) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001239 SmallVector<llvm::Metadata *, 16> TemplateParams;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1241 const TemplateArgument &TA = TAList[i];
David Blaikie47c11502013-06-22 18:59:18 +00001242 StringRef Name;
1243 if (TPList)
1244 Name = TPList->getParam(i)->getName();
David Blaikie38079fd2013-05-10 21:53:14 +00001245 switch (TA.getKind()) {
1246 case TemplateArgument::Type: {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001247 llvm::MDType *TTy = getOrCreateType(TA.getAsType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001248 TemplateParams.push_back(
1249 DBuilder.createTemplateTypeParameter(TheCU, Name, TTy));
David Blaikie38079fd2013-05-10 21:53:14 +00001250 } break;
1251 case TemplateArgument::Integral: {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001252 llvm::MDType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001253 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1254 TheCU, Name, TTy,
1255 llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
David Blaikie38079fd2013-05-10 21:53:14 +00001256 } break;
1257 case TemplateArgument::Declaration: {
1258 const ValueDecl *D = TA.getAsDecl();
David Blaikieb5c7e6a2014-10-18 02:21:26 +00001259 QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001260 llvm::MDType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001261 llvm::Constant *V = nullptr;
David Blaikie1a83db42014-10-20 18:56:54 +00001262 const CXXMethodDecl *MD;
David Blaikie38079fd2013-05-10 21:53:14 +00001263 // Variable pointer template parameters have a value that is the address
1264 // of the variable.
David Blaikie952a9b12014-10-17 18:00:12 +00001265 if (const auto *VD = dyn_cast<VarDecl>(D))
David Blaikie38079fd2013-05-10 21:53:14 +00001266 V = CGM.GetAddrOfGlobalVar(VD);
1267 // Member function pointers have special support for building them, though
1268 // this is currently unsupported in LLVM CodeGen.
David Blaikie1a83db42014-10-20 18:56:54 +00001269 else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance())
David Blaikie0a7c9d52014-10-20 20:29:35 +00001270 V = CGM.getCXXABI().EmitMemberPointer(MD);
David Blaikie952a9b12014-10-17 18:00:12 +00001271 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
David Blaikied900f982013-05-13 06:57:50 +00001272 V = CGM.GetAddrOfFunction(FD);
David Blaikie38079fd2013-05-10 21:53:14 +00001273 // Member data pointers have special handling too to compute the fixed
1274 // offset within the object.
David Blaikie952a9b12014-10-17 18:00:12 +00001275 else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) {
David Blaikie38079fd2013-05-10 21:53:14 +00001276 // These five lines (& possibly the above member function pointer
1277 // handling) might be able to be refactored to use similar code in
1278 // CodeGenModule::getMemberPointerConstant
1279 uint64_t fieldOffset = CGM.getContext().getFieldOffset(D);
1280 CharUnits chars =
Eric Christophere7b87e52014-10-26 23:40:33 +00001281 CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
David Blaikie952a9b12014-10-17 18:00:12 +00001282 V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
David Blaikie38079fd2013-05-10 21:53:14 +00001283 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001284 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1285 TheCU, Name, TTy,
1286 cast_or_null<llvm::Constant>(V->stripPointerCasts())));
David Blaikie38079fd2013-05-10 21:53:14 +00001287 } break;
1288 case TemplateArgument::NullPtr: {
1289 QualType T = TA.getNullPtrType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001290 llvm::MDType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001291 llvm::Constant *V = nullptr;
David Blaikie38079fd2013-05-10 21:53:14 +00001292 // Special case member data pointer null values since they're actually -1
1293 // instead of zero.
1294 if (const MemberPointerType *MPT =
1295 dyn_cast<MemberPointerType>(T.getTypePtr()))
1296 // But treat member function pointers as simple zero integers because
1297 // it's easier than having a special case in LLVM's CodeGen. If LLVM
1298 // CodeGen grows handling for values of non-null member function
1299 // pointers then perhaps we could remove this special case and rely on
1300 // EmitNullMemberPointer for member function pointers.
1301 if (MPT->isMemberDataPointer())
1302 V = CGM.getCXXABI().EmitNullMemberPointer(MPT);
1303 if (!V)
1304 V = llvm::ConstantInt::get(CGM.Int8Ty, 0);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001305 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1306 TheCU, Name, TTy, cast<llvm::Constant>(V)));
David Blaikie38079fd2013-05-10 21:53:14 +00001307 } break;
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001308 case TemplateArgument::Template:
1309 TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001310 TheCU, Name, nullptr,
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001311 TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString()));
1312 break;
1313 case TemplateArgument::Pack:
1314 TemplateParams.push_back(DBuilder.createTemplateParameterPack(
1315 TheCU, Name, nullptr,
1316 CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit)));
1317 break;
David Majnemer5559d472013-08-24 08:21:10 +00001318 case TemplateArgument::Expression: {
1319 const Expr *E = TA.getAsExpr();
1320 QualType T = E->getType();
David Majnemer922ad9f2014-10-24 19:49:04 +00001321 if (E->isGLValue())
1322 T = CGM.getContext().getLValueReferenceType(T);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001323 llvm::Constant *V = CGM.EmitConstantExpr(E, T);
David Majnemer5559d472013-08-24 08:21:10 +00001324 assert(V && "Expression in template argument isn't constant");
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001325 llvm::MDType *TTy = getOrCreateType(T, Unit);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001326 TemplateParams.push_back(DBuilder.createTemplateValueParameter(
1327 TheCU, Name, TTy, cast<llvm::Constant>(V->stripPointerCasts())));
David Majnemer5559d472013-08-24 08:21:10 +00001328 } break;
David Blaikie2b93c542013-05-10 23:36:06 +00001329 // And the following should never occur:
David Blaikie38079fd2013-05-10 21:53:14 +00001330 case TemplateArgument::TemplateExpansion:
David Blaikie38079fd2013-05-10 21:53:14 +00001331 case TemplateArgument::Null:
1332 llvm_unreachable(
1333 "These argument types shouldn't exist in concrete types");
Guy Benyei11169dd2012-12-18 14:30:41 +00001334 }
1335 }
1336 return DBuilder.getOrCreateArray(TemplateParams);
1337}
1338
1339/// CollectFunctionTemplateParams - A helper function to collect debug
1340/// info for function template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001341llvm::DIArray CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001342 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001343 if (FD->getTemplatedKind() ==
1344 FunctionDecl::TK_FunctionTemplateSpecialization) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001345 const TemplateParameterList *TList = FD->getTemplateSpecializationInfo()
1346 ->getTemplate()
1347 ->getTemplateParameters();
David Blaikie47c11502013-06-22 18:59:18 +00001348 return CollectTemplateParams(
1349 TList, FD->getTemplateSpecializationArgs()->asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001350 }
1351 return llvm::DIArray();
1352}
1353
1354/// CollectCXXTemplateParams - A helper function to collect debug info for
1355/// template parameters.
Eric Christophere7b87e52014-10-26 23:40:33 +00001356llvm::DIArray CGDebugInfo::CollectCXXTemplateParams(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001357 const ClassTemplateSpecializationDecl *TSpecial, llvm::MDFile *Unit) {
Adrian Prantl649f0302014-04-17 01:04:01 +00001358 // Always get the full list of parameters, not just the ones from
1359 // the specialization.
1360 TemplateParameterList *TPList =
Eric Christophere7b87e52014-10-26 23:40:33 +00001361 TSpecial->getSpecializedTemplate()->getTemplateParameters();
Adrian Prantl2c92e9c2014-04-17 00:30:48 +00001362 const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();
David Blaikie47c11502013-06-22 18:59:18 +00001363 return CollectTemplateParams(TPList, TAList.asArray(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001364}
1365
1366/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001367llvm::MDType *CGDebugInfo::getOrCreateVTablePtrType(llvm::MDFile *Unit) {
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001368 if (VTablePtrType)
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 return VTablePtrType;
1370
1371 ASTContext &Context = CGM.getContext();
1372
1373 /* Function type */
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001374 llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit);
Manman Ren67f005e2014-07-28 22:24:34 +00001375 llvm::DITypeArray SElements = DBuilder.getOrCreateTypeArray(STy);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001376 llvm::MDType *SubTy = DBuilder.createSubroutineType(Unit, SElements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001377 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001378 llvm::MDType *vtbl_ptr_type =
Eric Christophere7b87e52014-10-26 23:40:33 +00001379 DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type");
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
1381 return VTablePtrType;
1382}
1383
1384/// getVTableName - Get vtable name for the given Class.
1385StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Benjamin Kramer1b18a5e2013-09-09 16:39:06 +00001386 // Copy the gdb compatible name on the side and use its reference.
1387 return internString("_vptr$", RD->getNameAsString());
Guy Benyei11169dd2012-12-18 14:30:41 +00001388}
1389
Guy Benyei11169dd2012-12-18 14:30:41 +00001390/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
1391/// debug info entry in EltTys vector.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001392void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::MDFile *Unit,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001393 SmallVectorImpl<llvm::Metadata *> &EltTys) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001394 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1395
1396 // If there is a primary base then it will hold vtable info.
1397 if (RL.getPrimaryBase())
1398 return;
1399
1400 // If this class is not dynamic then there is not any vtable info to collect.
1401 if (!RD->isDynamicClass())
1402 return;
1403
1404 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001405 llvm::MDType *VPTR = DBuilder.createMemberType(
Eric Christophere7b87e52014-10-26 23:40:33 +00001406 Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001407 llvm::DebugNode::FlagArtificial, getOrCreateVTablePtrType(Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001408 EltTys.push_back(VPTR);
1409}
1410
Eric Christopherb2a008c2013-05-16 00:45:12 +00001411/// getOrCreateRecordType - Emit record type's standalone debug info.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001412llvm::MDType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
1413 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001414 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001415 llvm::MDType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001416 return T;
1417}
1418
1419/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1420/// debug info.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001421llvm::MDType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
1422 SourceLocation Loc) {
Eric Christopher75e17682013-05-16 00:45:23 +00001423 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001424 llvm::MDType *T = getOrCreateType(D, getOrCreateFile(Loc));
Adrian Prantl73409ce2013-03-11 18:33:46 +00001425 RetainedTypes.push_back(D.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 return T;
1427}
1428
David Blaikie483a9da2014-05-06 18:35:21 +00001429void CGDebugInfo::completeType(const EnumDecl *ED) {
1430 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1431 return;
1432 QualType Ty = CGM.getContext().getEnumType(ED);
Eric Christophere7b87e52014-10-26 23:40:33 +00001433 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikie483a9da2014-05-06 18:35:21 +00001434 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001435 if (I == TypeCache.end() || !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikie483a9da2014-05-06 18:35:21 +00001436 return;
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001437 llvm::MDType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001438 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001439 TypeCache[TyPtr].reset(Res);
David Blaikie483a9da2014-05-06 18:35:21 +00001440}
1441
David Blaikieb2e86eb2013-08-15 20:49:17 +00001442void CGDebugInfo::completeType(const RecordDecl *RD) {
1443 if (DebugKind > CodeGenOptions::LimitedDebugInfo ||
1444 !CGM.getLangOpts().CPlusPlus)
1445 completeRequiredType(RD);
1446}
1447
1448void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
David Blaikie0856f662014-03-04 22:01:08 +00001449 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
1450 return;
1451
David Blaikie6943dea2013-08-20 01:28:15 +00001452 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1453 if (CXXDecl->isDynamicClass())
1454 return;
1455
David Blaikieb2e86eb2013-08-15 20:49:17 +00001456 QualType Ty = CGM.getContext().getRecordType(RD);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001457 llvm::MDType *T = getTypeOrNull(Ty);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001458 if (T && T->isForwardDecl())
David Blaikie6943dea2013-08-20 01:28:15 +00001459 completeClassData(RD);
1460}
1461
1462void CGDebugInfo::completeClassData(const RecordDecl *RD) {
1463 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Michael Gottesman349542b2013-08-19 18:46:16 +00001464 return;
David Blaikie6943dea2013-08-20 01:28:15 +00001465 QualType Ty = CGM.getContext().getRecordType(RD);
Eric Christophere7b87e52014-10-26 23:40:33 +00001466 void *TyPtr = Ty.getAsOpaquePtr();
David Blaikieef8a9512014-05-05 23:23:53 +00001467 auto I = TypeCache.find(TyPtr);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001468 if (I != TypeCache.end() && !cast<llvm::MDType>(I->second)->isForwardDecl())
David Blaikieb2e86eb2013-08-15 20:49:17 +00001469 return;
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001470 llvm::MDType *Res = CreateTypeDefinition(Ty->castAs<RecordType>());
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00001471 assert(!Res->isForwardDecl());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001472 TypeCache[TyPtr].reset(Res);
David Blaikieb2e86eb2013-08-15 20:49:17 +00001473}
1474
David Blaikie0e716b42014-03-03 23:48:23 +00001475static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
1476 CXXRecordDecl::method_iterator End) {
1477 for (; I != End; ++I)
1478 if (FunctionDecl *Tmpl = I->getInstantiatedFromMemberFunction())
David Blaikief7f21852014-03-04 03:08:14 +00001479 if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() &&
1480 !I->getMemberSpecializationInfo()->isExplicitSpecialization())
David Blaikie0e716b42014-03-03 23:48:23 +00001481 return true;
1482 return false;
1483}
1484
1485static bool shouldOmitDefinition(CodeGenOptions::DebugInfoKind DebugKind,
1486 const RecordDecl *RD,
1487 const LangOptions &LangOpts) {
1488 if (DebugKind > CodeGenOptions::LimitedDebugInfo)
1489 return false;
1490
1491 if (!LangOpts.CPlusPlus)
1492 return false;
1493
1494 if (!RD->isCompleteDefinitionRequired())
1495 return true;
1496
1497 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1498
1499 if (!CXXDecl)
1500 return false;
1501
1502 if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass())
1503 return true;
1504
1505 TemplateSpecializationKind Spec = TSK_Undeclared;
1506 if (const ClassTemplateSpecializationDecl *SD =
1507 dyn_cast<ClassTemplateSpecializationDecl>(RD))
1508 Spec = SD->getSpecializationKind();
1509
1510 if (Spec == TSK_ExplicitInstantiationDeclaration &&
1511 hasExplicitMemberDefinition(CXXDecl->method_begin(),
1512 CXXDecl->method_end()))
1513 return true;
1514
1515 return false;
1516}
1517
Guy Benyei11169dd2012-12-18 14:30:41 +00001518/// CreateType - get structure or union type.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001519llvm::MDType *CGDebugInfo::CreateType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 RecordDecl *RD = Ty->getDecl();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001521 llvm::MDType *T = cast_or_null<llvm::MDType>(getTypeOrNull(QualType(Ty, 0)));
David Blaikie0e716b42014-03-03 23:48:23 +00001522 if (T || shouldOmitDefinition(DebugKind, RD, CGM.getLangOpts())) {
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001523 if (!T)
David Blaikie65ec94e2014-02-18 20:52:05 +00001524 T = getOrCreateRecordFwdDecl(
1525 Ty, getContextDescriptor(cast<Decl>(RD->getDeclContext())));
David Blaikie3b1cc9b2013-09-06 06:45:04 +00001526 return T;
David Blaikiee36464c2013-06-05 05:32:23 +00001527 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001528
David Blaikieb2e86eb2013-08-15 20:49:17 +00001529 return CreateTypeDefinition(Ty);
1530}
1531
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001532llvm::MDType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
David Blaikieb2e86eb2013-08-15 20:49:17 +00001533 RecordDecl *RD = Ty->getDecl();
1534
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001536 llvm::MDFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001537
1538 // Records and classes and unions can all be recursive. To handle them, we
1539 // first generate a debug descriptor for the struct as a forward declaration.
1540 // Then (if it is a definition) we go through and get debug info for all of
1541 // its members. Finally, we create a descriptor for the complete type (which
1542 // may refer to the forward decl if the struct is recursive) and replace all
1543 // uses of the forward declaration with the final definition.
1544
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001545 auto *FwdDecl =
1546 cast<llvm::MDCompositeType>(getOrCreateLimitedType(Ty, DefUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001547
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001548 const RecordDecl *D = RD->getDefinition();
1549 if (!D || !D->isCompleteDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 return FwdDecl;
1551
David Blaikieadfbf992013-08-18 16:55:33 +00001552 if (const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD))
1553 CollectContainingType(CXXDecl, FwdDecl);
1554
Guy Benyei11169dd2012-12-18 14:30:41 +00001555 // Push the struct on region stack.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001556 LexicalBlockStack.emplace_back(&*FwdDecl);
1557 RegionMap[Ty->getDecl()].reset(FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001558
Guy Benyei11169dd2012-12-18 14:30:41 +00001559 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001560 SmallVector<llvm::Metadata *, 16> EltTys;
David Blaikie6943dea2013-08-20 01:28:15 +00001561 // what about nested types?
Guy Benyei11169dd2012-12-18 14:30:41 +00001562
1563 // Note: The split of CXXDecl information here is intentional, the
1564 // gdb tests will depend on a certain ordering at printout. The debug
1565 // information offsets are still correct if we merge them all together
1566 // though.
1567 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1568 if (CXXDecl) {
1569 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1570 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
1571 }
1572
Eric Christopher91a31902013-01-16 01:22:32 +00001573 // Collect data fields (including static variables and any initializers).
Guy Benyei11169dd2012-12-18 14:30:41 +00001574 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
Eric Christopher2df080e2013-10-11 18:16:51 +00001575 if (CXXDecl)
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001577
1578 LexicalBlockStack.pop_back();
1579 RegionMap.erase(Ty->getDecl());
1580
1581 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001582 DBuilder.replaceArrays(FwdDecl, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00001583
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001584 if (FwdDecl->isTemporary())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001585 FwdDecl =
1586 llvm::MDNode::replaceWithPermanent(llvm::TempMDCompositeType(FwdDecl));
Adrian Prantl5f66bae2015-02-11 17:45:15 +00001587
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001588 RegionMap[Ty->getDecl()].reset(FwdDecl);
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001589 return FwdDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001590}
1591
1592/// CreateType - get objective-c object type.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001593llvm::MDType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001594 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 // Ignore protocols.
1596 return getOrCreateType(Ty->getBaseType(), Unit);
1597}
1598
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001599/// \return true if Getter has the default name for the property PD.
1600static bool hasDefaultGetterName(const ObjCPropertyDecl *PD,
1601 const ObjCMethodDecl *Getter) {
1602 assert(PD);
1603 if (!Getter)
1604 return true;
1605
1606 assert(Getter->getDeclName().isObjCZeroArgSelector());
1607 return PD->getName() ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001608 Getter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001609}
1610
1611/// \return true if Setter has the default name for the property PD.
1612static bool hasDefaultSetterName(const ObjCPropertyDecl *PD,
1613 const ObjCMethodDecl *Setter) {
1614 assert(PD);
1615 if (!Setter)
1616 return true;
1617
1618 assert(Setter->getDeclName().isObjCOneArgSelector());
Adrian Prantla4ce9062013-06-07 22:29:12 +00001619 return SelectorTable::constructSetterName(PD->getName()) ==
Eric Christophere7b87e52014-10-26 23:40:33 +00001620 Setter->getDeclName().getObjCSelector().getNameForSlot(0);
Adrian Prantlb8fad1a2013-06-07 01:10:45 +00001621}
1622
Guy Benyei11169dd2012-12-18 14:30:41 +00001623/// CreateType - get objective-c interface type.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001624llvm::MDType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001625 llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001626 ObjCInterfaceDecl *ID = Ty->getDecl();
1627 if (!ID)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001628 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001629
1630 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001631 llvm::MDFile *DefUnit = getOrCreateFile(ID->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001632 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001633 auto RuntimeLang =
1634 static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage());
Guy Benyei11169dd2012-12-18 14:30:41 +00001635
1636 // If this is just a forward declaration return a special forward-declaration
1637 // debug type since we won't be able to lay out the entire type.
1638 ObjCInterfaceDecl *Def = ID->getDefinition();
David Blaikieef8a9512014-05-05 23:23:53 +00001639 if (!Def || !Def->getImplementation()) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001640 llvm::MDType *FwdDecl = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001641 llvm::dwarf::DW_TAG_structure_type, ID->getName(), TheCU, DefUnit, Line,
1642 RuntimeLang);
David Blaikieef8a9512014-05-05 23:23:53 +00001643 ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001644 return FwdDecl;
1645 }
1646
David Blaikieef8a9512014-05-05 23:23:53 +00001647 return CreateTypeDefinition(Ty, Unit);
1648}
1649
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001650llvm::MDType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001651 llvm::MDFile *Unit) {
David Blaikieef8a9512014-05-05 23:23:53 +00001652 ObjCInterfaceDecl *ID = Ty->getDecl();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001653 llvm::MDFile *DefUnit = getOrCreateFile(ID->getLocation());
David Blaikieef8a9512014-05-05 23:23:53 +00001654 unsigned Line = getLineNumber(ID->getLocation());
Duncan P. N. Exon Smith798d5652015-04-15 23:19:15 +00001655 unsigned RuntimeLang = TheCU->getSourceLanguage();
Guy Benyei11169dd2012-12-18 14:30:41 +00001656
1657 // Bit size, align and offset of the type.
1658 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1659 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1660
1661 unsigned Flags = 0;
1662 if (ID->getImplementation())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001663 Flags |= llvm::DebugNode::FlagObjcClassComplete;
Guy Benyei11169dd2012-12-18 14:30:41 +00001664
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001665 llvm::MDCompositeType *RealDecl =
1666 DBuilder.createStructType(Unit, ID->getName(), DefUnit, Line, Size, Align,
1667 Flags, nullptr, llvm::DIArray(), RuntimeLang);
Guy Benyei11169dd2012-12-18 14:30:41 +00001668
David Blaikieef8a9512014-05-05 23:23:53 +00001669 QualType QTy(Ty, 0);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001670 TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001671
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001672 // Push the struct on region stack.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001673 LexicalBlockStack.emplace_back(RealDecl);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001674 RegionMap[Ty->getDecl()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00001675
1676 // Convert all the elements.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001677 SmallVector<llvm::Metadata *, 16> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00001678
1679 ObjCInterfaceDecl *SClass = ID->getSuperClass();
1680 if (SClass) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001681 llvm::MDType *SClassTy =
Eric Christophere7b87e52014-10-26 23:40:33 +00001682 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001683 if (!SClassTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001684 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001685
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001686 llvm::MDType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 EltTys.push_back(InhTag);
1688 }
1689
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001690 // Create entries for all of the properties.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001691 for (const auto *PD : ID->properties()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001693 llvm::MDFile *PUnit = getOrCreateFile(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 unsigned PLine = getLineNumber(Loc);
1695 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1696 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001697 llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
1698 PD->getName(), PUnit, PLine,
1699 hasDefaultGetterName(PD, Getter) ? ""
1700 : getSelectorName(PD->getGetterName()),
1701 hasDefaultSetterName(PD, Setter) ? ""
1702 : getSelectorName(PD->getSetterName()),
1703 PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001704 EltTys.push_back(PropertyNode);
1705 }
1706
1707 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
1708 unsigned FieldNo = 0;
1709 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
1710 Field = Field->getNextIvar(), ++FieldNo) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001711 llvm::MDType *FieldTy = getOrCreateType(Field->getType(), Unit);
Duncan P. N. Exon Smithb7470232015-04-15 23:48:50 +00001712 if (!FieldTy)
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001713 return nullptr;
Eric Christopherb2a008c2013-05-16 00:45:12 +00001714
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 StringRef FieldName = Field->getName();
1716
1717 // Ignore unnamed fields.
1718 if (FieldName.empty())
1719 continue;
1720
1721 // Get the location for the field.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001722 llvm::MDFile *FieldDefUnit = getOrCreateFile(Field->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001723 unsigned FieldLine = getLineNumber(Field->getLocation());
1724 QualType FType = Field->getType();
1725 uint64_t FieldSize = 0;
1726 unsigned FieldAlign = 0;
1727
1728 if (!FType->isIncompleteArrayType()) {
1729
1730 // Bit size, align and offset of the type.
1731 FieldSize = Field->isBitField()
Eric Christopher35f1f9f2013-07-14 21:00:07 +00001732 ? Field->getBitWidthValue(CGM.getContext())
1733 : CGM.getContext().getTypeSize(FType);
Guy Benyei11169dd2012-12-18 14:30:41 +00001734 FieldAlign = CGM.getContext().getTypeAlign(FType);
1735 }
1736
1737 uint64_t FieldOffset;
1738 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1739 // We don't know the runtime offset of an ivar if we're using the
1740 // non-fragile ABI. For bitfields, use the bit offset into the first
1741 // byte of storage of the bitfield. For other fields, use zero.
1742 if (Field->isBitField()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001743 FieldOffset =
1744 CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field);
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 FieldOffset %= CGM.getContext().getCharWidth();
1746 } else {
1747 FieldOffset = 0;
1748 }
1749 } else {
1750 FieldOffset = RL.getFieldOffset(FieldNo);
1751 }
1752
1753 unsigned Flags = 0;
1754 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001755 Flags = llvm::DebugNode::FlagProtected;
Guy Benyei11169dd2012-12-18 14:30:41 +00001756 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001757 Flags = llvm::DebugNode::FlagPrivate;
Adrian Prantl21361fb2014-08-29 22:44:27 +00001758 else if (Field->getAccessControl() == ObjCIvarDecl::Public)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001759 Flags = llvm::DebugNode::FlagPublic;
Guy Benyei11169dd2012-12-18 14:30:41 +00001760
Craig Topper8a13c412014-05-21 05:09:00 +00001761 llvm::MDNode *PropertyNode = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001762 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Eric Christopherb2a008c2013-05-16 00:45:12 +00001763 if (ObjCPropertyImplDecl *PImpD =
Eric Christophere7b87e52014-10-26 23:40:33 +00001764 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001765 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christopherc0c5d462013-02-21 22:35:08 +00001766 SourceLocation Loc = PD->getLocation();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001767 llvm::MDFile *PUnit = getOrCreateFile(Loc);
Eric Christopherc0c5d462013-02-21 22:35:08 +00001768 unsigned PLine = getLineNumber(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1770 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00001771 PropertyNode = DBuilder.createObjCProperty(
1772 PD->getName(), PUnit, PLine,
1773 hasDefaultGetterName(PD, Getter) ? "" : getSelectorName(
1774 PD->getGetterName()),
1775 hasDefaultSetterName(PD, Setter) ? "" : getSelectorName(
1776 PD->getSetterName()),
1777 PD->getPropertyAttributes(),
1778 getOrCreateType(PD->getType(), PUnit));
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 }
1780 }
1781 }
Eric Christophere7b87e52014-10-26 23:40:33 +00001782 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
1783 FieldSize, FieldAlign, FieldOffset, Flags,
1784 FieldTy, PropertyNode);
Guy Benyei11169dd2012-12-18 14:30:41 +00001785 EltTys.push_back(FieldTy);
1786 }
1787
1788 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00001789 DBuilder.replaceArrays(RealDecl, Elements);
Adrian Prantla03a85a2013-03-06 22:03:30 +00001790
Guy Benyei11169dd2012-12-18 14:30:41 +00001791 LexicalBlockStack.pop_back();
Eric Christopher5c7ee8b2013-04-02 22:59:11 +00001792 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00001793}
1794
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001795llvm::MDType *CGDebugInfo::CreateType(const VectorType *Ty,
1796 llvm::MDFile *Unit) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001797 llvm::MDType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 int64_t Count = Ty->getNumElements();
1799 if (Count == 0)
1800 // If number of elements are not known then this is an unbounded array.
1801 // Use Count == -1 to express such arrays.
1802 Count = -1;
1803
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001804 llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count);
Guy Benyei11169dd2012-12-18 14:30:41 +00001805 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1806
1807 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1808 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1809
1810 return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
1811}
1812
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001813llvm::MDType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001814 uint64_t Size;
1815 uint64_t Align;
1816
1817 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
1818 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
1819 Size = 0;
1820 Align =
Eric Christophere7b87e52014-10-26 23:40:33 +00001821 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Guy Benyei11169dd2012-12-18 14:30:41 +00001822 } else if (Ty->isIncompleteArrayType()) {
1823 Size = 0;
1824 if (Ty->getElementType()->isIncompleteType())
1825 Align = 0;
1826 else
1827 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
David Blaikief03b2e82013-05-09 20:48:12 +00001828 } else if (Ty->isIncompleteType()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001829 Size = 0;
1830 Align = 0;
1831 } else {
1832 // Size and align of the whole array, not the element type.
1833 Size = CGM.getContext().getTypeSize(Ty);
1834 Align = CGM.getContext().getTypeAlign(Ty);
1835 }
1836
1837 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1838 // interior arrays, do we care? Why aren't nested arrays represented the
1839 // obvious/recursive way?
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001840 SmallVector<llvm::Metadata *, 8> Subscripts;
Guy Benyei11169dd2012-12-18 14:30:41 +00001841 QualType EltTy(Ty, 0);
1842 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1843 // If the number of elements is known, then count is that number. Otherwise,
1844 // it's -1. This allows us to represent a subrange with an array of 0
1845 // elements, like this:
1846 //
1847 // struct foo {
1848 // int x[0];
1849 // };
Eric Christophere7b87e52014-10-26 23:40:33 +00001850 int64_t Count = -1; // Count == -1 is an unbounded array.
Guy Benyei11169dd2012-12-18 14:30:41 +00001851 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1852 Count = CAT->getSize().getZExtValue();
Eric Christopherb2a008c2013-05-16 00:45:12 +00001853
Guy Benyei11169dd2012-12-18 14:30:41 +00001854 // FIXME: Verify this is right for VLAs.
1855 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count));
1856 EltTy = Ty->getElementType();
1857 }
1858
1859 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
1860
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001861 return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1862 SubscriptArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00001863}
1864
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001865llvm::MDType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001866 llvm::MDFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001867 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
1868 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001869}
1870
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001871llvm::MDType *CGDebugInfo::CreateType(const RValueReferenceType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001872 llvm::MDFile *Unit) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001873 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty,
1874 Ty->getPointeeType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00001875}
1876
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001877llvm::MDType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001878 llvm::MDFile *U) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001879 llvm::MDType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U);
David Blaikie2c705ca2013-01-19 19:20:56 +00001880 if (!Ty->getPointeeType()->isFunctionType())
1881 return DBuilder.createMemberPointerType(
Adrian Prantlee24e142014-12-23 19:11:54 +00001882 getOrCreateType(Ty->getPointeeType(), U), ClassType,
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001883 CGM.getContext().getTypeSize(Ty));
Adrian Prantl0866acd2013-12-19 01:38:47 +00001884
1885 const FunctionProtoType *FPT =
Eric Christophere7b87e52014-10-26 23:40:33 +00001886 Ty->getPointeeType()->getAs<FunctionProtoType>();
1887 return DBuilder.createMemberPointerType(
1888 getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType(
1889 Ty->getClass(), FPT->getTypeQuals())),
1890 FPT, U),
Adrian Prantl0772dbd2015-01-07 17:49:30 +00001891 ClassType, CGM.getContext().getTypeSize(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00001892}
1893
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001894llvm::MDType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::MDFile *U) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001895 // Ignore the atomic wrapping
1896 // FIXME: What is the correct representation?
1897 return getOrCreateType(Ty->getValueType(), U);
1898}
1899
1900/// CreateEnumType - get enumeration type.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001901llvm::MDType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
Manman Ren1b457022013-08-28 21:20:28 +00001902 const EnumDecl *ED = Ty->getDecl();
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 uint64_t Size = 0;
1904 uint64_t Align = 0;
1905 if (!ED->getTypeForDecl()->isIncompleteType()) {
1906 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1907 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1908 }
1909
Manman Rene0064d82013-08-29 23:19:58 +00001910 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1911
Guy Benyei11169dd2012-12-18 14:30:41 +00001912 // If this is just a forward declaration, construct an appropriately
1913 // marked node and just return it.
1914 if (!ED->getDefinition()) {
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001915 llvm::MDScope *EDContext =
1916 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001917 llvm::MDFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001918 unsigned Line = getLineNumber(ED->getLocation());
1919 StringRef EDName = ED->getName();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001920 llvm::MDType *RetTy = DBuilder.createReplaceableCompositeType(
David Blaikief427b002014-05-06 03:42:01 +00001921 llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00001922 0, Size, Align, llvm::DebugNode::FlagFwdDecl, FullName);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001923 ReplaceMap.emplace_back(
1924 std::piecewise_construct, std::make_tuple(Ty),
1925 std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
David Blaikief427b002014-05-06 03:42:01 +00001926 return RetTy;
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 }
1928
David Blaikie483a9da2014-05-06 18:35:21 +00001929 return CreateTypeDefinition(Ty);
1930}
1931
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001932llvm::MDType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
David Blaikie483a9da2014-05-06 18:35:21 +00001933 const EnumDecl *ED = Ty->getDecl();
1934 uint64_t Size = 0;
1935 uint64_t Align = 0;
1936 if (!ED->getTypeForDecl()->isIncompleteType()) {
1937 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1938 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1939 }
1940
1941 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
1942
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00001943 // Create elements for each enumerator.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001944 SmallVector<llvm::Metadata *, 16> Enumerators;
Guy Benyei11169dd2012-12-18 14:30:41 +00001945 ED = ED->getDefinition();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00001946 for (const auto *Enum : ED->enumerators()) {
Eric Christophere7b87e52014-10-26 23:40:33 +00001947 Enumerators.push_back(DBuilder.createEnumerator(
1948 Enum->getName(), Enum->getInitVal().getSExtValue()));
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 }
1950
1951 // Return a CompositeType for the enum itself.
1952 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
1953
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00001954 llvm::MDFile *DefUnit = getOrCreateFile(ED->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00001955 unsigned Line = getLineNumber(ED->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00001956 llvm::MDScope *EnumContext =
Eric Christophere7b87e52014-10-26 23:40:33 +00001957 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00001958 llvm::MDType *ClassTy =
1959 ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
1960 return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
1961 Line, Size, Align, EltArray, ClassTy,
1962 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00001963}
1964
David Blaikie05491062013-01-21 04:37:12 +00001965static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
1966 Qualifiers Quals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001967 do {
Adrian Prantl179af902013-09-26 21:35:50 +00001968 Qualifiers InnerQuals = T.getLocalQualifiers();
1969 // Qualifiers::operator+() doesn't like it if you add a Qualifier
1970 // that is already there.
1971 Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals);
1972 Quals += InnerQuals;
Guy Benyei11169dd2012-12-18 14:30:41 +00001973 QualType LastT = T;
1974 switch (T->getTypeClass()) {
1975 default:
David Blaikie05491062013-01-21 04:37:12 +00001976 return C.getQualifiedType(T.getTypePtr(), Quals);
David Blaikief1b382e2014-04-06 17:14:06 +00001977 case Type::TemplateSpecialization: {
1978 const auto *Spec = cast<TemplateSpecializationType>(T);
1979 if (Spec->isTypeAlias())
1980 return C.getQualifiedType(T.getTypePtr(), Quals);
1981 T = Spec->desugar();
Eric Christophere7b87e52014-10-26 23:40:33 +00001982 break;
1983 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001984 case Type::TypeOfExpr:
1985 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
1986 break;
1987 case Type::TypeOf:
1988 T = cast<TypeOfType>(T)->getUnderlyingType();
1989 break;
1990 case Type::Decltype:
1991 T = cast<DecltypeType>(T)->getUnderlyingType();
1992 break;
1993 case Type::UnaryTransform:
1994 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1995 break;
1996 case Type::Attributed:
1997 T = cast<AttributedType>(T)->getEquivalentType();
1998 break;
1999 case Type::Elaborated:
2000 T = cast<ElaboratedType>(T)->getNamedType();
2001 break;
2002 case Type::Paren:
2003 T = cast<ParenType>(T)->getInnerType();
2004 break;
David Blaikie05491062013-01-21 04:37:12 +00002005 case Type::SubstTemplateTypeParm:
Guy Benyei11169dd2012-12-18 14:30:41 +00002006 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 break;
2008 case Type::Auto:
David Blaikie22c460a02013-05-24 21:24:35 +00002009 QualType DT = cast<AutoType>(T)->getDeducedType();
David Blaikie42edade2014-11-11 20:44:45 +00002010 assert(!DT.isNull() && "Undeduced types shouldn't reach here.");
David Blaikie22c460a02013-05-24 21:24:35 +00002011 T = DT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 break;
2013 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002014
Guy Benyei11169dd2012-12-18 14:30:41 +00002015 assert(T != LastT && "Type unwrapping failed to unwrap!");
NAKAMURA Takumi3e0a3632013-01-21 10:51:28 +00002016 (void)LastT;
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 } while (true);
2018}
2019
Eric Christopher0fdcb312013-05-16 00:52:20 +00002020/// getType - Get the type from the cache or return null type if it doesn't
2021/// exist.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002022llvm::MDType *CGDebugInfo::getTypeOrNull(QualType Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002023
2024 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002025 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002026
David Blaikief427b002014-05-06 03:42:01 +00002027 auto it = TypeCache.find(Ty.getAsOpaquePtr());
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 if (it != TypeCache.end()) {
2029 // Verify that the debug info still exists.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002030 if (llvm::Metadata *V = it->second)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002031 return cast<llvm::MDType>(V);
Guy Benyei11169dd2012-12-18 14:30:41 +00002032 }
2033
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002034 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002035}
2036
David Blaikie0e716b42014-03-03 23:48:23 +00002037void CGDebugInfo::completeTemplateDefinition(
2038 const ClassTemplateSpecializationDecl &SD) {
David Blaikie0856f662014-03-04 22:01:08 +00002039 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2040 return;
2041
David Blaikie0e716b42014-03-03 23:48:23 +00002042 completeClassData(&SD);
2043 // In case this type has no member function definitions being emitted, ensure
2044 // it is retained
2045 RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
2046}
2047
Guy Benyei11169dd2012-12-18 14:30:41 +00002048/// getOrCreateType - Get the type from the cache or create a new
2049/// one if necessary.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002050llvm::MDType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002051 if (Ty.isNull())
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002052 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002053
2054 // Unwrap the type as needed for debug information.
David Blaikie05491062013-01-21 04:37:12 +00002055 Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00002056
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002057 if (auto *T = getTypeOrNull(Ty))
Guy Benyei11169dd2012-12-18 14:30:41 +00002058 return T;
2059
2060 // Otherwise create the type.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002061 llvm::MDType *Res = CreateTypeNode(Ty, Unit);
Eric Christophere7b87e52014-10-26 23:40:33 +00002062 void *TyPtr = Ty.getAsOpaquePtr();
Adrian Prantl73409ce2013-03-11 18:33:46 +00002063
2064 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002065 TypeCache[TyPtr].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002066
Guy Benyei11169dd2012-12-18 14:30:41 +00002067 return Res;
2068}
2069
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002070/// Currently the checksum of an interface includes the number of
2071/// ivars and property accessors.
Eric Christopher1ecc5632013-06-07 22:54:39 +00002072unsigned CGDebugInfo::Checksum(const ObjCInterfaceDecl *ID) {
Adrian Prantl817bbb32013-06-07 01:10:48 +00002073 // The assumption is that the number of ivars can only increase
2074 // monotonically, so it is safe to just use their current number as
2075 // a checksum.
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002076 unsigned Sum = 0;
2077 for (const ObjCIvarDecl *Ivar = ID->all_declared_ivar_begin();
Craig Topper8a13c412014-05-21 05:09:00 +00002078 Ivar != nullptr; Ivar = Ivar->getNextIvar())
Adrian Prantlc4de1ef2013-06-07 01:10:41 +00002079 ++Sum;
2080
2081 return Sum;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002082}
2083
2084ObjCInterfaceDecl *CGDebugInfo::getObjCInterfaceDecl(QualType Ty) {
2085 switch (Ty->getTypeClass()) {
2086 case Type::ObjCObjectPointer:
Eric Christophere7b87e52014-10-26 23:40:33 +00002087 return getObjCInterfaceDecl(
2088 cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Adrian Prantla03a85a2013-03-06 22:03:30 +00002089 case Type::ObjCInterface:
2090 return cast<ObjCInterfaceType>(Ty)->getDecl();
2091 default:
Craig Topper8a13c412014-05-21 05:09:00 +00002092 return nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00002093 }
2094}
2095
Guy Benyei11169dd2012-12-18 14:30:41 +00002096/// CreateTypeNode - Create a new debug type node.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002097llvm::MDType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::MDFile *Unit) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 // Handle qualifiers, which recursively handles what they refer to.
2099 if (Ty.hasLocalQualifiers())
David Blaikie99dab3b2013-09-04 22:03:57 +00002100 return CreateQualifiedType(Ty, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002101
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 // Work out details of type.
2103 switch (Ty->getTypeClass()) {
2104#define TYPE(Class, Base)
2105#define ABSTRACT_TYPE(Class, Base)
2106#define NON_CANONICAL_TYPE(Class, Base)
2107#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2108#include "clang/AST/TypeNodes.def"
2109 llvm_unreachable("Dependent types cannot show up in debug information");
2110
2111 case Type::ExtVector:
2112 case Type::Vector:
2113 return CreateType(cast<VectorType>(Ty), Unit);
2114 case Type::ObjCObjectPointer:
2115 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
2116 case Type::ObjCObject:
2117 return CreateType(cast<ObjCObjectType>(Ty), Unit);
2118 case Type::ObjCInterface:
2119 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
2120 case Type::Builtin:
2121 return CreateType(cast<BuiltinType>(Ty));
2122 case Type::Complex:
2123 return CreateType(cast<ComplexType>(Ty));
2124 case Type::Pointer:
2125 return CreateType(cast<PointerType>(Ty), Unit);
Reid Kleckner0503a872013-12-05 01:23:43 +00002126 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +00002127 case Type::Decayed:
Reid Kleckner0503a872013-12-05 01:23:43 +00002128 // Decayed and adjusted types use the adjusted type in LLVM and DWARF.
Reid Kleckner8a365022013-06-24 17:51:48 +00002129 return CreateType(
Reid Kleckner0503a872013-12-05 01:23:43 +00002130 cast<PointerType>(cast<AdjustedType>(Ty)->getAdjustedType()), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002131 case Type::BlockPointer:
2132 return CreateType(cast<BlockPointerType>(Ty), Unit);
2133 case Type::Typedef:
David Blaikie99dab3b2013-09-04 22:03:57 +00002134 return CreateType(cast<TypedefType>(Ty), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002135 case Type::Record:
David Blaikie99dab3b2013-09-04 22:03:57 +00002136 return CreateType(cast<RecordType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002137 case Type::Enum:
Manman Ren1b457022013-08-28 21:20:28 +00002138 return CreateEnumType(cast<EnumType>(Ty));
Guy Benyei11169dd2012-12-18 14:30:41 +00002139 case Type::FunctionProto:
2140 case Type::FunctionNoProto:
2141 return CreateType(cast<FunctionType>(Ty), Unit);
2142 case Type::ConstantArray:
2143 case Type::VariableArray:
2144 case Type::IncompleteArray:
2145 return CreateType(cast<ArrayType>(Ty), Unit);
2146
2147 case Type::LValueReference:
2148 return CreateType(cast<LValueReferenceType>(Ty), Unit);
2149 case Type::RValueReference:
2150 return CreateType(cast<RValueReferenceType>(Ty), Unit);
2151
2152 case Type::MemberPointer:
2153 return CreateType(cast<MemberPointerType>(Ty), Unit);
2154
2155 case Type::Atomic:
2156 return CreateType(cast<AtomicType>(Ty), Unit);
2157
Guy Benyei11169dd2012-12-18 14:30:41 +00002158 case Type::TemplateSpecialization:
David Blaikief1b382e2014-04-06 17:14:06 +00002159 return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
2160
David Blaikie42edade2014-11-11 20:44:45 +00002161 case Type::Auto:
David Blaikief1b382e2014-04-06 17:14:06 +00002162 case Type::Attributed:
Guy Benyei11169dd2012-12-18 14:30:41 +00002163 case Type::Elaborated:
2164 case Type::Paren:
2165 case Type::SubstTemplateTypeParm:
2166 case Type::TypeOfExpr:
2167 case Type::TypeOf:
2168 case Type::Decltype:
2169 case Type::UnaryTransform:
David Blaikie66ed89d2013-07-13 21:08:08 +00002170 case Type::PackExpansion:
David Blaikie22c460a02013-05-24 21:24:35 +00002171 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002172 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002173
David Blaikie42edade2014-11-11 20:44:45 +00002174 llvm_unreachable("type should have been unwrapped!");
Guy Benyei11169dd2012-12-18 14:30:41 +00002175}
2176
2177/// getOrCreateLimitedType - Get the type from the cache or create a new
2178/// limited type if necessary.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002179llvm::MDType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002180 llvm::MDFile *Unit) {
David Blaikie4a2b5ef2013-08-12 22:24:20 +00002181 QualType QTy(Ty, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002182
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002183 auto *T = cast_or_null<llvm::MDCompositeTypeBase>(getTypeOrNull(QTy));
Guy Benyei11169dd2012-12-18 14:30:41 +00002184
2185 // We may have cached a forward decl when we could have created
2186 // a non-forward decl. Go ahead and create a non-forward decl
2187 // now.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002188 if (T && !T->isForwardDecl())
Eric Christophere7b87e52014-10-26 23:40:33 +00002189 return T;
Guy Benyei11169dd2012-12-18 14:30:41 +00002190
2191 // Otherwise create the type.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002192 llvm::MDCompositeType *Res = CreateLimitedType(Ty);
David Blaikie8d5e1282013-08-20 21:03:29 +00002193
2194 // Propagate members from the declaration to the definition
2195 // CreateType(const RecordType*) will overwrite this with the members in the
2196 // correct order if the full type is needed.
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002197 DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DIArray());
Guy Benyei11169dd2012-12-18 14:30:41 +00002198
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 // And update the type cache.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002200 TypeCache[QTy.getAsOpaquePtr()].reset(Res);
Guy Benyei11169dd2012-12-18 14:30:41 +00002201 return Res;
2202}
2203
2204// TODO: Currently used for context chains when limiting debug info.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002205llvm::MDCompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 RecordDecl *RD = Ty->getDecl();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002207
Guy Benyei11169dd2012-12-18 14:30:41 +00002208 // Get overall information about the record type for the debug info.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002209 llvm::MDFile *DefUnit = getOrCreateFile(RD->getLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 unsigned Line = getLineNumber(RD->getLocation());
2211 StringRef RDName = getClassName(RD);
2212
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002213 llvm::MDScope *RDContext =
Eric Christopher07429ff2013-10-15 21:22:34 +00002214 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002215
David Blaikied2785892013-08-18 17:36:19 +00002216 // If we ended up creating the type during the context chain construction,
2217 // just return that.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002218 auto *T = cast_or_null<llvm::MDCompositeType>(
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002219 getTypeOrNull(CGM.getContext().getRecordType(RD)));
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00002220 if (T && (!T->isForwardDecl() || !RD->getDefinition()))
Eric Christophere7b87e52014-10-26 23:40:33 +00002221 return T;
David Blaikied2785892013-08-18 17:36:19 +00002222
Adrian Prantl381e7552014-02-04 21:29:50 +00002223 // If this is just a forward or incomplete declaration, construct an
2224 // appropriately marked node and just return it.
2225 const RecordDecl *D = RD->getDefinition();
2226 if (!D || !D->isCompleteDefinition())
Manman Ren1b457022013-08-28 21:20:28 +00002227 return getOrCreateRecordFwdDecl(Ty, RDContext);
Guy Benyei11169dd2012-12-18 14:30:41 +00002228
2229 uint64_t Size = CGM.getContext().getTypeSize(Ty);
2230 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002231
Manman Rene0064d82013-08-29 23:19:58 +00002232 SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU);
2233
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002234 llvm::MDCompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
2235 getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, 0,
2236 FullName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002237
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002238 RegionMap[Ty->getDecl()].reset(RealDecl);
2239 TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00002240
David Blaikieadfbf992013-08-18 16:55:33 +00002241 if (const ClassTemplateSpecializationDecl *TSpecial =
2242 dyn_cast<ClassTemplateSpecializationDecl>(RD))
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002243 DBuilder.replaceArrays(RealDecl, llvm::DIArray(),
2244 CollectCXXTemplateParams(TSpecial, DefUnit));
David Blaikie952dac32013-08-15 22:42:12 +00002245 return RealDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00002246}
2247
David Blaikieadfbf992013-08-18 16:55:33 +00002248void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002249 llvm::MDCompositeType *RealDecl) {
David Blaikieadfbf992013-08-18 16:55:33 +00002250 // A class's primary base or the class itself contains the vtable.
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002251 llvm::MDCompositeType *ContainingType = nullptr;
David Blaikieadfbf992013-08-18 16:55:33 +00002252 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2253 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
Alp Tokerd4733632013-12-05 04:47:09 +00002254 // Seek non-virtual primary base root.
David Blaikieadfbf992013-08-18 16:55:33 +00002255 while (1) {
2256 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
2257 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
2258 if (PBT && !BRL.isPrimaryBaseVirtual())
2259 PBase = PBT;
2260 else
2261 break;
2262 }
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00002263 ContainingType = cast<llvm::MDCompositeType>(
David Blaikieadfbf992013-08-18 16:55:33 +00002264 getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
2265 getOrCreateFile(RD->getLocation())));
2266 } else if (RD->isDynamicClass())
2267 ContainingType = RealDecl;
2268
Duncan P. N. Exon Smithc8ee63e2014-12-18 00:48:56 +00002269 DBuilder.replaceVTableHolder(RealDecl, ContainingType);
David Blaikieadfbf992013-08-18 16:55:33 +00002270}
2271
Guy Benyei11169dd2012-12-18 14:30:41 +00002272/// CreateMemberType - Create new member and increase Offset by FType's size.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002273llvm::MDType *CGDebugInfo::CreateMemberType(llvm::MDFile *Unit, QualType FType,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002274 StringRef Name, uint64_t *Offset) {
2275 llvm::MDType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
2277 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002278 llvm::MDType *Ty = DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize,
2279 FieldAlign, *Offset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002280 *Offset += FieldSize;
2281 return Ty;
2282}
2283
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002284void CGDebugInfo::collectFunctionDeclProps(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002285 GlobalDecl GD, llvm::MDFile *Unit, StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002286 llvm::MDScope *&FDContext, llvm::DIArray &TParamsArray, unsigned &Flags) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002287 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2288 Name = getFunctionName(FD);
2289 // Use mangled name as linkage name for C/C++ functions.
2290 if (FD->hasPrototype()) {
2291 LinkageName = CGM.getMangledName(GD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002292 Flags |= llvm::DebugNode::FlagPrototyped;
Frederic Riss9db79f12014-11-18 03:40:46 +00002293 }
2294 // No need to replicate the linkage name if it isn't different from the
2295 // subprogram name, no need to have it at all unless coverage is enabled or
2296 // debug is set to more than just line tables.
2297 if (LinkageName == Name ||
2298 (!CGM.getCodeGenOpts().EmitGcovArcs &&
2299 !CGM.getCodeGenOpts().EmitGcovNotes &&
2300 DebugKind <= CodeGenOptions::DebugLineTablesOnly))
2301 LinkageName = StringRef();
2302
2303 if (DebugKind >= CodeGenOptions::LimitedDebugInfo) {
2304 if (const NamespaceDecl *NSDecl =
2305 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2306 FDContext = getOrCreateNameSpace(NSDecl);
2307 else if (const RecordDecl *RDecl =
2308 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2309 FDContext = getContextDescriptor(cast<Decl>(RDecl));
2310 // Collect template parameters.
2311 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2312 }
2313}
2314
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002315void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::MDFile *&Unit,
Frederic Riss9db79f12014-11-18 03:40:46 +00002316 unsigned &LineNo, QualType &T,
2317 StringRef &Name, StringRef &LinkageName,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002318 llvm::MDScope *&VDContext) {
Frederic Riss9db79f12014-11-18 03:40:46 +00002319 Unit = getOrCreateFile(VD->getLocation());
2320 LineNo = getLineNumber(VD->getLocation());
2321
2322 setLocation(VD->getLocation());
2323
2324 T = VD->getType();
2325 if (T->isIncompleteArrayType()) {
2326 // CodeGen turns int[] into int[1] so we'll do the same here.
2327 llvm::APInt ConstVal(32, 1);
2328 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
2329
2330 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
2331 ArrayType::Normal, 0);
2332 }
2333
2334 Name = VD->getName();
2335 if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
2336 !isa<ObjCMethodDecl>(VD->getDeclContext()))
2337 LinkageName = CGM.getMangledName(VD);
2338 if (LinkageName == Name)
2339 LinkageName = StringRef();
2340
2341 // Since we emit declarations (DW_AT_members) for static members, place the
2342 // definition of those static members in the namespace they were declared in
2343 // in the source code (the lexical decl context).
2344 // FIXME: Generalize this for even non-member global variables where the
2345 // declaration and definition may have different lexical decl contexts, once
2346 // we have support for emitting declarations of (non-member) global variables.
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00002347 const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext()
2348 : VD->getDeclContext();
2349 // When a record type contains an in-line initialization of a static data
2350 // member, and the record type is marked as __declspec(dllexport), an implicit
2351 // definition of the member will be created in the record context. DWARF
2352 // doesn't seem to have a nice way to describe this in a form that consumers
2353 // are likely to understand, so fake the "normal" situation of a definition
2354 // outside the class by putting it in the global scope.
2355 if (DC->isRecord())
2356 DC = CGM.getContext().getTranslationUnitDecl();
2357 VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
Frederic Riss9db79f12014-11-18 03:40:46 +00002358}
2359
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002360llvm::MDSubprogram *
Frederic Rissd253ed62014-11-18 03:40:51 +00002361CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) {
2362 llvm::DIArray TParamsArray;
2363 StringRef Name, LinkageName;
2364 unsigned Flags = 0;
2365 SourceLocation Loc = FD->getLocation();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002366 llvm::MDFile *Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002367 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002368 unsigned Line = getLineNumber(Loc);
2369
2370 collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext,
2371 TParamsArray, Flags);
2372 // Build function type.
2373 SmallVector<QualType, 16> ArgTypes;
2374 for (const ParmVarDecl *Parm: FD->parameters())
2375 ArgTypes.push_back(Parm->getType());
2376 QualType FnType =
2377 CGM.getContext().getFunctionType(FD->getReturnType(), ArgTypes,
2378 FunctionProtoType::ExtProtoInfo());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002379 llvm::MDSubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002380 DContext, Name, LinkageName, Unit, Line,
2381 getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(),
2382 false /*declaration*/, 0, Flags, CGM.getLangOpts().Optimize, nullptr,
2383 TParamsArray.get(), getFunctionDeclaration(FD));
Frederic Rissd253ed62014-11-18 03:40:51 +00002384 const FunctionDecl *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002385 FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
2386 std::make_tuple(CanonDecl),
2387 std::make_tuple(SP));
Frederic Rissd253ed62014-11-18 03:40:51 +00002388 return SP;
2389}
2390
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002391llvm::MDGlobalVariable *
Frederic Rissd253ed62014-11-18 03:40:51 +00002392CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
2393 QualType T;
2394 StringRef Name, LinkageName;
2395 SourceLocation Loc = VD->getLocation();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002396 llvm::MDFile *Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002397 llvm::MDScope *DContext = Unit;
Frederic Rissd253ed62014-11-18 03:40:51 +00002398 unsigned Line = getLineNumber(Loc);
2399
2400 collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002401 auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
2402 DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
2403 !VD->isExternallyVisible(), nullptr, nullptr);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002404 FwdDeclReplaceMap.emplace_back(
2405 std::piecewise_construct,
2406 std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
2407 std::make_tuple(static_cast<llvm::Metadata *>(GV)));
Frederic Rissd253ed62014-11-18 03:40:51 +00002408 return GV;
2409}
2410
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002411llvm::DebugNode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00002412 // We only need a declaration (not a definition) of the type - so use whatever
2413 // we would otherwise do to get a type for a pointee. (forward declarations in
2414 // limited debug info, full definitions (if the type definition is available)
2415 // in unlimited debug info)
David Blaikie6b7d060c2013-08-12 23:14:36 +00002416 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
2417 return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
David Blaikie99dab3b2013-09-04 22:03:57 +00002418 getOrCreateFile(TD->getLocation()));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002419 auto I = DeclCache.find(D->getCanonicalDecl());
Frederic Rissd253ed62014-11-18 03:40:51 +00002420
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002421 if (I != DeclCache.end())
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002422 return dyn_cast_or_null<llvm::DebugNode>(I->second);
Frederic Rissd253ed62014-11-18 03:40:51 +00002423
2424 // No definition for now. Emit a forward definition that might be
2425 // merged with a potential upcoming definition.
2426 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
2427 return getFunctionForwardDeclaration(FD);
2428 else if (const auto *VD = dyn_cast<VarDecl>(D))
2429 return getGlobalVariableForwardDeclaration(VD);
2430
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002431 return nullptr;
David Blaikiebd483762013-05-20 04:58:53 +00002432}
2433
Guy Benyei11169dd2012-12-18 14:30:41 +00002434/// getFunctionDeclaration - Return debug info descriptor to describe method
2435/// declaration for the given method definition.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002436llvm::MDSubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
Diego Novillo913690c2014-06-24 17:02:17 +00002437 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002438 return nullptr;
David Blaikie18cfbc52013-06-22 00:09:36 +00002439
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Eric Christophere7b87e52014-10-26 23:40:33 +00002441 if (!FD)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002442 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002443
2444 // Setup context.
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00002445 auto *S = getContextDescriptor(cast<Decl>(D->getDeclContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002446
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002447 auto MI = SPCache.find(FD->getCanonicalDecl());
David Blaikiefd07c602013-08-09 17:20:05 +00002448 if (MI == SPCache.end()) {
Eric Christopherf86c4052013-08-28 23:12:10 +00002449 if (const CXXMethodDecl *MD =
2450 dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00002451 return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
2452 cast<llvm::MDCompositeType>(S));
David Blaikiefd07c602013-08-09 17:20:05 +00002453 }
2454 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002455 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002456 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2457 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 return SP;
2459 }
2460
Aaron Ballman86c93902014-03-06 23:45:36 +00002461 for (auto NextFD : FD->redecls()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002462 auto MI = SPCache.find(NextFD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002463 if (MI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002464 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(MI->second);
2465 if (SP && !SP->isDefinition())
Guy Benyei11169dd2012-12-18 14:30:41 +00002466 return SP;
2467 }
2468 }
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002469 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002470}
2471
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002472// getOrCreateFunctionType - Construct type. If it is a c++ method, include
Guy Benyei11169dd2012-12-18 14:30:41 +00002473// implicit parameter "this".
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002474llvm::MDSubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D,
2475 QualType FnType,
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002476 llvm::MDFile *F) {
Diego Novillo913690c2014-06-24 17:02:17 +00002477 if (!D || DebugKind <= CodeGenOptions::DebugLineTablesOnly)
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002478 // Create fake but valid subroutine type. Otherwise -verify would fail, and
2479 // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields.
Manman Ren67f005e2014-07-28 22:24:34 +00002480 return DBuilder.createSubroutineType(F,
2481 DBuilder.getOrCreateTypeArray(None));
Guy Benyei11169dd2012-12-18 14:30:41 +00002482
2483 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
2484 return getOrCreateMethodType(Method, F);
2485 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
2486 // Add "self" and "_cmd"
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002487 SmallVector<llvm::Metadata *, 16> Elts;
Guy Benyei11169dd2012-12-18 14:30:41 +00002488
2489 // First element is always return type. For 'void' functions it is NULL.
Alp Toker314cc812014-01-25 16:55:45 +00002490 QualType ResultTy = OMethod->getReturnType();
Adrian Prantl5f360102013-05-22 21:37:49 +00002491
2492 // Replace the instancetype keyword with the actual type.
2493 if (ResultTy == CGM.getContext().getObjCInstanceType())
2494 ResultTy = CGM.getContext().getPointerType(
Eric Christophere7b87e52014-10-26 23:40:33 +00002495 QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
Adrian Prantl5f360102013-05-22 21:37:49 +00002496
Adrian Prantl7bec9032013-05-10 21:08:31 +00002497 Elts.push_back(getOrCreateType(ResultTy, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002498 // "self" pointer is always first argument.
Adrian Prantlde17db32013-03-29 19:20:29 +00002499 QualType SelfDeclTy = OMethod->getSelfDecl()->getType();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002500 Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 // "_cmd" pointer is always second argument.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002502 Elts.push_back(DBuilder.createArtificialType(
2503 getOrCreateType(OMethod->getCmdDecl()->getType(), F)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002504 // Get rest of the arguments.
Aaron Ballman43b68be2014-03-07 17:50:17 +00002505 for (const auto *PI : OMethod->params())
2506 Elts.push_back(getOrCreateType(PI->getType(), F));
Frederic Riss787d9d62014-08-12 04:42:23 +00002507 // Variadic methods need a special marker at the end of the type list.
2508 if (OMethod->isVariadic())
2509 Elts.push_back(DBuilder.createUnspecifiedParameter());
Guy Benyei11169dd2012-12-18 14:30:41 +00002510
Manman Ren67f005e2014-07-28 22:24:34 +00002511 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
Guy Benyei11169dd2012-12-18 14:30:41 +00002512 return DBuilder.createSubroutineType(F, EltTypeArray);
2513 }
Adrian Prantld45ba252014-02-25 19:38:11 +00002514
Adrian Prantl800faef2014-02-25 23:42:18 +00002515 // Handle variadic function types; they need an additional
2516 // unspecified parameter.
Adrian Prantld45ba252014-02-25 19:38:11 +00002517 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
2518 if (FD->isVariadic()) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002519 SmallVector<llvm::Metadata *, 16> EltTys;
Adrian Prantld45ba252014-02-25 19:38:11 +00002520 EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
2521 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FnType))
2522 for (unsigned i = 0, e = FPT->getNumParams(); i != e; ++i)
2523 EltTys.push_back(getOrCreateType(FPT->getParamType(i), F));
2524 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Manman Ren67f005e2014-07-28 22:24:34 +00002525 llvm::DITypeArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
Adrian Prantld45ba252014-02-25 19:38:11 +00002526 return DBuilder.createSubroutineType(F, EltTypeArray);
2527 }
2528
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002529 return cast<llvm::MDSubroutineType>(getOrCreateType(FnType, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002530}
2531
2532/// EmitFunctionStart - Constructs the debug code for entering a function.
Eric Christophere7b87e52014-10-26 23:40:33 +00002533void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
2534 SourceLocation ScopeLoc, QualType FnType,
2535 llvm::Function *Fn, CGBuilderTy &Builder) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002536
2537 StringRef Name;
2538 StringRef LinkageName;
2539
2540 FnBeginRegionCount.push_back(LexicalBlockStack.size());
2541
2542 const Decl *D = GD.getDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002543 bool HasDecl = (D != nullptr);
Eric Christopher885c41b2014-04-01 22:25:28 +00002544
Guy Benyei11169dd2012-12-18 14:30:41 +00002545 unsigned Flags = 0;
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002546 llvm::MDFile *Unit = getOrCreateFile(Loc);
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002547 llvm::MDScope *FDContext = Unit;
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 llvm::DIArray TParamsArray;
2549 if (!HasDecl) {
2550 // Use llvm function name.
David Blaikieebe87e12013-08-27 23:57:18 +00002551 LinkageName = Fn->getName();
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002553 // If there is a subprogram for this function available then use it.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002554 auto FI = SPCache.find(FD->getCanonicalDecl());
Guy Benyei11169dd2012-12-18 14:30:41 +00002555 if (FI != SPCache.end()) {
Duncan P. N. Exon Smith87afdeb2015-04-14 03:24:14 +00002556 auto *SP = dyn_cast_or_null<llvm::MDSubprogram>(FI->second);
2557 if (SP && SP->isDefinition()) {
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002558 LexicalBlockStack.emplace_back(SP);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002559 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 return;
2561 }
2562 }
Frederic Riss9db79f12014-11-18 03:40:46 +00002563 collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext,
2564 TParamsArray, Flags);
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
2566 Name = getObjCMethodName(OMD);
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002567 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 } else {
2569 // Use llvm function name.
2570 Name = Fn->getName();
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002571 Flags |= llvm::DebugNode::FlagPrototyped;
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 }
2573 if (!Name.empty() && Name[0] == '\01')
2574 Name = Name.substr(1);
2575
Adrian Prantl42d71b92014-04-10 23:21:53 +00002576 if (!HasDecl || D->isImplicit()) {
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002577 Flags |= llvm::DebugNode::FlagArtificial;
Adrian Prantl42d71b92014-04-10 23:21:53 +00002578 // Artificial functions without a location should not silently reuse CurLoc.
2579 if (Loc.isInvalid())
2580 CurLoc = SourceLocation();
2581 }
2582 unsigned LineNo = getLineNumber(Loc);
2583 unsigned ScopeLine = getLineNumber(ScopeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002584
Eric Christopher8018e412014-03-27 18:50:35 +00002585 // FIXME: The function declaration we're constructing here is mostly reusing
2586 // declarations from CXXMethodDecl and not constructing new ones for arbitrary
2587 // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for
2588 // all subprograms instead of the actual context since subprogram definitions
2589 // are emitted as CU level entities by the backend.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002590 llvm::MDSubprogram *SP = DBuilder.createFunction(
Eric Christophere7b87e52014-10-26 23:40:33 +00002591 FDContext, Name, LinkageName, Unit, LineNo,
2592 getOrCreateFunctionType(D, FnType, Unit), Fn->hasInternalLinkage(),
2593 true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, Fn,
Duncan P. N. Exon Smithebad0aa2015-04-07 16:50:49 +00002594 TParamsArray.get(), getFunctionDeclaration(D));
Frederic Rissb1ab28c2014-11-05 19:19:04 +00002595 // We might get here with a VarDecl in the case we're generating
2596 // code for the initialization of globals. Do not record these decls
2597 // as they will overwrite the actual VarDecl Decl in the cache.
2598 if (HasDecl && isa<FunctionDecl>(D))
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002599 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(SP));
Guy Benyei11169dd2012-12-18 14:30:41 +00002600
Adrian Prantlbebb8932014-03-21 21:01:58 +00002601 // Push the function onto the lexical block stack.
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002602 LexicalBlockStack.emplace_back(SP);
Adrian Prantlbebb8932014-03-21 21:01:58 +00002603
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 if (HasDecl)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002605 RegionMap[D].reset(SP);
Guy Benyei11169dd2012-12-18 14:30:41 +00002606}
2607
2608/// EmitLocation - Emit metadata to indicate a change in line/column
Adrian Prantl02c0caa2013-07-18 00:27:59 +00002609/// information in the source file. If the location is invalid, the
2610/// previous location will be reused.
David Blaikie835afb22015-01-21 23:08:17 +00002611void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002612 // Update our current location
2613 setLocation(Loc);
2614
Eric Christophere7b87e52014-10-26 23:40:33 +00002615 if (CurLoc.isInvalid() || CurLoc.isMacroID())
2616 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002617
Adrian Prantle83b1302014-01-07 22:05:52 +00002618 llvm::MDNode *Scope = LexicalBlockStack.back();
Eric Christophere7b87e52014-10-26 23:40:33 +00002619 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
David Blaikie835afb22015-01-21 23:08:17 +00002620 getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope));
Guy Benyei11169dd2012-12-18 14:30:41 +00002621}
2622
2623/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2624/// the stack.
2625void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Duncan P. N. Exon Smitha66e3052014-12-09 19:22:40 +00002626 llvm::MDNode *Back = nullptr;
2627 if (!LexicalBlockStack.empty())
2628 Back = LexicalBlockStack.back().get();
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002629 LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002630 cast<llvm::MDScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00002631 getColumnNumber(CurLoc)));
Guy Benyei11169dd2012-12-18 14:30:41 +00002632}
2633
2634/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2635/// region - beginning of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002636void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
2637 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002638 // Set our current location.
2639 setLocation(Loc);
2640
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 // Emit a line table change for the current location inside the new scope.
Eric Christophere7b87e52014-10-26 23:40:33 +00002642 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(
2643 getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back()));
David Blaikie60a877b2014-10-22 19:34:33 +00002644
2645 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2646 return;
2647
2648 // Create a new lexical block and push it on the stack.
2649 CreateLexicalBlock(Loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002650}
2651
2652/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
2653/// region - end of a DW_TAG_lexical_block.
Eric Christopher0fdcb312013-05-16 00:52:20 +00002654void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
2655 SourceLocation Loc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2657
2658 // Provide an entry in the line table for the end of the block.
2659 EmitLocation(Builder, Loc);
2660
David Blaikie60a877b2014-10-22 19:34:33 +00002661 if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
2662 return;
2663
Guy Benyei11169dd2012-12-18 14:30:41 +00002664 LexicalBlockStack.pop_back();
2665}
2666
2667/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2668void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
2669 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2670 unsigned RCount = FnBeginRegionCount.back();
2671 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
2672
2673 // Pop all regions for this function.
David Blaikie60a877b2014-10-22 19:34:33 +00002674 while (LexicalBlockStack.size() != RCount) {
2675 // Provide an entry in the line table for the end of the block.
2676 EmitLocation(Builder, CurLoc);
2677 LexicalBlockStack.pop_back();
2678 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 FnBeginRegionCount.pop_back();
2680}
2681
Eric Christopherb2a008c2013-05-16 00:45:12 +00002682// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
Guy Benyei11169dd2012-12-18 14:30:41 +00002683// See BuildByRefType.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002684llvm::MDType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
2685 uint64_t *XOffset) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002686
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00002687 SmallVector<llvm::Metadata *, 5> EltTys;
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 QualType FType;
2689 uint64_t FieldSize, FieldOffset;
2690 unsigned FieldAlign;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002691
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002692 llvm::MDFile *Unit = getOrCreateFile(VD->getLocation());
Eric Christopherb2a008c2013-05-16 00:45:12 +00002693 QualType Type = VD->getType();
Guy Benyei11169dd2012-12-18 14:30:41 +00002694
2695 FieldOffset = 0;
2696 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
2697 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2698 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
2699 FType = CGM.getContext().IntTy;
2700 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2701 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2702
2703 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
2704 if (HasCopyAndDispose) {
2705 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002706 EltTys.push_back(
2707 CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
2708 EltTys.push_back(
2709 CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00002710 }
2711 bool HasByrefExtendedLayout;
2712 Qualifiers::ObjCLifetime Lifetime;
Eric Christophere7b87e52014-10-26 23:40:33 +00002713 if (CGM.getContext().getByrefLifetime(Type, Lifetime,
2714 HasByrefExtendedLayout) &&
2715 HasByrefExtendedLayout) {
Adrian Prantlead2ba42013-07-23 00:12:14 +00002716 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002717 EltTys.push_back(
2718 CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
Adrian Prantlead2ba42013-07-23 00:12:14 +00002719 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002720
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2722 if (Align > CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002723 CGM.getTarget().getPointerAlign(0))) {
2724 CharUnits FieldOffsetInBytes =
2725 CGM.getContext().toCharUnitsFromBits(FieldOffset);
2726 CharUnits AlignedOffsetInBytes =
2727 FieldOffsetInBytes.RoundUpToAlignment(Align);
2728 CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002729
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 if (NumPaddingBytes.isPositive()) {
2731 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
2732 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2733 pad, ArrayType::Normal, 0);
2734 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
2735 }
2736 }
Eric Christopherb2a008c2013-05-16 00:45:12 +00002737
Guy Benyei11169dd2012-12-18 14:30:41 +00002738 FType = Type;
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002739 llvm::MDType *FieldTy = getOrCreateType(FType, Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 FieldSize = CGM.getContext().getTypeSize(FType);
2741 FieldAlign = CGM.getContext().toBits(Align);
2742
Eric Christopherb2a008c2013-05-16 00:45:12 +00002743 *XOffset = FieldOffset;
Eric Christophere7b87e52014-10-26 23:40:33 +00002744 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize,
2745 FieldAlign, FieldOffset, 0, FieldTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 EltTys.push_back(FieldTy);
2747 FieldOffset += FieldSize;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002748
Guy Benyei11169dd2012-12-18 14:30:41 +00002749 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002750
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002751 unsigned Flags = llvm::DebugNode::FlagBlockByrefStruct;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002752
Guy Benyei11169dd2012-12-18 14:30:41 +00002753 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002754 nullptr, Elements);
Guy Benyei11169dd2012-12-18 14:30:41 +00002755}
2756
2757/// EmitDeclare - Emit local variable declaration debug info.
Duncan P. N. Exon Smithf796a1d2015-02-03 21:25:34 +00002758void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::dwarf::Tag Tag,
Eric Christophere7b87e52014-10-26 23:40:33 +00002759 llvm::Value *Storage, unsigned ArgNo,
2760 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002761 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
2763
David Blaikie7fceebf2013-08-19 03:37:48 +00002764 bool Unwritten =
2765 VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
2766 cast<Decl>(VD->getDeclContext())->isImplicit());
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002767 llvm::MDFile *Unit = nullptr;
David Blaikie7fceebf2013-08-19 03:37:48 +00002768 if (!Unwritten)
2769 Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002770 llvm::MDType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002771 uint64_t XOffset = 0;
2772 if (VD->hasAttr<BlocksAttr>())
2773 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002774 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 Ty = getOrCreateType(VD->getType(), Unit);
2776
2777 // If there is no debug info for this type then do not emit debug info
2778 // for this variable.
2779 if (!Ty)
2780 return;
2781
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 // Get location information.
David Blaikie7fceebf2013-08-19 03:37:48 +00002783 unsigned Line = 0;
2784 unsigned Column = 0;
2785 if (!Unwritten) {
2786 Line = getLineNumber(VD->getLocation());
2787 Column = getColumnNumber(VD->getLocation());
2788 }
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002789 SmallVector<int64_t, 9> Expr;
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 unsigned Flags = 0;
2791 if (VD->isImplicit())
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002792 Flags |= llvm::DebugNode::FlagArtificial;
Guy Benyei11169dd2012-12-18 14:30:41 +00002793 // If this is the first argument and it is implicit then
2794 // give it an object pointer flag.
2795 // FIXME: There has to be a better way to do this, but for static
2796 // functions there won't be an implicit param at arg1 and
2797 // otherwise it is 'self' or 'this'.
2798 if (isa<ImplicitParamDecl>(VD) && ArgNo == 1)
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00002799 Flags |= llvm::DebugNode::FlagObjectPointer;
David Blaikieb9c667d2013-06-19 21:53:53 +00002800 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage))
Eric Christopherffdeb1e2013-07-17 22:52:53 +00002801 if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() &&
2802 !VD->getType()->isPointerType())
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002803 Expr.push_back(llvm::dwarf::DW_OP_deref);
Guy Benyei11169dd2012-12-18 14:30:41 +00002804
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002805 auto *Scope = cast<llvm::MDScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00002806
2807 StringRef Name = VD->getName();
2808 if (!Name.empty()) {
2809 if (VD->hasAttr<BlocksAttr>()) {
2810 CharUnits offset = CharUnits::fromQuantity(32);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002811 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002812 // offset of __forwarding field
2813 offset = CGM.getContext().toCharUnitsFromBits(
Eric Christophere7b87e52014-10-26 23:40:33 +00002814 CGM.getTarget().getPointerWidth(0));
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002815 Expr.push_back(offset.getQuantity());
2816 Expr.push_back(llvm::dwarf::DW_OP_deref);
2817 Expr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002818 // offset of x field
2819 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002820 Expr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002821
2822 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002823 auto *D = DBuilder.createLocalVariable(Tag, Scope, VD->getName(), Unit,
2824 Line, Ty, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002825
Guy Benyei11169dd2012-12-18 14:30:41 +00002826 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002827 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2828 llvm::DebugLoc::get(Line, Column, Scope),
2829 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002830 return;
Adrian Prantl7f2ef222013-09-18 22:18:17 +00002831 } else if (isa<VariableArrayType>(VD->getType()))
Adrian Prantl7c6f9442015-01-19 17:51:58 +00002832 Expr.push_back(llvm::dwarf::DW_OP_deref);
David Blaikiea76a7c92013-01-05 05:58:35 +00002833 } else if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2834 // If VD is an anonymous union then Storage represents value for
2835 // all union fields.
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
David Blaikie219c7d92013-01-05 20:03:07 +00002837 if (RD->isUnion() && RD->isAnonymousStructOrUnion()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002838 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002839 llvm::MDType *FieldTy = getOrCreateType(Field->getType(), Unit);
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 StringRef FieldName = Field->getName();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002841
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 // Ignore unnamed fields. Do not ignore unnamed records.
2843 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2844 continue;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002845
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002847 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002848 Tag, Scope, FieldName, Unit, Line, FieldTy,
Eric Christophere7b87e52014-10-26 23:40:33 +00002849 CGM.getLangOpts().Optimize, Flags, ArgNo);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002850
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002852 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2853 llvm::DebugLoc::get(Line, Column, Scope),
2854 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 }
David Blaikie219c7d92013-01-05 20:03:07 +00002856 return;
Guy Benyei11169dd2012-12-18 14:30:41 +00002857 }
2858 }
David Blaikiea76a7c92013-01-05 05:58:35 +00002859
2860 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002861 auto *D =
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002862 DBuilder.createLocalVariable(Tag, Scope, Name, Unit, Line, Ty,
2863 CGM.getLangOpts().Optimize, Flags, ArgNo);
David Blaikiea76a7c92013-01-05 05:58:35 +00002864
2865 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002866 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
2867 llvm::DebugLoc::get(Line, Column, Scope),
2868 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002869}
2870
2871void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2872 llvm::Value *Storage,
2873 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002874 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002875 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2876}
2877
Adrian Prantlde17db32013-03-29 19:20:29 +00002878/// Look up the completed type for a self pointer in the TypeCache and
2879/// create a copy of it with the ObjectPointer and Artificial flags
2880/// set. If the type is not cached, a new one is created. This should
2881/// never happen though, since creating a type for the implicit self
2882/// argument implies that we already parsed the interface definition
2883/// and the ivar declarations in the implementation.
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002884llvm::MDType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
2885 llvm::MDType *Ty) {
2886 llvm::MDType *CachedTy = getTypeOrNull(QualTy);
Eric Christophere7b87e52014-10-26 23:40:33 +00002887 if (CachedTy)
2888 Ty = CachedTy;
Adrian Prantlde17db32013-03-29 19:20:29 +00002889 return DBuilder.createObjectPointerType(Ty);
2890}
2891
Eric Christophere7b87e52014-10-26 23:40:33 +00002892void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2893 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
Adrian Prantl88eec392014-11-21 00:35:25 +00002894 const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) {
Eric Christopher75e17682013-05-16 00:45:23 +00002895 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002896 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Eric Christopherb2a008c2013-05-16 00:45:12 +00002897
Craig Topper8a13c412014-05-21 05:09:00 +00002898 if (Builder.GetInsertBlock() == nullptr)
Guy Benyei11169dd2012-12-18 14:30:41 +00002899 return;
Eric Christopherb2a008c2013-05-16 00:45:12 +00002900
Guy Benyei11169dd2012-12-18 14:30:41 +00002901 bool isByRef = VD->hasAttr<BlocksAttr>();
Eric Christopherb2a008c2013-05-16 00:45:12 +00002902
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 uint64_t XOffset = 0;
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002904 llvm::MDFile *Unit = getOrCreateFile(VD->getLocation());
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00002905 llvm::MDType *Ty;
Guy Benyei11169dd2012-12-18 14:30:41 +00002906 if (isByRef)
2907 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002908 else
Guy Benyei11169dd2012-12-18 14:30:41 +00002909 Ty = getOrCreateType(VD->getType(), Unit);
2910
2911 // Self is passed along as an implicit non-arg variable in a
2912 // block. Mark it as the object pointer.
2913 if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self")
Adrian Prantlde17db32013-03-29 19:20:29 +00002914 Ty = CreateSelfType(VD->getType(), Ty);
Guy Benyei11169dd2012-12-18 14:30:41 +00002915
2916 // Get location information.
2917 unsigned Line = getLineNumber(VD->getLocation());
2918 unsigned Column = getColumnNumber(VD->getLocation());
2919
2920 const llvm::DataLayout &target = CGM.getDataLayout();
2921
2922 CharUnits offset = CharUnits::fromQuantity(
Eric Christophere7b87e52014-10-26 23:40:33 +00002923 target.getStructLayout(blockInfo.StructureType)
Guy Benyei11169dd2012-12-18 14:30:41 +00002924 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2925
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002926 SmallVector<int64_t, 9> addr;
Adrian Prantl0f6df002013-03-29 19:20:35 +00002927 if (isa<llvm::AllocaInst>(Storage))
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002928 addr.push_back(llvm::dwarf::DW_OP_deref);
2929 addr.push_back(llvm::dwarf::DW_OP_plus);
2930 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 if (isByRef) {
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002932 addr.push_back(llvm::dwarf::DW_OP_deref);
2933 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 // offset of __forwarding field
Eric Christophere7b87e52014-10-26 23:40:33 +00002935 offset =
2936 CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002937 addr.push_back(offset.getQuantity());
2938 addr.push_back(llvm::dwarf::DW_OP_deref);
2939 addr.push_back(llvm::dwarf::DW_OP_plus);
Guy Benyei11169dd2012-12-18 14:30:41 +00002940 // offset of x field
2941 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Duncan P. N. Exon Smithf3dc4292014-10-01 20:26:18 +00002942 addr.push_back(offset.getQuantity());
Guy Benyei11169dd2012-12-18 14:30:41 +00002943 }
2944
2945 // Create the descriptor for the variable.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00002946 auto *D = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00002947 llvm::dwarf::DW_TAG_auto_variable,
2948 cast<llvm::MDLocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
2949 Line, Ty);
Adrian Prantl0f6df002013-03-29 19:20:35 +00002950
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00002952 auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back());
2953 if (InsertPoint)
2954 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2955 InsertPoint);
2956 else
2957 DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL,
2958 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00002959}
2960
2961/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2962/// variable declaration.
2963void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
2964 unsigned ArgNo,
2965 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002966 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002967 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
2968}
2969
2970namespace {
Eric Christophere7b87e52014-10-26 23:40:33 +00002971struct BlockLayoutChunk {
2972 uint64_t OffsetInBits;
2973 const BlockDecl::Capture *Capture;
2974};
2975bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2976 return l.OffsetInBits < r.OffsetInBits;
2977}
Guy Benyei11169dd2012-12-18 14:30:41 +00002978}
2979
2980void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002981 llvm::Value *Arg,
David Blaikie77bbb5f2014-08-08 17:10:14 +00002982 unsigned ArgNo,
Adrian Prantl51936dd2013-03-14 17:53:33 +00002983 llvm::Value *LocalAddr,
Guy Benyei11169dd2012-12-18 14:30:41 +00002984 CGBuilderTy &Builder) {
Eric Christopher75e17682013-05-16 00:45:23 +00002985 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 ASTContext &C = CGM.getContext();
2987 const BlockDecl *blockDecl = block.getBlockDecl();
2988
2989 // Collect some general information about the block's location.
2990 SourceLocation loc = blockDecl->getCaretLocation();
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00002991 llvm::MDFile *tunit = getOrCreateFile(loc);
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 unsigned line = getLineNumber(loc);
2993 unsigned column = getColumnNumber(loc);
Eric Christopherb2a008c2013-05-16 00:45:12 +00002994
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 // Build the debug-info type for the block literal.
2996 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
2997
2998 const llvm::StructLayout *blockLayout =
Eric Christophere7b87e52014-10-26 23:40:33 +00002999 CGM.getDataLayout().getStructLayout(block.StructureType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003000
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003001 SmallVector<llvm::Metadata *, 16> fields;
Guy Benyei11169dd2012-12-18 14:30:41 +00003002 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
3003 blockLayout->getElementOffsetInBits(0),
3004 tunit, tunit));
3005 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
3006 blockLayout->getElementOffsetInBits(1),
3007 tunit, tunit));
3008 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
3009 blockLayout->getElementOffsetInBits(2),
3010 tunit, tunit));
Adrian Prantl65d5d002014-11-05 01:01:30 +00003011 auto *FnTy = block.getBlockExpr()->getFunctionType();
3012 auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
3013 fields.push_back(createFieldType("__FuncPtr", FnPtrType, 0, loc, AS_public,
Guy Benyei11169dd2012-12-18 14:30:41 +00003014 blockLayout->getElementOffsetInBits(3),
3015 tunit, tunit));
Eric Christophere7b87e52014-10-26 23:40:33 +00003016 fields.push_back(createFieldType(
3017 "__descriptor", C.getPointerType(block.NeedsCopyDispose
3018 ? C.getBlockDescriptorExtendedType()
3019 : C.getBlockDescriptorType()),
3020 0, loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit));
Guy Benyei11169dd2012-12-18 14:30:41 +00003021
3022 // We want to sort the captures by offset, not because DWARF
3023 // requires this, but because we're paranoid about debuggers.
3024 SmallVector<BlockLayoutChunk, 8> chunks;
3025
3026 // 'this' capture.
3027 if (blockDecl->capturesCXXThis()) {
3028 BlockLayoutChunk chunk;
3029 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003030 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
Craig Topper8a13c412014-05-21 05:09:00 +00003031 chunk.Capture = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 chunks.push_back(chunk);
3033 }
3034
3035 // Variable captures.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003036 for (const auto &capture : blockDecl->captures()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 const VarDecl *variable = capture.getVariable();
3038 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
3039
3040 // Ignore constant captures.
3041 if (captureInfo.isConstant())
3042 continue;
3043
3044 BlockLayoutChunk chunk;
3045 chunk.OffsetInBits =
Eric Christophere7b87e52014-10-26 23:40:33 +00003046 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
Guy Benyei11169dd2012-12-18 14:30:41 +00003047 chunk.Capture = &capture;
3048 chunks.push_back(chunk);
3049 }
3050
3051 // Sort by offset.
3052 llvm::array_pod_sort(chunks.begin(), chunks.end());
3053
Eric Christophere7b87e52014-10-26 23:40:33 +00003054 for (SmallVectorImpl<BlockLayoutChunk>::iterator i = chunks.begin(),
3055 e = chunks.end();
3056 i != e; ++i) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003057 uint64_t offsetInBits = i->OffsetInBits;
3058 const BlockDecl::Capture *capture = i->Capture;
3059
3060 // If we have a null capture, this must be the C++ 'this' capture.
3061 if (!capture) {
3062 const CXXMethodDecl *method =
Eric Christophere7b87e52014-10-26 23:40:33 +00003063 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 QualType type = method->getThisType(C);
3065
3066 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
3067 offsetInBits, tunit, tunit));
3068 continue;
3069 }
3070
3071 const VarDecl *variable = capture->getVariable();
3072 StringRef name = variable->getName();
3073
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003074 llvm::MDType *fieldType;
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 if (capture->isByRef()) {
David Majnemer34b57492014-07-30 01:30:47 +00003076 TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy);
Guy Benyei11169dd2012-12-18 14:30:41 +00003077
3078 // FIXME: this creates a second copy of this type!
3079 uint64_t xoffset;
3080 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
David Majnemer34b57492014-07-30 01:30:47 +00003081 fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
3082 fieldType =
3083 DBuilder.createMemberType(tunit, name, tunit, line, PtrInfo.Width,
3084 PtrInfo.Align, offsetInBits, 0, fieldType);
Guy Benyei11169dd2012-12-18 14:30:41 +00003085 } else {
Eric Christophere7b87e52014-10-26 23:40:33 +00003086 fieldType = createFieldType(name, variable->getType(), 0, loc, AS_public,
3087 offsetInBits, tunit, tunit);
Guy Benyei11169dd2012-12-18 14:30:41 +00003088 }
3089 fields.push_back(fieldType);
3090 }
3091
3092 SmallString<36> typeName;
Eric Christophere7b87e52014-10-26 23:40:33 +00003093 llvm::raw_svector_ostream(typeName) << "__block_literal_"
3094 << CGM.getUniqueBlockCount();
Guy Benyei11169dd2012-12-18 14:30:41 +00003095
3096 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
3097
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003098 llvm::MDType *type = DBuilder.createStructType(
3099 tunit, typeName.str(), tunit, line,
3100 CGM.getContext().toBits(block.BlockSize),
3101 CGM.getContext().toBits(block.BlockAlign), 0, nullptr, fieldsArray);
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
3103
3104 // Get overall information about the block.
Duncan P. N. Exon Smith526ab072015-04-16 01:53:23 +00003105 unsigned flags = llvm::DebugNode::FlagArtificial;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003106 auto *scope = cast<llvm::MDLocalScope>(LexicalBlockStack.back());
Guy Benyei11169dd2012-12-18 14:30:41 +00003107
3108 // Create the descriptor for the parameter.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003109 auto *debugVar = DBuilder.createLocalVariable(
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003110 llvm::dwarf::DW_TAG_arg_variable, scope, Arg->getName(), tunit, line,
3111 type, CGM.getLangOpts().Optimize, flags, ArgNo);
Adrian Prantl51936dd2013-03-14 17:53:33 +00003112
Adrian Prantl616bef42013-03-14 21:52:59 +00003113 if (LocalAddr) {
Adrian Prantl51936dd2013-03-14 17:53:33 +00003114 // Insert an llvm.dbg.value into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003115 DBuilder.insertDbgValueIntrinsic(
Eric Christophere7b87e52014-10-26 23:40:33 +00003116 LocalAddr, 0, debugVar, DBuilder.createExpression(),
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003117 llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock());
Adrian Prantl616bef42013-03-14 21:52:59 +00003118 }
Adrian Prantl51936dd2013-03-14 17:53:33 +00003119
Adrian Prantl616bef42013-03-14 21:52:59 +00003120 // Insert an llvm.dbg.declare into the current block.
Duncan P. N. Exon Smithfe88b482015-04-15 21:18:30 +00003121 DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(),
3122 llvm::DebugLoc::get(line, column, scope),
3123 Builder.GetInsertBlock());
Guy Benyei11169dd2012-12-18 14:30:41 +00003124}
3125
David Blaikie6943dea2013-08-20 01:28:15 +00003126/// If D is an out-of-class definition of a static data member of a class, find
3127/// its corresponding in-class declaration.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003128llvm::MDDerivedType *
David Blaikie6943dea2013-08-20 01:28:15 +00003129CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
3130 if (!D->isStaticDataMember())
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003131 return nullptr;
Saleem Abdulrasoolcd187f02015-02-28 00:13:13 +00003132
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003133 auto MI = StaticDataMemberCache.find(D->getCanonicalDecl());
David Blaikie6943dea2013-08-20 01:28:15 +00003134 if (MI != StaticDataMemberCache.end()) {
3135 assert(MI->second && "Static data member declaration should still exist");
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003136 return cast<llvm::MDDerivedType>(MI->second);
Evgeniy Stepanov37b3f732013-08-16 10:35:31 +00003137 }
David Blaikiece763042013-08-20 21:49:21 +00003138
3139 // If the member wasn't found in the cache, lazily construct and add it to the
3140 // type (used when a limited form of the type is emitted).
Adrian Prantl21361fb2014-08-29 22:44:27 +00003141 auto DC = D->getDeclContext();
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003142 auto *Ctxt =
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003143 cast<llvm::MDCompositeType>(getContextDescriptor(cast<Decl>(DC)));
Adrian Prantl21361fb2014-08-29 22:44:27 +00003144 return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
David Blaikie6943dea2013-08-20 01:28:15 +00003145}
3146
Eric Christophercab9fae2014-04-10 05:20:00 +00003147/// Recursively collect all of the member fields of a global anonymous decl and
3148/// create static variables for them. The first time this is called it needs
3149/// to be on a union and then from there we can have additional unnamed fields.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003150llvm::MDGlobalVariable *CGDebugInfo::CollectAnonRecordDecls(
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003151 const RecordDecl *RD, llvm::MDFile *Unit, unsigned LineNo,
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003152 StringRef LinkageName, llvm::GlobalVariable *Var, llvm::MDScope *DContext) {
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003153 llvm::MDGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003154
3155 for (const auto *Field : RD->fields()) {
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003156 llvm::MDType *FieldTy = getOrCreateType(Field->getType(), Unit);
Eric Christophercab9fae2014-04-10 05:20:00 +00003157 StringRef FieldName = Field->getName();
3158
3159 // Ignore unnamed fields, but recurse into anonymous records.
3160 if (FieldName.empty()) {
3161 const RecordType *RT = dyn_cast<RecordType>(Field->getType());
3162 if (RT)
3163 GV = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName,
3164 Var, DContext);
3165 continue;
3166 }
3167 // Use VarDecl's Tag, Scope and Line number.
Duncan P. N. Exon Smithc09c5482015-04-20 21:17:26 +00003168 GV = DBuilder.createGlobalVariable(DContext, FieldName, LinkageName, Unit,
3169 LineNo, FieldTy,
3170 Var->hasInternalLinkage(), Var, nullptr);
Eric Christophercab9fae2014-04-10 05:20:00 +00003171 }
3172 return GV;
3173}
3174
Guy Benyei11169dd2012-12-18 14:30:41 +00003175/// EmitGlobalVariable - Emit information about a global variable.
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003176void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 const VarDecl *D) {
Eric Christopher75e17682013-05-16 00:45:23 +00003178 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Guy Benyei11169dd2012-12-18 14:30:41 +00003179 // Create global variable debug descriptor.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003180 llvm::MDFile *Unit = nullptr;
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003181 llvm::MDScope *DContext = nullptr;
Frederic Riss9db79f12014-11-18 03:40:46 +00003182 unsigned LineNo;
3183 StringRef DeclName, LinkageName;
3184 QualType T;
3185 collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext);
Eric Christophercab9fae2014-04-10 05:20:00 +00003186
3187 // Attempt to store one global variable for the declaration - even if we
3188 // emit a lot of fields.
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003189 llvm::MDGlobalVariable *GV = nullptr;
Eric Christophercab9fae2014-04-10 05:20:00 +00003190
3191 // If this is an anonymous union then we'll want to emit a global
3192 // variable for each member of the anonymous union so that it's possible
3193 // to find the name of any field in the union.
3194 if (T->isUnionType() && DeclName.empty()) {
3195 const RecordDecl *RD = cast<RecordType>(T)->getDecl();
Eric Christophere7b87e52014-10-26 23:40:33 +00003196 assert(RD->isAnonymousStructOrUnion() &&
3197 "unnamed non-anonymous struct or union?");
Eric Christophercab9fae2014-04-10 05:20:00 +00003198 GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext);
3199 } else {
David Blaikie7550b112014-10-20 17:42:23 +00003200 GV = DBuilder.createGlobalVariable(
Eric Christophercab9fae2014-04-10 05:20:00 +00003201 DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
3202 Var->hasInternalLinkage(), Var,
3203 getOrCreateStaticDataMemberDeclarationOrNull(D));
3204 }
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003205 DeclCache[D->getCanonicalDecl()].reset(static_cast<llvm::Metadata *>(GV));
Guy Benyei11169dd2012-12-18 14:30:41 +00003206}
3207
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003208/// EmitGlobalVariable - Emit global variable's debug info.
3209void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
3210 llvm::Constant *Init) {
Eric Christopher75e17682013-05-16 00:45:23 +00003211 assert(DebugKind >= CodeGenOptions::LimitedDebugInfo);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003212 // Create the descriptor for the variable.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003213 llvm::MDFile *Unit = getOrCreateFile(VD->getLocation());
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003214 StringRef Name = VD->getName();
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003215 llvm::MDType *Ty = getOrCreateType(VD->getType(), Unit);
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003216 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
3217 const EnumDecl *ED = cast<EnumDecl>(ECD->getDeclContext());
3218 assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?");
3219 Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit);
3220 }
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003221 // Do not use global variables for enums.
3222 //
3223 // FIXME: why not?
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003224 if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type)
Yunzhong Gao0ebf1bb2013-08-30 08:53:09 +00003225 return;
David Blaikiea15565562014-04-04 20:56:17 +00003226 // Do not emit separate definitions for function local const/statics.
3227 if (isa<FunctionDecl>(VD->getDeclContext()))
3228 return;
David Blaikiebb113912014-04-05 07:23:17 +00003229 VD = cast<ValueDecl>(VD->getCanonicalDecl());
David Blaikie423eb5a2014-11-19 19:42:40 +00003230 auto *VarD = cast<VarDecl>(VD);
David Blaikieaf080852014-11-21 00:20:58 +00003231 if (VarD->isStaticDataMember()) {
3232 auto *RD = cast<RecordDecl>(VarD->getDeclContext());
3233 getContextDescriptor(RD);
David Blaikie423eb5a2014-11-19 19:42:40 +00003234 // Ensure that the type is retained even though it's otherwise unreferenced.
3235 RetainedTypes.push_back(
David Blaikieaf080852014-11-21 00:20:58 +00003236 CGM.getContext().getRecordType(RD).getAsOpaquePtr());
David Blaikie423eb5a2014-11-19 19:42:40 +00003237 return;
3238 }
3239
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003240 llvm::MDScope *DContext =
David Blaikieaf080852014-11-21 00:20:58 +00003241 getContextDescriptor(dyn_cast<Decl>(VD->getDeclContext()));
3242
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003243 auto &GV = DeclCache[VD];
3244 if (GV)
David Blaikiebb113912014-04-05 07:23:17 +00003245 return;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003246 GV.reset(DBuilder.createGlobalVariable(
David Blaikie506a7452014-04-05 07:46:57 +00003247 DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003248 true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)));
David Blaikiebd483762013-05-20 04:58:53 +00003249}
3250
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003251llvm::MDScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
David Blaikiebd483762013-05-20 04:58:53 +00003252 if (!LexicalBlockStack.empty())
Duncan P. N. Exon Smithfc8d9d92015-04-20 18:32:15 +00003253 return LexicalBlockStack.back();
David Blaikiebd483762013-05-20 04:58:53 +00003254 return getContextDescriptor(D);
Guy Benyei11169dd2012-12-18 14:30:41 +00003255}
3256
David Blaikie9f88fe82013-04-22 06:13:21 +00003257void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
David Blaikiebd483762013-05-20 04:58:53 +00003258 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3259 return;
David Blaikie9f88fe82013-04-22 06:13:21 +00003260 DBuilder.createImportedModule(
David Blaikiebd483762013-05-20 04:58:53 +00003261 getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
3262 getOrCreateNameSpace(UD.getNominatedNamespace()),
David Blaikie9f88fe82013-04-22 06:13:21 +00003263 getLineNumber(UD.getLocation()));
3264}
3265
David Blaikiebd483762013-05-20 04:58:53 +00003266void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) {
3267 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3268 return;
3269 assert(UD.shadow_size() &&
3270 "We shouldn't be codegening an invalid UsingDecl containing no decls");
3271 // Emitting one decl is sufficient - debuggers can detect that this is an
3272 // overloaded name & provide lookup for all the overloads.
3273 const UsingShadowDecl &USD = **UD.shadow_begin();
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003274 if (llvm::DebugNode *Target =
Eric Christopher1ecc5632013-06-07 22:54:39 +00003275 getDeclarationOrDefinition(USD.getUnderlyingDecl()))
David Blaikiebd483762013-05-20 04:58:53 +00003276 DBuilder.createImportedDeclaration(
3277 getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
3278 getLineNumber(USD.getLocation()));
3279}
3280
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003281llvm::MDImportedEntity *
David Blaikief121b932013-05-20 22:50:41 +00003282CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
3283 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003284 return nullptr;
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003285 auto &VH = NamespaceAliasCache[&NA];
David Blaikief121b932013-05-20 22:50:41 +00003286 if (VH)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003287 return cast<llvm::MDImportedEntity>(VH);
Duncan P. N. Exon Smithdadc2b62015-04-21 18:43:54 +00003288 llvm::MDImportedEntity *R;
David Blaikief121b932013-05-20 22:50:41 +00003289 if (const NamespaceAliasDecl *Underlying =
3290 dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
3291 // This could cache & dedup here rather than relying on metadata deduping.
David Blaikie551fb0a2014-04-06 06:30:03 +00003292 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003293 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3294 EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()),
3295 NA.getName());
3296 else
David Blaikie551fb0a2014-04-06 06:30:03 +00003297 R = DBuilder.createImportedDeclaration(
David Blaikief121b932013-05-20 22:50:41 +00003298 getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
3299 getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
3300 getLineNumber(NA.getLocation()), NA.getName());
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003301 VH.reset(R);
David Blaikief121b932013-05-20 22:50:41 +00003302 return R;
3303}
3304
Guy Benyei11169dd2012-12-18 14:30:41 +00003305/// getOrCreateNamesSpace - Return namespace descriptor for the given
3306/// namespace decl.
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003307llvm::MDNamespace *
Guy Benyei11169dd2012-12-18 14:30:41 +00003308CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
David Blaikie9fdedec2013-08-16 22:52:07 +00003309 NSDecl = NSDecl->getCanonicalDecl();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003310 auto I = NameSpaceCache.find(NSDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00003311 if (I != NameSpaceCache.end())
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003312 return cast<llvm::MDNamespace>(I->second);
Eric Christopherb2a008c2013-05-16 00:45:12 +00003313
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003315 llvm::MDFile *FileD = getOrCreateFile(NSDecl->getLocation());
Duncan P. N. Exon Smith4078ad42015-04-16 16:36:45 +00003316 llvm::MDScope *Context =
3317 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Duncan P. N. Exon Smitha7fbcbf2015-04-20 22:09:57 +00003318 llvm::MDNamespace *NS =
3319 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003320 NameSpaceCache[NSDecl].reset(NS);
Guy Benyei11169dd2012-12-18 14:30:41 +00003321 return NS;
3322}
3323
3324void CGDebugInfo::finalize() {
David Blaikie87dab872014-05-07 16:56:58 +00003325 // Creating types might create further types - invalidating the current
3326 // element and the size(), so don't cache/reference them.
3327 for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) {
3328 ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i];
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003329 llvm::MDType *Ty = E.Type->getDecl()->getDefinition()
3330 ? CreateTypeDefinition(E.Type, E.Unit)
3331 : E.Decl;
3332 DBuilder.replaceTemporary(llvm::TempMDType(E.Decl), Ty);
David Blaikie87dab872014-05-07 16:56:58 +00003333 }
3334
David Blaikief427b002014-05-06 03:42:01 +00003335 for (auto p : ReplaceMap) {
3336 assert(p.second);
Duncan P. N. Exon Smith4caa7f22015-04-16 01:00:56 +00003337 auto *Ty = cast<llvm::MDType>(p.second);
3338 assert(Ty->isForwardDecl());
Eric Christopherb2a008c2013-05-16 00:45:12 +00003339
David Blaikief427b002014-05-06 03:42:01 +00003340 auto it = TypeCache.find(p.first);
David Blaikieb8149042014-05-05 21:21:39 +00003341 assert(it != TypeCache.end());
3342 assert(it->second);
Adrian Prantl73409ce2013-03-11 18:33:46 +00003343
Duncan P. N. Exon Smith497d4d462015-04-11 19:05:04 +00003344 DBuilder.replaceTemporary(llvm::TempMDType(Ty),
3345 cast<llvm::MDType>(it->second));
Guy Benyei11169dd2012-12-18 14:30:41 +00003346 }
Adrian Prantl73409ce2013-03-11 18:33:46 +00003347
Frederic Rissd253ed62014-11-18 03:40:51 +00003348 for (const auto &p : FwdDeclReplaceMap) {
3349 assert(p.second);
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003350 llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second));
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003351 llvm::Metadata *Repl;
Frederic Rissd253ed62014-11-18 03:40:51 +00003352
3353 auto it = DeclCache.find(p.first);
Adrian Prantl97f76852014-12-19 01:02:11 +00003354 // If there has been no definition for the declaration, call RAUW
Frederic Rissd253ed62014-11-18 03:40:51 +00003355 // with ourselves, that will destroy the temporary MDNode and
3356 // replace it with a standard one, avoiding leaking memory.
3357 if (it == DeclCache.end())
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003358 Repl = p.second;
Frederic Rissd253ed62014-11-18 03:40:51 +00003359 else
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00003360 Repl = it->second;
Frederic Rissdce60a72014-11-19 18:53:46 +00003361
Duncan P. N. Exon Smithd899f6e2015-04-18 00:07:30 +00003362 DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
Frederic Rissd253ed62014-11-18 03:40:51 +00003363 }
3364
Adrian Prantl73409ce2013-03-11 18:33:46 +00003365 // We keep our own list of retained types, because we need to look
3366 // up the final type in the type cache.
3367 for (std::vector<void *>::const_iterator RI = RetainedTypes.begin(),
3368 RE = RetainedTypes.end(); RI != RE; ++RI)
Duncan P. N. Exon Smithc7551282015-04-06 23:21:33 +00003369 DBuilder.retainType(cast<llvm::MDType>(TypeCache[*RI]));
Adrian Prantl73409ce2013-03-11 18:33:46 +00003370
Guy Benyei11169dd2012-12-18 14:30:41 +00003371 DBuilder.finalize();
3372}
David Blaikie66088d52014-09-24 17:01:27 +00003373
3374void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
3375 if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo)
3376 return;
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003377
Duncan P. N. Exon Smith0b6c3692015-04-20 18:51:48 +00003378 if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile()))
Duncan P. N. Exon Smith5043f912015-03-27 22:58:05 +00003379 // Don't ignore in case of explicit cast where it is referenced indirectly.
3380 DBuilder.retainType(DieTy);
David Blaikie66088d52014-09-24 17:01:27 +00003381}